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 | if ($::shutdown) { |
---|
119 | do_logout($jid); |
---|
120 | next; |
---|
121 | } |
---|
122 | if ($vars{status_changed}) { |
---|
123 | my $p = new Net::Jabber::Presence; |
---|
124 | $p->SetShow($vars{show}) if $vars{show}; |
---|
125 | $p->SetStatus($vars{status}) if $vars{status}; |
---|
126 | $client->Send($p); |
---|
127 | } |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | our $showOffline = 0; |
---|
132 | |
---|
133 | sub blist_listBuddy { |
---|
134 | my $roster = shift; |
---|
135 | my $buddy = shift; |
---|
136 | my $blistStr .= " "; |
---|
137 | my %jq = $roster->query($buddy); |
---|
138 | my $res = $roster->resource($buddy); |
---|
139 | |
---|
140 | my $name = $jq{name} || $buddy->GetUserID(); |
---|
141 | |
---|
142 | $blistStr .= sprintf '%-15s %s', $name, $buddy->GetJID(); |
---|
143 | |
---|
144 | if ($res) { |
---|
145 | my %rq = $roster->resourceQuery( $buddy, $res ); |
---|
146 | $blistStr .= " [" . ( $rq{show} ? $rq{show} : 'online' ) . "]"; |
---|
147 | $blistStr .= " " . $rq{status} if $rq{status}; |
---|
148 | $blistStr = BarnOwl::Style::boldify($blistStr) if $showOffline; |
---|
149 | } |
---|
150 | else { |
---|
151 | return '' unless $showOffline; |
---|
152 | if ($jq{ask}) { |
---|
153 | $blistStr .= " [pending]"; |
---|
154 | } |
---|
155 | elsif ($jq{subscription} eq 'none' || $jq{subscription} eq 'from') { |
---|
156 | $blistStr .= " [not subscribed]"; |
---|
157 | } |
---|
158 | else { |
---|
159 | $blistStr .= " [offline]"; |
---|
160 | } |
---|
161 | } |
---|
162 | return $blistStr . "\n"; |
---|
163 | } |
---|
164 | |
---|
165 | sub getSingleBuddyList { |
---|
166 | my $jid = shift; |
---|
167 | $jid = resolveConnectedJID($jid); |
---|
168 | return "" unless $jid; |
---|
169 | my $blist = ""; |
---|
170 | my $roster = $conn->getRosterFromJID($jid); |
---|
171 | if ($roster) { |
---|
172 | $blist .= "\n" . BarnOwl::Style::boldify("Jabber Roster for $jid\n"); |
---|
173 | |
---|
174 | foreach my $group ( $roster->groups() ) { |
---|
175 | $blist .= " Group: $group\n"; |
---|
176 | my @buddies = $roster->jids( 'group', $group ); |
---|
177 | foreach my $buddy ( @buddies ) { |
---|
178 | $blist .= blist_listBuddy( $roster, $buddy ); |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | my @unsorted = $roster->jids('nogroup'); |
---|
183 | if (@unsorted) { |
---|
184 | $blist .= " [unsorted]\n"; |
---|
185 | foreach my $buddy (@unsorted) { |
---|
186 | $blist .= blist_listBuddy( $roster, $buddy ); |
---|
187 | } |
---|
188 | } |
---|
189 | } |
---|
190 | return $blist; |
---|
191 | } |
---|
192 | |
---|
193 | sub onGetBuddyList { |
---|
194 | $showOffline = BarnOwl::getvar('jabber:show_offline_buddies') eq 'on'; |
---|
195 | my $blist = ""; |
---|
196 | foreach my $jid ($conn->getJIDs()) { |
---|
197 | $blist .= getSingleBuddyList($jid); |
---|
198 | } |
---|
199 | return $blist; |
---|
200 | } |
---|
201 | |
---|
202 | ################################################################################ |
---|
203 | ### Owl Commands |
---|
204 | sub register_owl_commands() { |
---|
205 | BarnOwl::new_command( |
---|
206 | jabberlogin => \&cmd_login, |
---|
207 | { |
---|
208 | summary => "Log into jabber", |
---|
209 | usage => "jabberlogin JID [PASSWORD]" |
---|
210 | } |
---|
211 | ); |
---|
212 | BarnOwl::new_command( |
---|
213 | jabberlogout => \&cmd_logout, |
---|
214 | { summary => "Log out of jabber" } |
---|
215 | ); |
---|
216 | BarnOwl::new_command( |
---|
217 | jwrite => \&cmd_jwrite, |
---|
218 | { |
---|
219 | summary => "Send a Jabber Message", |
---|
220 | usage => "jwrite JID [-t thread] [-s subject]" |
---|
221 | } |
---|
222 | ); |
---|
223 | BarnOwl::new_command( |
---|
224 | jlist => \&cmd_jlist, |
---|
225 | { |
---|
226 | summary => "Show your Jabber roster.", |
---|
227 | usage => "jlist" |
---|
228 | } |
---|
229 | ); |
---|
230 | BarnOwl::new_command( |
---|
231 | jmuc => \&cmd_jmuc, |
---|
232 | { |
---|
233 | summary => "Jabber MUC related commands.", |
---|
234 | description => "jmuc sends jabber commands related to muc.\n\n" |
---|
235 | . "The following commands are available\n\n" |
---|
236 | . "join <muc> Join a muc.\n\n" |
---|
237 | . "part <muc> Part a muc.\n" |
---|
238 | . " The muc is taken from the current message if not supplied.\n\n" |
---|
239 | . "invite <jid> <muc>\n" |
---|
240 | . " Invite <jid> to <muc>.\n" |
---|
241 | . " The muc is taken from the current message if not supplied.\n\n" |
---|
242 | . "configure <muc>\n" |
---|
243 | . " Configures a MUC.\n" |
---|
244 | . " Necessary to initalize a new MUC.\n" |
---|
245 | . " At present, only the default configuration is supported.\n" |
---|
246 | . " The muc is taken from the current message if not supplied.\n\n" |
---|
247 | . "presence <muc>\n" |
---|
248 | . " Shows the roster for <muc>.\n" |
---|
249 | . " The muc is taken from the current message if not supplied.\n\n" |
---|
250 | . "presence -a\n" |
---|
251 | . " Shows rosters for all MUCs you're participating in.\n\n", |
---|
252 | usage => "jmuc COMMAND ARGS" |
---|
253 | } |
---|
254 | ); |
---|
255 | BarnOwl::new_command( |
---|
256 | jroster => \&cmd_jroster, |
---|
257 | { |
---|
258 | summary => "Jabber Roster related commands.", |
---|
259 | description => "jroster sends jabber commands related to rosters.\n\n" |
---|
260 | . "The following commands are available\n\n" |
---|
261 | . "sub <jid> Subscribe to <jid>'s presence. (implicit add)\n\n" |
---|
262 | . "add <jid> Adds <jid> to your roster.\n\n" |
---|
263 | . "unsub <jid> Unsubscribe from <jid>'s presence.\n\n" |
---|
264 | . "remove <jid> Removes <jid> to your roster. (implicit unsub)\n\n" |
---|
265 | . "auth <jid> Authorizes <jid> to subscribe to your presence.\n\n" |
---|
266 | . "deauth <jid> De-authorizes <jid>'s subscription to your presence.\n\n" |
---|
267 | . "The following arguments are supported for all commands\n\n" |
---|
268 | . "-a <jid> Specify which account to make the roster changes on.\n" |
---|
269 | . " Required if you're signed into more than one account.\n\n" |
---|
270 | . "The following arguments only work with the add and sub commands.\n\n" |
---|
271 | . "-g <group> Add <jid> to group <group>.\n" |
---|
272 | . " May be specified more than once, will not remove <jid> from any groups.\n\n" |
---|
273 | . "-p Purge. Removes <jid> from all groups.\n" |
---|
274 | . " May be combined with -g completely alter <jid>'s groups.\n\n" |
---|
275 | . "-n <name> Sets <name> as <jid>'s short name.\n\n" |
---|
276 | . "Note: Unless -n is used, you can specify multiple <jid> arguments.\n", |
---|
277 | usage => "jroster COMMAND ARGS" |
---|
278 | } |
---|
279 | ); |
---|
280 | } |
---|
281 | |
---|
282 | sub register_keybindings { |
---|
283 | BarnOwl::bindkey("recv j command start-command jwrite "); |
---|
284 | } |
---|
285 | |
---|
286 | sub register_filters { |
---|
287 | BarnOwl::filter('jabber type ^jabber$'); |
---|
288 | } |
---|
289 | |
---|
290 | sub cmd_login { |
---|
291 | my $cmd = shift; |
---|
292 | my $jid = new Net::Jabber::JID; |
---|
293 | $jid->SetJID(shift); |
---|
294 | my $password = ''; |
---|
295 | $password = shift if @_; |
---|
296 | |
---|
297 | my $uid = $jid->GetUserID(); |
---|
298 | my $componentname = $jid->GetServer(); |
---|
299 | my $resource = $jid->GetResource() || 'owl'; |
---|
300 | $jid->SetResource($resource); |
---|
301 | my $jidStr = $jid->GetJID('full'); |
---|
302 | |
---|
303 | if ( !$uid || !$componentname ) { |
---|
304 | BarnOwl::error("usage: $cmd JID"); |
---|
305 | return; |
---|
306 | } |
---|
307 | |
---|
308 | if ( $conn->jidExists($jidStr) ) { |
---|
309 | BarnOwl::error("Already logged in as $jidStr."); |
---|
310 | return; |
---|
311 | } |
---|
312 | |
---|
313 | my ( $server, $port ) = getServerFromJID($jid); |
---|
314 | |
---|
315 | $vars{jlogin_jid} = $jidStr; |
---|
316 | $vars{jlogin_connhash} = { |
---|
317 | hostname => $server, |
---|
318 | tls => 1, |
---|
319 | port => $port, |
---|
320 | componentname => $componentname |
---|
321 | }; |
---|
322 | $vars{jlogin_authhash} = |
---|
323 | { username => $uid, |
---|
324 | resource => $resource, |
---|
325 | }; |
---|
326 | |
---|
327 | return do_login($password); |
---|
328 | } |
---|
329 | |
---|
330 | sub do_login { |
---|
331 | $vars{jlogin_password} = shift; |
---|
332 | $vars{jlogin_authhash}->{password} = sub { return $vars{jlogin_password} || '' }; |
---|
333 | my $jidStr = $vars{jlogin_jid}; |
---|
334 | if ( !$jidStr && $vars{jlogin_havepass}) { |
---|
335 | BarnOwl::error("Got password but have no jid!"); |
---|
336 | } |
---|
337 | else |
---|
338 | { |
---|
339 | my $client = $conn->addConnection($jidStr); |
---|
340 | |
---|
341 | #XXX Todo: Add more callbacks. |
---|
342 | # * MUC presence handlers |
---|
343 | # We use the anonymous subrefs in order to have the correct behavior |
---|
344 | # when we reload |
---|
345 | $client->SetMessageCallBacks( |
---|
346 | chat => sub { BarnOwl::Module::Jabber::process_incoming_chat_message(@_) }, |
---|
347 | error => sub { BarnOwl::Module::Jabber::process_incoming_error_message(@_) }, |
---|
348 | groupchat => sub { BarnOwl::Module::Jabber::process_incoming_groupchat_message(@_) }, |
---|
349 | headline => sub { BarnOwl::Module::Jabber::process_incoming_headline_message(@_) }, |
---|
350 | normal => sub { BarnOwl::Module::Jabber::process_incoming_normal_message(@_) } |
---|
351 | ); |
---|
352 | $client->SetPresenceCallBacks( |
---|
353 | available => sub { BarnOwl::Module::Jabber::process_presence_available(@_) }, |
---|
354 | unavailable => sub { BarnOwl::Module::Jabber::process_presence_available(@_) }, |
---|
355 | subscribe => sub { BarnOwl::Module::Jabber::process_presence_subscribe(@_) }, |
---|
356 | subscribed => sub { BarnOwl::Module::Jabber::process_presence_subscribed(@_) }, |
---|
357 | unsubscribe => sub { BarnOwl::Module::Jabber::process_presence_unsubscribe(@_) }, |
---|
358 | unsubscribed => sub { BarnOwl::Module::Jabber::process_presence_unsubscribed(@_) }, |
---|
359 | error => sub { BarnOwl::Module::Jabber::process_presence_error(@_) }); |
---|
360 | |
---|
361 | my $status = $client->Connect( %{ $vars{jlogin_connhash} } ); |
---|
362 | if ( !$status ) { |
---|
363 | $conn->removeConnection($jidStr); |
---|
364 | BarnOwl::error("We failed to connect"); |
---|
365 | } else { |
---|
366 | my @result = $client->AuthSend( %{ $vars{jlogin_authhash} } ); |
---|
367 | |
---|
368 | if ( !@result || $result[0] ne 'ok' ) { |
---|
369 | if ( !$vars{jlogin_havepass} && ( !@result || $result[0] eq '401' || $result[0] eq 'error') ) { |
---|
370 | $vars{jlogin_havepass} = 1; |
---|
371 | $conn->removeConnection($jidStr); |
---|
372 | BarnOwl::start_password( "Password for $jidStr: ", \&do_login ); |
---|
373 | return ""; |
---|
374 | } |
---|
375 | $conn->removeConnection($jidStr); |
---|
376 | BarnOwl::error( "Error in connect: " . join( " ", @result ) ); |
---|
377 | } else { |
---|
378 | $conn->getRosterFromJID($jidStr)->fetch(); |
---|
379 | $client->PresenceSend( priority => 1 ); |
---|
380 | my $fullJid = $client->{SESSION}->{FULLJID} || $jidStr; |
---|
381 | $conn->renameConnection($jidStr, $fullJid); |
---|
382 | queue_admin_msg("Connected to jabber as $fullJid"); |
---|
383 | # The remove_dispatch() method is called from the |
---|
384 | # ConnectionManager's removeConnection() method. |
---|
385 | BarnOwl::add_dispatch($client->getSocket()->fileno(), sub { $client->OwlProcess() }); |
---|
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; |
---|