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 OwlProcess { |
---|
80 | return unless defined $irc; |
---|
81 | eval { |
---|
82 | $irc->do_one_loop(); |
---|
83 | }; |
---|
84 | return; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | sub register_handlers { |
---|
89 | if(!$irc) { |
---|
90 | $irc = Net::IRC->new; |
---|
91 | $irc->timeout(0); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | sub register_commands { |
---|
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 | }); |
---|
108 | BarnOwl::new_command('irc-disconnect' => \&cmd_disconnect); |
---|
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); |
---|
115 | BarnOwl::new_command('irc-motd' => \&cmd_motd); |
---|
116 | BarnOwl::new_command('irc-list' => \&cmd_list); |
---|
117 | BarnOwl::new_command('irc-who' => \&cmd_who); |
---|
118 | BarnOwl::new_command('irc-stats' => \&cmd_stats); |
---|
119 | } |
---|
120 | |
---|
121 | $BarnOwl::Hooks::startup->add(\&startup); |
---|
122 | $BarnOwl::Hooks::shutdown->add(\&shutdown); |
---|
123 | #$BarnOwl::Hooks::mainLoop->add(\&mainloop_hook); |
---|
124 | |
---|
125 | ################################################################################ |
---|
126 | ######################## Owl command handlers ################################## |
---|
127 | ################################################################################ |
---|
128 | |
---|
129 | sub cmd_connect { |
---|
130 | my $cmd = shift; |
---|
131 | |
---|
132 | my $nick = BarnOwl::getvar('irc:nick'); |
---|
133 | my $username = BarnOwl::getvar('irc:user'); |
---|
134 | my $ircname = BarnOwl::getvar('irc:name'); |
---|
135 | my $host; |
---|
136 | my $port; |
---|
137 | my $alias; |
---|
138 | my $ssl; |
---|
139 | my $password = undef; |
---|
140 | |
---|
141 | { |
---|
142 | local @ARGV = @_; |
---|
143 | GetOptions( |
---|
144 | "alias=s" => \$alias, |
---|
145 | "ssl" => \$ssl, |
---|
146 | "password=s" => \$password, |
---|
147 | "nick=s" => \$nick, |
---|
148 | ); |
---|
149 | $host = shift @ARGV or die("Usage: $cmd HOST\n"); |
---|
150 | if(!$alias) { |
---|
151 | if($host =~ /^(?:irc[.])?([\w-]+)[.]\w+$/) { |
---|
152 | $alias = $1; |
---|
153 | } else { |
---|
154 | $alias = $host; |
---|
155 | } |
---|
156 | } |
---|
157 | $ssl ||= 0; |
---|
158 | $port = shift @ARGV || ($ssl ? 6697 : 6667); |
---|
159 | } |
---|
160 | |
---|
161 | if(exists $ircnets{$alias}) { |
---|
162 | die("Already connected to a server with alias '$alias'. Either disconnect or specify an alias with -a.\n"); |
---|
163 | } |
---|
164 | |
---|
165 | my $conn = BarnOwl::Module::IRC::Connection->new($irc, $alias, |
---|
166 | Nick => $nick, |
---|
167 | Server => $host, |
---|
168 | Port => $port, |
---|
169 | Username => $username, |
---|
170 | Ircname => $ircname, |
---|
171 | Port => $port, |
---|
172 | Password => $password, |
---|
173 | SSL => $ssl |
---|
174 | ); |
---|
175 | |
---|
176 | if ($conn->conn->connected) { |
---|
177 | BarnOwl::admin_message("IRC", "Connected to $alias as $nick"); |
---|
178 | $ircnets{$alias} = $conn; |
---|
179 | my $fd = $conn->getSocket()->fileno(); |
---|
180 | BarnOwl::add_dispatch($fd, \&OwlProcess); |
---|
181 | $conn->{FD} = $fd; |
---|
182 | } else { |
---|
183 | die("IRC::Connection->connect failed: $!"); |
---|
184 | } |
---|
185 | |
---|
186 | return; |
---|
187 | } |
---|
188 | |
---|
189 | sub cmd_disconnect { |
---|
190 | my $cmd = shift; |
---|
191 | my $conn = get_connection(\@_); |
---|
192 | $conn->conn->disconnect; |
---|
193 | delete $ircnets{$conn->alias}; |
---|
194 | } |
---|
195 | |
---|
196 | sub cmd_msg { |
---|
197 | my $cmd = shift; |
---|
198 | my $conn = get_connection(\@_); |
---|
199 | my $to = shift or die("Usage: $cmd NICK\n"); |
---|
200 | # handle multiple recipients? |
---|
201 | if(@_) { |
---|
202 | process_msg($conn, $to, join(" ", @_)); |
---|
203 | } else { |
---|
204 | BarnOwl::start_edit_win("/msg -a " . $conn->alias . " $to", sub {process_msg($conn, $to, @_)}); |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | sub process_msg { |
---|
209 | my $conn = shift; |
---|
210 | my $to = shift; |
---|
211 | my $body = shift; |
---|
212 | # Strip whitespace. In the future -- send one message/line? |
---|
213 | $body =~ tr/\n\r/ /; |
---|
214 | if ($body =~ /^\/me (.*)/) { |
---|
215 | $conn->conn->me($to, $1); |
---|
216 | $body = BarnOwl::Style::boldify($conn->nick.' '.$1); |
---|
217 | } else { |
---|
218 | $conn->conn->privmsg($to, $body); |
---|
219 | } |
---|
220 | my $msg = BarnOwl::Message->new( |
---|
221 | type => 'IRC', |
---|
222 | direction => is_private($to) ? 'out' : 'in', |
---|
223 | server => $conn->server, |
---|
224 | network => $conn->alias, |
---|
225 | recipient => $to, |
---|
226 | body => $body, |
---|
227 | sender => $conn->nick, |
---|
228 | is_private($to) ? |
---|
229 | (isprivate => 'true') : (channel => $to), |
---|
230 | replycmd => "irc-msg -a " . $conn->alias . " $to", |
---|
231 | replysendercmd => "irc-msg -a " . $conn->alias . " $to" |
---|
232 | ); |
---|
233 | BarnOwl::queue_message($msg); |
---|
234 | } |
---|
235 | |
---|
236 | sub cmd_join { |
---|
237 | my $cmd = shift; |
---|
238 | my $conn = get_connection(\@_); |
---|
239 | my $chan = shift or die("Usage: $cmd channel\n"); |
---|
240 | $channels{$chan} ||= []; |
---|
241 | push @{$channels{$chan}}, $conn; |
---|
242 | $conn->conn->join($chan); |
---|
243 | } |
---|
244 | |
---|
245 | sub cmd_part { |
---|
246 | my $cmd = shift; |
---|
247 | my $conn = get_connection(\@_); |
---|
248 | my $chan = get_channel(\@_) || die("Usage: $cmd <channel>\n"); |
---|
249 | $channels{$chan} = [grep {$_ ne $conn} @{$channels{$chan} || []}]; |
---|
250 | $conn->conn->part($chan); |
---|
251 | } |
---|
252 | |
---|
253 | sub cmd_nick { |
---|
254 | my $cmd = shift; |
---|
255 | my $conn = get_connection(\@_); |
---|
256 | my $nick = shift or die("Usage: $cmd <new nick>\n"); |
---|
257 | $conn->conn->nick($nick); |
---|
258 | } |
---|
259 | |
---|
260 | sub cmd_names { |
---|
261 | my $cmd = shift; |
---|
262 | my $conn = get_connection(\@_); |
---|
263 | my $chan = get_channel(\@_) || die("Usage: $cmd <channel>\n"); |
---|
264 | $conn->conn->names($chan); |
---|
265 | } |
---|
266 | |
---|
267 | sub cmd_whois { |
---|
268 | my $cmd = shift; |
---|
269 | my $conn = get_connection(\@_); |
---|
270 | my $who = shift || die("Usage: $cmd <user>\n"); |
---|
271 | $conn->conn->whois($who); |
---|
272 | } |
---|
273 | |
---|
274 | sub cmd_motd { |
---|
275 | my $cmd = shift; |
---|
276 | my $conn = get_connection(\@_); |
---|
277 | $conn->conn->motd; |
---|
278 | } |
---|
279 | |
---|
280 | sub cmd_list { |
---|
281 | my $cmd = shift; |
---|
282 | my $message = BarnOwl::Style::boldify('Current IRC networks:') . "\n"; |
---|
283 | while (my ($alias, $conn) = each %ircnets) { |
---|
284 | $message .= ' ' . $alias . ' => ' . $conn->nick . '@' . $conn->server . "\n"; |
---|
285 | } |
---|
286 | BarnOwl::popless_ztext($message); |
---|
287 | } |
---|
288 | |
---|
289 | sub cmd_who { |
---|
290 | my $cmd = shift; |
---|
291 | my $conn = get_connection(\@_); |
---|
292 | my $who = shift || die("Usage: $cmd <user>\n"); |
---|
293 | $conn->conn->who($who); |
---|
294 | } |
---|
295 | |
---|
296 | sub cmd_stats { |
---|
297 | my $cmd = shift; |
---|
298 | my $conn = get_connection(\@_); |
---|
299 | my $type = shift || die("Usage: $cmd <chiklmouy> [server] \n"); |
---|
300 | $conn->conn->stats($type, @_); |
---|
301 | } |
---|
302 | |
---|
303 | ################################################################################ |
---|
304 | ########################### Utilities/Helpers ################################## |
---|
305 | ################################################################################ |
---|
306 | |
---|
307 | sub get_connection { |
---|
308 | my $args = shift; |
---|
309 | if(scalar @$args >= 2 && $args->[0] eq '-a') { |
---|
310 | shift @$args; |
---|
311 | return get_connection_by_alias(shift @$args); |
---|
312 | } |
---|
313 | my $channel = $args->[-1]; |
---|
314 | if (defined($channel) && $channel =~ /^#/ |
---|
315 | and $channels{$channel} and @{$channels{$channel}} == 1) { |
---|
316 | return $channels{$channel}[0]; |
---|
317 | } |
---|
318 | my $m = BarnOwl::getcurmsg(); |
---|
319 | if($m && $m->type eq 'IRC') { |
---|
320 | return get_connection_by_alias($m->network); |
---|
321 | } |
---|
322 | if(scalar keys %ircnets == 1) { |
---|
323 | return [values(%ircnets)]->[0]; |
---|
324 | } |
---|
325 | die("You must specify a network with -a\n"); |
---|
326 | } |
---|
327 | |
---|
328 | sub get_channel { |
---|
329 | my $args = shift; |
---|
330 | if(scalar @$args) { |
---|
331 | return shift @$args; |
---|
332 | } |
---|
333 | my $m = BarnOwl::getcurmsg(); |
---|
334 | if($m && $m->type eq 'IRC') { |
---|
335 | return $m->channel if !$m->is_private; |
---|
336 | } |
---|
337 | return undef; |
---|
338 | } |
---|
339 | |
---|
340 | sub get_connection_by_alias { |
---|
341 | my $key = shift; |
---|
342 | die("No such ircnet: $key\n") unless exists $ircnets{$key}; |
---|
343 | return $ircnets{$key}; |
---|
344 | } |
---|
345 | |
---|
346 | 1; |
---|