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

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since e4db5ae was e4db5ae, checked in by Nelson Elhage <nelhage@mit.edu>, 16 years ago
Don't clober our parent's connected() method, either.
  • 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 _connected _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    $self->_connected(0);
33    bless($self, $class);
34
35    $self->add_default_handler(sub { goto &on_event; });
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 new_message {
61    my $self = shift;
62    my $evt = shift;
63    return BarnOwl::Message->new(
64        type        => 'IRC',
65        server      => $self->server,
66        network     => $self->alias,
67        sender      => $evt->nick,
68        hostname    => $evt->host,
69        from        => $evt->from,
70        @_
71       );
72}
73
74sub on_msg {
75    my ($self, $evt) = @_;
76    my ($recipient) = $evt->to;
77    my $body = strip_irc_formatting([$evt->args]->[0]);
78    my $nick = $self->nick;
79    BarnOwl::beep() if $body =~ /\b\Q$nick\E\b/;
80    $body = BarnOwl::Style::boldify($evt->nick.' '.$body) if $evt->type eq 'caction';
81    my $msg = $self->new_message($evt,
82        direction   => 'in',
83        recipient   => $recipient,
84        body => $body,
85        $evt->type eq 'notice' ?
86          (notice     => 'true') : (),
87        is_private($recipient) ?
88          (isprivate  => 'true') : (channel => $recipient),
89        replycmd    => 'irc-msg ' .
90            (is_private($recipient) ? $evt->nick : $recipient),
91        replysendercmd => 'irc-msg ' . $evt->nick
92       );
93
94    BarnOwl::queue_message($msg);
95}
96
97sub on_ping {
98    my ($self, $evt) = @_;
99    $self->ctcp_reply($evt->nick, join (' ', ($evt->args)));
100}
101
102sub on_admin_msg {
103    my ($self, $evt) = @_;
104    BarnOwl::admin_message("IRC",
105            BarnOwl::Style::boldify('IRC ' . $evt->type . ' message from '
106                . $evt->alias) . "\n"
107            . strip_irc_formatting(join '\n', cdr $evt->args));
108}
109
110sub on_motdstart {
111    my ($self, $evt) = @_;
112    $self->_motd(join "\n", cdr $evt->args);
113}
114
115sub on_motd {
116    my ($self, $evt) = @_;
117    $self->_motd(join "\n", $self->_motd, cdr $evt->args);
118}
119
120sub on_endofmotd {
121    my ($self, $evt) = @_;
122    $self->_motd(join "\n", $self->_motd, cdr $evt->args);
123    if(!$self->_connected) {
124        BarnOwl::admin_message("IRC", "Connected to " .
125                               $self->server . " (" . $self->alias . ")");
126        $self->_connected(1);
127       
128    }
129    BarnOwl::admin_message("IRC",
130            BarnOwl::Style::boldify('MOTD for ' . $self->alias) . "\n"
131            . strip_irc_formatting($self->_motd));
132}
133
134sub on_join {
135    my ($self, $evt) = @_;
136    my $msg = $self->new_message($evt,
137        loginout   => 'login',
138        channel    => $evt->to,
139        );
140    BarnOwl::queue_message($msg);
141}
142
143sub on_part {
144    my ($self, $evt) = @_;
145    my $msg = $self->new_message($evt,
146        loginout   => 'logout',
147        channel    => $evt->to,
148        );
149    BarnOwl::queue_message($msg);
150}
151
152sub on_disconnect {
153    my $self = shift;
154    delete $BarnOwl::Module::IRC::ircnets{$self->alias};
155
156    BarnOwl::admin_message('IRC',
157                           "[" . $self->alias . "] Disconnected from server");
158}
159
160sub on_nickinuse {
161    my ($self, $evt) = @_;
162    BarnOwl::admin_message("IRC",
163                           "[" . $self->alias . "] " .
164                           [$evt->args]->[1] . ": Nick already in use");
165}
166
167sub on_event {
168    my ($self, $evt) = @_;
169    BarnOwl::admin_message("IRC",
170            "[" . $self->alias . "] Unhandled IRC event of type " . $evt->type . ":\n"
171            . strip_irc_formatting(join("\n", $evt->args)))
172        if BarnOwl::getvar('irc:spew') eq 'on';
173}
174
175
176################################################################################
177########################### Utilities/Helpers ##################################
178################################################################################
179
180sub strip_irc_formatting {
181    my $body = shift;
182    # Strip mIRC colors. If someone wants to write code to convert
183    # these to zephyr colors, be my guest.
184    $body =~ s/\cC\d+(?:,\d+)?//g;
185    $body =~ s/\cO//g;
186   
187    my @pieces = split /\cB/, $body;
188     my $out;
189    while(@pieces) {
190        $out .= shift @pieces;
191        $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces;
192    }
193    return $out;
194}
195
196# Determines if the given message recipient is a username, as opposed to
197# a channel that starts with # or &.
198sub is_private {
199    return shift !~ /^[\#\&]/;
200}
201
202sub cdr {
203    shift;
204    return @_;
205}
206
2071;
Note: See TracBrowser for help on using the repository browser.