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

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since b38b0b2 was b38b0b2, checked in by Nelson Elhage <nelhage@mit.edu>, 16 years ago
Committing a partial IRC plugin
  • Property mode set to 100644
File size: 2.3 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);
18__PACKAGE__->mk_accessors(qw(alias channels));
19
20use BarnOwl;
21
22sub new {
23    my $class = shift;
24    my $irc = shift;
25    my $alias = shift;
26    my %args = (@_);
27    my $self = $class->SUPER::new($irc, %args);
28    $self->alias($alias);
29    $self->channels([]);
30    bless($self, $class);
31
32    $self->add_global_handler(endofmotd => sub { goto &on_connect });
33    $self->add_global_handler(msg => sub { goto &on_msg });
34    $self->add_global_handler(notice => sub { goto &on_msg });
35    $self->add_default_handler(sub { goto &on_event; });
36
37    return $self;
38}
39
40################################################################################
41############################### IRC callbacks ##################################
42################################################################################
43
44sub on_connect {
45    my ($self, $evt) = @_;
46    BarnOwl::admin_message("IRC", "Connected to " . $self->server . " (" . $self->alias . ")");
47}
48
49sub on_msg {
50    my ($self, $evt) = @_;
51    my $replycmd = "irc-msg " . $evt->nick;
52    my $msg = BarnOwl::Message->new(
53        type        => 'IRC',
54        direction   => 'in',
55        server      => $self->server,
56        network     => $self->alias,
57        recipient   => $self->nick,
58        body        => strip_irc_formatting([$evt->args]->[0]),
59        sender      => $evt->nick,
60        hostname    => $evt->host,
61        from        => $evt->from,
62        notice      => $evt->type eq 'notice' ? 'true' : '',
63        isprivate   => 'true',
64        replycmd    => $replycmd,
65        replysendercmd => $replycmd
66       );
67    BarnOwl::queue_message($msg);
68}
69
70################################################################################
71########################### Utilities/Helpers ##################################
72################################################################################
73
74sub strip_irc_formatting {
75    my $body = shift;
76    my @pieces = split /\x02/, $body;
77     my $out;
78    while(@pieces) {
79        $out .= shift @pieces;
80        $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces;
81    }
82    return $out;
83}
84
85
861;
Note: See TracBrowser for help on using the repository browser.