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