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