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

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since bc0d7bc was bc0d7bc, checked in by Geoffrey Thomas <geofft@mit.edu>, 16 years ago
A (broken) attempt at handling motds and other IRC admin messages Export owl_function_beep() to Perl, and ring the bell on receiving a message containing your nick (on that network)
  • Property mode set to 100644
File size: 5.1 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 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    bless($self, $class);
33
34    $self->add_global_handler(376 => sub { goto &on_connect });
35    $self->add_global_handler(['msg', 'notice', 'public', 'caction'],
36            sub { goto &on_msg });
37    $self->add_global_handler(['welcome', 'yourhost', 'created',
38            'luserclient', 'luserop', 'luserchannels', 'luserme'],
39            sub { goto &on_admin_msg });
40    $self->add_global_handler(['myinfo', 'map', 'n_local', 'n_global',
41            'luserconns'],
42            sub { });
43    $self->add_handler(375 => sub { goto &on_motdstart });
44    $self->add_handler(372 => sub { goto &on_motd });
45    $self->add_handler(376 => sub { goto &on_endofmotd });
46    $self->add_global_handler(cping => sub { goto &on_ping });
47    $self->add_global_handler(join  => sub { goto &on_join });
48    $self->add_global_handler(part  => sub { goto &on_part });
49    $self->add_default_handler(sub { goto &on_event; });
50
51    return $self;
52}
53
54################################################################################
55############################### IRC callbacks ##################################
56################################################################################
57
58sub on_connect {
59    my ($self, $evt) = @_;
60    BarnOwl::admin_message("IRC", "Connected to " . $self->server . " (" . $self->alias . ")");
61}
62
63sub new_message {
64    my $self = shift;
65    my $evt = shift;
66    return BarnOwl::Message->new(
67        type        => 'IRC',
68        server      => $self->server,
69        network     => $self->alias,
70        sender      => $evt->nick,
71        hostname    => $evt->host,
72        from        => $evt->from,
73        @_
74       );
75}
76
77sub on_msg {
78    my ($self, $evt) = @_;
79    my ($recipient) = $evt->to;
80    my $body = strip_irc_formatting([$evt->args]->[0]);
81    my $nick = $self->nick;
82    BarnOwl::beep() if $body =~ /\b\Q$nick\E\b/;
83    $body = BarnOwl::Style::boldify($evt->nick.' '.$body) if $evt->type eq 'caction';
84    my $msg = $self->new_message($evt,
85        direction   => 'in',
86        recipient   => $recipient,
87        body => $body,
88        $evt->type eq 'notice' ?
89          (notice     => 'true') : (),
90        is_private($recipient) ?
91          (isprivate  => 'true') : (channel => $recipient),
92        replycmd    => 'irc-msg ' .
93            (is_private($recipient) ? $evt->nick : $recipient),
94        replysendercmd => 'irc-msg ' . $evt->nick
95       );
96
97    BarnOwl::queue_message($msg);
98}
99
100sub on_ping {
101    my ($self, $evt) = @_;
102    $self->ctcp_reply($evt->nick, join (' ', ($evt->args)));
103}
104
105sub on_admin_msg {
106    my ($self, $evt) = @_;
107    BarnOwl::admin_message("IRC",
108            BarnOwl::Style::boldify('IRC ' . $evt->type . ' message from '
109                . $evt->alias) . "\n"
110            . strip_irc_formatting(join '\n', cdr $evt->args));
111}
112
113sub on_motdstart {
114    my ($self, $evt) = @_;
115    $self->motd(join "\n", cdr $evt->args);
116}
117
118sub on_motd {
119    my ($self, $evt) = @_;
120    $self->motd(join "\n", $self->motd, cdr $evt->args);
121}
122
123sub on_endofmotd {
124    my ($self, $evt) = @_;
125    $self->motd(join "\n", $self->motd, cdr $evt->args);
126    BarnOwl::admin_message("IRC",
127            BarnOwl::Style::boldify('MOTD for ' . $evt->alias) . "\n"
128            . strip_irc_formatting($self->motd));
129}
130
131sub on_join {
132    my ($self, $evt) = @_;
133    my $msg = $self->new_message($evt,
134        loginout   => 'login',
135        channel    => $evt->to,
136        );
137    BarnOwl::queue_message($msg);
138}
139
140sub on_part {
141    my ($self, $evt) = @_;
142    my $msg = $self->new_message($evt,
143        loginout   => 'logout',
144        channel    => $evt->to,
145        );
146    BarnOwl::queue_message($msg);
147}
148
149sub on_event {
150    my ($self, $evt) = @_;
151    BarnOwl::admin_message("IRC",
152            "[" . $self->alias . "] Unhandled IRC event of type " . $evt->type . ":\n"
153            . strip_irc_formatting(join("\n", $evt->args)))
154        if BarnOwl::getvar('irc:spew') eq 'on';
155}
156
157################################################################################
158########################### Utilities/Helpers ##################################
159################################################################################
160
161sub strip_irc_formatting {
162    my $body = shift;
163    my @pieces = split /\x02/, $body;
164     my $out;
165    while(@pieces) {
166        $out .= shift @pieces;
167        $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces;
168    }
169    return $out;
170}
171
172# Determines if the given message recipient is a username, as opposed to
173# a channel that starts with # or &.
174sub is_private {
175    return shift !~ /^[\#\&]/;
176}
177
178sub cdr {
179    shift;
180    return @_;
181}
182
1831;
Note: See TracBrowser for help on using the repository browser.