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