1 | # $Id$ |
---|
2 | # |
---|
3 | # This is all linked into the binary and evaluated when perl starts up... |
---|
4 | # |
---|
5 | ##################################################################### |
---|
6 | ##################################################################### |
---|
7 | # XXX NOTE: This file is sourced before almost any barnowl |
---|
8 | # architecture is loaded. This means, for example, that it cannot |
---|
9 | # execute any owl commands. Any code that needs to do so should live |
---|
10 | # in BarnOwl::Hooks::_startup |
---|
11 | |
---|
12 | use strict; |
---|
13 | use warnings; |
---|
14 | |
---|
15 | package BarnOwl; |
---|
16 | |
---|
17 | BEGIN { |
---|
18 | # bootstrap in C bindings and glue |
---|
19 | *owl:: = \*BarnOwl::; |
---|
20 | bootstrap BarnOwl 1.2; |
---|
21 | }; |
---|
22 | |
---|
23 | use lib(get_data_dir()."/lib"); |
---|
24 | use lib($ENV{HOME}."/.owl/lib"); |
---|
25 | |
---|
26 | our $configfile; |
---|
27 | |
---|
28 | if(!$configfile && -f $ENV{HOME} . "/.barnowlconf") { |
---|
29 | $configfile = $ENV{HOME} . "/.barnowlconf"; |
---|
30 | } |
---|
31 | $configfile ||= $ENV{HOME}."/.owlconf"; |
---|
32 | |
---|
33 | # populate global variable space for legacy owlconf files |
---|
34 | sub _format_msg_legacy_wrap { |
---|
35 | my ($m) = @_; |
---|
36 | $m->legacy_populate_global(); |
---|
37 | return &BarnOwl::format_msg($m); |
---|
38 | } |
---|
39 | |
---|
40 | # populate global variable space for legacy owlconf files |
---|
41 | sub _receive_msg_legacy_wrap { |
---|
42 | my ($m) = @_; |
---|
43 | $m->legacy_populate_global(); |
---|
44 | return &BarnOwl::Hooks::_receive_msg($m); |
---|
45 | } |
---|
46 | |
---|
47 | # make BarnOwl::<command>("foo") be aliases to BarnOwl::command("<command> foo"); |
---|
48 | sub AUTOLOAD { |
---|
49 | our $AUTOLOAD; |
---|
50 | my $called = $AUTOLOAD; |
---|
51 | $called =~ s/.*:://; |
---|
52 | $called =~ s/_/-/g; |
---|
53 | return &BarnOwl::command("$called ".join(" ",@_)); |
---|
54 | } |
---|
55 | |
---|
56 | =head2 new_command NAME FUNC [{ARGS}] |
---|
57 | |
---|
58 | Add a new owl command. When owl executes the command NAME, FUNC will |
---|
59 | be called with the arguments passed to the command, with NAME as the |
---|
60 | first argument. |
---|
61 | |
---|
62 | ARGS should be a hashref containing any or all of C<summary>, |
---|
63 | C<usage>, or C<description> keys. |
---|
64 | |
---|
65 | =cut |
---|
66 | |
---|
67 | sub new_command { |
---|
68 | my $name = shift; |
---|
69 | my $func = shift; |
---|
70 | my $args = shift || {}; |
---|
71 | my %args = ( |
---|
72 | summary => undef, |
---|
73 | usage => undef, |
---|
74 | description => undef, |
---|
75 | %{$args} |
---|
76 | ); |
---|
77 | |
---|
78 | no warnings 'uninitialized'; |
---|
79 | BarnOwl::new_command_internal($name, $func, $args{summary}, $args{usage}, $args{description}); |
---|
80 | } |
---|
81 | |
---|
82 | ##################################################################### |
---|
83 | ##################################################################### |
---|
84 | |
---|
85 | package BarnOwl::Message; |
---|
86 | |
---|
87 | sub new { |
---|
88 | my $class = shift; |
---|
89 | my %args = (@_); |
---|
90 | if($class eq __PACKAGE__ && $args{type}) { |
---|
91 | $class = "BarnOwl::Message::" . ucfirst $args{type}; |
---|
92 | } |
---|
93 | return bless {%args}, $class; |
---|
94 | } |
---|
95 | |
---|
96 | sub type { return shift->{"type"}; } |
---|
97 | sub direction { return shift->{"direction"}; } |
---|
98 | sub time { return shift->{"time"}; } |
---|
99 | sub id { return shift->{"id"}; } |
---|
100 | sub body { return shift->{"body"}; } |
---|
101 | sub sender { return shift->{"sender"}; } |
---|
102 | sub recipient { return shift->{"recipient"}; } |
---|
103 | sub login { return shift->{"login"}; } |
---|
104 | sub is_private { return shift->{"private"}; } |
---|
105 | |
---|
106 | sub is_login { return shift->login eq "login"; } |
---|
107 | sub is_logout { return shift->login eq "logout"; } |
---|
108 | sub is_loginout { my $m=shift; return ($m->is_login or $m->is_logout); } |
---|
109 | sub is_incoming { return (shift->{"direction"} eq "in"); } |
---|
110 | sub is_outgoing { return (shift->{"direction"} eq "out"); } |
---|
111 | |
---|
112 | sub is_deleted { return shift->{"deleted"}; } |
---|
113 | |
---|
114 | sub is_admin { return (shift->{"type"} eq "admin"); } |
---|
115 | sub is_generic { return (shift->{"type"} eq "generic"); } |
---|
116 | sub is_zephyr { return (shift->{"type"} eq "zephyr"); } |
---|
117 | sub is_aim { return (shift->{"type"} eq "AIM"); } |
---|
118 | sub is_jabber { return (shift->{"type"} eq "jabber"); } |
---|
119 | sub is_icq { return (shift->{"type"} eq "icq"); } |
---|
120 | sub is_yahoo { return (shift->{"type"} eq "yahoo"); } |
---|
121 | sub is_msn { return (shift->{"type"} eq "msn"); } |
---|
122 | sub is_loopback { return (shift->{"type"} eq "loopback"); } |
---|
123 | |
---|
124 | # These are overridden by appropriate message types |
---|
125 | sub is_ping { return 0; } |
---|
126 | sub is_mail { return 0; } |
---|
127 | sub is_personal { return shift->is_private; } |
---|
128 | sub class { return undef; } |
---|
129 | sub instance { return undef; } |
---|
130 | sub realm { return undef; } |
---|
131 | sub opcode { return undef; } |
---|
132 | sub header { return undef; } |
---|
133 | sub host { return undef; } |
---|
134 | sub hostname { return undef; } |
---|
135 | sub auth { return undef; } |
---|
136 | sub fields { return undef; } |
---|
137 | sub zsig { return undef; } |
---|
138 | sub zwriteline { return undef; } |
---|
139 | sub login_host { return undef; } |
---|
140 | sub login_tty { return undef; } |
---|
141 | |
---|
142 | sub pretty_sender { return shift->sender; } |
---|
143 | sub pretty_recipient { return shift->recipient; } |
---|
144 | |
---|
145 | sub delete { |
---|
146 | my ($m) = @_; |
---|
147 | &BarnOwl::command("delete --id ".$m->id); |
---|
148 | } |
---|
149 | |
---|
150 | sub undelete { |
---|
151 | my ($m) = @_; |
---|
152 | &BarnOwl::command("undelete --id ".$m->id); |
---|
153 | } |
---|
154 | |
---|
155 | # Serializes the message into something similar to the zwgc->vt format |
---|
156 | sub serialize { |
---|
157 | my ($this) = @_; |
---|
158 | my $s; |
---|
159 | for my $f (keys %$this) { |
---|
160 | my $val = $this->{$f}; |
---|
161 | if (ref($val) eq "ARRAY") { |
---|
162 | for my $i (0..@$val-1) { |
---|
163 | my $aval; |
---|
164 | $aval = $val->[$i]; |
---|
165 | $aval =~ s/\n/\n$f.$i: /g; |
---|
166 | $s .= "$f.$i: $aval\n"; |
---|
167 | } |
---|
168 | } else { |
---|
169 | $val =~ s/\n/\n$f: /g; |
---|
170 | $s .= "$f: $val\n"; |
---|
171 | } |
---|
172 | } |
---|
173 | return $s; |
---|
174 | } |
---|
175 | |
---|
176 | # Populate the annoying legacy global variables |
---|
177 | sub legacy_populate_global { |
---|
178 | my ($m) = @_; |
---|
179 | $BarnOwl::direction = $m->direction ; |
---|
180 | $BarnOwl::type = $m->type ; |
---|
181 | $BarnOwl::id = $m->id ; |
---|
182 | $BarnOwl::class = $m->class ; |
---|
183 | $BarnOwl::instance = $m->instance ; |
---|
184 | $BarnOwl::recipient = $m->recipient ; |
---|
185 | $BarnOwl::sender = $m->sender ; |
---|
186 | $BarnOwl::realm = $m->realm ; |
---|
187 | $BarnOwl::opcode = $m->opcode ; |
---|
188 | $BarnOwl::zsig = $m->zsig ; |
---|
189 | $BarnOwl::msg = $m->body ; |
---|
190 | $BarnOwl::time = $m->time ; |
---|
191 | $BarnOwl::host = $m->host ; |
---|
192 | $BarnOwl::login = $m->login ; |
---|
193 | $BarnOwl::auth = $m->auth ; |
---|
194 | if ($m->fields) { |
---|
195 | @BarnOwl::fields = @{$m->fields}; |
---|
196 | @main::fields = @{$m->fields}; |
---|
197 | } else { |
---|
198 | @BarnOwl::fields = undef; |
---|
199 | @main::fields = undef; |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | sub smartfilter { |
---|
204 | die("smartfilter not supported for this message\n"); |
---|
205 | } |
---|
206 | |
---|
207 | # Display fields -- overridden by subclasses when needed |
---|
208 | sub login_type {""} |
---|
209 | sub login_extra {""} |
---|
210 | sub long_sender {""} |
---|
211 | |
---|
212 | # The context in which a non-personal message was sent, e.g. a chat or |
---|
213 | # class |
---|
214 | sub context {""} |
---|
215 | |
---|
216 | # Some indicator of context *within* $self->context. e.g. the zephyr |
---|
217 | # instance |
---|
218 | sub subcontext {""} |
---|
219 | |
---|
220 | ##################################################################### |
---|
221 | ##################################################################### |
---|
222 | |
---|
223 | package BarnOwl::Message::Admin; |
---|
224 | |
---|
225 | use base qw( BarnOwl::Message ); |
---|
226 | |
---|
227 | sub header { return shift->{"header"}; } |
---|
228 | |
---|
229 | ##################################################################### |
---|
230 | ##################################################################### |
---|
231 | |
---|
232 | package BarnOwl::Message::Generic; |
---|
233 | |
---|
234 | use base qw( BarnOwl::Message ); |
---|
235 | |
---|
236 | ##################################################################### |
---|
237 | ##################################################################### |
---|
238 | |
---|
239 | package BarnOwl::Message::Loopback; |
---|
240 | |
---|
241 | use base qw( BarnOwl::Message ); |
---|
242 | |
---|
243 | # all loopback messages are private |
---|
244 | sub is_private { |
---|
245 | return 1; |
---|
246 | } |
---|
247 | |
---|
248 | ##################################################################### |
---|
249 | ##################################################################### |
---|
250 | |
---|
251 | package BarnOwl::Message::AIM; |
---|
252 | |
---|
253 | use base qw( BarnOwl::Message ); |
---|
254 | |
---|
255 | # all non-loginout AIM messages are private for now... |
---|
256 | sub is_private { |
---|
257 | return !(shift->is_loginout); |
---|
258 | } |
---|
259 | |
---|
260 | ##################################################################### |
---|
261 | ##################################################################### |
---|
262 | |
---|
263 | package BarnOwl::Message::Zephyr; |
---|
264 | |
---|
265 | use base qw( BarnOwl::Message ); |
---|
266 | |
---|
267 | sub login_type { |
---|
268 | return (shift->zsig eq "") ? "(PSEUDO)" : ""; |
---|
269 | } |
---|
270 | |
---|
271 | sub login_extra { |
---|
272 | my $m = shift; |
---|
273 | return undef if (!$m->is_loginout); |
---|
274 | my $s = lc($m->host); |
---|
275 | $s .= " " . $m->login_tty if defined $m->login_tty; |
---|
276 | return $s; |
---|
277 | } |
---|
278 | |
---|
279 | sub long_sender { |
---|
280 | my $m = shift; |
---|
281 | return $m->zsig; |
---|
282 | } |
---|
283 | |
---|
284 | sub context { |
---|
285 | return shift->class; |
---|
286 | } |
---|
287 | |
---|
288 | sub subcontext { |
---|
289 | return shift->instance; |
---|
290 | } |
---|
291 | |
---|
292 | sub login_tty { |
---|
293 | my ($m) = @_; |
---|
294 | return undef if (!$m->is_loginout); |
---|
295 | return $m->fields->[2]; |
---|
296 | } |
---|
297 | |
---|
298 | sub login_host { |
---|
299 | my ($m) = @_; |
---|
300 | return undef if (!$m->is_loginout); |
---|
301 | return $m->fields->[0]; |
---|
302 | } |
---|
303 | |
---|
304 | sub zwriteline { return shift->{"zwriteline"}; } |
---|
305 | |
---|
306 | sub is_ping { return (lc(shift->opcode) eq "ping"); } |
---|
307 | |
---|
308 | sub is_personal { |
---|
309 | my ($m) = @_; |
---|
310 | return ((lc($m->class) eq "message") |
---|
311 | && (lc($m->instance) eq "personal") |
---|
312 | && $m->is_private); |
---|
313 | } |
---|
314 | |
---|
315 | sub is_mail { |
---|
316 | my ($m) = @_; |
---|
317 | return ((lc($m->class) eq "mail") && $m->is_private); |
---|
318 | } |
---|
319 | |
---|
320 | sub pretty_sender { |
---|
321 | my ($m) = @_; |
---|
322 | my $sender = $m->sender; |
---|
323 | my $realm = BarnOwl::zephyr_getrealm(); |
---|
324 | $sender =~ s/\@$realm$//; |
---|
325 | return $sender; |
---|
326 | } |
---|
327 | |
---|
328 | sub pretty_recipient { |
---|
329 | my ($m) = @_; |
---|
330 | my $recip = $m->recipient; |
---|
331 | my $realm = BarnOwl::zephyr_getrealm(); |
---|
332 | $recip =~ s/\@$realm$//; |
---|
333 | return $recip; |
---|
334 | } |
---|
335 | |
---|
336 | # These are arguably zephyr-specific |
---|
337 | sub class { return shift->{"class"}; } |
---|
338 | sub instance { return shift->{"instance"}; } |
---|
339 | sub realm { return shift->{"realm"}; } |
---|
340 | sub opcode { return shift->{"opcode"}; } |
---|
341 | sub host { return shift->{"hostname"}; } |
---|
342 | sub hostname { return shift->{"hostname"}; } |
---|
343 | sub header { return shift->{"header"}; } |
---|
344 | sub auth { return shift->{"auth"}; } |
---|
345 | sub fields { return shift->{"fields"}; } |
---|
346 | sub zsig { return shift->{"zsig"}; } |
---|
347 | |
---|
348 | ##################################################################### |
---|
349 | ##################################################################### |
---|
350 | ################################################################################ |
---|
351 | |
---|
352 | package BarnOwl::Hook; |
---|
353 | |
---|
354 | sub new { |
---|
355 | my $class = shift; |
---|
356 | return bless [], $class; |
---|
357 | } |
---|
358 | |
---|
359 | sub run { |
---|
360 | my $self = shift; |
---|
361 | my @args = @_; |
---|
362 | return map {$_->(@args)} @$self; |
---|
363 | } |
---|
364 | |
---|
365 | sub add { |
---|
366 | my $self = shift; |
---|
367 | my $func = shift; |
---|
368 | die("Not a coderef!") unless ref($func) eq 'CODE'; |
---|
369 | push @$self, $func; |
---|
370 | } |
---|
371 | |
---|
372 | sub clear { |
---|
373 | my $self = shift; |
---|
374 | @$self = (); |
---|
375 | } |
---|
376 | |
---|
377 | package BarnOwl::Hooks; |
---|
378 | |
---|
379 | use Exporter; |
---|
380 | |
---|
381 | our @EXPORT_OK = qw($startup $shutdown |
---|
382 | $receiveMessage $mainLoop |
---|
383 | $getBuddyList); |
---|
384 | |
---|
385 | our %EXPORT_TAGS = (all => [@EXPORT_OK]); |
---|
386 | |
---|
387 | our $startup = BarnOwl::Hook->new; |
---|
388 | our $shutdown = BarnOwl::Hook->new; |
---|
389 | our $receiveMessage = BarnOwl::Hook->new; |
---|
390 | our $mainLoop = BarnOwl::Hook->new; |
---|
391 | our $getBuddyList = BarnOwl::Hook->new; |
---|
392 | |
---|
393 | # Internal startup/shutdown routines called by the C code |
---|
394 | |
---|
395 | sub _load_owlconf { |
---|
396 | # load the config file |
---|
397 | if ( -r $BarnOwl::configfile ) { |
---|
398 | undef $@; |
---|
399 | package main; |
---|
400 | do $BarnOwl::configfile; |
---|
401 | die $@ if $@; |
---|
402 | package BarnOwl; |
---|
403 | if(*BarnOwl::format_msg{CODE}) { |
---|
404 | # if the config defines a legacy formatting function, add 'perl' as a style |
---|
405 | BarnOwl::_create_style("perl", "BarnOwl::_format_msg_legacy_wrap", |
---|
406 | "User-defined perl style that calls BarnOwl::format_msg" |
---|
407 | . " with legacy global variable support"); |
---|
408 | BarnOwl::set("-q default_style perl"); |
---|
409 | } |
---|
410 | } |
---|
411 | } |
---|
412 | |
---|
413 | sub _startup { |
---|
414 | _load_owlconf(); |
---|
415 | |
---|
416 | if(eval {require BarnOwl::ModuleLoader}) { |
---|
417 | eval { |
---|
418 | BarnOwl::ModuleLoader->load_all; |
---|
419 | }; |
---|
420 | BarnOwl::error("$@") if $@; |
---|
421 | open TMP, ">/tmp/error"; |
---|
422 | print TMP $@; |
---|
423 | |
---|
424 | } else { |
---|
425 | BarnOwl::error("Can't load BarnOwl::ModuleLoader, loadable module support disabled:\n$@"); |
---|
426 | } |
---|
427 | |
---|
428 | $startup->run(0); |
---|
429 | BarnOwl::startup() if *BarnOwl::startup{CODE}; |
---|
430 | } |
---|
431 | |
---|
432 | sub _shutdown { |
---|
433 | $shutdown->run; |
---|
434 | |
---|
435 | BarnOwl::shutdown() if *BarnOwl::shutdown{CODE}; |
---|
436 | } |
---|
437 | |
---|
438 | sub _receive_msg { |
---|
439 | my $m = shift; |
---|
440 | |
---|
441 | $receiveMessage->run($m); |
---|
442 | |
---|
443 | BarnOwl::receive_msg($m) if *BarnOwl::receive_msg{CODE}; |
---|
444 | } |
---|
445 | |
---|
446 | sub _mainloop_hook { |
---|
447 | $mainLoop->run; |
---|
448 | BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE}; |
---|
449 | } |
---|
450 | |
---|
451 | sub _get_blist { |
---|
452 | return join("\n", $getBuddyList->run); |
---|
453 | } |
---|
454 | |
---|
455 | ################################################################################ |
---|
456 | # Built-in perl styles |
---|
457 | ################################################################################ |
---|
458 | package BarnOwl::Style::Default; |
---|
459 | ################################################################################ |
---|
460 | # Branching point for various formatting functions in this style. |
---|
461 | ################################################################################ |
---|
462 | sub format_message($) |
---|
463 | { |
---|
464 | my $m = shift; |
---|
465 | |
---|
466 | if ( $m->is_loginout) { |
---|
467 | return format_login($m); |
---|
468 | } elsif($m->is_ping) { |
---|
469 | return ( "\@b(PING) from \@b(" . $m->pretty_sender . ")\n" ); |
---|
470 | } elsif($m->is_admin) { |
---|
471 | return "\@bold(OWL ADMIN)\n" . indentBody($m); |
---|
472 | } else { |
---|
473 | return format_chat($m); |
---|
474 | } |
---|
475 | } |
---|
476 | |
---|
477 | BarnOwl::_create_style("default", "BarnOwl::Style::Default::format_message", "Default style"); |
---|
478 | |
---|
479 | ################################################################################ |
---|
480 | |
---|
481 | sub time_hhmm { |
---|
482 | my $m = shift; |
---|
483 | my ($time) = $m->time =~ /(\d\d:\d\d)/; |
---|
484 | return $time; |
---|
485 | } |
---|
486 | |
---|
487 | sub format_login($) { |
---|
488 | my $m = shift; |
---|
489 | return sprintf( |
---|
490 | '@b<%s%s> for @b(%s) (%s) %s', |
---|
491 | uc( $m->login ), |
---|
492 | $m->login_type, |
---|
493 | $m->pretty_sender, |
---|
494 | $m->login_extra, |
---|
495 | time_hhmm($m) |
---|
496 | ); |
---|
497 | } |
---|
498 | |
---|
499 | sub format_chat($) { |
---|
500 | my $m = shift; |
---|
501 | my $header; |
---|
502 | if ( $m->is_personal ) { |
---|
503 | if ( $m->direction eq "out" ) { |
---|
504 | $header = ucfirst $m->type . " sent to " . $m->pretty_recipient; |
---|
505 | } else { |
---|
506 | $header = ucfirst $m->type . " from " . $m->pretty_sender; |
---|
507 | } |
---|
508 | } else { |
---|
509 | $header = $m->context; |
---|
510 | if(defined $m->subcontext) { |
---|
511 | $header .= ' / ' . $m->subcontext; |
---|
512 | } |
---|
513 | $header .= ' / @b{' . $m->pretty_sender . '}'; |
---|
514 | } |
---|
515 | |
---|
516 | $header .= " " . time_hhmm($m); |
---|
517 | my $sender = $m->long_sender; |
---|
518 | $sender =~ s/\n.*$//s; |
---|
519 | $header .= " " x (4 - ((length $header) % 4)); |
---|
520 | $header .= "(" . $sender . ")"; |
---|
521 | my $message = $header . "\n". indentBody($m); |
---|
522 | if($m->is_personal && $m->direction eq "in") { |
---|
523 | $message = BarnOwl::Style::boldify($message); |
---|
524 | } |
---|
525 | return $message; |
---|
526 | } |
---|
527 | |
---|
528 | sub indentBody($) |
---|
529 | { |
---|
530 | my $m = shift; |
---|
531 | |
---|
532 | my $body = $m->body; |
---|
533 | if ($m->{should_wordwrap}) { |
---|
534 | $body = BarnOwl::wordwrap($body, BarnOwl::getnumcols()-8); |
---|
535 | } |
---|
536 | # replace newline followed by anything with |
---|
537 | # newline plus four spaces and that thing. |
---|
538 | $body =~ s/\n(.)/\n $1/g; |
---|
539 | |
---|
540 | return " ".$body; |
---|
541 | } |
---|
542 | |
---|
543 | |
---|
544 | package BarnOwl::Style; |
---|
545 | |
---|
546 | # This takes a zephyr to be displayed and modifies it to be displayed |
---|
547 | # entirely in bold. |
---|
548 | sub boldify($) |
---|
549 | { |
---|
550 | local $_ = shift; |
---|
551 | if ( !(/\)/) ) { |
---|
552 | return '@b(' . $_ . ')'; |
---|
553 | } elsif ( !(/\>/) ) { |
---|
554 | return '@b<' . $_ . '>'; |
---|
555 | } elsif ( !(/\}/) ) { |
---|
556 | return '@b{' . $_ . '}'; |
---|
557 | } elsif ( !(/\]/) ) { |
---|
558 | return '@b[' . $_ . ']'; |
---|
559 | } else { |
---|
560 | my $txt = "\@b($_"; |
---|
561 | $txt =~ s/\)/\)\@b\[\)\]\@b\(/g; |
---|
562 | return $txt . ')'; |
---|
563 | } |
---|
564 | } |
---|
565 | |
---|
566 | |
---|
567 | # switch to package main when we're done |
---|
568 | package main; |
---|
569 | |
---|
570 | # Shove a bunch of fake entries into @INC so modules can use or |
---|
571 | # require them without choking |
---|
572 | $::INC{$_} = 1 for (qw(BarnOwl.pm BarnOwl/Hooks.pm |
---|
573 | BarnOwl/Message.pm BarnOwl/Style.pm)); |
---|
574 | |
---|
575 | 1; |
---|
576 | |
---|