| 1 | use strict; |
|---|
| 2 | use warnings; |
|---|
| 3 | |
|---|
| 4 | package BarnOwl::Module::IRC; |
|---|
| 5 | |
|---|
| 6 | =head1 NAME |
|---|
| 7 | |
|---|
| 8 | BarnOwl::Module::IRC |
|---|
| 9 | |
|---|
| 10 | =head1 DESCRIPTION |
|---|
| 11 | |
|---|
| 12 | This module implements IRC support for barnowl. |
|---|
| 13 | |
|---|
| 14 | =cut |
|---|
| 15 | |
|---|
| 16 | use BarnOwl; |
|---|
| 17 | use BarnOwl::Hooks; |
|---|
| 18 | use BarnOwl::Message::IRC; |
|---|
| 19 | use BarnOwl::Module::IRC::Connection qw(is_private); |
|---|
| 20 | |
|---|
| 21 | use Net::IRC; |
|---|
| 22 | use Getopt::Long; |
|---|
| 23 | |
|---|
| 24 | our $VERSION = 0.02; |
|---|
| 25 | |
|---|
| 26 | our $irc; |
|---|
| 27 | |
|---|
| 28 | # Hash alias -> BarnOwl::Module::IRC::Connection object |
|---|
| 29 | our %ircnets; |
|---|
| 30 | our %channels; |
|---|
| 31 | |
|---|
| 32 | sub startup { |
|---|
| 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 | |
|---|
| 60 | register_commands(); |
|---|
| 61 | register_handlers(); |
|---|
| 62 | BarnOwl::filter('irc type ^IRC$'); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | sub shutdown { |
|---|
| 66 | for my $conn (values %ircnets) { |
|---|
| 67 | $conn->conn->disconnect(); |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | sub mainloop_hook { |
|---|
| 72 | return unless defined $irc; |
|---|
| 73 | eval { |
|---|
| 74 | $irc->do_one_loop(); |
|---|
| 75 | }; |
|---|
| 76 | return; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | sub register_handlers { |
|---|
| 80 | if(!$irc) { |
|---|
| 81 | $irc = Net::IRC->new; |
|---|
| 82 | $irc->timeout(0); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | sub register_commands { |
|---|
| 87 | BarnOwl::new_command('irc-connect' => \&cmd_connect, |
|---|
| 88 | { |
|---|
| 89 | summary => 'Connect to an IRC server', |
|---|
| 90 | usage => 'irc-connect [-a ALIAS ] [-s] [-p PASSWORD] [-n NICK] SERVER [port]', |
|---|
| 91 | description => |
|---|
| 92 | |
|---|
| 93 | "Connect to an IRC server. Supported options are\n\n" . |
|---|
| 94 | "-a <alias> Define an alias for this server\n" . |
|---|
| 95 | "-s Use SSL\n" . |
|---|
| 96 | "-p <password> Specify the password to use\n" . |
|---|
| 97 | "-n <nick> Use a non-default nick" |
|---|
| 98 | }); |
|---|
| 99 | BarnOwl::new_command('irc-disconnect' => \&cmd_disconnect); |
|---|
| 100 | BarnOwl::new_command('irc-msg' => \&cmd_msg); |
|---|
| 101 | BarnOwl::new_command('irc-join' => \&cmd_join); |
|---|
| 102 | BarnOwl::new_command('irc-part' => \&cmd_part); |
|---|
| 103 | BarnOwl::new_command('irc-nick' => \&cmd_nick); |
|---|
| 104 | BarnOwl::new_command('irc-names' => \&cmd_names); |
|---|
| 105 | BarnOwl::new_command('irc-whois' => \&cmd_whois); |
|---|
| 106 | BarnOwl::new_command('irc-motd' => \&cmd_motd); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | $BarnOwl::Hooks::startup->add(\&startup); |
|---|
| 110 | $BarnOwl::Hooks::shutdown->add(\&shutdown); |
|---|
| 111 | $BarnOwl::Hooks::mainLoop->add(\&mainloop_hook); |
|---|
| 112 | |
|---|
| 113 | ################################################################################ |
|---|
| 114 | ######################## Owl command handlers ################################## |
|---|
| 115 | ################################################################################ |
|---|
| 116 | |
|---|
| 117 | sub cmd_connect { |
|---|
| 118 | my $cmd = shift; |
|---|
| 119 | |
|---|
| 120 | my $nick = BarnOwl::getvar('irc:nick'); |
|---|
| 121 | my $username = BarnOwl::getvar('irc:user'); |
|---|
| 122 | my $ircname = BarnOwl::getvar('irc:name'); |
|---|
| 123 | my $host; |
|---|
| 124 | my $port; |
|---|
| 125 | my $alias; |
|---|
| 126 | my $ssl; |
|---|
| 127 | my $password = undef; |
|---|
| 128 | |
|---|
| 129 | { |
|---|
| 130 | local @ARGV = @_; |
|---|
| 131 | GetOptions( |
|---|
| 132 | "alias=s" => \$alias, |
|---|
| 133 | "ssl" => \$ssl, |
|---|
| 134 | "password=s" => \$password, |
|---|
| 135 | "nick=s" => \$nick, |
|---|
| 136 | ); |
|---|
| 137 | $host = shift @ARGV or die("Usage: $cmd HOST\n"); |
|---|
| 138 | if(!$alias) { |
|---|
| 139 | if($host =~ /^(?:irc[.])?(\w+)[.]\w+$/) { |
|---|
| 140 | $alias = $1; |
|---|
| 141 | } else { |
|---|
| 142 | $alias = $host; |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | $port = shift @ARGV || 6667; |
|---|
| 146 | $ssl ||= 0; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | if(exists $ircnets{$alias}) { |
|---|
| 150 | die("Already connected to a server with alias '$alias'. Either disconnect or specify an alias with -a.\n"); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | my $conn = BarnOwl::Module::IRC::Connection->new($irc, $alias, |
|---|
| 154 | Nick => $nick, |
|---|
| 155 | Server => $host, |
|---|
| 156 | Port => $port, |
|---|
| 157 | Username => $username, |
|---|
| 158 | Ircname => $ircname, |
|---|
| 159 | Port => $port, |
|---|
| 160 | Password => $password, |
|---|
| 161 | SSL => $ssl |
|---|
| 162 | ); |
|---|
| 163 | |
|---|
| 164 | if ($conn->conn->connected) { |
|---|
| 165 | BarnOwl::admin_message("IRC", "Connected to $alias as $nick"); |
|---|
| 166 | $ircnets{$alias} = $conn; |
|---|
| 167 | } else { |
|---|
| 168 | die("IRC::Connection->connect failed: $!"); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | return; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | sub cmd_disconnect { |
|---|
| 175 | my $cmd = shift; |
|---|
| 176 | my $conn = get_connection(\@_); |
|---|
| 177 | $conn->conn->disconnect; |
|---|
| 178 | delete $ircnets{$conn->alias}; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | sub cmd_msg { |
|---|
| 182 | my $cmd = shift; |
|---|
| 183 | my $conn = get_connection(\@_); |
|---|
| 184 | my $to = shift or die("Usage: $cmd NICK\n"); |
|---|
| 185 | # handle multiple recipients? |
|---|
| 186 | if(@_) { |
|---|
| 187 | process_msg($conn, $to, join(" ", @_)); |
|---|
| 188 | } else { |
|---|
| 189 | BarnOwl::start_edit_win("/msg $to -a " . $conn->alias, sub {process_msg($conn, $to, @_)}); |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | sub process_msg { |
|---|
| 194 | my $conn = shift; |
|---|
| 195 | my $to = shift; |
|---|
| 196 | my $body = shift; |
|---|
| 197 | # Strip whitespace. In the future -- send one message/line? |
|---|
| 198 | $body =~ tr/\n\r/ /; |
|---|
| 199 | $conn->conn->privmsg($to, $body); |
|---|
| 200 | my $msg = BarnOwl::Message->new( |
|---|
| 201 | type => 'IRC', |
|---|
| 202 | direction => is_private($to) ? 'out' : 'in', |
|---|
| 203 | server => $conn->server, |
|---|
| 204 | network => $conn->alias, |
|---|
| 205 | recipient => $to, |
|---|
| 206 | body => $body, |
|---|
| 207 | sender => $conn->nick, |
|---|
| 208 | is_private($to) ? |
|---|
| 209 | (isprivate => 'true') : (channel => $to), |
|---|
| 210 | replycmd => "irc-msg $to", |
|---|
| 211 | replysendercmd => "irc-msg $to" |
|---|
| 212 | ); |
|---|
| 213 | BarnOwl::queue_message($msg); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | sub cmd_join { |
|---|
| 217 | my $cmd = shift; |
|---|
| 218 | my $conn = get_connection(\@_); |
|---|
| 219 | my $chan = shift or die("Usage: $cmd channel\n"); |
|---|
| 220 | $channels{$chan} ||= []; |
|---|
| 221 | push @{$channels{$chan}}, $conn; |
|---|
| 222 | $conn->conn->join($chan); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | sub cmd_part { |
|---|
| 226 | my $cmd = shift; |
|---|
| 227 | my $conn = get_connection(\@_); |
|---|
| 228 | my $chan = get_channel(\@_) || die("Usage: $cmd <channel>\n"); |
|---|
| 229 | $channels{$chan} = [grep {$_ ne $conn} @{$channels{$chan} || []}]; |
|---|
| 230 | $conn->conn->part($chan); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | sub cmd_nick { |
|---|
| 234 | my $cmd = shift; |
|---|
| 235 | my $conn = get_connection(\@_); |
|---|
| 236 | my $nick = shift or die("Usage: $cmd <new nick>\n"); |
|---|
| 237 | $conn->conn->nick($nick); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | sub cmd_names { |
|---|
| 241 | my $cmd = shift; |
|---|
| 242 | my $conn = get_connection(\@_); |
|---|
| 243 | my $chan = get_channel(\@_) || die("Usage: $cmd <channel>\n"); |
|---|
| 244 | $conn->conn->names($chan); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | sub cmd_whois { |
|---|
| 248 | my $cmd = shift; |
|---|
| 249 | my $conn = get_connection(\@_); |
|---|
| 250 | my $who = shift || die("Usage: $cmd <user>\n"); |
|---|
| 251 | $conn->conn->whois($who); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | sub cmd_motd { |
|---|
| 255 | my $cmd = shift; |
|---|
| 256 | my $conn = get_connection(\@_); |
|---|
| 257 | $conn->conn->motd; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | ################################################################################ |
|---|
| 261 | ########################### Utilities/Helpers ################################## |
|---|
| 262 | ################################################################################ |
|---|
| 263 | |
|---|
| 264 | sub get_connection { |
|---|
| 265 | my $args = shift; |
|---|
| 266 | if(scalar @$args >= 2 && $args->[0] eq '-a') { |
|---|
| 267 | shift @$args; |
|---|
| 268 | return get_connection_by_alias(shift @$args); |
|---|
| 269 | } |
|---|
| 270 | my $channel = $args->[-1]; |
|---|
| 271 | if (defined($channel) && $channel =~ /^#/ |
|---|
| 272 | and $channels{$channel} and @{$channels{$channel}} == 1) { |
|---|
| 273 | return $channels{$channel}[0]; |
|---|
| 274 | } |
|---|
| 275 | my $m = BarnOwl::getcurmsg(); |
|---|
| 276 | if($m && $m->type eq 'IRC') { |
|---|
| 277 | return get_connection_by_alias($m->network); |
|---|
| 278 | } |
|---|
| 279 | if(scalar keys %ircnets == 1) { |
|---|
| 280 | return [values(%ircnets)]->[0]; |
|---|
| 281 | } |
|---|
| 282 | die("You must specify a network with -a\n"); |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | sub get_channel { |
|---|
| 286 | my $args = shift; |
|---|
| 287 | if(scalar @$args) { |
|---|
| 288 | return shift @$args; |
|---|
| 289 | } |
|---|
| 290 | my $m = BarnOwl::getcurmsg(); |
|---|
| 291 | if($m && $m->type eq 'IRC') { |
|---|
| 292 | return $m->channel if !$m->is_private; |
|---|
| 293 | } |
|---|
| 294 | return undef; |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | sub get_connection_by_alias { |
|---|
| 298 | my $key = shift; |
|---|
| 299 | die("No such ircnet: $key\n") unless exists $ircnets{$key}; |
|---|
| 300 | return $ircnets{$key}; |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | 1; |
|---|