[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 | |
---|
[2c40dc0] | 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); |
---|
[b38b0b2] | 20 | |
---|
| 21 | use Net::IRC; |
---|
| 22 | use Getopt::Long; |
---|
| 23 | |
---|
[2c40dc0] | 24 | our $VERSION = 0.02; |
---|
[b38b0b2] | 25 | |
---|
| 26 | our $irc; |
---|
| 27 | |
---|
| 28 | # Hash alias -> BarnOwl::Module::IRC::Connection object |
---|
| 29 | our %ircnets; |
---|
[fe8cad8] | 30 | our %channels; |
---|
[b38b0b2] | 31 | |
---|
| 32 | sub startup { |
---|
[b10f340] | 33 | BarnOwl::new_variable_string('irc:nick', { |
---|
| 34 | default => $ENV{USER}, |
---|
| 35 | summary => 'The default IRC nickname', |
---|
| 36 | description => 'By default, irc-connect will use this nick ' . |
---|
| 37 | 'when connecting to a new server. See :help irc-connect for ' . |
---|
| 38 | 'more information.' |
---|
| 39 | }); |
---|
| 40 | |
---|
| 41 | BarnOwl::new_variable_string('irc:user', { |
---|
| 42 | default => $ENV{USER}, |
---|
| 43 | summary => 'The IRC "username" field' |
---|
| 44 | }); |
---|
| 45 | BarnOwl::new_variable_string('irc:name', { |
---|
| 46 | default => "", |
---|
| 47 | summary => 'A short name field for IRC', |
---|
| 48 | description => 'A short (maybe 60 or so chars) piece of text, ' . |
---|
| 49 | 'originally intended to display your real name, which people ' . |
---|
| 50 | 'often use for pithy quotes and URLs.' |
---|
| 51 | }); |
---|
| 52 | |
---|
| 53 | BarnOwl::new_variable_bool('irc:spew', { |
---|
| 54 | default => 0, |
---|
| 55 | summary => 'Show unhandled IRC events', |
---|
| 56 | description => 'If set, display all unrecognized IRC events as ' . |
---|
| 57 | 'admin messages. Intended for debugging and development use only ' |
---|
| 58 | }); |
---|
| 59 | |
---|
[b38b0b2] | 60 | register_commands(); |
---|
| 61 | register_handlers(); |
---|
| 62 | BarnOwl::filter('irc type ^IRC$'); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | sub shutdown { |
---|
| 66 | for my $conn (values %ircnets) { |
---|
[ba2ca66] | 67 | $conn->conn->disconnect(); |
---|
[b38b0b2] | 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
[9c7a701] | 71 | #sub mainloop_hook { |
---|
| 72 | # return unless defined $irc; |
---|
| 73 | # eval { |
---|
| 74 | # $irc->do_one_loop(); |
---|
| 75 | # }; |
---|
| 76 | # return; |
---|
| 77 | #} |
---|
| 78 | |
---|
| 79 | sub OwlProcess { |
---|
[b38b0b2] | 80 | return unless defined $irc; |
---|
| 81 | eval { |
---|
| 82 | $irc->do_one_loop(); |
---|
| 83 | }; |
---|
| 84 | return; |
---|
| 85 | } |
---|
| 86 | |
---|
[9c7a701] | 87 | |
---|
[b38b0b2] | 88 | sub register_handlers { |
---|
| 89 | if(!$irc) { |
---|
| 90 | $irc = Net::IRC->new; |
---|
| 91 | $irc->timeout(0); |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | sub register_commands { |
---|
[b0c8011] | 96 | BarnOwl::new_command('irc-connect' => \&cmd_connect, |
---|
| 97 | { |
---|
| 98 | summary => 'Connect to an IRC server', |
---|
| 99 | usage => 'irc-connect [-a ALIAS ] [-s] [-p PASSWORD] [-n NICK] SERVER [port]', |
---|
| 100 | description => |
---|
| 101 | |
---|
| 102 | "Connect to an IRC server. Supported options are\n\n" . |
---|
| 103 | "-a <alias> Define an alias for this server\n" . |
---|
| 104 | "-s Use SSL\n" . |
---|
| 105 | "-p <password> Specify the password to use\n" . |
---|
| 106 | "-n <nick> Use a non-default nick" |
---|
| 107 | }); |
---|
[b38b0b2] | 108 | BarnOwl::new_command('irc-disconnect' => \&cmd_disconnect); |
---|
[b0c8011] | 109 | BarnOwl::new_command('irc-msg' => \&cmd_msg); |
---|
| 110 | BarnOwl::new_command('irc-join' => \&cmd_join); |
---|
| 111 | BarnOwl::new_command('irc-part' => \&cmd_part); |
---|
| 112 | BarnOwl::new_command('irc-nick' => \&cmd_nick); |
---|
| 113 | BarnOwl::new_command('irc-names' => \&cmd_names); |
---|
| 114 | BarnOwl::new_command('irc-whois' => \&cmd_whois); |
---|
[56e72d5] | 115 | BarnOwl::new_command('irc-motd' => \&cmd_motd); |
---|
[b38b0b2] | 116 | } |
---|
| 117 | |
---|
| 118 | $BarnOwl::Hooks::startup->add(\&startup); |
---|
| 119 | $BarnOwl::Hooks::shutdown->add(\&shutdown); |
---|
[9c7a701] | 120 | #$BarnOwl::Hooks::mainLoop->add(\&mainloop_hook); |
---|
[b38b0b2] | 121 | |
---|
| 122 | ################################################################################ |
---|
| 123 | ######################## Owl command handlers ################################## |
---|
| 124 | ################################################################################ |
---|
| 125 | |
---|
| 126 | sub cmd_connect { |
---|
| 127 | my $cmd = shift; |
---|
| 128 | |
---|
[2c40dc0] | 129 | my $nick = BarnOwl::getvar('irc:nick'); |
---|
| 130 | my $username = BarnOwl::getvar('irc:user'); |
---|
| 131 | my $ircname = BarnOwl::getvar('irc:name'); |
---|
[b38b0b2] | 132 | my $host; |
---|
| 133 | my $port; |
---|
| 134 | my $alias; |
---|
| 135 | my $ssl; |
---|
| 136 | my $password = undef; |
---|
| 137 | |
---|
| 138 | { |
---|
| 139 | local @ARGV = @_; |
---|
| 140 | GetOptions( |
---|
| 141 | "alias=s" => \$alias, |
---|
| 142 | "ssl" => \$ssl, |
---|
[2c40dc0] | 143 | "password=s" => \$password, |
---|
[b10f340] | 144 | "nick=s" => \$nick, |
---|
[2c40dc0] | 145 | ); |
---|
[b38b0b2] | 146 | $host = shift @ARGV or die("Usage: $cmd HOST\n"); |
---|
| 147 | if(!$alias) { |
---|
[b0c8011] | 148 | if($host =~ /^(?:irc[.])?(\w+)[.]\w+$/) { |
---|
| 149 | $alias = $1; |
---|
| 150 | } else { |
---|
| 151 | $alias = $host; |
---|
| 152 | } |
---|
[b38b0b2] | 153 | } |
---|
[b0c8011] | 154 | $port = shift @ARGV || 6667; |
---|
[b38b0b2] | 155 | $ssl ||= 0; |
---|
| 156 | } |
---|
| 157 | |
---|
[b0c8011] | 158 | if(exists $ircnets{$alias}) { |
---|
| 159 | die("Already connected to a server with alias '$alias'. Either disconnect or specify an alias with -a.\n"); |
---|
| 160 | } |
---|
| 161 | |
---|
[b38b0b2] | 162 | my $conn = BarnOwl::Module::IRC::Connection->new($irc, $alias, |
---|
| 163 | Nick => $nick, |
---|
| 164 | Server => $host, |
---|
| 165 | Port => $port, |
---|
| 166 | Username => $username, |
---|
| 167 | Ircname => $ircname, |
---|
| 168 | Port => $port, |
---|
| 169 | Password => $password, |
---|
| 170 | SSL => $ssl |
---|
| 171 | ); |
---|
| 172 | |
---|
[cab045b] | 173 | if ($conn->conn->connected) { |
---|
[5ff830a] | 174 | BarnOwl::admin_message("IRC", "Connected to $alias as $nick"); |
---|
| 175 | $ircnets{$alias} = $conn; |
---|
[9c7a701] | 176 | my $fd = $conn->getSocket()->fileno(); |
---|
| 177 | BarnOwl::add_dispatch($fd, \&OwlProcess); |
---|
| 178 | $conn->{FD} = $fd; |
---|
[5ff830a] | 179 | } else { |
---|
| 180 | die("IRC::Connection->connect failed: $!"); |
---|
| 181 | } |
---|
| 182 | |
---|
[b38b0b2] | 183 | return; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | sub cmd_disconnect { |
---|
| 187 | my $cmd = shift; |
---|
| 188 | my $conn = get_connection(\@_); |
---|
[ba2ca66] | 189 | $conn->conn->disconnect; |
---|
[b38b0b2] | 190 | delete $ircnets{$conn->alias}; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | sub cmd_msg { |
---|
| 194 | my $cmd = shift; |
---|
| 195 | my $conn = get_connection(\@_); |
---|
| 196 | my $to = shift or die("Usage: $cmd NICK\n"); |
---|
[2c40dc0] | 197 | # handle multiple recipients? |
---|
[b38b0b2] | 198 | if(@_) { |
---|
| 199 | process_msg($conn, $to, join(" ", @_)); |
---|
| 200 | } else { |
---|
| 201 | BarnOwl::start_edit_win("/msg $to -a " . $conn->alias, sub {process_msg($conn, $to, @_)}); |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | sub process_msg { |
---|
| 206 | my $conn = shift; |
---|
| 207 | my $to = shift; |
---|
| 208 | my $body = shift; |
---|
| 209 | # Strip whitespace. In the future -- send one message/line? |
---|
| 210 | $body =~ tr/\n\r/ /; |
---|
[ba2ca66] | 211 | $conn->conn->privmsg($to, $body); |
---|
[b38b0b2] | 212 | my $msg = BarnOwl::Message->new( |
---|
| 213 | type => 'IRC', |
---|
[6858d2d] | 214 | direction => is_private($to) ? 'out' : 'in', |
---|
[b38b0b2] | 215 | server => $conn->server, |
---|
| 216 | network => $conn->alias, |
---|
| 217 | recipient => $to, |
---|
| 218 | body => $body, |
---|
| 219 | sender => $conn->nick, |
---|
[2c40dc0] | 220 | is_private($to) ? |
---|
[0e52069] | 221 | (isprivate => 'true') : (channel => $to), |
---|
[b38b0b2] | 222 | replycmd => "irc-msg $to", |
---|
[2c40dc0] | 223 | replysendercmd => "irc-msg $to" |
---|
[b38b0b2] | 224 | ); |
---|
| 225 | BarnOwl::queue_message($msg); |
---|
| 226 | } |
---|
| 227 | |
---|
[2c40dc0] | 228 | sub cmd_join { |
---|
| 229 | my $cmd = shift; |
---|
| 230 | my $conn = get_connection(\@_); |
---|
| 231 | my $chan = shift or die("Usage: $cmd channel\n"); |
---|
[fe8cad8] | 232 | $channels{$chan} ||= []; |
---|
| 233 | push @{$channels{$chan}}, $conn; |
---|
[ba2ca66] | 234 | $conn->conn->join($chan); |
---|
[2c40dc0] | 235 | } |
---|
[b38b0b2] | 236 | |
---|
[6858d2d] | 237 | sub cmd_part { |
---|
| 238 | my $cmd = shift; |
---|
| 239 | my $conn = get_connection(\@_); |
---|
[b0c8011] | 240 | my $chan = get_channel(\@_) || die("Usage: $cmd <channel>\n"); |
---|
[fe8cad8] | 241 | $channels{$chan} = [grep {$_ ne $conn} @{$channels{$chan} || []}]; |
---|
[ba2ca66] | 242 | $conn->conn->part($chan); |
---|
[6858d2d] | 243 | } |
---|
| 244 | |
---|
[6286f26] | 245 | sub cmd_nick { |
---|
| 246 | my $cmd = shift; |
---|
| 247 | my $conn = get_connection(\@_); |
---|
[b0c8011] | 248 | my $nick = shift or die("Usage: $cmd <new nick>\n"); |
---|
[ba2ca66] | 249 | $conn->conn->nick($nick); |
---|
[6286f26] | 250 | } |
---|
| 251 | |
---|
[6858d2d] | 252 | sub cmd_names { |
---|
| 253 | my $cmd = shift; |
---|
| 254 | my $conn = get_connection(\@_); |
---|
[b0c8011] | 255 | my $chan = get_channel(\@_) || die("Usage: $cmd <channel>\n"); |
---|
[ba2ca66] | 256 | $conn->conn->names($chan); |
---|
[6858d2d] | 257 | } |
---|
| 258 | |
---|
[b0c8011] | 259 | sub cmd_whois { |
---|
| 260 | my $cmd = shift; |
---|
| 261 | my $conn = get_connection(\@_); |
---|
| 262 | my $who = shift || die("Usage: $cmd <user>\n"); |
---|
[ba2ca66] | 263 | $conn->conn->whois($who); |
---|
[b0c8011] | 264 | } |
---|
| 265 | |
---|
[56e72d5] | 266 | sub cmd_motd { |
---|
| 267 | my $cmd = shift; |
---|
| 268 | my $conn = get_connection(\@_); |
---|
[ba2ca66] | 269 | $conn->conn->motd; |
---|
[56e72d5] | 270 | } |
---|
| 271 | |
---|
[b38b0b2] | 272 | ################################################################################ |
---|
| 273 | ########################### Utilities/Helpers ################################## |
---|
| 274 | ################################################################################ |
---|
| 275 | |
---|
| 276 | sub get_connection { |
---|
| 277 | my $args = shift; |
---|
| 278 | if(scalar @$args >= 2 && $args->[0] eq '-a') { |
---|
| 279 | shift @$args; |
---|
| 280 | return get_connection_by_alias(shift @$args); |
---|
| 281 | } |
---|
[fe8cad8] | 282 | my $channel = $args->[-1]; |
---|
[56e72d5] | 283 | if (defined($channel) && $channel =~ /^#/ |
---|
| 284 | and $channels{$channel} and @{$channels{$channel}} == 1) { |
---|
[fe8cad8] | 285 | return $channels{$channel}[0]; |
---|
| 286 | } |
---|
[b38b0b2] | 287 | my $m = BarnOwl::getcurmsg(); |
---|
| 288 | if($m && $m->type eq 'IRC') { |
---|
| 289 | return get_connection_by_alias($m->network); |
---|
| 290 | } |
---|
| 291 | if(scalar keys %ircnets == 1) { |
---|
| 292 | return [values(%ircnets)]->[0]; |
---|
| 293 | } |
---|
| 294 | die("You must specify a network with -a\n"); |
---|
| 295 | } |
---|
| 296 | |
---|
[6858d2d] | 297 | sub get_channel { |
---|
| 298 | my $args = shift; |
---|
| 299 | if(scalar @$args) { |
---|
| 300 | return shift @$args; |
---|
| 301 | } |
---|
| 302 | my $m = BarnOwl::getcurmsg(); |
---|
| 303 | if($m && $m->type eq 'IRC') { |
---|
| 304 | return $m->channel if !$m->is_private; |
---|
| 305 | } |
---|
| 306 | return undef; |
---|
| 307 | } |
---|
| 308 | |
---|
[b38b0b2] | 309 | sub get_connection_by_alias { |
---|
[2c40dc0] | 310 | my $key = shift; |
---|
| 311 | die("No such ircnet: $key\n") unless exists $ircnets{$key}; |
---|
[b38b0b2] | 312 | return $ircnets{$key}; |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | 1; |
---|