Ignore:
Timestamp:
Dec 28, 2009, 12:24:59 AM (15 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:
3acab0e
Parents:
dc8f6e0
git-author:
Nelson Elhage <nelhage@mit.edu> (12/23/09 12:28:21)
git-committer:
Nelson Elhage <nelhage@mit.edu> (12/28/09 00:24:59)
Message:
IRC: Refactor the mk_irc_command API and document it.
File:
1 edited

Legend:

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

    rac374fc r54b4a87  
    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=back
     164
     165=cut
     166
     167use constant CHANNEL_ARG        => 1;
     168use constant CHANNEL_OPTIONAL   => 2;
    141169
    142170sub register_commands {
     
    192220
    193221    BarnOwl::new_command(
    194         'irc-mode' => mk_irc_command( \&cmd_mode, OPTIONAL_CHANNEL ),
     222        'irc-mode' => mk_irc_command( \&cmd_mode, CHANNEL_OPTIONAL|CHANNEL_ARG ),
    195223        {
    196224            summary => 'Change an IRC channel or user mode',
     
    216244
    217245    BarnOwl::new_command(
    218         'irc-part' => mk_irc_command( \&cmd_part, REQUIRE_CHANNEL ),
     246        'irc-part' => mk_irc_command( \&cmd_part, CHANNEL_ARG ),
    219247        {
    220248            summary => 'Leave an IRC channel',
     
    241269
    242270    BarnOwl::new_command(
    243         'irc-names' => mk_irc_command( \&cmd_names, REQUIRE_CHANNEL ),
     271        'irc-names' => mk_irc_command( \&cmd_names, CHANNEL_ARG ),
    244272        {
    245273            summary => 'View the list of users in a channel',
     
    294322
    295323    BarnOwl::new_command(
    296         'irc-topic' => mk_irc_command( \&cmd_topic, REQUIRE_CHANNEL ),
     324        'irc-topic' => mk_irc_command( \&cmd_topic, CHANNEL_ARG ),
    297325        {
    298326            summary => 'View or change the topic of an IRC channel',
     
    547575sub mk_irc_command {
    548576    my $sub = shift;
    549     my $use_channel = shift || 0;
     577    my $flags = shift || 0;
    550578    return sub {
    551579        my $cmd = shift;
     
    563591            $conn = get_connection_by_alias($alias);
    564592        }
    565         if($use_channel) {
     593        if($flags & CHANNEL_ARG) {
    566594            $channel = $ARGV[0];
    567595            if(defined($channel) && $channel =~ /^#/) {
     
    577605        }
    578606
    579         if(!$channel && $use_channel == REQUIRE_CHANNEL) {
     607        if(!$channel &&
     608           ($flags & CHANNEL_ARG) &&
     609           !($flags & CHANNEL_OPTIONAL)) {
    580610            die("Usage: $cmd <channel>\n");
    581611        }
     
    591621            die("You must specify an IRC network using -a.\n");
    592622        }
    593         if($use_channel) {
     623        if($flags & CHANNEL_ARG) {
    594624            $sub->($cmd, $conn, $channel, @ARGV);
    595625        } else {
Note: See TracChangeset for help on using the changeset viewer.