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

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 330c55a was 330c55a, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
Refactor IRC argument processing. The primary concrete improvement here is that '-a [connection]' no longer needs to come at the start of the command line.
  • Property mode set to 100644
File size: 9.5 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
76    # * nosuchchannel
77    # *
78
79    return $self;
80}
81
82sub getSocket
83{
84    my $self = shift;
85    return $self->conn->socket;
86}
87
88################################################################################
89############################### IRC callbacks ##################################
90################################################################################
91
92sub new_message {
93    my $self = shift;
94    my $evt = shift;
95    return BarnOwl::Message->new(
96        type        => 'IRC',
97        server      => $self->server,
98        network     => $self->alias,
99        sender      => $evt->nick,
100        hostname    => $evt->host,
101        from        => $evt->from,
102        @_
103       );
104}
105
106sub on_msg {
107    my ($self, $evt) = @_;
108    my ($recipient) = $evt->to;
109    my $body = strip_irc_formatting([$evt->args]->[0]);
110    my $nick = $self->nick;
111    BarnOwl::beep() if $body =~ /\b\Q$nick\E\b/;
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    => 'irc-msg -a ' . $self->alias . ' ' .
122            (is_private($recipient) ? $evt->nick : $recipient),
123        replysendercmd => '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 => 'irc-msg -a ' . $self->alias . ' ' . join(' ', $evt->to),
173        replysendercmd => '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 => 'irc-msg -a ' . $self->alias . ' ' . join(' ', $evt->to),
185        replysendercmd => '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 => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick,
198        replysendercmd => '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    BarnOwl::remove_dispatch($self->{FD});
207    BarnOwl::admin_message('IRC',
208                           "[" . $self->alias . "] Disconnected from server");
209}
210
211sub on_nickinuse {
212    my ($self, $evt) = @_;
213    BarnOwl::admin_message("IRC",
214                           "[" . $self->alias . "] " .
215                           [$evt->args]->[1] . ": Nick already in use");
216    unless($self->connected) {
217        $self->conn->disconnect;
218    }
219}
220
221sub on_topic {
222    my ($self, $evt) = @_;
223    my @args = $evt->args;
224    if (scalar @args > 1) {
225        BarnOwl::admin_message("IRC",
226                "Topic for $args[1] on " . $self->alias . " is $args[2]");
227    } else {
228        BarnOwl::admin_message("IRC",
229                "Topic changed to $args[0]");
230    }
231}
232
233sub on_topicinfo {
234    my ($self, $evt) = @_;
235    my @args = $evt->args;
236    BarnOwl::admin_message("IRC",
237        "Topic for $args[1] set by $args[2] at " . localtime($args[3]));
238}
239
240# IRC gives us a bunch of namreply messages, followed by an endofnames.
241# We need to collect them from the namreply and wait for the endofnames message.
242# After this happens, the names_tmp variable is cleared.
243
244sub on_namreply {
245    my ($self, $evt) = @_;
246    return unless $self->names_tmp;
247    $self->names_tmp([@{$self->names_tmp}, split(' ', [$evt->args]->[3])]);
248}
249
250sub on_endofnames {
251    my ($self, $evt) = @_;
252    return unless $self->names_tmp;
253    my $names = BarnOwl::Style::boldify("Members of " . [$evt->args]->[1] . ":\n");
254    for my $name (@{$self->names_tmp}) {
255        $names .= "  $name\n";
256    }
257    BarnOwl::popless_ztext($names);
258    $self->names_tmp(0);
259}
260
261sub on_whois {
262    my ($self, $evt) = @_;
263    $self->whois_tmp(
264      $self->whois_tmp . "\n" . $evt->type . ":\n  " .
265      join("\n  ", cdr(cdr($evt->args))) . "\n"
266    );
267}
268
269sub on_endofwhois {
270    my ($self, $evt) = @_;
271    BarnOwl::popless_ztext(
272        BarnOwl::Style::boldify("/whois for " . [$evt->args]->[1] . ":\n") .
273        $self->whois_tmp
274    );
275    $self->whois_tmp([]);
276}
277
278sub on_event {
279    my ($self, $evt) = @_;
280    return on_whois(@_) if ($evt->type =~ /^whois/);
281    BarnOwl::admin_message("IRC",
282            "[" . $self->alias . "] Unhandled IRC event of type " . $evt->type . ":\n"
283            . strip_irc_formatting(join("\n", $evt->args)))
284        if BarnOwl::getvar('irc:spew') eq 'on';
285}
286
287################################################################################
288########################### Utilities/Helpers ##################################
289################################################################################
290
291sub strip_irc_formatting {
292    my $body = shift;
293    # Strip mIRC colors. If someone wants to write code to convert
294    # these to zephyr colors, be my guest.
295    $body =~ s/\cC\d+(?:,\d+)?//g;
296    $body =~ s/\cO//g;
297   
298    my @pieces = split /\cB/, $body;
299    my $out = '';
300    while(@pieces) {
301        $out .= shift @pieces;
302        $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces;
303    }
304    return $out;
305}
306
307# Determines if the given message recipient is a username, as opposed to
308# a channel that starts with # or &.
309sub is_private {
310    return shift !~ /^[\#\&]/;
311}
312
313sub cdr {
314    shift;
315    return @_;
316}
317
3181;
Note: See TracBrowser for help on using the repository browser.