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

barnowl_perlaimdebianrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 2c40dc0 was 2c40dc0, checked in by Geoffrey Thomas <geofft@mit.edu>, 13 years ago
Added support for channels in IRC. After you :irc-connect to a server, you can :irc-join #channel, and then :irc-msg either a user or a #channel. Replies, isprivate, and context should work vaguely as expected.
  • Property mode set to 100644
File size: 3.1 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
12This module is a Net::IRC::Connection subclass for BarnOwl's IRC
13support
14
15=cut
16
[2c40dc0]17use base qw(Net::IRC::Connection Class::Accessor Exporter);
[b38b0b2]18__PACKAGE__->mk_accessors(qw(alias channels));
[2c40dc0]19our @EXPORT_OK = qw(&is_private);
[b38b0b2]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
[2c40dc0]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 });
[b38b0b2]37    $self->add_default_handler(sub { goto &on_event; });
38
39    return $self;
40}
41
42################################################################################
43############################### IRC callbacks ##################################
44################################################################################
45
46sub on_connect {
47    my ($self, $evt) = @_;
48    BarnOwl::admin_message("IRC", "Connected to " . $self->server . " (" . $self->alias . ")");
49}
50
51sub on_msg {
52    my ($self, $evt) = @_;
[2c40dc0]53    my ($recipient) = $evt->to;
54    my $body = strip_irc_formatting([$evt->args]->[0]);
55    $body = BarnOwl::Style::boldify($evt->nick.' '.$body) if $evt->type eq 'caction';
[b38b0b2]56    my $msg = BarnOwl::Message->new(
57        type        => 'IRC',
58        direction   => 'in',
59        server      => $self->server,
60        network     => $self->alias,
[2c40dc0]61        recipient   => $recipient,
62        body        => $body,
[b38b0b2]63        sender      => $evt->nick,
64        hostname    => $evt->host,
65        from        => $evt->from,
[2c40dc0]66        $evt->type eq 'notice' ?
67          (notice     => 'true') : (),
68        is_private($recipient) ?
69          (isprivate  => 'true') : (),
70        replycmd    => 'irc-msg ' .
71            (is_private($recipient) ? $evt->nick : $recipient),
72        replysendercmd => 'irc-msg ' . $evt->nick,
[b38b0b2]73       );
74    BarnOwl::queue_message($msg);
75}
76
[2c40dc0]77sub on_ping {
78    my ($self, $evt) = @_;
79    $self->ctcp_reply($evt->nick, join (' ', ($evt->args)));
80}
81
82sub on_event {
83    my ($self, $evt) = @_;
84    BarnOwl::admin_message("IRC",
85            "Unhandled IRC event of type " . $evt->type . ":\n"
86            . strip_irc_formatting(join("\n", $evt->args)))
87        if BarnOwl::getvar('irc:spew') eq 'on';
88}
89
[b38b0b2]90################################################################################
91########################### Utilities/Helpers ##################################
92################################################################################
93
94sub strip_irc_formatting {
95    my $body = shift;
96    my @pieces = split /\x02/, $body;
97     my $out;
98    while(@pieces) {
99        $out .= shift @pieces;
100        $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces;
101    }
102    return $out;
103}
104
[2c40dc0]105# Determines if the given message recipient is a username, as opposed to
106# a channel that starts with # or &.
107sub is_private {
108    return shift !~ /^[\#\&]/;
109}
[b38b0b2]110
1111;
Note: See TracBrowser for help on using the repository browser.