source: perl/modules/jabber.pl @ 6e9e50e

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 6e9e50e was 6e9e50e, checked in by Nelson Elhage <nelhage@mit.edu>, 17 years ago
Update to use new N::J MUC features
  • Property mode set to 100644
File size: 21.1 KB
RevLine 
[b405ff6]1# -*- mode: cperl; cperl-indent-level: 4; indent-tabs-mode: nil -*-
[38ffdf9]2package owl_jabber;
[9f183ff]3use warnings;
4use strict;
5
[38ffdf9]6use Authen::SASL qw(Perl);
7use Net::Jabber;
[6a6dd47]8use Net::DNS;
9use Getopt::Long;
10
[38ffdf9]11################################################################################
12# owl perl jabber support
13#
[b6a253c]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)
[38ffdf9]23#
24################################################################################
25
[6a6dd47]26our $connections;
27our %vars;
[38ffdf9]28
[b405ff6]29sub 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(@_) };
[38ffdf9]34    }
[b405ff6]35    else {
36
[38ffdf9]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}
[9f183ff]42
[b6a253c]43push @::onStartSubs, sub { owl_jabber::onStart(@_) };
[38ffdf9]44
[b405ff6]45sub onMainLoop {
46    return if ( !connected() );
[6a6dd47]47
[b405ff6]48    foreach my $jid ( keys %$connections ) {
49        my $client = \$connections->{$jid}->{client};
50
51        my $status = $$client->Process(0);
52        if ( !defined($status) ) {
[960395d]53            owl::error("Jabber account $jid disconnected!");
54            do_logout($jid);
55        }
[b405ff6]56        if ($::shutdown) {
57            do_logout($jid);
58            return;
59        }
[38ffdf9]60    }
61}
[b6a253c]62
[b405ff6]63sub blist_listBuddy {
[6a6dd47]64    my $roster = shift;
[b405ff6]65    my $buddy  = shift;
[b6a253c]66    my $blistStr .= "    ";
[b405ff6]67    my %jq  = $$roster->query($buddy);
[6a6dd47]68    my $res = $$roster->resource($buddy);
[b6a253c]69
70    $blistStr .= $jq{name} ? $jq{name} : $buddy->GetJID();
[b405ff6]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);
[b6a253c]77    }
[b405ff6]78    else {
79        $blistStr .= $jq{ask} ? " [pending]" : " [offline]";
[b6a253c]80    }
81
[b405ff6]82    return $blistStr . "\n";
[b6a253c]83}
84
[b405ff6]85sub onGetBuddyList {
[6a6dd47]86    my $blist = "";
[b405ff6]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        }
[b6a253c]107    }
[6a6dd47]108    return $blist;
[b6a253c]109}
[38ffdf9]110
111################################################################################
112### Owl Commands
[b405ff6]113sub register_owl_commands() {
[38ffdf9]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        {
[b405ff6]125            summary => "Send a Jabber Message",
126            usage   => "jwrite JID [-g] [-t thread] [-s subject]"
[38ffdf9]127        }
128    );
129    owl::new_command(
[b6a253c]130        jlist => \&cmd_jlist,
[38ffdf9]131        {
[b405ff6]132            summary => "Show your Jabber roster.",
133            usage   => "jlist"
[38ffdf9]134        }
135    );
136    owl::new_command(
[b6a253c]137        jmuc => \&cmd_jmuc,
[38ffdf9]138        {
[b6a253c]139            summary     => "Jabber MUC related commands.",
[b405ff6]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"
[d9f4a5c]148              . "configure [muc]\n"
149              . "            Configure [muc].\n"
[b405ff6]150              . "            Necessary to initalize a new MUC",
151            usage => "jmuc {command} {args}"
[38ffdf9]152        }
153    );
154}
155
[b405ff6]156sub cmd_login {
[6a6dd47]157    my $cmd = shift;
158    my $jid = new Net::XMPP::JID;
159    $jid->SetJID(shift);
[b405ff6]160
161    my $uid           = $jid->GetUserID();
[6a6dd47]162    my $componentname = $jid->GetServer();
[b405ff6]163    my $resource      = $jid->GetResource() || 'owl';
[6a6dd47]164    $jid->SetResource($resource);
165    my $jidStr = $jid->GetJID('full');
166
[b405ff6]167    if ( !$uid || !$componentname ) {
168        owl::error("usage: $cmd {jid}");
169        return;
[38ffdf9]170    }
[b6a253c]171
[b405ff6]172    if ( $connections->{$jidStr} ) {
173        owl::error("Already logged in as $jidStr.");
174        return;
[6a6dd47]175    }
176
[b405ff6]177    my ( $server, $port ) = getServerFromJID($jid);
[6a6dd47]178
[b405ff6]179    $connections->{$jidStr}->{client} = Net::Jabber::Client->new(
180        debuglevel => owl::getvar('debug') eq 'on' ? 1 : 0,
181        debugfile => 'jabber.log'
182    );
[6a6dd47]183    my $client = \$connections->{$jidStr}->{client};
[b405ff6]184    $connections->{$jidStr}->{roster} =
185      $connections->{$jidStr}->{client}->Roster();
[b6a253c]186
187    #XXX Todo: Add more callbacks.
188    # MUC presence handlers
[b405ff6]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    );
[6a6dd47]196
[b405ff6]197    $vars{jlogin_connhash} = {
198        hostname      => $server,
199        tls           => 1,
200        port          => $port,
201        componentname => $componentname
202    };
[6a6dd47]203
[b405ff6]204    my $status = $$client->Connect( %{ $vars{jlogin_connhash} } );
[6a6dd47]205
[b405ff6]206    if ( !$status ) {
207        delete $connections->{$jidStr};
208        delete $vars{jlogin_connhash};
209        owl::error("We failed to connect");
210        return "";
[38ffdf9]211    }
212
[b405ff6]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};
[6a6dd47]228        return "";
[38ffdf9]229    }
[6a6dd47]230    $connections->{$jidStr}->{roster}->fetch();
[b405ff6]231    $$client->PresenceSend( priority => 1 );
[6a6dd47]232    queue_admin_msg("Connected to jabber as $jidStr");
233    delete $vars{jlogin_connhash};
234    delete $vars{jlogin_authhash};
235    return "";
236}
[38ffdf9]237
[b405ff6]238sub do_login_with_pw {
[6a6dd47]239    $vars{jlogin_authhash}->{password} = shift;
240    my $jidStr = delete $vars{jlogin_jid};
[b405ff6]241    if ( !$jidStr ) {
242        owl::error("Got password but have no jid!");
[6a6dd47]243    }
244
245    $connections->{$jidStr}->{client} = Net::Jabber::Client->new();
246    my $client = \$connections->{$jidStr}->{client};
[b405ff6]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    );
[38ffdf9]257
[b405ff6]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 "";
[6a6dd47]265    }
266
[b405ff6]267    my @result = $$client->AuthSend( %{ $vars{jlogin_authhash} } );
[6a6dd47]268
[b405ff6]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};
[6a6dd47]275        return "";
276    }
277
278    $connections->{$jidStr}->{roster}->fetch();
[b405ff6]279    $$client->PresenceSend( priority => 1 );
[6a6dd47]280    queue_admin_msg("Connected to jabber as $jidStr");
281    delete $vars{jlogin_connhash};
282    delete $vars{jlogin_authhash};
[38ffdf9]283    return "";
284}
285
[b405ff6]286sub do_logout {
[6a6dd47]287    my $jid = shift;
288    $connections->{$jid}->{client}->Disconnect();
289    delete $connections->{$jid};
290    queue_admin_msg("Jabber disconnected ($jid).");
291}
292
[b405ff6]293sub cmd_logout {
294
[6a6dd47]295    # Logged into multiple accounts
[b405ff6]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.
[6a6dd47]324    {
[b405ff6]325
326        do_logout( ( keys %$connections )[0] );
[38ffdf9]327    }
328    return "";
329}
330
[b405ff6]331sub cmd_jlist {
332    if ( !( scalar keys %$connections ) ) {
333        owl::error("You are not logged in to Jabber.");
334        return;
[b6a253c]335    }
[b405ff6]336    owl::popless_ztext( onGetBuddyList() );
[b6a253c]337}
338
[b405ff6]339sub cmd_jwrite {
340    if ( !connected() ) {
341        owl::error("You are not logged in to Jabber.");
342        return;
[38ffdf9]343    }
344
[b405ff6]345    my $jwrite_to      = "";
346    my $jwrite_from    = "";
347    my $jwrite_thread  = "";
[6a6dd47]348    my $jwrite_subject = "";
[b405ff6]349    my $jwrite_type    = "chat";
[38ffdf9]350
[6a6dd47]351    my @args = @_;
352    shift;
[9f183ff]353    local @ARGV = @_;
[6a6dd47]354    my $gc;
[b405ff6]355    GetOptions(
356        'thread=s'  => \$jwrite_thread,
357        'subject=s' => \$jwrite_subject,
358        'account=s' => \$jwrite_from,
359        'groupchat' => \$gc
360    );
[6a6dd47]361    $jwrite_type = 'groupchat' if $gc;
362
[b405ff6]363    if ( scalar @ARGV != 1 ) {
364        owl::error(
365            "Usage: jwrite JID [-g] [-t thread] [-s 'subject'] [-a account]");
366        return;
[6a6dd47]367    }
[b405ff6]368    else {
369        $jwrite_to = shift @ARGV;
[6a6dd47]370    }
[b6a253c]371
[b405ff6]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(
[005562f]395        "Type your message below.  End with a dot on a line by itself.  ^C will quit."
396       );
[b405ff6]397    owl::start_edit_win( join( ' ', @args ), \&process_owl_jwrite );
[38ffdf9]398}
399
[b405ff6]400sub 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    }
[6df381b]445}
446
447sub jmuc_join {
[b405ff6]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
[6e9e50e]456    my $presence = new Net::Jabber::Presence;
457    $presence->SetPresence( to => $muc );
458    my $x = $presence->NewChild('http://jabber.org/protocol/muc');
459    $x->AddHistory()->SetMaxChars(0);
[b405ff6]460    if ($password) {
[6e9e50e]461        $x->SetPassword($password);
[b405ff6]462    }
463
464    $connections->{$jid}->{client}->Send($presence);
[6df381b]465}
466
467sub jmuc_part {
[b405ff6]468    my ( $jid, $muc, @args ) = @_;
[9f183ff]469
[b405ff6]470    $muc = shift @args if scalar @args;
471    die("Usage: jmuc part {muc} [-a account]") unless $muc;
[9f183ff]472
[b405ff6]473    $connections->{$jid}->{client}
474      ->PresenceSend( to => $muc, type => 'unavailable' );
475    queue_admin_msg("$jid has left $muc.");
[6df381b]476}
477
[b405ff6]478sub jmuc_invite {
479    my ( $jid, $muc, @args ) = @_;
480
481    my $invite_jid = shift @args;
482    $muc = shift @args if scalar @args;
483
484    die('Usage: jmuc invite {jid} [muc] [-a account]')
485      unless $muc && $invite_jid;
486
[d9f4a5c]487    my $message = Net::Jabber::Message->new();
[b405ff6]488    $message->SetTo($muc);
[d9f4a5c]489    my $x = $message->NewChild('http://jabber.org/protocol/muc#user');
490    $x->AddInvite();
491    $x->GetInvite()->SetTo($invite_jid);
[b405ff6]492    $connections->{$jid}->{client}->Send($message);
493    queue_admin_msg("$jid has invited $invite_jid to $muc.");
[38ffdf9]494}
495
[9f183ff]496sub jmuc_configure {
[b405ff6]497    my ( $jid, $muc, @args ) = @_;
498    $muc = shift @args if scalar @args;
499    die("Usage: jmuc configure [muc]") unless $muc;
500    my $iq = Net::Jabber::IQ->new();
501    $iq->SetTo($muc);
502    $iq->SetType('set');
503    my $query = $iq->NewQuery("http://jabber.org/protocol/muc#owner");
504    my $x     = $query->NewChild("jabber:x:data");
505    $x->SetType('submit');
506
507    $connections->{$jid}->{client}->Send($iq);
508    queue_admin_msg("Accepted default instant configuration for $muc");
[9f183ff]509}
510
[38ffdf9]511################################################################################
512### Owl Callbacks
[b405ff6]513sub process_owl_jwrite {
[38ffdf9]514    my $body = shift;
515
516    my $j = new Net::XMPP::Message;
517    $body =~ s/\n\z//;
[b405ff6]518    $j->SetMessage(
519        to   => $vars{jwrite}{to},
520        from => $vars{jwrite}{from},
521        type => $vars{jwrite}{type},
522        body => $body
523    );
524    $j->SetThread( $vars{jwrite}{thread} )   if ( $vars{jwrite}{thread} );
525    $j->SetSubject( $vars{jwrite}{subject} ) if ( $vars{jwrite}{subject} );
526
527    my $m = j2o( $j, 'out' );
528    if ( $vars{jwrite}{type} ne 'groupchat' ) {
529
530        #XXX TODO: Check for displayoutgoing.
531        owl::queue_message($m);
[38ffdf9]532    }
[b405ff6]533    $connections->{ $vars{jwrite}{from} }->{client}->Send($j);
[6a6dd47]534    delete $vars{jwrite};
[005562f]535    owl::message("");   # Kludge to make the ``type your message...'' message go away
[38ffdf9]536}
537
538### XMPP Callbacks
539
[b405ff6]540sub process_incoming_chat_message {
541    my ( $session, $j ) = @_;
542    owl::queue_message( j2o( $j, 'in' ) );
[38ffdf9]543}
544
[b405ff6]545sub process_incoming_error_message {
546    my ( $session, $j ) = @_;
547    my %jhash = j2hash( $j, 'in' );
[b6a253c]548    $jhash{type} = 'admin';
[b405ff6]549    owl::queue_message( owl::Message->new(%jhash) );
[38ffdf9]550}
551
[b405ff6]552sub process_incoming_groupchat_message {
553    my ( $session, $j ) = @_;
554
[38ffdf9]555    # HACK IN PROGRESS (ignoring delayed messages)
[b405ff6]556    return if ( $j->DefinedX('jabber:x:delay') && $j->GetX('jabber:x:delay') );
557    owl::queue_message( j2o( $j, 'in' ) );
[38ffdf9]558}
559
[b405ff6]560sub process_incoming_headline_message {
561    my ( $session, $j ) = @_;
562    owl::queue_message( j2o( $j, 'in' ) );
[38ffdf9]563}
564
[b405ff6]565sub process_incoming_normal_message {
566    my ( $session, $j ) = @_;
567    my %props = j2hash( $j, 'in' );
[b6a253c]568
569    # XXX TODO: handle things such as MUC invites here.
570
[b405ff6]571    #    if ($j->HasX('http://jabber.org/protocol/muc#user'))
572    #    {
573    #   my $x = $j->GetX('http://jabber.org/protocol/muc#user');
574    #   if ($x->HasChild('invite'))
575    #   {
576    #       $props
577    #   }
578    #    }
579    #
580    owl::queue_message( owl::Message->new(%props) );
[b6a253c]581}
582
[b405ff6]583sub process_muc_presence {
584    my ( $session, $p ) = @_;
585    return unless ( $p->HasX('http://jabber.org/protocol/muc#user') );
[38ffdf9]586
[b405ff6]587}
[38ffdf9]588
589### Helper functions
590
[b405ff6]591sub j2hash {
592    my $j   = shift;
[38ffdf9]593    my $dir = shift;
594
[b405ff6]595    my %props = (
596        type      => 'jabber',
597        direction => $dir
598    );
[38ffdf9]599
[b6a253c]600    my $jtype = $props{jtype} = $j->GetType();
[b405ff6]601    my $from = $j->GetFrom('jid');
602    my $to   = $j->GetTo('jid');
[38ffdf9]603
[b6a253c]604    $props{from} = $from->GetJID('full');
605    $props{to}   = $to->GetJID('full');
[38ffdf9]606
[005562f]607    my $account = ( $dir eq 'out' ) ? $props{from} : $props{to};
608
[b6a253c]609    $props{recipient}  = $to->GetJID('base');
610    $props{sender}     = $from->GetJID('base');
[b405ff6]611    $props{subject}    = $j->GetSubject() if ( $j->DefinedSubject() );
612    $props{thread}     = $j->GetThread() if ( $j->DefinedThread() );
613    $props{body}       = $j->GetBody() if ( $j->DefinedBody() );
614    $props{error}      = $j->GetError() if ( $j->DefinedError() );
615    $props{error_code} = $j->GetErrorCode() if ( $j->DefinedErrorCode() );
[b6a253c]616    $props{xml}        = $j->GetXML();
[38ffdf9]617
[b405ff6]618    if ( $jtype eq 'chat' ) {
619        $props{replycmd} =
[005562f]620          "jwrite " . ( ( $dir eq 'in' ) ? $props{from} : $props{to} ) . " -a $account";
[b405ff6]621        $props{isprivate} = 1;
[005562f]622        $props{replysendercmd} = $props{replycmd};
[38ffdf9]623    }
[b405ff6]624    elsif ( $jtype eq 'groupchat' ) {
625        my $nick = $props{nick} = $from->GetResource();
626        my $room = $props{room} = $from->GetJID('base');
[005562f]627        $props{replycmd} = "jwrite -g $room -a $account";
628
629        $props{replysendercmd} = "jwrite " . $from->GetJID('full') . " -a $account";
[b405ff6]630
631        $props{sender} = $nick || $room;
632        $props{recipient} = $room;
633
634        if ( $props{subject} && !$props{body} ) {
635            $props{body} =
636              '[' . $nick . " has set the topic to: " . $props{subject} . "]";
637        }
[b6a253c]638    }
[b405ff6]639    elsif ( $jtype eq 'normal' ) {
640        $props{replycmd}  = undef;
641        $props{isprivate} = 1;
[b6a253c]642    }
[b405ff6]643    elsif ( $jtype eq 'headline' ) {
644        $props{replycmd} = undef;
[b6a253c]645    }
[b405ff6]646    elsif ( $jtype eq 'error' ) {
647        $props{replycmd} = undef;
648        $props{body}     = "Error "
649          . $props{error_code}
650          . " sending to "
651          . $props{from} . "\n"
652          . $props{error};
653    }
654
[b6a253c]655    return %props;
656}
[38ffdf9]657
[b405ff6]658sub j2o {
659    return owl::Message->new( j2hash(@_) );
[38ffdf9]660}
661
[b405ff6]662sub queue_admin_msg {
[38ffdf9]663    my $err = shift;
[b405ff6]664    my $m   = owl::Message->new(
665        type      => 'admin',
666        direction => 'none',
667        body      => $err
668    );
[38ffdf9]669    owl::queue_message($m);
670}
[b6a253c]671
[b405ff6]672sub boldify($) {
[9f183ff]673    my $str = shift;
[b6a253c]674
[b405ff6]675    return '@b(' . $str . ')' if ( $str !~ /\)/ );
676    return '@b<' . $str . '>' if ( $str !~ /\>/ );
677    return '@b{' . $str . '}' if ( $str !~ /\}/ );
678    return '@b[' . $str . ']' if ( $str !~ /\]/ );
[b6a253c]679
680    my $txt = "\@b($str";
681    $txt =~ s/\)/\)\@b\[\)\]\@b\(/g;
[b405ff6]682    return $txt . ')';
[b6a253c]683}
684
[b405ff6]685sub getServerFromJID {
[6a6dd47]686    my $jid = shift;
687    my $res = new Net::DNS::Resolver;
[b405ff6]688    my $packet =
689      $res->search( '_xmpp-client._tcp.' . $jid->GetServer(), 'srv' );
[6a6dd47]690
[b405ff6]691    if ($packet)    # Got srv record.
[6a6dd47]692    {
[b405ff6]693        my @answer = $packet->answer;
694        return $answer[0]{target}, $answer[0]{port};
[6a6dd47]695    }
696
697    return $jid->GetServer(), 5222;
698}
699
[b405ff6]700sub connected {
[6a6dd47]701    return scalar keys %$connections;
702}
703
[9f183ff]704sub defaultJID {
[b405ff6]705    return ( keys %$connections )[0] if ( connected() == 1 );
706    return;
[9f183ff]707}
708
[b405ff6]709sub resolveJID {
[6a6dd47]710    my $givenJidStr = shift;
[b405ff6]711    my $givenJid    = new Net::XMPP::JID;
[6a6dd47]712    $givenJid->SetJID($givenJidStr);
[b405ff6]713
[6a6dd47]714    # Account fully specified.
[b405ff6]715    if ( $givenJid->GetResource() ) {
716
717        # Specified account exists
718        if ( defined $connections->{$givenJidStr} ) {
719            return $givenJidStr;
720        }
721        else    #Specified account doesn't exist
722        {
723            owl::error("Invalid account: $givenJidStr");
724        }
[6a6dd47]725    }
[b405ff6]726
[6a6dd47]727    # Disambiguate.
[b405ff6]728    else {
729        my $matchingJid = "";
730        my $errStr =
731          "Ambiguous account reference. Please specify a resource.\n";
732        my $ambiguous = 0;
733
734        foreach my $jid ( keys %$connections ) {
735            my $cJid = new Net::XMPP::JID;
736            $cJid->SetJID($jid);
737            if ( $givenJidStr eq $cJid->GetJID('base') ) {
738                $ambiguous = 1 if ( $matchingJid ne "" );
739                $matchingJid = $jid;
740                $errStr .= "\t$jid\n";
741            }
742        }
743
744        # Need further disambiguation.
745        if ($ambiguous) {
746            queue_admin_msg($errStr);
747        }
748
749        # Not one of ours.
750        elsif ( $matchingJid eq "" ) {
751            owl::error("Invalid account: $givenJidStr");
752        }
753
754        # Log out this one.
755        else {
756            return $matchingJid;
757        }
[6a6dd47]758    }
759    return "";
760}
Note: See TracBrowser for help on using the repository browser.