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

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