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 quickstart { |
---|
72 | return <<'END_QUICKSTART'; |
---|
73 | @b[IRC:] |
---|
74 | Use ':irc-connect @b[server]' to connect to an IRC server, and |
---|
75 | ':irc-join @b[#channel]' to join a channel. ':irc-msg @b[#channel] |
---|
76 | @b[message]' sends a message to a channel. |
---|
77 | END_QUICKSTART |
---|
78 | } |
---|
79 | |
---|
80 | #sub mainloop_hook { |
---|
81 | # return unless defined $irc; |
---|
82 | # eval { |
---|
83 | # $irc->do_one_loop(); |
---|
84 | # }; |
---|
85 | # return; |
---|
86 | #} |
---|
87 | |
---|
88 | sub OwlProcess { |
---|
89 | return unless defined $irc; |
---|
90 | eval { |
---|
91 | $irc->do_one_loop(); |
---|
92 | }; |
---|
93 | return; |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | sub register_handlers { |
---|
98 | if(!$irc) { |
---|
99 | $irc = Net::IRC->new; |
---|
100 | $irc->timeout(0); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | use constant OPTIONAL_CHANNEL => 1; |
---|
105 | use constant REQUIRE_CHANNEL => 2; |
---|
106 | |
---|
107 | sub register_commands { |
---|
108 | BarnOwl::new_command( |
---|
109 | 'irc-connect' => \&cmd_connect, |
---|
110 | { |
---|
111 | summary => 'Connect to an IRC server', |
---|
112 | usage => |
---|
113 | 'irc-connect [-a ALIAS ] [-s] [-p PASSWORD] [-n NICK] SERVER [port]', |
---|
114 | description => |
---|
115 | |
---|
116 | <<END_DESCR |
---|
117 | Connect to an IRC server. Supported options are |
---|
118 | |
---|
119 | -a <alias> Define an alias for this server |
---|
120 | -s Use SSL |
---|
121 | -p <password> Specify the password to use |
---|
122 | -n <nick> Use a non-default nick |
---|
123 | |
---|
124 | The -a option specifies an alias to use for this connection. This |
---|
125 | alias can be passed to the '-a' argument of any other IRC command to |
---|
126 | control which connection it operates on. |
---|
127 | |
---|
128 | For servers with hostnames of the form "irc.FOO.{com,org,...}", the |
---|
129 | alias will default to "FOO"; For other servers the full hostname is |
---|
130 | used. |
---|
131 | END_DESCR |
---|
132 | } |
---|
133 | ); |
---|
134 | |
---|
135 | BarnOwl::new_command( |
---|
136 | 'irc-disconnect' => mk_irc_command( \&cmd_disconnect ), |
---|
137 | { |
---|
138 | summary => 'Disconnect from an IRC server', |
---|
139 | usage => 'irc-disconnect [-a ALIAS]', |
---|
140 | |
---|
141 | description => <<END_DESCR |
---|
142 | Disconnect from an IRC server. You can specify a specific server with |
---|
143 | "-a SERVER-ALIAS" if necessary. |
---|
144 | END_DESCR |
---|
145 | } |
---|
146 | ); |
---|
147 | |
---|
148 | BarnOwl::new_command( |
---|
149 | 'irc-msg' => mk_irc_command( \&cmd_msg ), |
---|
150 | { |
---|
151 | summary => 'Send an IRC message', |
---|
152 | usage => 'irc-msg [-a ALIAS] DESTINATION MESSAGE', |
---|
153 | |
---|
154 | description => <<END_DESCR |
---|
155 | Send an IRC message |
---|
156 | END_DESCR |
---|
157 | } |
---|
158 | ); |
---|
159 | |
---|
160 | BarnOwl::new_command( |
---|
161 | 'irc-mode' => mk_irc_command( \&cmd_mode, OPTIONAL_CHANNEL ), |
---|
162 | { |
---|
163 | summary => 'Change an IRC channel or user mode', |
---|
164 | usage => 'irc-mode [-a ALIAS] TARGET [+-]MODE OPTIONS', |
---|
165 | |
---|
166 | description => <<END_DESCR |
---|
167 | |
---|
168 | Change the mode of an IRC user or channel. |
---|
169 | END_DESCR |
---|
170 | } |
---|
171 | ); |
---|
172 | |
---|
173 | BarnOwl::new_command( |
---|
174 | 'irc-join' => mk_irc_command( \&cmd_join ), |
---|
175 | { |
---|
176 | summary => 'Join an IRC channel', |
---|
177 | usage => 'irc-join [-a ALIAS] #channel [KEY]', |
---|
178 | |
---|
179 | description => <<END_DESCR |
---|
180 | |
---|
181 | Join an IRC channel. |
---|
182 | END_DESCR |
---|
183 | } |
---|
184 | ); |
---|
185 | |
---|
186 | BarnOwl::new_command( |
---|
187 | 'irc-part' => mk_irc_command( \&cmd_part, REQUIRE_CHANNEL ), |
---|
188 | { |
---|
189 | summary => 'Leave an IRC channel', |
---|
190 | usage => 'irc-part [-a ALIAS] #channel', |
---|
191 | |
---|
192 | description => <<END_DESCR |
---|
193 | |
---|
194 | Part from an IRC channel. |
---|
195 | END_DESCR |
---|
196 | } |
---|
197 | ); |
---|
198 | |
---|
199 | BarnOwl::new_command( |
---|
200 | 'irc-nick' => mk_irc_command( \&cmd_nick ), |
---|
201 | { |
---|
202 | summary => 'Change your IRC nick on an existing connection.', |
---|
203 | usage => 'irc-nick [-a ALIAS] NEW-NICK', |
---|
204 | |
---|
205 | description => <<END_DESCR |
---|
206 | |
---|
207 | Set your IRC nickname on an existing connect. To change it prior to |
---|
208 | connecting, adjust the `irc:nick' variable. |
---|
209 | END_DESCR |
---|
210 | } |
---|
211 | ); |
---|
212 | |
---|
213 | BarnOwl::new_command( |
---|
214 | 'irc-names' => mk_irc_command( \&cmd_names, REQUIRE_CHANNEL ), |
---|
215 | { |
---|
216 | summary => 'View the list of users in a channel', |
---|
217 | usage => 'irc-names [-a ALIAS] #channel', |
---|
218 | |
---|
219 | description => <<END_DESCR |
---|
220 | |
---|
221 | `irc-names' displays the list of users in a given channel in a pop-up |
---|
222 | window. |
---|
223 | END_DESCR |
---|
224 | } |
---|
225 | ); |
---|
226 | |
---|
227 | BarnOwl::new_command( |
---|
228 | 'irc-whois' => mk_irc_command( \&cmd_whois ), |
---|
229 | { |
---|
230 | summary => 'Displays information about a given IRC user', |
---|
231 | usage => 'irc-whois [-a ALIAS] NICK', |
---|
232 | |
---|
233 | description => <<END_DESCR |
---|
234 | |
---|
235 | Pops up information about a given IRC user. |
---|
236 | END_DESCR |
---|
237 | } |
---|
238 | ); |
---|
239 | |
---|
240 | BarnOwl::new_command( |
---|
241 | 'irc-motd' => mk_irc_command( \&cmd_motd ), |
---|
242 | { |
---|
243 | summary => 'Displays an IRC server\'s MOTD (Message of the Day)', |
---|
244 | usage => 'irc-motd [-a ALIAS]', |
---|
245 | |
---|
246 | description => <<END_DESCR |
---|
247 | |
---|
248 | Displays an IRC server's message of the day. |
---|
249 | END_DESCR |
---|
250 | } |
---|
251 | ); |
---|
252 | |
---|
253 | BarnOwl::new_command( |
---|
254 | 'irc-list' => \&cmd_list, |
---|
255 | { |
---|
256 | summary => 'Show all the active IRC connections.', |
---|
257 | usage => 'irc-list', |
---|
258 | |
---|
259 | description => <<END_DESCR |
---|
260 | |
---|
261 | Show all the currently active IRC connections with their aliases and |
---|
262 | server names. |
---|
263 | END_DESCR |
---|
264 | } |
---|
265 | ); |
---|
266 | |
---|
267 | BarnOwl::new_command( 'irc-who' => mk_irc_command( \&cmd_who ) ); |
---|
268 | BarnOwl::new_command( 'irc-stats' => mk_irc_command( \&cmd_stats ) ); |
---|
269 | |
---|
270 | BarnOwl::new_command( |
---|
271 | 'irc-topic' => mk_irc_command( \&cmd_topic, REQUIRE_CHANNEL ), |
---|
272 | { |
---|
273 | summary => 'View or change the topic of an IRC channel', |
---|
274 | usage => 'irc-topic [-a ALIAS] #channel [TOPIC]', |
---|
275 | |
---|
276 | description => <<END_DESCR |
---|
277 | |
---|
278 | Without extra arguments, fetches and displays a given channel's topic. |
---|
279 | |
---|
280 | With extra arguments, changes the target channel's topic string. This |
---|
281 | may require +o on some channels. |
---|
282 | END_DESCR |
---|
283 | } |
---|
284 | ); |
---|
285 | |
---|
286 | BarnOwl::new_command( |
---|
287 | 'irc-quote' => mk_irc_command( \&cmd_quote ), |
---|
288 | { |
---|
289 | summary => 'Send a raw command to the IRC servers.', |
---|
290 | usage => 'irc-quote [-a ALIAS] TEXT', |
---|
291 | |
---|
292 | description => <<END_DESCR |
---|
293 | |
---|
294 | Send a raw command line to an IRC server. |
---|
295 | |
---|
296 | This can be used to perform some operation not yet supported by |
---|
297 | BarnOwl, or to define new IRC commands. |
---|
298 | END_DESCR |
---|
299 | } |
---|
300 | ); |
---|
301 | } |
---|
302 | |
---|
303 | |
---|
304 | $BarnOwl::Hooks::startup->add('BarnOwl::Module::IRC::startup'); |
---|
305 | $BarnOwl::Hooks::shutdown->add('BarnOwl::Module::IRC::shutdown'); |
---|
306 | $BarnOwl::Hooks::getQuickstart->add('BarnOwl::Module::IRC::quickstart'); |
---|
307 | |
---|
308 | ################################################################################ |
---|
309 | ######################## Owl command handlers ################################## |
---|
310 | ################################################################################ |
---|
311 | |
---|
312 | sub cmd_connect { |
---|
313 | my $cmd = shift; |
---|
314 | |
---|
315 | my $nick = BarnOwl::getvar('irc:nick'); |
---|
316 | my $username = BarnOwl::getvar('irc:user'); |
---|
317 | my $ircname = BarnOwl::getvar('irc:name'); |
---|
318 | my $host; |
---|
319 | my $port; |
---|
320 | my $alias; |
---|
321 | my $ssl; |
---|
322 | my $password = undef; |
---|
323 | |
---|
324 | { |
---|
325 | local @ARGV = @_; |
---|
326 | GetOptions( |
---|
327 | "alias=s" => \$alias, |
---|
328 | "ssl" => \$ssl, |
---|
329 | "password=s" => \$password, |
---|
330 | "nick=s" => \$nick, |
---|
331 | ); |
---|
332 | $host = shift @ARGV or die("Usage: $cmd HOST\n"); |
---|
333 | if(!$alias) { |
---|
334 | if($host =~ /^(?:irc[.])?([\w-]+)[.]\w+$/) { |
---|
335 | $alias = $1; |
---|
336 | } else { |
---|
337 | $alias = $host; |
---|
338 | } |
---|
339 | } |
---|
340 | $ssl ||= 0; |
---|
341 | $port = shift @ARGV || ($ssl ? 6697 : 6667); |
---|
342 | } |
---|
343 | |
---|
344 | if(exists $ircnets{$alias}) { |
---|
345 | die("Already connected to a server with alias '$alias'. Either disconnect or specify an alias with -a.\n"); |
---|
346 | } |
---|
347 | |
---|
348 | my $conn = BarnOwl::Module::IRC::Connection->new($irc, $alias, |
---|
349 | Nick => $nick, |
---|
350 | Server => $host, |
---|
351 | Port => $port, |
---|
352 | Username => $username, |
---|
353 | Ircname => $ircname, |
---|
354 | Port => $port, |
---|
355 | Password => $password, |
---|
356 | SSL => $ssl |
---|
357 | ); |
---|
358 | |
---|
359 | if ($conn->conn->connected) { |
---|
360 | BarnOwl::admin_message("IRC", "Connected to $alias as $nick"); |
---|
361 | $ircnets{$alias} = $conn; |
---|
362 | my $fd = $conn->getSocket()->fileno(); |
---|
363 | BarnOwl::add_dispatch($fd, \&OwlProcess); |
---|
364 | $conn->{FD} = $fd; |
---|
365 | } else { |
---|
366 | die("IRC::Connection->connect failed: $!"); |
---|
367 | } |
---|
368 | |
---|
369 | return; |
---|
370 | } |
---|
371 | |
---|
372 | sub cmd_disconnect { |
---|
373 | my $cmd = shift; |
---|
374 | my $conn = shift; |
---|
375 | $conn->conn->disconnect; |
---|
376 | delete $ircnets{$conn->alias}; |
---|
377 | } |
---|
378 | |
---|
379 | sub cmd_msg { |
---|
380 | my $cmd = shift; |
---|
381 | my $conn = shift; |
---|
382 | my $to = shift or die("Usage: $cmd [NICK|CHANNEL]\n"); |
---|
383 | # handle multiple recipients? |
---|
384 | if(@_) { |
---|
385 | process_msg($conn, $to, join(" ", @_)); |
---|
386 | } else { |
---|
387 | BarnOwl::start_edit_win("/msg -a " . $conn->alias . " $to", sub {process_msg($conn, $to, @_)}); |
---|
388 | } |
---|
389 | } |
---|
390 | |
---|
391 | sub process_msg { |
---|
392 | my $conn = shift; |
---|
393 | my $to = shift; |
---|
394 | my $body = shift; |
---|
395 | # Strip whitespace. In the future -- send one message/line? |
---|
396 | $body =~ tr/\n\r/ /; |
---|
397 | if ($body =~ /^\/me (.*)/) { |
---|
398 | $conn->conn->me($to, $1); |
---|
399 | $body = '* '.$conn->nick.' '.$1; |
---|
400 | } else { |
---|
401 | $conn->conn->privmsg($to, $body); |
---|
402 | } |
---|
403 | my $msg = BarnOwl::Message->new( |
---|
404 | type => 'IRC', |
---|
405 | direction => is_private($to) ? 'out' : 'in', |
---|
406 | server => $conn->server, |
---|
407 | network => $conn->alias, |
---|
408 | recipient => $to, |
---|
409 | body => $body, |
---|
410 | sender => $conn->nick, |
---|
411 | is_private($to) ? |
---|
412 | (isprivate => 'true') : (channel => $to), |
---|
413 | replycmd => "irc-msg -a " . $conn->alias . " $to", |
---|
414 | replysendercmd => "irc-msg -a " . $conn->alias . " $to" |
---|
415 | ); |
---|
416 | BarnOwl::queue_message($msg); |
---|
417 | } |
---|
418 | |
---|
419 | sub cmd_mode { |
---|
420 | my $cmd = shift; |
---|
421 | my $conn = shift; |
---|
422 | my $target = shift; |
---|
423 | $target ||= shift; |
---|
424 | $conn->conn->mode($target, @_); |
---|
425 | } |
---|
426 | |
---|
427 | sub cmd_join { |
---|
428 | my $cmd = shift; |
---|
429 | my $conn = shift; |
---|
430 | my $chan = shift or die("Usage: $cmd channel\n"); |
---|
431 | $channels{$chan} ||= []; |
---|
432 | push @{$channels{$chan}}, $conn; |
---|
433 | $conn->conn->join($chan, @_); |
---|
434 | } |
---|
435 | |
---|
436 | sub cmd_part { |
---|
437 | my $cmd = shift; |
---|
438 | my $conn = shift; |
---|
439 | my $chan = shift; |
---|
440 | $channels{$chan} = [grep {$_ ne $conn} @{$channels{$chan} || []}]; |
---|
441 | $conn->conn->part($chan); |
---|
442 | } |
---|
443 | |
---|
444 | sub cmd_nick { |
---|
445 | my $cmd = shift; |
---|
446 | my $conn = shift; |
---|
447 | my $nick = shift or die("Usage: $cmd <new nick>\n"); |
---|
448 | $conn->conn->nick($nick); |
---|
449 | } |
---|
450 | |
---|
451 | sub cmd_names { |
---|
452 | my $cmd = shift; |
---|
453 | my $conn = shift; |
---|
454 | my $chan = shift; |
---|
455 | $conn->names_tmp([]); |
---|
456 | $conn->conn->names($chan); |
---|
457 | } |
---|
458 | |
---|
459 | sub cmd_whois { |
---|
460 | my $cmd = shift; |
---|
461 | my $conn = shift; |
---|
462 | my $who = shift || die("Usage: $cmd <user>\n"); |
---|
463 | $conn->conn->whois($who); |
---|
464 | } |
---|
465 | |
---|
466 | sub cmd_motd { |
---|
467 | my $cmd = shift; |
---|
468 | my $conn = shift; |
---|
469 | $conn->conn->motd; |
---|
470 | } |
---|
471 | |
---|
472 | sub cmd_list { |
---|
473 | my $cmd = shift; |
---|
474 | my $message = BarnOwl::Style::boldify('Current IRC networks:') . "\n"; |
---|
475 | while (my ($alias, $conn) = each %ircnets) { |
---|
476 | $message .= ' ' . $alias . ' => ' . $conn->nick . '@' . $conn->server . "\n"; |
---|
477 | } |
---|
478 | BarnOwl::popless_ztext($message); |
---|
479 | } |
---|
480 | |
---|
481 | sub cmd_who { |
---|
482 | my $cmd = shift; |
---|
483 | my $conn = shift; |
---|
484 | my $who = shift || die("Usage: $cmd <user>\n"); |
---|
485 | BarnOwl::error("WHO $cmd $conn $who"); |
---|
486 | $conn->conn->who($who); |
---|
487 | } |
---|
488 | |
---|
489 | sub cmd_stats { |
---|
490 | my $cmd = shift; |
---|
491 | my $conn = shift; |
---|
492 | my $type = shift || die("Usage: $cmd <chiklmouy> [server] \n"); |
---|
493 | $conn->conn->stats($type, @_); |
---|
494 | } |
---|
495 | |
---|
496 | sub cmd_topic { |
---|
497 | my $cmd = shift; |
---|
498 | my $conn = shift; |
---|
499 | my $chan = shift; |
---|
500 | $conn->conn->topic($chan, @_ ? join(" ", @_) : undef); |
---|
501 | } |
---|
502 | |
---|
503 | sub cmd_quote { |
---|
504 | my $cmd = shift; |
---|
505 | my $conn = shift; |
---|
506 | $conn->conn->sl(join(" ", @_)); |
---|
507 | } |
---|
508 | |
---|
509 | ################################################################################ |
---|
510 | ########################### Utilities/Helpers ################################## |
---|
511 | ################################################################################ |
---|
512 | |
---|
513 | sub mk_irc_command { |
---|
514 | my $sub = shift; |
---|
515 | my $use_channel = shift || 0; |
---|
516 | return sub { |
---|
517 | my $cmd = shift; |
---|
518 | my $conn; |
---|
519 | my $alias; |
---|
520 | my $channel; |
---|
521 | my $getopt = Getopt::Long::Parser->new; |
---|
522 | my $m = BarnOwl::getcurmsg(); |
---|
523 | |
---|
524 | local @ARGV = @_; |
---|
525 | $getopt->configure(qw(pass_through permute no_getopt_compat prefix_pattern=-|--)); |
---|
526 | $getopt->getoptions("alias=s" => \$alias); |
---|
527 | |
---|
528 | if(defined($alias)) { |
---|
529 | $conn = get_connection_by_alias($alias); |
---|
530 | } |
---|
531 | if(!$conn && $use_channel) { |
---|
532 | $channel = $ARGV[0]; |
---|
533 | if(defined($channel) && $channel =~ /^#/) { |
---|
534 | if($channels{$channel} && @{$channels{$channel}} == 1) { |
---|
535 | shift @ARGV; |
---|
536 | $conn = $channels{$channel}[0]; |
---|
537 | } |
---|
538 | } else { |
---|
539 | if($m && $m->type eq 'IRC' && !$m->is_private) { |
---|
540 | $channel = $m->channel; |
---|
541 | } else { |
---|
542 | undef $channel; |
---|
543 | } |
---|
544 | } |
---|
545 | } |
---|
546 | if(!$channel && $use_channel == REQUIRE_CHANNEL) { |
---|
547 | die("Usage: $cmd <channel>\n"); |
---|
548 | } |
---|
549 | if(!$conn) { |
---|
550 | if($m && $m->type eq 'IRC') { |
---|
551 | $conn = get_connection_by_alias($m->network); |
---|
552 | } |
---|
553 | } |
---|
554 | if(!$conn && scalar keys %ircnets == 1) { |
---|
555 | $conn = [values(%ircnets)]->[0]; |
---|
556 | } |
---|
557 | if(!$conn) { |
---|
558 | die("You must specify an IRC network using -a.\n"); |
---|
559 | } |
---|
560 | if($use_channel) { |
---|
561 | $sub->($cmd, $conn, $channel, @ARGV); |
---|
562 | } else { |
---|
563 | $sub->($cmd, $conn, @ARGV); |
---|
564 | } |
---|
565 | }; |
---|
566 | } |
---|
567 | |
---|
568 | sub get_connection_by_alias { |
---|
569 | my $key = shift; |
---|
570 | die("No such ircnet: $key\n") unless exists $ircnets{$key}; |
---|
571 | return $ircnets{$key}; |
---|
572 | } |
---|
573 | |
---|
574 | 1; |
---|