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

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