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

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since ba2ca66 was ba2ca66, checked in by Nelson Elhage <nelhage@mit.edu>, 16 years ago
Rewrite ::Connection to not subclass Net::IRC::Connection to avoid stupid namespace conflicts
  • Property mode set to 100644
File size: 6.3 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));
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
48    $self->conn->add_default_handler(sub { shift; $self->on_event(@_) });
49    $self->conn->add_handler(['msg', 'notice', 'public', 'caction'],
50            sub { shift; $self->on_msg(@_) });
51    $self->conn->add_handler(['welcome', 'yourhost', 'created',
52            'luserclient', 'luserop', 'luserchannels', 'luserme'],
53            sub { shift; $self->on_admin_msg(@_) });
54    $self->conn->add_handler(['myinfo', 'map', 'n_local', 'n_global',
55            'luserconns'],
56            sub { });
57    $self->conn->add_handler(motdstart => sub { shift; $self->on_motdstart(@_) });
58    $self->conn->add_handler(motd      => sub { shift; $self->on_motd(@_) });
59    $self->conn->add_handler(endofmotd => sub { shift; $self->on_endofmotd(@_) });
60    $self->conn->add_handler(join      => sub { shift; $self->on_join(@_) });
61    $self->conn->add_handler(part      => sub { shift; $self->on_part(@_) });
62    $self->conn->add_handler(disconnect => sub { shift; $self->on_disconnect(@_) });
63    $self->conn->add_handler(nicknameinuse => sub { shift; $self->on_nickinuse(@_) });
64    $self->conn->add_handler(cping     => sub { shift; $self->on_ping(@_) });
65
66    return $self;
67}
68
69################################################################################
70############################### IRC callbacks ##################################
71################################################################################
72
73sub new_message {
74    my $self = shift;
75    my $evt = shift;
76    return BarnOwl::Message->new(
77        type        => 'IRC',
78        server      => $self->server,
79        network     => $self->alias,
80        sender      => $evt->nick,
81        hostname    => $evt->host,
82        from        => $evt->from,
83        @_
84       );
85}
86
87sub on_msg {
88    my ($self, $evt) = @_;
89    my ($recipient) = $evt->to;
90    my $body = strip_irc_formatting([$evt->args]->[0]);
91    my $nick = $self->nick;
92    BarnOwl::beep() if $body =~ /\b\Q$nick\E\b/;
93    $body = BarnOwl::Style::boldify($evt->nick.' '.$body) if $evt->type eq 'caction';
94    my $msg = $self->new_message($evt,
95        direction   => 'in',
96        recipient   => $recipient,
97        body => $body,
98        $evt->type eq 'notice' ?
99          (notice     => 'true') : (),
100        is_private($recipient) ?
101          (isprivate  => 'true') : (channel => $recipient),
102        replycmd    => 'irc-msg ' .
103            (is_private($recipient) ? $evt->nick : $recipient),
104        replysendercmd => 'irc-msg ' . $evt->nick
105       );
106
107    BarnOwl::queue_message($msg);
108}
109
110sub on_ping {
111    my ($self, $evt) = @_;
112    $self->conn->ctcp_reply($evt->nick, join (' ', ($evt->args)));
113}
114
115sub on_admin_msg {
116    my ($self, $evt) = @_;
117    BarnOwl::admin_message("IRC",
118            BarnOwl::Style::boldify('IRC ' . $evt->type . ' message from '
119                . $evt->alias) . "\n"
120            . strip_irc_formatting(join '\n', cdr $evt->args));
121}
122
123sub on_motdstart {
124    my ($self, $evt) = @_;
125    $self->motd(join "\n", cdr $evt->args);
126}
127
128sub on_motd {
129    my ($self, $evt) = @_;
130    $self->motd(join "\n", $self->motd, cdr $evt->args);
131}
132
133sub on_endofmotd {
134    my ($self, $evt) = @_;
135    $self->motd(join "\n", $self->motd, cdr $evt->args);
136    if(!$self->connected) {
137        BarnOwl::admin_message("IRC", "Connected to " .
138                               $self->server . " (" . $self->alias . ")");
139        $self->connected(1);
140       
141    }
142    BarnOwl::admin_message("IRC",
143            BarnOwl::Style::boldify('MOTD for ' . $self->alias) . "\n"
144            . strip_irc_formatting($self->motd));
145}
146
147sub on_join {
148    my ($self, $evt) = @_;
149    my $msg = $self->new_message($evt,
150        loginout   => 'login',
151        channel    => $evt->to,
152        );
153    BarnOwl::queue_message($msg);
154}
155
156sub on_part {
157    my ($self, $evt) = @_;
158    my $msg = $self->new_message($evt,
159        loginout   => 'logout',
160        channel    => $evt->to,
161        );
162    BarnOwl::queue_message($msg);
163}
164
165sub on_disconnect {
166    my $self = shift;
167    delete $BarnOwl::Module::IRC::ircnets{$self->alias};
168
169    BarnOwl::admin_message('IRC',
170                           "[" . $self->alias . "] Disconnected from server");
171}
172
173sub on_nickinuse {
174    my ($self, $evt) = @_;
175    BarnOwl::admin_message("IRC",
176                           "[" . $self->alias . "] " .
177                           [$evt->args]->[1] . ": Nick already in use");
178    unless($self->connected) {
179        $self->conn->disconnect;
180    }
181}
182
183sub on_event {
184    my ($self, $evt) = @_;
185    BarnOwl::admin_message("IRC",
186            "[" . $self->alias . "] Unhandled IRC event of type " . $evt->type . ":\n"
187            . strip_irc_formatting(join("\n", $evt->args)))
188        if BarnOwl::getvar('irc:spew') eq 'on';
189}
190
191
192################################################################################
193########################### Utilities/Helpers ##################################
194################################################################################
195
196sub strip_irc_formatting {
197    my $body = shift;
198    # Strip mIRC colors. If someone wants to write code to convert
199    # these to zephyr colors, be my guest.
200    $body =~ s/\cC\d+(?:,\d+)?//g;
201    $body =~ s/\cO//g;
202   
203    my @pieces = split /\cB/, $body;
204     my $out;
205    while(@pieces) {
206        $out .= shift @pieces;
207        $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces;
208    }
209    return $out;
210}
211
212# Determines if the given message recipient is a username, as opposed to
213# a channel that starts with # or &.
214sub is_private {
215    return shift !~ /^[\#\&]/;
216}
217
218sub cdr {
219    shift;
220    return @_;
221}
222
2231;
Note: See TracBrowser for help on using the repository browser.