source: perl/modules/IRC/lib/BarnOwl/Module/IRC.pm @ 618a980

release-1.10release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 618a980 was 618a980, checked in by Nelson Elhage <nelhage@mit.edu>, 14 years ago
irc-disconnect on a pending reconnect should cancel the reconnect
  • Property mode set to 100644
File size: 16.4 KB
RevLine 
[b38b0b2]1use strict;
2use warnings;
3
4package BarnOwl::Module::IRC;
5
6=head1 NAME
7
[2c40dc0]8BarnOwl::Module::IRC
[b38b0b2]9
10=head1 DESCRIPTION
11
[2c40dc0]12This module implements IRC support for barnowl.
[b38b0b2]13
14=cut
15
16use BarnOwl;
17use BarnOwl::Hooks;
18use BarnOwl::Message::IRC;
[380b1ab]19use BarnOwl::Module::IRC::Connection qw(is_private);
[ab9cd8f]20use BarnOwl::Module::IRC::Completion;
[b38b0b2]21
22use Net::IRC;
23use Getopt::Long;
24
[2c40dc0]25our $VERSION = 0.02;
[b38b0b2]26
27our $irc;
28
29# Hash alias -> BarnOwl::Module::IRC::Connection object
30our %ircnets;
[fe8cad8]31our %channels;
[3b4ba7d]32our %reconnect;
[b38b0b2]33
34sub startup {
[b10f340]35    BarnOwl::new_variable_string('irc:nick', {
36        default     => $ENV{USER},
37        summary     => 'The default IRC nickname',
38        description => 'By default, irc-connect will use this nick '  .
39        'when connecting to a new server. See :help irc-connect for ' .
40        'more information.'
41       });
42
43    BarnOwl::new_variable_string('irc:user', {
44        default => $ENV{USER},
45        summary => 'The IRC "username" field'
46       });
47        BarnOwl::new_variable_string('irc:name', {
48        default => "",
49        summary     => 'A short name field for IRC',
50        description => 'A short (maybe 60 or so chars) piece of text, ' .
51        'originally intended to display your real name, which people '  .
52        'often use for pithy quotes and URLs.'
53       });
[cd12307]54
[b10f340]55    BarnOwl::new_variable_bool('irc:spew', {
56        default     => 0,
57        summary     => 'Show unhandled IRC events',
58        description => 'If set, display all unrecognized IRC events as ' .
[cd12307]59        'admin messages. Intended for debugging and development use only.'
[b10f340]60       });
[f81176c]61
62    BarnOwl::new_variable_string('irc:skip', {
63        default     => 'welcome yourhost created ' .
64        'luserclient luserme luserop luserchannels',
65        summary     => 'Skip messages of these types',
66        description => 'If set, each (space-separated) message type ' .
67        'provided will be hidden and ignored if received.'
68       });
69
[b38b0b2]70    register_commands();
71    register_handlers();
[96f7b07]72    BarnOwl::filter(qw{irc type ^IRC$ or ( type ^admin$ and adminheader ^IRC$ )});
[b38b0b2]73}
74
75sub shutdown {
76    for my $conn (values %ircnets) {
[ba2ca66]77        $conn->conn->disconnect();
[b38b0b2]78    }
79}
80
[f17bb2c0]81sub quickstart {
82    return <<'END_QUICKSTART';
83@b[IRC:]
84Use ':irc-connect @b[server]' to connect to an IRC server, and
85':irc-join @b[#channel]' to join a channel. ':irc-msg @b[#channel]
86@b[message]' sends a message to a channel.
87END_QUICKSTART
88}
89
[da554da]90sub buddylist {
91    my $list = "";
92
93    for my $net (sort keys %ircnets) {
94        my $conn = $ircnets{$net};
95        my ($nick, $server) = ($conn->nick, $conn->server);
[6396c1e]96        $list .= BarnOwl::Style::boldify("IRC channels for $net ($nick\@$server)");
97        $list .= "\n";
[da554da]98
99        for my $chan (keys %channels) {
100            next unless grep $_ eq $conn, @{$channels{$chan}};
101            $list .= "  $chan\n";
102        }
103    }
104
105    return $list;
106}
107
[9c7a701]108#sub mainloop_hook {
109#    return unless defined $irc;
110#    eval {
111#        $irc->do_one_loop();
112#    };
113#    return;
114#}
115
116sub OwlProcess {
[b38b0b2]117    return unless defined $irc;
118    eval {
119        $irc->do_one_loop();
120    };
121    return;
122}
123
[9c7a701]124
[b38b0b2]125sub register_handlers {
126    if(!$irc) {
127        $irc = Net::IRC->new;
128        $irc->timeout(0);
129    }
130}
131
[f81176c]132sub skip_msg {
133    my $class = shift;
134    my $type = lc shift;
135    my $skip = lc BarnOwl::getvar('irc:skip');
136    return grep {$_ eq $type} split ' ', $skip;
137}
138
[330c55a]139use constant OPTIONAL_CHANNEL => 1;
140use constant REQUIRE_CHANNEL => 2;
141
[b38b0b2]142sub register_commands {
[f17bb2c0]143    BarnOwl::new_command(
144        'irc-connect' => \&cmd_connect,
145        {
146            summary => 'Connect to an IRC server',
147            usage =>
148'irc-connect [-a ALIAS ] [-s] [-p PASSWORD] [-n NICK] SERVER [port]',
[cd12307]149            description => <<END_DESCR
150Connect to an IRC server. Supported options are:
[f17bb2c0]151
152 -a <alias>          Define an alias for this server
153 -s                  Use SSL
154 -p <password>       Specify the password to use
155 -n <nick>           Use a non-default nick
156
157The -a option specifies an alias to use for this connection. This
158alias can be passed to the '-a' argument of any other IRC command to
159control which connection it operates on.
160
161For servers with hostnames of the form "irc.FOO.{com,org,...}", the
162alias will default to "FOO"; For other servers the full hostname is
163used.
164END_DESCR
165        }
166    );
167
168    BarnOwl::new_command(
[618a980]169        'irc-disconnect' => \&cmd_disconnect,
[f17bb2c0]170        {
171            summary => 'Disconnect from an IRC server',
172            usage   => 'irc-disconnect [-a ALIAS]',
173
174            description => <<END_DESCR
175Disconnect from an IRC server. You can specify a specific server with
176"-a SERVER-ALIAS" if necessary.
177END_DESCR
178        }
179    );
180
181    BarnOwl::new_command(
[e0fba58]182        'irc-msg' => mk_irc_command( \&cmd_msg ),
[f17bb2c0]183        {
184            summary => 'Send an IRC message',
185            usage   => 'irc-msg [-a ALIAS] DESTINATION MESSAGE',
186
187            description => <<END_DESCR
[cd12307]188Send an IRC message.
[f17bb2c0]189END_DESCR
190        }
191    );
192
193    BarnOwl::new_command(
194        'irc-mode' => mk_irc_command( \&cmd_mode, OPTIONAL_CHANNEL ),
195        {
196            summary => 'Change an IRC channel or user mode',
197            usage   => 'irc-mode [-a ALIAS] TARGET [+-]MODE OPTIONS',
198
199            description => <<END_DESCR
200Change the mode of an IRC user or channel.
201END_DESCR
202        }
203    );
204
205    BarnOwl::new_command(
206        'irc-join' => mk_irc_command( \&cmd_join ),
207        {
208            summary => 'Join an IRC channel',
[1b62a55]209            usage   => 'irc-join [-a ALIAS] #channel [KEY]',
[f17bb2c0]210
211            description => <<END_DESCR
212Join an IRC channel.
213END_DESCR
214        }
215    );
216
217    BarnOwl::new_command(
218        'irc-part' => mk_irc_command( \&cmd_part, REQUIRE_CHANNEL ),
219        {
220            summary => 'Leave an IRC channel',
221            usage   => 'irc-part [-a ALIAS] #channel',
222
223            description => <<END_DESCR
224Part from an IRC channel.
225END_DESCR
226        }
227    );
228
229    BarnOwl::new_command(
230        'irc-nick' => mk_irc_command( \&cmd_nick ),
231        {
232            summary => 'Change your IRC nick on an existing connection.',
233            usage   => 'irc-nick [-a ALIAS] NEW-NICK',
234
235            description => <<END_DESCR
236Set your IRC nickname on an existing connect. To change it prior to
237connecting, adjust the `irc:nick' variable.
238END_DESCR
239        }
240    );
241
242    BarnOwl::new_command(
243        'irc-names' => mk_irc_command( \&cmd_names, REQUIRE_CHANNEL ),
244        {
245            summary => 'View the list of users in a channel',
246            usage   => 'irc-names [-a ALIAS] #channel',
247
248            description => <<END_DESCR
249`irc-names' displays the list of users in a given channel in a pop-up
250window.
251END_DESCR
252        }
253    );
254
255    BarnOwl::new_command(
256        'irc-whois' => mk_irc_command( \&cmd_whois ),
257        {
258            summary => 'Displays information about a given IRC user',
259            usage   => 'irc-whois [-a ALIAS] NICK',
260
261            description => <<END_DESCR
262Pops up information about a given IRC user.
263END_DESCR
264        }
265    );
266
267    BarnOwl::new_command(
268        'irc-motd' => mk_irc_command( \&cmd_motd ),
269        {
270            summary => 'Displays an IRC server\'s MOTD (Message of the Day)',
271            usage   => 'irc-motd [-a ALIAS]',
272
273            description => <<END_DESCR
274Displays an IRC server's message of the day.
275END_DESCR
276        }
277    );
278
279    BarnOwl::new_command(
280        'irc-list' => \&cmd_list,
281        {
282            summary => 'Show all the active IRC connections.',
283            usage   => 'irc-list',
284
285            description => <<END_DESCR
286Show all the currently active IRC connections with their aliases and
287server names.
288END_DESCR
289        }
290    );
291
292    BarnOwl::new_command( 'irc-who'   => mk_irc_command( \&cmd_who ) );
293    BarnOwl::new_command( 'irc-stats' => mk_irc_command( \&cmd_stats ) );
294
295    BarnOwl::new_command(
296        'irc-topic' => mk_irc_command( \&cmd_topic, REQUIRE_CHANNEL ),
297        {
298            summary => 'View or change the topic of an IRC channel',
299            usage   => 'irc-topic [-a ALIAS] #channel [TOPIC]',
300
301            description => <<END_DESCR
302Without extra arguments, fetches and displays a given channel's topic.
303
304With extra arguments, changes the target channel's topic string. This
305may require +o on some channels.
306END_DESCR
307        }
308    );
309
310    BarnOwl::new_command(
311        'irc-quote' => mk_irc_command( \&cmd_quote ),
312        {
313            summary => 'Send a raw command to the IRC servers.',
314            usage   => 'irc-quote [-a ALIAS] TEXT',
315
316            description => <<END_DESCR
317Send a raw command line to an IRC server.
318
319This can be used to perform some operation not yet supported by
320BarnOwl, or to define new IRC commands.
321END_DESCR
322        }
323    );
[b38b0b2]324}
325
[f17bb2c0]326
[167044b]327$BarnOwl::Hooks::startup->add('BarnOwl::Module::IRC::startup');
328$BarnOwl::Hooks::shutdown->add('BarnOwl::Module::IRC::shutdown');
[f17bb2c0]329$BarnOwl::Hooks::getQuickstart->add('BarnOwl::Module::IRC::quickstart');
[da554da]330$BarnOwl::Hooks::getBuddyList->add("BarnOwl::Module::IRC::buddylist");
[b38b0b2]331
332################################################################################
333######################## Owl command handlers ##################################
334################################################################################
335
336sub cmd_connect {
337    my $cmd = shift;
338
[2c40dc0]339    my $nick = BarnOwl::getvar('irc:nick');
340    my $username = BarnOwl::getvar('irc:user');
341    my $ircname = BarnOwl::getvar('irc:name');
[b38b0b2]342    my $host;
343    my $port;
344    my $alias;
345    my $ssl;
346    my $password = undef;
347
348    {
349        local @ARGV = @_;
350        GetOptions(
351            "alias=s"    => \$alias,
352            "ssl"        => \$ssl,
[2c40dc0]353            "password=s" => \$password,
[b10f340]354            "nick=s"     => \$nick,
[2c40dc0]355        );
[b38b0b2]356        $host = shift @ARGV or die("Usage: $cmd HOST\n");
357        if(!$alias) {
[f094fc4]358            if($host =~ /^(?:irc[.])?([\w-]+)[.]\w+$/) {
[b0c8011]359                $alias = $1;
360            } else {
361                $alias = $host;
362            }
[b38b0b2]363        }
364        $ssl ||= 0;
[f094fc4]365        $port = shift @ARGV || ($ssl ? 6697 : 6667);
[b38b0b2]366    }
367
[b0c8011]368    if(exists $ircnets{$alias}) {
369        die("Already connected to a server with alias '$alias'. Either disconnect or specify an alias with -a.\n");
370    }
371
[b38b0b2]372    my $conn = BarnOwl::Module::IRC::Connection->new($irc, $alias,
373        Nick      => $nick,
374        Server    => $host,
375        Port      => $port,
376        Username  => $username,
377        Ircname   => $ircname,
378        Port      => $port,
379        Password  => $password,
380        SSL       => $ssl
381       );
382
[cab045b]383    if ($conn->conn->connected) {
[3b4ba7d]384        $conn->connected("Connected to $alias as $nick");
[5ff830a]385    } else {
386        die("IRC::Connection->connect failed: $!");
387    }
388
[b38b0b2]389    return;
390}
391
392sub cmd_disconnect {
[618a980]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    )->(@_);
[b38b0b2]414}
415
416sub cmd_msg {
[330c55a]417    my $cmd  = shift;
418    my $conn = shift;
[e0fba58]419    my $to = shift or die("Usage: $cmd [NICK|CHANNEL]\n");
[2c40dc0]420    # handle multiple recipients?
[b38b0b2]421    if(@_) {
422        process_msg($conn, $to, join(" ", @_));
423    } else {
[744769e]424        BarnOwl::start_edit_win(BarnOwl::quote('/msg', '-a', $conn->alias, $to), sub {process_msg($conn, $to, @_)});
[b38b0b2]425    }
[48f7d12]426    return;
[b38b0b2]427}
428
429sub process_msg {
430    my $conn = shift;
431    my $to = shift;
432    my $body = shift;
433    # Strip whitespace. In the future -- send one message/line?
434    $body =~ tr/\n\r/  /;
[919535f]435    if ($body =~ /^\/me (.*)/) {
[0c4a190]436        $conn->conn->me($to, Encode::encode('utf-8', $1));
[56d0189]437        $body = '* '.$conn->nick.' '.$1;
[919535f]438    } else {
[0c4a190]439        $conn->conn->privmsg($to, Encode::encode('utf-8', $body));
[919535f]440    }
[b38b0b2]441    my $msg = BarnOwl::Message->new(
442        type        => 'IRC',
[6858d2d]443        direction   => is_private($to) ? 'out' : 'in',
[b38b0b2]444        server      => $conn->server,
445        network     => $conn->alias,
446        recipient   => $to,
447        body        => $body,
448        sender      => $conn->nick,
[2c40dc0]449        is_private($to) ?
[0e52069]450          (isprivate  => 'true') : (channel => $to),
[744769e]451        replycmd    => BarnOwl::quote('irc-msg',  '-a', $conn->alias, $to),
452        replysendercmd => BarnOwl::quote('irc-msg', '-a', $conn->alias, $to),
[b38b0b2]453       );
454    BarnOwl::queue_message($msg);
[48f7d12]455    return;
[b38b0b2]456}
457
[e625b5e]458sub cmd_mode {
459    my $cmd = shift;
460    my $conn = shift;
461    my $target = shift;
462    $target ||= shift;
463    $conn->conn->mode($target, @_);
[48f7d12]464    return;
[e625b5e]465}
466
[2c40dc0]467sub cmd_join {
468    my $cmd = shift;
[330c55a]469    my $conn = shift;
[2c40dc0]470    my $chan = shift or die("Usage: $cmd channel\n");
[fe8cad8]471    $channels{$chan} ||= [];
472    push @{$channels{$chan}}, $conn;
[1b62a55]473    $conn->conn->join($chan, @_);
[48f7d12]474    return;
[2c40dc0]475}
[b38b0b2]476
[6858d2d]477sub cmd_part {
478    my $cmd = shift;
[330c55a]479    my $conn = shift;
480    my $chan = shift;
[fe8cad8]481    $channels{$chan} = [grep {$_ ne $conn} @{$channels{$chan} || []}];
[ba2ca66]482    $conn->conn->part($chan);
[48f7d12]483    return;
[6858d2d]484}
485
[6286f26]486sub cmd_nick {
487    my $cmd = shift;
[330c55a]488    my $conn = shift;
[b0c8011]489    my $nick = shift or die("Usage: $cmd <new nick>\n");
[ba2ca66]490    $conn->conn->nick($nick);
[48f7d12]491    return;
[6286f26]492}
493
[6858d2d]494sub cmd_names {
495    my $cmd = shift;
[330c55a]496    my $conn = shift;
497    my $chan = shift;
[d264c6d]498    $conn->names_tmp([]);
[ba2ca66]499    $conn->conn->names($chan);
[48f7d12]500    return;
[6858d2d]501}
502
[b0c8011]503sub cmd_whois {
504    my $cmd = shift;
[330c55a]505    my $conn = shift;
[b0c8011]506    my $who = shift || die("Usage: $cmd <user>\n");
[ba2ca66]507    $conn->conn->whois($who);
[48f7d12]508    return;
[b0c8011]509}
510
[56e72d5]511sub cmd_motd {
512    my $cmd = shift;
[330c55a]513    my $conn = shift;
[ba2ca66]514    $conn->conn->motd;
[48f7d12]515    return;
[56e72d5]516}
517
[f094fc4]518sub cmd_list {
519    my $cmd = shift;
520    my $message = BarnOwl::Style::boldify('Current IRC networks:') . "\n";
521    while (my ($alias, $conn) = each %ircnets) {
522        $message .= '  ' . $alias . ' => ' . $conn->nick . '@' . $conn->server . "\n";
523    }
524    BarnOwl::popless_ztext($message);
[48f7d12]525    return;
[f094fc4]526}
527
528sub cmd_who {
529    my $cmd = shift;
[330c55a]530    my $conn = shift;
[f094fc4]531    my $who = shift || die("Usage: $cmd <user>\n");
[330c55a]532    BarnOwl::error("WHO $cmd $conn $who");
[f094fc4]533    $conn->conn->who($who);
[48f7d12]534    return;
[f094fc4]535}
536
537sub cmd_stats {
538    my $cmd = shift;
[330c55a]539    my $conn = shift;
[f094fc4]540    my $type = shift || die("Usage: $cmd <chiklmouy> [server] \n");
541    $conn->conn->stats($type, @_);
[48f7d12]542    return;
[f094fc4]543}
544
[3ad15ff]545sub cmd_topic {
546    my $cmd = shift;
[330c55a]547    my $conn = shift;
548    my $chan = shift;
549    $conn->conn->topic($chan, @_ ? join(" ", @_) : undef);
[48f7d12]550    return;
[3ad15ff]551}
552
[af9de56]553sub cmd_quote {
554    my $cmd = shift;
555    my $conn = shift;
556    $conn->conn->sl(join(" ", @_));
[48f7d12]557    return;
[af9de56]558}
559
[b38b0b2]560################################################################################
561########################### Utilities/Helpers ##################################
562################################################################################
563
[330c55a]564sub mk_irc_command {
565    my $sub = shift;
566    my $use_channel = shift || 0;
567    return sub {
568        my $cmd = shift;
569        my $conn;
570        my $alias;
571        my $channel;
572        my $getopt = Getopt::Long::Parser->new;
573        my $m = BarnOwl::getcurmsg();
[b38b0b2]574
[330c55a]575        local @ARGV = @_;
576        $getopt->configure(qw(pass_through permute no_getopt_compat prefix_pattern=-|--));
577        $getopt->getoptions("alias=s" => \$alias);
578
579        if(defined($alias)) {
580            $conn = get_connection_by_alias($alias);
581        }
[ecee82f]582        if($use_channel) {
[e625b5e]583            $channel = $ARGV[0];
[330c55a]584            if(defined($channel) && $channel =~ /^#/) {
585                if($channels{$channel} && @{$channels{$channel}} == 1) {
[e625b5e]586                    shift @ARGV;
[ecee82f]587                    $conn = $channels{$channel}[0] unless $conn;
[330c55a]588                }
[ecee82f]589            } elsif ($m && $m->type eq 'IRC' && !$m->is_private) {
590                $channel = $m->channel;
591            } else {
592                undef $channel;
[330c55a]593            }
594        }
[ecee82f]595
[330c55a]596        if(!$channel && $use_channel == REQUIRE_CHANNEL) {
597            die("Usage: $cmd <channel>\n");
598        }
599        if(!$conn) {
600            if($m && $m->type eq 'IRC') {
601                $conn = get_connection_by_alias($m->network);
602            }
603        }
604        if(!$conn && scalar keys %ircnets == 1) {
605            $conn = [values(%ircnets)]->[0];
606        }
607        if(!$conn) {
608            die("You must specify an IRC network using -a.\n");
609        }
610        if($use_channel) {
611            $sub->($cmd, $conn, $channel, @ARGV);
612        } else {
613            $sub->($cmd, $conn, @ARGV);
614        }
615    };
[6858d2d]616}
617
[b38b0b2]618sub get_connection_by_alias {
[2c40dc0]619    my $key = shift;
620    die("No such ircnet: $key\n") unless exists $ircnets{$key};
[b38b0b2]621    return $ircnets{$key};
622}
623
6241;
Note: See TracBrowser for help on using the repository browser.