1 | use strict; |
---|
2 | use warnings; |
---|
3 | |
---|
4 | package BarnOwl::Module::IRC; |
---|
5 | |
---|
6 | =head1 NAME |
---|
7 | |
---|
8 | BarnOwl::Module::IRC |
---|
9 | |
---|
10 | =head1 DESCRIPTION |
---|
11 | |
---|
12 | This module implements IRC support for barnowl. |
---|
13 | |
---|
14 | =cut |
---|
15 | |
---|
16 | use BarnOwl; |
---|
17 | use BarnOwl::Hooks; |
---|
18 | use BarnOwl::Message::IRC; |
---|
19 | use BarnOwl::Module::IRC::Connection qw(is_private); |
---|
20 | |
---|
21 | use Net::IRC; |
---|
22 | use Getopt::Long; |
---|
23 | |
---|
24 | our $VERSION = 0.02; |
---|
25 | |
---|
26 | our $irc; |
---|
27 | |
---|
28 | # Hash alias -> BarnOwl::Module::IRC::Connection object |
---|
29 | our %ircnets; |
---|
30 | our %channels; |
---|
31 | |
---|
32 | sub startup { |
---|
33 | BarnOwl::new_variable_string('irc:nick', { |
---|
34 | default => $ENV{USER}, |
---|
35 | summary => 'The default IRC nickname', |
---|
36 | description => 'By default, irc-connect will use this nick ' . |
---|
37 | 'when connecting to a new server. See :help irc-connect for ' . |
---|
38 | 'more information.' |
---|
39 | }); |
---|
40 | |
---|
41 | BarnOwl::new_variable_string('irc:user', { |
---|
42 | default => $ENV{USER}, |
---|
43 | summary => 'The IRC "username" field' |
---|
44 | }); |
---|
45 | BarnOwl::new_variable_string('irc:name', { |
---|
46 | default => "", |
---|
47 | summary => 'A short name field for IRC', |
---|
48 | description => 'A short (maybe 60 or so chars) piece of text, ' . |
---|
49 | 'originally intended to display your real name, which people ' . |
---|
50 | 'often use for pithy quotes and URLs.' |
---|
51 | }); |
---|
52 | |
---|
53 | BarnOwl::new_variable_bool('irc:spew', { |
---|
54 | default => 0, |
---|
55 | summary => 'Show unhandled IRC events', |
---|
56 | description => 'If set, display all unrecognized IRC events as ' . |
---|
57 | 'admin messages. Intended for debugging and development use only ' |
---|
58 | }); |
---|
59 | |
---|
60 | register_commands(); |
---|
61 | register_handlers(); |
---|
62 | BarnOwl::filter('irc type ^IRC$'); |
---|
63 | } |
---|
64 | |
---|
65 | sub shutdown { |
---|
66 | for my $conn (values %ircnets) { |
---|
67 | $conn->conn->disconnect(); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | #sub mainloop_hook { |
---|
72 | # return unless defined $irc; |
---|
73 | # eval { |
---|
74 | # $irc->do_one_loop(); |
---|
75 | # }; |
---|
76 | # return; |
---|
77 | #} |
---|
78 | |
---|
79 | sub OwlProcess { |
---|
80 | return unless defined $irc; |
---|
81 | eval { |
---|
82 | $irc->do_one_loop(); |
---|
83 | }; |
---|
84 | return; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | sub register_handlers { |
---|
89 | if(!$irc) { |
---|
90 | $irc = Net::IRC->new; |
---|
91 | $irc->timeout(0); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | sub register_commands { |
---|
96 | BarnOwl::new_command('irc-connect' => \&cmd_connect, |
---|
97 | { |
---|
98 | summary => 'Connect to an IRC server', |
---|
99 | usage => 'irc-connect [-a ALIAS ] [-s] [-p PASSWORD] [-n NICK] SERVER [port]', |
---|
100 | description => |
---|
101 | |
---|
102 | "Connect to an IRC server. Supported options are\n\n" . |
---|
103 | "-a <alias> Define an alias for this server\n" . |
---|
104 | "-s Use SSL\n" . |
---|
105 | "-p <password> Specify the password to use\n" . |
---|
106 | "-n <nick> Use a non-default nick" |
---|
107 | }); |
---|
108 | BarnOwl::new_command('irc-disconnect' => \&cmd_disconnect); |
---|
109 | BarnOwl::new_command('irc-msg' => \&cmd_msg); |
---|
110 | BarnOwl::new_command('irc-join' => \&cmd_join); |
---|
111 | BarnOwl::new_command('irc-part' => \&cmd_part); |
---|
112 | BarnOwl::new_command('irc-nick' => \&cmd_nick); |
---|
113 | BarnOwl::new_command('irc-names' => \&cmd_names); |
---|
114 | BarnOwl::new_command('irc-whois' => \&cmd_whois); |
---|
115 | BarnOwl::new_command('irc-motd' => \&cmd_motd); |
---|
116 | BarnOwl::new_command('irc-list' => \&cmd_list); |
---|
117 | BarnOwl::new_command('irc-who' => \&cmd_who); |
---|
118 | BarnOwl::new_command('irc-stats' => \&cmd_stats); |
---|
119 | BarnOwl::new_command('irc-topic' => \&cmd_topic); |
---|
120 | } |
---|
121 | |
---|
122 | $BarnOwl::Hooks::startup->add('BarnOwl::Module::IRC::startup'); |
---|
123 | $BarnOwl::Hooks::shutdown->add('BarnOwl::Module::IRC::shutdown'); |
---|
124 | |
---|
125 | ################################################################################ |
---|
126 | ######################## Owl command handlers ################################## |
---|
127 | ################################################################################ |
---|
128 | |
---|
129 | sub cmd_connect { |
---|
130 | my $cmd = shift; |
---|
131 | |
---|
132 | my $nick = BarnOwl::getvar('irc:nick'); |
---|
133 | my $username = BarnOwl::getvar('irc:user'); |
---|
134 | my $ircname = BarnOwl::getvar('irc:name'); |
---|
135 | my $host; |
---|
136 | my $port; |
---|
137 | my $alias; |
---|
138 | my $ssl; |
---|
139 | my $password = undef; |
---|
140 | |
---|
141 | { |
---|
142 | local @ARGV = @_; |
---|
143 | GetOptions( |
---|
144 | "alias=s" => \$alias, |
---|
145 | "ssl" => \$ssl, |
---|
146 | "password=s" => \$password, |
---|
147 | "nick=s" => \$nick, |
---|
148 | ); |
---|
149 | $host = shift @ARGV or die("Usage: $cmd HOST\n"); |
---|
150 | if(!$alias) { |
---|
151 | if($host =~ /^(?:irc[.])?([\w-]+)[.]\w+$/) { |
---|
152 | $alias = $1; |
---|
153 | } else { |
---|
154 | $alias = $host; |
---|
155 | } |
---|
156 | } |
---|
157 | $ssl ||= 0; |
---|
158 | $port = shift @ARGV || ($ssl ? 6697 : 6667); |
---|
159 | } |
---|
160 | |
---|
161 | if(exists $ircnets{$alias}) { |
---|
162 | die("Already connected to a server with alias '$alias'. Either disconnect or specify an alias with -a.\n"); |
---|
163 | } |
---|
164 | |
---|
165 | my $conn = BarnOwl::Module::IRC::Connection->new($irc, $alias, |
---|
166 | Nick => $nick, |
---|
167 | Server => $host, |
---|
168 | Port => $port, |
---|
169 | Username => $username, |
---|
170 | Ircname => $ircname, |
---|
171 | Port => $port, |
---|
172 | Password => $password, |
---|
173 | SSL => $ssl |
---|
174 | ); |
---|
175 | |
---|
176 | if ($conn->conn->connected) { |
---|
177 | BarnOwl::admin_message("IRC", "Connected to $alias as $nick"); |
---|
178 | $ircnets{$alias} = $conn; |
---|
179 | my $fd = $conn->getSocket()->fileno(); |
---|
180 | BarnOwl::add_dispatch($fd, \&OwlProcess); |
---|
181 | $conn->{FD} = $fd; |
---|
182 | } else { |
---|
183 | die("IRC::Connection->connect failed: $!"); |
---|
184 | } |
---|
185 | |
---|
186 | return; |
---|
187 | } |
---|
188 | |
---|
189 | sub cmd_disconnect { |
---|
190 | my $cmd = shift; |
---|
191 | my $conn = get_connection(\@_); |
---|
192 | $conn->conn->disconnect; |
---|
193 | delete $ircnets{$conn->alias}; |
---|
194 | } |
---|
195 | |
---|
196 | sub cmd_msg { |
---|
197 | my $cmd = shift; |
---|
198 | my $conn = get_connection(\@_); |
---|
199 | my $to = shift or die("Usage: $cmd NICK\n"); |
---|
200 | # handle multiple recipients? |
---|
201 | if(@_) { |
---|
202 | process_msg($conn, $to, join(" ", @_)); |
---|
203 | } else { |
---|
204 | BarnOwl::start_edit_win("/msg -a " . $conn->alias . " $to", sub {process_msg($conn, $to, @_)}); |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | sub process_msg { |
---|
209 | my $conn = shift; |
---|
210 | my $to = shift; |
---|
211 | my $body = shift; |
---|
212 | # Strip whitespace. In the future -- send one message/line? |
---|
213 | $body =~ tr/\n\r/ /; |
---|
214 | if ($body =~ /^\/me (.*)/) { |
---|
215 | $conn->conn->me($to, $1); |
---|
216 | $body = '* '.$conn->nick.' '.$1; |
---|
217 | } else { |
---|
218 | $conn->conn->privmsg($to, $body); |
---|
219 | } |
---|
220 | my $msg = BarnOwl::Message->new( |
---|
221 | type => 'IRC', |
---|
222 | direction => is_private($to) ? 'out' : 'in', |
---|
223 | server => $conn->server, |
---|
224 | network => $conn->alias, |
---|
225 | recipient => $to, |
---|
226 | body => $body, |
---|
227 | sender => $conn->nick, |
---|
228 | is_private($to) ? |
---|
229 | (isprivate => 'true') : (channel => $to), |
---|
230 | replycmd => "irc-msg -a " . $conn->alias . " $to", |
---|
231 | replysendercmd => "irc-msg -a " . $conn->alias . " $to" |
---|
232 | ); |
---|
233 | BarnOwl::queue_message($msg); |
---|
234 | } |
---|
235 | |
---|
236 | sub cmd_join { |
---|
237 | my $cmd = shift; |
---|
238 | my $conn = get_connection(\@_); |
---|
239 | my $chan = shift or die("Usage: $cmd channel\n"); |
---|
240 | $channels{$chan} ||= []; |
---|
241 | push @{$channels{$chan}}, $conn; |
---|
242 | $conn->conn->join($chan); |
---|
243 | } |
---|
244 | |
---|
245 | sub cmd_part { |
---|
246 | my $cmd = shift; |
---|
247 | my $conn = get_connection(\@_); |
---|
248 | my $chan = get_channel(\@_) || die("Usage: $cmd <channel>\n"); |
---|
249 | $channels{$chan} = [grep {$_ ne $conn} @{$channels{$chan} || []}]; |
---|
250 | $conn->conn->part($chan); |
---|
251 | } |
---|
252 | |
---|
253 | sub cmd_nick { |
---|
254 | my $cmd = shift; |
---|
255 | my $conn = get_connection(\@_); |
---|
256 | my $nick = shift or die("Usage: $cmd <new nick>\n"); |
---|
257 | $conn->conn->nick($nick); |
---|
258 | } |
---|
259 | |
---|
260 | sub cmd_names { |
---|
261 | my $cmd = shift; |
---|
262 | my $conn = get_connection(\@_); |
---|
263 | my $chan = get_channel(\@_) || die("Usage: $cmd <channel>\n"); |
---|
264 | $conn->names_tmp([]); |
---|
265 | $conn->conn->names($chan); |
---|
266 | } |
---|
267 | |
---|
268 | sub cmd_whois { |
---|
269 | my $cmd = shift; |
---|
270 | my $conn = get_connection(\@_); |
---|
271 | my $who = shift || die("Usage: $cmd <user>\n"); |
---|
272 | $conn->conn->whois($who); |
---|
273 | } |
---|
274 | |
---|
275 | sub cmd_motd { |
---|
276 | my $cmd = shift; |
---|
277 | my $conn = get_connection(\@_); |
---|
278 | $conn->conn->motd; |
---|
279 | } |
---|
280 | |
---|
281 | sub cmd_list { |
---|
282 | my $cmd = shift; |
---|
283 | my $message = BarnOwl::Style::boldify('Current IRC networks:') . "\n"; |
---|
284 | while (my ($alias, $conn) = each %ircnets) { |
---|
285 | $message .= ' ' . $alias . ' => ' . $conn->nick . '@' . $conn->server . "\n"; |
---|
286 | } |
---|
287 | BarnOwl::popless_ztext($message); |
---|
288 | } |
---|
289 | |
---|
290 | sub cmd_who { |
---|
291 | my $cmd = shift; |
---|
292 | my $conn = get_connection(\@_); |
---|
293 | my $who = shift || die("Usage: $cmd <user>\n"); |
---|
294 | $conn->conn->who($who); |
---|
295 | } |
---|
296 | |
---|
297 | sub cmd_stats { |
---|
298 | my $cmd = shift; |
---|
299 | my $conn = get_connection(\@_); |
---|
300 | my $type = shift || die("Usage: $cmd <chiklmouy> [server] \n"); |
---|
301 | $conn->conn->stats($type, @_); |
---|
302 | } |
---|
303 | |
---|
304 | sub cmd_topic { |
---|
305 | my $cmd = shift; |
---|
306 | my $conn = get_connection(\@_); |
---|
307 | $conn->conn->topic(@_); |
---|
308 | } |
---|
309 | |
---|
310 | ################################################################################ |
---|
311 | ########################### Utilities/Helpers ################################## |
---|
312 | ################################################################################ |
---|
313 | |
---|
314 | sub get_connection { |
---|
315 | my $args = shift; |
---|
316 | if(scalar @$args >= 2 && $args->[0] eq '-a') { |
---|
317 | shift @$args; |
---|
318 | return get_connection_by_alias(shift @$args); |
---|
319 | } |
---|
320 | my $channel = $args->[-1]; |
---|
321 | if (defined($channel) && $channel =~ /^#/ |
---|
322 | and $channels{$channel} and @{$channels{$channel}} == 1) { |
---|
323 | return $channels{$channel}[0]; |
---|
324 | } |
---|
325 | my $m = BarnOwl::getcurmsg(); |
---|
326 | if($m && $m->type eq 'IRC') { |
---|
327 | return get_connection_by_alias($m->network); |
---|
328 | } |
---|
329 | if(scalar keys %ircnets == 1) { |
---|
330 | return [values(%ircnets)]->[0]; |
---|
331 | } |
---|
332 | die("You must specify a network with -a\n"); |
---|
333 | } |
---|
334 | |
---|
335 | sub get_channel { |
---|
336 | my $args = shift; |
---|
337 | if(scalar @$args) { |
---|
338 | return shift @$args; |
---|
339 | } |
---|
340 | my $m = BarnOwl::getcurmsg(); |
---|
341 | if($m && $m->type eq 'IRC') { |
---|
342 | return $m->channel if !$m->is_private; |
---|
343 | } |
---|
344 | return undef; |
---|
345 | } |
---|
346 | |
---|
347 | sub get_connection_by_alias { |
---|
348 | my $key = shift; |
---|
349 | die("No such ircnet: $key\n") unless exists $ircnets{$key}; |
---|
350 | return $ircnets{$key}; |
---|
351 | } |
---|
352 | |
---|
353 | 1; |
---|