1 | # $Id$ |
---|
2 | # |
---|
3 | # This is all linked into the binary and evaluated when perl starts up... |
---|
4 | # |
---|
5 | ##################################################################### |
---|
6 | ##################################################################### |
---|
7 | |
---|
8 | package owl; |
---|
9 | |
---|
10 | |
---|
11 | BEGIN { |
---|
12 | # bootstrap in C bindings and glue |
---|
13 | bootstrap owl 1.2; |
---|
14 | }; |
---|
15 | |
---|
16 | use lib(get_data_dir()."/owl/lib"); |
---|
17 | use lib($::ENV{'HOME'}."/.owl/lib"); |
---|
18 | |
---|
19 | |
---|
20 | our $configfile = $::ENV{'HOME'}."/.owlconf"; |
---|
21 | |
---|
22 | # populate global variable space for legacy owlconf files |
---|
23 | sub _format_msg_legacy_wrap { |
---|
24 | my ($m) = @_; |
---|
25 | $m->legacy_populate_global(); |
---|
26 | return &owl::format_msg($m); |
---|
27 | } |
---|
28 | |
---|
29 | # populate global variable space for legacy owlconf files |
---|
30 | sub _receive_msg_legacy_wrap { |
---|
31 | my ($m) = @_; |
---|
32 | $m->legacy_populate_global(); |
---|
33 | return &owl::receive_msg($m); |
---|
34 | } |
---|
35 | |
---|
36 | # make owl::<command>("foo") be aliases to owl::command("<command> foo"); |
---|
37 | sub AUTOLOAD { |
---|
38 | my $called = $AUTOLOAD; |
---|
39 | $called =~ s/.*:://; |
---|
40 | $called =~ s/_/-/g; |
---|
41 | return &owl::command("$called ".join(" ",@_)); |
---|
42 | } |
---|
43 | |
---|
44 | =head2 new_command NAME FUNC [{ARGS}] |
---|
45 | |
---|
46 | Add a new owl command. When owl executes the command NAME, FUNC will |
---|
47 | be called with the arguments passed to the command, with NAME as the |
---|
48 | first argument. |
---|
49 | |
---|
50 | ARGS should be a hashref containing any or all of C<summary>, |
---|
51 | C<usage>, or C<description> keys. |
---|
52 | |
---|
53 | =cut |
---|
54 | |
---|
55 | sub new_command { |
---|
56 | my $name = shift; |
---|
57 | my $func = shift; |
---|
58 | my $args = shift || {}; |
---|
59 | my %args = ( |
---|
60 | summary => undef, |
---|
61 | usage => undef, |
---|
62 | description => undef, |
---|
63 | %{$args} |
---|
64 | ); |
---|
65 | |
---|
66 | owl::new_command_internal($name, $func, $args{summary}, $args{usage}, $args{description}); |
---|
67 | } |
---|
68 | |
---|
69 | ##################################################################### |
---|
70 | ##################################################################### |
---|
71 | |
---|
72 | package owl::Message; |
---|
73 | |
---|
74 | sub new { |
---|
75 | my $class = shift; |
---|
76 | my %args = (@_); |
---|
77 | if($class eq __PACKAGE__ && $args{type}) { |
---|
78 | $class = "owl::Message::" . ucfirst $args{type}; |
---|
79 | } |
---|
80 | return bless {%args}, $class; |
---|
81 | } |
---|
82 | |
---|
83 | sub type { return shift->{"type"}; } |
---|
84 | sub direction { return shift->{"direction"}; } |
---|
85 | sub time { return shift->{"time"}; } |
---|
86 | sub id { return shift->{"id"}; } |
---|
87 | sub body { return shift->{"body"}; } |
---|
88 | sub sender { return shift->{"sender"}; } |
---|
89 | sub recipient { return shift->{"recipient"}; } |
---|
90 | sub login { return shift->{"login"}; } |
---|
91 | sub is_private { return shift->{"private"}; } |
---|
92 | |
---|
93 | sub is_login { return shift->login eq "login"; } |
---|
94 | sub is_logout { return shift->login eq "logout"; } |
---|
95 | sub is_loginout { my $m=shift; return ($m->is_login or $m->is_logout); } |
---|
96 | sub is_incoming { return (shift->{"direction"} eq "in"); } |
---|
97 | sub is_outgoing { return (shift->{"direction"} eq "out"); } |
---|
98 | |
---|
99 | sub is_deleted { return shift->{"deleted"}; } |
---|
100 | |
---|
101 | sub is_admin { return (shift->{"type"} eq "admin"); } |
---|
102 | sub is_generic { return (shift->{"type"} eq "generic"); } |
---|
103 | sub is_zephyr { return (shift->{"type"} eq "zephyr"); } |
---|
104 | sub is_aim { return (shift->{"type"} eq "aim"); } |
---|
105 | sub is_jabber { return (shift->{"type"} eq "jabber"); } |
---|
106 | sub is_icq { return (shift->{"type"} eq "icq"); } |
---|
107 | sub is_yahoo { return (shift->{"type"} eq "yahoo"); } |
---|
108 | sub is_msn { return (shift->{"type"} eq "msn"); } |
---|
109 | sub is_loopback { return (shift->{"type"} eq "loopback"); } |
---|
110 | |
---|
111 | # These are overridden by appropriate message types |
---|
112 | sub is_ping { return 0; } |
---|
113 | sub is_mail { return 0; } |
---|
114 | sub is_personal { return shift->is_private; } |
---|
115 | sub class { return undef; } |
---|
116 | sub instance { return undef; } |
---|
117 | sub realm { return undef; } |
---|
118 | sub opcode { return undef; } |
---|
119 | sub header { return undef; } |
---|
120 | sub host { return undef; } |
---|
121 | sub hostname { return undef; } |
---|
122 | sub auth { return undef; } |
---|
123 | sub fields { return undef; } |
---|
124 | sub zsig { return undef; } |
---|
125 | sub zwriteline { return undef; } |
---|
126 | sub login_host { return undef; } |
---|
127 | sub login_tty { return undef; } |
---|
128 | |
---|
129 | sub pretty_sender { return shift->sender; } |
---|
130 | |
---|
131 | sub delete { |
---|
132 | my ($m) = @_; |
---|
133 | &owl::command("delete --id ".$m->id); |
---|
134 | } |
---|
135 | |
---|
136 | sub undelete { |
---|
137 | my ($m) = @_; |
---|
138 | &owl::command("undelete --id ".$m->id); |
---|
139 | } |
---|
140 | |
---|
141 | # Serializes the message into something similar to the zwgc->vt format |
---|
142 | sub serialize { |
---|
143 | my ($this) = @_; |
---|
144 | my $s; |
---|
145 | for my $f (keys %$this) { |
---|
146 | my $val = $this->{$f}; |
---|
147 | if (ref($val) eq "ARRAY") { |
---|
148 | for my $i (0..@$val-1) { |
---|
149 | my $aval; |
---|
150 | $aval = $val->[$i]; |
---|
151 | $aval =~ s/\n/\n$f.$i: /g; |
---|
152 | $s .= "$f.$i: $aval\n"; |
---|
153 | } |
---|
154 | } else { |
---|
155 | $val =~ s/\n/\n$f: /g; |
---|
156 | $s .= "$f: $val\n"; |
---|
157 | } |
---|
158 | } |
---|
159 | return $s; |
---|
160 | } |
---|
161 | |
---|
162 | # Populate the annoying legacy global variables |
---|
163 | sub legacy_populate_global { |
---|
164 | my ($m) = @_; |
---|
165 | $owl::direction = $m->direction ; |
---|
166 | $owl::type = $m->type ; |
---|
167 | $owl::id = $m->id ; |
---|
168 | $owl::class = $m->class ; |
---|
169 | $owl::instance = $m->instance ; |
---|
170 | $owl::recipient = $m->recipient ; |
---|
171 | $owl::sender = $m->sender ; |
---|
172 | $owl::realm = $m->realm ; |
---|
173 | $owl::opcode = $m->opcode ; |
---|
174 | $owl::zsig = $m->zsig ; |
---|
175 | $owl::msg = $m->body ; |
---|
176 | $owl::time = $m->time ; |
---|
177 | $owl::host = $m->host ; |
---|
178 | $owl::login = $m->login ; |
---|
179 | $owl::auth = $m->auth ; |
---|
180 | if ($m->fields) { |
---|
181 | @owl::fields = @{$m->fields}; |
---|
182 | @main::fields = @{$m->fields}; |
---|
183 | } else { |
---|
184 | @owl::fields = undef; |
---|
185 | @main::fields = undef; |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | ##################################################################### |
---|
190 | ##################################################################### |
---|
191 | |
---|
192 | package owl::Message::Admin; |
---|
193 | |
---|
194 | @ISA = qw( owl::Message ); |
---|
195 | |
---|
196 | sub header { return shift->{"header"}; } |
---|
197 | |
---|
198 | ##################################################################### |
---|
199 | ##################################################################### |
---|
200 | |
---|
201 | package owl::Message::Generic; |
---|
202 | |
---|
203 | @ISA = qw( owl::Message ); |
---|
204 | |
---|
205 | ##################################################################### |
---|
206 | ##################################################################### |
---|
207 | |
---|
208 | package owl::Message::AIM; |
---|
209 | |
---|
210 | @ISA = qw( owl::Message ); |
---|
211 | |
---|
212 | # all non-loginout AIM messages are personal for now... |
---|
213 | sub is_personal { |
---|
214 | return !(shift->is_loginout); |
---|
215 | } |
---|
216 | |
---|
217 | ##################################################################### |
---|
218 | ##################################################################### |
---|
219 | |
---|
220 | package owl::Message::Zephyr; |
---|
221 | |
---|
222 | @ISA = qw( owl::Message ); |
---|
223 | |
---|
224 | sub login_tty { |
---|
225 | my ($m) = @_; |
---|
226 | return undef if (!$m->is_loginout); |
---|
227 | return $m->fields->[2]; |
---|
228 | } |
---|
229 | |
---|
230 | sub login_host { |
---|
231 | my ($m) = @_; |
---|
232 | return undef if (!$m->is_loginout); |
---|
233 | return $m->fields->[0]; |
---|
234 | } |
---|
235 | |
---|
236 | sub zwriteline { return shift->{"zwriteline"}; } |
---|
237 | |
---|
238 | sub zsig { return shift->{"zsig"}; } |
---|
239 | |
---|
240 | sub is_ping { return (lc(shift->opcode) eq "ping"); } |
---|
241 | |
---|
242 | sub is_personal { |
---|
243 | my ($m) = @_; |
---|
244 | return ((lc($m->class) eq "message") |
---|
245 | && (lc($m->instance) eq "personal") |
---|
246 | && $m->is_private); |
---|
247 | } |
---|
248 | |
---|
249 | sub is_mail { |
---|
250 | my ($m) = @_; |
---|
251 | return ((lc($m->class) eq "mail") && $m->is_private); |
---|
252 | } |
---|
253 | |
---|
254 | sub pretty_sender { |
---|
255 | my ($m) = @_; |
---|
256 | my $sender = $m->sender; |
---|
257 | my $realm = owl::zephyr_getrealm(); |
---|
258 | $sender =~ s/\@$realm$//; |
---|
259 | return $sender; |
---|
260 | } |
---|
261 | |
---|
262 | # These are arguably zephyr-specific |
---|
263 | sub class { return shift->{"class"}; } |
---|
264 | sub instance { return shift->{"instance"}; } |
---|
265 | sub realm { return shift->{"realm"}; } |
---|
266 | sub opcode { return shift->{"opcode"}; } |
---|
267 | sub host { return shift->{"hostname"}; } |
---|
268 | sub hostname { return shift->{"hostname"}; } |
---|
269 | sub header { return shift->{"header"}; } |
---|
270 | sub auth { return shift->{"auth"}; } |
---|
271 | sub fields { return shift->{"fields"}; } |
---|
272 | sub zsig { return shift->{"zsig"}; } |
---|
273 | |
---|
274 | ##################################################################### |
---|
275 | ##################################################################### |
---|
276 | |
---|
277 | package owl::Message::Jabber; |
---|
278 | |
---|
279 | @ISA = qw( owl::Message ); |
---|
280 | |
---|
281 | ##################################################################### |
---|
282 | ##################################################################### |
---|
283 | ################################################################################ |
---|
284 | package owl; |
---|
285 | |
---|
286 | # Arrays of subrefs to be called at specific times. |
---|
287 | our @onStartSubs = (); |
---|
288 | our @onReceiveMsg = (); |
---|
289 | our @onMainLoop = (); |
---|
290 | our @onGetBuddyList = (); |
---|
291 | |
---|
292 | ################################################################################ |
---|
293 | # Mainloop hook and threading. |
---|
294 | ################################################################################ |
---|
295 | |
---|
296 | use threads; |
---|
297 | use threads::shared; |
---|
298 | |
---|
299 | # Shared thread shutdown flag. |
---|
300 | # Consider adding a reload flag, so threads that should persist across reloads |
---|
301 | # can distinguish the two events. We wouldn't want a reload to cause us to |
---|
302 | # log out of and in to a perl-based IM session. |
---|
303 | our $shutdown : shared; |
---|
304 | $shutdown = 0; |
---|
305 | our $reload : shared; |
---|
306 | $reload = 0; |
---|
307 | |
---|
308 | # Functions to call hook lists |
---|
309 | sub runHook($@) |
---|
310 | { |
---|
311 | my $hook = shift; |
---|
312 | my @args = @_; |
---|
313 | $_->(@args) for (@$hook); |
---|
314 | } |
---|
315 | |
---|
316 | sub runHook_accumulate($@) |
---|
317 | { |
---|
318 | my $hook = shift; |
---|
319 | use Data::Dumper; |
---|
320 | my @args = @_; |
---|
321 | return join("\n", map {$_->(@args)} @$hook); |
---|
322 | } |
---|
323 | |
---|
324 | sub mainloop_hook |
---|
325 | { |
---|
326 | runHook(\@onMainLoop); |
---|
327 | } |
---|
328 | |
---|
329 | ################################################################################ |
---|
330 | # Startup and Shutdown code |
---|
331 | ################################################################################ |
---|
332 | sub startup |
---|
333 | { |
---|
334 | # Modern versions of owl provides a great place to have startup stuff. |
---|
335 | # Put things in ~/.owl/startup |
---|
336 | onStart(); |
---|
337 | } |
---|
338 | |
---|
339 | sub shutdown |
---|
340 | { |
---|
341 | # Modern versions of owl provides a great place to have shutdown stuff. |
---|
342 | # Put things in ~/.owl/shutdown |
---|
343 | |
---|
344 | # At this point I use owl::shutdown to tell any auxillary threads that they |
---|
345 | # should terminate. |
---|
346 | $shutdown = 1; |
---|
347 | mainloop_hook(); |
---|
348 | } |
---|
349 | |
---|
350 | #Run this on start and reload. Adds modules and runs their startup functions. |
---|
351 | sub onStart |
---|
352 | { |
---|
353 | reload_init(); |
---|
354 | #So that the user's .owlconf can have startsubs, we don't clear |
---|
355 | #onStartSubs; reload does however |
---|
356 | @onReceiveMsg = (); |
---|
357 | @onMainLoop = (); |
---|
358 | @onGetBuddyList = (); |
---|
359 | |
---|
360 | loadModules(); |
---|
361 | runHook(\@onStartSubs); |
---|
362 | } |
---|
363 | ################################################################################ |
---|
364 | # Reload Code, taken from /afs/sipb/user/jdaniel/project/owl/perl |
---|
365 | ################################################################################ |
---|
366 | sub reload_hook (@) |
---|
367 | { |
---|
368 | |
---|
369 | |
---|
370 | onStart(); |
---|
371 | return 1; |
---|
372 | } |
---|
373 | |
---|
374 | sub reload |
---|
375 | { |
---|
376 | # Shutdown existing threads. |
---|
377 | $reload = 1; |
---|
378 | owl::mainloop_hook(); |
---|
379 | $reload = 0; |
---|
380 | @onMainLoop = (); |
---|
381 | @onStartSubs = (); |
---|
382 | |
---|
383 | # Do reload |
---|
384 | package main; |
---|
385 | if (do "$ENV{HOME}/.owlconf" && owl::reload_hook(@_)) |
---|
386 | { |
---|
387 | return "owlconf reloaded"; |
---|
388 | } |
---|
389 | else |
---|
390 | { |
---|
391 | return "$ENV{HOME}/.owlconf load attempted, but error encountered:\n$@"; |
---|
392 | } |
---|
393 | package owl; |
---|
394 | } |
---|
395 | |
---|
396 | sub reload_init () |
---|
397 | { |
---|
398 | owl::command('alias reload perl owl::reload()'); |
---|
399 | owl::command('bindkey global "C-x C-r" command reload'); |
---|
400 | } |
---|
401 | |
---|
402 | ################################################################################ |
---|
403 | # Loads modules from ~/.owl/modules and owl's data directory |
---|
404 | ################################################################################ |
---|
405 | |
---|
406 | sub loadModules () { |
---|
407 | my @modules; |
---|
408 | foreach my $dir ( owl::get_data_dir() . "/owl/modules", |
---|
409 | $ENV{HOME} . "/.owl/modules" ) |
---|
410 | { |
---|
411 | opendir( MODULES, $dir ); |
---|
412 | |
---|
413 | # source ./modules/*.pl |
---|
414 | @modules = grep( /\.pl$/, readdir(MODULES) ); |
---|
415 | |
---|
416 | foreach my $mod (@modules) { |
---|
417 | unless (do "$dir/$mod") { |
---|
418 | if($!) { |
---|
419 | owl::error("Error loading $dir/$mod: $!"); |
---|
420 | } elsif($@) { |
---|
421 | owl::error("Error loading $dir/$mod: $@"); |
---|
422 | } |
---|
423 | } |
---|
424 | } |
---|
425 | closedir(MODULES); |
---|
426 | } |
---|
427 | |
---|
428 | } |
---|
429 | |
---|
430 | |
---|
431 | |
---|
432 | ################################################################################ |
---|
433 | # Hooks into receive_msg() |
---|
434 | ################################################################################ |
---|
435 | |
---|
436 | sub receive_msg |
---|
437 | { |
---|
438 | my $m = shift; |
---|
439 | runHook(\@onReceiveMsg, $m); |
---|
440 | } |
---|
441 | |
---|
442 | ################################################################################ |
---|
443 | # Hooks into get_blist() |
---|
444 | ################################################################################ |
---|
445 | |
---|
446 | sub get_blist |
---|
447 | { |
---|
448 | my $m = shift; |
---|
449 | return runHook_accumulate(\@onGetBuddyList, $m); |
---|
450 | } |
---|
451 | |
---|
452 | # switch to package main when we're done |
---|
453 | package main; |
---|
454 | # alias the hooks |
---|
455 | foreach my $hook qw (onStartSubs |
---|
456 | onReceiveMsg |
---|
457 | onMainLoop |
---|
458 | onGetBuddyList ) { |
---|
459 | *{"main::".$hook} = \*{"owl::".$hook}; |
---|
460 | } |
---|
461 | |
---|
462 | # load the config file |
---|
463 | if (-r $owl::configfile) { |
---|
464 | do $owl::configfile or die $@; |
---|
465 | } |
---|
466 | |
---|
467 | 1; |
---|