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