[b38b0b2] | 1 | use strict; |
---|
| 2 | use warnings; |
---|
| 3 | |
---|
| 4 | package BarnOwl::Module::IRC; |
---|
| 5 | |
---|
| 6 | =head1 NAME |
---|
| 7 | |
---|
[2c40dc0] | 8 | BarnOwl::Module::IRC |
---|
[b38b0b2] | 9 | |
---|
| 10 | =head1 DESCRIPTION |
---|
| 11 | |
---|
[b8a3e00] | 12 | This module implements IRC support for BarnOwl. |
---|
[b38b0b2] | 13 | |
---|
| 14 | =cut |
---|
| 15 | |
---|
| 16 | use BarnOwl; |
---|
| 17 | use BarnOwl::Hooks; |
---|
| 18 | use BarnOwl::Message::IRC; |
---|
[380b1ab] | 19 | use BarnOwl::Module::IRC::Connection qw(is_private); |
---|
[ab9cd8f] | 20 | use BarnOwl::Module::IRC::Completion; |
---|
[b38b0b2] | 21 | |
---|
[8ba9313] | 22 | use AnyEvent::IRC; |
---|
[b38b0b2] | 23 | use Getopt::Long; |
---|
[9620c8d] | 24 | use Encode; |
---|
[b38b0b2] | 25 | |
---|
[2c40dc0] | 26 | our $VERSION = 0.02; |
---|
[b38b0b2] | 27 | |
---|
| 28 | our $irc; |
---|
| 29 | |
---|
| 30 | # Hash alias -> BarnOwl::Module::IRC::Connection object |
---|
| 31 | our %ircnets; |
---|
| 32 | |
---|
| 33 | sub startup { |
---|
[b10f340] | 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 | }); |
---|
[cd12307] | 53 | |
---|
[b10f340] | 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 ' . |
---|
[cd12307] | 58 | 'admin messages. Intended for debugging and development use only.' |
---|
[b10f340] | 59 | }); |
---|
[f81176c] | 60 | |
---|
| 61 | BarnOwl::new_variable_string('irc:skip', { |
---|
| 62 | default => 'welcome yourhost created ' . |
---|
| 63 | 'luserclient luserme luserop luserchannels', |
---|
| 64 | summary => 'Skip messages of these types', |
---|
| 65 | description => 'If set, each (space-separated) message type ' . |
---|
| 66 | 'provided will be hidden and ignored if received.' |
---|
| 67 | }); |
---|
| 68 | |
---|
[b38b0b2] | 69 | register_commands(); |
---|
[96f7b07] | 70 | BarnOwl::filter(qw{irc type ^IRC$ or ( type ^admin$ and adminheader ^IRC$ )}); |
---|
[b38b0b2] | 71 | } |
---|
| 72 | |
---|
| 73 | sub shutdown { |
---|
| 74 | for my $conn (values %ircnets) { |
---|
[8ba9313] | 75 | $conn->conn->disconnect('Quitting'); |
---|
[b38b0b2] | 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
[f17bb2c0] | 79 | sub quickstart { |
---|
| 80 | return <<'END_QUICKSTART'; |
---|
| 81 | @b[IRC:] |
---|
| 82 | Use ':irc-connect @b[server]' to connect to an IRC server, and |
---|
| 83 | ':irc-join @b[#channel]' to join a channel. ':irc-msg @b[#channel] |
---|
| 84 | @b[message]' sends a message to a channel. |
---|
| 85 | END_QUICKSTART |
---|
| 86 | } |
---|
| 87 | |
---|
[da554da] | 88 | sub buddylist { |
---|
| 89 | my $list = ""; |
---|
| 90 | |
---|
| 91 | for my $net (sort keys %ircnets) { |
---|
| 92 | my $conn = $ircnets{$net}; |
---|
| 93 | my ($nick, $server) = ($conn->nick, $conn->server); |
---|
[6396c1e] | 94 | $list .= BarnOwl::Style::boldify("IRC channels for $net ($nick\@$server)"); |
---|
| 95 | $list .= "\n"; |
---|
[da554da] | 96 | |
---|
[dace02a] | 97 | for my $chan (keys %{$conn->conn->{channel_list}}) { |
---|
[da554da] | 98 | $list .= " $chan\n"; |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | return $list; |
---|
| 103 | } |
---|
| 104 | |
---|
[f81176c] | 105 | sub skip_msg { |
---|
| 106 | my $class = shift; |
---|
| 107 | my $type = lc shift; |
---|
| 108 | my $skip = lc BarnOwl::getvar('irc:skip'); |
---|
| 109 | return grep {$_ eq $type} split ' ', $skip; |
---|
| 110 | } |
---|
| 111 | |
---|
[54b4a87] | 112 | =head2 mk_irc_command SUB FLAGS |
---|
| 113 | |
---|
| 114 | Return a subroutine that can be bound as a an IRC command. The |
---|
| 115 | subroutine will be called with arguments (COMMAND-NAME, |
---|
| 116 | IRC-CONNECTION, [CHANNEL], ARGV...). |
---|
| 117 | |
---|
| 118 | C<IRC-CONNECTION> and C<CHANNEL> will be inferred from arguments to |
---|
| 119 | the command and the current message if appropriate. |
---|
| 120 | |
---|
| 121 | The bitwise C<or> of zero or more C<FLAGS> can be passed in as a |
---|
| 122 | second argument to alter the behavior of the returned commands: |
---|
| 123 | |
---|
| 124 | =over 4 |
---|
| 125 | |
---|
| 126 | =item C<CHANNEL_ARG> |
---|
| 127 | |
---|
| 128 | This command accepts the name of a channel. Pass in the C<CHANNEL> |
---|
| 129 | argument listed above, and die if no channel argument can be found. |
---|
| 130 | |
---|
| 131 | =item C<CHANNEL_OPTIONAL> |
---|
| 132 | |
---|
| 133 | Pass the channel argument, but don't die if not present. Only relevant |
---|
| 134 | with C<CHANNEL_ARG>. |
---|
| 135 | |
---|
[8a22ff2] | 136 | =item C<CHANNEL_EXPLICIT> |
---|
| 137 | |
---|
| 138 | Do not determine the channel from the current message. Only relevant |
---|
| 139 | with C<CHANNEL_ARG>. |
---|
| 140 | |
---|
[416241f] | 141 | =item C<ALLOW_DISCONNECTED> |
---|
| 142 | |
---|
| 143 | C<IRC-CONNECTION> may be a disconnected connection object that is |
---|
| 144 | currently pending a reconnect. |
---|
| 145 | |
---|
[54b4a87] | 146 | =back |
---|
| 147 | |
---|
| 148 | =cut |
---|
| 149 | |
---|
| 150 | use constant CHANNEL_ARG => 1; |
---|
| 151 | use constant CHANNEL_OPTIONAL => 2; |
---|
[8a22ff2] | 152 | use constant CHANNEL_EXPLICIT => 4; |
---|
[330c55a] | 153 | |
---|
[8a22ff2] | 154 | use constant ALLOW_DISCONNECTED => 8; |
---|
[416241f] | 155 | |
---|
[b38b0b2] | 156 | sub register_commands { |
---|
[f17bb2c0] | 157 | BarnOwl::new_command( |
---|
| 158 | 'irc-connect' => \&cmd_connect, |
---|
| 159 | { |
---|
| 160 | summary => 'Connect to an IRC server', |
---|
| 161 | usage => |
---|
| 162 | 'irc-connect [-a ALIAS ] [-s] [-p PASSWORD] [-n NICK] SERVER [port]', |
---|
[cd12307] | 163 | description => <<END_DESCR |
---|
| 164 | Connect to an IRC server. Supported options are: |
---|
[f17bb2c0] | 165 | |
---|
| 166 | -a <alias> Define an alias for this server |
---|
| 167 | -s Use SSL |
---|
| 168 | -p <password> Specify the password to use |
---|
| 169 | -n <nick> Use a non-default nick |
---|
| 170 | |
---|
| 171 | The -a option specifies an alias to use for this connection. This |
---|
| 172 | alias can be passed to the '-a' argument of any other IRC command to |
---|
| 173 | control which connection it operates on. |
---|
| 174 | |
---|
| 175 | For servers with hostnames of the form "irc.FOO.{com,org,...}", the |
---|
| 176 | alias will default to "FOO"; For other servers the full hostname is |
---|
| 177 | used. |
---|
| 178 | END_DESCR |
---|
| 179 | } |
---|
| 180 | ); |
---|
| 181 | |
---|
| 182 | BarnOwl::new_command( |
---|
[416241f] | 183 | 'irc-disconnect' => mk_irc_command( \&cmd_disconnect, ALLOW_DISCONNECTED ), |
---|
[f17bb2c0] | 184 | { |
---|
| 185 | summary => 'Disconnect from an IRC server', |
---|
| 186 | usage => 'irc-disconnect [-a ALIAS]', |
---|
| 187 | |
---|
| 188 | description => <<END_DESCR |
---|
| 189 | Disconnect from an IRC server. You can specify a specific server with |
---|
| 190 | "-a SERVER-ALIAS" if necessary. |
---|
| 191 | END_DESCR |
---|
| 192 | } |
---|
| 193 | ); |
---|
| 194 | |
---|
| 195 | BarnOwl::new_command( |
---|
[8a22ff2] | 196 | 'irc-msg' => mk_irc_command( \&cmd_msg, CHANNEL_ARG|CHANNEL_OPTIONAL|CHANNEL_EXPLICIT ), |
---|
[f17bb2c0] | 197 | { |
---|
| 198 | summary => 'Send an IRC message', |
---|
[8a22ff2] | 199 | usage => 'irc-msg [-a ALIAS] DESTINATION [MESSAGE]', |
---|
[f17bb2c0] | 200 | |
---|
| 201 | description => <<END_DESCR |
---|
[cd12307] | 202 | Send an IRC message. |
---|
[f17bb2c0] | 203 | END_DESCR |
---|
| 204 | } |
---|
| 205 | ); |
---|
| 206 | |
---|
| 207 | BarnOwl::new_command( |
---|
[54b4a87] | 208 | 'irc-mode' => mk_irc_command( \&cmd_mode, CHANNEL_OPTIONAL|CHANNEL_ARG ), |
---|
[f17bb2c0] | 209 | { |
---|
| 210 | summary => 'Change an IRC channel or user mode', |
---|
| 211 | usage => 'irc-mode [-a ALIAS] TARGET [+-]MODE OPTIONS', |
---|
| 212 | |
---|
| 213 | description => <<END_DESCR |
---|
| 214 | Change the mode of an IRC user or channel. |
---|
| 215 | END_DESCR |
---|
| 216 | } |
---|
| 217 | ); |
---|
| 218 | |
---|
| 219 | BarnOwl::new_command( |
---|
| 220 | 'irc-join' => mk_irc_command( \&cmd_join ), |
---|
| 221 | { |
---|
| 222 | summary => 'Join an IRC channel', |
---|
[1b62a55] | 223 | usage => 'irc-join [-a ALIAS] #channel [KEY]', |
---|
[f17bb2c0] | 224 | |
---|
| 225 | description => <<END_DESCR |
---|
| 226 | Join an IRC channel. |
---|
| 227 | END_DESCR |
---|
| 228 | } |
---|
| 229 | ); |
---|
| 230 | |
---|
| 231 | BarnOwl::new_command( |
---|
[54b4a87] | 232 | 'irc-part' => mk_irc_command( \&cmd_part, CHANNEL_ARG ), |
---|
[f17bb2c0] | 233 | { |
---|
| 234 | summary => 'Leave an IRC channel', |
---|
| 235 | usage => 'irc-part [-a ALIAS] #channel', |
---|
| 236 | |
---|
| 237 | description => <<END_DESCR |
---|
| 238 | Part from an IRC channel. |
---|
| 239 | END_DESCR |
---|
| 240 | } |
---|
| 241 | ); |
---|
| 242 | |
---|
| 243 | BarnOwl::new_command( |
---|
| 244 | 'irc-nick' => mk_irc_command( \&cmd_nick ), |
---|
| 245 | { |
---|
| 246 | summary => 'Change your IRC nick on an existing connection.', |
---|
| 247 | usage => 'irc-nick [-a ALIAS] NEW-NICK', |
---|
| 248 | |
---|
| 249 | description => <<END_DESCR |
---|
| 250 | Set your IRC nickname on an existing connect. To change it prior to |
---|
| 251 | connecting, adjust the `irc:nick' variable. |
---|
| 252 | END_DESCR |
---|
| 253 | } |
---|
| 254 | ); |
---|
| 255 | |
---|
| 256 | BarnOwl::new_command( |
---|
[54b4a87] | 257 | 'irc-names' => mk_irc_command( \&cmd_names, CHANNEL_ARG ), |
---|
[f17bb2c0] | 258 | { |
---|
| 259 | summary => 'View the list of users in a channel', |
---|
| 260 | usage => 'irc-names [-a ALIAS] #channel', |
---|
| 261 | |
---|
| 262 | description => <<END_DESCR |
---|
| 263 | `irc-names' displays the list of users in a given channel in a pop-up |
---|
| 264 | window. |
---|
| 265 | END_DESCR |
---|
| 266 | } |
---|
| 267 | ); |
---|
| 268 | |
---|
| 269 | BarnOwl::new_command( |
---|
| 270 | 'irc-whois' => mk_irc_command( \&cmd_whois ), |
---|
| 271 | { |
---|
| 272 | summary => 'Displays information about a given IRC user', |
---|
| 273 | usage => 'irc-whois [-a ALIAS] NICK', |
---|
| 274 | |
---|
| 275 | description => <<END_DESCR |
---|
| 276 | Pops up information about a given IRC user. |
---|
| 277 | END_DESCR |
---|
| 278 | } |
---|
| 279 | ); |
---|
| 280 | |
---|
| 281 | BarnOwl::new_command( |
---|
| 282 | 'irc-motd' => mk_irc_command( \&cmd_motd ), |
---|
| 283 | { |
---|
| 284 | summary => 'Displays an IRC server\'s MOTD (Message of the Day)', |
---|
| 285 | usage => 'irc-motd [-a ALIAS]', |
---|
| 286 | |
---|
| 287 | description => <<END_DESCR |
---|
| 288 | Displays an IRC server's message of the day. |
---|
| 289 | END_DESCR |
---|
| 290 | } |
---|
| 291 | ); |
---|
| 292 | |
---|
| 293 | BarnOwl::new_command( |
---|
| 294 | 'irc-list' => \&cmd_list, |
---|
| 295 | { |
---|
| 296 | summary => 'Show all the active IRC connections.', |
---|
| 297 | usage => 'irc-list', |
---|
| 298 | |
---|
| 299 | description => <<END_DESCR |
---|
| 300 | Show all the currently active IRC connections with their aliases and |
---|
| 301 | server names. |
---|
| 302 | END_DESCR |
---|
| 303 | } |
---|
| 304 | ); |
---|
| 305 | |
---|
| 306 | BarnOwl::new_command( 'irc-who' => mk_irc_command( \&cmd_who ) ); |
---|
| 307 | BarnOwl::new_command( 'irc-stats' => mk_irc_command( \&cmd_stats ) ); |
---|
| 308 | |
---|
| 309 | BarnOwl::new_command( |
---|
[54b4a87] | 310 | 'irc-topic' => mk_irc_command( \&cmd_topic, CHANNEL_ARG ), |
---|
[f17bb2c0] | 311 | { |
---|
| 312 | summary => 'View or change the topic of an IRC channel', |
---|
| 313 | usage => 'irc-topic [-a ALIAS] #channel [TOPIC]', |
---|
| 314 | |
---|
| 315 | description => <<END_DESCR |
---|
| 316 | Without extra arguments, fetches and displays a given channel's topic. |
---|
| 317 | |
---|
| 318 | With extra arguments, changes the target channel's topic string. This |
---|
| 319 | may require +o on some channels. |
---|
| 320 | END_DESCR |
---|
| 321 | } |
---|
| 322 | ); |
---|
| 323 | |
---|
| 324 | BarnOwl::new_command( |
---|
| 325 | 'irc-quote' => mk_irc_command( \&cmd_quote ), |
---|
| 326 | { |
---|
| 327 | summary => 'Send a raw command to the IRC servers.', |
---|
| 328 | usage => 'irc-quote [-a ALIAS] TEXT', |
---|
| 329 | |
---|
| 330 | description => <<END_DESCR |
---|
| 331 | Send a raw command line to an IRC server. |
---|
| 332 | |
---|
| 333 | This can be used to perform some operation not yet supported by |
---|
| 334 | BarnOwl, or to define new IRC commands. |
---|
| 335 | END_DESCR |
---|
| 336 | } |
---|
| 337 | ); |
---|
[b38b0b2] | 338 | } |
---|
| 339 | |
---|
[f17bb2c0] | 340 | |
---|
[167044b] | 341 | $BarnOwl::Hooks::startup->add('BarnOwl::Module::IRC::startup'); |
---|
| 342 | $BarnOwl::Hooks::shutdown->add('BarnOwl::Module::IRC::shutdown'); |
---|
[f17bb2c0] | 343 | $BarnOwl::Hooks::getQuickstart->add('BarnOwl::Module::IRC::quickstart'); |
---|
[da554da] | 344 | $BarnOwl::Hooks::getBuddyList->add("BarnOwl::Module::IRC::buddylist"); |
---|
[b38b0b2] | 345 | |
---|
| 346 | ################################################################################ |
---|
| 347 | ######################## Owl command handlers ################################## |
---|
| 348 | ################################################################################ |
---|
| 349 | |
---|
| 350 | sub cmd_connect { |
---|
| 351 | my $cmd = shift; |
---|
| 352 | |
---|
[2c40dc0] | 353 | my $nick = BarnOwl::getvar('irc:nick'); |
---|
| 354 | my $username = BarnOwl::getvar('irc:user'); |
---|
| 355 | my $ircname = BarnOwl::getvar('irc:name'); |
---|
[b38b0b2] | 356 | my $host; |
---|
| 357 | my $port; |
---|
| 358 | my $alias; |
---|
| 359 | my $ssl; |
---|
| 360 | my $password = undef; |
---|
| 361 | |
---|
| 362 | { |
---|
| 363 | local @ARGV = @_; |
---|
| 364 | GetOptions( |
---|
| 365 | "alias=s" => \$alias, |
---|
| 366 | "ssl" => \$ssl, |
---|
[2c40dc0] | 367 | "password=s" => \$password, |
---|
[b10f340] | 368 | "nick=s" => \$nick, |
---|
[2c40dc0] | 369 | ); |
---|
[b38b0b2] | 370 | $host = shift @ARGV or die("Usage: $cmd HOST\n"); |
---|
| 371 | if(!$alias) { |
---|
[f094fc4] | 372 | if($host =~ /^(?:irc[.])?([\w-]+)[.]\w+$/) { |
---|
[b0c8011] | 373 | $alias = $1; |
---|
| 374 | } else { |
---|
| 375 | $alias = $host; |
---|
| 376 | } |
---|
[b38b0b2] | 377 | } |
---|
| 378 | $ssl ||= 0; |
---|
[f094fc4] | 379 | $port = shift @ARGV || ($ssl ? 6697 : 6667); |
---|
[b38b0b2] | 380 | } |
---|
| 381 | |
---|
[b0c8011] | 382 | if(exists $ircnets{$alias}) { |
---|
| 383 | die("Already connected to a server with alias '$alias'. Either disconnect or specify an alias with -a.\n"); |
---|
| 384 | } |
---|
| 385 | |
---|
[8ba9313] | 386 | my $conn = BarnOwl::Module::IRC::Connection->new($alias, $host, $port, { |
---|
| 387 | nick => $nick, |
---|
| 388 | user => $username, |
---|
| 389 | real => $ircname, |
---|
| 390 | password => $password, |
---|
| 391 | SSL => $ssl, |
---|
| 392 | timeout => sub {0} |
---|
| 393 | }); |
---|
[851a0e0] | 394 | $ircnets{$alias} = $conn; |
---|
[b38b0b2] | 395 | return; |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | sub cmd_disconnect { |
---|
[ac374fc] | 399 | my $cmd = shift; |
---|
| 400 | my $conn = shift; |
---|
[851a0e0] | 401 | if ($conn->conn->{socket}) { |
---|
[5c6d661] | 402 | $conn->did_quit(1); |
---|
[8ba9313] | 403 | $conn->conn->disconnect("Goodbye!"); |
---|
[3713b86] | 404 | } elsif ($conn->{reconnect_timer}) { |
---|
[416241f] | 405 | BarnOwl::admin_message('IRC', |
---|
| 406 | "[" . $conn->alias . "] Reconnect cancelled"); |
---|
| 407 | $conn->cancel_reconnect; |
---|
[3713b86] | 408 | delete $ircnets{$conn->alias}; |
---|
[416241f] | 409 | } |
---|
[b38b0b2] | 410 | } |
---|
| 411 | |
---|
| 412 | sub cmd_msg { |
---|
[330c55a] | 413 | my $cmd = shift; |
---|
| 414 | my $conn = shift; |
---|
[8a22ff2] | 415 | my $to = shift || shift or die("Usage: $cmd [NICK|CHANNEL]\n"); |
---|
[2c40dc0] | 416 | # handle multiple recipients? |
---|
[b38b0b2] | 417 | if(@_) { |
---|
| 418 | process_msg($conn, $to, join(" ", @_)); |
---|
| 419 | } else { |
---|
[744769e] | 420 | BarnOwl::start_edit_win(BarnOwl::quote('/msg', '-a', $conn->alias, $to), sub {process_msg($conn, $to, @_)}); |
---|
[b38b0b2] | 421 | } |
---|
[48f7d12] | 422 | return; |
---|
[b38b0b2] | 423 | } |
---|
| 424 | |
---|
| 425 | sub process_msg { |
---|
| 426 | my $conn = shift; |
---|
| 427 | my $to = shift; |
---|
[9a023d0] | 428 | my $fullbody = shift; |
---|
| 429 | my @msgs; |
---|
| 430 | # Require the user to send in paragraphs (double-newline between) to |
---|
| 431 | # actually send multiple PRIVMSGs, in order to play nice with autofill. |
---|
| 432 | $fullbody =~ s/\r//g; |
---|
| 433 | @msgs = split "\n\n", $fullbody; |
---|
| 434 | map { tr/\n/ / } @msgs; |
---|
| 435 | for my $body (@msgs) { |
---|
| 436 | if ($body =~ /^\/me (.*)/) { |
---|
[8ba9313] | 437 | $conn->me($to, Encode::encode('utf-8', $1)); |
---|
[9a023d0] | 438 | $body = '* '.$conn->nick.' '.$1; |
---|
| 439 | } else { |
---|
[8ba9313] | 440 | $conn->conn->send_msg('privmsg', $to, Encode::encode('utf-8', $body)); |
---|
[9a023d0] | 441 | } |
---|
| 442 | my $msg = BarnOwl::Message->new( |
---|
| 443 | type => 'IRC', |
---|
| 444 | direction => is_private($to) ? 'out' : 'in', |
---|
| 445 | server => $conn->server, |
---|
| 446 | network => $conn->alias, |
---|
| 447 | recipient => $to, |
---|
| 448 | body => $body, |
---|
| 449 | sender => $conn->nick, |
---|
| 450 | is_private($to) ? |
---|
| 451 | (isprivate => 'true') : (channel => $to), |
---|
| 452 | replycmd => BarnOwl::quote('irc-msg', '-a', $conn->alias, $to), |
---|
| 453 | replysendercmd => BarnOwl::quote('irc-msg', '-a', $conn->alias, $to), |
---|
| 454 | ); |
---|
| 455 | BarnOwl::queue_message($msg); |
---|
[919535f] | 456 | } |
---|
[48f7d12] | 457 | return; |
---|
[b38b0b2] | 458 | } |
---|
| 459 | |
---|
[e625b5e] | 460 | sub cmd_mode { |
---|
| 461 | my $cmd = shift; |
---|
| 462 | my $conn = shift; |
---|
| 463 | my $target = shift; |
---|
| 464 | $target ||= shift; |
---|
[8ba9313] | 465 | $conn->conn->send_msg(mode => $target, @_); |
---|
[48f7d12] | 466 | return; |
---|
[e625b5e] | 467 | } |
---|
| 468 | |
---|
[2c40dc0] | 469 | sub cmd_join { |
---|
| 470 | my $cmd = shift; |
---|
[330c55a] | 471 | my $conn = shift; |
---|
[2c40dc0] | 472 | my $chan = shift or die("Usage: $cmd channel\n"); |
---|
[8ba9313] | 473 | $conn->conn->send_msg(join => $chan, @_); |
---|
[48f7d12] | 474 | return; |
---|
[2c40dc0] | 475 | } |
---|
[b38b0b2] | 476 | |
---|
[6858d2d] | 477 | sub cmd_part { |
---|
| 478 | my $cmd = shift; |
---|
[330c55a] | 479 | my $conn = shift; |
---|
| 480 | my $chan = shift; |
---|
[8ba9313] | 481 | $conn->conn->send_msg(part => $chan); |
---|
[48f7d12] | 482 | return; |
---|
[6858d2d] | 483 | } |
---|
| 484 | |
---|
[6286f26] | 485 | sub cmd_nick { |
---|
| 486 | my $cmd = shift; |
---|
[330c55a] | 487 | my $conn = shift; |
---|
[b0c8011] | 488 | my $nick = shift or die("Usage: $cmd <new nick>\n"); |
---|
[8ba9313] | 489 | $conn->conn->send_msg(nick => $nick); |
---|
[48f7d12] | 490 | return; |
---|
[6286f26] | 491 | } |
---|
| 492 | |
---|
[6858d2d] | 493 | sub cmd_names { |
---|
| 494 | my $cmd = shift; |
---|
[330c55a] | 495 | my $conn = shift; |
---|
| 496 | my $chan = shift; |
---|
[d264c6d] | 497 | $conn->names_tmp([]); |
---|
[8ba9313] | 498 | $conn->conn->send_msg(names => $chan); |
---|
[48f7d12] | 499 | return; |
---|
[6858d2d] | 500 | } |
---|
| 501 | |
---|
[b0c8011] | 502 | sub cmd_whois { |
---|
| 503 | my $cmd = shift; |
---|
[330c55a] | 504 | my $conn = shift; |
---|
[b0c8011] | 505 | my $who = shift || die("Usage: $cmd <user>\n"); |
---|
[8ba9313] | 506 | $conn->conn->send_msg(whois => $who); |
---|
[48f7d12] | 507 | return; |
---|
[b0c8011] | 508 | } |
---|
| 509 | |
---|
[56e72d5] | 510 | sub cmd_motd { |
---|
| 511 | my $cmd = shift; |
---|
[330c55a] | 512 | my $conn = shift; |
---|
[8ba9313] | 513 | $conn->conn->send_msg('motd'); |
---|
[48f7d12] | 514 | return; |
---|
[56e72d5] | 515 | } |
---|
| 516 | |
---|
[f094fc4] | 517 | sub cmd_list { |
---|
| 518 | my $cmd = shift; |
---|
| 519 | my $message = BarnOwl::Style::boldify('Current IRC networks:') . "\n"; |
---|
| 520 | while (my ($alias, $conn) = each %ircnets) { |
---|
| 521 | $message .= ' ' . $alias . ' => ' . $conn->nick . '@' . $conn->server . "\n"; |
---|
| 522 | } |
---|
| 523 | BarnOwl::popless_ztext($message); |
---|
[48f7d12] | 524 | return; |
---|
[f094fc4] | 525 | } |
---|
| 526 | |
---|
| 527 | sub cmd_who { |
---|
| 528 | my $cmd = shift; |
---|
[330c55a] | 529 | my $conn = shift; |
---|
[f094fc4] | 530 | my $who = shift || die("Usage: $cmd <user>\n"); |
---|
[8ba9313] | 531 | $conn->conn->send_msg(who => $who); |
---|
[48f7d12] | 532 | return; |
---|
[f094fc4] | 533 | } |
---|
| 534 | |
---|
| 535 | sub cmd_stats { |
---|
| 536 | my $cmd = shift; |
---|
[330c55a] | 537 | my $conn = shift; |
---|
[f094fc4] | 538 | my $type = shift || die("Usage: $cmd <chiklmouy> [server] \n"); |
---|
[8ba9313] | 539 | $conn->conn->send_msg(stats => $type, @_); |
---|
[48f7d12] | 540 | return; |
---|
[f094fc4] | 541 | } |
---|
| 542 | |
---|
[3ad15ff] | 543 | sub cmd_topic { |
---|
| 544 | my $cmd = shift; |
---|
[330c55a] | 545 | my $conn = shift; |
---|
| 546 | my $chan = shift; |
---|
[8ba9313] | 547 | $conn->conn->send_msg(topic => $chan, @_ ? join(" ", @_) : undef); |
---|
[48f7d12] | 548 | return; |
---|
[3ad15ff] | 549 | } |
---|
| 550 | |
---|
[af9de56] | 551 | sub cmd_quote { |
---|
| 552 | my $cmd = shift; |
---|
| 553 | my $conn = shift; |
---|
[8ba9313] | 554 | $conn->conn->send_msg(@_); |
---|
[48f7d12] | 555 | return; |
---|
[af9de56] | 556 | } |
---|
| 557 | |
---|
[b38b0b2] | 558 | ################################################################################ |
---|
| 559 | ########################### Utilities/Helpers ################################## |
---|
| 560 | ################################################################################ |
---|
| 561 | |
---|
[dace02a] | 562 | sub find_channel { |
---|
| 563 | my $channel = shift; |
---|
| 564 | my @found; |
---|
| 565 | for my $conn (values %ircnets) { |
---|
| 566 | if($conn->conn->{channel_list}{lc $channel}) { |
---|
| 567 | push @found, $conn; |
---|
| 568 | } |
---|
| 569 | } |
---|
| 570 | return $found[0] if(scalar @found == 1); |
---|
| 571 | } |
---|
| 572 | |
---|
[330c55a] | 573 | sub mk_irc_command { |
---|
| 574 | my $sub = shift; |
---|
[54b4a87] | 575 | my $flags = shift || 0; |
---|
[330c55a] | 576 | return sub { |
---|
| 577 | my $cmd = shift; |
---|
| 578 | my $conn; |
---|
| 579 | my $alias; |
---|
| 580 | my $channel; |
---|
| 581 | my $getopt = Getopt::Long::Parser->new; |
---|
| 582 | my $m = BarnOwl::getcurmsg(); |
---|
[b38b0b2] | 583 | |
---|
[330c55a] | 584 | local @ARGV = @_; |
---|
| 585 | $getopt->configure(qw(pass_through permute no_getopt_compat prefix_pattern=-|--)); |
---|
| 586 | $getopt->getoptions("alias=s" => \$alias); |
---|
| 587 | |
---|
| 588 | if(defined($alias)) { |
---|
[416241f] | 589 | $conn = get_connection_by_alias($alias, |
---|
| 590 | $flags & ALLOW_DISCONNECTED); |
---|
[330c55a] | 591 | } |
---|
[54b4a87] | 592 | if($flags & CHANNEL_ARG) { |
---|
[e625b5e] | 593 | $channel = $ARGV[0]; |
---|
[330c55a] | 594 | if(defined($channel) && $channel =~ /^#/) { |
---|
[8a22ff2] | 595 | shift @ARGV; |
---|
[dace02a] | 596 | if(my $c = find_channel($channel)) { |
---|
| 597 | $conn ||= $c; |
---|
[330c55a] | 598 | } |
---|
[8a22ff2] | 599 | } elsif (!($flags & CHANNEL_EXPLICIT) && |
---|
| 600 | $m && $m->type eq 'IRC' && !$m->is_private) { |
---|
[ecee82f] | 601 | $channel = $m->channel; |
---|
| 602 | } else { |
---|
| 603 | undef $channel; |
---|
[330c55a] | 604 | } |
---|
| 605 | } |
---|
[ecee82f] | 606 | |
---|
[54b4a87] | 607 | if(!$channel && |
---|
| 608 | ($flags & CHANNEL_ARG) && |
---|
| 609 | !($flags & CHANNEL_OPTIONAL)) { |
---|
[330c55a] | 610 | die("Usage: $cmd <channel>\n"); |
---|
| 611 | } |
---|
| 612 | if(!$conn) { |
---|
| 613 | if($m && $m->type eq 'IRC') { |
---|
[416241f] | 614 | $conn = get_connection_by_alias($m->network, |
---|
| 615 | $flags & ALLOW_DISCONNECTED); |
---|
[330c55a] | 616 | } |
---|
| 617 | } |
---|
| 618 | if(!$conn && scalar keys %ircnets == 1) { |
---|
| 619 | $conn = [values(%ircnets)]->[0]; |
---|
| 620 | } |
---|
| 621 | if(!$conn) { |
---|
| 622 | die("You must specify an IRC network using -a.\n"); |
---|
| 623 | } |
---|
[54b4a87] | 624 | if($flags & CHANNEL_ARG) { |
---|
[330c55a] | 625 | $sub->($cmd, $conn, $channel, @ARGV); |
---|
| 626 | } else { |
---|
| 627 | $sub->($cmd, $conn, @ARGV); |
---|
| 628 | } |
---|
| 629 | }; |
---|
[6858d2d] | 630 | } |
---|
| 631 | |
---|
[b38b0b2] | 632 | sub get_connection_by_alias { |
---|
[2c40dc0] | 633 | my $key = shift; |
---|
[416241f] | 634 | my $allow_disconnected = shift; |
---|
| 635 | |
---|
[3713b86] | 636 | my $conn = $ircnets{$key}; |
---|
| 637 | die("No such ircnet: $key\n") unless $conn; |
---|
| 638 | if ($conn->conn->{registered} || $allow_disconnected) { |
---|
| 639 | return $conn; |
---|
| 640 | } |
---|
| 641 | die("[@{[$conn->alias]}] Not currently connected."); |
---|
[b38b0b2] | 642 | } |
---|
| 643 | |
---|
| 644 | 1; |
---|