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

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