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

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