DELTA 799 658 1155
SVN  H΄V1¬3  ` J@ .v Vn i(N J@ _$D )Kwrapper around Net::IRC::Connection for BarnOwl's IRC
support

=cut

use Net::IRC::Connection;

use base qw(Class::Accessor Exporter);
__PACKAGE__->mk_accessors(qw(conn alias channels connected motd));
our @EXPORT_OK = qw(&is_private);

use BarnOwl;

BEGIN {
    no strict 'refs';
    my @delegate = qw(nick server);
    for my $meth (@delegate) {
        *{"BarnOwl::Module::IRC::Connection::$meth"} = sub {
            shift->conn->$meth(@_);
        }
    }
};

sub new {
    my $class = shift;
    my $irc = shift;
    my $alias = shift;
    my %args = (@_);
    my $conn = Net::IRC::Connection->new($irc, %args);
    my $self = bless({}, $class);
    $self->conn($conn);
    $self->alias($alias);
    $self->channels([]);
    $self->motd("");
    $self->connected(0);

    $self->conn->add_handler(376 => sub { shift; $self->on_connect(@_) });
    $self->conn->add_default_handler(sub { shift; $self->on_event(@_) });
    $self->conn->add_handler(['msg', 'notice', 'public', 'caction'],
            sub { shift; $self->on_msg(@_) });
    $self->conn->add_handler(['welcome', 'yourhost', 'created',
                              'luserclient', 'luserop', 'luserchannels', 'luserme',
                              'error'],
            sub { shift; $self->on_admin_msg(@_) });
    $self->conn->add_handler(['myinfo', 'map', 'n_local', 'n_global',
            'luserconns'],
            sub { });
    $self->conn->add_handler(motdstart => sub { shift; $self->on_motdstart(@_) });
    $self->conn->add_handler(motd      => sub { shift; $self->on_motd(@_) });
    $self->conn->add_handler(endofmotd => sub { shift; $self->on_endofmotd(@_) });
    $self->conn->add_handler(join      => sub { shift; $self->on_join(@_) });
    $self->conn->add_handler(part      => sub { shift; $self->on_part(@_) });
    $self->conn->add_handler(disconnect => sub { shift; $self->on_disconnect(@_) });
    $self->conn->add_handler(nicknameinuse => sub { shift; $self->on_nickinuse(@_) });
    $self->conn->add_handler(cping     => sub { shift; $self->on_ping(@_) });

    return $self;
}

sub getSocket
{
    my $self = shift;
    return $self->conn->socket;
}

new_message {
    my $self = shift;
    my $evt = shift;
    return BarnOwl::Message->new(
        type        => 'IRC@_
       );
}

sub on_msg {
    my ($self, $evt) = @_;
    my ($recipient) = $evt->to;
    my $body = strip_irc_formatting([$evt->args]->[0]);
    my $nick = $self->nick;
    BarnOwl::beep() if $body =~ /\b\Q$nick\E\b/;
    $body = BarnOwl::Style::boldify($evt->nick.' '.$body) if $evt->type eq 'caction';
    my $msg = $self->new_message($evt,
        direction   => 'in',
        recipient   => $recipient,
        body => $body,
        $evt->type eq 'notice' ?
          (notice     => 'true') : (),
        is_private($recipient) ?
          (isprivate  => 'true') : (channel => $recipient),
        replycmd    => 'irc-msg ' .
            (is_private($recipient) ? $evt->nick : $recipient),
        replysendercmd => 'irc-msg ' . $evt->nick
       );

    BarnOwl::queue_message($msg);
}

sub on_ping {
    my ($self, $evt) = @_;
    $self->conn->ctcp_reply($evt->nick, join (' ', ($evt->args)));
}

sub on_admin_msg {
    my ($self, $evt) = @_;
    BarnOwl::admin_message("IRC",
            BarnOwl::Style::boldify('IRC ' . $evt->type . ' message from '
                . $self->alias) . "\n"
            . strip_irc_formatting(join '\n', $evt->args));
}

sub on_motdstart {
    my ($self, $evt) = @_;
    $self->motd(join "\n", cdr($evt->args));
}

sub on_motd {
    my ($self, $evt) = @_;
    $self->motd(join "\n", $self->motd, cdr($evt->args));
}

sub on_endofmotd {
    my ($self, $evt) = @_;
    $self->motd(join "\n", $self->motd, cdr($evt->args));
    if(!$self->connected) {
        BarnOwl::admin_message("IRC", "Connected to " .
                               $self->server . " (" . $self->alias . ")");
        $self->connected(1);
        
    }
    BarnOwl::admin_message("IRC",
            BarnOwl::Style::boldify('MOTD for ' . $self->alias) . "\n"
            . strip_irc_formatting($self->motd));
}

sub on_join {
    my ($self, $evt) = @_;
    my $msg = $self->new_message($evt,
        loginout   => 'login',
        channel    => $evt->to,
        );
    BarnOwl::queue_message($msg);
}

sub on_part {
    my ($self, $evt) = @_;
    my $msg = $self->new_message($evt,
        loginout   => 'logout',
        channel    => $evt->to,
        );
    BarnOwl::queue_message($msg);
}

sub on_disconnect {
    my $self = shift;
    delete $BarnOwl::Module::IRC::ircnets{$self->alias};
    BarnOwl::remove_dispatch($self->{FD});
    BarnOwl::admin_message('IRC',
                           "[" . $self->alias . "] Disconnected from server");
}

sub on_nickinuse {
    my ($self, $evt) = @_;
    BarnOwl::admin_message("IRC",
                           "[" . $self->alias . "] " .
                           [$evt->args]->[1] . ": Nick already in use");
    unless($self->connected) {
        $self->conn->disconnect;
    }
}

sub on_event {
    my ($self, $evt) = @_;
    BarnOwl::admin_message("IRC",
            "[" . $self->alias . "] Unhandled IRC event of type " . $evt->type . ":\n"
            . strip_irc_formatting(join("\n", $evt->args)))
        if BarnOwl::getvar('irc:spew') eq 'on';
}


# Strip mIRC colors. If someone wants to write code to convert
    # these to zephyr colors, be my guest.
    $body =~ s/\cC\d+(?:,\d+)?//g;
    $body =~ s/\cO//g;
    
    my @pieces = split /\cB# Determines if the given message recipient is a username, as opposed to
# a channel that starts with # or &.
sub is_private {
    return shift !~ /^[\#\&]/;
}

sub cdr {
    shift;
    return @_;
}

1;
ENDREP
id: 8a.0.r980/5770
type: file
pred: 8a.0.r973/54
count: 16
text: 980 0 5744 6742 ecb77207d815333c3802ceb8b371e0b1
cpath: /trunk/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm
copyroot: 0 /

PLAIN
K 13
Connection.pm
V 19
file 8a.0.r980/5770
END
ENDREP
id: 89.0.r980/6032
type: dir
pred: 89.0.r973/311
count: 16
text: 980 5971 48 48 3c5375a528a056f10943383f8655c327
cpath: /trunk/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC
copyroot: 0 /

PLAIN
K 3
IRC
V 18
dir 89.0.r980/6032
K 6
IRC.pm
V 20
file 8b.0.r949/13729
END
ENDREP
id: 88.0.r980/6304
type: dir
pred: 88.0.r973/580
count: 24
text: 980 6218 73 73 8a449b713b808bd8185a3a68cf1e21b2
cpath: /trunk/owl/perl/modules/IRC/lib/BarnOwl/Module
copyroot: 0 /

PLAIN
K 7
Message
V 17
dir 86.0.r861/942
K 6
Module
V 18
dir 88.0.r980/6304
END
ENDREP
id: 85.0.r980/6573
type: dir
pred: 85.0.r973/846
count: 26
text: 980 6486 74 74 2a1bfa6d9699630d3c23978f9c39ad94
cpath: /trunk/owl/perl/modules/IRC/lib/BarnOwl
copyroot: 0 /

PLAIN
K 7
BarnOwl
V 18
dir 85.0.r980/6573
END
ENDREP
id: 84.0.r980/6801
type: dir
pred: 84.0.r973/1072
count: 26
text: 980 6748 40 40 1d4970b8e0c1b94a06cf426ca07eb3c6
cpath: /trunk/owl/perl/modules/IRC/lib
copyroot: 0 /

PLAIN
K 11
Makefile.PL
V 19
file 83.0.r799/5757
K 3
lib
V 18
dir 84.0.r980/6801
END
ENDREP
id: 82.0.r980/7060
type: dir
pred: 82.0.r973/1331
count: 27
text: 980 6969 78 78 266768b536d8627cc87171d95cecb9bc
props: 890 0 72 0 01f51dd7f7b5d4f3e04c1d64e0d8fadc
cpath: /trunk/owl/perl/modules/IRC
copyroot: 0 /

PLAIN
K 3
IRC
V 18
dir 82.0.r980/7060
K 6
Jabber
V 18
dir 7b.0.r974/1871
END
ENDREP
id: 4c.0.r980/7359
type: dir
pred: 4c.0.r974/2178
count: 113
text: 980 7275 71 71 00a753f4214cd8a0e7011f6fb187b783
cpath: /trunk/owl/perl/modules
copyroot: 0 /

PLAIN
K 3
lib
V 17
dir 4b.0.r976/662
K 7
modules
V 18
dir 4c.0.r980/7359
END
ENDREP
id: 4a.0.r980/7604
type: dir
pred: 4a.0.r976/899
count: 131
text: 980 7520 71 71 d357deb294799539d2df6de1510a3b62
cpath: /trunk/owl/perl
copyroot: 0 /

PLAIN
K 4
BUGS
V 20
file 5u.0.r689/48450
K 9
ChangeLog
V 18
file h.0.r393/2438
K 11
Makefile.in
V 19
file i.0.r949/10952
K 4
NEWS
V 16
file j.0.r5/8251
K 6
README
V 17
file 5r.0.r848/91
K 4
TODO
V 18
file 5s.0.r619/324
K 5
aim.c
V 19
file 22.0.r321/7423
K 9
athstatic
V 18
file 1x.0.r533/117
K 7
buddy.c
V 20
file 3o.0.r234/19726
K 11
buddylist.c
V 20
file 3d.0.r238/12168
K 5
cmd.c
V 18
file k.0.r405/7146
K 11
codelist.pl
V 19
file l.0.r212/11814
K 10
commands.c
V 18
file m.0.r789/3245
K 11
config.h.in
V 18
file n.0.r707/3406
K 12
configure.in
V 17
file p.0.r760/683
K 9
context.c
V 18
file q.0.r209/8754
K 6
debian
V 17
dir 63.0.r772/647
K 6
dict.c
V 19
file r.0.r923/12768
K 3
doc
V 18
dir 1n.0.r589/1227
K 9
editwin.c
V 19
file s.0.r923/13788
K 14
encapsulate.pl
V 20
file 3l.0.r225/44724
K 10
errqueue.c
V 20
file 3p.0.r242/19206
K 8
examples
V 17
dir 1s.0.r226/477
K 8
filter.c
V 19
file t.0.r923/13138
K 15
filterelement.c
V 19
file u.0.r923/12120
K 8
fmtext.c
V 16
file v.0.r927/68
K 11
functions.c
V 19
file w.0.r949/11166
K 8
global.c
V 17
file x.0.r963/521
K 6
help.c
V 17
file y.0.r712/145
K 9
history.c
V 17
file z.0.r283/439
K 10
install.sh
V 19
file 21.0.r122/7916
K 12
keybinding.c
V 18
file 10.0.r640/656
K 8
keymap.c
V 19
file 11.0.r209/8965
K 10
keypress.c
V 18
file 12.0.r359/996
K 6
keys.c
V 18
file 13.0.r937/128
K 7
libfaim
V 16
dir 23.0.r716/87
K 6
list.c
V 17
file 14.0.r635/59
K 9
logging.c
V 20
file 15.0.r923/14217
K 9
mainwin.c
V 20
file 16.0.r693/11667
K 9
message.c
V 18
file 17.0.r956/134
K 13
messagelist.c
V 18
file 18.0.r558/539
K 13
mkinstalldirs
V 19
file 3v.0.r346/1232
K 11
muxevents.c
V 18
file 3w.0.r359/781
K 9
obarray.c
V 20
file 62.0.r923/12978
K 5
owl.c
V 19
file 19.0.r957/1555
K 5
owl.h
V 20
file 1a.0.r949/16591
K 6
pair.c
V 18
file 1z.0.r102/270
K 4
perl
V 18
dir 4a.0.r980/7604
K 12
perlconfig.c
V 20
file 3n.0.r949/16156
K 11
perlglue.xs
V 20
file 1v.0.r949/15939
K 11
perlwrap.pm
V 18
file 3m.0.r978/458
K 9
popexec.c
V 19
file 3x.0.r364/1658
K 8
popwin.c
V 18
file 1b.0.r373/263
K 7
regex.c
V 19
file 1c.0.r597/1404
K 8
select.c
V 18
file 8l.0.r954/747
K 10
stubgen.pl
V 17
file 1d.0.r5/8659
K 7
style.c
V 19
file 3f.0.r280/1095
K 11
stylefunc.c
V 19
file 3g.0.r881/5076
K 10
svkversion
V 18
file 6d.0.r660/834
K 1
t
V 18
dir 7y.0.r738/6796
K 6
test.h
V 19
file 3y.0.r738/5953
K 8
tester.c
V 19
file 1e.0.r738/6378
K 6
text.c
V 17
file 1f.0.r966/48
K 7
timer.c
V 20
file 3e.0.r143/11961
K 6
util.c
V 20
file 1g.0.r923/12554
K 10
variable.c
V 18
file 1h.0.r926/526
K 6
view.c
V 19
file 1i.0.r586/9731
K 9
viewwin.c
V 19
file 1j.0.r362/1349
K 12
zbuddylist.c
V 18
file 3u.0.r330/561
K 8
zcrypt.c
V 20
file 1y.0.r219/11682
K 8
zephyr.c
V 20
file 1k.0.r949/16804
K 8
zwrite.c
V 17
file 1l.0.r744/99
END
ENDREP
id: g.0.r980/10518
type: dir
pred: g.0.r978/3434
count: 772
text: 980 7756 2749 2749 4b090aa37572ac55be0924871087c0d6
props: 960 4804 508 0 000a49fe022dfbf0d61c1b737956e1d2
cpath: /trunk/owl
copyroot: 0 /

PLAIN
K 7
CVSROOT
V 16
dir 4.0.r112/895
K 11
conf.alexmv
V 18
dir 5z.0.r790/1571
K 12
conf.asedeno
V 18
dir 3z.0.r617/2908
K 3
owl
V 18
dir g.0.r980/10518
END
ENDREP
id: 1.0.r980/10890
type: dir
pred: 1.0.r978/3803
count: 805
text: 980 10724 153 153 af6aa4019b337c8cf79906dde8318e5a
props: 808 8881 206 0 fb63802d68de5c3dce8ab7f057f7c25c
cpath: /trunk
copyroot: 0 /

PLAIN
K 8
branches
V 17
dir 2.0.r979/4326
K 4
tags
V 16
dir 3.0.r391/435
K 5
trunk
V 18
dir 1.0.r980/10890
END
ENDREP
id: 0.0.r980/11209
type: dir
pred: 0.0.r979/4591
count: 980
text: 980 11091 105 105 fdbbb9f891944cba802c4a28226c15fa
props: 814 5811 257 0 1bba3ff3b0bec5c44a6f38be0d2645e9
cpath: /
copyroot: 0 /

8a.0.t979-1 modify true false /trunk/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm


11209 11405
