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