Ignore:
Timestamp:
Feb 16, 2009, 4:38:22 PM (16 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, debian, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
10d67d5
Parents:
52a0f14
Message:
Refactor IRC argument processing.

The primary concrete improvement here is that '-a [connection]' no
longer needs to come at the start of the command line.
Location:
perl/modules/IRC/lib/BarnOwl/Module
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • perl/modules/IRC/lib/BarnOwl/Module/IRC.pm

    r56d0189 r330c55a  
    9393}
    9494
     95use constant OPTIONAL_CHANNEL => 1;
     96use constant REQUIRE_CHANNEL => 2;
     97
    9598sub register_commands {
    9699    BarnOwl::new_command('irc-connect' => \&cmd_connect,
     
    106109                           "-n <nick>           Use a non-default nick"
    107110                       });
    108     BarnOwl::new_command('irc-disconnect' => \&cmd_disconnect);
    109     BarnOwl::new_command('irc-msg'        => \&cmd_msg);
    110     BarnOwl::new_command('irc-join'       => \&cmd_join);
    111     BarnOwl::new_command('irc-part'       => \&cmd_part);
    112     BarnOwl::new_command('irc-nick'       => \&cmd_nick);
    113     BarnOwl::new_command('irc-names'      => \&cmd_names);
    114     BarnOwl::new_command('irc-whois'      => \&cmd_whois);
    115     BarnOwl::new_command('irc-motd'       => \&cmd_motd);
     111    BarnOwl::new_command('irc-disconnect' => mk_irc_command(\&cmd_disconnect));
     112    BarnOwl::new_command('irc-msg'        => mk_irc_command(\&cmd_msg, OPTIONAL_CHANNEL));
     113    BarnOwl::new_command('irc-join'       => mk_irc_command(\&cmd_join));
     114    BarnOwl::new_command('irc-part'       => mk_irc_command(\&cmd_part, REQUIRE_CHANNEL));
     115    BarnOwl::new_command('irc-nick'       => mk_irc_command(\&cmd_nick));
     116    BarnOwl::new_command('irc-names'      => mk_irc_command(\&cmd_names, REQUIRE_CHANNEL));
     117    BarnOwl::new_command('irc-whois'      => mk_irc_command(\&cmd_whois));
     118    BarnOwl::new_command('irc-motd'       => mk_irc_command(\&cmd_motd));
    116119    BarnOwl::new_command('irc-list'       => \&cmd_list);
    117     BarnOwl::new_command('irc-who'        => \&cmd_who);
    118     BarnOwl::new_command('irc-stats'      => \&cmd_stats);
    119     BarnOwl::new_command('irc-topic'      => \&cmd_topic);
     120    BarnOwl::new_command('irc-who'        => mk_irc_command(\&cmd_who));
     121    BarnOwl::new_command('irc-stats'      => mk_irc_command(\&cmd_stats));
     122    BarnOwl::new_command('irc-topic'      => mk_irc_command(\&cmd_topic, REQUIRE_CHANNEL));
    120123}
    121124
     
    195198
    196199sub cmd_msg {
    197     my $cmd = shift;
    198     my $conn = get_connection(\@_);
    199     my $to = shift or die("Usage: $cmd NICK\n");
     200    my $cmd  = shift;
     201    my $conn = shift;
     202    my $to = shift || shift or die("Usage: $cmd NICK\n");
    200203    # handle multiple recipients?
    201204    if(@_) {
     
    236239sub cmd_join {
    237240    my $cmd = shift;
    238     my $conn = get_connection(\@_);
     241    my $conn = shift;
    239242    my $chan = shift or die("Usage: $cmd channel\n");
    240243    $channels{$chan} ||= [];
     
    245248sub cmd_part {
    246249    my $cmd = shift;
    247     my $conn = get_connection(\@_);
    248     my $chan = get_channel(\@_) || die("Usage: $cmd <channel>\n");
     250    my $conn = shift;
     251    my $chan = shift;
    249252    $channels{$chan} = [grep {$_ ne $conn} @{$channels{$chan} || []}];
    250253    $conn->conn->part($chan);
     
    253256sub cmd_nick {
    254257    my $cmd = shift;
    255     my $conn = get_connection(\@_);
     258    my $conn = shift;
    256259    my $nick = shift or die("Usage: $cmd <new nick>\n");
    257260    $conn->conn->nick($nick);
     
    260263sub cmd_names {
    261264    my $cmd = shift;
    262     my $conn = get_connection(\@_);
    263     my $chan = get_channel(\@_) || die("Usage: $cmd <channel>\n");
     265    my $conn = shift;
     266    my $chan = shift;
    264267    $conn->names_tmp([]);
    265268    $conn->conn->names($chan);
     
    268271sub cmd_whois {
    269272    my $cmd = shift;
    270     my $conn = get_connection(\@_);
     273    my $conn = shift;
    271274    my $who = shift || die("Usage: $cmd <user>\n");
    272275    $conn->conn->whois($who);
     
    275278sub cmd_motd {
    276279    my $cmd = shift;
    277     my $conn = get_connection(\@_);
     280    my $conn = shift;
    278281    $conn->conn->motd;
    279282}
     
    290293sub cmd_who {
    291294    my $cmd = shift;
    292     my $conn = get_connection(\@_);
     295    my $conn = shift;
    293296    my $who = shift || die("Usage: $cmd <user>\n");
     297    BarnOwl::error("WHO $cmd $conn $who");
    294298    $conn->conn->who($who);
    295299}
     
    297301sub cmd_stats {
    298302    my $cmd = shift;
    299     my $conn = get_connection(\@_);
     303    my $conn = shift;
    300304    my $type = shift || die("Usage: $cmd <chiklmouy> [server] \n");
    301305    $conn->conn->stats($type, @_);
     
    304308sub cmd_topic {
    305309    my $cmd = shift;
    306     my $conn = get_connection(\@_);
    307     $conn->conn->topic(@_);
     310    my $conn = shift;
     311    my $chan = shift;
     312    $conn->conn->topic($chan, @_ ? join(" ", @_) : undef);
    308313}
    309314
     
    312317################################################################################
    313318
    314 sub get_connection {
    315     my $args = shift;
    316     if(scalar @$args >= 2 && $args->[0] eq '-a') {
    317         shift @$args;
    318         return get_connection_by_alias(shift @$args);
    319     }
    320     my $channel = $args->[-1];
    321     if (defined($channel) && $channel =~ /^#/
    322         and $channels{$channel} and @{$channels{$channel}} == 1) {
    323         return $channels{$channel}[0];
    324     }
    325     my $m = BarnOwl::getcurmsg();
    326     if($m && $m->type eq 'IRC') {
    327         return get_connection_by_alias($m->network);
    328     }
    329     if(scalar keys %ircnets == 1) {
    330         return [values(%ircnets)]->[0];
    331     }
    332     die("You must specify a network with -a\n");
    333 }
    334 
    335 sub get_channel {
    336     my $args = shift;
    337     if(scalar @$args) {
    338         return shift @$args;
    339     }
    340     my $m = BarnOwl::getcurmsg();
    341     if($m && $m->type eq 'IRC') {
    342         return $m->channel if !$m->is_private;
    343     }
    344     return undef;
     319sub mk_irc_command {
     320    my $sub = shift;
     321    my $use_channel = shift || 0;
     322    return sub {
     323        my $cmd = shift;
     324        my $conn;
     325        my $alias;
     326        my $channel;
     327        my $getopt = Getopt::Long::Parser->new;
     328        my $m = BarnOwl::getcurmsg();
     329
     330        local @ARGV = @_;
     331        $getopt->configure(qw(pass_through permute no_getopt_compat prefix_pattern=-|--));
     332        $getopt->getoptions("alias=s" => \$alias);
     333
     334        if(defined($alias)) {
     335            $conn = get_connection_by_alias($alias);
     336        }
     337        if(!$conn && $use_channel) {
     338            $channel = $ARGV[-1];
     339            if(defined($channel) && $channel =~ /^#/) {
     340                if($channels{$channel} && @{$channels{$channel}} == 1) {
     341                    pop @ARGV;
     342                    $conn = $channels{$channel}[0];
     343                } 
     344            } else {
     345                if($m && $m->type eq 'IRC' && !$m->is_private) {
     346                    $channel = $m->channel;
     347                } else {
     348                    undef $channel;
     349                }
     350            }
     351        }
     352        if(!$channel && $use_channel == REQUIRE_CHANNEL) {
     353            die("Usage: $cmd <channel>\n");
     354        }
     355        if(!$conn) {
     356            if($m && $m->type eq 'IRC') {
     357                $conn = get_connection_by_alias($m->network);
     358            }
     359        }
     360        if(!$conn && scalar keys %ircnets == 1) {
     361            $conn = [values(%ircnets)]->[0];
     362        }
     363        if(!$conn) {
     364            die("You must specify an IRC network using -a.\n");
     365        }
     366        if($use_channel) {
     367            $sub->($cmd, $conn, $channel, @ARGV);
     368        } else {
     369            $sub->($cmd, $conn, @ARGV);
     370        }
     371    };
    345372}
    346373
  • perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm

    r3048f1f r330c55a  
    7474    $self->conn->add_handler(endofwhois=> sub { shift; $self->on_endofwhois(@_) });
    7575
     76    # * nosuchchannel
     77    # *
     78
    7679    return $self;
    7780}
Note: See TracChangeset for help on using the changeset viewer.