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

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since d264c6d was d264c6d, checked in by Geoffrey Thomas <geofft@mit.edu>, 16 years ago
IRC: Support for displaying :irc-whois replies, and various bugfixes.
  • Property mode set to 100644
File size: 9.0 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(disconnect => sub { shift; $self->on_disconnect(@_) });
67    $self->conn->add_handler(nicknameinuse => sub { shift; $self->on_nickinuse(@_) });
68    $self->conn->add_handler(cping     => sub { shift; $self->on_ping(@_) });
69    $self->conn->add_handler(topic     => sub { shift; $self->on_topic(@_) });
70    $self->conn->add_handler(topicinfo => sub { shift; $self->on_topicinfo(@_) });
71    $self->conn->add_handler(namreply  => sub { shift; $self->on_namreply(@_) });
72    $self->conn->add_handler(endofnames=> sub { shift; $self->on_endofnames(@_) });
73    $self->conn->add_handler(endofwhois=> sub { shift; $self->on_endofwhois(@_) });
74
75    return $self;
76}
77
78sub getSocket
79{
80    my $self = shift;
81    return $self->conn->socket;
82}
83
84################################################################################
85############################### IRC callbacks ##################################
86################################################################################
87
88sub new_message {
89    my $self = shift;
90    my $evt = shift;
91    return BarnOwl::Message->new(
92        type        => 'IRC',
93        server      => $self->server,
94        network     => $self->alias,
95        sender      => $evt->nick,
96        hostname    => $evt->host,
97        from        => $evt->from,
98        @_
99       );
100}
101
102sub on_msg {
103    my ($self, $evt) = @_;
104    my ($recipient) = $evt->to;
105    my $body = strip_irc_formatting([$evt->args]->[0]);
106    my $nick = $self->nick;
107    BarnOwl::beep() if $body =~ /\b\Q$nick\E\b/;
108    $body = BarnOwl::Style::boldify($evt->nick.' '.$body) if $evt->type eq 'caction';
109    my $msg = $self->new_message($evt,
110        direction   => 'in',
111        recipient   => $recipient,
112        body => $body,
113        $evt->type eq 'notice' ?
114          (notice     => 'true') : (),
115        is_private($recipient) ?
116          (isprivate  => 'true') : (channel => $recipient),
117        replycmd    => 'irc-msg -a ' . $self->alias . ' ' .
118            (is_private($recipient) ? $evt->nick : $recipient),
119        replysendercmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick
120       );
121
122    BarnOwl::queue_message($msg);
123}
124
125sub on_ping {
126    my ($self, $evt) = @_;
127    $self->conn->ctcp_reply($evt->nick, join (' ', ($evt->args)));
128}
129
130sub on_admin_msg {
131    my ($self, $evt) = @_;
132    BarnOwl::admin_message("IRC",
133            BarnOwl::Style::boldify('IRC ' . $evt->type . ' message from '
134                . $self->alias) . "\n"
135            . strip_irc_formatting(join ' ', cdr($evt->args)));
136}
137
138sub on_motdstart {
139    my ($self, $evt) = @_;
140    $self->motd(join "\n", cdr($evt->args));
141}
142
143sub on_motd {
144    my ($self, $evt) = @_;
145    $self->motd(join "\n", $self->motd, cdr($evt->args));
146}
147
148sub on_endofmotd {
149    my ($self, $evt) = @_;
150    $self->motd(join "\n", $self->motd, cdr($evt->args));
151    if(!$self->connected) {
152        BarnOwl::admin_message("IRC", "Connected to " .
153                               $self->server . " (" . $self->alias . ")");
154        $self->connected(1);
155       
156    }
157    BarnOwl::admin_message("IRC",
158            BarnOwl::Style::boldify('MOTD for ' . $self->alias) . "\n"
159            . strip_irc_formatting($self->motd));
160}
161
162sub on_join {
163    my ($self, $evt) = @_;
164    my $msg = $self->new_message($evt,
165        loginout   => 'login',
166        channel    => $evt->to,
167        replycmd => 'irc-msg -a ' . $self->alias . ' ' . join(' ', $evt->to),
168        replysendercmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick
169        );
170    BarnOwl::queue_message($msg);
171}
172
173sub on_part {
174    my ($self, $evt) = @_;
175    my $msg = $self->new_message($evt,
176        loginout   => 'logout',
177        channel    => $evt->to,
178        replycmd => 'irc-msg -a ' . $self->alias . ' ' . join(' ', $evt->to),
179        replysendercmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick
180        );
181    BarnOwl::queue_message($msg);
182}
183
184sub on_disconnect {
185    my $self = shift;
186    delete $BarnOwl::Module::IRC::ircnets{$self->alias};
187    BarnOwl::remove_dispatch($self->{FD});
188    BarnOwl::admin_message('IRC',
189                           "[" . $self->alias . "] Disconnected from server");
190}
191
192sub on_nickinuse {
193    my ($self, $evt) = @_;
194    BarnOwl::admin_message("IRC",
195                           "[" . $self->alias . "] " .
196                           [$evt->args]->[1] . ": Nick already in use");
197    unless($self->connected) {
198        $self->conn->disconnect;
199    }
200}
201
202sub on_topic {
203    my ($self, $evt) = @_;
204    my @args = $evt->args;
205    if (scalar @args > 1) {
206        BarnOwl::admin_message("IRC",
207                "Topic for $args[1] on " . $self->alias . " is $args[2]");
208    } else {
209        BarnOwl::admin_message("IRC",
210                "Topic changed to $args[0]");
211    }
212}
213
214sub on_topicinfo {
215    my ($self, $evt) = @_;
216    my @args = $evt->args;
217    BarnOwl::admin_message("IRC",
218        "Topic for $args[1] set by $args[2] at " . localtime($args[3]));
219}
220
221# IRC gives us a bunch of namreply messages, followed by an endofnames.
222# We need to collect them from the namreply and wait for the endofnames message.
223# After this happens, the names_tmp variable is cleared.
224
225sub on_namreply {
226    my ($self, $evt) = @_;
227    return unless $self->names_tmp;
228    $self->names_tmp([@{$self->names_tmp}, split(' ', [$evt->args]->[3])]);
229}
230
231sub on_endofnames {
232    my ($self, $evt) = @_;
233    return unless $self->names_tmp;
234    my $names = BarnOwl::Style::boldify("Members of " . [$evt->args]->[1] . ":\n");
235    for my $name (@{$self->names_tmp}) {
236        $names .= "  $name\n";
237    }
238    BarnOwl::popless_ztext($names);
239    $self->names_tmp(0);
240}
241
242sub on_whois {
243    my ($self, $evt) = @_;
244    $self->whois_tmp(
245      $self->whois_tmp . "\n" . $evt->type . ":\n  " .
246      join("\n  ", cdr(cdr($evt->args))) . "\n"
247    );
248}
249
250sub on_endofwhois {
251    my ($self, $evt) = @_;
252    BarnOwl::popless_ztext(
253        BarnOwl::Style::boldify("/whois for " . [$evt->args]->[1] . ":\n") .
254        $self->whois_tmp
255    );
256    $self->whois_tmp([]);
257}
258
259sub on_event {
260    my ($self, $evt) = @_;
261    return on_whois(@_) if ($evt->type =~ /^whois/);
262    BarnOwl::admin_message("IRC",
263            "[" . $self->alias . "] Unhandled IRC event of type " . $evt->type . ":\n"
264            . strip_irc_formatting(join("\n", $evt->args)))
265        if BarnOwl::getvar('irc:spew') eq 'on';
266}
267
268################################################################################
269########################### Utilities/Helpers ##################################
270################################################################################
271
272sub strip_irc_formatting {
273    my $body = shift;
274    # Strip mIRC colors. If someone wants to write code to convert
275    # these to zephyr colors, be my guest.
276    $body =~ s/\cC\d+(?:,\d+)?//g;
277    $body =~ s/\cO//g;
278   
279    my @pieces = split /\cB/, $body;
280    my $out = '';
281    while(@pieces) {
282        $out .= shift @pieces;
283        $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces;
284    }
285    return $out;
286}
287
288# Determines if the given message recipient is a username, as opposed to
289# a channel that starts with # or &.
290sub is_private {
291    return shift !~ /^[\#\&]/;
292}
293
294sub cdr {
295    shift;
296    return @_;
297}
298
2991;
Note: See TracBrowser for help on using the repository browser.