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