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

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