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