source: perl/modules/jabber.pl @ eaf9ed0

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