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