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
Line 
1use strict;
2use warnings;
3
4package BarnOwl::Module::IRC;
5
6=head1 NAME
7
8BarnOwl::Module::IRC
9
10=head1 DESCRIPTION
11
12This module implements IRC support for barnowl.
13
14=cut
15
16use BarnOwl;
17use BarnOwl::Hooks;
18use BarnOwl::Message::IRC;
19use BarnOwl::Module::IRC::Connection qw(is_private);
20use BarnOwl::Module::IRC::Completion;
21
22use Net::IRC;
23use Getopt::Long;
24
25our $VERSION = 0.02;
26
27our $irc;
28
29# Hash alias -> BarnOwl::Module::IRC::Connection object
30our %ircnets;
31our %channels;
32our %reconnect;
33
34sub startup {
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       });
54
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 ' .
59        'admin messages. Intended for debugging and development use only.'
60       });
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
70    register_commands();
71    register_handlers();
72    BarnOwl::filter(qw{irc type ^IRC$ or ( type ^admin$ and adminheader ^IRC$ )});
73}
74
75sub shutdown {
76    for my $conn (values %ircnets) {
77        $conn->conn->disconnect();
78    }
79}
80
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
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
108#sub mainloop_hook {
109#    return unless defined $irc;
110#    eval {
111#        $irc->do_one_loop();
112#    };
113#    return;
114#}
115
116sub OwlProcess {
117    return unless defined $irc;
118    eval {
119        $irc->do_one_loop();
120    };
121    return;
122}
123
124
125sub register_handlers {
126    if(!$irc) {
127        $irc = Net::IRC->new;
128        $irc->timeout(0);
129    }
130}
131
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
139use constant OPTIONAL_CHANNEL => 1;
140use constant REQUIRE_CHANNEL => 2;
141
142sub register_commands {
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]',
149            description => <<END_DESCR
150Connect to an IRC server. Supported options are:
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(
182        'irc-msg' => mk_irc_command( \&cmd_msg ),
183        {
184            summary => 'Send an IRC message',
185            usage   => 'irc-msg [-a ALIAS] DESTINATION MESSAGE',
186
187            description => <<END_DESCR
188Send an IRC message.
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',
209            usage   => 'irc-join [-a ALIAS] #channel [KEY]',
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    );
324}
325
326
327$BarnOwl::Hooks::startup->add('BarnOwl::Module::IRC::startup');
328$BarnOwl::Hooks::shutdown->add('BarnOwl::Module::IRC::shutdown');
329$BarnOwl::Hooks::getQuickstart->add('BarnOwl::Module::IRC::quickstart');
330$BarnOwl::Hooks::getBuddyList->add("BarnOwl::Module::IRC::buddylist");
331
332################################################################################
333######################## Owl command handlers ##################################
334################################################################################
335
336sub cmd_connect {
337    my $cmd = shift;
338
339    my $nick = BarnOwl::getvar('irc:nick');
340    my $username = BarnOwl::getvar('irc:user');
341    my $ircname = BarnOwl::getvar('irc:name');
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,
353            "password=s" => \$password,
354            "nick=s"     => \$nick,
355        );
356        $host = shift @ARGV or die("Usage: $cmd HOST\n");
357        if(!$alias) {
358            if($host =~ /^(?:irc[.])?([\w-]+)[.]\w+$/) {
359                $alias = $1;
360            } else {
361                $alias = $host;
362            }
363        }
364        $ssl ||= 0;
365        $port = shift @ARGV || ($ssl ? 6697 : 6667);
366    }
367
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
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
383    if ($conn->conn->connected) {
384        $conn->connected("Connected to $alias as $nick");
385    } else {
386        die("IRC::Connection->connect failed: $!");
387    }
388
389    return;
390}
391
392sub cmd_disconnect {
393    my $cmd = shift;
394    my $conn = shift;
395    $conn->conn->disconnect;
396    return;
397}
398
399sub cmd_msg {
400    my $cmd  = shift;
401    my $conn = shift;
402    my $to = shift or die("Usage: $cmd [NICK|CHANNEL]\n");
403    # handle multiple recipients?
404    if(@_) {
405        process_msg($conn, $to, join(" ", @_));
406    } else {
407        BarnOwl::start_edit_win(BarnOwl::quote('/msg', '-a', $conn->alias, $to), sub {process_msg($conn, $to, @_)});
408    }
409    return;
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/  /;
418    if ($body =~ /^\/me (.*)/) {
419        $conn->conn->me($to, Encode::encode('utf-8', $1));
420        $body = '* '.$conn->nick.' '.$1;
421    } else {
422        $conn->conn->privmsg($to, Encode::encode('utf-8', $body));
423    }
424    my $msg = BarnOwl::Message->new(
425        type        => 'IRC',
426        direction   => is_private($to) ? 'out' : 'in',
427        server      => $conn->server,
428        network     => $conn->alias,
429        recipient   => $to,
430        body        => $body,
431        sender      => $conn->nick,
432        is_private($to) ?
433          (isprivate  => 'true') : (channel => $to),
434        replycmd    => BarnOwl::quote('irc-msg',  '-a', $conn->alias, $to),
435        replysendercmd => BarnOwl::quote('irc-msg', '-a', $conn->alias, $to),
436       );
437    BarnOwl::queue_message($msg);
438    return;
439}
440
441sub cmd_mode {
442    my $cmd = shift;
443    my $conn = shift;
444    my $target = shift;
445    $target ||= shift;
446    $conn->conn->mode($target, @_);
447    return;
448}
449
450sub cmd_join {
451    my $cmd = shift;
452    my $conn = shift;
453    my $chan = shift or die("Usage: $cmd channel\n");
454    $channels{$chan} ||= [];
455    push @{$channels{$chan}}, $conn;
456    $conn->conn->join($chan, @_);
457    return;
458}
459
460sub cmd_part {
461    my $cmd = shift;
462    my $conn = shift;
463    my $chan = shift;
464    $channels{$chan} = [grep {$_ ne $conn} @{$channels{$chan} || []}];
465    $conn->conn->part($chan);
466    return;
467}
468
469sub cmd_nick {
470    my $cmd = shift;
471    my $conn = shift;
472    my $nick = shift or die("Usage: $cmd <new nick>\n");
473    $conn->conn->nick($nick);
474    return;
475}
476
477sub cmd_names {
478    my $cmd = shift;
479    my $conn = shift;
480    my $chan = shift;
481    $conn->names_tmp([]);
482    $conn->conn->names($chan);
483    return;
484}
485
486sub cmd_whois {
487    my $cmd = shift;
488    my $conn = shift;
489    my $who = shift || die("Usage: $cmd <user>\n");
490    $conn->conn->whois($who);
491    return;
492}
493
494sub cmd_motd {
495    my $cmd = shift;
496    my $conn = shift;
497    $conn->conn->motd;
498    return;
499}
500
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);
508    return;
509}
510
511sub cmd_who {
512    my $cmd = shift;
513    my $conn = shift;
514    my $who = shift || die("Usage: $cmd <user>\n");
515    BarnOwl::error("WHO $cmd $conn $who");
516    $conn->conn->who($who);
517    return;
518}
519
520sub cmd_stats {
521    my $cmd = shift;
522    my $conn = shift;
523    my $type = shift || die("Usage: $cmd <chiklmouy> [server] \n");
524    $conn->conn->stats($type, @_);
525    return;
526}
527
528sub cmd_topic {
529    my $cmd = shift;
530    my $conn = shift;
531    my $chan = shift;
532    $conn->conn->topic($chan, @_ ? join(" ", @_) : undef);
533    return;
534}
535
536sub cmd_quote {
537    my $cmd = shift;
538    my $conn = shift;
539    $conn->conn->sl(join(" ", @_));
540    return;
541}
542
543################################################################################
544########################### Utilities/Helpers ##################################
545################################################################################
546
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();
557
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        }
565        if($use_channel) {
566            $channel = $ARGV[0];
567            if(defined($channel) && $channel =~ /^#/) {
568                if($channels{$channel} && @{$channels{$channel}} == 1) {
569                    shift @ARGV;
570                    $conn = $channels{$channel}[0] unless $conn;
571                }
572            } elsif ($m && $m->type eq 'IRC' && !$m->is_private) {
573                $channel = $m->channel;
574            } else {
575                undef $channel;
576            }
577        }
578
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    };
599}
600
601sub get_connection_by_alias {
602    my $key = shift;
603    die("No such ircnet: $key\n") unless exists $ircnets{$key};
604    return $ircnets{$key};
605}
606
6071;
Note: See TracBrowser for help on using the repository browser.