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

release-1.10release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since f81176c was f81176c, checked in by Alex Vandiver <alexmv@mit.edu>, 14 years ago
Skip some IRC admin messages by default, controlled by irc:skip
  • Property mode set to 100644
File size: 15.8 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);
96        $list .= BarnOwl::Style::boldify("IRC channels for $net ($nick\@$server)\n");
97
98        for my $chan (keys %channels) {
99            next unless grep $_ eq $conn, @{$channels{$chan}};
100            $list .= "  $chan\n";
101        }
102        $list .= "\n";
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(
169        'irc-disconnect' => mk_irc_command( \&cmd_disconnect ),
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 {
393    my $cmd = shift;
[33db995]394    my $conn = shift;
[ba2ca66]395    $conn->conn->disconnect;
[48f7d12]396    return;
[b38b0b2]397}
398
399sub cmd_msg {
[330c55a]400    my $cmd  = shift;
401    my $conn = shift;
[e0fba58]402    my $to = shift or die("Usage: $cmd [NICK|CHANNEL]\n");
[2c40dc0]403    # handle multiple recipients?
[b38b0b2]404    if(@_) {
405        process_msg($conn, $to, join(" ", @_));
406    } else {
[744769e]407        BarnOwl::start_edit_win(BarnOwl::quote('/msg', '-a', $conn->alias, $to), sub {process_msg($conn, $to, @_)});
[b38b0b2]408    }
[48f7d12]409    return;
[b38b0b2]410}
411
412sub process_msg {
413    my $conn = shift;
414    my $to = shift;
415    my $body = shift;
416    # Strip whitespace. In the future -- send one message/line?
417    $body =~ tr/\n\r/  /;
[919535f]418    if ($body =~ /^\/me (.*)/) {
[0c4a190]419        $conn->conn->me($to, Encode::encode('utf-8', $1));
[56d0189]420        $body = '* '.$conn->nick.' '.$1;
[919535f]421    } else {
[0c4a190]422        $conn->conn->privmsg($to, Encode::encode('utf-8', $body));
[919535f]423    }
[b38b0b2]424    my $msg = BarnOwl::Message->new(
425        type        => 'IRC',
[6858d2d]426        direction   => is_private($to) ? 'out' : 'in',
[b38b0b2]427        server      => $conn->server,
428        network     => $conn->alias,
429        recipient   => $to,
430        body        => $body,
431        sender      => $conn->nick,
[2c40dc0]432        is_private($to) ?
[0e52069]433          (isprivate  => 'true') : (channel => $to),
[744769e]434        replycmd    => BarnOwl::quote('irc-msg',  '-a', $conn->alias, $to),
435        replysendercmd => BarnOwl::quote('irc-msg', '-a', $conn->alias, $to),
[b38b0b2]436       );
437    BarnOwl::queue_message($msg);
[48f7d12]438    return;
[b38b0b2]439}
440
[e625b5e]441sub cmd_mode {
442    my $cmd = shift;
443    my $conn = shift;
444    my $target = shift;
445    $target ||= shift;
446    $conn->conn->mode($target, @_);
[48f7d12]447    return;
[e625b5e]448}
449
[2c40dc0]450sub cmd_join {
451    my $cmd = shift;
[330c55a]452    my $conn = shift;
[2c40dc0]453    my $chan = shift or die("Usage: $cmd channel\n");
[fe8cad8]454    $channels{$chan} ||= [];
455    push @{$channels{$chan}}, $conn;
[1b62a55]456    $conn->conn->join($chan, @_);
[48f7d12]457    return;
[2c40dc0]458}
[b38b0b2]459
[6858d2d]460sub cmd_part {
461    my $cmd = shift;
[330c55a]462    my $conn = shift;
463    my $chan = shift;
[fe8cad8]464    $channels{$chan} = [grep {$_ ne $conn} @{$channels{$chan} || []}];
[ba2ca66]465    $conn->conn->part($chan);
[48f7d12]466    return;
[6858d2d]467}
468
[6286f26]469sub cmd_nick {
470    my $cmd = shift;
[330c55a]471    my $conn = shift;
[b0c8011]472    my $nick = shift or die("Usage: $cmd <new nick>\n");
[ba2ca66]473    $conn->conn->nick($nick);
[48f7d12]474    return;
[6286f26]475}
476
[6858d2d]477sub cmd_names {
478    my $cmd = shift;
[330c55a]479    my $conn = shift;
480    my $chan = shift;
[d264c6d]481    $conn->names_tmp([]);
[ba2ca66]482    $conn->conn->names($chan);
[48f7d12]483    return;
[6858d2d]484}
485
[b0c8011]486sub cmd_whois {
487    my $cmd = shift;
[330c55a]488    my $conn = shift;
[b0c8011]489    my $who = shift || die("Usage: $cmd <user>\n");
[ba2ca66]490    $conn->conn->whois($who);
[48f7d12]491    return;
[b0c8011]492}
493
[56e72d5]494sub cmd_motd {
495    my $cmd = shift;
[330c55a]496    my $conn = shift;
[ba2ca66]497    $conn->conn->motd;
[48f7d12]498    return;
[56e72d5]499}
500
[f094fc4]501sub cmd_list {
502    my $cmd = shift;
503    my $message = BarnOwl::Style::boldify('Current IRC networks:') . "\n";
504    while (my ($alias, $conn) = each %ircnets) {
505        $message .= '  ' . $alias . ' => ' . $conn->nick . '@' . $conn->server . "\n";
506    }
507    BarnOwl::popless_ztext($message);
[48f7d12]508    return;
[f094fc4]509}
510
511sub cmd_who {
512    my $cmd = shift;
[330c55a]513    my $conn = shift;
[f094fc4]514    my $who = shift || die("Usage: $cmd <user>\n");
[330c55a]515    BarnOwl::error("WHO $cmd $conn $who");
[f094fc4]516    $conn->conn->who($who);
[48f7d12]517    return;
[f094fc4]518}
519
520sub cmd_stats {
521    my $cmd = shift;
[330c55a]522    my $conn = shift;
[f094fc4]523    my $type = shift || die("Usage: $cmd <chiklmouy> [server] \n");
524    $conn->conn->stats($type, @_);
[48f7d12]525    return;
[f094fc4]526}
527
[3ad15ff]528sub cmd_topic {
529    my $cmd = shift;
[330c55a]530    my $conn = shift;
531    my $chan = shift;
532    $conn->conn->topic($chan, @_ ? join(" ", @_) : undef);
[48f7d12]533    return;
[3ad15ff]534}
535
[af9de56]536sub cmd_quote {
537    my $cmd = shift;
538    my $conn = shift;
539    $conn->conn->sl(join(" ", @_));
[48f7d12]540    return;
[af9de56]541}
542
[b38b0b2]543################################################################################
544########################### Utilities/Helpers ##################################
545################################################################################
546
[330c55a]547sub mk_irc_command {
548    my $sub = shift;
549    my $use_channel = shift || 0;
550    return sub {
551        my $cmd = shift;
552        my $conn;
553        my $alias;
554        my $channel;
555        my $getopt = Getopt::Long::Parser->new;
556        my $m = BarnOwl::getcurmsg();
[b38b0b2]557
[330c55a]558        local @ARGV = @_;
559        $getopt->configure(qw(pass_through permute no_getopt_compat prefix_pattern=-|--));
560        $getopt->getoptions("alias=s" => \$alias);
561
562        if(defined($alias)) {
563            $conn = get_connection_by_alias($alias);
564        }
[ecee82f]565        if($use_channel) {
[e625b5e]566            $channel = $ARGV[0];
[330c55a]567            if(defined($channel) && $channel =~ /^#/) {
568                if($channels{$channel} && @{$channels{$channel}} == 1) {
[e625b5e]569                    shift @ARGV;
[ecee82f]570                    $conn = $channels{$channel}[0] unless $conn;
[330c55a]571                }
[ecee82f]572            } elsif ($m && $m->type eq 'IRC' && !$m->is_private) {
573                $channel = $m->channel;
574            } else {
575                undef $channel;
[330c55a]576            }
577        }
[ecee82f]578
[330c55a]579        if(!$channel && $use_channel == REQUIRE_CHANNEL) {
580            die("Usage: $cmd <channel>\n");
581        }
582        if(!$conn) {
583            if($m && $m->type eq 'IRC') {
584                $conn = get_connection_by_alias($m->network);
585            }
586        }
587        if(!$conn && scalar keys %ircnets == 1) {
588            $conn = [values(%ircnets)]->[0];
589        }
590        if(!$conn) {
591            die("You must specify an IRC network using -a.\n");
592        }
593        if($use_channel) {
594            $sub->($cmd, $conn, $channel, @ARGV);
595        } else {
596            $sub->($cmd, $conn, @ARGV);
597        }
598    };
[6858d2d]599}
600
[b38b0b2]601sub get_connection_by_alias {
[2c40dc0]602    my $key = shift;
603    die("No such ircnet: $key\n") unless exists $ircnets{$key};
[b38b0b2]604    return $ircnets{$key};
605}
606
6071;
Note: See TracBrowser for help on using the repository browser.