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

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 4df2568 was 4df2568, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
IRC: Show admin messages for mode changes.
  • Property mode set to 100644
File size: 9.9 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    BarnOwl::beep() if $body =~ /\b\Q$nick\E\b/;
113    $body = '* '.$evt->nick.' '.$body if $evt->type eq 'caction';
114    my $msg = $self->new_message($evt,
115        direction   => 'in',
116        recipient   => $recipient,
117        body => $body,
118        $evt->type eq 'notice' ?
119          (notice     => 'true') : (),
120        is_private($recipient) ?
121          (isprivate  => 'true') : (channel => $recipient),
122        replycmd    => 'irc-msg -a ' . $self->alias . ' ' .
123            (is_private($recipient) ? $evt->nick : $recipient),
124        replysendercmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick
125       );
126
127    BarnOwl::queue_message($msg);
128}
129
130sub on_ping {
131    my ($self, $evt) = @_;
132    $self->conn->ctcp_reply($evt->nick, join (' ', ($evt->args)));
133}
134
135sub on_admin_msg {
136    my ($self, $evt) = @_;
137    BarnOwl::admin_message("IRC",
138            BarnOwl::Style::boldify('IRC ' . $evt->type . ' message from '
139                . $self->alias) . "\n"
140            . strip_irc_formatting(join ' ', cdr($evt->args)));
141}
142
143sub on_motdstart {
144    my ($self, $evt) = @_;
145    $self->motd(join "\n", cdr($evt->args));
146}
147
148sub on_motd {
149    my ($self, $evt) = @_;
150    $self->motd(join "\n", $self->motd, cdr($evt->args));
151}
152
153sub on_endofmotd {
154    my ($self, $evt) = @_;
155    $self->motd(join "\n", $self->motd, cdr($evt->args));
156    if(!$self->connected) {
157        BarnOwl::admin_message("IRC", "Connected to " .
158                               $self->server . " (" . $self->alias . ")");
159        $self->connected(1);
160       
161    }
162    BarnOwl::admin_message("IRC",
163            BarnOwl::Style::boldify('MOTD for ' . $self->alias) . "\n"
164            . strip_irc_formatting($self->motd));
165}
166
167sub on_join {
168    my ($self, $evt) = @_;
169    my $msg = $self->new_message($evt,
170        loginout   => 'login',
171        action     => 'join',
172        channel    => $evt->to,
173        replycmd => 'irc-msg -a ' . $self->alias . ' ' . join(' ', $evt->to),
174        replysendercmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick
175        );
176    BarnOwl::queue_message($msg);
177}
178
179sub on_part {
180    my ($self, $evt) = @_;
181    my $msg = $self->new_message($evt,
182        loginout   => 'logout',
183        action     => 'part',
184        channel    => $evt->to,
185        replycmd => 'irc-msg -a ' . $self->alias . ' ' . join(' ', $evt->to),
186        replysendercmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick
187        );
188    BarnOwl::queue_message($msg);
189}
190
191sub on_quit {
192    my ($self, $evt) = @_;
193    my $msg = $self->new_message($evt,
194        loginout   => 'logout',
195        action     => 'quit',
196        from       => $evt->to,
197        reason     => [$evt->args]->[0],
198        replycmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick,
199        replysendercmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick
200        );
201    BarnOwl::queue_message($msg);
202}
203
204sub on_disconnect {
205    my $self = shift;
206    delete $BarnOwl::Module::IRC::ircnets{$self->alias};
207    BarnOwl::remove_dispatch($self->{FD});
208    BarnOwl::admin_message('IRC',
209                           "[" . $self->alias . "] Disconnected from server");
210}
211
212sub on_nickinuse {
213    my ($self, $evt) = @_;
214    BarnOwl::admin_message("IRC",
215                           "[" . $self->alias . "] " .
216                           [$evt->args]->[1] . ": Nick already in use");
217    unless($self->connected) {
218        $self->conn->disconnect;
219    }
220}
221
222sub on_topic {
223    my ($self, $evt) = @_;
224    my @args = $evt->args;
225    if (scalar @args > 1) {
226        BarnOwl::admin_message("IRC",
227                "Topic for $args[1] on " . $self->alias . " is $args[2]");
228    } else {
229        BarnOwl::admin_message("IRC",
230                "Topic changed to $args[0]");
231    }
232}
233
234sub on_topicinfo {
235    my ($self, $evt) = @_;
236    my @args = $evt->args;
237    BarnOwl::admin_message("IRC",
238        "Topic for $args[1] set by $args[2] at " . localtime($args[3]));
239}
240
241# IRC gives us a bunch of namreply messages, followed by an endofnames.
242# We need to collect them from the namreply and wait for the endofnames message.
243# After this happens, the names_tmp variable is cleared.
244
245sub on_namreply {
246    my ($self, $evt) = @_;
247    return unless $self->names_tmp;
248    $self->names_tmp([@{$self->names_tmp}, split(' ', [$evt->args]->[3])]);
249}
250
251sub on_endofnames {
252    my ($self, $evt) = @_;
253    return unless $self->names_tmp;
254    my $names = BarnOwl::Style::boldify("Members of " . [$evt->args]->[1] . ":\n");
255    for my $name (@{$self->names_tmp}) {
256        $names .= "  $name\n";
257    }
258    BarnOwl::popless_ztext($names);
259    $self->names_tmp(0);
260}
261
262sub on_whois {
263    my ($self, $evt) = @_;
264    $self->whois_tmp(
265      $self->whois_tmp . "\n" . $evt->type . ":\n  " .
266      join("\n  ", cdr(cdr($evt->args))) . "\n"
267    );
268}
269
270sub on_endofwhois {
271    my ($self, $evt) = @_;
272    BarnOwl::popless_ztext(
273        BarnOwl::Style::boldify("/whois for " . [$evt->args]->[1] . ":\n") .
274        $self->whois_tmp
275    );
276    $self->whois_tmp([]);
277}
278
279sub on_mode {
280    my ($self, $evt) = @_;
281    BarnOwl::admin_message("IRC",
282                           "[" . $self->alias . "] User " . ($evt->nick) . + " set mode " .
283                           join(" ", $evt->args) . "on " . $evt->to->[0]
284                          );
285}
286
287sub on_event {
288    my ($self, $evt) = @_;
289    return on_whois(@_) if ($evt->type =~ /^whois/);
290    BarnOwl::admin_message("IRC",
291            "[" . $self->alias . "] Unhandled IRC event of type " . $evt->type . ":\n"
292            . strip_irc_formatting(join("\n", $evt->args)))
293        if BarnOwl::getvar('irc:spew') eq 'on';
294}
295
296################################################################################
297########################### Utilities/Helpers ##################################
298################################################################################
299
300sub strip_irc_formatting {
301    my $body = shift;
302    # Strip mIRC colors. If someone wants to write code to convert
303    # these to zephyr colors, be my guest.
304    $body =~ s/\cC\d+(?:,\d+)?//g;
305    $body =~ s/\cO//g;
306   
307    my @pieces = split /\cB/, $body;
308    my $out = '';
309    while(@pieces) {
310        $out .= shift @pieces;
311        $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces;
312    }
313    return $out;
314}
315
316# Determines if the given message recipient is a username, as opposed to
317# a channel that starts with # or &.
318sub is_private {
319    return shift !~ /^[\#\&]/;
320}
321
322sub cdr {
323    shift;
324    return @_;
325}
326
3271;
Note: See TracBrowser for help on using the repository browser.