[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; |
---|
[9620c8d] | 23 | use Encode; |
---|
[be43554] | 24 | use File::Spec; |
---|
| 25 | use Getopt::Long; |
---|
[c2866ec] | 26 | use Text::Wrap; |
---|
[b38b0b2] | 27 | |
---|
[2c40dc0] | 28 | our $VERSION = 0.02; |
---|
[b38b0b2] | 29 | |
---|
[be43554] | 30 | our $IRC_SUBS_FILENAME = "ircchannels"; |
---|
| 31 | |
---|
[b38b0b2] | 32 | our $irc; |
---|
| 33 | |
---|
| 34 | # Hash alias -> BarnOwl::Module::IRC::Connection object |
---|
| 35 | our %ircnets; |
---|
| 36 | |
---|
| 37 | sub startup { |
---|
[b10f340] | 38 | BarnOwl::new_variable_string('irc:nick', { |
---|
| 39 | default => $ENV{USER}, |
---|
| 40 | summary => 'The default IRC nickname', |
---|
| 41 | description => 'By default, irc-connect will use this nick ' . |
---|
| 42 | 'when connecting to a new server. See :help irc-connect for ' . |
---|
| 43 | 'more information.' |
---|
| 44 | }); |
---|
| 45 | |
---|
| 46 | BarnOwl::new_variable_string('irc:user', { |
---|
| 47 | default => $ENV{USER}, |
---|
| 48 | summary => 'The IRC "username" field' |
---|
| 49 | }); |
---|
| 50 | BarnOwl::new_variable_string('irc:name', { |
---|
| 51 | default => "", |
---|
| 52 | summary => 'A short name field for IRC', |
---|
| 53 | description => 'A short (maybe 60 or so chars) piece of text, ' . |
---|
| 54 | 'originally intended to display your real name, which people ' . |
---|
| 55 | 'often use for pithy quotes and URLs.' |
---|
| 56 | }); |
---|
[cd12307] | 57 | |
---|
[b10f340] | 58 | BarnOwl::new_variable_bool('irc:spew', { |
---|
| 59 | default => 0, |
---|
| 60 | summary => 'Show unhandled IRC events', |
---|
| 61 | description => 'If set, display all unrecognized IRC events as ' . |
---|
[cd12307] | 62 | 'admin messages. Intended for debugging and development use only.' |
---|
[b10f340] | 63 | }); |
---|
[f81176c] | 64 | |
---|
| 65 | BarnOwl::new_variable_string('irc:skip', { |
---|
| 66 | default => 'welcome yourhost created ' . |
---|
| 67 | 'luserclient luserme luserop luserchannels', |
---|
| 68 | summary => 'Skip messages of these types', |
---|
| 69 | description => 'If set, each (space-separated) message type ' . |
---|
| 70 | 'provided will be hidden and ignored if received.' |
---|
| 71 | }); |
---|
| 72 | |
---|
[c2866ec] | 73 | BarnOwl::new_variable_int('irc:max-message-length', { |
---|
| 74 | default => 450, |
---|
| 75 | summary => 'Split messages to at most this many characters.' . |
---|
| 76 | "If non-positive, don't split messages", |
---|
| 77 | description => 'If set to a positive number, any paragraph in an ' . |
---|
| 78 | 'IRC message will be split after this many characters.' |
---|
| 79 | }); |
---|
| 80 | |
---|
[b38b0b2] | 81 | register_commands(); |
---|
[96f7b07] | 82 | BarnOwl::filter(qw{irc type ^IRC$ or ( type ^admin$ and adminheader ^IRC$ )}); |
---|
[b38b0b2] | 83 | } |
---|
| 84 | |
---|
| 85 | sub shutdown { |
---|
| 86 | for my $conn (values %ircnets) { |
---|
[8ba9313] | 87 | $conn->conn->disconnect('Quitting'); |
---|
[b38b0b2] | 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
[f17bb2c0] | 91 | sub quickstart { |
---|
| 92 | return <<'END_QUICKSTART'; |
---|
| 93 | @b[IRC:] |
---|
| 94 | Use ':irc-connect @b[server]' to connect to an IRC server, and |
---|
| 95 | ':irc-join @b[#channel]' to join a channel. ':irc-msg @b[#channel] |
---|
| 96 | @b[message]' sends a message to a channel. |
---|
| 97 | END_QUICKSTART |
---|
| 98 | } |
---|
| 99 | |
---|
[da554da] | 100 | sub buddylist { |
---|
| 101 | my $list = ""; |
---|
| 102 | |
---|
| 103 | for my $net (sort keys %ircnets) { |
---|
| 104 | my $conn = $ircnets{$net}; |
---|
| 105 | my ($nick, $server) = ($conn->nick, $conn->server); |
---|
[6396c1e] | 106 | $list .= BarnOwl::Style::boldify("IRC channels for $net ($nick\@$server)"); |
---|
| 107 | $list .= "\n"; |
---|
[da554da] | 108 | |
---|
[dace02a] | 109 | for my $chan (keys %{$conn->conn->{channel_list}}) { |
---|
[da554da] | 110 | $list .= " $chan\n"; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | return $list; |
---|
| 115 | } |
---|
| 116 | |
---|
[f81176c] | 117 | sub skip_msg { |
---|
| 118 | my $class = shift; |
---|
| 119 | my $type = lc shift; |
---|
| 120 | my $skip = lc BarnOwl::getvar('irc:skip'); |
---|
| 121 | return grep {$_ eq $type} split ' ', $skip; |
---|
| 122 | } |
---|
| 123 | |
---|
[54b4a87] | 124 | =head2 mk_irc_command SUB FLAGS |
---|
| 125 | |
---|
| 126 | Return a subroutine that can be bound as a an IRC command. The |
---|
| 127 | subroutine will be called with arguments (COMMAND-NAME, |
---|
| 128 | IRC-CONNECTION, [CHANNEL], ARGV...). |
---|
| 129 | |
---|
| 130 | C<IRC-CONNECTION> and C<CHANNEL> will be inferred from arguments to |
---|
| 131 | the command and the current message if appropriate. |
---|
| 132 | |
---|
| 133 | The bitwise C<or> of zero or more C<FLAGS> can be passed in as a |
---|
| 134 | second argument to alter the behavior of the returned commands: |
---|
| 135 | |
---|
| 136 | =over 4 |
---|
| 137 | |
---|
| 138 | =item C<CHANNEL_ARG> |
---|
| 139 | |
---|
| 140 | This command accepts the name of a channel. Pass in the C<CHANNEL> |
---|
| 141 | argument listed above, and die if no channel argument can be found. |
---|
| 142 | |
---|
[4f7b1f4] | 143 | =item C<CHANNEL_OR_USER> |
---|
| 144 | |
---|
| 145 | Pass the channel argument, but accept it if it's a username (e.g. |
---|
| 146 | has no hash). Only relevant with C<CHANNEL_ARG>. |
---|
| 147 | |
---|
[54b4a87] | 148 | =item C<CHANNEL_OPTIONAL> |
---|
| 149 | |
---|
| 150 | Pass the channel argument, but don't die if not present. Only relevant |
---|
| 151 | with C<CHANNEL_ARG>. |
---|
| 152 | |
---|
[416241f] | 153 | =item C<ALLOW_DISCONNECTED> |
---|
| 154 | |
---|
| 155 | C<IRC-CONNECTION> may be a disconnected connection object that is |
---|
| 156 | currently pending a reconnect. |
---|
| 157 | |
---|
[54b4a87] | 158 | =back |
---|
| 159 | |
---|
| 160 | =cut |
---|
| 161 | |
---|
| 162 | use constant CHANNEL_ARG => 1; |
---|
| 163 | use constant CHANNEL_OPTIONAL => 2; |
---|
[4f7b1f4] | 164 | use constant CHANNEL_OR_USER => 4; |
---|
[330c55a] | 165 | |
---|
[4f7b1f4] | 166 | use constant ALLOW_DISCONNECTED => 8; |
---|
[416241f] | 167 | |
---|
[b38b0b2] | 168 | sub register_commands { |
---|
[f17bb2c0] | 169 | BarnOwl::new_command( |
---|
| 170 | 'irc-connect' => \&cmd_connect, |
---|
| 171 | { |
---|
| 172 | summary => 'Connect to an IRC server', |
---|
| 173 | usage => |
---|
[c84c365] | 174 | 'irc-connect [-a ALIAS] [-s] [-p PASSWORD] [-n NICK] SERVER [port]', |
---|
[cd12307] | 175 | description => <<END_DESCR |
---|
| 176 | Connect to an IRC server. Supported options are: |
---|
[f17bb2c0] | 177 | |
---|
| 178 | -a <alias> Define an alias for this server |
---|
| 179 | -s Use SSL |
---|
| 180 | -p <password> Specify the password to use |
---|
| 181 | -n <nick> Use a non-default nick |
---|
| 182 | |
---|
| 183 | The -a option specifies an alias to use for this connection. This |
---|
| 184 | alias can be passed to the '-a' argument of any other IRC command to |
---|
| 185 | control which connection it operates on. |
---|
| 186 | |
---|
| 187 | For servers with hostnames of the form "irc.FOO.{com,org,...}", the |
---|
| 188 | alias will default to "FOO"; For other servers the full hostname is |
---|
| 189 | used. |
---|
| 190 | END_DESCR |
---|
| 191 | } |
---|
| 192 | ); |
---|
| 193 | |
---|
| 194 | BarnOwl::new_command( |
---|
[416241f] | 195 | 'irc-disconnect' => mk_irc_command( \&cmd_disconnect, ALLOW_DISCONNECTED ), |
---|
[f17bb2c0] | 196 | { |
---|
| 197 | summary => 'Disconnect from an IRC server', |
---|
| 198 | usage => 'irc-disconnect [-a ALIAS]', |
---|
| 199 | |
---|
| 200 | description => <<END_DESCR |
---|
| 201 | Disconnect from an IRC server. You can specify a specific server with |
---|
| 202 | "-a SERVER-ALIAS" if necessary. |
---|
| 203 | END_DESCR |
---|
| 204 | } |
---|
| 205 | ); |
---|
| 206 | |
---|
| 207 | BarnOwl::new_command( |
---|
[4f7b1f4] | 208 | 'irc-msg' => mk_irc_command( \&cmd_msg, CHANNEL_OR_USER|CHANNEL_ARG|CHANNEL_OPTIONAL ), |
---|
[f17bb2c0] | 209 | { |
---|
| 210 | summary => 'Send an IRC message', |
---|
| 211 | usage => 'irc-msg [-a ALIAS] DESTINATION MESSAGE', |
---|
| 212 | |
---|
| 213 | description => <<END_DESCR |
---|
[cd12307] | 214 | Send an IRC message. |
---|
[f17bb2c0] | 215 | END_DESCR |
---|
| 216 | } |
---|
| 217 | ); |
---|
| 218 | |
---|
| 219 | BarnOwl::new_command( |
---|
[54b4a87] | 220 | 'irc-mode' => mk_irc_command( \&cmd_mode, CHANNEL_OPTIONAL|CHANNEL_ARG ), |
---|
[f17bb2c0] | 221 | { |
---|
| 222 | summary => 'Change an IRC channel or user mode', |
---|
| 223 | usage => 'irc-mode [-a ALIAS] TARGET [+-]MODE OPTIONS', |
---|
| 224 | |
---|
| 225 | description => <<END_DESCR |
---|
| 226 | Change the mode of an IRC user or channel. |
---|
| 227 | END_DESCR |
---|
| 228 | } |
---|
| 229 | ); |
---|
| 230 | |
---|
| 231 | BarnOwl::new_command( |
---|
| 232 | 'irc-join' => mk_irc_command( \&cmd_join ), |
---|
| 233 | { |
---|
| 234 | summary => 'Join an IRC channel', |
---|
[be43554] | 235 | usage => 'irc-join [-a ALIAS] [-t] #channel [KEY]', |
---|
[f17bb2c0] | 236 | |
---|
| 237 | description => <<END_DESCR |
---|
[be43554] | 238 | Join an IRC channel. If the -t option is present the subscription will only be |
---|
| 239 | temporary, i.e., it will not be written to the subscription file and will |
---|
| 240 | therefore not be present the next time BarnOwl is started, and will disappear |
---|
| 241 | if the connection is lost. |
---|
[f17bb2c0] | 242 | END_DESCR |
---|
| 243 | } |
---|
| 244 | ); |
---|
| 245 | |
---|
| 246 | BarnOwl::new_command( |
---|
[54b4a87] | 247 | 'irc-part' => mk_irc_command( \&cmd_part, CHANNEL_ARG ), |
---|
[f17bb2c0] | 248 | { |
---|
| 249 | summary => 'Leave an IRC channel', |
---|
[be43554] | 250 | usage => 'irc-part [-a ALIAS] [-t] #channel', |
---|
[f17bb2c0] | 251 | |
---|
| 252 | description => <<END_DESCR |
---|
[be43554] | 253 | Part from an IRC channel. If the -t option is present the unsubscription will |
---|
| 254 | only be temporary, i.e., it will not be updated in the subscription file and |
---|
| 255 | will therefore not be in effect the next time BarnOwl is started, or if the |
---|
| 256 | connection is lost. |
---|
[f17bb2c0] | 257 | END_DESCR |
---|
| 258 | } |
---|
| 259 | ); |
---|
| 260 | |
---|
| 261 | BarnOwl::new_command( |
---|
| 262 | 'irc-nick' => mk_irc_command( \&cmd_nick ), |
---|
| 263 | { |
---|
| 264 | summary => 'Change your IRC nick on an existing connection.', |
---|
| 265 | usage => 'irc-nick [-a ALIAS] NEW-NICK', |
---|
| 266 | |
---|
| 267 | description => <<END_DESCR |
---|
| 268 | Set your IRC nickname on an existing connect. To change it prior to |
---|
| 269 | connecting, adjust the `irc:nick' variable. |
---|
| 270 | END_DESCR |
---|
| 271 | } |
---|
| 272 | ); |
---|
| 273 | |
---|
| 274 | BarnOwl::new_command( |
---|
[54b4a87] | 275 | 'irc-names' => mk_irc_command( \&cmd_names, CHANNEL_ARG ), |
---|
[f17bb2c0] | 276 | { |
---|
| 277 | summary => 'View the list of users in a channel', |
---|
| 278 | usage => 'irc-names [-a ALIAS] #channel', |
---|
| 279 | |
---|
| 280 | description => <<END_DESCR |
---|
| 281 | `irc-names' displays the list of users in a given channel in a pop-up |
---|
| 282 | window. |
---|
| 283 | END_DESCR |
---|
| 284 | } |
---|
| 285 | ); |
---|
| 286 | |
---|
| 287 | BarnOwl::new_command( |
---|
| 288 | 'irc-whois' => mk_irc_command( \&cmd_whois ), |
---|
| 289 | { |
---|
| 290 | summary => 'Displays information about a given IRC user', |
---|
| 291 | usage => 'irc-whois [-a ALIAS] NICK', |
---|
| 292 | |
---|
| 293 | description => <<END_DESCR |
---|
| 294 | Pops up information about a given IRC user. |
---|
| 295 | END_DESCR |
---|
| 296 | } |
---|
| 297 | ); |
---|
| 298 | |
---|
| 299 | BarnOwl::new_command( |
---|
| 300 | 'irc-motd' => mk_irc_command( \&cmd_motd ), |
---|
| 301 | { |
---|
| 302 | summary => 'Displays an IRC server\'s MOTD (Message of the Day)', |
---|
| 303 | usage => 'irc-motd [-a ALIAS]', |
---|
| 304 | |
---|
| 305 | description => <<END_DESCR |
---|
| 306 | Displays an IRC server's message of the day. |
---|
| 307 | END_DESCR |
---|
| 308 | } |
---|
| 309 | ); |
---|
| 310 | |
---|
| 311 | BarnOwl::new_command( |
---|
| 312 | 'irc-list' => \&cmd_list, |
---|
| 313 | { |
---|
| 314 | summary => 'Show all the active IRC connections.', |
---|
| 315 | usage => 'irc-list', |
---|
| 316 | |
---|
| 317 | description => <<END_DESCR |
---|
| 318 | Show all the currently active IRC connections with their aliases and |
---|
| 319 | server names. |
---|
| 320 | END_DESCR |
---|
| 321 | } |
---|
| 322 | ); |
---|
| 323 | |
---|
| 324 | BarnOwl::new_command( 'irc-who' => mk_irc_command( \&cmd_who ) ); |
---|
| 325 | BarnOwl::new_command( 'irc-stats' => mk_irc_command( \&cmd_stats ) ); |
---|
| 326 | |
---|
| 327 | BarnOwl::new_command( |
---|
[54b4a87] | 328 | 'irc-topic' => mk_irc_command( \&cmd_topic, CHANNEL_ARG ), |
---|
[f17bb2c0] | 329 | { |
---|
| 330 | summary => 'View or change the topic of an IRC channel', |
---|
| 331 | usage => 'irc-topic [-a ALIAS] #channel [TOPIC]', |
---|
| 332 | |
---|
| 333 | description => <<END_DESCR |
---|
| 334 | Without extra arguments, fetches and displays a given channel's topic. |
---|
| 335 | |
---|
| 336 | With extra arguments, changes the target channel's topic string. This |
---|
| 337 | may require +o on some channels. |
---|
| 338 | END_DESCR |
---|
| 339 | } |
---|
| 340 | ); |
---|
| 341 | |
---|
| 342 | BarnOwl::new_command( |
---|
| 343 | 'irc-quote' => mk_irc_command( \&cmd_quote ), |
---|
| 344 | { |
---|
| 345 | summary => 'Send a raw command to the IRC servers.', |
---|
| 346 | usage => 'irc-quote [-a ALIAS] TEXT', |
---|
| 347 | |
---|
| 348 | description => <<END_DESCR |
---|
| 349 | Send a raw command line to an IRC server. |
---|
| 350 | |
---|
| 351 | This can be used to perform some operation not yet supported by |
---|
| 352 | BarnOwl, or to define new IRC commands. |
---|
| 353 | END_DESCR |
---|
| 354 | } |
---|
| 355 | ); |
---|
[be43554] | 356 | |
---|
| 357 | BarnOwl::new_command( |
---|
| 358 | 'irc-loadchannels' => \&cmd_loadchannels, |
---|
| 359 | { |
---|
| 360 | summary => 'Reload persistent channels', |
---|
| 361 | usage => 'irc-loadchannels [-a ALIAS] [<file>]', |
---|
| 362 | |
---|
| 363 | description => <<END_DESCR |
---|
| 364 | Load persistent channels from a file. The file defaults to |
---|
| 365 | \$HOME/.owl/$IRC_SUBS_FILENAME. If the ALIAS is present, only channels |
---|
| 366 | on the given alias are loaded. The ALIAS is case-sensitive. |
---|
| 367 | |
---|
| 368 | Each line of the file should describe a single channel, in the format |
---|
| 369 | '\$alias \$channel' (without quotes). |
---|
| 370 | END_DESCR |
---|
| 371 | } |
---|
| 372 | ); |
---|
[b38b0b2] | 373 | } |
---|
| 374 | |
---|
[f17bb2c0] | 375 | |
---|
[167044b] | 376 | $BarnOwl::Hooks::startup->add('BarnOwl::Module::IRC::startup'); |
---|
| 377 | $BarnOwl::Hooks::shutdown->add('BarnOwl::Module::IRC::shutdown'); |
---|
[f17bb2c0] | 378 | $BarnOwl::Hooks::getQuickstart->add('BarnOwl::Module::IRC::quickstart'); |
---|
[da554da] | 379 | $BarnOwl::Hooks::getBuddyList->add("BarnOwl::Module::IRC::buddylist"); |
---|
[b38b0b2] | 380 | |
---|
| 381 | ################################################################################ |
---|
| 382 | ######################## Owl command handlers ################################## |
---|
| 383 | ################################################################################ |
---|
| 384 | |
---|
[be43554] | 385 | sub make_autoconnect_filename { |
---|
| 386 | # can't use ||, or else we'll treat '0' as invalid. We could check for eq "" ... |
---|
| 387 | # TODO(jgross): When we move to requiring perl 5.10, combine the |
---|
| 388 | # following two lines using // |
---|
| 389 | my $filename = shift; |
---|
| 390 | $filename = File::Spec->catfile(BarnOwl::get_config_dir(), $IRC_SUBS_FILENAME) unless defined $filename; |
---|
| 391 | if (!File::Spec->file_name_is_absolute($filename)) { |
---|
| 392 | $filename = File::Spec->catfile($ENV{HOME}, $filename); |
---|
| 393 | } |
---|
| 394 | return $filename; |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | sub _get_autoconnect_lines { |
---|
| 398 | my $filename = shift; |
---|
| 399 | |
---|
| 400 | # TODO(jgross): Write a C-side function to do this, asynchronously; |
---|
| 401 | # AIUI, perl doesn't do asynchronous I/O in any useful way |
---|
| 402 | if (open (my $subsfile, "<:encoding(UTF-8)", $filename)) { |
---|
| 403 | my @lines = <$subsfile>; |
---|
| 404 | close($subsfile); |
---|
| 405 | |
---|
| 406 | # strip trailing newlines |
---|
| 407 | local $/ = ""; |
---|
| 408 | chomp(@lines); |
---|
| 409 | |
---|
| 410 | return @lines; |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | return (); |
---|
| 414 | } |
---|
| 415 | |
---|
| 416 | sub get_autoconnect_channels { |
---|
| 417 | my $filename = make_autoconnect_filename(shift); |
---|
| 418 | my %channel_hash = (); |
---|
| 419 | |
---|
| 420 | # Load the subs from the file |
---|
| 421 | my @lines = _get_autoconnect_lines($filename); |
---|
| 422 | |
---|
| 423 | foreach my $line (@lines) { |
---|
| 424 | my @parsed_args = split(' ', $line); |
---|
| 425 | if (scalar @parsed_args == 2) { |
---|
| 426 | push @{$channel_hash{$parsed_args[0]}}, $parsed_args[1]; |
---|
| 427 | } else { |
---|
| 428 | warn "Trouble parsing irc configuration file '$filename' line '$line'; the format is '\$alias \$channel', with no spaces in either\n"; |
---|
| 429 | } |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | return %channel_hash; |
---|
| 433 | } |
---|
| 434 | |
---|
| 435 | sub add_autoconnect_channel { |
---|
| 436 | my $conn = shift; |
---|
| 437 | my $channel = shift; |
---|
| 438 | my $alias = $conn->alias; |
---|
| 439 | my $filename = make_autoconnect_filename(shift); |
---|
| 440 | |
---|
| 441 | # we already checked for spaces in $channel in cmd_join, but we still need |
---|
| 442 | # to check $alias |
---|
| 443 | die "Alias name '$alias' contains a space; parsing will fail. Use the -t flag.\n" unless index($alias, " ") == -1; |
---|
| 444 | |
---|
| 445 | my $line = "$alias $channel"; |
---|
| 446 | |
---|
| 447 | my @lines = _get_autoconnect_lines($filename); |
---|
| 448 | |
---|
| 449 | # We don't want to be noisy about duplicated joins. For example, some |
---|
| 450 | # people might have :irc-join in startup files, even though that doesn't |
---|
| 451 | # work correctly anymore because connect is asynchronous and so join on |
---|
| 452 | # startup races with connect. Regardless, just fail silently if the line |
---|
| 453 | # already exists. |
---|
| 454 | return if grep { $_ eq $line } @lines; |
---|
| 455 | |
---|
| 456 | open (my $subsfile, ">>:encoding(UTF-8)", make_autoconnect_filename($filename)) |
---|
| 457 | or die "Cannot open $filename for writing: $!\n"; |
---|
| 458 | local $, = ""; |
---|
| 459 | local $/ = ""; |
---|
| 460 | print $subsfile "$line\n"; |
---|
| 461 | close($subsfile); |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | sub remove_autoconnect_channel { |
---|
| 465 | my $conn = shift; |
---|
| 466 | my $channel = shift; |
---|
| 467 | my $alias = $conn->alias; |
---|
| 468 | my $filename = make_autoconnect_filename(shift); |
---|
| 469 | |
---|
| 470 | BarnOwl::Internal::file_deleteline($filename, "$alias $channel", 1); |
---|
| 471 | } |
---|
| 472 | |
---|
| 473 | sub cmd_loadchannels { |
---|
| 474 | my $cmd = shift; |
---|
| 475 | my $alias; |
---|
| 476 | my $getopt = Getopt::Long::Parser->new; |
---|
| 477 | |
---|
| 478 | local @ARGV = @_; |
---|
| 479 | $getopt->configure(qw(pass_through permute no_getopt_compat prefix_pattern=-|--)); |
---|
| 480 | $getopt->getoptions("alias=s" => \$alias); |
---|
| 481 | |
---|
| 482 | my %channel_hash = get_autoconnect_channels(@ARGV); |
---|
| 483 | |
---|
| 484 | my $aliases = (defined $alias) ? [$alias] : [keys %channel_hash]; |
---|
| 485 | |
---|
| 486 | foreach my $cur_alias (@$aliases) { |
---|
| 487 | # get_connection_by_alias might die, and we don't want to |
---|
| 488 | eval { |
---|
| 489 | my $conn = get_connection_by_alias($cur_alias, 1); |
---|
| 490 | my %existing_channels = map { $_ => 1 } @{$conn->autoconnect_channels}, @{$channel_hash{$cur_alias}}; |
---|
| 491 | $conn->autoconnect_channels([keys %existing_channels]); |
---|
| 492 | }; |
---|
| 493 | foreach my $channel (@{$channel_hash{$cur_alias}}) { |
---|
| 494 | if ($cur_alias eq "") { |
---|
| 495 | BarnOwl::command("irc-join", "-t", $channel); |
---|
| 496 | } else { |
---|
| 497 | BarnOwl::command("irc-join", "-t", "-a", $cur_alias, $channel); |
---|
| 498 | } |
---|
| 499 | } |
---|
| 500 | } |
---|
| 501 | } |
---|
| 502 | |
---|
[b38b0b2] | 503 | sub cmd_connect { |
---|
| 504 | my $cmd = shift; |
---|
| 505 | |
---|
[2c40dc0] | 506 | my $nick = BarnOwl::getvar('irc:nick'); |
---|
| 507 | my $username = BarnOwl::getvar('irc:user'); |
---|
| 508 | my $ircname = BarnOwl::getvar('irc:name'); |
---|
[b38b0b2] | 509 | my $host; |
---|
| 510 | my $port; |
---|
| 511 | my $alias; |
---|
| 512 | my $ssl; |
---|
| 513 | my $password = undef; |
---|
| 514 | |
---|
| 515 | { |
---|
| 516 | local @ARGV = @_; |
---|
| 517 | GetOptions( |
---|
| 518 | "alias=s" => \$alias, |
---|
| 519 | "ssl" => \$ssl, |
---|
[2c40dc0] | 520 | "password=s" => \$password, |
---|
[b10f340] | 521 | "nick=s" => \$nick, |
---|
[2c40dc0] | 522 | ); |
---|
[b38b0b2] | 523 | $host = shift @ARGV or die("Usage: $cmd HOST\n"); |
---|
| 524 | if(!$alias) { |
---|
[f094fc4] | 525 | if($host =~ /^(?:irc[.])?([\w-]+)[.]\w+$/) { |
---|
[b0c8011] | 526 | $alias = $1; |
---|
| 527 | } else { |
---|
| 528 | $alias = $host; |
---|
| 529 | } |
---|
[b38b0b2] | 530 | } |
---|
| 531 | $ssl ||= 0; |
---|
[f094fc4] | 532 | $port = shift @ARGV || ($ssl ? 6697 : 6667); |
---|
[b38b0b2] | 533 | } |
---|
| 534 | |
---|
[b0c8011] | 535 | if(exists $ircnets{$alias}) { |
---|
| 536 | die("Already connected to a server with alias '$alias'. Either disconnect or specify an alias with -a.\n"); |
---|
| 537 | } |
---|
| 538 | |
---|
[be43554] | 539 | my %channel_hash = get_autoconnect_channels; |
---|
| 540 | |
---|
[8ba9313] | 541 | my $conn = BarnOwl::Module::IRC::Connection->new($alias, $host, $port, { |
---|
[be43554] | 542 | nick => $nick, |
---|
| 543 | user => $username, |
---|
| 544 | real => $ircname, |
---|
| 545 | password => $password, |
---|
| 546 | SSL => $ssl, |
---|
| 547 | timeout => sub {0}, |
---|
| 548 | autoconnect_channels => $channel_hash{$alias} |
---|
[8ba9313] | 549 | }); |
---|
[851a0e0] | 550 | $ircnets{$alias} = $conn; |
---|
[b38b0b2] | 551 | return; |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | sub cmd_disconnect { |
---|
[ac374fc] | 555 | my $cmd = shift; |
---|
| 556 | my $conn = shift; |
---|
[851a0e0] | 557 | if ($conn->conn->{socket}) { |
---|
[5c6d661] | 558 | $conn->did_quit(1); |
---|
[8ba9313] | 559 | $conn->conn->disconnect("Goodbye!"); |
---|
[3713b86] | 560 | } elsif ($conn->{reconnect_timer}) { |
---|
[416241f] | 561 | BarnOwl::admin_message('IRC', |
---|
| 562 | "[" . $conn->alias . "] Reconnect cancelled"); |
---|
| 563 | $conn->cancel_reconnect; |
---|
[3713b86] | 564 | delete $ircnets{$conn->alias}; |
---|
[416241f] | 565 | } |
---|
[b38b0b2] | 566 | } |
---|
| 567 | |
---|
| 568 | sub cmd_msg { |
---|
[330c55a] | 569 | my $cmd = shift; |
---|
| 570 | my $conn = shift; |
---|
[e0fba58] | 571 | my $to = shift or die("Usage: $cmd [NICK|CHANNEL]\n"); |
---|
[2c40dc0] | 572 | # handle multiple recipients? |
---|
[b38b0b2] | 573 | if(@_) { |
---|
| 574 | process_msg($conn, $to, join(" ", @_)); |
---|
| 575 | } else { |
---|
[744769e] | 576 | BarnOwl::start_edit_win(BarnOwl::quote('/msg', '-a', $conn->alias, $to), sub {process_msg($conn, $to, @_)}); |
---|
[b38b0b2] | 577 | } |
---|
[48f7d12] | 578 | return; |
---|
[b38b0b2] | 579 | } |
---|
| 580 | |
---|
| 581 | sub process_msg { |
---|
| 582 | my $conn = shift; |
---|
| 583 | my $to = shift; |
---|
[9a023d0] | 584 | my $fullbody = shift; |
---|
| 585 | my @msgs; |
---|
| 586 | # Require the user to send in paragraphs (double-newline between) to |
---|
| 587 | # actually send multiple PRIVMSGs, in order to play nice with autofill. |
---|
| 588 | $fullbody =~ s/\r//g; |
---|
| 589 | @msgs = split "\n\n", $fullbody; |
---|
| 590 | map { tr/\n/ / } @msgs; |
---|
[c2866ec] | 591 | # split each body at irc:max-message-length characters, if that number |
---|
| 592 | # is positive. Only split at space boundaries. Start counting a-fresh |
---|
| 593 | # at the beginning of each paragraph |
---|
| 594 | my $max_len = BarnOwl::getvar('irc:max-message-length'); |
---|
| 595 | if ($max_len > 0) { |
---|
| 596 | local($Text::Wrap::columns) = $max_len; |
---|
| 597 | @msgs = split "\n", wrap("", "", join "\n", @msgs); |
---|
| 598 | } |
---|
[9a023d0] | 599 | for my $body (@msgs) { |
---|
| 600 | if ($body =~ /^\/me (.*)/) { |
---|
[8ba9313] | 601 | $conn->me($to, Encode::encode('utf-8', $1)); |
---|
[9a023d0] | 602 | $body = '* '.$conn->nick.' '.$1; |
---|
| 603 | } else { |
---|
[8ba9313] | 604 | $conn->conn->send_msg('privmsg', $to, Encode::encode('utf-8', $body)); |
---|
[9a023d0] | 605 | } |
---|
| 606 | my $msg = BarnOwl::Message->new( |
---|
| 607 | type => 'IRC', |
---|
| 608 | direction => is_private($to) ? 'out' : 'in', |
---|
| 609 | server => $conn->server, |
---|
| 610 | network => $conn->alias, |
---|
| 611 | recipient => $to, |
---|
| 612 | body => $body, |
---|
| 613 | sender => $conn->nick, |
---|
| 614 | is_private($to) ? |
---|
| 615 | (isprivate => 'true') : (channel => $to), |
---|
| 616 | replycmd => BarnOwl::quote('irc-msg', '-a', $conn->alias, $to), |
---|
| 617 | replysendercmd => BarnOwl::quote('irc-msg', '-a', $conn->alias, $to), |
---|
| 618 | ); |
---|
| 619 | BarnOwl::queue_message($msg); |
---|
[919535f] | 620 | } |
---|
[48f7d12] | 621 | return; |
---|
[b38b0b2] | 622 | } |
---|
| 623 | |
---|
[e625b5e] | 624 | sub cmd_mode { |
---|
| 625 | my $cmd = shift; |
---|
| 626 | my $conn = shift; |
---|
| 627 | my $target = shift; |
---|
| 628 | $target ||= shift; |
---|
[8ba9313] | 629 | $conn->conn->send_msg(mode => $target, @_); |
---|
[48f7d12] | 630 | return; |
---|
[e625b5e] | 631 | } |
---|
| 632 | |
---|
[2c40dc0] | 633 | sub cmd_join { |
---|
| 634 | my $cmd = shift; |
---|
[be43554] | 635 | my $is_temporary; |
---|
| 636 | |
---|
| 637 | my $getopt = Getopt::Long::Parser->new; |
---|
| 638 | |
---|
| 639 | local @ARGV = @_; |
---|
| 640 | $getopt->configure(qw(pass_through permute no_getopt_compat prefix_pattern=-|--)); |
---|
| 641 | $getopt->getoptions("temporary" => \$is_temporary); |
---|
| 642 | |
---|
| 643 | my $conn = shift @ARGV; |
---|
| 644 | my $chan = shift @ARGV or die("Usage: $cmd channel\n"); |
---|
| 645 | |
---|
| 646 | die "Channel name '$chan' contains a space. As per RFC 2812, IRC channel names may not contain spaces.\n" unless index($channel, " ") == -1; |
---|
| 647 | |
---|
| 648 | $conn->conn->send_msg(join => $chan, @ARGV); |
---|
| 649 | |
---|
| 650 | # regardless of whether or not this is temporary, we want to persist it |
---|
| 651 | # across reconnects. |
---|
| 652 | |
---|
| 653 | # check if the channel is already in the list |
---|
| 654 | if (!grep { $_ eq $chan } @{$conn->autoconnect_channels}) { |
---|
| 655 | push @{$conn->autoconnect_channels}, $chan; |
---|
| 656 | } |
---|
| 657 | |
---|
| 658 | if (!$is_temporary) { |
---|
| 659 | # add the line to the subs file |
---|
| 660 | add_autoconnect_channel($conn, $chan); |
---|
| 661 | } |
---|
| 662 | |
---|
[48f7d12] | 663 | return; |
---|
[2c40dc0] | 664 | } |
---|
[b38b0b2] | 665 | |
---|
[6858d2d] | 666 | sub cmd_part { |
---|
| 667 | my $cmd = shift; |
---|
[be43554] | 668 | my $is_temporary; |
---|
| 669 | |
---|
| 670 | my $getopt = Getopt::Long::Parser->new; |
---|
| 671 | |
---|
| 672 | local @ARGV = @_; |
---|
| 673 | $getopt->configure(qw(pass_through permute no_getopt_compat prefix_pattern=-|--)); |
---|
| 674 | $getopt->getoptions("temporary" => \$is_temporary); |
---|
| 675 | |
---|
| 676 | my $conn = shift @ARGV; |
---|
| 677 | my $chan = shift @ARGV or die("Usage: $cmd channel\n"); |
---|
| 678 | |
---|
[8ba9313] | 679 | $conn->conn->send_msg(part => $chan); |
---|
[be43554] | 680 | |
---|
| 681 | # regardless of whether or not this is temporary, we want to persist it |
---|
| 682 | # across reconnects |
---|
| 683 | my %existing_channels = map { $_ => 1 } @{$conn->autoconnect_channels}; |
---|
| 684 | delete $existing_channels{$chan}; |
---|
| 685 | $conn->autoconnect_channels([keys %existing_channels]); |
---|
| 686 | |
---|
| 687 | if (!$is_temporary) { |
---|
| 688 | # remove the line from the subs file |
---|
| 689 | remove_autoconnect_channel($conn, $chan); |
---|
| 690 | } |
---|
| 691 | |
---|
[48f7d12] | 692 | return; |
---|
[6858d2d] | 693 | } |
---|
| 694 | |
---|
[6286f26] | 695 | sub cmd_nick { |
---|
| 696 | my $cmd = shift; |
---|
[330c55a] | 697 | my $conn = shift; |
---|
[b0c8011] | 698 | my $nick = shift or die("Usage: $cmd <new nick>\n"); |
---|
[8ba9313] | 699 | $conn->conn->send_msg(nick => $nick); |
---|
[48f7d12] | 700 | return; |
---|
[6286f26] | 701 | } |
---|
| 702 | |
---|
[6858d2d] | 703 | sub cmd_names { |
---|
| 704 | my $cmd = shift; |
---|
[330c55a] | 705 | my $conn = shift; |
---|
| 706 | my $chan = shift; |
---|
[d264c6d] | 707 | $conn->names_tmp([]); |
---|
[8ba9313] | 708 | $conn->conn->send_msg(names => $chan); |
---|
[48f7d12] | 709 | return; |
---|
[6858d2d] | 710 | } |
---|
| 711 | |
---|
[b0c8011] | 712 | sub cmd_whois { |
---|
| 713 | my $cmd = shift; |
---|
[330c55a] | 714 | my $conn = shift; |
---|
[b0c8011] | 715 | my $who = shift || die("Usage: $cmd <user>\n"); |
---|
[8ba9313] | 716 | $conn->conn->send_msg(whois => $who); |
---|
[48f7d12] | 717 | return; |
---|
[b0c8011] | 718 | } |
---|
| 719 | |
---|
[56e72d5] | 720 | sub cmd_motd { |
---|
| 721 | my $cmd = shift; |
---|
[330c55a] | 722 | my $conn = shift; |
---|
[8ba9313] | 723 | $conn->conn->send_msg('motd'); |
---|
[48f7d12] | 724 | return; |
---|
[56e72d5] | 725 | } |
---|
| 726 | |
---|
[f094fc4] | 727 | sub cmd_list { |
---|
| 728 | my $cmd = shift; |
---|
| 729 | my $message = BarnOwl::Style::boldify('Current IRC networks:') . "\n"; |
---|
| 730 | while (my ($alias, $conn) = each %ircnets) { |
---|
| 731 | $message .= ' ' . $alias . ' => ' . $conn->nick . '@' . $conn->server . "\n"; |
---|
| 732 | } |
---|
| 733 | BarnOwl::popless_ztext($message); |
---|
[48f7d12] | 734 | return; |
---|
[f094fc4] | 735 | } |
---|
| 736 | |
---|
| 737 | sub cmd_who { |
---|
| 738 | my $cmd = shift; |
---|
[330c55a] | 739 | my $conn = shift; |
---|
[f094fc4] | 740 | my $who = shift || die("Usage: $cmd <user>\n"); |
---|
[8ba9313] | 741 | $conn->conn->send_msg(who => $who); |
---|
[48f7d12] | 742 | return; |
---|
[f094fc4] | 743 | } |
---|
| 744 | |
---|
| 745 | sub cmd_stats { |
---|
| 746 | my $cmd = shift; |
---|
[330c55a] | 747 | my $conn = shift; |
---|
[f094fc4] | 748 | my $type = shift || die("Usage: $cmd <chiklmouy> [server] \n"); |
---|
[8ba9313] | 749 | $conn->conn->send_msg(stats => $type, @_); |
---|
[48f7d12] | 750 | return; |
---|
[f094fc4] | 751 | } |
---|
| 752 | |
---|
[3ad15ff] | 753 | sub cmd_topic { |
---|
| 754 | my $cmd = shift; |
---|
[330c55a] | 755 | my $conn = shift; |
---|
| 756 | my $chan = shift; |
---|
[8ba9313] | 757 | $conn->conn->send_msg(topic => $chan, @_ ? join(" ", @_) : undef); |
---|
[48f7d12] | 758 | return; |
---|
[3ad15ff] | 759 | } |
---|
| 760 | |
---|
[af9de56] | 761 | sub cmd_quote { |
---|
| 762 | my $cmd = shift; |
---|
| 763 | my $conn = shift; |
---|
[8ba9313] | 764 | $conn->conn->send_msg(@_); |
---|
[48f7d12] | 765 | return; |
---|
[af9de56] | 766 | } |
---|
| 767 | |
---|
[b38b0b2] | 768 | ################################################################################ |
---|
| 769 | ########################### Utilities/Helpers ################################## |
---|
| 770 | ################################################################################ |
---|
| 771 | |
---|
[dace02a] | 772 | sub find_channel { |
---|
| 773 | my $channel = shift; |
---|
| 774 | my @found; |
---|
| 775 | for my $conn (values %ircnets) { |
---|
| 776 | if($conn->conn->{channel_list}{lc $channel}) { |
---|
| 777 | push @found, $conn; |
---|
| 778 | } |
---|
| 779 | } |
---|
| 780 | return $found[0] if(scalar @found == 1); |
---|
| 781 | } |
---|
| 782 | |
---|
[330c55a] | 783 | sub mk_irc_command { |
---|
| 784 | my $sub = shift; |
---|
[54b4a87] | 785 | my $flags = shift || 0; |
---|
[330c55a] | 786 | return sub { |
---|
| 787 | my $cmd = shift; |
---|
| 788 | my $conn; |
---|
| 789 | my $alias; |
---|
| 790 | my $channel; |
---|
[be43554] | 791 | my $is_temporary; |
---|
[330c55a] | 792 | my $getopt = Getopt::Long::Parser->new; |
---|
| 793 | my $m = BarnOwl::getcurmsg(); |
---|
[b38b0b2] | 794 | |
---|
[330c55a] | 795 | local @ARGV = @_; |
---|
| 796 | $getopt->configure(qw(pass_through permute no_getopt_compat prefix_pattern=-|--)); |
---|
[be43554] | 797 | $getopt->getoptions("alias=s" => \$alias, |
---|
| 798 | "temporary" => \$is_temporary); |
---|
[330c55a] | 799 | |
---|
| 800 | if(defined($alias)) { |
---|
[416241f] | 801 | $conn = get_connection_by_alias($alias, |
---|
| 802 | $flags & ALLOW_DISCONNECTED); |
---|
[330c55a] | 803 | } |
---|
[54b4a87] | 804 | if($flags & CHANNEL_ARG) { |
---|
[e625b5e] | 805 | $channel = $ARGV[0]; |
---|
[330c55a] | 806 | if(defined($channel) && $channel =~ /^#/) { |
---|
[dace02a] | 807 | if(my $c = find_channel($channel)) { |
---|
[e625b5e] | 808 | shift @ARGV; |
---|
[dace02a] | 809 | $conn ||= $c; |
---|
[330c55a] | 810 | } |
---|
[4f7b1f4] | 811 | } elsif (defined($channel) && ($flags & CHANNEL_OR_USER)) { |
---|
| 812 | shift @ARGV; |
---|
[ecee82f] | 813 | } elsif ($m && $m->type eq 'IRC' && !$m->is_private) { |
---|
| 814 | $channel = $m->channel; |
---|
| 815 | } else { |
---|
| 816 | undef $channel; |
---|
[330c55a] | 817 | } |
---|
| 818 | } |
---|
[ecee82f] | 819 | |
---|
[731e921] | 820 | if(!defined($channel) && |
---|
[54b4a87] | 821 | ($flags & CHANNEL_ARG) && |
---|
| 822 | !($flags & CHANNEL_OPTIONAL)) { |
---|
[330c55a] | 823 | die("Usage: $cmd <channel>\n"); |
---|
| 824 | } |
---|
| 825 | if(!$conn) { |
---|
| 826 | if($m && $m->type eq 'IRC') { |
---|
[416241f] | 827 | $conn = get_connection_by_alias($m->network, |
---|
| 828 | $flags & ALLOW_DISCONNECTED); |
---|
[330c55a] | 829 | } |
---|
| 830 | } |
---|
| 831 | if(!$conn && scalar keys %ircnets == 1) { |
---|
| 832 | $conn = [values(%ircnets)]->[0]; |
---|
| 833 | } |
---|
| 834 | if(!$conn) { |
---|
| 835 | die("You must specify an IRC network using -a.\n"); |
---|
| 836 | } |
---|
[be43554] | 837 | push @ARGV, "-t" if $is_temporary; |
---|
[54b4a87] | 838 | if($flags & CHANNEL_ARG) { |
---|
[330c55a] | 839 | $sub->($cmd, $conn, $channel, @ARGV); |
---|
| 840 | } else { |
---|
| 841 | $sub->($cmd, $conn, @ARGV); |
---|
| 842 | } |
---|
| 843 | }; |
---|
[6858d2d] | 844 | } |
---|
| 845 | |
---|
[b38b0b2] | 846 | sub get_connection_by_alias { |
---|
[2c40dc0] | 847 | my $key = shift; |
---|
[416241f] | 848 | my $allow_disconnected = shift; |
---|
| 849 | |
---|
[3713b86] | 850 | my $conn = $ircnets{$key}; |
---|
| 851 | die("No such ircnet: $key\n") unless $conn; |
---|
| 852 | if ($conn->conn->{registered} || $allow_disconnected) { |
---|
| 853 | return $conn; |
---|
| 854 | } |
---|
| 855 | die("[@{[$conn->alias]}] Not currently connected."); |
---|
[b38b0b2] | 856 | } |
---|
| 857 | |
---|
| 858 | 1; |
---|