Changeset 455f1ab for perl/modules


Ignore:
Timestamp:
Jan 23, 2007, 6:26:41 PM (18 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, barnowl_perlaim, debian, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
65581e9
Parents:
17b7fc5
git-author:
Nelson Elhage <nelhage@mit.edu> (01/23/07 17:35:07)
git-committer:
Nelson Elhage <nelhage@mit.edu> (01/23/07 18:26:41)
Message:
The beginnings of writing to shortnames
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perl/modules/jabber.pl

    r1d8503b r455f1ab  
    183183}
    184184
    185 sub getConnectionFromJidStr {
    186     my $self = shift;
    187     my $jidStr = shift;
    188     return $self->{$jidStr}->{Client};
     185sub getConnectionFromJID {
     186    my $self = shift;
     187    my $jid = shift;
     188    $jid = $jid->GetJID('full') if UNIVERSAL::isa($jid, 'Net::XMPP::JID');
     189    return $self->{$jid}->{Client} if exists $self->{$jid};
    189190}
    190191
     
    199200}
    200201
    201 sub getRosterFromJidStr {
    202     my $self = shift;
    203     my $jidStr = shift;
    204     return $self->{$jidStr}->{Roster};
     202sub getRosterFromJID {
     203    my $self = shift;
     204    my $jid = shift;
     205    $jid = $jid->GetJID('full') if UNIVERSAL::isa($jid, 'Net::XMPP::JID');
     206    return $self->{$jid}->{Roster} if exists $self->{$jid};
    205207}
    206208################################################################################
     
    246248
    247249    foreach my $jid ( $conn->getJids() ) {
    248         my $client = $conn->getConnectionFromJidStr($jid);
     250        my $client = $conn->getConnectionFromJID($jid);
    249251
    250252        unless($client) {
     
    304306sub getSingleBuddyList {
    305307    my $jid = shift;
    306     $jid = resolveJID($jid);
     308    $jid = resolveConnectedJID($jid);
    307309    return "" unless $jid;
    308310    my $blist = "";
    309     my $roster = $conn->getRosterFromJidStr($jid);
     311    my $roster = $conn->getRosterFromJID($jid);
    310312    if ($roster) {
    311313        $blist .= "\n" . boldify("Jabber Roster for $jid\n");
     
    354356        {
    355357            summary => "Send a Jabber Message",
    356             usage   => "jwrite JID [-g] [-t thread] [-s subject]"
     358            usage   => "jwrite JID [-t thread] [-s subject]"
    357359        }
    358360    );
     
    480482                BarnOwl::error( "Error in connect: " . join( " ", @result ) );
    481483            } else {
    482                 $conn->getRosterFromJidStr($jidStr)->fetch();
     484                $conn->getRosterFromJID($jidStr)->fetch();
    483485                $client->PresenceSend( priority => 1 );
    484486                queue_admin_msg("Connected to jabber as $jidStr");
     
    524526            else                    #One account.
    525527            {
    526                 my $jid = resolveJID( $_[1] );
     528                my $jid = resolveConnectedJID( $_[1] );
    527529                do_logout($jid) if ( $jid ne '' );
    528530            }
     
    555557    my $jwrite_thread  = "";
    556558    my $jwrite_subject = "";
     559    my $to;
    557560    my $jwrite_type    = "chat";
    558561
     
    566569        'account=s' => \$jwrite_from,
    567570        'id=s'     =>  \$jwrite_sid,
    568         'groupchat' => \$gc
    569571    );
    570572    $jwrite_type = 'groupchat' if $gc;
     
    572574    if ( scalar @ARGV != 1 ) {
    573575        BarnOwl::error(
    574             "Usage: jwrite JID [-g] [-t thread] [-s 'subject'] [-a account]");
     576            "Usage: jwrite JID [-t thread] [-s 'subject'] [-a account]");
    575577        return;
    576578    }
    577579    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 
     580        $to = shift @ARGV;
     581    }
     582
     583    ($jwrite_from, $jwrite_to, $jwrite_type) = guess_jwrite($jwrite_from, $to);
     584
     585    unless($jwrite_from && $jwrite_to) {
     586        die("Unable to resolve JID $to");
     587    }
     588   
    595589    $vars{jwrite} = {
    596590        to      => $jwrite_to,
     
    605599"Type your message below.  End with a dot on a line by itself.  ^C will quit."
    606600    );
    607     BarnOwl::start_edit_win( join( ' ', @args ), \&process_owl_jwrite );
     601    my $cmd = "jwrite $jwrite_to -a $jwrite_from";
     602    $cmd .= " -t $jwrite_thread" if $jwrite_thread;
     603    $cmd .= " -t $jwrite_subject" if $jwrite_subject;
     604    BarnOwl::start_edit_win( $cmd, \&process_owl_jwrite );
    608605}
    609606
     
    646643        $jid ||= defaultJID();
    647644        if ($jid) {
    648             $jid = resolveJID($jid);
     645            $jid = resolveConnectedJID($jid);
    649646            return unless $jid;
    650647        }
     
    665662      or die("Usage: jmuc join MUC [-p password] [-a account]");
    666663
    667     $conn->getConnectionFromJidStr($jid)->MUCJoin(Jid      => $muc,
     664    $conn->getConnectionFromJID($jid)->MUCJoin(Jid      => $muc,
    668665                                                  Password => $password,
    669666                                                  History  => {
     
    679676    die("Usage: jmuc part MUC [-a account]") unless $muc;
    680677
    681     $conn->getConnectionFromJidStr($jid)->MUCLeave(JID => $muc);
     678    $conn->getConnectionFromJID($jid)->MUCLeave(JID => $muc);
    682679    queue_admin_msg("$jid has left $muc.");
    683680}
     
    697694    $x->AddInvite();
    698695    $x->GetInvite()->SetTo($invite_jid);
    699     $conn->getConnectionFromJidStr($jid)->Send($message);
     696    $conn->getConnectionFromJID($jid)->Send($message);
    700697    queue_admin_msg("$jid has invited $invite_jid to $muc.");
    701698}
     
    712709    $x->SetType('submit');
    713710
    714     $conn->getConnectionFromJidStr($jid)->Send($iq);
     711    $conn->getConnectionFromJID($jid)->Send($iq);
    715712    queue_admin_msg("Accepted default instant configuration for $muc");
    716713}
     
    722719    die("Usage: jmuc presence MUC") unless $muc;
    723720
    724     my $m = $conn->getConnectionFromJidStr($jid)->FindMUC(jid => $muc);
     721    my $m = $conn->getConnectionFromJID($jid)->FindMUC(jid => $muc);
    725722    die("No such muc: $muc") unless $m;
    726723
     
    772769        $jid ||= defaultJID();
    773770        if ($jid) {
    774             $jid = resolveJID($jid);
     771            $jid = resolveConnectedJID($jid);
    775772            return unless $jid;
    776773        }
     
    789786    my $baseJid = baseJID($jid);
    790787
    791     my $roster = $conn->getRosterFromJidStr($jid);
     788    my $roster = $conn->getRosterFromJID($jid);
    792789
    793790    # Adding lots of users with the same name is a bad idea.
     
    801798
    802799        $p->SetTo($to);
    803         $conn->getConnectionFromJidStr($jid)->Send($p);
     800        $conn->getConnectionFromJID($jid)->Send($p);
    804801        queue_admin_msg("You ($baseJid) have requested a subscription to ($to)'s presence.");
    805802    }
     
    817814    foreach my $to (@ARGV) {
    818815        $p->SetTo($to);
    819         $conn->getConnectionFromJidStr($jid)->Send($p);
     816        $conn->getConnectionFromJID($jid)->Send($p);
    820817        queue_admin_msg("You ($baseJid) have unsubscribed from ($to)'s presence.");
    821818    }
     
    829826    my $baseJid = baseJID($jid);
    830827
    831     my $roster = $conn->getRosterFromJidStr($jid);
     828    my $roster = $conn->getRosterFromJID($jid);
    832829
    833830    # Adding lots of users with the same name is a bad idea.
     
    859856        $item->put_attrib(jid => $to);
    860857        $item->put_attrib(name => $name) if $name;
    861         $conn->getConnectionFromJidStr($jid)->Send($iq);
     858        $conn->getConnectionFromJID($jid)->Send($iq);
    862859        my $msg = "$baseJid: "
    863860          . ($name ? "$name ($to)" : "($to)")
     
    883880    foreach my $to (@ARGV) {
    884881        $item->put_attrib(jid => $to);
    885         $conn->getConnectionFromJidStr($jid)->Send($iq);
     882        $conn->getConnectionFromJID($jid)->Send($iq);
    886883        queue_admin_msg("You ($baseJid) have removed ($to) from your roster.");
    887884    }
     
    899896    foreach my $to (@ARGV) {
    900897        $p->SetTo($to);
    901         $conn->getConnectionFromJidStr($jid)->Send($p);
     898        $conn->getConnectionFromJID($jid)->Send($p);
    902899        queue_admin_msg("($to) has been subscribed to your ($baseJid) presence.");
    903900    }
     
    915912    foreach my $to (@ARGV) {
    916913        $p->SetTo($to);
    917         $conn->getConnectionFromJidStr($jid)->Send($p);
     914        $conn->getConnectionFromJID($jid)->Send($p);
    918915        queue_admin_msg("($to) has been unsubscribed from your ($baseJid) presence.");
    919916    }
     
    947944    }
    948945    else {
    949         $conn->getConnectionFromJidStr($vars{jwrite}{from})->Send($j);
     946        $conn->getConnectionFromJID($vars{jwrite}{from})->Send($j);
    950947    }
    951948
     
    10831080            $to eq $cJid->GetJID('full')) {
    10841081            my $reply = $p->Reply(type=>"unsubscribed");
    1085             $conn->getConnectionFromJidStr($jid)->Send($reply);
     1082            $conn->getConnectionFromJID($jid)->Send($reply);
    10861083            return;
    10871084        }
     
    11571154        my $nick = $props{nick} = $from->GetResource();
    11581155        my $room = $props{room} = $from->GetJID('base');
    1159         $props{replycmd} = "jwrite -g $room";
     1156        $props{replycmd} = "jwrite $room";
    11601157        $props{replycmd} .=
    11611158          " -a " . ( ( $dir eq 'out' ) ? $props{from} : $props{to} );
     
    12431240}
    12441241
    1245 sub resolveJID {
     1242sub resolveConnectedJID {
    12461243    my $givenJidStr = shift;
    12471244    my $givenJid    = new Net::XMPP::JID;
     
    12521249        # Specified account exists
    12531250        return $givenJidStr if ($conn->jidExists($givenJidStr) );
    1254         BarnOwl::error("Invalid account: $givenJidStr");
     1251        die("Invalid account: $givenJidStr");
    12551252    }
    12561253
     
    12741271        # Need further disambiguation.
    12751272        if ($ambiguous) {
    1276             queue_admin_msg($errStr);
     1273            die($errStr);
    12771274        }
    12781275
    12791276        # Not one of ours.
    12801277        elsif ( $matchingJid eq "" ) {
    1281             BarnOwl::error("Invalid account: $givenJidStr");
     1278            die("Invalid account: $givenJidStr");
    12821279        }
    12831280
     
    12881285    }
    12891286    return "";
     1287}
     1288
     1289sub resolveDestJID {
     1290    my ($to, $from) = @_;
     1291    my $jid = Net::Jabber::JID->new($to);
     1292    if($jid->GetResource()) {
     1293        return $jid->GetJID('full');
     1294    }
     1295
     1296    my $roster = $conn->getRosterFromJID($from);
     1297    my @jids = $roster->jids('all');
     1298    for my $j (@jids) {
     1299        if($roster->query($j, 'name') eq $to) {
     1300            return $j->GetJID('full');
     1301        }
     1302    }
     1303
     1304    return undef;
     1305}
     1306
     1307sub resolveType {
     1308    my $to = shift;
     1309    my $from = shift;
     1310    my @mucs = $conn->getConnectionFromJID($from)->MUCs;
     1311    if(grep {$_->BaseJID eq $to } @mucs) {
     1312        return 'groupchat';
     1313    } else {
     1314        return 'chat';
     1315    }
     1316}
     1317
     1318sub guess_jwrite {
     1319    # Heuristically guess what jids a jwrite was meant to be going to/from
     1320    my ($from, $to) = (@_);
     1321    my ($from_jid, $to_jid);
     1322    if($from) {
     1323        $from_jid = resolveConnectedJID($from);
     1324        die("Unable to resolve account $from") unless $from_jid;
     1325        $to_jid = resolveDestJID($to, $from_jid);
     1326    } elsif($to =~ /@/) {
     1327        $to_jid = $to;
     1328        $from_jid = defaultJID();
     1329        die("You must specify a JID with -a") unless $from_jid;
     1330    } else {
     1331        for my $f ($conn->getJids) {
     1332            $to_jid = resolveDestJID($to, $f);
     1333            if(defined($to_jid)) {
     1334                $from_jid = $f;
     1335            }
     1336        }
     1337        die("Unable to resolve JID $to") unless $to_jid;
     1338    }
     1339
     1340    my $type = resolveType($to_jid, $from_jid);
     1341    return ($from_jid, $to_jid, $type);
    12901342}
    12911343
Note: See TracChangeset for help on using the changeset viewer.