1 | use strict; |
---|
2 | use warnings; |
---|
3 | |
---|
4 | package BarnOwl::Module::IRC::Connection; |
---|
5 | |
---|
6 | =head1 NAME |
---|
7 | |
---|
8 | BarnOwl::Module::IRC::Connection |
---|
9 | |
---|
10 | =head1 DESCRIPTION |
---|
11 | |
---|
12 | This module is a wrapper around Net::IRC::Connection for BarnOwl's IRC |
---|
13 | support |
---|
14 | |
---|
15 | =cut |
---|
16 | |
---|
17 | use Net::IRC::Connection; |
---|
18 | |
---|
19 | use base qw(Class::Accessor Exporter); |
---|
20 | __PACKAGE__->mk_accessors(qw(conn alias channels connected motd)); |
---|
21 | our @EXPORT_OK = qw(&is_private); |
---|
22 | |
---|
23 | use BarnOwl; |
---|
24 | |
---|
25 | BEGIN { |
---|
26 | no strict 'refs'; |
---|
27 | my @delegate = qw(nick server); |
---|
28 | for my $meth (@delegate) { |
---|
29 | *{"BarnOwl::Module::IRC::Connection::$meth"} = sub { |
---|
30 | shift->conn->$meth(@_); |
---|
31 | } |
---|
32 | } |
---|
33 | }; |
---|
34 | |
---|
35 | sub new { |
---|
36 | my $class = shift; |
---|
37 | my $irc = shift; |
---|
38 | my $alias = shift; |
---|
39 | my %args = (@_); |
---|
40 | my $conn = Net::IRC::Connection->new($irc, %args); |
---|
41 | my $self = bless({}, $class); |
---|
42 | $self->conn($conn); |
---|
43 | $self->alias($alias); |
---|
44 | $self->channels([]); |
---|
45 | $self->motd(""); |
---|
46 | $self->connected(0); |
---|
47 | |
---|
48 | $self->conn->add_handler(376 => sub { shift; $self->on_connect(@_) }); |
---|
49 | $self->conn->add_default_handler(sub { shift; $self->on_event(@_) }); |
---|
50 | $self->conn->add_handler(['msg', 'notice', 'public', 'caction'], |
---|
51 | sub { shift; $self->on_msg(@_) }); |
---|
52 | $self->conn->add_handler(['welcome', 'yourhost', 'created', |
---|
53 | 'luserclient', 'luserop', 'luserchannels', 'luserme', |
---|
54 | 'notice', 'error'], |
---|
55 | sub { shift; $self->on_admin_msg(@_) }); |
---|
56 | $self->conn->add_handler(['myinfo', 'map', 'n_local', 'n_global', |
---|
57 | 'luserconns'], |
---|
58 | sub { }); |
---|
59 | $self->conn->add_handler(motdstart => sub { shift; $self->on_motdstart(@_) }); |
---|
60 | $self->conn->add_handler(motd => sub { shift; $self->on_motd(@_) }); |
---|
61 | $self->conn->add_handler(endofmotd => sub { shift; $self->on_endofmotd(@_) }); |
---|
62 | $self->conn->add_handler(join => sub { shift; $self->on_join(@_) }); |
---|
63 | $self->conn->add_handler(part => sub { shift; $self->on_part(@_) }); |
---|
64 | $self->conn->add_handler(disconnect => sub { shift; $self->on_disconnect(@_) }); |
---|
65 | $self->conn->add_handler(nicknameinuse => sub { shift; $self->on_nickinuse(@_) }); |
---|
66 | $self->conn->add_handler(cping => sub { shift; $self->on_ping(@_) }); |
---|
67 | |
---|
68 | return $self; |
---|
69 | } |
---|
70 | |
---|
71 | sub getSocket |
---|
72 | { |
---|
73 | my $self = shift; |
---|
74 | return $self->conn->socket; |
---|
75 | } |
---|
76 | |
---|
77 | ################################################################################ |
---|
78 | ############################### IRC callbacks ################################## |
---|
79 | ################################################################################ |
---|
80 | |
---|
81 | sub new_message { |
---|
82 | my $self = shift; |
---|
83 | my $evt = shift; |
---|
84 | return BarnOwl::Message->new( |
---|
85 | type => 'IRC', |
---|
86 | server => $self->server, |
---|
87 | network => $self->alias, |
---|
88 | sender => $evt->nick, |
---|
89 | hostname => $evt->host, |
---|
90 | from => $evt->from, |
---|
91 | @_ |
---|
92 | ); |
---|
93 | } |
---|
94 | |
---|
95 | sub on_msg { |
---|
96 | my ($self, $evt) = @_; |
---|
97 | my ($recipient) = $evt->to; |
---|
98 | my $body = strip_irc_formatting([$evt->args]->[0]); |
---|
99 | my $nick = $self->nick; |
---|
100 | BarnOwl::beep() if $body =~ /\b\Q$nick\E\b/; |
---|
101 | $body = BarnOwl::Style::boldify($evt->nick.' '.$body) if $evt->type eq 'caction'; |
---|
102 | my $msg = $self->new_message($evt, |
---|
103 | direction => 'in', |
---|
104 | recipient => $recipient, |
---|
105 | body => $body, |
---|
106 | $evt->type eq 'notice' ? |
---|
107 | (notice => 'true') : (), |
---|
108 | is_private($recipient) ? |
---|
109 | (isprivate => 'true') : (channel => $recipient), |
---|
110 | replycmd => 'irc-msg ' . |
---|
111 | (is_private($recipient) ? $evt->nick : $recipient), |
---|
112 | replysendercmd => 'irc-msg ' . $evt->nick |
---|
113 | ); |
---|
114 | |
---|
115 | BarnOwl::queue_message($msg); |
---|
116 | } |
---|
117 | |
---|
118 | sub on_ping { |
---|
119 | my ($self, $evt) = @_; |
---|
120 | $self->conn->ctcp_reply($evt->nick, join (' ', ($evt->args))); |
---|
121 | } |
---|
122 | |
---|
123 | sub on_admin_msg { |
---|
124 | my ($self, $evt) = @_; |
---|
125 | BarnOwl::admin_message("IRC", |
---|
126 | BarnOwl::Style::boldify('IRC ' . $evt->type . ' message from ' |
---|
127 | . $self->alias) . "\n" |
---|
128 | . strip_irc_formatting(join '\n', cdr $evt->args)); |
---|
129 | } |
---|
130 | |
---|
131 | sub on_motdstart { |
---|
132 | my ($self, $evt) = @_; |
---|
133 | $self->motd(join "\n", cdr $evt->args); |
---|
134 | } |
---|
135 | |
---|
136 | sub on_motd { |
---|
137 | my ($self, $evt) = @_; |
---|
138 | $self->motd(join "\n", $self->motd, cdr $evt->args); |
---|
139 | } |
---|
140 | |
---|
141 | sub on_endofmotd { |
---|
142 | my ($self, $evt) = @_; |
---|
143 | $self->motd(join "\n", $self->motd, cdr $evt->args); |
---|
144 | if(!$self->connected) { |
---|
145 | BarnOwl::admin_message("IRC", "Connected to " . |
---|
146 | $self->server . " (" . $self->alias . ")"); |
---|
147 | $self->connected(1); |
---|
148 | |
---|
149 | } |
---|
150 | BarnOwl::admin_message("IRC", |
---|
151 | BarnOwl::Style::boldify('MOTD for ' . $self->alias) . "\n" |
---|
152 | . strip_irc_formatting($self->motd)); |
---|
153 | } |
---|
154 | |
---|
155 | sub on_join { |
---|
156 | my ($self, $evt) = @_; |
---|
157 | my $msg = $self->new_message($evt, |
---|
158 | loginout => 'login', |
---|
159 | channel => $evt->to, |
---|
160 | ); |
---|
161 | BarnOwl::queue_message($msg); |
---|
162 | } |
---|
163 | |
---|
164 | sub on_part { |
---|
165 | my ($self, $evt) = @_; |
---|
166 | my $msg = $self->new_message($evt, |
---|
167 | loginout => 'logout', |
---|
168 | channel => $evt->to, |
---|
169 | ); |
---|
170 | BarnOwl::queue_message($msg); |
---|
171 | } |
---|
172 | |
---|
173 | sub on_disconnect { |
---|
174 | my $self = shift; |
---|
175 | delete $BarnOwl::Module::IRC::ircnets{$self->alias}; |
---|
176 | BarnOwl::remove_dispatch($self->{FD}); |
---|
177 | BarnOwl::admin_message('IRC', |
---|
178 | "[" . $self->alias . "] Disconnected from server"); |
---|
179 | } |
---|
180 | |
---|
181 | sub on_nickinuse { |
---|
182 | my ($self, $evt) = @_; |
---|
183 | BarnOwl::admin_message("IRC", |
---|
184 | "[" . $self->alias . "] " . |
---|
185 | [$evt->args]->[1] . ": Nick already in use"); |
---|
186 | unless($self->connected) { |
---|
187 | $self->conn->disconnect; |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | sub on_event { |
---|
192 | my ($self, $evt) = @_; |
---|
193 | BarnOwl::admin_message("IRC", |
---|
194 | "[" . $self->alias . "] Unhandled IRC event of type " . $evt->type . ":\n" |
---|
195 | . strip_irc_formatting(join("\n", $evt->args))) |
---|
196 | if BarnOwl::getvar('irc:spew') eq 'on'; |
---|
197 | } |
---|
198 | |
---|
199 | |
---|
200 | ################################################################################ |
---|
201 | ########################### Utilities/Helpers ################################## |
---|
202 | ################################################################################ |
---|
203 | |
---|
204 | sub strip_irc_formatting { |
---|
205 | my $body = shift; |
---|
206 | # Strip mIRC colors. If someone wants to write code to convert |
---|
207 | # these to zephyr colors, be my guest. |
---|
208 | $body =~ s/\cC\d+(?:,\d+)?//g; |
---|
209 | $body =~ s/\cO//g; |
---|
210 | |
---|
211 | my @pieces = split /\cB/, $body; |
---|
212 | my $out; |
---|
213 | while(@pieces) { |
---|
214 | $out .= shift @pieces; |
---|
215 | $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces; |
---|
216 | } |
---|
217 | return $out; |
---|
218 | } |
---|
219 | |
---|
220 | # Determines if the given message recipient is a username, as opposed to |
---|
221 | # a channel that starts with # or &. |
---|
222 | sub is_private { |
---|
223 | return shift !~ /^[\#\&]/; |
---|
224 | } |
---|
225 | |
---|
226 | sub cdr { |
---|
227 | shift; |
---|
228 | return @_; |
---|
229 | } |
---|
230 | |
---|
231 | 1; |
---|