Ignore:
Timestamp:
Dec 30, 2009, 2:12:15 PM (14 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, release-1.10, release-1.6, release-1.7, release-1.8, release-1.9
Children:
901b931
Parents:
85fa6e4 (diff), d41f773 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:
Merge branch 'release-1.5'
Location:
perl/modules/IRC/lib/BarnOwl/Module
Files:
2 edited

Legend:

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

    r618a980 r416241f  
    137137}
    138138
    139 use constant OPTIONAL_CHANNEL => 1;
    140 use constant REQUIRE_CHANNEL => 2;
     139=head2 mk_irc_command SUB FLAGS
     140
     141Return a subroutine that can be bound as a an IRC command. The
     142subroutine will be called with arguments (COMMAND-NAME,
     143IRC-CONNECTION, [CHANNEL], ARGV...).
     144
     145C<IRC-CONNECTION> and C<CHANNEL> will be inferred from arguments to
     146the command and the current message if appropriate.
     147
     148The bitwise C<or> of zero or more C<FLAGS> can be passed in as a
     149second argument to alter the behavior of the returned commands:
     150
     151=over 4
     152
     153=item C<CHANNEL_ARG>
     154
     155This command accepts the name of a channel. Pass in the C<CHANNEL>
     156argument listed above, and die if no channel argument can be found.
     157
     158=item C<CHANNEL_OPTIONAL>
     159
     160Pass the channel argument, but don't die if not present. Only relevant
     161with C<CHANNEL_ARG>.
     162
     163=item C<ALLOW_DISCONNECTED>
     164
     165C<IRC-CONNECTION> may be a disconnected connection object that is
     166currently pending a reconnect.
     167
     168=back
     169
     170=cut
     171
     172use constant CHANNEL_ARG        => 1;
     173use constant CHANNEL_OPTIONAL   => 2;
     174
     175use constant ALLOW_DISCONNECTED => 4;
    141176
    142177sub register_commands {
     
    167202
    168203    BarnOwl::new_command(
    169         'irc-disconnect' => \&cmd_disconnect,
     204        'irc-disconnect' => mk_irc_command( \&cmd_disconnect, ALLOW_DISCONNECTED ),
    170205        {
    171206            summary => 'Disconnect from an IRC server',
     
    192227
    193228    BarnOwl::new_command(
    194         'irc-mode' => mk_irc_command( \&cmd_mode, OPTIONAL_CHANNEL ),
     229        'irc-mode' => mk_irc_command( \&cmd_mode, CHANNEL_OPTIONAL|CHANNEL_ARG ),
    195230        {
    196231            summary => 'Change an IRC channel or user mode',
     
    216251
    217252    BarnOwl::new_command(
    218         'irc-part' => mk_irc_command( \&cmd_part, REQUIRE_CHANNEL ),
     253        'irc-part' => mk_irc_command( \&cmd_part, CHANNEL_ARG ),
    219254        {
    220255            summary => 'Leave an IRC channel',
     
    241276
    242277    BarnOwl::new_command(
    243         'irc-names' => mk_irc_command( \&cmd_names, REQUIRE_CHANNEL ),
     278        'irc-names' => mk_irc_command( \&cmd_names, CHANNEL_ARG ),
    244279        {
    245280            summary => 'View the list of users in a channel',
     
    294329
    295330    BarnOwl::new_command(
    296         'irc-topic' => mk_irc_command( \&cmd_topic, REQUIRE_CHANNEL ),
     331        'irc-topic' => mk_irc_command( \&cmd_topic, CHANNEL_ARG ),
    297332        {
    298333            summary => 'View or change the topic of an IRC channel',
     
    391426
    392427sub cmd_disconnect {
    393     # Such a hack
    394     local *get_connection_by_alias = sub {
    395         my $key = shift;
    396         return $ircnets{$key} if exists $ircnets{$key};
    397         return $reconnect{$key}{conn} if exists $reconnect{$key};
    398         die("No such ircnet: $key\n");
    399     };
    400 
    401     mk_irc_command(
    402         sub {
    403             my $cmd = shift;
    404             my $conn = shift;
    405             if ($conn->conn->connected) {
    406                 $conn->conn->disconnect;
    407             } elsif ($reconnect{$conn->alias}) {
    408                 BarnOwl::admin_message('IRC',
    409                                        "[" . $conn->alias . "] Reconnect cancelled");
    410                 delete $reconnect{$conn->alias};
    411             }
    412         }
    413     )->(@_);
     428    my $cmd = shift;
     429    my $conn = shift;
     430    if ($conn->conn->connected) {
     431        $conn->conn->disconnect;
     432    } elsif ($reconnect{$conn->alias}) {
     433        BarnOwl::admin_message('IRC',
     434                               "[" . $conn->alias . "] Reconnect cancelled");
     435        $conn->cancel_reconnect;
     436    }
    414437}
    415438
     
    564587sub mk_irc_command {
    565588    my $sub = shift;
    566     my $use_channel = shift || 0;
     589    my $flags = shift || 0;
    567590    return sub {
    568591        my $cmd = shift;
     
    578601
    579602        if(defined($alias)) {
    580             $conn = get_connection_by_alias($alias);
    581         }
    582         if($use_channel) {
     603            $conn = get_connection_by_alias($alias,
     604                                            $flags & ALLOW_DISCONNECTED);
     605        }
     606        if($flags & CHANNEL_ARG) {
    583607            $channel = $ARGV[0];
    584608            if(defined($channel) && $channel =~ /^#/) {
     
    594618        }
    595619
    596         if(!$channel && $use_channel == REQUIRE_CHANNEL) {
     620        if(!$channel &&
     621           ($flags & CHANNEL_ARG) &&
     622           !($flags & CHANNEL_OPTIONAL)) {
    597623            die("Usage: $cmd <channel>\n");
    598624        }
    599625        if(!$conn) {
    600626            if($m && $m->type eq 'IRC') {
    601                 $conn = get_connection_by_alias($m->network);
     627                $conn = get_connection_by_alias($m->network,
     628                                               $flags & ALLOW_DISCONNECTED);
    602629            }
    603630        }
     
    608635            die("You must specify an IRC network using -a.\n");
    609636        }
    610         if($use_channel) {
     637        if($flags & CHANNEL_ARG) {
    611638            $sub->($cmd, $conn, $channel, @ARGV);
    612639        } else {
     
    618645sub get_connection_by_alias {
    619646    my $key = shift;
    620     die("No such ircnet: $key\n") unless exists $ircnets{$key};
    621     return $ircnets{$key};
     647    my $allow_disconnected = shift;
     648
     649    return $ircnets{$key} if exists $ircnets{$key};
     650    return $reconnect{$key} if $allow_disconnected && exists $reconnect{$key};
     651    die("No such ircnet: $key\n")
    622652}
    623653
  • perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm

    r618a980 r416241f  
    2323
    2424use BarnOwl;
     25use Scalar::Util qw(weaken);
    2526
    2627BEGIN {
     
    315316    my $interval = shift || 5;
    316317    delete $BarnOwl::Module::IRC::ircnets{$self->alias};
    317     $BarnOwl::Module::IRC::reconnect{$self->alias} =
     318    $BarnOwl::Module::IRC::reconnect{$self->alias} = $self;
     319    my $weak = $self;
     320    weaken($weak);
     321    $self->{reconnect_timer} =
    318322        BarnOwl::Timer->new( {
    319323            after => $interval,
    320324            cb    => sub {
    321                 $self->reconnect( $interval );
     325                $weak->reconnect( $interval ) if $weak;
    322326            },
    323327        } );
    324     $BarnOwl::Module::IRC::reconnect{$self->alias}{conn} = $self;
     328}
     329
     330sub cancel_reconnect {
     331    my $self = shift;
     332    delete $BarnOwl::Module::IRC::reconnect{$self->alias};
     333    delete $self->{reconnect_timer};
    325334}
    326335
     
    329338    my $msg = shift;
    330339    BarnOwl::admin_message("IRC", $msg);
    331     delete $BarnOwl::Module::IRC::reconnect{$self->alias};
     340    $self->cancel_reconnect;
    332341    $BarnOwl::Module::IRC::ircnets{$self->alias} = $self;
    333342    my $fd = $self->getSocket()->fileno();
Note: See TracChangeset for help on using the changeset viewer.