1 | # -*- mode: cperl; cperl-indent-level: 4; indent-tabs-mode: nil -*- |
---|
2 | package BarnOwl::Jabber; |
---|
3 | use warnings; |
---|
4 | use strict; |
---|
5 | |
---|
6 | use Authen::SASL qw(Perl); |
---|
7 | use Net::Jabber; |
---|
8 | use Net::Jabber::MUC; |
---|
9 | use Net::DNS; |
---|
10 | use Getopt::Long; |
---|
11 | |
---|
12 | no warnings 'redefine'; |
---|
13 | |
---|
14 | ################################################################################ |
---|
15 | # owl perl jabber support |
---|
16 | # |
---|
17 | # XXX Todo: |
---|
18 | # Rosters for MUCs |
---|
19 | # More user feedback |
---|
20 | # * joining MUC |
---|
21 | # * parting MUC |
---|
22 | # * presence (Roster and MUC) |
---|
23 | # Implementing formatting and logging callbacks for C |
---|
24 | # Appropriate callbacks for presence subscription messages. |
---|
25 | # |
---|
26 | ################################################################################ |
---|
27 | |
---|
28 | |
---|
29 | ################################################################################ |
---|
30 | ################################################################################ |
---|
31 | package BarnOwl::Jabber::Connection; |
---|
32 | |
---|
33 | use base qw(Net::Jabber::Client); |
---|
34 | |
---|
35 | sub new { |
---|
36 | my $class = shift; |
---|
37 | |
---|
38 | my %args = (); |
---|
39 | if(BarnOwl::getvar('debug') eq 'on') { |
---|
40 | $args{debuglevel} = 1; |
---|
41 | $args{debugfile} = 'jabber.log'; |
---|
42 | } |
---|
43 | my $self = $class->SUPER::new(%args); |
---|
44 | $self->{_BARNOWL_MUCS} = []; |
---|
45 | return $self; |
---|
46 | } |
---|
47 | |
---|
48 | =head2 MUCJoin |
---|
49 | |
---|
50 | Extends MUCJoin to keep track of the MUCs we're joined to as |
---|
51 | Net::Jabber::MUC objects. Takes the same arguments as |
---|
52 | L<Net::Jabber::MUC/new> and L<Net::Jabber::MUC/Connect> |
---|
53 | |
---|
54 | =cut |
---|
55 | |
---|
56 | sub MUCJoin { |
---|
57 | my $self = shift; |
---|
58 | my $muc = Net::Jabber::MUC->new(connection => $self, @_); |
---|
59 | $muc->Join(@_); |
---|
60 | push @{$self->MUCs}, $muc; |
---|
61 | } |
---|
62 | |
---|
63 | =head2 MUCLeave ARGS |
---|
64 | |
---|
65 | Leave a MUC. The MUC is specified in the same form as L</FindMUC> |
---|
66 | |
---|
67 | =cut |
---|
68 | |
---|
69 | sub MUCLeave { |
---|
70 | my $self = shift; |
---|
71 | my $muc = $self->FindMUC(@_); |
---|
72 | return unless $muc; |
---|
73 | |
---|
74 | $muc->Leave(); |
---|
75 | $self->{_BARNOWL_MUCS} = [grep {$_->BaseJID ne $muc->BaseJID} $self->MUCs]; |
---|
76 | } |
---|
77 | |
---|
78 | =head2 FindMUC ARGS |
---|
79 | |
---|
80 | Return the Net::Jabber::MUC object representing a specific MUC we're |
---|
81 | joined to, undef if it doesn't exists. ARGS can be either JID => $JID, |
---|
82 | or Room => $room, Server => $server. |
---|
83 | |
---|
84 | =cut |
---|
85 | |
---|
86 | sub FindMUC { |
---|
87 | my $self = shift; |
---|
88 | |
---|
89 | my %args; |
---|
90 | while($#_ >= 0) { $args{ lc(pop(@_)) } = pop(@_); } |
---|
91 | |
---|
92 | my $jid; |
---|
93 | if($args{jid}) { |
---|
94 | $jid = $args{jid}; |
---|
95 | } elsif($args{room} && $args{server}) { |
---|
96 | $jid = Net::Jabber::JID->new(userid => $args{room}, |
---|
97 | server => $args{server}); |
---|
98 | } |
---|
99 | $jid = $jid->GetJID('base') if UNIVERSAL::isa($jid, 'Net::XMPP::JID'); |
---|
100 | |
---|
101 | foreach my $muc ($self->MUCs) { |
---|
102 | return $muc if $muc->BaseJID eq $jid; |
---|
103 | } |
---|
104 | return undef; |
---|
105 | } |
---|
106 | |
---|
107 | =head2 MUCs |
---|
108 | |
---|
109 | Returns a list (or arrayref in scalar context) of Net::Jabber::MUC |
---|
110 | objects we believe ourself to be connected to. |
---|
111 | |
---|
112 | =cut |
---|
113 | |
---|
114 | sub MUCs { |
---|
115 | my $self = shift; |
---|
116 | my $mucs = $self->{_BARNOWL_MUCS}; |
---|
117 | return wantarray ? @$mucs : $mucs; |
---|
118 | } |
---|
119 | |
---|
120 | ################################################################################ |
---|
121 | ################################################################################ |
---|
122 | package BarnOwl::Jabber::ConnectionManager; |
---|
123 | sub new { |
---|
124 | my $class = shift; |
---|
125 | return bless { }, $class; |
---|
126 | } |
---|
127 | |
---|
128 | sub addConnection { |
---|
129 | my $self = shift; |
---|
130 | my $jidStr = shift; |
---|
131 | |
---|
132 | my $client = BarnOwl::Jabber::Connection->new; |
---|
133 | |
---|
134 | $self->{$jidStr}->{Client} = $client; |
---|
135 | $self->{$jidStr}->{Roster} = $client->Roster(); |
---|
136 | $self->{$jidStr}->{Status} = "available"; |
---|
137 | return $client; |
---|
138 | } |
---|
139 | |
---|
140 | sub removeConnection { |
---|
141 | my $self = shift; |
---|
142 | my $jidStr = shift; |
---|
143 | return 0 unless exists $self->{$jidStr}; |
---|
144 | |
---|
145 | $self->{$jidStr}->{Client}->Disconnect() |
---|
146 | if $self->{$jidStr}->{Client}; |
---|
147 | delete $self->{$jidStr}; |
---|
148 | |
---|
149 | return 1; |
---|
150 | } |
---|
151 | |
---|
152 | sub connected { |
---|
153 | my $self = shift; |
---|
154 | return scalar keys %{ $self }; |
---|
155 | } |
---|
156 | |
---|
157 | sub getJIDs { |
---|
158 | my $self = shift; |
---|
159 | return keys %{ $self }; |
---|
160 | } |
---|
161 | |
---|
162 | sub jidExists { |
---|
163 | my $self = shift; |
---|
164 | my $jidStr = shift; |
---|
165 | return exists $self->{$jidStr}; |
---|
166 | } |
---|
167 | |
---|
168 | sub sidExists { |
---|
169 | my $self = shift; |
---|
170 | my $sid = shift || ""; |
---|
171 | foreach my $c ( values %{ $self } ) { |
---|
172 | return 1 if ($c->{Client}->{SESSION}->{id} eq $sid); |
---|
173 | } |
---|
174 | return 0; |
---|
175 | } |
---|
176 | |
---|
177 | sub getConnectionFromSid { |
---|
178 | my $self = shift; |
---|
179 | my $sid = shift; |
---|
180 | foreach my $c (values %{ $self }) { |
---|
181 | return $c->{Client} if $c->{Client}->{SESSION}->{id} eq $sid; |
---|
182 | } |
---|
183 | return undef; |
---|
184 | } |
---|
185 | |
---|
186 | sub getConnectionFromJID { |
---|
187 | my $self = shift; |
---|
188 | my $jid = shift; |
---|
189 | $jid = $jid->GetJID('full') if UNIVERSAL::isa($jid, 'Net::XMPP::JID'); |
---|
190 | return $self->{$jid}->{Client} if exists $self->{$jid}; |
---|
191 | } |
---|
192 | |
---|
193 | sub getRosterFromSid { |
---|
194 | my $self = shift; |
---|
195 | my $sid = shift; |
---|
196 | foreach my $c (values %{ $self }) { |
---|
197 | return $c->{Roster} |
---|
198 | if $c->{Client}->{SESSION}->{id} eq $sid; |
---|
199 | } |
---|
200 | return undef; |
---|
201 | } |
---|
202 | |
---|
203 | sub getRosterFromJID { |
---|
204 | my $self = shift; |
---|
205 | my $jid = shift; |
---|
206 | $jid = $jid->GetJID('full') if UNIVERSAL::isa($jid, 'Net::XMPP::JID'); |
---|
207 | return $self->{$jid}->{Roster} if exists $self->{$jid}; |
---|
208 | } |
---|
209 | ################################################################################ |
---|
210 | |
---|
211 | package BarnOwl::Jabber; |
---|
212 | |
---|
213 | our $conn = new BarnOwl::Jabber::ConnectionManager unless $conn;; |
---|
214 | our %vars; |
---|
215 | |
---|
216 | sub onStart { |
---|
217 | if ( *BarnOwl::queue_message{CODE} ) { |
---|
218 | register_owl_commands(); |
---|
219 | push @::onMainLoop, sub { BarnOwl::Jabber::onMainLoop(@_) }; |
---|
220 | push @::onGetBuddyList, sub { BarnOwl::Jabber::onGetBuddyList(@_) }; |
---|
221 | $vars{show} = ''; |
---|
222 | } else { |
---|
223 | # Our owl doesn't support queue_message. Unfortunately, this |
---|
224 | # means it probably *also* doesn't support BarnOwl::error. So just |
---|
225 | # give up silently. |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | push @::onStartSubs, sub { BarnOwl::Jabber::onStart(@_) }; |
---|
230 | |
---|
231 | sub onMainLoop { |
---|
232 | return if ( !$conn->connected() ); |
---|
233 | |
---|
234 | $vars{status_changed} = 0; |
---|
235 | my $idletime = owl::getidletime(); |
---|
236 | if ($idletime >= 900 && $vars{show} eq 'away') { |
---|
237 | $vars{show} = 'xa'; |
---|
238 | $vars{status} = 'Auto extended-away after 15 minutes idle.'; |
---|
239 | $vars{status_changed} = 1; |
---|
240 | } elsif ($idletime >= 300 && $vars{show} eq '') { |
---|
241 | $vars{show} = 'away'; |
---|
242 | $vars{status} = 'Auto away after 5 minutes idle.'; |
---|
243 | $vars{status_changed} = 1; |
---|
244 | } elsif ($idletime == 0 && $vars{show} ne '') { |
---|
245 | $vars{show} = ''; |
---|
246 | $vars{status} = ''; |
---|
247 | $vars{status_changed} = 1; |
---|
248 | } |
---|
249 | |
---|
250 | foreach my $jid ( $conn->getJIDs() ) { |
---|
251 | my $client = $conn->getConnectionFromJID($jid); |
---|
252 | |
---|
253 | unless($client) { |
---|
254 | $conn->removeConnection($jid); |
---|
255 | BarnOwl::error("Connection for $jid undefined -- error in reload?"); |
---|
256 | } |
---|
257 | |
---|
258 | my $status = $client->Process(0); |
---|
259 | if ( !defined($status) ) { |
---|
260 | BarnOwl::error("Jabber account $jid disconnected!"); |
---|
261 | do_logout($jid); |
---|
262 | } |
---|
263 | if ($::shutdown) { |
---|
264 | do_logout($jid); |
---|
265 | return; |
---|
266 | } |
---|
267 | if ($vars{status_changed}) { |
---|
268 | my $p = new Net::Jabber::Presence; |
---|
269 | $p->SetShow($vars{show}) if $vars{show}; |
---|
270 | $p->SetStatus($vars{status}) if $vars{status}; |
---|
271 | $client->Send($p); |
---|
272 | } |
---|
273 | } |
---|
274 | } |
---|
275 | |
---|
276 | sub blist_listBuddy { |
---|
277 | my $roster = shift; |
---|
278 | my $buddy = shift; |
---|
279 | my $blistStr .= " "; |
---|
280 | my %jq = $roster->query($buddy); |
---|
281 | my $res = $roster->resource($buddy); |
---|
282 | |
---|
283 | my $name = $jq{name} || $buddy->GetUserID(); |
---|
284 | |
---|
285 | $blistStr .= sprintf '%-15s %s', $name, $buddy->GetJID(); |
---|
286 | |
---|
287 | if ($res) { |
---|
288 | my %rq = $roster->resourceQuery( $buddy, $res ); |
---|
289 | $blistStr .= " [" . ( $rq{show} ? $rq{show} : 'online' ) . "]"; |
---|
290 | $blistStr .= " " . $rq{status} if $rq{status}; |
---|
291 | $blistStr = boldify($blistStr); |
---|
292 | } |
---|
293 | else { |
---|
294 | if ($jq{ask}) { |
---|
295 | $blistStr .= " [pending]"; |
---|
296 | } |
---|
297 | elsif ($jq{subscription} eq 'none' || $jq{subscription} eq 'from') { |
---|
298 | $blistStr .= " [not subscribed]"; |
---|
299 | } |
---|
300 | else { |
---|
301 | $blistStr .= " [offline]"; |
---|
302 | } |
---|
303 | } |
---|
304 | return $blistStr . "\n"; |
---|
305 | } |
---|
306 | |
---|
307 | sub getSingleBuddyList { |
---|
308 | my $jid = shift; |
---|
309 | $jid = resolveConnectedJID($jid); |
---|
310 | return "" unless $jid; |
---|
311 | my $blist = ""; |
---|
312 | my $roster = $conn->getRosterFromJID($jid); |
---|
313 | if ($roster) { |
---|
314 | $blist .= "\n" . boldify("Jabber Roster for $jid\n"); |
---|
315 | |
---|
316 | foreach my $group ( $roster->groups() ) { |
---|
317 | $blist .= " Group: $group\n"; |
---|
318 | my @buddies = $roster->jids( 'group', $group ); |
---|
319 | foreach my $buddy ( @buddies ) { |
---|
320 | $blist .= blist_listBuddy( $roster, $buddy ); |
---|
321 | } |
---|
322 | } |
---|
323 | |
---|
324 | my @unsorted = $roster->jids('nogroup'); |
---|
325 | if (@unsorted) { |
---|
326 | $blist .= " [unsorted]\n"; |
---|
327 | foreach my $buddy (@unsorted) { |
---|
328 | $blist .= blist_listBuddy( $roster, $buddy ); |
---|
329 | } |
---|
330 | } |
---|
331 | } |
---|
332 | return $blist; |
---|
333 | } |
---|
334 | |
---|
335 | sub onGetBuddyList { |
---|
336 | my $blist = ""; |
---|
337 | foreach my $jid ($conn->getJIDs()) { |
---|
338 | $blist .= getSingleBuddyList($jid); |
---|
339 | } |
---|
340 | return $blist; |
---|
341 | } |
---|
342 | |
---|
343 | ################################################################################ |
---|
344 | ### Owl Commands |
---|
345 | sub register_owl_commands() { |
---|
346 | BarnOwl::new_command( |
---|
347 | jabberlogin => \&cmd_login, |
---|
348 | { summary => "Log into jabber", }, |
---|
349 | { usage => "jabberlogin JID [PASSWORD]" } |
---|
350 | ); |
---|
351 | BarnOwl::new_command( |
---|
352 | jabberlogout => \&cmd_logout, |
---|
353 | { summary => "Log out of jabber" } |
---|
354 | ); |
---|
355 | BarnOwl::new_command( |
---|
356 | jwrite => \&cmd_jwrite, |
---|
357 | { |
---|
358 | summary => "Send a Jabber Message", |
---|
359 | usage => "jwrite JID [-t thread] [-s subject]" |
---|
360 | } |
---|
361 | ); |
---|
362 | BarnOwl::new_command( |
---|
363 | jlist => \&cmd_jlist, |
---|
364 | { |
---|
365 | summary => "Show your Jabber roster.", |
---|
366 | usage => "jlist" |
---|
367 | } |
---|
368 | ); |
---|
369 | BarnOwl::new_command( |
---|
370 | jmuc => \&cmd_jmuc, |
---|
371 | { |
---|
372 | summary => "Jabber MUC related commands.", |
---|
373 | description => "jmuc sends jabber commands related to muc.\n\n" |
---|
374 | . "The following commands are available\n\n" |
---|
375 | . "join <muc> Join a muc.\n\n" |
---|
376 | . "part <muc> Part a muc.\n" |
---|
377 | . " The muc is taken from the current message if not supplied.\n\n" |
---|
378 | . "invite <jid> <muc>\n" |
---|
379 | . " Invite <jid> to <muc>.\n" |
---|
380 | . " The muc is taken from the current message if not supplied.\n\n" |
---|
381 | . "configure <muc>\n" |
---|
382 | . " Configures a MUC.\n" |
---|
383 | . " Necessary to initalize a new MUC.\n" |
---|
384 | . " At present, only the default configuration is supported.\n" |
---|
385 | . " The muc is taken from the current message if not supplied.\n\n" |
---|
386 | . "presence <muc>\n" |
---|
387 | . " Shows the roster for <muc>.\n" |
---|
388 | . " The muc is taken from the current message if not supplied.\n\n" |
---|
389 | . "presence -a\n" |
---|
390 | . " Shows rosters for all MUCs you're participating in.\n\n", |
---|
391 | usage => "jmuc COMMAND ARGS" |
---|
392 | } |
---|
393 | ); |
---|
394 | BarnOwl::new_command( |
---|
395 | jroster => \&cmd_jroster, |
---|
396 | { |
---|
397 | summary => "Jabber Roster related commands.", |
---|
398 | description => "jroster sends jabber commands related to rosters.\n\n" |
---|
399 | . "The following commands are available\n\n" |
---|
400 | . "sub <jid> Subscribe to <jid>'s presence. (implicit add)\n\n" |
---|
401 | . "add <jid> Adds <jid> to your roster.\n\n" |
---|
402 | . "unsub <jid> Unsubscribe from <jid>'s presence.\n\n" |
---|
403 | . "remove <jid> Removes <jid> to your roster. (implicit unsub)\n\n" |
---|
404 | . "auth <jid> Authorizes <jid> to subscribe to your presence.\n\n" |
---|
405 | . "deauth <jid> De-authorizes <jid>'s subscription to your presence.\n\n" |
---|
406 | . "The following arguments are supported for all commands\n\n" |
---|
407 | . "-a <jid> Specify which account to make the roster changes on.\n" |
---|
408 | . " Required if you're signed into more than one account.\n\n" |
---|
409 | . "The following arguments only work with the add and sub commands.\n\n" |
---|
410 | . "-g <group> Add <jid> to group <group>.\n" |
---|
411 | . " May be specified more than once, will not remove <jid> from any groups.\n\n" |
---|
412 | . "-p Purge. Removes <jid> from all groups.\n" |
---|
413 | . " May be combined with -g completely alter <jid>'s groups.\n\n" |
---|
414 | . "-n <name> Sets <name> as <jid>'s short name.\n\n" |
---|
415 | . "Note: Unless -n is used, you can specify multiple <jid> arguments.\n", |
---|
416 | usage => "jroster COMMAND ARGS" |
---|
417 | } |
---|
418 | ); |
---|
419 | } |
---|
420 | |
---|
421 | sub cmd_login { |
---|
422 | my $cmd = shift; |
---|
423 | my $jid = new Net::Jabber::JID; |
---|
424 | $jid->SetJID(shift); |
---|
425 | my $password = ''; |
---|
426 | $password = shift if @_; |
---|
427 | |
---|
428 | my $uid = $jid->GetUserID(); |
---|
429 | my $componentname = $jid->GetServer(); |
---|
430 | my $resource = $jid->GetResource() || 'owl'; |
---|
431 | $jid->SetResource($resource); |
---|
432 | my $jidStr = $jid->GetJID('full'); |
---|
433 | |
---|
434 | if ( !$uid || !$componentname ) { |
---|
435 | BarnOwl::error("usage: $cmd JID"); |
---|
436 | return; |
---|
437 | } |
---|
438 | |
---|
439 | if ( $conn->jidExists($jidStr) ) { |
---|
440 | BarnOwl::error("Already logged in as $jidStr."); |
---|
441 | return; |
---|
442 | } |
---|
443 | |
---|
444 | my ( $server, $port ) = getServerFromJID($jid); |
---|
445 | |
---|
446 | $vars{jlogin_jid} = $jidStr; |
---|
447 | $vars{jlogin_connhash} = { |
---|
448 | hostname => $server, |
---|
449 | tls => 1, |
---|
450 | port => $port, |
---|
451 | componentname => $componentname |
---|
452 | }; |
---|
453 | $vars{jlogin_authhash} = |
---|
454 | { username => $uid, |
---|
455 | resource => $resource, |
---|
456 | }; |
---|
457 | |
---|
458 | return do_login($password); |
---|
459 | } |
---|
460 | |
---|
461 | sub do_login { |
---|
462 | $vars{jlogin_password} = shift; |
---|
463 | $vars{jlogin_authhash}->{password} = sub { return $vars{jlogin_password} || '' }; |
---|
464 | my $jidStr = $vars{jlogin_jid}; |
---|
465 | if ( !$jidStr && $vars{jlogin_havepass}) { |
---|
466 | BarnOwl::error("Got password but have no jid!"); |
---|
467 | } |
---|
468 | else |
---|
469 | { |
---|
470 | my $client = $conn->addConnection($jidStr); |
---|
471 | |
---|
472 | #XXX Todo: Add more callbacks. |
---|
473 | # * MUC presence handlers |
---|
474 | # We use the anonymous subrefs in order to have the correct behavior |
---|
475 | # when we reload |
---|
476 | $client->SetMessageCallBacks( |
---|
477 | chat => sub { BarnOwl::Jabber::process_incoming_chat_message(@_) }, |
---|
478 | error => sub { BarnOwl::Jabber::process_incoming_error_message(@_) }, |
---|
479 | groupchat => sub { BarnOwl::Jabber::process_incoming_groupchat_message(@_) }, |
---|
480 | headline => sub { BarnOwl::Jabber::process_incoming_headline_message(@_) }, |
---|
481 | normal => sub { BarnOwl::Jabber::process_incoming_normal_message(@_) } |
---|
482 | ); |
---|
483 | $client->SetPresenceCallBacks( |
---|
484 | available => sub { BarnOwl::Jabber::process_presence_available(@_) }, |
---|
485 | unavailable => sub { BarnOwl::Jabber::process_presence_available(@_) }, |
---|
486 | subscribe => sub { BarnOwl::Jabber::process_presence_subscribe(@_) }, |
---|
487 | subscribed => sub { BarnOwl::Jabber::process_presence_subscribed(@_) }, |
---|
488 | unsubscribe => sub { BarnOwl::Jabber::process_presence_unsubscribe(@_) }, |
---|
489 | unsubscribed => sub { BarnOwl::Jabber::process_presence_unsubscribed(@_) }, |
---|
490 | error => sub { BarnOwl::Jabber::process_presence_error(@_) }); |
---|
491 | |
---|
492 | my $status = $client->Connect( %{ $vars{jlogin_connhash} } ); |
---|
493 | if ( !$status ) { |
---|
494 | $conn->removeConnection($jidStr); |
---|
495 | BarnOwl::error("We failed to connect"); |
---|
496 | } else { |
---|
497 | my @result = $client->AuthSend( %{ $vars{jlogin_authhash} } ); |
---|
498 | |
---|
499 | if ( !@result || $result[0] ne 'ok' ) { |
---|
500 | if ( !$vars{jlogin_havepass} && ( !@result || $result[0] eq '401' ) ) { |
---|
501 | $vars{jlogin_havepass} = 1; |
---|
502 | $conn->removeConnection($jidStr); |
---|
503 | BarnOwl::start_password( "Password for $jidStr: ", \&do_login ); |
---|
504 | return ""; |
---|
505 | } |
---|
506 | $conn->removeConnection($jidStr); |
---|
507 | BarnOwl::error( "Error in connect: " . join( " ", @result ) ); |
---|
508 | } else { |
---|
509 | $conn->getRosterFromJID($jidStr)->fetch(); |
---|
510 | $client->PresenceSend( priority => 1 ); |
---|
511 | queue_admin_msg("Connected to jabber as $jidStr"); |
---|
512 | } |
---|
513 | } |
---|
514 | |
---|
515 | } |
---|
516 | delete $vars{jlogin_jid}; |
---|
517 | $vars{jlogin_password} =~ tr/\0-\377/x/ if $vars{jlogin_password}; |
---|
518 | delete $vars{jlogin_password}; |
---|
519 | delete $vars{jlogin_havepass}; |
---|
520 | delete $vars{jlogin_connhash}; |
---|
521 | delete $vars{jlogin_authhash}; |
---|
522 | return ""; |
---|
523 | } |
---|
524 | |
---|
525 | sub do_logout { |
---|
526 | my $jid = shift; |
---|
527 | my $disconnected = $conn->removeConnection($jid); |
---|
528 | queue_admin_msg("Jabber disconnected ($jid).") if $disconnected; |
---|
529 | } |
---|
530 | |
---|
531 | sub cmd_logout { |
---|
532 | # Logged into multiple accounts |
---|
533 | if ( $conn->connected() > 1 ) { |
---|
534 | # Logged into multiple accounts, no accout specified. |
---|
535 | if ( !$_[1] ) { |
---|
536 | my $errStr = |
---|
537 | "You are logged into multiple accounts. Please specify an account to log out of.\n"; |
---|
538 | foreach my $jid ( $conn->getJIDs() ) { |
---|
539 | $errStr .= "\t$jid\n"; |
---|
540 | } |
---|
541 | queue_admin_msg($errStr); |
---|
542 | } |
---|
543 | # Logged into multiple accounts, account specified. |
---|
544 | else { |
---|
545 | if ( $_[1] eq '-a' ) #All accounts. |
---|
546 | { |
---|
547 | foreach my $jid ( $conn->getJIDs() ) { |
---|
548 | do_logout($jid); |
---|
549 | } |
---|
550 | } |
---|
551 | else #One account. |
---|
552 | { |
---|
553 | my $jid = resolveConnectedJID( $_[1] ); |
---|
554 | do_logout($jid) if ( $jid ne '' ); |
---|
555 | } |
---|
556 | } |
---|
557 | } |
---|
558 | else # Only one account logged in. |
---|
559 | { |
---|
560 | do_logout( ( $conn->getJIDs() )[0] ); |
---|
561 | } |
---|
562 | return ""; |
---|
563 | } |
---|
564 | |
---|
565 | sub cmd_jlist { |
---|
566 | if ( !( scalar $conn->getJIDs() ) ) { |
---|
567 | BarnOwl::error("You are not logged in to Jabber."); |
---|
568 | return; |
---|
569 | } |
---|
570 | BarnOwl::popless_ztext( onGetBuddyList() ); |
---|
571 | } |
---|
572 | |
---|
573 | sub cmd_jwrite { |
---|
574 | if ( !$conn->connected() ) { |
---|
575 | BarnOwl::error("You are not logged in to Jabber."); |
---|
576 | return; |
---|
577 | } |
---|
578 | |
---|
579 | my $jwrite_to = ""; |
---|
580 | my $jwrite_from = ""; |
---|
581 | my $jwrite_sid = ""; |
---|
582 | my $jwrite_thread = ""; |
---|
583 | my $jwrite_subject = ""; |
---|
584 | my ($to, $from); |
---|
585 | my $jwrite_type = "chat"; |
---|
586 | |
---|
587 | my @args = @_; |
---|
588 | shift; |
---|
589 | local @ARGV = @_; |
---|
590 | my $gc; |
---|
591 | GetOptions( |
---|
592 | 'thread=s' => \$jwrite_thread, |
---|
593 | 'subject=s' => \$jwrite_subject, |
---|
594 | 'account=s' => \$from, |
---|
595 | 'id=s' => \$jwrite_sid, |
---|
596 | ); |
---|
597 | $jwrite_type = 'groupchat' if $gc; |
---|
598 | |
---|
599 | if ( scalar @ARGV != 1 ) { |
---|
600 | BarnOwl::error( |
---|
601 | "Usage: jwrite JID [-t thread] [-s 'subject'] [-a account]"); |
---|
602 | return; |
---|
603 | } |
---|
604 | else { |
---|
605 | $to = shift @ARGV; |
---|
606 | } |
---|
607 | |
---|
608 | my @candidates = guess_jwrite($from, $to); |
---|
609 | |
---|
610 | unless(scalar @candidates) { |
---|
611 | die("Unable to resolve JID $to"); |
---|
612 | } |
---|
613 | |
---|
614 | @candidates = grep {defined $_->[0]} @candidates; |
---|
615 | |
---|
616 | unless(scalar @candidates) { |
---|
617 | if(!$from) { |
---|
618 | die("You must specify an account with -a"); |
---|
619 | } else { |
---|
620 | die("Unable to resolve account $from"); |
---|
621 | } |
---|
622 | } |
---|
623 | |
---|
624 | |
---|
625 | ($jwrite_from, $jwrite_to, $jwrite_type) = @{$candidates[0]}; |
---|
626 | |
---|
627 | $vars{jwrite} = { |
---|
628 | to => $jwrite_to, |
---|
629 | from => $jwrite_from, |
---|
630 | sid => $jwrite_sid, |
---|
631 | subject => $jwrite_subject, |
---|
632 | thread => $jwrite_thread, |
---|
633 | type => $jwrite_type |
---|
634 | }; |
---|
635 | |
---|
636 | if(scalar @candidates > 1) { |
---|
637 | BarnOwl::message( |
---|
638 | "Warning: Guessing account and/or destination JID" |
---|
639 | ); |
---|
640 | } else { |
---|
641 | BarnOwl::message( |
---|
642 | "Type your message below. End with a dot on a line by itself. ^C will quit." |
---|
643 | ); |
---|
644 | } |
---|
645 | |
---|
646 | my $cmd = "jwrite $jwrite_to -a $jwrite_from"; |
---|
647 | $cmd .= " -t $jwrite_thread" if $jwrite_thread; |
---|
648 | $cmd .= " -t $jwrite_subject" if $jwrite_subject; |
---|
649 | BarnOwl::start_edit_win( $cmd, \&process_owl_jwrite ); |
---|
650 | } |
---|
651 | |
---|
652 | sub cmd_jmuc { |
---|
653 | die "You are not logged in to Jabber" unless $conn->connected(); |
---|
654 | my $ocmd = shift; |
---|
655 | my $cmd = shift; |
---|
656 | if ( !$cmd ) { |
---|
657 | |
---|
658 | #XXX TODO: Write general usage for jmuc command. |
---|
659 | return; |
---|
660 | } |
---|
661 | |
---|
662 | my %jmuc_commands = ( |
---|
663 | join => \&jmuc_join, |
---|
664 | part => \&jmuc_part, |
---|
665 | invite => \&jmuc_invite, |
---|
666 | configure => \&jmuc_configure, |
---|
667 | presence => \&jmuc_presence |
---|
668 | ); |
---|
669 | my $func = $jmuc_commands{$cmd}; |
---|
670 | if ( !$func ) { |
---|
671 | BarnOwl::error("jmuc: Unknown command: $cmd"); |
---|
672 | return; |
---|
673 | } |
---|
674 | |
---|
675 | { |
---|
676 | local @ARGV = @_; |
---|
677 | my $jid; |
---|
678 | my $muc; |
---|
679 | my $m = BarnOwl::getcurmsg(); |
---|
680 | if ( $m && $m->is_jabber && $m->{jtype} eq 'groupchat' ) { |
---|
681 | $muc = $m->{room}; |
---|
682 | $jid = $m->{to}; |
---|
683 | } |
---|
684 | |
---|
685 | my $getopt = Getopt::Long::Parser->new; |
---|
686 | $getopt->configure('pass_through'); |
---|
687 | $getopt->getoptions( 'account=s' => \$jid ); |
---|
688 | $jid ||= defaultJID(); |
---|
689 | if ($jid) { |
---|
690 | $jid = resolveConnectedJID($jid); |
---|
691 | return unless $jid; |
---|
692 | } |
---|
693 | else { |
---|
694 | BarnOwl::error('You must specify an account with -a {jid}'); |
---|
695 | } |
---|
696 | return $func->( $jid, $muc, @ARGV ); |
---|
697 | } |
---|
698 | } |
---|
699 | |
---|
700 | sub jmuc_join { |
---|
701 | my ( $jid, $muc, @args ) = @_; |
---|
702 | local @ARGV = @args; |
---|
703 | my $password; |
---|
704 | GetOptions( 'password=s' => \$password ); |
---|
705 | |
---|
706 | $muc = shift @ARGV |
---|
707 | or die("Usage: jmuc join MUC [-p password] [-a account]"); |
---|
708 | |
---|
709 | $conn->getConnectionFromJID($jid)->MUCJoin(JID => $muc, |
---|
710 | Password => $password, |
---|
711 | History => { |
---|
712 | MaxChars => 0 |
---|
713 | }); |
---|
714 | return; |
---|
715 | } |
---|
716 | |
---|
717 | sub jmuc_part { |
---|
718 | my ( $jid, $muc, @args ) = @_; |
---|
719 | |
---|
720 | $muc = shift @args if scalar @args; |
---|
721 | die("Usage: jmuc part MUC [-a account]") unless $muc; |
---|
722 | |
---|
723 | $conn->getConnectionFromJID($jid)->MUCLeave(JID => $muc); |
---|
724 | queue_admin_msg("$jid has left $muc."); |
---|
725 | } |
---|
726 | |
---|
727 | sub jmuc_invite { |
---|
728 | my ( $jid, $muc, @args ) = @_; |
---|
729 | |
---|
730 | my $invite_jid = shift @args; |
---|
731 | $muc = shift @args if scalar @args; |
---|
732 | |
---|
733 | die('Usage: jmuc invite JID [muc] [-a account]') |
---|
734 | unless $muc && $invite_jid; |
---|
735 | |
---|
736 | my $message = Net::Jabber::Message->new(); |
---|
737 | $message->SetTo($muc); |
---|
738 | my $x = $message->NewChild('http://jabber.org/protocol/muc#user'); |
---|
739 | $x->AddInvite(); |
---|
740 | $x->GetInvite()->SetTo($invite_jid); |
---|
741 | $conn->getConnectionFromJID($jid)->Send($message); |
---|
742 | queue_admin_msg("$jid has invited $invite_jid to $muc."); |
---|
743 | } |
---|
744 | |
---|
745 | sub jmuc_configure { |
---|
746 | my ( $jid, $muc, @args ) = @_; |
---|
747 | $muc = shift @args if scalar @args; |
---|
748 | die("Usage: jmuc configure [muc]") unless $muc; |
---|
749 | my $iq = Net::Jabber::IQ->new(); |
---|
750 | $iq->SetTo($muc); |
---|
751 | $iq->SetType('set'); |
---|
752 | my $query = $iq->NewQuery("http://jabber.org/protocol/muc#owner"); |
---|
753 | my $x = $query->NewChild("jabber:x:data"); |
---|
754 | $x->SetType('submit'); |
---|
755 | |
---|
756 | $conn->getConnectionFromJID($jid)->Send($iq); |
---|
757 | queue_admin_msg("Accepted default instant configuration for $muc"); |
---|
758 | } |
---|
759 | |
---|
760 | sub jmuc_presence_single { |
---|
761 | my $m = shift; |
---|
762 | my @jids = $m->Presence(); |
---|
763 | return "JIDs present in " . $m->BaseJID . "\n\t" |
---|
764 | . join("\n\t", map {$_->GetResource}@jids) . "\n"; |
---|
765 | } |
---|
766 | |
---|
767 | sub jmuc_presence { |
---|
768 | my ( $jid, $muc, @args ) = @_; |
---|
769 | |
---|
770 | $muc = shift @args if scalar @args; |
---|
771 | die("Usage: jmuc presence MUC") unless $muc; |
---|
772 | |
---|
773 | if ($muc eq '-a') { |
---|
774 | my $str = ""; |
---|
775 | foreach my $jid ($conn->getJIDs()) { |
---|
776 | $str .= boldify("Conferences for $jid:\n"); |
---|
777 | my $connection = $conn->getConnectionFromJID($jid); |
---|
778 | foreach my $muc ($connection->MUCs) { |
---|
779 | $str .= jmuc_presence_single($muc)."\n"; |
---|
780 | } |
---|
781 | } |
---|
782 | BarnOwl::popless_ztext($str); |
---|
783 | } |
---|
784 | else { |
---|
785 | my $m = $conn->getConnectionFromJID($jid)->FindMUC(jid => $muc); |
---|
786 | die("No such muc: $muc") unless $m; |
---|
787 | BarnOwl::popless_ztext(jmuc_presence_single($m)); |
---|
788 | } |
---|
789 | } |
---|
790 | |
---|
791 | |
---|
792 | #XXX TODO: Consider merging this with jmuc and selecting off the first two args. |
---|
793 | sub cmd_jroster { |
---|
794 | die "You are not logged in to Jabber" unless $conn->connected(); |
---|
795 | my $ocmd = shift; |
---|
796 | my $cmd = shift; |
---|
797 | if ( !$cmd ) { |
---|
798 | |
---|
799 | #XXX TODO: Write general usage for jroster command. |
---|
800 | return; |
---|
801 | } |
---|
802 | |
---|
803 | my %jroster_commands = ( |
---|
804 | sub => \&jroster_sub, |
---|
805 | unsub => \&jroster_unsub, |
---|
806 | add => \&jroster_add, |
---|
807 | remove => \&jroster_remove, |
---|
808 | auth => \&jroster_auth, |
---|
809 | deauth => \&jroster_deauth |
---|
810 | ); |
---|
811 | my $func = $jroster_commands{$cmd}; |
---|
812 | if ( !$func ) { |
---|
813 | BarnOwl::error("jroster: Unknown command: $cmd"); |
---|
814 | return; |
---|
815 | } |
---|
816 | |
---|
817 | { |
---|
818 | local @ARGV = @_; |
---|
819 | my $jid; |
---|
820 | my $name; |
---|
821 | my @groups; |
---|
822 | my $purgeGroups; |
---|
823 | my $getopt = Getopt::Long::Parser->new; |
---|
824 | $getopt->configure('pass_through'); |
---|
825 | $getopt->getoptions( |
---|
826 | 'account=s' => \$jid, |
---|
827 | 'group=s' => \@groups, |
---|
828 | 'purgegroups' => \$purgeGroups, |
---|
829 | 'name=s' => \$name |
---|
830 | ); |
---|
831 | $jid ||= defaultJID(); |
---|
832 | if ($jid) { |
---|
833 | $jid = resolveConnectedJID($jid); |
---|
834 | return unless $jid; |
---|
835 | } |
---|
836 | else { |
---|
837 | BarnOwl::error('You must specify an account with -a {jid}'); |
---|
838 | } |
---|
839 | return $func->( $jid, $name, \@groups, $purgeGroups, @ARGV ); |
---|
840 | } |
---|
841 | } |
---|
842 | |
---|
843 | sub jroster_sub { |
---|
844 | my $jid = shift; |
---|
845 | my $name = shift; |
---|
846 | my @groups = @{ shift() }; |
---|
847 | my $purgeGroups = shift; |
---|
848 | my $baseJID = baseJID($jid); |
---|
849 | |
---|
850 | my $roster = $conn->getRosterFromJID($jid); |
---|
851 | |
---|
852 | # Adding lots of users with the same name is a bad idea. |
---|
853 | $name = "" unless (1 == scalar(@ARGV)); |
---|
854 | |
---|
855 | my $p = new Net::Jabber::Presence; |
---|
856 | $p->SetType('subscribe'); |
---|
857 | |
---|
858 | foreach my $to (@ARGV) { |
---|
859 | jroster_add($jid, $name, \@groups, $purgeGroups, ($to)) unless ($roster->exists($to)); |
---|
860 | |
---|
861 | $p->SetTo($to); |
---|
862 | $conn->getConnectionFromJID($jid)->Send($p); |
---|
863 | queue_admin_msg("You ($baseJID) have requested a subscription to ($to)'s presence."); |
---|
864 | } |
---|
865 | } |
---|
866 | |
---|
867 | sub jroster_unsub { |
---|
868 | my $jid = shift; |
---|
869 | my $name = shift; |
---|
870 | my @groups = @{ shift() }; |
---|
871 | my $purgeGroups = shift; |
---|
872 | my $baseJID = baseJID($jid); |
---|
873 | |
---|
874 | my $p = new Net::Jabber::Presence; |
---|
875 | $p->SetType('unsubscribe'); |
---|
876 | foreach my $to (@ARGV) { |
---|
877 | $p->SetTo($to); |
---|
878 | $conn->getConnectionFromJID($jid)->Send($p); |
---|
879 | queue_admin_msg("You ($baseJID) have unsubscribed from ($to)'s presence."); |
---|
880 | } |
---|
881 | } |
---|
882 | |
---|
883 | sub jroster_add { |
---|
884 | my $jid = shift; |
---|
885 | my $name = shift; |
---|
886 | my @groups = @{ shift() }; |
---|
887 | my $purgeGroups = shift; |
---|
888 | my $baseJID = baseJID($jid); |
---|
889 | |
---|
890 | my $roster = $conn->getRosterFromJID($jid); |
---|
891 | |
---|
892 | # Adding lots of users with the same name is a bad idea. |
---|
893 | $name = "" unless (1 == scalar(@ARGV)); |
---|
894 | |
---|
895 | foreach my $to (@ARGV) { |
---|
896 | my %jq = $roster->query($to); |
---|
897 | my $iq = new Net::Jabber::IQ; |
---|
898 | $iq->SetType('set'); |
---|
899 | my $item = new XML::Stream::Node('item'); |
---|
900 | $iq->NewChild('jabber:iq:roster')->AddChild($item); |
---|
901 | |
---|
902 | my %allGroups = (); |
---|
903 | |
---|
904 | foreach my $g (@groups) { |
---|
905 | $allGroups{$g} = $g; |
---|
906 | } |
---|
907 | |
---|
908 | unless ($purgeGroups) { |
---|
909 | foreach my $g (@{$jq{groups}}) { |
---|
910 | $allGroups{$g} = $g; |
---|
911 | } |
---|
912 | } |
---|
913 | |
---|
914 | foreach my $g (keys %allGroups) { |
---|
915 | $item->add_child('group')->add_cdata($g); |
---|
916 | } |
---|
917 | |
---|
918 | $item->put_attrib(jid => $to); |
---|
919 | $item->put_attrib(name => $name) if $name; |
---|
920 | $conn->getConnectionFromJID($jid)->Send($iq); |
---|
921 | my $msg = "$baseJID: " |
---|
922 | . ($name ? "$name ($to)" : "($to)") |
---|
923 | . " is on your roster in the following groups: { " |
---|
924 | . join(" , ", keys %allGroups) |
---|
925 | . " }"; |
---|
926 | queue_admin_msg($msg); |
---|
927 | } |
---|
928 | } |
---|
929 | |
---|
930 | sub jroster_remove { |
---|
931 | my $jid = shift; |
---|
932 | my $name = shift; |
---|
933 | my @groups = @{ shift() }; |
---|
934 | my $purgeGroups = shift; |
---|
935 | my $baseJID = baseJID($jid); |
---|
936 | |
---|
937 | my $iq = new Net::Jabber::IQ; |
---|
938 | $iq->SetType('set'); |
---|
939 | my $item = new XML::Stream::Node('item'); |
---|
940 | $iq->NewChild('jabber:iq:roster')->AddChild($item); |
---|
941 | $item->put_attrib(subscription=> 'remove'); |
---|
942 | foreach my $to (@ARGV) { |
---|
943 | $item->put_attrib(jid => $to); |
---|
944 | $conn->getConnectionFromJID($jid)->Send($iq); |
---|
945 | queue_admin_msg("You ($baseJID) have removed ($to) from your roster."); |
---|
946 | } |
---|
947 | } |
---|
948 | |
---|
949 | sub jroster_auth { |
---|
950 | my $jid = shift; |
---|
951 | my $name = shift; |
---|
952 | my @groups = @{ shift() }; |
---|
953 | my $purgeGroups = shift; |
---|
954 | my $baseJID = baseJID($jid); |
---|
955 | |
---|
956 | my $p = new Net::Jabber::Presence; |
---|
957 | $p->SetType('subscribed'); |
---|
958 | foreach my $to (@ARGV) { |
---|
959 | $p->SetTo($to); |
---|
960 | $conn->getConnectionFromJID($jid)->Send($p); |
---|
961 | queue_admin_msg("($to) has been subscribed to your ($baseJID) presence."); |
---|
962 | } |
---|
963 | } |
---|
964 | |
---|
965 | sub jroster_deauth { |
---|
966 | my $jid = shift; |
---|
967 | my $name = shift; |
---|
968 | my @groups = @{ shift() }; |
---|
969 | my $purgeGroups = shift; |
---|
970 | my $baseJID = baseJID($jid); |
---|
971 | |
---|
972 | my $p = new Net::Jabber::Presence; |
---|
973 | $p->SetType('unsubscribed'); |
---|
974 | foreach my $to (@ARGV) { |
---|
975 | $p->SetTo($to); |
---|
976 | $conn->getConnectionFromJID($jid)->Send($p); |
---|
977 | queue_admin_msg("($to) has been unsubscribed from your ($baseJID) presence."); |
---|
978 | } |
---|
979 | } |
---|
980 | |
---|
981 | ################################################################################ |
---|
982 | ### Owl Callbacks |
---|
983 | sub process_owl_jwrite { |
---|
984 | my $body = shift; |
---|
985 | |
---|
986 | my $j = new Net::Jabber::Message; |
---|
987 | $body =~ s/\n\z//; |
---|
988 | $j->SetMessage( |
---|
989 | to => $vars{jwrite}{to}, |
---|
990 | from => $vars{jwrite}{from}, |
---|
991 | type => $vars{jwrite}{type}, |
---|
992 | body => $body |
---|
993 | ); |
---|
994 | |
---|
995 | $j->SetThread( $vars{jwrite}{thread} ) if ( $vars{jwrite}{thread} ); |
---|
996 | $j->SetSubject( $vars{jwrite}{subject} ) if ( $vars{jwrite}{subject} ); |
---|
997 | |
---|
998 | my $m = j2o( $j, { direction => 'out' } ); |
---|
999 | if ( $vars{jwrite}{type} ne 'groupchat' && BarnOwl::getvar('displayoutgoing') eq 'on') { |
---|
1000 | BarnOwl::queue_message($m); |
---|
1001 | } |
---|
1002 | |
---|
1003 | $j->RemoveFrom(); # Kludge to get around gtalk's random bits after the resource. |
---|
1004 | if ($vars{jwrite}{sid} && $conn->sidExists( $vars{jwrite}{sid} )) { |
---|
1005 | $conn->getConnectionFromSid($vars{jwrite}{sid})->Send($j); |
---|
1006 | } |
---|
1007 | else { |
---|
1008 | $conn->getConnectionFromJID($vars{jwrite}{from})->Send($j); |
---|
1009 | } |
---|
1010 | |
---|
1011 | delete $vars{jwrite}; |
---|
1012 | BarnOwl::message(""); # Kludge to make the ``type your message...'' message go away |
---|
1013 | } |
---|
1014 | |
---|
1015 | ### XMPP Callbacks |
---|
1016 | |
---|
1017 | sub process_incoming_chat_message { |
---|
1018 | my ( $sid, $j ) = @_; |
---|
1019 | BarnOwl::queue_message( j2o( $j, { direction => 'in', |
---|
1020 | sid => $sid } ) ); |
---|
1021 | } |
---|
1022 | |
---|
1023 | sub process_incoming_error_message { |
---|
1024 | my ( $sid, $j ) = @_; |
---|
1025 | my %jhash = j2hash( $j, { direction => 'in', |
---|
1026 | sid => $sid } ); |
---|
1027 | $jhash{type} = 'admin'; |
---|
1028 | BarnOwl::queue_message( BarnOwl::Message->new(%jhash) ); |
---|
1029 | } |
---|
1030 | |
---|
1031 | sub process_incoming_groupchat_message { |
---|
1032 | my ( $sid, $j ) = @_; |
---|
1033 | |
---|
1034 | # HACK IN PROGRESS (ignoring delayed messages) |
---|
1035 | return if ( $j->DefinedX('jabber:x:delay') && $j->GetX('jabber:x:delay') ); |
---|
1036 | BarnOwl::queue_message( j2o( $j, { direction => 'in', |
---|
1037 | sid => $sid } ) ); |
---|
1038 | } |
---|
1039 | |
---|
1040 | sub process_incoming_headline_message { |
---|
1041 | my ( $sid, $j ) = @_; |
---|
1042 | BarnOwl::queue_message( j2o( $j, { direction => 'in', |
---|
1043 | sid => $sid } ) ); |
---|
1044 | } |
---|
1045 | |
---|
1046 | sub process_incoming_normal_message { |
---|
1047 | my ( $sid, $j ) = @_; |
---|
1048 | my %jhash = j2hash( $j, { direction => 'in', |
---|
1049 | sid => $sid } ); |
---|
1050 | |
---|
1051 | # XXX TODO: handle things such as MUC invites here. |
---|
1052 | |
---|
1053 | # if ($j->HasX('http://jabber.org/protocol/muc#user')) |
---|
1054 | # { |
---|
1055 | # my $x = $j->GetX('http://jabber.org/protocol/muc#user'); |
---|
1056 | # if ($x->HasChild('invite')) |
---|
1057 | # { |
---|
1058 | # $props |
---|
1059 | # } |
---|
1060 | # } |
---|
1061 | # |
---|
1062 | BarnOwl::queue_message( BarnOwl::Message->new(%jhash) ); |
---|
1063 | } |
---|
1064 | |
---|
1065 | sub process_muc_presence { |
---|
1066 | my ( $sid, $p ) = @_; |
---|
1067 | return unless ( $p->HasX('http://jabber.org/protocol/muc#user') ); |
---|
1068 | } |
---|
1069 | |
---|
1070 | |
---|
1071 | sub process_presence_available { |
---|
1072 | my ( $sid, $p ) = @_; |
---|
1073 | my $from = $p->GetFrom(); |
---|
1074 | my $to = $p->GetTo(); |
---|
1075 | my $type = $p->GetType(); |
---|
1076 | my %props = ( |
---|
1077 | to => $to, |
---|
1078 | from => $from, |
---|
1079 | recipient => $to, |
---|
1080 | sender => $from, |
---|
1081 | type => 'jabber', |
---|
1082 | jtype => $p->GetType(), |
---|
1083 | status => $p->GetStatus(), |
---|
1084 | show => $p->GetShow(), |
---|
1085 | xml => $p->GetXML(), |
---|
1086 | direction => 'in'); |
---|
1087 | |
---|
1088 | if ($type eq '' || $type eq 'available') { |
---|
1089 | $props{body} = "$from is now online. "; |
---|
1090 | $props{loginout} = 'login'; |
---|
1091 | } |
---|
1092 | else { |
---|
1093 | $props{body} = "$from is now offline. "; |
---|
1094 | $props{loginout} = 'logout'; |
---|
1095 | } |
---|
1096 | $props{replysendercmd} = $props{replycmd} = "jwrite $from -i $sid"; |
---|
1097 | if(BarnOwl::getvar('debug') eq 'on') { |
---|
1098 | BarnOwl::queue_message(BarnOwl::Message->new(%props)); |
---|
1099 | } |
---|
1100 | } |
---|
1101 | |
---|
1102 | sub process_presence_subscribe { |
---|
1103 | my ( $sid, $p ) = @_; |
---|
1104 | my $from = $p->GetFrom(); |
---|
1105 | my $to = $p->GetTo(); |
---|
1106 | my %props = ( |
---|
1107 | to => $to, |
---|
1108 | from => $from, |
---|
1109 | xml => $p->GetXML(), |
---|
1110 | type => 'admin', |
---|
1111 | adminheader => 'Jabber presence: subscribe', |
---|
1112 | direction => 'in'); |
---|
1113 | |
---|
1114 | $props{body} = "Allow user ($from) to subscribe to your ($to) presence?\n" . |
---|
1115 | "(Answer with the `yes' or `no' commands)"; |
---|
1116 | $props{yescommand} = "jroster auth $from -a $to"; |
---|
1117 | $props{nocommand} = "jroster deauth $from -a $to"; |
---|
1118 | $props{question} = "true"; |
---|
1119 | BarnOwl::queue_message(BarnOwl::Message->new(%props)); |
---|
1120 | } |
---|
1121 | |
---|
1122 | sub process_presence_unsubscribe { |
---|
1123 | my ( $sid, $p ) = @_; |
---|
1124 | my $from = $p->GetFrom(); |
---|
1125 | my $to = $p->GetTo(); |
---|
1126 | my %props = ( |
---|
1127 | to => $to, |
---|
1128 | from => $from, |
---|
1129 | xml => $p->GetXML(), |
---|
1130 | type => 'admin', |
---|
1131 | adminheader => 'Jabber presence: unsubscribe', |
---|
1132 | direction => 'in'); |
---|
1133 | |
---|
1134 | $props{body} = "The user ($from) has been unsubscribed from your ($to) presence.\n"; |
---|
1135 | BarnOwl::queue_message(BarnOwl::Message->new(%props)); |
---|
1136 | |
---|
1137 | # Find a connection to reply with. |
---|
1138 | foreach my $jid ($conn->getJIDs()) { |
---|
1139 | my $cJID = new Net::Jabber::JID; |
---|
1140 | $cJID->SetJID($jid); |
---|
1141 | if ($to eq $cJID->GetJID('base') || |
---|
1142 | $to eq $cJID->GetJID('full')) { |
---|
1143 | my $reply = $p->Reply(type=>"unsubscribed"); |
---|
1144 | $conn->getConnectionFromJID($jid)->Send($reply); |
---|
1145 | return; |
---|
1146 | } |
---|
1147 | } |
---|
1148 | } |
---|
1149 | |
---|
1150 | sub process_presence_subscribed { |
---|
1151 | my ( $sid, $p ) = @_; |
---|
1152 | queue_admin_msg("ignoring:".$p->GetXML()); |
---|
1153 | # RFC 3921 says we should respond to this with a "subscribe" |
---|
1154 | # but this causes a flood of sub/sub'd presence packets with |
---|
1155 | # some servers, so we won't. We may want to detect this condition |
---|
1156 | # later, and have per-server settings. |
---|
1157 | return; |
---|
1158 | } |
---|
1159 | |
---|
1160 | sub process_presence_unsubscribed { |
---|
1161 | my ( $sid, $p ) = @_; |
---|
1162 | queue_admin_msg("ignoring:".$p->GetXML()); |
---|
1163 | # RFC 3921 says we should respond to this with a "subscribe" |
---|
1164 | # but this causes a flood of unsub/unsub'd presence packets with |
---|
1165 | # some servers, so we won't. We may want to detect this condition |
---|
1166 | # later, and have per-server settings. |
---|
1167 | return; |
---|
1168 | } |
---|
1169 | |
---|
1170 | sub process_presence_error { |
---|
1171 | my ( $sid, $p ) = @_; |
---|
1172 | my $code = $p->GetErrorCode(); |
---|
1173 | my $error = $p->GetError(); |
---|
1174 | BarnOwl::error("Jabber: $code $error"); |
---|
1175 | } |
---|
1176 | |
---|
1177 | |
---|
1178 | ### Helper functions |
---|
1179 | |
---|
1180 | sub j2hash { |
---|
1181 | my $j = shift; |
---|
1182 | my %initProps = %{ shift() }; |
---|
1183 | |
---|
1184 | my $dir = 'none'; |
---|
1185 | my %props = ( type => 'jabber' ); |
---|
1186 | |
---|
1187 | foreach my $k (keys %initProps) { |
---|
1188 | $dir = $initProps{$k} if ($k eq 'direction'); |
---|
1189 | $props{$k} = $initProps{$k}; |
---|
1190 | } |
---|
1191 | |
---|
1192 | my $jtype = $props{jtype} = $j->GetType(); |
---|
1193 | my $from = $j->GetFrom('jid'); |
---|
1194 | my $to = $j->GetTo('jid'); |
---|
1195 | |
---|
1196 | $props{from} = $from->GetJID('full'); |
---|
1197 | $props{to} = $to->GetJID('full'); |
---|
1198 | |
---|
1199 | $props{recipient} = $to->GetJID('base'); |
---|
1200 | $props{sender} = $from->GetJID('base'); |
---|
1201 | $props{subject} = $j->GetSubject() if ( $j->DefinedSubject() ); |
---|
1202 | $props{thread} = $j->GetThread() if ( $j->DefinedThread() ); |
---|
1203 | $props{body} = $j->GetBody() if ( $j->DefinedBody() ); |
---|
1204 | $props{error} = $j->GetError() if ( $j->DefinedError() ); |
---|
1205 | $props{error_code} = $j->GetErrorCode() if ( $j->DefinedErrorCode() ); |
---|
1206 | $props{xml} = $j->GetXML(); |
---|
1207 | |
---|
1208 | if ( $jtype eq 'chat' ) { |
---|
1209 | $props{replycmd} = |
---|
1210 | "jwrite " . ( ( $dir eq 'in' ) ? $props{from} : $props{to} ); |
---|
1211 | $props{replycmd} .= |
---|
1212 | " -a " . ( ( $dir eq 'out' ) ? $props{from} : $props{to} ); |
---|
1213 | $props{private} = 1; |
---|
1214 | |
---|
1215 | my $connection; |
---|
1216 | if ($dir eq 'in') { |
---|
1217 | $connection = $conn->getConnectionFromSid($props{sid}); |
---|
1218 | } |
---|
1219 | else { |
---|
1220 | $connection = $conn->getConnectionFromJID($props{from}); |
---|
1221 | } |
---|
1222 | |
---|
1223 | # Check to see if we're doing personals with someone in a muc. |
---|
1224 | # If we are, show the full jid because the base jid is the room. |
---|
1225 | if ($connection) { |
---|
1226 | $props{sender} = $props{from} |
---|
1227 | if ($connection->FindMUC(jid => $from)); |
---|
1228 | $props{recipient} = $props{to} |
---|
1229 | if ($connection->FindMUC(jid => $to)); |
---|
1230 | } |
---|
1231 | } |
---|
1232 | elsif ( $jtype eq 'groupchat' ) { |
---|
1233 | my $nick = $props{nick} = $from->GetResource(); |
---|
1234 | my $room = $props{room} = $from->GetJID('base'); |
---|
1235 | $props{replycmd} = "jwrite $room"; |
---|
1236 | $props{replycmd} .= |
---|
1237 | " -a " . ( ( $dir eq 'out' ) ? $props{from} : $props{to} ); |
---|
1238 | |
---|
1239 | if ($dir eq 'out') { |
---|
1240 | $props{replysendercmd} = "jwrite ".$props{to}." -a ".$props{from}; |
---|
1241 | } |
---|
1242 | else { |
---|
1243 | $props{replysendercmd} = "jwrite ".$props{from}." -a ".$props{to}; |
---|
1244 | } |
---|
1245 | |
---|
1246 | $props{sender} = $nick || $room; |
---|
1247 | $props{recipient} = $room; |
---|
1248 | |
---|
1249 | if ( $props{subject} && !$props{body} ) { |
---|
1250 | $props{body} = |
---|
1251 | '[' . $nick . " has set the topic to: " . $props{subject} . "]"; |
---|
1252 | } |
---|
1253 | } |
---|
1254 | elsif ( $jtype eq 'normal' ) { |
---|
1255 | $props{replycmd} = undef; |
---|
1256 | $props{private} = 1; |
---|
1257 | } |
---|
1258 | elsif ( $jtype eq 'headline' ) { |
---|
1259 | $props{replycmd} = undef; |
---|
1260 | } |
---|
1261 | elsif ( $jtype eq 'error' ) { |
---|
1262 | $props{replycmd} = undef; |
---|
1263 | $props{body} = "Error " |
---|
1264 | . $props{error_code} |
---|
1265 | . " sending to " |
---|
1266 | . $props{from} . "\n" |
---|
1267 | . $props{error}; |
---|
1268 | } |
---|
1269 | |
---|
1270 | $props{replysendercmd} = $props{replycmd} unless $props{replysendercmd}; |
---|
1271 | return %props; |
---|
1272 | } |
---|
1273 | |
---|
1274 | sub j2o { |
---|
1275 | return BarnOwl::Message->new( j2hash(@_) ); |
---|
1276 | } |
---|
1277 | |
---|
1278 | sub queue_admin_msg { |
---|
1279 | my $err = shift; |
---|
1280 | my $m = BarnOwl::Message->new( |
---|
1281 | type => 'admin', |
---|
1282 | direction => 'none', |
---|
1283 | body => $err |
---|
1284 | ); |
---|
1285 | BarnOwl::queue_message($m); |
---|
1286 | } |
---|
1287 | |
---|
1288 | sub boldify($) { |
---|
1289 | my $str = shift; |
---|
1290 | |
---|
1291 | return '@b(' . $str . ')' if ( $str !~ /\)/ ); |
---|
1292 | return '@b<' . $str . '>' if ( $str !~ /\>/ ); |
---|
1293 | return '@b{' . $str . '}' if ( $str !~ /\}/ ); |
---|
1294 | return '@b[' . $str . ']' if ( $str !~ /\]/ ); |
---|
1295 | |
---|
1296 | my $txt = "$str"; |
---|
1297 | $txt =~ s{[)]}{)\@b[)]\@b(}g; |
---|
1298 | return '@b(' . $txt . ')'; |
---|
1299 | } |
---|
1300 | |
---|
1301 | sub getServerFromJID { |
---|
1302 | my $jid = shift; |
---|
1303 | my $res = new Net::DNS::Resolver; |
---|
1304 | my $packet = |
---|
1305 | $res->search( '_xmpp-client._tcp.' . $jid->GetServer(), 'srv' ); |
---|
1306 | |
---|
1307 | if ($packet) # Got srv record. |
---|
1308 | { |
---|
1309 | my @answer = $packet->answer; |
---|
1310 | return $answer[0]{target}, $answer[0]{port}; |
---|
1311 | } |
---|
1312 | |
---|
1313 | return $jid->GetServer(), 5222; |
---|
1314 | } |
---|
1315 | |
---|
1316 | sub defaultJID { |
---|
1317 | return ( $conn->getJIDs() )[0] if ( $conn->connected() == 1 ); |
---|
1318 | return; |
---|
1319 | } |
---|
1320 | |
---|
1321 | sub baseJID { |
---|
1322 | my $givenJIDStr = shift; |
---|
1323 | my $givenJID = new Net::Jabber::JID; |
---|
1324 | $givenJID->SetJID($givenJIDStr); |
---|
1325 | return $givenJID->GetJID('base'); |
---|
1326 | } |
---|
1327 | |
---|
1328 | sub resolveConnectedJID { |
---|
1329 | my $givenJIDStr = shift; |
---|
1330 | my $givenJID = new Net::Jabber::JID; |
---|
1331 | $givenJID->SetJID($givenJIDStr); |
---|
1332 | |
---|
1333 | # Account fully specified. |
---|
1334 | if ( $givenJID->GetResource() ) { |
---|
1335 | # Specified account exists |
---|
1336 | return $givenJIDStr if ($conn->jidExists($givenJIDStr) ); |
---|
1337 | die("Invalid account: $givenJIDStr"); |
---|
1338 | } |
---|
1339 | |
---|
1340 | # Disambiguate. |
---|
1341 | else { |
---|
1342 | my $matchingJID = ""; |
---|
1343 | my $errStr = |
---|
1344 | "Ambiguous account reference. Please specify a resource.\n"; |
---|
1345 | my $ambiguous = 0; |
---|
1346 | |
---|
1347 | foreach my $jid ( $conn->getJIDs() ) { |
---|
1348 | my $cJID = new Net::Jabber::JID; |
---|
1349 | $cJID->SetJID($jid); |
---|
1350 | if ( $givenJIDStr eq $cJID->GetJID('base') ) { |
---|
1351 | $ambiguous = 1 if ( $matchingJID ne "" ); |
---|
1352 | $matchingJID = $jid; |
---|
1353 | $errStr .= "\t$jid\n"; |
---|
1354 | } |
---|
1355 | } |
---|
1356 | |
---|
1357 | # Need further disambiguation. |
---|
1358 | if ($ambiguous) { |
---|
1359 | die($errStr); |
---|
1360 | } |
---|
1361 | |
---|
1362 | # Not one of ours. |
---|
1363 | elsif ( $matchingJID eq "" ) { |
---|
1364 | die("Invalid account: $givenJIDStr"); |
---|
1365 | } |
---|
1366 | |
---|
1367 | # It's this one. |
---|
1368 | else { |
---|
1369 | return $matchingJID; |
---|
1370 | } |
---|
1371 | } |
---|
1372 | return ""; |
---|
1373 | } |
---|
1374 | |
---|
1375 | sub resolveDestJID { |
---|
1376 | my ($to, $from) = @_; |
---|
1377 | my $jid = Net::Jabber::JID->new($to); |
---|
1378 | |
---|
1379 | my $roster = $conn->getRosterFromJID($from); |
---|
1380 | my @jids = $roster->jids('all'); |
---|
1381 | for my $j (@jids) { |
---|
1382 | if(($roster->query($j, 'name') || $j->GetUserID()) eq $to) { |
---|
1383 | return $j->GetJID('full'); |
---|
1384 | } elsif($j->GetJID('base') eq baseJID($to)) { |
---|
1385 | return $j->GetJID('full'); |
---|
1386 | } |
---|
1387 | } |
---|
1388 | |
---|
1389 | # If we found nothing being clever, check to see if our input was |
---|
1390 | # sane enough to look like a jid with a UserID. |
---|
1391 | return $jid->GetJID('full') if $jid->GetUserID(); |
---|
1392 | return undef; |
---|
1393 | } |
---|
1394 | |
---|
1395 | sub resolveType { |
---|
1396 | my $to = shift; |
---|
1397 | my $from = shift; |
---|
1398 | return unless $from; |
---|
1399 | my @mucs = $conn->getConnectionFromJID($from)->MUCs; |
---|
1400 | if(grep {$_->BaseJID eq $to } @mucs) { |
---|
1401 | return 'groupchat'; |
---|
1402 | } else { |
---|
1403 | return 'chat'; |
---|
1404 | } |
---|
1405 | } |
---|
1406 | |
---|
1407 | sub guess_jwrite { |
---|
1408 | # Heuristically guess what jids a jwrite was meant to be going to/from |
---|
1409 | my ($from, $to) = (@_); |
---|
1410 | my ($from_jid, $to_jid); |
---|
1411 | my @matches; |
---|
1412 | if($from) { |
---|
1413 | $from_jid = resolveConnectedJID($from); |
---|
1414 | die("Unable to resolve account $from") unless $from_jid; |
---|
1415 | $to_jid = resolveDestJID($to, $from_jid); |
---|
1416 | push @matches, [$from_jid, $to_jid]; |
---|
1417 | } else { |
---|
1418 | for my $f ($conn->getJIDs) { |
---|
1419 | $to_jid = resolveDestJID($to, $f); |
---|
1420 | if(defined($to_jid)) { |
---|
1421 | push @matches, [$f, $to_jid]; |
---|
1422 | } |
---|
1423 | } |
---|
1424 | if($to =~ /@/) { |
---|
1425 | push @matches, [$_, $to] |
---|
1426 | for ($conn->getJIDs); |
---|
1427 | } |
---|
1428 | } |
---|
1429 | |
---|
1430 | for my $m (@matches) { |
---|
1431 | my $type = resolveType($m->[1], $m->[0]); |
---|
1432 | push @$m, $type; |
---|
1433 | } |
---|
1434 | |
---|
1435 | return @matches; |
---|
1436 | } |
---|
1437 | |
---|
1438 | ##################################################################### |
---|
1439 | ##################################################################### |
---|
1440 | |
---|
1441 | package BarnOwl::Message::Jabber; |
---|
1442 | |
---|
1443 | our @ISA = qw( BarnOwl::Message ); |
---|
1444 | |
---|
1445 | sub jtype { shift->{jtype} }; |
---|
1446 | sub from { shift->{from} }; |
---|
1447 | sub to { shift->{to} }; |
---|
1448 | sub room { shift->{room} }; |
---|
1449 | |
---|
1450 | sub smartfilter { |
---|
1451 | my $self = shift; |
---|
1452 | my $inst = shift; |
---|
1453 | |
---|
1454 | my ($filter, $ftext); |
---|
1455 | |
---|
1456 | if($self->jtype eq 'chat') { |
---|
1457 | my $user; |
---|
1458 | if($self->direction eq 'in') { |
---|
1459 | $user = $self->from; |
---|
1460 | } else { |
---|
1461 | $user = $self->to; |
---|
1462 | } |
---|
1463 | $user = Net::Jabber::JID->new($user)->GetJID($inst ? 'full' : 'base'); |
---|
1464 | $filter = "jabber-user-$user"; |
---|
1465 | $ftext = qq{type ^jabber\$ and ( ( direction ^in\$ and from ^$user ) } . |
---|
1466 | qq{or ( direction ^out\$ and to ^$user ) ) }; |
---|
1467 | BarnOwl::filter("$filter $ftext"); |
---|
1468 | return $filter; |
---|
1469 | } elsif ($self->jtype eq 'groupchat') { |
---|
1470 | my $room = $self->room; |
---|
1471 | $filter = "jabber-room-$room"; |
---|
1472 | $ftext = qq{type ^jabber\$ and room ^$room\$}; |
---|
1473 | BarnOwl::filter("$filter $ftext"); |
---|
1474 | return $filter; |
---|
1475 | } |
---|
1476 | } |
---|
1477 | |
---|
1478 | 1; |
---|