Amethyst/Connection/IRC.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 132 CC 3 SCAR 10.2
CC
3
Cov
0%
handler_send
6.0
handler_irc_001
6.0
handler_init
2.0
CRAP 2.8
1 package Amethyst::Connection::IRC;
2  
3 use strict;
4 use vars qw(@ISA);
5 use Data::Dumper;
6 use POE;
7 use POE::Component::IRC;
8 use Parse::Lex;
9 use Amethyst::Connection;
10 use Amethyst::Message;
11  
12 @ISA = qw(Amethyst::Connection);
13  
14 sub handler_init {
15 my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
16  
17 print STDERR "Init IRC\n";
18  
19 my @events = qw(
20 irc_connected irc_socketerr irc_001
21 irc_public irc_msg
22 irc_ctcp_action
23 process send
24 );
25 foreach my $event (@events) {
26 $session->register_state($event, __PACKAGE__, "handler_$event");
27 }
28  
29 my $ircalias = $heap->{Args}->{ClientAlias} || 'amethyst irc';
30  
31 my $client = new POE::Component::IRC($ircalias);
32 $heap->{Client} = $client->ID;
33  
34 print STDERR "Started IRC client " . $heap->{Client} . "\n";
35  
36 $kernel->post($heap->{Client}, 'register',
37 qw(001 public msg ctcp_action));
38 }
39  
40 sub handler_connect {
41 my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
42  
43 local *::args = $heap->{Args};
44
45 my $nick = $::args{Nick} || 'Amethyst';
46 my $server = $::args{Server} || 'london.rhizomatic.net';
47 my $port = $::args{Port} || 6667;
48 my $username = $::args{Username} || 'amethyst';
49 my $ircname = $::args{Ircname} || 'Amethyst by Shevek';
50  
51 $kernel->post($heap->{Client}, 'connect', {
52 Nick => $nick,
53 Server => $server,
54 Port => $port,
55 Username => $username,
56 Ircname => $ircname,
57 } );
58 }
59  
60 sub handler_disconnect {
61 my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
62 $kernel->post($heap->{Client}, 'quit', 'Received quit signal');
63 }
64  
65 sub handler_send {
66 my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
67 my @messages = @_[ARG0..$#_];
68 my @out = ();
69  
70 foreach my $message (@messages) {
71 my $channel = $message->channel;
72 $channel =~ s/^#*/#/;
73 # print STDERR "$heap->{Client} PRIVMSG " . $channel
74 # . ": " . $message->content . "\n";
75 if ($message->action == ACT_EMOTE) {
76 # Work out how to do actions!
77 $kernel->post($heap->{Client}, 'ctcp',
78 $channel, "ACTION " . $message->content);
79 }
80 else {
81 $kernel->post($heap->{Client}, 'privmsg',
82 $channel, $message->content);
83 }
84 }
85 }
86  
87 sub handler_irc_connected {
88 my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
89 print STDERR "IRC: Connected to IRC\n";
90  
91 # if ($heap->{Args}->{Channels}) {
92 # my @channels = @{ $heap->{Args}->{Channels} };
93 # foreach my $channel (@channels) {
94 # print STDERR "IRC: Joining $channel\n";
95 # $channel =~ s/^#*/#/;
96 # $kernel->post($heap->{Client}, 'join', $channel);
97 # }
98 # }
99  
100 }
101  
102 sub handler_irc_001 {
103 my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
104 print STDERR "IRC: Got IRC 001\n";
105  
106 if ($heap->{Args}->{Channels}) {
107 my @channels = @{ $heap->{Args}->{Channels} };
108 foreach my $channel (@channels) {
109 print STDERR "IRC: Joining $channel\n";
110 $channel =~ s/^#*/#/;
111 $kernel->post($heap->{Client}, 'join', $channel);
112 }
113 }
114 }
115  
116 sub handler_irc_socketerr {
117 my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
118 }
119  
120 # Unsuprisingly, much of this code needs refactoring.
121  
122 sub handler_process {
123 my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
124 my ($sender, $channel, $action, $text) = @_[ARG0..ARG3];
125  
126 $sender =~ s/!.*//;
127  
128 # IRC doesn't send our messages back to us.
129  
130 my $message = new Amethyst::Message(
131 Connection => $session->ID,
132 Channel => $channel,
133 User => $sender,
134 Action => $action,
135 Content => $text,
136 );
137  
138 $kernel->post($heap->{Amethyst}, 'think', $message,$heap->{Brains});
139 }
140  
141 sub handler_irc_public {
142 my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
143 my ($sender, $channels, $text) = @_[ARG0..ARG2];
144  
145 # print STDERR "$sender: $text\n";
146  
147 $kernel->post($session, 'process',
148 $sender, $channels->[0], ACT_SAY, $text);
149 }
150  
151 sub handler_irc_msg {
152 my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
153 my ($sender, $recips, $text) = @_[ARG0..ARG2];
154  
155 # print STDERR "$sender (private): $text\n";
156  
157 $kernel->post($session, 'process',
158 $sender, CHAN_PRIVATE, ACT_SAY, $text);
159 }
160  
161 sub handler_irc_ctcp_action {
162 my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
163 my ($sender, $channels, $text) = @_[ARG0..ARG2];
164  
165 # print STDERR "$sender (action) $text\n";
166  
167 $kernel->post($session, 'process',
168 $sender, $channels->[0], ACT_EMOTE, $text);
169 }
170  
171 1;