1 | package owl_jabber; |
---|
2 | use warnings; |
---|
3 | use strict; |
---|
4 | |
---|
5 | use Authen::SASL qw(Perl); |
---|
6 | use Net::Jabber; |
---|
7 | use Net::DNS; |
---|
8 | use Getopt::Long; |
---|
9 | |
---|
10 | ################################################################################ |
---|
11 | # owl perl jabber support |
---|
12 | # |
---|
13 | # XXX Todo: |
---|
14 | # Rosters for MUCs |
---|
15 | # More user feedback |
---|
16 | # * joining MUC |
---|
17 | # * parting MUC |
---|
18 | # * presence (Roster and MUC) |
---|
19 | # Implementing formatting and logging callbacks for C |
---|
20 | # Appropriate callbacks for presence subscription messages. |
---|
21 | # * Current behavior => auto-accept (default for Net::Jabber) |
---|
22 | # |
---|
23 | ################################################################################ |
---|
24 | |
---|
25 | our $connections; |
---|
26 | our %vars; |
---|
27 | |
---|
28 | sub onStart |
---|
29 | { |
---|
30 | if(eval{\&owl::queue_message}) |
---|
31 | { |
---|
32 | register_owl_commands(); |
---|
33 | push @::onMainLoop, sub { owl_jabber::onMainLoop(@_) }; |
---|
34 | push @::onGetBuddyList, sub { owl_jabber::onGetBuddyList(@_) }; |
---|
35 | } |
---|
36 | else |
---|
37 | { |
---|
38 | # Our owl doesn't support queue_message. Unfortunately, this |
---|
39 | # means it probably *also* doesn't support owl::error. So just |
---|
40 | # give up silently. |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | push @::onStartSubs, sub { owl_jabber::onStart(@_) }; |
---|
45 | |
---|
46 | sub onMainLoop |
---|
47 | { |
---|
48 | return if (!connected()); |
---|
49 | |
---|
50 | foreach my $jid (keys %$connections) |
---|
51 | { |
---|
52 | my $client = \$connections->{$jid}->{client}; |
---|
53 | |
---|
54 | my $status = $$client->Process(0); |
---|
55 | if(!defined($status)) { |
---|
56 | owl::error("Jabber account $jid disconnected!"); |
---|
57 | do_logout($jid); |
---|
58 | } |
---|
59 | if ($::shutdown) |
---|
60 | { |
---|
61 | do_logout($jid); |
---|
62 | return; |
---|
63 | } |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | sub blist_listBuddy |
---|
68 | { |
---|
69 | my $roster = shift; |
---|
70 | my $buddy = shift; |
---|
71 | my $blistStr .= " "; |
---|
72 | my %jq = $$roster->query($buddy); |
---|
73 | my $res = $$roster->resource($buddy); |
---|
74 | |
---|
75 | $blistStr .= $jq{name} ? $jq{name} : $buddy->GetJID(); |
---|
76 | |
---|
77 | if ($res) |
---|
78 | { |
---|
79 | my %rq = $$roster->resourceQuery($buddy, $res); |
---|
80 | $blistStr .= " [".($rq{show} ? $rq{show} : 'online')."]"; |
---|
81 | $blistStr .= " ".$rq{status} if $rq{status}; |
---|
82 | $blistStr = boldify($blistStr); |
---|
83 | } |
---|
84 | else |
---|
85 | { |
---|
86 | $blistStr .= $jq{ask} ? " [pending]" : " [offline]"; |
---|
87 | } |
---|
88 | |
---|
89 | return $blistStr."\n"; |
---|
90 | } |
---|
91 | |
---|
92 | sub onGetBuddyList |
---|
93 | { |
---|
94 | my $blist = ""; |
---|
95 | foreach my $jid (keys %{$connections}) |
---|
96 | { |
---|
97 | my $roster = \$connections->{$jid}->{roster}; |
---|
98 | if ($$roster) |
---|
99 | { |
---|
100 | $blist .= "\n".boldify("Jabber Roster for $jid\n"); |
---|
101 | |
---|
102 | foreach my $group ($$roster->groups()) |
---|
103 | { |
---|
104 | $blist .= " Group: $group\n"; |
---|
105 | foreach my $buddy ($$roster->jids('group',$group)) |
---|
106 | { |
---|
107 | $blist .= blist_listBuddy($roster, $buddy); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | my @unsorted = $$roster->jids('nogroup'); |
---|
112 | if (@unsorted) |
---|
113 | { |
---|
114 | $blist .= " [unsorted]\n"; |
---|
115 | foreach my $buddy (@unsorted) |
---|
116 | { |
---|
117 | $blist .= blist_listBuddy($roster, $buddy); |
---|
118 | } |
---|
119 | } |
---|
120 | } |
---|
121 | } |
---|
122 | return $blist; |
---|
123 | } |
---|
124 | |
---|
125 | ################################################################################ |
---|
126 | ### Owl Commands |
---|
127 | sub register_owl_commands() |
---|
128 | { |
---|
129 | owl::new_command( |
---|
130 | jabberlogin => \&cmd_login, |
---|
131 | { summary => "Log into jabber", } |
---|
132 | ); |
---|
133 | owl::new_command( |
---|
134 | jabberlogout => \&cmd_logout, |
---|
135 | { summary => "Log out of jabber" } |
---|
136 | ); |
---|
137 | owl::new_command( |
---|
138 | jwrite => \&cmd_jwrite, |
---|
139 | { |
---|
140 | summary => "Send a Jabber Message", |
---|
141 | usage => "jwrite JID [-g] [-t thread] [-s subject]" |
---|
142 | } |
---|
143 | ); |
---|
144 | owl::new_command( |
---|
145 | jlist => \&cmd_jlist, |
---|
146 | { |
---|
147 | summary => "Show your Jabber roster.", |
---|
148 | usage => "jlist" |
---|
149 | } |
---|
150 | ); |
---|
151 | owl::new_command( |
---|
152 | jmuc => \&cmd_jmuc, |
---|
153 | { |
---|
154 | summary => "Jabber MUC related commands.", |
---|
155 | description => "jmuc sends jabber commands related to muc.\n\n". |
---|
156 | "The following commands are available\n\n". |
---|
157 | "join {muc} Join a muc.\n\n". |
---|
158 | "part [muc] Part a muc.". |
---|
159 | " The muc is taken from the current message if not supplied.\n\n". |
---|
160 | "invite {jid} [muc]\n\n". |
---|
161 | " Invite {jid} to [muc].\n". |
---|
162 | " The muc is taken from the current message if not supplied.\n\n", |
---|
163 | usage => "jmuc {command} {args}" |
---|
164 | } |
---|
165 | ); |
---|
166 | } |
---|
167 | |
---|
168 | sub cmd_login |
---|
169 | { |
---|
170 | my $cmd = shift; |
---|
171 | my $jid = new Net::XMPP::JID; |
---|
172 | $jid->SetJID(shift); |
---|
173 | |
---|
174 | my $uid = $jid->GetUserID(); |
---|
175 | my $componentname = $jid->GetServer(); |
---|
176 | my $resource = $jid->GetResource() || 'owl'; |
---|
177 | $jid->SetResource($resource); |
---|
178 | my $jidStr = $jid->GetJID('full'); |
---|
179 | |
---|
180 | if (!$uid || !$componentname) |
---|
181 | { |
---|
182 | owl::error("usage: $cmd {jid}"); |
---|
183 | return; |
---|
184 | } |
---|
185 | |
---|
186 | if ($connections->{$jidStr}) |
---|
187 | { |
---|
188 | owl::error("Already logged in as $jidStr."); |
---|
189 | return; |
---|
190 | } |
---|
191 | |
---|
192 | my ($server, $port) = getServerFromJID($jid); |
---|
193 | |
---|
194 | $connections->{$jidStr}->{client} = Net::Jabber::Client->new(debuglevel => owl::getvar('debug') eq 'on' ? 1 : 0, |
---|
195 | debugfile => 'jabber.log'); |
---|
196 | my $client = \$connections->{$jidStr}->{client}; |
---|
197 | $connections->{$jidStr}->{roster} = $connections->{$jidStr}->{client}->Roster(); |
---|
198 | |
---|
199 | #XXX Todo: Add more callbacks. |
---|
200 | # MUC presence handlers |
---|
201 | $$client->SetMessageCallBacks(chat => sub { owl_jabber::process_incoming_chat_message(@_) }, |
---|
202 | error => sub { owl_jabber::process_incoming_error_message(@_) }, |
---|
203 | groupchat => sub { owl_jabber::process_incoming_groupchat_message(@_) }, |
---|
204 | headline => sub { owl_jabber::process_incoming_headline_message(@_) }, |
---|
205 | normal => sub { owl_jabber::process_incoming_normal_message(@_) }); |
---|
206 | |
---|
207 | $vars{jlogin_connhash} = {hostname => $server, |
---|
208 | tls => 1, |
---|
209 | port => $port, |
---|
210 | componentname => $componentname}; |
---|
211 | |
---|
212 | my $status = $$client->Connect(%{$vars{jlogin_connhash}}); |
---|
213 | |
---|
214 | if (!$status) |
---|
215 | { |
---|
216 | delete $connections->{$jidStr}; |
---|
217 | delete $vars{jlogin_connhash}; |
---|
218 | owl::error("We failed to connect"); |
---|
219 | return ""; |
---|
220 | } |
---|
221 | |
---|
222 | |
---|
223 | $vars{jlogin_authhash} = {username => $uid, resource => $resource, password => ''}; |
---|
224 | my @result = $$client->AuthSend(%{$vars{jlogin_authhash}}); |
---|
225 | if($result[0] ne 'ok') |
---|
226 | { |
---|
227 | if ($result[1] == 401) |
---|
228 | { |
---|
229 | $vars{jlogin_jid} = $jidStr; |
---|
230 | delete $connections->{$jidStr}; |
---|
231 | owl::start_password("Password for $jidStr: ", \&do_login_with_pw); |
---|
232 | return ""; |
---|
233 | } |
---|
234 | owl::error("Error in connect: " . join(" ", $result[1..$#result])); |
---|
235 | do_logout($jidStr); |
---|
236 | delete $vars{jlogin_connhash}; |
---|
237 | delete $vars{jlogin_authhash}; |
---|
238 | return ""; |
---|
239 | } |
---|
240 | $connections->{$jidStr}->{roster}->fetch(); |
---|
241 | $$client->PresenceSend(priority => 1); |
---|
242 | queue_admin_msg("Connected to jabber as $jidStr"); |
---|
243 | delete $vars{jlogin_connhash}; |
---|
244 | delete $vars{jlogin_authhash}; |
---|
245 | return ""; |
---|
246 | } |
---|
247 | |
---|
248 | sub do_login_with_pw |
---|
249 | { |
---|
250 | $vars{jlogin_authhash}->{password} = shift; |
---|
251 | my $jidStr = delete $vars{jlogin_jid}; |
---|
252 | if (!$jidStr) |
---|
253 | { |
---|
254 | owl::error("Got password but have no jid!"); |
---|
255 | } |
---|
256 | |
---|
257 | $connections->{$jidStr}->{client} = Net::Jabber::Client->new(); |
---|
258 | my $client = \$connections->{$jidStr}->{client}; |
---|
259 | $connections->{$jidStr}->{roster} = $connections->{$jidStr}->{client}->Roster(); |
---|
260 | |
---|
261 | $$client->SetMessageCallBacks(chat => sub { owl_jabber::process_incoming_chat_message(@_) }, |
---|
262 | error => sub { owl_jabber::process_incoming_error_message(@_) }, |
---|
263 | groupchat => sub { owl_jabber::process_incoming_groupchat_message(@_) }, |
---|
264 | headline => sub { owl_jabber::process_incoming_headline_message(@_) }, |
---|
265 | normal => sub { owl_jabber::process_incoming_normal_message(@_) }); |
---|
266 | |
---|
267 | my $status = $$client->Connect(%{$vars{jlogin_connhash}}); |
---|
268 | if (!$status) |
---|
269 | { |
---|
270 | delete $connections->{$jidStr}; |
---|
271 | delete $vars{jlogin_connhash}; |
---|
272 | delete $vars{jlogin_authhash}; |
---|
273 | owl::error("We failed to connect"); |
---|
274 | return ""; |
---|
275 | } |
---|
276 | |
---|
277 | my @result = $$client->AuthSend(%{$vars{jlogin_authhash}}); |
---|
278 | |
---|
279 | if($result[0] ne 'ok') |
---|
280 | { |
---|
281 | owl::error("Error in connect: " . join(" ", $result[1..$#result])); |
---|
282 | do_logout($jidStr); |
---|
283 | delete $vars{jlogin_connhash}; |
---|
284 | delete $vars{jlogin_authhash}; |
---|
285 | return ""; |
---|
286 | } |
---|
287 | |
---|
288 | $connections->{$jidStr}->{roster}->fetch(); |
---|
289 | $$client->PresenceSend(priority => 1); |
---|
290 | queue_admin_msg("Connected to jabber as $jidStr"); |
---|
291 | delete $vars{jlogin_connhash}; |
---|
292 | delete $vars{jlogin_authhash}; |
---|
293 | return ""; |
---|
294 | } |
---|
295 | |
---|
296 | sub do_logout |
---|
297 | { |
---|
298 | my $jid = shift; |
---|
299 | $connections->{$jid}->{client}->Disconnect(); |
---|
300 | delete $connections->{$jid}; |
---|
301 | queue_admin_msg("Jabber disconnected ($jid)."); |
---|
302 | } |
---|
303 | |
---|
304 | sub cmd_logout |
---|
305 | { |
---|
306 | # Logged into multiple accounts |
---|
307 | if (connected() > 1) |
---|
308 | { |
---|
309 | # Logged into multiple accounts, no accout specified. |
---|
310 | if (!$_[1]) |
---|
311 | { |
---|
312 | my $errStr = "You are logged into multiple accounts. Please specify an account to log out of.\n"; |
---|
313 | foreach my $jid (keys %$connections) |
---|
314 | { |
---|
315 | $errStr .= "\t$jid\n"; |
---|
316 | } |
---|
317 | queue_admin_msg($errStr); |
---|
318 | } |
---|
319 | # Logged into multiple accounts, account specified. |
---|
320 | else |
---|
321 | { |
---|
322 | if ($_[1] eq '-a') #All accounts. |
---|
323 | { |
---|
324 | foreach my $jid (keys %$connections) |
---|
325 | { |
---|
326 | do_logout($jid); |
---|
327 | } |
---|
328 | } |
---|
329 | else #One account. |
---|
330 | { |
---|
331 | my $jid = resolveJID($_[1]); |
---|
332 | do_logout($jid) if ($jid ne ''); |
---|
333 | } |
---|
334 | } |
---|
335 | } |
---|
336 | else # Only one account logged in. |
---|
337 | { |
---|
338 | |
---|
339 | do_logout((keys %$connections)[0]); |
---|
340 | } |
---|
341 | return ""; |
---|
342 | } |
---|
343 | |
---|
344 | sub cmd_jlist |
---|
345 | { |
---|
346 | if (!(scalar keys %$connections)) |
---|
347 | { |
---|
348 | owl::error("You are not logged in to Jabber."); |
---|
349 | return; |
---|
350 | } |
---|
351 | owl::popless_ztext(onGetBuddyList()); |
---|
352 | } |
---|
353 | |
---|
354 | sub cmd_jwrite |
---|
355 | { |
---|
356 | if (!connected()) |
---|
357 | { |
---|
358 | owl::error("You are not logged in to Jabber."); |
---|
359 | return; |
---|
360 | } |
---|
361 | |
---|
362 | my $jwrite_to = ""; |
---|
363 | my $jwrite_from = ""; |
---|
364 | my $jwrite_thread = ""; |
---|
365 | my $jwrite_subject = ""; |
---|
366 | my $jwrite_type = "chat"; |
---|
367 | |
---|
368 | my @args = @_; |
---|
369 | shift; |
---|
370 | local @ARGV = @_; |
---|
371 | my $gc; |
---|
372 | GetOptions('thread=s' => \$jwrite_thread, |
---|
373 | 'subject=s' => \$jwrite_subject, |
---|
374 | 'account=s' => \$jwrite_from, |
---|
375 | 'groupchat' => \$gc); |
---|
376 | $jwrite_type = 'groupchat' if $gc; |
---|
377 | |
---|
378 | if (scalar @ARGV != 1) |
---|
379 | { |
---|
380 | owl::error("Usage: jwrite JID [-g] [-t thread] [-s 'subject'] [-a account]"); |
---|
381 | return; |
---|
382 | } |
---|
383 | else |
---|
384 | { |
---|
385 | $jwrite_to = shift @ARGV; |
---|
386 | } |
---|
387 | |
---|
388 | if (!$jwrite_from) |
---|
389 | { |
---|
390 | if (connected() == 1) |
---|
391 | { |
---|
392 | $jwrite_from = (keys %$connections)[0]; |
---|
393 | } |
---|
394 | else |
---|
395 | { |
---|
396 | owl::error("Please specify an account with -a {jid}"); |
---|
397 | return; |
---|
398 | } |
---|
399 | } |
---|
400 | else |
---|
401 | { |
---|
402 | $jwrite_from = resolveJID($jwrite_from); |
---|
403 | return unless $jwrite_from; |
---|
404 | } |
---|
405 | |
---|
406 | $vars{jwrite} = {to => $jwrite_to, |
---|
407 | from => $jwrite_from, |
---|
408 | subject => $jwrite_subject, |
---|
409 | thread => $jwrite_thread, |
---|
410 | type => $jwrite_type}; |
---|
411 | |
---|
412 | owl::message("Type your message below. End with a dot on a line by itself. ^C will quit."); |
---|
413 | owl::start_edit_win(join(' ', @args), \&process_owl_jwrite); |
---|
414 | } |
---|
415 | |
---|
416 | sub cmd_jmuc |
---|
417 | { |
---|
418 | die "You are not logged in to Jabber" unless connected(); |
---|
419 | my $ocmd = shift; |
---|
420 | my $cmd = shift; |
---|
421 | if (!$cmd) |
---|
422 | { |
---|
423 | #XXX TODO: Write general usage for jmuc command. |
---|
424 | return; |
---|
425 | } |
---|
426 | |
---|
427 | my %jmuc_commands = ( |
---|
428 | join => \&jmuc_join, |
---|
429 | part => \&jmuc_part, |
---|
430 | invite => \&jmuc_invite, |
---|
431 | configure => \&jmuc_configure |
---|
432 | ); |
---|
433 | my $func = $jmuc_commands{$cmd}; |
---|
434 | if(!$func) { |
---|
435 | owl::error("jmuc: Unknown command: $cmd"); |
---|
436 | return; |
---|
437 | } |
---|
438 | |
---|
439 | { |
---|
440 | local @ARGV = @_; |
---|
441 | my $jid; |
---|
442 | my $muc; |
---|
443 | my $m = owl::getcurmsg(); |
---|
444 | if ($m->is_jabber && $m->{jtype} eq 'groupchat') |
---|
445 | { |
---|
446 | $muc = $m->{room}; |
---|
447 | $jid = $m->{to}; |
---|
448 | } |
---|
449 | |
---|
450 | my $getopt = Getopt::Long::Parser->new; |
---|
451 | $getopt->configure('pass_through'); |
---|
452 | $getopt->getoptions('account=s' => \$jid); |
---|
453 | $jid ||= defaultJID(); |
---|
454 | if($jid) { |
---|
455 | $jid = resolveJID($jid); |
---|
456 | return unless $jid; |
---|
457 | } else { |
---|
458 | owl::error('You must specify an account with -a {jid}'); |
---|
459 | } |
---|
460 | return $func->($jid, $muc, @ARGV); |
---|
461 | } |
---|
462 | } |
---|
463 | |
---|
464 | sub jmuc_join { |
---|
465 | my ($jid, $muc, @args) = @_; |
---|
466 | local @ARGV = @args; |
---|
467 | my $password; |
---|
468 | GetOptions('password=s' => \$password); |
---|
469 | |
---|
470 | $muc = shift @ARGV or die("Usage: jmuc join {muc} [-p password] [-a account]"); |
---|
471 | |
---|
472 | my $x = new XML::Stream::Node('x'); |
---|
473 | $x->put_attrib(xmlns => 'http://jabber.org/protocol/muc'); |
---|
474 | $x->add_child('history')->put_attrib(maxchars => '0'); |
---|
475 | |
---|
476 | if ($password) |
---|
477 | { |
---|
478 | $x->add_child('password')->add_cdata($password); |
---|
479 | } |
---|
480 | |
---|
481 | my $presence = new Net::Jabber::Presence; |
---|
482 | $presence->SetPresence(to => $muc); |
---|
483 | $presence->AddX($x); |
---|
484 | $connections->{$jid}->{client}->Send($presence); |
---|
485 | } |
---|
486 | |
---|
487 | sub jmuc_part { |
---|
488 | my ($jid, $muc, @args) = @_; |
---|
489 | |
---|
490 | $muc = shift @args if scalar @args; |
---|
491 | die("Usage: jmuc part {muc} [-a account]") unless $muc; |
---|
492 | |
---|
493 | $connections->{$jid}->{client}->PresenceSend(to => $muc, type => 'unavailable'); |
---|
494 | queue_admin_msg("$jid has left $muc."); |
---|
495 | } |
---|
496 | |
---|
497 | sub jmuc_invite |
---|
498 | { |
---|
499 | my ($jid, $muc, @args) = @_; |
---|
500 | |
---|
501 | my $invite_jid = shift @args; |
---|
502 | $muc = shift @args if scalar @args; |
---|
503 | |
---|
504 | die('Usage: jmuc invite {jid} [muc] [-a account]') unless $muc && $invite_jid; |
---|
505 | |
---|
506 | my $x = new XML::Stream::Node('x'); |
---|
507 | $x->put_attrib(xmlns => 'http://jabber.org/protocol/muc#user'); |
---|
508 | $x->add_child('invite')->put_attrib(to => $invite_jid); |
---|
509 | |
---|
510 | my $message = new Net::Jabber::Message; |
---|
511 | $message->SetTo($muc); |
---|
512 | $message->AddX($x); |
---|
513 | $connections->{$jid}->{client}->Send($message); |
---|
514 | queue_admin_msg("$jid has invited $invite_jid to $muc."); |
---|
515 | } |
---|
516 | |
---|
517 | Net::Jabber::Namespaces::add_ns( |
---|
518 | ns => "http://jabber.org/protocol/muc#owner", |
---|
519 | tag => 'query', |
---|
520 | ); |
---|
521 | |
---|
522 | sub jmuc_configure { |
---|
523 | my ($jid, $muc, @args) = @_; |
---|
524 | $muc = shift @args if scalar @args; |
---|
525 | die("Usage: jmuc configure [muc]") unless $muc; |
---|
526 | my $iq = Net::Jabber::IQ->new(); |
---|
527 | $iq->SetTo($muc); |
---|
528 | $iq->SetType('set'); |
---|
529 | my $query = $iq->NewQuery("http://jabber.org/protocol/muc#owner"); |
---|
530 | my $x = $query->NewChild("jabber:x:data"); |
---|
531 | $x->SetType('submit'); |
---|
532 | |
---|
533 | $connections->{$jid}->{client}->Send($iq); |
---|
534 | queue_admin_msg("Accepted default instant configuration for $muc"); |
---|
535 | } |
---|
536 | |
---|
537 | |
---|
538 | ################################################################################ |
---|
539 | ### Owl Callbacks |
---|
540 | sub process_owl_jwrite |
---|
541 | { |
---|
542 | my $body = shift; |
---|
543 | |
---|
544 | my $j = new Net::XMPP::Message; |
---|
545 | $body =~ s/\n\z//; |
---|
546 | $j->SetMessage(to => $vars{jwrite}{to}, |
---|
547 | from => $vars{jwrite}{from}, |
---|
548 | type => $vars{jwrite}{type}, |
---|
549 | body => $body |
---|
550 | ); |
---|
551 | $j->SetThread($vars{jwrite}{thread}) if ($vars{jwrite}{thread}); |
---|
552 | $j->SetSubject($vars{jwrite}{subject}) if ($vars{jwrite}{subject}); |
---|
553 | |
---|
554 | my $m = j2o($j, 'out'); |
---|
555 | if ($vars{jwrite}{type} ne 'groupchat') |
---|
556 | { |
---|
557 | #XXX TODO: Check for displayoutgoing. |
---|
558 | owl::queue_message($m); |
---|
559 | } |
---|
560 | $connections->{$vars{jwrite}{from}}->{client}->Send($j); |
---|
561 | delete $vars{jwrite}; |
---|
562 | } |
---|
563 | |
---|
564 | ### XMPP Callbacks |
---|
565 | |
---|
566 | sub process_incoming_chat_message |
---|
567 | { |
---|
568 | my ($session, $j) = @_; |
---|
569 | owl::queue_message(j2o($j, 'in')); |
---|
570 | } |
---|
571 | |
---|
572 | sub process_incoming_error_message |
---|
573 | { |
---|
574 | my ($session, $j) = @_; |
---|
575 | my %jhash = j2hash($j, 'in'); |
---|
576 | $jhash{type} = 'admin'; |
---|
577 | owl::queue_message(owl::Message->new(%jhash)); |
---|
578 | } |
---|
579 | |
---|
580 | sub process_incoming_groupchat_message |
---|
581 | { |
---|
582 | my ($session, $j) = @_; |
---|
583 | # HACK IN PROGRESS (ignoring delayed messages) |
---|
584 | return if ($j->DefinedX('jabber:x:delay') && $j->GetX('jabber:x:delay')); |
---|
585 | owl::queue_message(j2o($j, 'in')); |
---|
586 | } |
---|
587 | |
---|
588 | sub process_incoming_headline_message |
---|
589 | { |
---|
590 | my ($session, $j) = @_; |
---|
591 | owl::queue_message(j2o($j, 'in')); |
---|
592 | } |
---|
593 | |
---|
594 | sub process_incoming_normal_message |
---|
595 | { |
---|
596 | my ($session, $j) = @_; |
---|
597 | my %props = j2hash($j, 'in'); |
---|
598 | |
---|
599 | # XXX TODO: handle things such as MUC invites here. |
---|
600 | |
---|
601 | # if ($j->HasX('http://jabber.org/protocol/muc#user')) |
---|
602 | # { |
---|
603 | # my $x = $j->GetX('http://jabber.org/protocol/muc#user'); |
---|
604 | # if ($x->HasChild('invite')) |
---|
605 | # { |
---|
606 | # $props |
---|
607 | # } |
---|
608 | # } |
---|
609 | # |
---|
610 | owl::queue_message(owl::Message->new(%props)); |
---|
611 | } |
---|
612 | |
---|
613 | sub process_muc_presence |
---|
614 | { |
---|
615 | my ($session, $p) = @_; |
---|
616 | return unless ($p->HasX('http://jabber.org/protocol/muc#user')); |
---|
617 | |
---|
618 | } |
---|
619 | |
---|
620 | |
---|
621 | ### Helper functions |
---|
622 | |
---|
623 | sub j2hash |
---|
624 | { |
---|
625 | my $j = shift; |
---|
626 | my $dir = shift; |
---|
627 | |
---|
628 | my %props = (type => 'jabber', |
---|
629 | direction => $dir); |
---|
630 | |
---|
631 | my $jtype = $props{jtype} = $j->GetType(); |
---|
632 | my $from = $j->GetFrom('jid'); |
---|
633 | my $to = $j->GetTo('jid'); |
---|
634 | |
---|
635 | $props{from} = $from->GetJID('full'); |
---|
636 | $props{to} = $to->GetJID('full'); |
---|
637 | |
---|
638 | $props{recipient} = $to->GetJID('base'); |
---|
639 | $props{sender} = $from->GetJID('base'); |
---|
640 | $props{subject} = $j->GetSubject() if ($j->DefinedSubject()); |
---|
641 | $props{thread} = $j->GetThread() if ($j->DefinedThread()); |
---|
642 | $props{body} = $j->GetBody() if ($j->DefinedBody()); |
---|
643 | $props{error} = $j->GetError() if ($j->DefinedError()); |
---|
644 | $props{error_code} = $j->GetErrorCode() if ($j->DefinedErrorCode()); |
---|
645 | $props{xml} = $j->GetXML(); |
---|
646 | |
---|
647 | if ($jtype eq 'chat') |
---|
648 | { |
---|
649 | $props{replycmd} = "jwrite ".(($dir eq 'in') ? $props{from} : $props{to}); |
---|
650 | $props{replycmd} .= " -a ".(($dir eq 'out') ? $props{from} : $props{to}); |
---|
651 | $props{isprivate} = 1; |
---|
652 | } |
---|
653 | elsif ($jtype eq 'groupchat') |
---|
654 | { |
---|
655 | my $nick = $props{nick} = $from->GetResource(); |
---|
656 | my $room = $props{room} = $from->GetJID('base'); |
---|
657 | $props{replycmd} = "jwrite -g $room"; |
---|
658 | $props{replycmd} .= " -a ".(($dir eq 'out') ? $props{from} : $props{to}); |
---|
659 | |
---|
660 | $props{sender} = $nick || $room; |
---|
661 | $props{recipient} = $room; |
---|
662 | |
---|
663 | if ($props{subject} && !$props{body}) |
---|
664 | { |
---|
665 | $props{body} = '['.$nick." has set the topic to: ".$props{subject}."]" |
---|
666 | } |
---|
667 | } |
---|
668 | elsif ($jtype eq 'normal') |
---|
669 | { |
---|
670 | $props{replycmd} = undef; |
---|
671 | $props{isprivate} = 1; |
---|
672 | } |
---|
673 | elsif ($jtype eq 'headline') |
---|
674 | { |
---|
675 | $props{replycmd} = undef; |
---|
676 | } |
---|
677 | elsif ($jtype eq 'error') |
---|
678 | { |
---|
679 | $props{replycmd} = undef; |
---|
680 | $props{body} = "Error ".$props{error_code}." sending to ".$props{from}."\n".$props{error}; |
---|
681 | } |
---|
682 | |
---|
683 | $props{replysendercmd} = $props{replycmd}; |
---|
684 | return %props; |
---|
685 | } |
---|
686 | |
---|
687 | sub j2o |
---|
688 | { |
---|
689 | return owl::Message->new(j2hash(@_)); |
---|
690 | } |
---|
691 | |
---|
692 | sub queue_admin_msg |
---|
693 | { |
---|
694 | my $err = shift; |
---|
695 | my $m = owl::Message->new(type => 'admin', |
---|
696 | direction => 'none', |
---|
697 | body => $err); |
---|
698 | owl::queue_message($m); |
---|
699 | } |
---|
700 | |
---|
701 | sub boldify($) |
---|
702 | { |
---|
703 | my $str = shift; |
---|
704 | |
---|
705 | return '@b('.$str.')' if ( $str !~ /\)/ ); |
---|
706 | return '@b<'.$str.'>' if ( $str !~ /\>/ ); |
---|
707 | return '@b{'.$str.'}' if ( $str !~ /\}/ ); |
---|
708 | return '@b['.$str.']' if ( $str !~ /\]/ ); |
---|
709 | |
---|
710 | my $txt = "\@b($str"; |
---|
711 | $txt =~ s/\)/\)\@b\[\)\]\@b\(/g; |
---|
712 | return $txt.')'; |
---|
713 | } |
---|
714 | |
---|
715 | sub getServerFromJID |
---|
716 | { |
---|
717 | my $jid = shift; |
---|
718 | my $res = new Net::DNS::Resolver; |
---|
719 | my $packet = $res->search('_xmpp-client._tcp.'.$jid->GetServer(), 'srv'); |
---|
720 | |
---|
721 | if ($packet) # Got srv record. |
---|
722 | { |
---|
723 | my @answer = $packet->answer; |
---|
724 | return $answer[0]{target}, |
---|
725 | $answer[0]{port}; |
---|
726 | } |
---|
727 | |
---|
728 | return $jid->GetServer(), 5222; |
---|
729 | } |
---|
730 | |
---|
731 | sub connected |
---|
732 | { |
---|
733 | return scalar keys %$connections; |
---|
734 | } |
---|
735 | |
---|
736 | sub defaultJID { |
---|
737 | return (keys %$connections)[0] if (connected() == 1); |
---|
738 | return; |
---|
739 | } |
---|
740 | |
---|
741 | sub resolveJID |
---|
742 | { |
---|
743 | my $givenJidStr = shift; |
---|
744 | my $givenJid = new Net::XMPP::JID; |
---|
745 | $givenJid->SetJID($givenJidStr); |
---|
746 | |
---|
747 | # Account fully specified. |
---|
748 | if ($givenJid->GetResource()) |
---|
749 | { |
---|
750 | # Specified account exists |
---|
751 | if (defined $connections->{$givenJidStr}) |
---|
752 | { |
---|
753 | return $givenJidStr; |
---|
754 | } |
---|
755 | else #Specified account doesn't exist |
---|
756 | { |
---|
757 | owl::error("Invalid account: $givenJidStr"); |
---|
758 | } |
---|
759 | } |
---|
760 | # Disambiguate. |
---|
761 | else |
---|
762 | { |
---|
763 | my $matchingJid = ""; |
---|
764 | my $errStr = "Ambiguous account reference. Please specify a resource.\n"; |
---|
765 | my $ambiguous = 0; |
---|
766 | |
---|
767 | foreach my $jid (keys %$connections) |
---|
768 | { |
---|
769 | my $cJid = new Net::XMPP::JID; |
---|
770 | $cJid->SetJID($jid); |
---|
771 | if ($givenJidStr eq $cJid->GetJID('base')) |
---|
772 | { |
---|
773 | $ambiguous = 1 if ($matchingJid ne ""); |
---|
774 | $matchingJid = $jid; |
---|
775 | $errStr .= "\t$jid\n"; |
---|
776 | } |
---|
777 | } |
---|
778 | # Need further disambiguation. |
---|
779 | if ($ambiguous) |
---|
780 | { |
---|
781 | queue_admin_msg($errStr); |
---|
782 | } |
---|
783 | # Not one of ours. |
---|
784 | elsif ($matchingJid eq "") |
---|
785 | { |
---|
786 | owl::error("Invalid account: $givenJidStr"); |
---|
787 | } |
---|
788 | # Log out this one. |
---|
789 | else |
---|
790 | { |
---|
791 | return $matchingJid; |
---|
792 | } |
---|
793 | } |
---|
794 | return ""; |
---|
795 | } |
---|