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