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