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