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