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 | } |
---|
117 | |
---|
118 | $BarnOwl::Hooks::startup->add(\&startup); |
---|
119 | $BarnOwl::Hooks::shutdown->add(\&shutdown); |
---|
120 | #$BarnOwl::Hooks::mainLoop->add(\&mainloop_hook); |
---|
121 | |
---|
122 | ################################################################################ |
---|
123 | ######################## Owl command handlers ################################## |
---|
124 | ################################################################################ |
---|
125 | |
---|
126 | sub cmd_connect { |
---|
127 | my $cmd = shift; |
---|
128 | |
---|
129 | my $nick = BarnOwl::getvar('irc:nick'); |
---|
130 | my $username = BarnOwl::getvar('irc:user'); |
---|
131 | my $ircname = BarnOwl::getvar('irc:name'); |
---|
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, |
---|
143 | "password=s" => \$password, |
---|
144 | "nick=s" => \$nick, |
---|
145 | ); |
---|
146 | $host = shift @ARGV or die("Usage: $cmd HOST\n"); |
---|
147 | if(!$alias) { |
---|
148 | if($host =~ /^(?:irc[.])?(\w+)[.]\w+$/) { |
---|
149 | $alias = $1; |
---|
150 | } else { |
---|
151 | $alias = $host; |
---|
152 | } |
---|
153 | } |
---|
154 | $port = shift @ARGV || 6667; |
---|
155 | $ssl ||= 0; |
---|
156 | } |
---|
157 | |
---|
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 | |
---|
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 | |
---|
173 | if ($conn->conn->connected) { |
---|
174 | BarnOwl::admin_message("IRC", "Connected to $alias as $nick"); |
---|
175 | $ircnets{$alias} = $conn; |
---|
176 | my $fd = $conn->getSocket()->fileno(); |
---|
177 | BarnOwl::add_dispatch($fd, \&OwlProcess); |
---|
178 | $conn->{FD} = $fd; |
---|
179 | } else { |
---|
180 | die("IRC::Connection->connect failed: $!"); |
---|
181 | } |
---|
182 | |
---|
183 | return; |
---|
184 | } |
---|
185 | |
---|
186 | sub cmd_disconnect { |
---|
187 | my $cmd = shift; |
---|
188 | my $conn = get_connection(\@_); |
---|
189 | $conn->conn->disconnect; |
---|
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"); |
---|
197 | # handle multiple recipients? |
---|
198 | if(@_) { |
---|
199 | process_msg($conn, $to, join(" ", @_)); |
---|
200 | } else { |
---|
201 | BarnOwl::start_edit_win("/msg -a " . $conn->alias . " $to", 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/ /; |
---|
211 | $conn->conn->privmsg($to, $body); |
---|
212 | my $msg = BarnOwl::Message->new( |
---|
213 | type => 'IRC', |
---|
214 | direction => is_private($to) ? 'out' : 'in', |
---|
215 | server => $conn->server, |
---|
216 | network => $conn->alias, |
---|
217 | recipient => $to, |
---|
218 | body => $body, |
---|
219 | sender => $conn->nick, |
---|
220 | is_private($to) ? |
---|
221 | (isprivate => 'true') : (channel => $to), |
---|
222 | replycmd => "irc-msg -a " . $conn->alias . " $to", |
---|
223 | replysendercmd => "irc-msg -a " . $conn->alias . " $to" |
---|
224 | ); |
---|
225 | BarnOwl::queue_message($msg); |
---|
226 | } |
---|
227 | |
---|
228 | sub cmd_join { |
---|
229 | my $cmd = shift; |
---|
230 | my $conn = get_connection(\@_); |
---|
231 | my $chan = shift or die("Usage: $cmd channel\n"); |
---|
232 | $channels{$chan} ||= []; |
---|
233 | push @{$channels{$chan}}, $conn; |
---|
234 | $conn->conn->join($chan); |
---|
235 | } |
---|
236 | |
---|
237 | sub cmd_part { |
---|
238 | my $cmd = shift; |
---|
239 | my $conn = get_connection(\@_); |
---|
240 | my $chan = get_channel(\@_) || die("Usage: $cmd <channel>\n"); |
---|
241 | $channels{$chan} = [grep {$_ ne $conn} @{$channels{$chan} || []}]; |
---|
242 | $conn->conn->part($chan); |
---|
243 | } |
---|
244 | |
---|
245 | sub cmd_nick { |
---|
246 | my $cmd = shift; |
---|
247 | my $conn = get_connection(\@_); |
---|
248 | my $nick = shift or die("Usage: $cmd <new nick>\n"); |
---|
249 | $conn->conn->nick($nick); |
---|
250 | } |
---|
251 | |
---|
252 | sub cmd_names { |
---|
253 | my $cmd = shift; |
---|
254 | my $conn = get_connection(\@_); |
---|
255 | my $chan = get_channel(\@_) || die("Usage: $cmd <channel>\n"); |
---|
256 | $conn->conn->names($chan); |
---|
257 | } |
---|
258 | |
---|
259 | sub cmd_whois { |
---|
260 | my $cmd = shift; |
---|
261 | my $conn = get_connection(\@_); |
---|
262 | my $who = shift || die("Usage: $cmd <user>\n"); |
---|
263 | $conn->conn->whois($who); |
---|
264 | } |
---|
265 | |
---|
266 | sub cmd_motd { |
---|
267 | my $cmd = shift; |
---|
268 | my $conn = get_connection(\@_); |
---|
269 | $conn->conn->motd; |
---|
270 | } |
---|
271 | |
---|
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 | } |
---|
282 | my $channel = $args->[-1]; |
---|
283 | if (defined($channel) && $channel =~ /^#/ |
---|
284 | and $channels{$channel} and @{$channels{$channel}} == 1) { |
---|
285 | return $channels{$channel}[0]; |
---|
286 | } |
---|
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 | |
---|
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 | |
---|
309 | sub get_connection_by_alias { |
---|
310 | my $key = shift; |
---|
311 | die("No such ircnet: $key\n") unless exists $ircnets{$key}; |
---|
312 | return $ircnets{$key}; |
---|
313 | } |
---|
314 | |
---|
315 | 1; |
---|