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