1 | use warnings; |
---|
2 | use strict; |
---|
3 | |
---|
4 | =head1 NAME |
---|
5 | |
---|
6 | BarnOwl::Message::Jabber |
---|
7 | |
---|
8 | =head1 DESCRIPTION |
---|
9 | |
---|
10 | A subclass of BarnOwl::Message for Jabber messages |
---|
11 | |
---|
12 | =cut |
---|
13 | |
---|
14 | package BarnOwl::Message::Jabber; |
---|
15 | |
---|
16 | use base qw( BarnOwl::Message ); |
---|
17 | |
---|
18 | sub jtype { shift->{jtype} }; |
---|
19 | sub from { shift->{from} }; |
---|
20 | sub to { shift->{to} }; |
---|
21 | sub room { shift->{room} }; |
---|
22 | sub subject { shift->{subject} }; |
---|
23 | sub status { shift->{status} } |
---|
24 | |
---|
25 | sub login_type { |
---|
26 | my $self = shift; |
---|
27 | my $type = $self->jtype; |
---|
28 | return " ($type)" if $type; |
---|
29 | } |
---|
30 | |
---|
31 | sub login_extra { |
---|
32 | my $self = shift; |
---|
33 | my $show = $self->{show}; |
---|
34 | my $status = $self->status; |
---|
35 | my $s = ""; |
---|
36 | $s .= $show if $show; |
---|
37 | $s .= ", $status" if $status; |
---|
38 | return $s; |
---|
39 | } |
---|
40 | |
---|
41 | sub long_sender { |
---|
42 | my $self = shift; |
---|
43 | return $self->from; |
---|
44 | } |
---|
45 | |
---|
46 | sub context { |
---|
47 | return shift->room; |
---|
48 | } |
---|
49 | |
---|
50 | sub subcontext { |
---|
51 | return shift->subject || ""; |
---|
52 | } |
---|
53 | |
---|
54 | sub personal_context { |
---|
55 | my $self = shift; |
---|
56 | if ($self->is_incoming) { |
---|
57 | return "to " . $self->to; |
---|
58 | } else { |
---|
59 | return "from " . $self->from; |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | sub smartfilter { |
---|
64 | my $self = shift; |
---|
65 | my $inst = shift; |
---|
66 | |
---|
67 | my $filter; |
---|
68 | |
---|
69 | if($self->jtype eq 'chat') { |
---|
70 | my $user; |
---|
71 | if($self->direction eq 'in') { |
---|
72 | $user = $self->from; |
---|
73 | } else { |
---|
74 | $user = $self->to; |
---|
75 | } |
---|
76 | return smartfilter_user($user, $inst); |
---|
77 | } elsif ($self->jtype eq 'groupchat') { |
---|
78 | my $room = $self->room; |
---|
79 | $filter = "jabber-room-$room"; |
---|
80 | BarnOwl::command(qw[filter], $filter, |
---|
81 | qw[type ^jabber$ and room], "^\Q$room\E\$"); |
---|
82 | return $filter; |
---|
83 | } elsif ($self->login ne 'none') { |
---|
84 | return smartfilter_user($self->from, $inst); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | sub smartfilter_user { |
---|
89 | my $user = shift; |
---|
90 | my $inst = shift; |
---|
91 | |
---|
92 | $user = Net::Jabber::JID->new($user)->GetJID( $inst ? 'full' : 'base' ); |
---|
93 | my $filter = "jabber-user-$user"; |
---|
94 | BarnOwl::command(qw[filter], $filter, qw[type ^jabber$], |
---|
95 | qw[and ( ( direction ^in$ and from], "^\Q$user\E(/.*)?\$", |
---|
96 | qw[) or ( direction ^out$ and to ], "^\Q$user\E(/.*)?\$", |
---|
97 | qw[ ) ) ]); |
---|
98 | return $filter; |
---|
99 | |
---|
100 | } |
---|
101 | |
---|
102 | sub replycmd { |
---|
103 | my $self = shift; |
---|
104 | my ($recip, $account, $subject); |
---|
105 | if ($self->is_loginout) { |
---|
106 | $recip = $self->sender; |
---|
107 | $account = $self->recipient; |
---|
108 | } elsif ($self->jtype eq 'chat') { |
---|
109 | return $self->replysendercmd; |
---|
110 | } elsif ($self->jtype eq 'groupchat') { |
---|
111 | $recip = $self->room; |
---|
112 | if ($self->is_incoming) { |
---|
113 | $account = $self->to; |
---|
114 | } else { |
---|
115 | $account = $self->from; |
---|
116 | } |
---|
117 | $subject = $self->subject; |
---|
118 | } else { |
---|
119 | # Unknown type |
---|
120 | return; |
---|
121 | } |
---|
122 | return jwrite_cmd($recip, $account, $subject); |
---|
123 | } |
---|
124 | |
---|
125 | sub replysendercmd { |
---|
126 | my $self = shift; |
---|
127 | if($self->jtype eq 'groupchat' |
---|
128 | || $self->jtype eq 'chat') { |
---|
129 | my ($recip, $account); |
---|
130 | if ($self->is_incoming) { |
---|
131 | $recip = $self->from; |
---|
132 | $account = $self->to; |
---|
133 | } else { |
---|
134 | $recip = $self->to; |
---|
135 | $account = $self->from; |
---|
136 | } |
---|
137 | return jwrite_cmd($recip, $account); |
---|
138 | } |
---|
139 | return $self->replycmd; |
---|
140 | } |
---|
141 | |
---|
142 | sub jwrite_cmd { |
---|
143 | my ($recip, $account, $subject) = @_; |
---|
144 | if (defined $recip) { |
---|
145 | $recip = BarnOwl::quote($recip); |
---|
146 | $account = BarnOwl::quote($account); |
---|
147 | my $cmd = "jwrite $recip -a $account"; |
---|
148 | if (defined $subject) { |
---|
149 | $cmd .= " -s " . BarnOwl::quote($subject); |
---|
150 | } |
---|
151 | return $cmd; |
---|
152 | } else { |
---|
153 | return undef; |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | =head1 SEE ALSO |
---|
158 | |
---|
159 | L<BarnOwl::Message> |
---|
160 | |
---|
161 | =cut |
---|
162 | |
---|
163 | 1; |
---|