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

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 47b6a5f was 47b6a5f, checked in by Nelson Elhage <nelhage@mit.edu>, 16 years ago
Show PART and JOIN messages
  • Property mode set to 100644
File size: 3.8 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));
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    bless($self, $class);
32
33    $self->add_global_handler(376 => sub { goto &on_connect });
34    $self->add_global_handler(['msg', 'notice', 'public', 'caction'],
35            sub { goto &on_msg });
36    $self->add_global_handler(cping => sub { goto &on_ping });
37    $self->add_global_handler(join  => sub { goto &on_join });
38    $self->add_global_handler(part  => sub { goto &on_part });
39    $self->add_default_handler(sub { goto &on_event; });
40
41    return $self;
42}
43
44################################################################################
45############################### IRC callbacks ##################################
46################################################################################
47
48sub on_connect {
49    my ($self, $evt) = @_;
50    BarnOwl::admin_message("IRC", "Connected to " . $self->server . " (" . $self->alias . ")");
51}
52
53sub new_message {
54    my $self = shift;
55    my $evt = shift;
56    return BarnOwl::Message->new(
57        type        => 'IRC',
58        server      => $self->server,
59        network     => $self->alias,
60        sender      => $evt->nick,
61        hostname    => $evt->host,
62        from        => $evt->from,
63        @_
64       );
65}
66
67sub on_msg {
68    my ($self, $evt) = @_;
69    my ($recipient) = $evt->to;
70    my $body = strip_irc_formatting([$evt->args]->[0]);
71    $body = BarnOwl::Style::boldify($evt->nick.' '.$body) if $evt->type eq 'caction';
72    my $msg = $self->new_message($evt,
73        direction   => 'in',
74        recipient   => $recipient,
75        body => $body,
76        $evt->type eq 'notice' ?
77          (notice     => 'true') : (),
78        is_private($recipient) ?
79          (isprivate  => 'true') : (channel => $recipient),
80        replycmd    => 'irc-msg ' .
81            (is_private($recipient) ? $evt->nick : $recipient),
82        replysendercmd => 'irc-msg ' . $evt->nick
83       );
84
85    BarnOwl::queue_message($msg);
86}
87
88sub on_ping {
89    my ($self, $evt) = @_;
90    $self->ctcp_reply($evt->nick, join (' ', ($evt->args)));
91}
92
93sub on_join {
94    my ($self, $evt) = @_;
95    my $msg = $self->new_message($evt,
96        loginout   => 'login',
97        channel    => $evt->to,
98        );
99    BarnOwl::queue_message($msg);
100}
101
102sub on_part {
103    my ($self, $evt) = @_;
104    my $msg = $self->new_message($evt,
105        loginout   => 'logout',
106        channel    => $evt->to,
107        );
108    BarnOwl::queue_message($msg);
109}
110
111sub on_event {
112    my ($self, $evt) = @_;
113    BarnOwl::admin_message("IRC",
114            "[" . $self->alias . "] Unhandled IRC event of type " . $evt->type . ":\n"
115            . strip_irc_formatting(join("\n", $evt->args)))
116        if BarnOwl::getvar('irc:spew') eq 'on';
117}
118
119################################################################################
120########################### Utilities/Helpers ##################################
121################################################################################
122
123sub strip_irc_formatting {
124    my $body = shift;
125    my @pieces = split /\x02/, $body;
126     my $out;
127    while(@pieces) {
128        $out .= shift @pieces;
129        $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces;
130    }
131    return $out;
132}
133
134# Determines if the given message recipient is a username, as opposed to
135# a channel that starts with # or &.
136sub is_private {
137    return shift !~ /^[\#\&]/;
138}
139
1401;
Note: See TracBrowser for help on using the repository browser.