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