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

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 4df2568 was 4df2568, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
IRC: Show admin messages for mode changes.
  • Property mode set to 100644
File size: 9.9 KB
RevLine 
[b38b0b2]1use strict;
2use warnings;
3
4package BarnOwl::Module::IRC::Connection;
5
6=head1 NAME
7
8BarnOwl::Module::IRC::Connection
9
10=head1 DESCRIPTION
11
[ba2ca66]12This module is a wrapper around Net::IRC::Connection for BarnOwl's IRC
[b38b0b2]13support
14
15=cut
16
[ba2ca66]17use Net::IRC::Connection;
18
19use base qw(Class::Accessor Exporter);
[38cfdb5d]20__PACKAGE__->mk_accessors(qw(conn alias channels connected motd names_tmp whois_tmp));
[2c40dc0]21our @EXPORT_OK = qw(&is_private);
[b38b0b2]22
23use BarnOwl;
24
[ba2ca66]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
[b38b0b2]35sub new {
36    my $class = shift;
37    my $irc = shift;
38    my $alias = shift;
39    my %args = (@_);
[ba2ca66]40    my $conn = Net::IRC::Connection->new($irc, %args);
41    my $self = bless({}, $class);
42    $self->conn($conn);
[b38b0b2]43    $self->alias($alias);
44    $self->channels([]);
[ba2ca66]45    $self->motd("");
46    $self->connected(0);
[d264c6d]47    $self->names_tmp(0);
[38cfdb5d]48    $self->whois_tmp("");
[ba2ca66]49
[5ff830a]50    $self->conn->add_handler(376 => sub { shift; $self->on_connect(@_) });
[ba2ca66]51    $self->conn->add_default_handler(sub { shift; $self->on_event(@_) });
52    $self->conn->add_handler(['msg', 'notice', 'public', 'caction'],
53            sub { shift; $self->on_msg(@_) });
54    $self->conn->add_handler(['welcome', 'yourhost', 'created',
[661d2eb]55                              'luserclient', 'luserop', 'luserchannels', 'luserme',
56                              'error'],
[ba2ca66]57            sub { shift; $self->on_admin_msg(@_) });
58    $self->conn->add_handler(['myinfo', 'map', 'n_local', 'n_global',
[bc0d7bc]59            'luserconns'],
60            sub { });
[ba2ca66]61    $self->conn->add_handler(motdstart => sub { shift; $self->on_motdstart(@_) });
62    $self->conn->add_handler(motd      => sub { shift; $self->on_motd(@_) });
63    $self->conn->add_handler(endofmotd => sub { shift; $self->on_endofmotd(@_) });
64    $self->conn->add_handler(join      => sub { shift; $self->on_join(@_) });
65    $self->conn->add_handler(part      => sub { shift; $self->on_part(@_) });
[4789b17]66    $self->conn->add_handler(quit      => sub { shift; $self->on_quit(@_) });
[ba2ca66]67    $self->conn->add_handler(disconnect => sub { shift; $self->on_disconnect(@_) });
68    $self->conn->add_handler(nicknameinuse => sub { shift; $self->on_nickinuse(@_) });
69    $self->conn->add_handler(cping     => sub { shift; $self->on_ping(@_) });
[3ad15ff]70    $self->conn->add_handler(topic     => sub { shift; $self->on_topic(@_) });
71    $self->conn->add_handler(topicinfo => sub { shift; $self->on_topicinfo(@_) });
[38cfdb5d]72    $self->conn->add_handler(namreply  => sub { shift; $self->on_namreply(@_) });
73    $self->conn->add_handler(endofnames=> sub { shift; $self->on_endofnames(@_) });
[d264c6d]74    $self->conn->add_handler(endofwhois=> sub { shift; $self->on_endofwhois(@_) });
[4df2568]75    $self->conn->add_handler(mode      => sub { shift; $self->on_mode(@_) });
[b38b0b2]76
[330c55a]77    # * nosuchchannel
78    # *
79
[b38b0b2]80    return $self;
81}
82
[9c7a701]83sub getSocket
84{
85    my $self = shift;
86    return $self->conn->socket;
87}
88
[b38b0b2]89################################################################################
90############################### IRC callbacks ##################################
91################################################################################
92
[47b6a5f]93sub new_message {
94    my $self = shift;
95    my $evt = shift;
96    return BarnOwl::Message->new(
97        type        => 'IRC',
98        server      => $self->server,
99        network     => $self->alias,
100        sender      => $evt->nick,
101        hostname    => $evt->host,
102        from        => $evt->from,
103        @_
104       );
105}
106
[b38b0b2]107sub on_msg {
108    my ($self, $evt) = @_;
[2c40dc0]109    my ($recipient) = $evt->to;
110    my $body = strip_irc_formatting([$evt->args]->[0]);
[bc0d7bc]111    my $nick = $self->nick;
112    BarnOwl::beep() if $body =~ /\b\Q$nick\E\b/;
[3048f1f]113    $body = '* '.$evt->nick.' '.$body if $evt->type eq 'caction';
[47b6a5f]114    my $msg = $self->new_message($evt,
[b38b0b2]115        direction   => 'in',
[2c40dc0]116        recipient   => $recipient,
[47b6a5f]117        body => $body,
[2c40dc0]118        $evt->type eq 'notice' ?
119          (notice     => 'true') : (),
120        is_private($recipient) ?
[0e52069]121          (isprivate  => 'true') : (channel => $recipient),
[3c5fe43]122        replycmd    => 'irc-msg -a ' . $self->alias . ' ' .
[2c40dc0]123            (is_private($recipient) ? $evt->nick : $recipient),
[3c5fe43]124        replysendercmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick
[b38b0b2]125       );
[47b6a5f]126
[b38b0b2]127    BarnOwl::queue_message($msg);
128}
129
[2c40dc0]130sub on_ping {
131    my ($self, $evt) = @_;
[ba2ca66]132    $self->conn->ctcp_reply($evt->nick, join (' ', ($evt->args)));
[2c40dc0]133}
134
[bc0d7bc]135sub on_admin_msg {
136    my ($self, $evt) = @_;
137    BarnOwl::admin_message("IRC",
138            BarnOwl::Style::boldify('IRC ' . $evt->type . ' message from '
[1951db8]139                . $self->alias) . "\n"
[d264c6d]140            . strip_irc_formatting(join ' ', cdr($evt->args)));
[bc0d7bc]141}
142
143sub on_motdstart {
144    my ($self, $evt) = @_;
[1af21e8]145    $self->motd(join "\n", cdr($evt->args));
[bc0d7bc]146}
147
148sub on_motd {
149    my ($self, $evt) = @_;
[1af21e8]150    $self->motd(join "\n", $self->motd, cdr($evt->args));
[bc0d7bc]151}
152
153sub on_endofmotd {
154    my ($self, $evt) = @_;
[1af21e8]155    $self->motd(join "\n", $self->motd, cdr($evt->args));
[ba2ca66]156    if(!$self->connected) {
[3baf77f]157        BarnOwl::admin_message("IRC", "Connected to " .
158                               $self->server . " (" . $self->alias . ")");
[ba2ca66]159        $self->connected(1);
[3baf77f]160       
161    }
[bc0d7bc]162    BarnOwl::admin_message("IRC",
[3baf77f]163            BarnOwl::Style::boldify('MOTD for ' . $self->alias) . "\n"
[ba2ca66]164            . strip_irc_formatting($self->motd));
[bc0d7bc]165}
166
[47b6a5f]167sub on_join {
168    my ($self, $evt) = @_;
169    my $msg = $self->new_message($evt,
170        loginout   => 'login',
[4789b17]171        action     => 'join',
[47b6a5f]172        channel    => $evt->to,
[d264c6d]173        replycmd => 'irc-msg -a ' . $self->alias . ' ' . join(' ', $evt->to),
[3c5fe43]174        replysendercmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick
[47b6a5f]175        );
176    BarnOwl::queue_message($msg);
177}
178
179sub on_part {
180    my ($self, $evt) = @_;
181    my $msg = $self->new_message($evt,
182        loginout   => 'logout',
[4789b17]183        action     => 'part',
[47b6a5f]184        channel    => $evt->to,
[d264c6d]185        replycmd => 'irc-msg -a ' . $self->alias . ' ' . join(' ', $evt->to),
[3c5fe43]186        replysendercmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick
[47b6a5f]187        );
188    BarnOwl::queue_message($msg);
189}
190
[4789b17]191sub on_quit {
192    my ($self, $evt) = @_;
193    my $msg = $self->new_message($evt,
194        loginout   => 'logout',
195        action     => 'quit',
196        from       => $evt->to,
197        reason     => [$evt->args]->[0],
198        replycmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick,
199        replysendercmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick
200        );
201    BarnOwl::queue_message($msg);
202}
203
[9e02bb7]204sub on_disconnect {
205    my $self = shift;
206    delete $BarnOwl::Module::IRC::ircnets{$self->alias};
[9c7a701]207    BarnOwl::remove_dispatch($self->{FD});
[9e02bb7]208    BarnOwl::admin_message('IRC',
209                           "[" . $self->alias . "] Disconnected from server");
210}
211
212sub on_nickinuse {
213    my ($self, $evt) = @_;
214    BarnOwl::admin_message("IRC",
215                           "[" . $self->alias . "] " .
216                           [$evt->args]->[1] . ": Nick already in use");
[ba2ca66]217    unless($self->connected) {
218        $self->conn->disconnect;
[eab7a4c]219    }
[9e02bb7]220}
221
[3ad15ff]222sub on_topic {
223    my ($self, $evt) = @_;
224    my @args = $evt->args;
225    if (scalar @args > 1) {
226        BarnOwl::admin_message("IRC",
227                "Topic for $args[1] on " . $self->alias . " is $args[2]");
228    } else {
229        BarnOwl::admin_message("IRC",
230                "Topic changed to $args[0]");
231    }
232}
233
234sub on_topicinfo {
235    my ($self, $evt) = @_;
236    my @args = $evt->args;
237    BarnOwl::admin_message("IRC",
238        "Topic for $args[1] set by $args[2] at " . localtime($args[3]));
239}
240
[38cfdb5d]241# IRC gives us a bunch of namreply messages, followed by an endofnames.
242# We need to collect them from the namreply and wait for the endofnames message.
243# After this happens, the names_tmp variable is cleared.
244
245sub on_namreply {
246    my ($self, $evt) = @_;
[d264c6d]247    return unless $self->names_tmp;
[38cfdb5d]248    $self->names_tmp([@{$self->names_tmp}, split(' ', [$evt->args]->[3])]);
249}
250
251sub on_endofnames {
252    my ($self, $evt) = @_;
[d264c6d]253    return unless $self->names_tmp;
[38cfdb5d]254    my $names = BarnOwl::Style::boldify("Members of " . [$evt->args]->[1] . ":\n");
255    for my $name (@{$self->names_tmp}) {
256        $names .= "  $name\n";
257    }
258    BarnOwl::popless_ztext($names);
[d264c6d]259    $self->names_tmp(0);
260}
261
262sub on_whois {
263    my ($self, $evt) = @_;
264    $self->whois_tmp(
265      $self->whois_tmp . "\n" . $evt->type . ":\n  " .
266      join("\n  ", cdr(cdr($evt->args))) . "\n"
267    );
268}
269
270sub on_endofwhois {
271    my ($self, $evt) = @_;
272    BarnOwl::popless_ztext(
273        BarnOwl::Style::boldify("/whois for " . [$evt->args]->[1] . ":\n") .
274        $self->whois_tmp
275    );
276    $self->whois_tmp([]);
277}
278
[4df2568]279sub on_mode {
280    my ($self, $evt) = @_;
281    BarnOwl::admin_message("IRC",
282                           "[" . $self->alias . "] User " . ($evt->nick) . + " set mode " .
283                           join(" ", $evt->args) . "on " . $evt->to->[0]
284                          );
285}
286
[d264c6d]287sub on_event {
288    my ($self, $evt) = @_;
289    return on_whois(@_) if ($evt->type =~ /^whois/);
290    BarnOwl::admin_message("IRC",
291            "[" . $self->alias . "] Unhandled IRC event of type " . $evt->type . ":\n"
292            . strip_irc_formatting(join("\n", $evt->args)))
293        if BarnOwl::getvar('irc:spew') eq 'on';
[38cfdb5d]294}
[9e02bb7]295
[b38b0b2]296################################################################################
297########################### Utilities/Helpers ##################################
298################################################################################
299
300sub strip_irc_formatting {
301    my $body = shift;
[214b790]302    # Strip mIRC colors. If someone wants to write code to convert
303    # these to zephyr colors, be my guest.
304    $body =~ s/\cC\d+(?:,\d+)?//g;
305    $body =~ s/\cO//g;
306   
307    my @pieces = split /\cB/, $body;
[3835ae8]308    my $out = '';
[b38b0b2]309    while(@pieces) {
310        $out .= shift @pieces;
311        $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces;
312    }
313    return $out;
314}
315
[2c40dc0]316# Determines if the given message recipient is a username, as opposed to
317# a channel that starts with # or &.
318sub is_private {
319    return shift !~ /^[\#\&]/;
320}
[b38b0b2]321
[bc0d7bc]322sub cdr {
323    shift;
324    return @_;
325}
326
[b38b0b2]3271;
Note: See TracBrowser for help on using the repository browser.