Changeset 84296f6 for perl/modules


Ignore:
Timestamp:
Nov 14, 2006, 9:44:50 AM (15 years ago)
Author:
Alejandro R. Sedeño <asedeno@mit.edu>
Branches:
master, barnowl_perlaim, debian, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
99dabee
Parents:
6e9e50e
Message:
jabber.pl:
* Refactoring of Buddy List code.
* Refactoring of login code
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perl/modules/jabber.pl

    r6e9e50e r84296f6  
    88use Net::DNS;
    99use Getopt::Long;
     10
     11no warnings 'redefine';
    1012
    1113################################################################################
     
    2022# Implementing formatting and logging callbacks for C
    2123# Appropriate callbacks for presence subscription messages.
    22 #  * Current behavior => auto-accept (default for Net::Jabber)
     24#  * Current behavior is auto-accept (default for Net::Jabber)
    2325#
    2426################################################################################
     
    3436    }
    3537    else {
    36 
    3738        # Our owl doesn't support queue_message. Unfortunately, this
    3839        # means it probably *also* doesn't support owl::error. So just
     
    7778    }
    7879    else {
    79         $blistStr .= $jq{ask} ? " [pending]" : " [offline]";
    80     }
    81 
     80        if ($jq{ask}) {
     81            $blistStr .= " [pending]";
     82        }
     83        elsif ($jq{subscription} eq 'none' || $jq{subscription} eq 'from') {
     84            $blistStr .= " [not subscribed]";
     85        }
     86        else {
     87            $blistStr .= " [offline]";
     88        }
     89    }
    8290    return $blistStr . "\n";
     91}
     92
     93sub getSingleBuddyList {
     94    my $jid = shift;
     95    $jid = resolveJID($jid);
     96    return "" unless $jid;
     97    my $blist = "";
     98    my $roster = \$connections->{$jid}->{roster};
     99    if ($$roster) {
     100        $blist .= "\n" . boldify("Jabber Roster for $jid\n");
     101
     102        foreach my $group ( $$roster->groups() ) {
     103            $blist .= "  Group: $group\n";
     104            foreach my $buddy ( $$roster->jids( 'group', $group ) ) {
     105                $blist .= blist_listBuddy( $roster, $buddy );
     106            }
     107        }
     108
     109        my @unsorted = $$roster->jids('nogroup');
     110        if (@unsorted) {
     111            $blist .= "  [unsorted]\n";
     112            foreach my $buddy (@unsorted) {
     113                $blist .= blist_listBuddy( $roster, $buddy );
     114            }
     115        }
     116    }
     117    return $blist;
    83118}
    84119
    85120sub onGetBuddyList {
    86121    my $blist = "";
    87     foreach my $jid ( keys %{$connections} ) {
    88         my $roster = \$connections->{$jid}->{roster};
    89         if ($$roster) {
    90             $blist .= "\n" . boldify("Jabber Roster for $jid\n");
    91 
    92             foreach my $group ( $$roster->groups() ) {
    93                 $blist .= "  Group: $group\n";
    94                 foreach my $buddy ( $$roster->jids( 'group', $group ) ) {
    95                     $blist .= blist_listBuddy( $roster, $buddy );
    96                 }
    97             }
    98 
    99             my @unsorted = $$roster->jids('nogroup');
    100             if (@unsorted) {
    101                 $blist .= "  [unsorted]\n";
    102                 foreach my $buddy (@unsorted) {
    103                     $blist .= blist_listBuddy( $roster, $buddy );
    104                 }
    105             }
    106         }
     122    foreach my $jid (keys %{$connections}) {
     123        $blist .= getSingleBuddyList($jid);
    107124    }
    108125    return $blist;
     
    177194    my ( $server, $port ) = getServerFromJID($jid);
    178195
    179     $connections->{$jidStr}->{client} = Net::Jabber::Client->new(
    180         debuglevel => owl::getvar('debug') eq 'on' ? 1 : 0,
    181         debugfile => 'jabber.log'
    182     );
    183     my $client = \$connections->{$jidStr}->{client};
    184     $connections->{$jidStr}->{roster} =
    185       $connections->{$jidStr}->{client}->Roster();
    186 
    187     #XXX Todo: Add more callbacks.
    188     # MUC presence handlers
    189     $$client->SetMessageCallBacks(
    190         chat      => sub { owl_jabber::process_incoming_chat_message(@_) },
    191         error     => sub { owl_jabber::process_incoming_error_message(@_) },
    192         groupchat => sub { owl_jabber::process_incoming_groupchat_message(@_) },
    193         headline  => sub { owl_jabber::process_incoming_headline_message(@_) },
    194         normal    => sub { owl_jabber::process_incoming_normal_message(@_) }
    195     );
    196 
     196    $vars{jlogin_jid} = $jidStr;
     197    $vars{jlogin_havepass} = 0;
    197198    $vars{jlogin_connhash} = {
    198199        hostname      => $server,
     
    201202        componentname => $componentname
    202203    };
    203 
    204     my $status = $$client->Connect( %{ $vars{jlogin_connhash} } );
    205 
    206     if ( !$status ) {
    207         delete $connections->{$jidStr};
    208         delete $vars{jlogin_connhash};
    209         owl::error("We failed to connect");
    210         return "";
    211     }
    212 
    213204    $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;
     205      { username => $uid,
     206        resource => $resource,
     207    };
     208
     209    return do_login('');
     210}
     211
     212sub do_login {
     213    $vars{jlogin_password} = shift;
     214    $vars{jlogin_authhash}->{password} = sub { return $vars{jlogin_password} || '' };
     215    my $jidStr = $vars{jlogin_jid};
     216    if ( !$jidStr && $vars{jlogin_havepass}) {
     217        owl::error("Got password but have no jid!");
     218    }
     219    else
     220    {
     221        $connections->{$jidStr}->{client} = Net::Jabber::Client->new(
     222            debuglevel => owl::getvar('debug') eq 'on' ? 1 : 0,
     223            debugfile => 'jabber.log'
     224        );
     225        my $client = \$connections->{$jidStr}->{client};
     226        $connections->{$jidStr}->{roster} =
     227          $connections->{$jidStr}->{client}->Roster();
     228
     229        #XXX Todo: Add more callbacks.
     230        # * MUC presence handlers
     231        $$client->SetMessageCallBacks(
     232            chat      => sub { owl_jabber::process_incoming_chat_message(@_) },
     233            error     => sub { owl_jabber::process_incoming_error_message(@_) },
     234            groupchat => sub { owl_jabber::process_incoming_groupchat_message(@_) },
     235            headline  => sub { owl_jabber::process_incoming_headline_message(@_) },
     236            normal    => sub { owl_jabber::process_incoming_normal_message(@_) }
     237        );
     238        $$client->SetPresenceCallBacks(
     239            subscribe   => sub { owl_jabber::process_presence_subscribe(@_) },
     240            unsubscribe => sub { owl_jabber::process_presence_unsubscribe(@_) });
     241
     242        my $status = $$client->Connect( %{ $vars{jlogin_connhash} } );
     243        if ( !$status ) {
    219244            delete $connections->{$jidStr};
    220             owl::start_password( "Password for $jidStr: ", \&do_login_with_pw );
    221             return "";
    222         }
    223         owl::error(
    224             "Error in connect: " . join( " ", $result[ 1 .. $#result ] ) );
    225         do_logout($jidStr);
    226         delete $vars{jlogin_connhash};
    227         delete $vars{jlogin_authhash};
    228         return "";
    229     }
    230     $connections->{$jidStr}->{roster}->fetch();
    231     $$client->PresenceSend( priority => 1 );
    232     queue_admin_msg("Connected to jabber as $jidStr");
    233     delete $vars{jlogin_connhash};
    234     delete $vars{jlogin_authhash};
    235     return "";
    236 }
    237 
    238 sub do_login_with_pw {
    239     $vars{jlogin_authhash}->{password} = shift;
    240     my $jidStr = delete $vars{jlogin_jid};
    241     if ( !$jidStr ) {
    242         owl::error("Got password but have no jid!");
    243     }
    244 
    245     $connections->{$jidStr}->{client} = Net::Jabber::Client->new();
    246     my $client = \$connections->{$jidStr}->{client};
    247     $connections->{$jidStr}->{roster} =
    248       $connections->{$jidStr}->{client}->Roster();
    249 
    250     $$client->SetMessageCallBacks(
    251         chat      => sub { owl_jabber::process_incoming_chat_message(@_) },
    252         error     => sub { owl_jabber::process_incoming_error_message(@_) },
    253         groupchat => sub { owl_jabber::process_incoming_groupchat_message(@_) },
    254         headline  => sub { owl_jabber::process_incoming_headline_message(@_) },
    255         normal    => sub { owl_jabber::process_incoming_normal_message(@_) }
    256     );
    257 
    258     my $status = $$client->Connect( %{ $vars{jlogin_connhash} } );
    259     if ( !$status ) {
    260         delete $connections->{$jidStr};
    261         delete $vars{jlogin_connhash};
    262         delete $vars{jlogin_authhash};
    263         owl::error("We failed to connect");
    264         return "";
    265     }
    266 
    267     my @result = $$client->AuthSend( %{ $vars{jlogin_authhash} } );
    268 
    269     if ( $result[0] ne 'ok' ) {
    270         owl::error(
    271             "Error in connect: " . join( " ", $result[ 1 .. $#result ] ) );
    272         do_logout($jidStr);
    273         delete $vars{jlogin_connhash};
    274         delete $vars{jlogin_authhash};
    275         return "";
    276     }
    277 
    278     $connections->{$jidStr}->{roster}->fetch();
    279     $$client->PresenceSend( priority => 1 );
    280     queue_admin_msg("Connected to jabber as $jidStr");
     245            owl::error("We failed to connect");
     246        }
     247        else {
     248            my @result = $$client->AuthSend( %{ $vars{jlogin_authhash} } );
     249
     250            if ( $result[0] ne 'ok' ) {
     251            if ( !$vars{jlogin_havepass} && $result[0] == 401 ) {
     252                $vars{jlogin_havepass} = 1;
     253                delete $connections->{$jidStr};
     254                owl::start_password( "Password for $jidStr: ", \&do_login );
     255                return "";
     256            }
     257            delete $connections->{$jidStr};
     258            owl::error(
     259                "Error in connect: " . join( " ", @result ) );
     260        }
     261            else {
     262                $connections->{$jidStr}->{roster}->fetch();
     263                $$client->PresenceSend( priority => 1 );
     264                queue_admin_msg("Connected to jabber as $jidStr");
     265            }
     266        }
     267    }
     268    delete $vars{jlogin_jid};
     269    $vars{jlogin_password} =~ tr/\0-\377/x/;
     270    delete $vars{jlogin_password};
     271    delete $vars{jlogin_havepass};
    281272    delete $vars{jlogin_connhash};
    282273    delete $vars{jlogin_authhash};
     
    292283
    293284sub cmd_logout {
    294 
    295285    # Logged into multiple accounts
    296286    if ( connected() > 1 ) {
    297 
    298287        # Logged into multiple accounts, no accout specified.
    299288        if ( !$_[1] ) {
    300289            my $errStr =
    301 "You are logged into multiple accounts. Please specify an account to log out of.\n";
     290              "You are logged into multiple accounts. Please specify an account to log out of.\n";
    302291            foreach my $jid ( keys %$connections ) {
    303292                $errStr .= "\t$jid\n";
     
    305294            queue_admin_msg($errStr);
    306295        }
    307 
    308296        # Logged into multiple accounts, account specified.
    309297        else {
     
    323311    else                            # Only one account logged in.
    324312    {
    325 
    326313        do_logout( ( keys %$connections )[0] );
    327314    }
     
    707694}
    708695
     696sub baseJID {
     697    my $givenJidStr = shift;
     698    my $givenJid    = new Net::XMPP::JID;
     699    $givenJid->SetJID($givenJidStr);
     700    return $givenJid->GetJID('base');
     701}
     702
    709703sub resolveJID {
    710704    my $givenJidStr = shift;
     
    752746        }
    753747
    754         # Log out this one.
     748        # It's this one.
    755749        else {
    756750            return $matchingJid;
     
    759753    return "";
    760754}
     755
     7561;
Note: See TracChangeset for help on using the changeset viewer.