Changeset 9f183ff


Ignore:
Timestamp:
Nov 10, 2006, 12:38:53 PM (17 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:
b405ff6
Parents:
71e1e80
git-author:
Nelson Elhage <nelhage@mit.edu> (11/10/06 12:38:43)
git-committer:
Nelson Elhage <nelhage@mit.edu> (11/10/06 12:38:53)
Message:
* using strict and warnings in jabber.pl
* Extracting JID/MUC extraction into cmd_jmuc
* Adding a jmuc configure command to allow creating new rooms
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perl/modules/jabber.pl

    r6df381b r9f183ff  
    11package owl_jabber;
     2use warnings;
     3use strict;
     4
    25use Authen::SASL qw(Perl);
    36use Net::Jabber;
     
    3841    }
    3942}
     43
    4044push @::onStartSubs, sub { owl_jabber::onStart(@_) };
    4145
     
    221225    if($result[0] ne 'ok')
    222226    {
    223         if ($result[1] = "401")
     227        if ($result[1] == 401)
    224228        {
    225229            $vars{jlogin_jid} = $jidStr;
     
    316320        else
    317321        {
    318             if ($_[1] = '-a') #All accounts.
     322            if ($_[1] eq '-a') #All accounts.
    319323            {
    320324                foreach my $jid (keys %$connections)
     
    364368    my @args = @_;
    365369    shift;
    366     local @::ARGV = @_;
     370    local @ARGV = @_;
    367371    my $gc;
    368372    GetOptions('thread=s' => \$jwrite_thread,
     
    372376    $jwrite_type = 'groupchat' if $gc;
    373377
    374     if (scalar @::ARGV != 1)
     378    if (scalar @ARGV != 1)
    375379    {
    376380        owl::error("Usage: jwrite JID [-g] [-t thread] [-s 'subject'] [-a account]");
     
    379383    else
    380384    {
    381         $jwrite_to = @::ARGV[0];
     385            $jwrite_to = shift @ARGV;
    382386    }
    383387
     
    410414}
    411415
    412 #XXX Todo: Split off sub-commands into their own subroutines.
    413 # It'll make them more managable.
    414416sub cmd_jmuc
    415417{
    416         if (!connected())
    417                 {
    418                         owl::error("You are not logged in to Jabber.");
    419                         return;
    420                 }
    421    
     418        die "You are not logged in to Jabber" unless connected();
    422419        my $ocmd = shift;
    423420        my $cmd = shift;   
     
    429426
    430427        my %jmuc_commands = (
    431                 join => \&jmuc_join,
    432                 part => \&jmuc_part,
    433                 invite => \&jmuc_invite
     428                join   => \&jmuc_join,
     429                part   => \&jmuc_part,
     430                invite => \&jmuc_invite,
     431                configure => \&jmuc_configure
    434432               );
    435433        my $func = $jmuc_commands{$cmd};
    436434        if(!$func) {
    437435                owl::error("jmuc: Unknown command: $cmd");
    438         } else {
    439                 return $func->(@_);
     436                return;
     437        }
     438
     439        {
     440                local @ARGV = @_;
     441                my $jid;
     442                my $muc;
     443                my $m = owl::getcurmsg();
     444                if ($m->is_jabber && $m->{jtype} eq 'groupchat')
     445                {
     446                        $muc = $m->{room};
     447                        $jid = $m->{to};
     448                }
     449
     450                my $getopt = Getopt::Long::Parser->new;
     451                $getopt->configure('pass_through');
     452                $getopt->getoptions('account=s' => \$jid);
     453                $jid ||= defaultJID();
     454                if($jid) {
     455                        $jid = resolveJID($jid);
     456                        return unless $jid;
     457                } else {
     458                        owl::error('You must specify an account with -a {jid}');
     459                }
     460                return $func->($jid, $muc, @ARGV);
    440461        }
    441462}
    442463
    443464sub jmuc_join {
    444         local @ARGV = @_;
     465        my ($jid, $muc, @args) = @_;
     466        local @ARGV = @args;
    445467        my $password;
    446         my $jid;
    447         GetOptions('password=s' => \$password,
    448                    'account=s' => \$jid);
    449 
    450         my $muc;
    451         if (scalar @ARGV != 1)
    452         {
    453             owl::error('Usage: jmuc join {muc} [-p password] [-a account]');
    454             return;
    455         }
    456         else
    457         {
    458             $muc = @ARGV[0];
    459         }
    460 
    461         if (!$jid)
    462         {
    463             if (connected() == 1)
    464             {
    465                 $jid = (keys %$connections)[0];
    466             }
    467             else
    468             {
    469                 owl::error("Please specify an account with -a {jid}");
    470                 return;
    471             }
    472         }
    473         else
    474         {
    475             $jid = resolveJID($jid);
    476             return unless $jid;
    477         }
     468        GetOptions('password=s' => \$password);
     469
     470        $muc = shift @ARGV or die("Usage: jmuc join {muc} [-p password] [-a account]");
    478471
    479472        my $x = new XML::Stream::Node('x');
     
    481474        $x->add_child('history')->put_attrib(maxchars => '0');
    482475       
    483         if ($jmuc_password)
    484         {
    485             $x->add_child('password')->add_cdata($jmuc_password);
     476        if ($password)
     477        {
     478            $x->add_child('password')->add_cdata($password);
    486479        }
    487480
     
    493486
    494487sub jmuc_part {
    495         my $muc;
    496         my $jid;
    497         if (!$_[0])
    498         {
    499             my $m = owl::getcurmsg();
    500             if ($m->is_jabber && $m->{jtype} eq 'groupchat')
    501             {
    502                 $muc = $m->{room};
    503                 $jid = $m->{to};
    504             }
    505             else
    506             {
    507                 owl::error('Usage: jmuc part {muc} [-a account]');
    508                 return;
    509             }
    510         }
    511         else
    512         {
    513             local @ARGV = @_;
    514             GetOptions('account=s' => \$jid);
    515             if (scalar @ARGV != 1)
    516             {
    517                 owl::error('Usage: jmuc part {muc} [-a account]');
    518                 return;
    519             }
    520             else
    521             {
    522                 $muc = @ARGV[0];
    523             }
    524             if (!$jid)
    525             {
    526                 if (connected() == 1)
    527                 {
    528                     $jid = (keys %$connections)[0];
    529                 }
    530                 else
    531                 {
    532                     owl::error("Please specify an account with -a {jid}");
    533                     return;
    534                 }
    535             }
    536             else
    537             {
    538                 $jid = resolveJID($jid);
    539                 return unless $jid;
    540             }
    541         }
     488        my ($jid, $muc, @args) = @_;
     489
     490        $muc = shift @args if scalar @args;
     491        die("Usage: jmuc part {muc} [-a account]") unless $muc;
     492
    542493        $connections->{$jid}->{client}->PresenceSend(to => $muc, type => 'unavailable');
    543494        queue_admin_msg("$jid has left $muc.");
     
    546497sub jmuc_invite
    547498{
    548         my $jid;
    549         my $invite_jid;
    550         my $muc;
    551 
    552         owl::error('Usage: jmuc invite {jid} [muc] [-a account]') if (!$_[0]);
    553         $invite_jid = $_[0];
    554        
    555         if (!@_[1])
    556         {       
    557             my $m = owl::getcurmsg();
    558             if ($m->is_jabber && $m->{jtype} eq 'groupchat')
    559             {
    560                 $muc = $m->{room};
    561                 $jid = $m->{to};
    562             }
    563             else
    564             {
    565                 owl::error('Usage: jmuc invite {jid} [muc] [-a account]');
    566                 return;
    567             }
    568         }
    569         else
    570         {
    571             local @ARGV = @_;
    572             GetOptions('account=s' => \$jid);
    573             if (scalar @ARGV != 2)
    574             {
    575                 owl::error('Usage: jmuc invite {jid} [muc] [-a account]');
    576                 return;
    577             }
    578             else
    579             {
    580                 ($muc, $invite_jid) = @ARGV;
    581             }
    582             if (!$jid)
    583             {
    584                 if (connected() == 1)
    585                 {
    586                     $jid = (keys %$connections)[0];
    587                 }
    588                 else
    589                 {
    590                     owl::error("Please specify an account with -a {jid}");
    591                     return;
    592                 }
    593             }
    594             else
    595             {
    596                 $jid = resolveJID($jid);
    597                 return unless $jid;
    598             }
    599         }
     499        my ($jid, $muc, @args) = @_;
     500
     501        my $invite_jid = shift @args;
     502        $muc = shift @args if scalar @args;
     503
     504        die('Usage: jmuc invite {jid} [muc] [-a account]') unless $muc && $invite_jid;
    600505       
    601506        my $x = new XML::Stream::Node('x');
     
    608513        $connections->{$jid}->{client}->Send($message);
    609514        queue_admin_msg("$jid has invited $invite_jid to $muc.");
     515}
     516
     517Net::Jabber::Namespaces::add_ns(
     518        ns  => "http://jabber.org/protocol/muc#owner",
     519        tag => 'query',
     520       );
     521
     522sub jmuc_configure {
     523        my ($jid, $muc, @args) = @_;
     524        $muc = shift @args if scalar @args;
     525        die("Usage: jmuc configure [muc]") unless $muc;
     526        my $iq = Net::Jabber::IQ->new();
     527        $iq->SetTo($muc);
     528        $iq->SetType('set');
     529        my $query = $iq->NewQuery("http://jabber.org/protocol/muc#owner");
     530        my $x = $query->NewChild("jabber:x:data");
     531        $x->SetType('submit');
     532
     533        $connections->{$jid}->{client}->Send($iq);
     534        queue_admin_msg("Accepted default instant configuration for $muc");
    610535}
    611536
     
    776701sub boldify($)
    777702{
    778     $str = shift;
     703    my $str = shift;
    779704
    780705    return '@b('.$str.')' if ( $str !~ /\)/ );
     
    807732{
    808733    return scalar keys %$connections;
     734}
     735
     736sub defaultJID {
     737        return (keys %$connections)[0]  if (connected() == 1);
     738        return;
    809739}
    810740
Note: See TracChangeset for help on using the changeset viewer.