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

release-1.10release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since f1a2736 was f1a2736, checked in by Alejandro R. Sedeño <asedeno@mit.edu>, 14 years ago
Switch perl modules to the new I/O Dispatch API. Signed-off-by: Alejandro R. Sedeño <asedeno@mit.edu>
  • Property mode set to 100644
File size: 10.5 KB
RevLine 
[b38b0b2]1use strict;
2use warnings;
3
4package BarnOwl::Module::IRC::Connection;
5
6=head1 NAME
7
8BarnOwl::Module::IRC::Connection
9
10=head1 DESCRIPTION
11
[ba2ca66]12This module is a wrapper around Net::IRC::Connection for BarnOwl's IRC
[b38b0b2]13support
14
15=cut
16
[ba2ca66]17use Net::IRC::Connection;
18
19use base qw(Class::Accessor Exporter);
[38cfdb5d]20__PACKAGE__->mk_accessors(qw(conn alias channels connected motd names_tmp whois_tmp));
[2c40dc0]21our @EXPORT_OK = qw(&is_private);
[b38b0b2]22
23use BarnOwl;
24
[ba2ca66]25BEGIN {
26    no strict 'refs';
27    my @delegate = qw(nick server);
28    for my $meth (@delegate) {
29        *{"BarnOwl::Module::IRC::Connection::$meth"} = sub {
30            shift->conn->$meth(@_);
31        }
32    }
33};
34
[b38b0b2]35sub new {
36    my $class = shift;
37    my $irc = shift;
38    my $alias = shift;
39    my %args = (@_);
[ba2ca66]40    my $conn = Net::IRC::Connection->new($irc, %args);
41    my $self = bless({}, $class);
42    $self->conn($conn);
[b38b0b2]43    $self->alias($alias);
44    $self->channels([]);
[ba2ca66]45    $self->motd("");
46    $self->connected(0);
[d264c6d]47    $self->names_tmp(0);
[38cfdb5d]48    $self->whois_tmp("");
[ba2ca66]49
[5ff830a]50    $self->conn->add_handler(376 => sub { shift; $self->on_connect(@_) });
[ba2ca66]51    $self->conn->add_default_handler(sub { shift; $self->on_event(@_) });
52    $self->conn->add_handler(['msg', 'notice', 'public', 'caction'],
53            sub { shift; $self->on_msg(@_) });
54    $self->conn->add_handler(['welcome', 'yourhost', 'created',
[661d2eb]55                              'luserclient', 'luserop', 'luserchannels', 'luserme',
56                              'error'],
[ba2ca66]57            sub { shift; $self->on_admin_msg(@_) });
58    $self->conn->add_handler(['myinfo', 'map', 'n_local', 'n_global',
[bc0d7bc]59            'luserconns'],
60            sub { });
[ba2ca66]61    $self->conn->add_handler(motdstart => sub { shift; $self->on_motdstart(@_) });
62    $self->conn->add_handler(motd      => sub { shift; $self->on_motd(@_) });
63    $self->conn->add_handler(endofmotd => sub { shift; $self->on_endofmotd(@_) });
64    $self->conn->add_handler(join      => sub { shift; $self->on_join(@_) });
65    $self->conn->add_handler(part      => sub { shift; $self->on_part(@_) });
[4789b17]66    $self->conn->add_handler(quit      => sub { shift; $self->on_quit(@_) });
[ba2ca66]67    $self->conn->add_handler(disconnect => sub { shift; $self->on_disconnect(@_) });
68    $self->conn->add_handler(nicknameinuse => sub { shift; $self->on_nickinuse(@_) });
69    $self->conn->add_handler(cping     => sub { shift; $self->on_ping(@_) });
[3ad15ff]70    $self->conn->add_handler(topic     => sub { shift; $self->on_topic(@_) });
71    $self->conn->add_handler(topicinfo => sub { shift; $self->on_topicinfo(@_) });
[38cfdb5d]72    $self->conn->add_handler(namreply  => sub { shift; $self->on_namreply(@_) });
73    $self->conn->add_handler(endofnames=> sub { shift; $self->on_endofnames(@_) });
[d264c6d]74    $self->conn->add_handler(endofwhois=> sub { shift; $self->on_endofwhois(@_) });
[4df2568]75    $self->conn->add_handler(mode      => sub { shift; $self->on_mode(@_) });
[7cfb1df]76    $self->conn->add_handler(nosuchchannel => sub { shift; $self->on_nosuchchannel(@_) });
[330c55a]77
[b38b0b2]78    return $self;
79}
80
[9c7a701]81sub getSocket
82{
83    my $self = shift;
84    return $self->conn->socket;
85}
86
[b38b0b2]87################################################################################
88############################### IRC callbacks ##################################
89################################################################################
90
[47b6a5f]91sub new_message {
92    my $self = shift;
93    my $evt = shift;
94    return BarnOwl::Message->new(
95        type        => 'IRC',
96        server      => $self->server,
97        network     => $self->alias,
98        sender      => $evt->nick,
99        hostname    => $evt->host,
100        from        => $evt->from,
101        @_
102       );
103}
104
[b38b0b2]105sub on_msg {
106    my ($self, $evt) = @_;
[2c40dc0]107    my ($recipient) = $evt->to;
108    my $body = strip_irc_formatting([$evt->args]->[0]);
[bc0d7bc]109    my $nick = $self->nick;
[3048f1f]110    $body = '* '.$evt->nick.' '.$body if $evt->type eq 'caction';
[47b6a5f]111    my $msg = $self->new_message($evt,
[b38b0b2]112        direction   => 'in',
[2c40dc0]113        recipient   => $recipient,
[47b6a5f]114        body => $body,
[2c40dc0]115        $evt->type eq 'notice' ?
116          (notice     => 'true') : (),
117        is_private($recipient) ?
[0e52069]118          (isprivate  => 'true') : (channel => $recipient),
[744769e]119        replycmd    => BarnOwl::quote('irc-msg', '-a', $self->alias,
120          (is_private($recipient) ? $evt->nick : $recipient)),
121        replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick),
[b38b0b2]122       );
[47b6a5f]123
[b38b0b2]124    BarnOwl::queue_message($msg);
125}
126
[2c40dc0]127sub on_ping {
128    my ($self, $evt) = @_;
[ba2ca66]129    $self->conn->ctcp_reply($evt->nick, join (' ', ($evt->args)));
[2c40dc0]130}
131
[bc0d7bc]132sub on_admin_msg {
133    my ($self, $evt) = @_;
134    BarnOwl::admin_message("IRC",
135            BarnOwl::Style::boldify('IRC ' . $evt->type . ' message from '
[1951db8]136                . $self->alias) . "\n"
[d264c6d]137            . strip_irc_formatting(join ' ', cdr($evt->args)));
[bc0d7bc]138}
139
140sub on_motdstart {
141    my ($self, $evt) = @_;
[1af21e8]142    $self->motd(join "\n", cdr($evt->args));
[bc0d7bc]143}
144
145sub on_motd {
146    my ($self, $evt) = @_;
[1af21e8]147    $self->motd(join "\n", $self->motd, cdr($evt->args));
[bc0d7bc]148}
149
150sub on_endofmotd {
151    my ($self, $evt) = @_;
[1af21e8]152    $self->motd(join "\n", $self->motd, cdr($evt->args));
[ba2ca66]153    if(!$self->connected) {
[3baf77f]154        BarnOwl::admin_message("IRC", "Connected to " .
155                               $self->server . " (" . $self->alias . ")");
[ba2ca66]156        $self->connected(1);
[3baf77f]157       
158    }
[bc0d7bc]159    BarnOwl::admin_message("IRC",
[3baf77f]160            BarnOwl::Style::boldify('MOTD for ' . $self->alias) . "\n"
[ba2ca66]161            . strip_irc_formatting($self->motd));
[bc0d7bc]162}
163
[47b6a5f]164sub on_join {
165    my ($self, $evt) = @_;
166    my $msg = $self->new_message($evt,
167        loginout   => 'login',
[4789b17]168        action     => 'join',
[47b6a5f]169        channel    => $evt->to,
[744769e]170        replycmd   => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->to),
171        replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick),
[47b6a5f]172        );
173    BarnOwl::queue_message($msg);
174}
175
176sub on_part {
177    my ($self, $evt) = @_;
178    my $msg = $self->new_message($evt,
179        loginout   => 'logout',
[4789b17]180        action     => 'part',
[47b6a5f]181        channel    => $evt->to,
[744769e]182        replycmd   => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->to),
183        replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick),
[47b6a5f]184        );
185    BarnOwl::queue_message($msg);
186}
187
[4789b17]188sub on_quit {
189    my ($self, $evt) = @_;
190    my $msg = $self->new_message($evt,
191        loginout   => 'logout',
192        action     => 'quit',
193        from       => $evt->to,
194        reason     => [$evt->args]->[0],
[744769e]195        replycmd   => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick),
196        replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick),
[4789b17]197        );
198    BarnOwl::queue_message($msg);
199}
200
[9e02bb7]201sub on_disconnect {
202    my $self = shift;
203    delete $BarnOwl::Module::IRC::ircnets{$self->alias};
[0e8a0fc]204    for my $k (keys %BarnOwl::Module::IRC::channels) {
205        my @conns = grep {$_ ne $self} @{$BarnOwl::Module::IRC::channels{$k}};
206        if(@conns) {
207            $BarnOwl::Module::IRC::channels{$k} = \@conns;
208        } else {
209            delete $BarnOwl::Module::IRC::channels{$k};
210        }
211    }
[f1a2736]212    BarnOwl::remove_io_dispatch($self->{FD});
[9e02bb7]213    BarnOwl::admin_message('IRC',
214                           "[" . $self->alias . "] Disconnected from server");
215}
216
217sub on_nickinuse {
218    my ($self, $evt) = @_;
219    BarnOwl::admin_message("IRC",
220                           "[" . $self->alias . "] " .
221                           [$evt->args]->[1] . ": Nick already in use");
[ba2ca66]222    unless($self->connected) {
223        $self->conn->disconnect;
[eab7a4c]224    }
[9e02bb7]225}
226
[3ad15ff]227sub on_topic {
228    my ($self, $evt) = @_;
229    my @args = $evt->args;
230    if (scalar @args > 1) {
231        BarnOwl::admin_message("IRC",
232                "Topic for $args[1] on " . $self->alias . " is $args[2]");
233    } else {
234        BarnOwl::admin_message("IRC",
235                "Topic changed to $args[0]");
236    }
237}
238
239sub on_topicinfo {
240    my ($self, $evt) = @_;
241    my @args = $evt->args;
242    BarnOwl::admin_message("IRC",
243        "Topic for $args[1] set by $args[2] at " . localtime($args[3]));
244}
245
[38cfdb5d]246# IRC gives us a bunch of namreply messages, followed by an endofnames.
247# We need to collect them from the namreply and wait for the endofnames message.
248# After this happens, the names_tmp variable is cleared.
249
250sub on_namreply {
251    my ($self, $evt) = @_;
[d264c6d]252    return unless $self->names_tmp;
[38cfdb5d]253    $self->names_tmp([@{$self->names_tmp}, split(' ', [$evt->args]->[3])]);
254}
255
256sub on_endofnames {
257    my ($self, $evt) = @_;
[d264c6d]258    return unless $self->names_tmp;
[38cfdb5d]259    my $names = BarnOwl::Style::boldify("Members of " . [$evt->args]->[1] . ":\n");
260    for my $name (@{$self->names_tmp}) {
261        $names .= "  $name\n";
262    }
263    BarnOwl::popless_ztext($names);
[d264c6d]264    $self->names_tmp(0);
265}
266
267sub on_whois {
268    my ($self, $evt) = @_;
269    $self->whois_tmp(
270      $self->whois_tmp . "\n" . $evt->type . ":\n  " .
271      join("\n  ", cdr(cdr($evt->args))) . "\n"
272    );
273}
274
275sub on_endofwhois {
276    my ($self, $evt) = @_;
277    BarnOwl::popless_ztext(
278        BarnOwl::Style::boldify("/whois for " . [$evt->args]->[1] . ":\n") .
279        $self->whois_tmp
280    );
[7c83a32]281    $self->whois_tmp('');
[d264c6d]282}
283
[4df2568]284sub on_mode {
285    my ($self, $evt) = @_;
286    BarnOwl::admin_message("IRC",
287                           "[" . $self->alias . "] User " . ($evt->nick) . + " set mode " .
288                           join(" ", $evt->args) . "on " . $evt->to->[0]
289                          );
290}
291
[7cfb1df]292sub on_nosuchchannel {
293    my ($self, $evt) = @_;
294    BarnOwl::admin_message("IRC",
295                           "[" . $self->alias . "] " .
296                           "No such channel: " . [$evt->args]->[1])
297}
298
[d264c6d]299sub on_event {
300    my ($self, $evt) = @_;
301    return on_whois(@_) if ($evt->type =~ /^whois/);
302    BarnOwl::admin_message("IRC",
303            "[" . $self->alias . "] Unhandled IRC event of type " . $evt->type . ":\n"
304            . strip_irc_formatting(join("\n", $evt->args)))
305        if BarnOwl::getvar('irc:spew') eq 'on';
[38cfdb5d]306}
[9e02bb7]307
[b38b0b2]308################################################################################
309########################### Utilities/Helpers ##################################
310################################################################################
311
312sub strip_irc_formatting {
313    my $body = shift;
[214b790]314    # Strip mIRC colors. If someone wants to write code to convert
315    # these to zephyr colors, be my guest.
316    $body =~ s/\cC\d+(?:,\d+)?//g;
317    $body =~ s/\cO//g;
318   
319    my @pieces = split /\cB/, $body;
[3835ae8]320    my $out = '';
[b38b0b2]321    while(@pieces) {
322        $out .= shift @pieces;
323        $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces;
324    }
325    return $out;
326}
327
[2c40dc0]328# Determines if the given message recipient is a username, as opposed to
329# a channel that starts with # or &.
330sub is_private {
331    return shift !~ /^[\#\&]/;
332}
[b38b0b2]333
[bc0d7bc]334sub cdr {
335    shift;
336    return @_;
337}
338
[b38b0b2]3391;
Note: See TracBrowser for help on using the repository browser.