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 | =head1 NAME |
---|
18 | |
---|
19 | BarnOwl |
---|
20 | |
---|
21 | =head1 DESCRIPTION |
---|
22 | |
---|
23 | The BarnOwl module contains the core of BarnOwl's perl |
---|
24 | bindings. Source in this module is also run at startup to bootstrap |
---|
25 | barnowl by defining things like the default style. |
---|
26 | |
---|
27 | =for NOTE |
---|
28 | These following functions are defined in perlglue.xs. Keep the |
---|
29 | documentation here in sync with the user-visible commands defined |
---|
30 | there! |
---|
31 | |
---|
32 | =head2 command STRING |
---|
33 | |
---|
34 | Executes a BarnOwl command in the same manner as if the user had |
---|
35 | executed it at the BarnOwl command prompt. If the command returns a |
---|
36 | value, return it as a string, otherwise return undef. |
---|
37 | |
---|
38 | =head2 getcurmsg |
---|
39 | |
---|
40 | Returns the current message as a C<BarnOwl::Message> subclass, or |
---|
41 | undef if there is no message selected |
---|
42 | |
---|
43 | =head2 getnumcols |
---|
44 | |
---|
45 | Returns the width of the display window BarnOwl is currently using |
---|
46 | |
---|
47 | =head2 getidletime |
---|
48 | |
---|
49 | Returns the length of time since the user has pressed a key, in |
---|
50 | seconds. |
---|
51 | |
---|
52 | =head2 zephyr_getrealm |
---|
53 | |
---|
54 | Returns the zephyr realm barnowl is running in |
---|
55 | |
---|
56 | =head2 zephyr_getsender |
---|
57 | |
---|
58 | Returns the fully-qualified name of the zephyr sender barnowl is |
---|
59 | running as, e.g. C<nelhage@ATHENA.MIT.EDU> |
---|
60 | |
---|
61 | =head2 zephyr_zwrite COMMAND MESSAGE |
---|
62 | |
---|
63 | Sends a zephyr programmatically. C<COMMAND> should be a C<zwrite> |
---|
64 | command line, and C<MESSAGE> is the zephyr body to send. |
---|
65 | |
---|
66 | =head2 ztext_stylestrip STRING |
---|
67 | |
---|
68 | Strips zephyr formatting from a string and returns the result |
---|
69 | |
---|
70 | =head2 queue_message MESSAGE |
---|
71 | |
---|
72 | Enqueue a message in the BarnOwl message list, logging it and |
---|
73 | processing it appropriately. C<MESSAGE> should be an instance of |
---|
74 | BarnOwl::Message or a subclass. |
---|
75 | |
---|
76 | =head2 admin_message HEADER BODY |
---|
77 | |
---|
78 | Display a BarnOwl B<Admin> message, with the given header and body. |
---|
79 | |
---|
80 | =head2 start_question PROMPT CALLBACK |
---|
81 | |
---|
82 | Displays C<PROMPT> on the screen and lets the user enter a line of |
---|
83 | text, and calls C<CALLBACK>, which must be a perl subroutine |
---|
84 | reference, with the text the user entered |
---|
85 | |
---|
86 | =head2 start_password PROMPT CALLBACK |
---|
87 | |
---|
88 | Like C<start_question>, but echoes the user's input as C<*>s when they |
---|
89 | input. |
---|
90 | |
---|
91 | =head2 start_edit_win PROMPT CALLBACK |
---|
92 | |
---|
93 | Like C<start_question>, but displays C<PROMPT> on a line of its own |
---|
94 | and opens the editwin. If the user cancels the edit win, C<CALLBACK> |
---|
95 | is not invoked. |
---|
96 | |
---|
97 | =head2 get_data_dir |
---|
98 | |
---|
99 | Returns the BarnOwl system data directory, where system libraries and |
---|
100 | modules are stored |
---|
101 | |
---|
102 | =head2 get_config_dir |
---|
103 | |
---|
104 | Returns the BarnOwl user configuration directory, where user modules |
---|
105 | and configuration are stored (by default, C<$HOME/.owl>) |
---|
106 | |
---|
107 | =head2 popless_text TEXT |
---|
108 | |
---|
109 | Show a popup window containing the given C<TEXT> |
---|
110 | |
---|
111 | =head2 popless_ztext TEXT |
---|
112 | |
---|
113 | Show a popup window containing the provided zephyr-formatted C<TEXT> |
---|
114 | |
---|
115 | =head2 error STRING |
---|
116 | |
---|
117 | Reports an error and log it in `show errors'. Note that in any |
---|
118 | callback or hook called in perl code from BarnOwl, a C<die> will be |
---|
119 | caught and passed to C<error>. |
---|
120 | |
---|
121 | =head2 getnumcolors |
---|
122 | |
---|
123 | Returns the number of colors this BarnOwl is capable of displaying |
---|
124 | |
---|
125 | =head2 add_dispatch FD CALLBACK |
---|
126 | |
---|
127 | Adds a file descriptor to C<BarnOwl>'s internal C<select()> |
---|
128 | loop. C<CALLBACK> will be invoked whenever data is available to be |
---|
129 | read from C<FD>. |
---|
130 | |
---|
131 | =head2 remove_dispatch FD |
---|
132 | |
---|
133 | Remove a file descriptor previously registered via C<add_dispatch> |
---|
134 | |
---|
135 | =head2 create_style NAME OBJECT |
---|
136 | |
---|
137 | Creates a new barnowl style with the given NAME defined by the given |
---|
138 | object. The object must have a C<description> method which returns a |
---|
139 | string description of the style, and a and C<format_message> method |
---|
140 | which accepts a C<BarnOwl::Message> object and returns a string that |
---|
141 | is the result of formatting the message for display. |
---|
142 | |
---|
143 | =cut |
---|
144 | |
---|
145 | |
---|
146 | BEGIN { |
---|
147 | # bootstrap in C bindings and glue |
---|
148 | *owl:: = \*BarnOwl::; |
---|
149 | bootstrap BarnOwl 1.2; |
---|
150 | }; |
---|
151 | |
---|
152 | use lib(get_data_dir() . "/lib"); |
---|
153 | use lib(get_config_dir() . "/lib"); |
---|
154 | |
---|
155 | # perlconfig.c will set this to the value of the -c command-line |
---|
156 | # switch, if present. |
---|
157 | our $configfile; |
---|
158 | |
---|
159 | if(!$configfile && -f $ENV{HOME} . "/.barnowlconf") { |
---|
160 | $configfile = $ENV{HOME} . "/.barnowlconf"; |
---|
161 | } |
---|
162 | $configfile ||= $ENV{HOME}."/.owlconf"; |
---|
163 | |
---|
164 | # populate global variable space for legacy owlconf files |
---|
165 | sub _receive_msg_legacy_wrap { |
---|
166 | my ($m) = @_; |
---|
167 | $m->legacy_populate_global(); |
---|
168 | return &BarnOwl::Hooks::_receive_msg($m); |
---|
169 | } |
---|
170 | |
---|
171 | =head2 AUTOLOAD |
---|
172 | |
---|
173 | BarnOwl.pm has a C<AUTOLOAD> method that translates unused names in |
---|
174 | the BarnOwl:: namespace to a call to BarnOwl::command() with that |
---|
175 | command. Underscores are also translated to C<->s, so you can do |
---|
176 | e.g. C<BarnOwl::start_command()> and it will be translated into |
---|
177 | C<start-command>. |
---|
178 | |
---|
179 | So, if you're looking for functionality that you can't find in the |
---|
180 | perl interface, check C<:show commands> or C<commands.c> in the |
---|
181 | BarnOwl source tree -- there's a good chance it exists as a BarnOwl |
---|
182 | command. |
---|
183 | |
---|
184 | =head3 BUGS |
---|
185 | |
---|
186 | There are horrible quoting issues here. The AUTOLOAD simple joins your |
---|
187 | commands with spaces and passes them unmodified to C<::command> |
---|
188 | |
---|
189 | =cut |
---|
190 | |
---|
191 | # make BarnOwl::<command>("foo") be aliases to BarnOwl::command("<command> foo"); |
---|
192 | sub AUTOLOAD { |
---|
193 | our $AUTOLOAD; |
---|
194 | my $called = $AUTOLOAD; |
---|
195 | $called =~ s/.*:://; |
---|
196 | $called =~ s/_/-/g; |
---|
197 | return &BarnOwl::command("$called ".join(" ",@_)); |
---|
198 | } |
---|
199 | |
---|
200 | =head2 new_command NAME FUNC [{ARGS}] |
---|
201 | |
---|
202 | Add a new owl command. When owl executes the command NAME, FUNC will |
---|
203 | be called with the arguments passed to the command, with NAME as the |
---|
204 | first argument. |
---|
205 | |
---|
206 | ARGS should be a hashref containing any or all of C<summary>, |
---|
207 | C<usage>, or C<description> keys: |
---|
208 | |
---|
209 | =over 4 |
---|
210 | |
---|
211 | =item summary |
---|
212 | |
---|
213 | A one-line summary of the purpose of the command |
---|
214 | |
---|
215 | =item usage |
---|
216 | |
---|
217 | A one-line usage synopsis, showing available options and syntax |
---|
218 | |
---|
219 | =item description |
---|
220 | |
---|
221 | A longer description of the syntax and semantics of the command, |
---|
222 | explaining usage and options |
---|
223 | |
---|
224 | =back |
---|
225 | |
---|
226 | =cut |
---|
227 | |
---|
228 | sub new_command { |
---|
229 | my $name = shift; |
---|
230 | my $func = shift; |
---|
231 | my $args = shift || {}; |
---|
232 | my %args = ( |
---|
233 | summary => "", |
---|
234 | usage => "", |
---|
235 | description => "", |
---|
236 | %{$args} |
---|
237 | ); |
---|
238 | |
---|
239 | BarnOwl::new_command_internal($name, $func, $args{summary}, $args{usage}, $args{description}); |
---|
240 | } |
---|
241 | |
---|
242 | =head2 new_variable_int NAME [{ARGS}] |
---|
243 | |
---|
244 | =head2 new_variable_bool NAME [{ARGS}] |
---|
245 | |
---|
246 | =head2 new_variable_string NAME [{ARGS}] |
---|
247 | |
---|
248 | Add a new owl variable, either an int, a bool, or a string, with the |
---|
249 | specified name. |
---|
250 | |
---|
251 | ARGS can optionally contain the following keys: |
---|
252 | |
---|
253 | =over 4 |
---|
254 | |
---|
255 | =item default |
---|
256 | |
---|
257 | The default and initial value for the variable |
---|
258 | |
---|
259 | =item summary |
---|
260 | |
---|
261 | A one-line summary of the variable's purpose |
---|
262 | |
---|
263 | =item description |
---|
264 | |
---|
265 | A longer description of the function of the variable |
---|
266 | |
---|
267 | =back |
---|
268 | |
---|
269 | =cut |
---|
270 | |
---|
271 | sub new_variable_int { |
---|
272 | unshift @_, \&BarnOwl::new_variable_int_internal, 0; |
---|
273 | goto \&_new_variable; |
---|
274 | } |
---|
275 | |
---|
276 | sub new_variable_bool { |
---|
277 | unshift @_, \&BarnOwl::new_variable_bool_internal, 0; |
---|
278 | goto \&_new_variable; |
---|
279 | } |
---|
280 | |
---|
281 | sub new_variable_string { |
---|
282 | unshift @_, \&BarnOwl::new_variable_string_internal, ""; |
---|
283 | goto \&_new_variable; |
---|
284 | } |
---|
285 | |
---|
286 | sub _new_variable { |
---|
287 | my $func = shift; |
---|
288 | my $default_default = shift; |
---|
289 | my $name = shift; |
---|
290 | my $args = shift || {}; |
---|
291 | my %args = ( |
---|
292 | summary => "", |
---|
293 | description => "", |
---|
294 | default => $default_default, |
---|
295 | %{$args}); |
---|
296 | $func->($name, $args{default}, $args{summary}, $args{description}); |
---|
297 | } |
---|
298 | |
---|
299 | ##################################################################### |
---|
300 | ##################################################################### |
---|
301 | |
---|
302 | package BarnOwl::Message; |
---|
303 | |
---|
304 | sub new { |
---|
305 | my $class = shift; |
---|
306 | my %args = (@_); |
---|
307 | if($class eq __PACKAGE__ && $args{type}) { |
---|
308 | $class = "BarnOwl::Message::" . ucfirst $args{type}; |
---|
309 | } |
---|
310 | return bless {%args}, $class; |
---|
311 | } |
---|
312 | |
---|
313 | sub type { return shift->{"type"}; } |
---|
314 | sub direction { return shift->{"direction"}; } |
---|
315 | sub time { return shift->{"time"}; } |
---|
316 | sub id { return shift->{"id"}; } |
---|
317 | sub body { return shift->{"body"}; } |
---|
318 | sub sender { return shift->{"sender"}; } |
---|
319 | sub recipient { return shift->{"recipient"}; } |
---|
320 | sub login { return shift->{"login"}; } |
---|
321 | sub is_private { return shift->{"private"}; } |
---|
322 | |
---|
323 | sub is_login { return shift->login eq "login"; } |
---|
324 | sub is_logout { return shift->login eq "logout"; } |
---|
325 | sub is_loginout { my $m=shift; return ($m->is_login or $m->is_logout); } |
---|
326 | sub is_incoming { return (shift->{"direction"} eq "in"); } |
---|
327 | sub is_outgoing { return (shift->{"direction"} eq "out"); } |
---|
328 | |
---|
329 | sub is_deleted { return shift->{"deleted"}; } |
---|
330 | |
---|
331 | sub is_admin { return (shift->{"type"} eq "admin"); } |
---|
332 | sub is_generic { return (shift->{"type"} eq "generic"); } |
---|
333 | sub is_zephyr { return (shift->{"type"} eq "zephyr"); } |
---|
334 | sub is_aim { return (shift->{"type"} eq "AIM"); } |
---|
335 | sub is_jabber { return (shift->{"type"} eq "jabber"); } |
---|
336 | sub is_icq { return (shift->{"type"} eq "icq"); } |
---|
337 | sub is_yahoo { return (shift->{"type"} eq "yahoo"); } |
---|
338 | sub is_msn { return (shift->{"type"} eq "msn"); } |
---|
339 | sub is_loopback { return (shift->{"type"} eq "loopback"); } |
---|
340 | |
---|
341 | # These are overridden by appropriate message types |
---|
342 | sub is_ping { return 0; } |
---|
343 | sub is_mail { return 0; } |
---|
344 | sub is_personal { return shift->is_private; } |
---|
345 | sub class { return undef; } |
---|
346 | sub instance { return undef; } |
---|
347 | sub realm { return undef; } |
---|
348 | sub opcode { return undef; } |
---|
349 | sub header { return undef; } |
---|
350 | sub host { return undef; } |
---|
351 | sub hostname { return undef; } |
---|
352 | sub auth { return undef; } |
---|
353 | sub fields { return undef; } |
---|
354 | sub zsig { return undef; } |
---|
355 | sub zwriteline { return undef; } |
---|
356 | sub login_host { return undef; } |
---|
357 | sub login_tty { return undef; } |
---|
358 | |
---|
359 | sub pretty_sender { return shift->sender; } |
---|
360 | sub pretty_recipient { return shift->recipient; } |
---|
361 | |
---|
362 | sub delete { |
---|
363 | my ($m) = @_; |
---|
364 | &BarnOwl::command("delete --id ".$m->id); |
---|
365 | } |
---|
366 | |
---|
367 | sub undelete { |
---|
368 | my ($m) = @_; |
---|
369 | &BarnOwl::command("undelete --id ".$m->id); |
---|
370 | } |
---|
371 | |
---|
372 | # Serializes the message into something similar to the zwgc->vt format |
---|
373 | sub serialize { |
---|
374 | my ($this) = @_; |
---|
375 | my $s; |
---|
376 | for my $f (keys %$this) { |
---|
377 | my $val = $this->{$f}; |
---|
378 | if (ref($val) eq "ARRAY") { |
---|
379 | for my $i (0..@$val-1) { |
---|
380 | my $aval; |
---|
381 | $aval = $val->[$i]; |
---|
382 | $aval =~ s/\n/\n$f.$i: /g; |
---|
383 | $s .= "$f.$i: $aval\n"; |
---|
384 | } |
---|
385 | } else { |
---|
386 | $val =~ s/\n/\n$f: /g; |
---|
387 | $s .= "$f: $val\n"; |
---|
388 | } |
---|
389 | } |
---|
390 | return $s; |
---|
391 | } |
---|
392 | |
---|
393 | # Populate the annoying legacy global variables |
---|
394 | sub legacy_populate_global { |
---|
395 | my ($m) = @_; |
---|
396 | $BarnOwl::direction = $m->direction ; |
---|
397 | $BarnOwl::type = $m->type ; |
---|
398 | $BarnOwl::id = $m->id ; |
---|
399 | $BarnOwl::class = $m->class ; |
---|
400 | $BarnOwl::instance = $m->instance ; |
---|
401 | $BarnOwl::recipient = $m->recipient ; |
---|
402 | $BarnOwl::sender = $m->sender ; |
---|
403 | $BarnOwl::realm = $m->realm ; |
---|
404 | $BarnOwl::opcode = $m->opcode ; |
---|
405 | $BarnOwl::zsig = $m->zsig ; |
---|
406 | $BarnOwl::msg = $m->body ; |
---|
407 | $BarnOwl::time = $m->time ; |
---|
408 | $BarnOwl::host = $m->host ; |
---|
409 | $BarnOwl::login = $m->login ; |
---|
410 | $BarnOwl::auth = $m->auth ; |
---|
411 | if ($m->fields) { |
---|
412 | @BarnOwl::fields = @{$m->fields}; |
---|
413 | @main::fields = @{$m->fields}; |
---|
414 | } else { |
---|
415 | @BarnOwl::fields = undef; |
---|
416 | @main::fields = undef; |
---|
417 | } |
---|
418 | } |
---|
419 | |
---|
420 | sub smartfilter { |
---|
421 | die("smartfilter not supported for this message\n"); |
---|
422 | } |
---|
423 | |
---|
424 | # Display fields -- overridden by subclasses when needed |
---|
425 | sub login_type {""} |
---|
426 | sub login_extra {""} |
---|
427 | sub long_sender {""} |
---|
428 | |
---|
429 | # The context in which a non-personal message was sent, e.g. a chat or |
---|
430 | # class |
---|
431 | sub context {""} |
---|
432 | |
---|
433 | # Some indicator of context *within* $self->context. e.g. the zephyr |
---|
434 | # instance |
---|
435 | sub subcontext {""} |
---|
436 | |
---|
437 | ##################################################################### |
---|
438 | ##################################################################### |
---|
439 | |
---|
440 | package BarnOwl::Message::Admin; |
---|
441 | |
---|
442 | use base qw( BarnOwl::Message ); |
---|
443 | |
---|
444 | sub header { return shift->{"header"}; } |
---|
445 | |
---|
446 | ##################################################################### |
---|
447 | ##################################################################### |
---|
448 | |
---|
449 | package BarnOwl::Message::Generic; |
---|
450 | |
---|
451 | use base qw( BarnOwl::Message ); |
---|
452 | |
---|
453 | ##################################################################### |
---|
454 | ##################################################################### |
---|
455 | |
---|
456 | package BarnOwl::Message::Loopback; |
---|
457 | |
---|
458 | use base qw( BarnOwl::Message ); |
---|
459 | |
---|
460 | # all loopback messages are private |
---|
461 | sub is_private { |
---|
462 | return 1; |
---|
463 | } |
---|
464 | |
---|
465 | ##################################################################### |
---|
466 | ##################################################################### |
---|
467 | |
---|
468 | package BarnOwl::Message::AIM; |
---|
469 | |
---|
470 | use base qw( BarnOwl::Message ); |
---|
471 | |
---|
472 | # all non-loginout AIM messages are private for now... |
---|
473 | sub is_private { |
---|
474 | return !(shift->is_loginout); |
---|
475 | } |
---|
476 | |
---|
477 | ##################################################################### |
---|
478 | ##################################################################### |
---|
479 | |
---|
480 | package BarnOwl::Message::Zephyr; |
---|
481 | |
---|
482 | use base qw( BarnOwl::Message ); |
---|
483 | |
---|
484 | sub login_type { |
---|
485 | return (shift->zsig eq "") ? "(PSEUDO)" : ""; |
---|
486 | } |
---|
487 | |
---|
488 | sub login_extra { |
---|
489 | my $m = shift; |
---|
490 | return undef if (!$m->is_loginout); |
---|
491 | my $s = lc($m->host); |
---|
492 | $s .= " " . $m->login_tty if defined $m->login_tty; |
---|
493 | return $s; |
---|
494 | } |
---|
495 | |
---|
496 | sub long_sender { |
---|
497 | my $m = shift; |
---|
498 | return $m->zsig; |
---|
499 | } |
---|
500 | |
---|
501 | sub context { |
---|
502 | return shift->class; |
---|
503 | } |
---|
504 | |
---|
505 | sub subcontext { |
---|
506 | return shift->instance; |
---|
507 | } |
---|
508 | |
---|
509 | sub login_tty { |
---|
510 | my ($m) = @_; |
---|
511 | return undef if (!$m->is_loginout); |
---|
512 | return $m->fields->[2]; |
---|
513 | } |
---|
514 | |
---|
515 | sub login_host { |
---|
516 | my ($m) = @_; |
---|
517 | return undef if (!$m->is_loginout); |
---|
518 | return $m->fields->[0]; |
---|
519 | } |
---|
520 | |
---|
521 | sub zwriteline { return shift->{"zwriteline"}; } |
---|
522 | |
---|
523 | sub is_ping { return (lc(shift->opcode) eq "ping"); } |
---|
524 | |
---|
525 | sub is_personal { |
---|
526 | my ($m) = @_; |
---|
527 | return ((lc($m->class) eq "message") |
---|
528 | && (lc($m->instance) eq "personal") |
---|
529 | && $m->is_private); |
---|
530 | } |
---|
531 | |
---|
532 | sub is_mail { |
---|
533 | my ($m) = @_; |
---|
534 | return ((lc($m->class) eq "mail") && $m->is_private); |
---|
535 | } |
---|
536 | |
---|
537 | sub pretty_sender { |
---|
538 | my ($m) = @_; |
---|
539 | my $sender = $m->sender; |
---|
540 | my $realm = BarnOwl::zephyr_getrealm(); |
---|
541 | $sender =~ s/\@$realm$//; |
---|
542 | return $sender; |
---|
543 | } |
---|
544 | |
---|
545 | sub pretty_recipient { |
---|
546 | my ($m) = @_; |
---|
547 | my $recip = $m->recipient; |
---|
548 | my $realm = BarnOwl::zephyr_getrealm(); |
---|
549 | $recip =~ s/\@$realm$//; |
---|
550 | return $recip; |
---|
551 | } |
---|
552 | |
---|
553 | # These are arguably zephyr-specific |
---|
554 | sub class { return shift->{"class"}; } |
---|
555 | sub instance { return shift->{"instance"}; } |
---|
556 | sub realm { return shift->{"realm"}; } |
---|
557 | sub opcode { return shift->{"opcode"}; } |
---|
558 | sub host { return shift->{"hostname"}; } |
---|
559 | sub hostname { return shift->{"hostname"}; } |
---|
560 | sub header { return shift->{"header"}; } |
---|
561 | sub auth { return shift->{"auth"}; } |
---|
562 | sub fields { return shift->{"fields"}; } |
---|
563 | sub zsig { return shift->{"zsig"}; } |
---|
564 | |
---|
565 | ##################################################################### |
---|
566 | ##################################################################### |
---|
567 | ################################################################################ |
---|
568 | |
---|
569 | package BarnOwl::Hook; |
---|
570 | |
---|
571 | =head1 BarnOwl::Hook |
---|
572 | |
---|
573 | =head1 DESCRIPTION |
---|
574 | |
---|
575 | A C<BarnOwl::Hook> represents a list of functions to be triggered on |
---|
576 | some event. C<BarnOwl> exports a default set of these (see |
---|
577 | C<BarnOwl::Hooks>), but can also be created and used by module code. |
---|
578 | |
---|
579 | =head2 new |
---|
580 | |
---|
581 | Creates a new Hook object |
---|
582 | |
---|
583 | =cut |
---|
584 | |
---|
585 | sub new { |
---|
586 | my $class = shift; |
---|
587 | return bless [], $class; |
---|
588 | } |
---|
589 | |
---|
590 | =head2 run [ARGS] |
---|
591 | |
---|
592 | Calls each of the functions registered with this hook with the given |
---|
593 | arguments. |
---|
594 | |
---|
595 | =cut |
---|
596 | |
---|
597 | sub run { |
---|
598 | my $self = shift; |
---|
599 | my @args = @_; |
---|
600 | return map {$_->(@args)} @$self; |
---|
601 | } |
---|
602 | |
---|
603 | =head2 add SUBREF |
---|
604 | |
---|
605 | Registers a given subroutine with this hook |
---|
606 | |
---|
607 | =cut |
---|
608 | |
---|
609 | sub add { |
---|
610 | my $self = shift; |
---|
611 | my $func = shift; |
---|
612 | die("Not a coderef!") unless ref($func) eq 'CODE'; |
---|
613 | push @$self, $func; |
---|
614 | } |
---|
615 | |
---|
616 | =head2 clear |
---|
617 | |
---|
618 | Remove all functions registered with this hook. |
---|
619 | |
---|
620 | =cut |
---|
621 | |
---|
622 | sub clear { |
---|
623 | my $self = shift; |
---|
624 | @$self = (); |
---|
625 | } |
---|
626 | |
---|
627 | package BarnOwl::Hooks; |
---|
628 | |
---|
629 | =head1 BarnOwl::Hooks |
---|
630 | |
---|
631 | =head1 DESCRIPTION |
---|
632 | |
---|
633 | C<BarnOwl::Hooks> exports a set of C<BarnOwl::Hook> objects made |
---|
634 | available by BarnOwl internally. |
---|
635 | |
---|
636 | =head2 USAGE |
---|
637 | |
---|
638 | Modules wishing to respond to events in BarnOwl should register |
---|
639 | functions with these hooks. |
---|
640 | |
---|
641 | =head2 EXPORTS |
---|
642 | |
---|
643 | None by default. Either import the hooks you need explicitly, or refer |
---|
644 | to them with fully-qualified names. Available hooks are: |
---|
645 | |
---|
646 | =over 4 |
---|
647 | |
---|
648 | =item $startup |
---|
649 | |
---|
650 | Called on BarnOwl startup, and whenever modules are |
---|
651 | reloaded. Functions registered with the C<$startup> hook get a true |
---|
652 | argument if this is a reload, and false if this is a true startup |
---|
653 | |
---|
654 | =item $shutdown |
---|
655 | |
---|
656 | Called before BarnOwl shutdown |
---|
657 | |
---|
658 | =item $receiveMessage |
---|
659 | |
---|
660 | Called with a C<BarnOwl::Message> object every time BarnOwl appends a |
---|
661 | new message to its message list |
---|
662 | |
---|
663 | =item $mainLoop |
---|
664 | |
---|
665 | Called on every pass through the C<BarnOwl> main loop. This is |
---|
666 | guaranteed to be called at least once/sec and may be called more |
---|
667 | frequently. |
---|
668 | |
---|
669 | =item $getBuddyList |
---|
670 | |
---|
671 | Called to display buddy lists for all protocol handlers. The result |
---|
672 | from every function registered with this hook will be appended and |
---|
673 | displayed in a popup window, with zephyr formatting parsed. |
---|
674 | |
---|
675 | =back |
---|
676 | |
---|
677 | =cut |
---|
678 | |
---|
679 | use Exporter; |
---|
680 | |
---|
681 | our @EXPORT_OK = qw($startup $shutdown |
---|
682 | $receiveMessage $mainLoop |
---|
683 | $getBuddyList); |
---|
684 | |
---|
685 | our %EXPORT_TAGS = (all => [@EXPORT_OK]); |
---|
686 | |
---|
687 | our $startup = BarnOwl::Hook->new; |
---|
688 | our $shutdown = BarnOwl::Hook->new; |
---|
689 | our $receiveMessage = BarnOwl::Hook->new; |
---|
690 | our $mainLoop = BarnOwl::Hook->new; |
---|
691 | our $getBuddyList = BarnOwl::Hook->new; |
---|
692 | |
---|
693 | # Internal startup/shutdown routines called by the C code |
---|
694 | |
---|
695 | sub _load_perl_commands { |
---|
696 | # Load builtin perl commands |
---|
697 | BarnOwl::new_command(style => \&BarnOwl::Style::style_command, |
---|
698 | { |
---|
699 | summary => "creates a new style", |
---|
700 | usage => "style <name> perl <function_name>", |
---|
701 | description => |
---|
702 | "A style named <name> will be created that will\n" . |
---|
703 | "format messages using the perl function <function_name>.\n\n" . |
---|
704 | "SEE ALSO: show styles, view -s, filter -s\n\n" . |
---|
705 | "DEPRECATED in favor of BarnOwl::create_style(NAME, OBJECT)", |
---|
706 | }); |
---|
707 | } |
---|
708 | |
---|
709 | sub _load_owlconf { |
---|
710 | # load the config file |
---|
711 | if ( -r $BarnOwl::configfile ) { |
---|
712 | undef $@; |
---|
713 | package main; |
---|
714 | do $BarnOwl::configfile; |
---|
715 | die $@ if $@; |
---|
716 | package BarnOwl; |
---|
717 | if(*BarnOwl::format_msg{CODE}) { |
---|
718 | # if the config defines a legacy formatting function, add 'perl' as a style |
---|
719 | BarnOwl::create_style("perl", BarnOwl::Style::Legacy->new( |
---|
720 | "BarnOwl::format_msg", |
---|
721 | "User-defined perl style that calls BarnOwl::format_msg" |
---|
722 | . " with legacy global variable support", |
---|
723 | 1)); |
---|
724 | BarnOwl::set("-q default_style perl"); |
---|
725 | } |
---|
726 | } |
---|
727 | } |
---|
728 | |
---|
729 | # These are the internal hooks called by the barnowl C code, which |
---|
730 | # take care of dispatching to the appropriate perl hooks, and deal |
---|
731 | # with compatibility by calling the old, fixed-name hooks. |
---|
732 | |
---|
733 | sub _startup { |
---|
734 | _load_perl_commands(); |
---|
735 | _load_owlconf(); |
---|
736 | |
---|
737 | if(eval {require BarnOwl::ModuleLoader}) { |
---|
738 | eval { |
---|
739 | BarnOwl::ModuleLoader->load_all; |
---|
740 | }; |
---|
741 | BarnOwl::error("$@") if $@; |
---|
742 | |
---|
743 | } else { |
---|
744 | BarnOwl::error("Can't load BarnOwl::ModuleLoader, loadable module support disabled:\n$@"); |
---|
745 | } |
---|
746 | |
---|
747 | $startup->run(0); |
---|
748 | BarnOwl::startup() if *BarnOwl::startup{CODE}; |
---|
749 | } |
---|
750 | |
---|
751 | sub _shutdown { |
---|
752 | $shutdown->run; |
---|
753 | |
---|
754 | BarnOwl::shutdown() if *BarnOwl::shutdown{CODE}; |
---|
755 | } |
---|
756 | |
---|
757 | sub _receive_msg { |
---|
758 | my $m = shift; |
---|
759 | |
---|
760 | $receiveMessage->run($m); |
---|
761 | |
---|
762 | BarnOwl::receive_msg($m) if *BarnOwl::receive_msg{CODE}; |
---|
763 | } |
---|
764 | |
---|
765 | sub _mainloop_hook { |
---|
766 | $mainLoop->run; |
---|
767 | BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE}; |
---|
768 | } |
---|
769 | |
---|
770 | sub _get_blist { |
---|
771 | return join("\n", $getBuddyList->run); |
---|
772 | } |
---|
773 | |
---|
774 | ################################################################################ |
---|
775 | # Built-in perl styles |
---|
776 | ################################################################################ |
---|
777 | package BarnOwl::Style::Default; |
---|
778 | ################################################################################ |
---|
779 | # Branching point for various formatting functions in this style. |
---|
780 | ################################################################################ |
---|
781 | sub format_message |
---|
782 | { |
---|
783 | my $self = shift; |
---|
784 | my $m = shift; |
---|
785 | my $fmt; |
---|
786 | |
---|
787 | if ( $m->is_loginout) { |
---|
788 | $fmt = $self->format_login($m); |
---|
789 | } elsif($m->is_ping && $m->is_personal) { |
---|
790 | $fmt = $self->format_ping($m); |
---|
791 | } elsif($m->is_admin) { |
---|
792 | $fmt = $self->format_admin($m); |
---|
793 | } else { |
---|
794 | $fmt = $self->format_chat($m); |
---|
795 | } |
---|
796 | $fmt = BarnOwl::Style::boldify($fmt) if $self->should_bold($m); |
---|
797 | return $fmt; |
---|
798 | } |
---|
799 | |
---|
800 | sub should_bold { |
---|
801 | my $self = shift; |
---|
802 | my $m = shift; |
---|
803 | return $m->is_personal && $m->direction eq "in"; |
---|
804 | } |
---|
805 | |
---|
806 | sub description {"Default style";} |
---|
807 | |
---|
808 | BarnOwl::create_style("default", "BarnOwl::Style::Default"); |
---|
809 | |
---|
810 | ################################################################################ |
---|
811 | |
---|
812 | sub format_time { |
---|
813 | my $self = shift; |
---|
814 | my $m = shift; |
---|
815 | my ($time) = $m->time =~ /(\d\d:\d\d)/; |
---|
816 | return $time; |
---|
817 | } |
---|
818 | |
---|
819 | sub format_login { |
---|
820 | my $self = shift; |
---|
821 | my $m = shift; |
---|
822 | return sprintf( |
---|
823 | '@b<%s%s> for @b(%s) (%s) %s', |
---|
824 | uc( $m->login ), |
---|
825 | $m->login_type, |
---|
826 | $m->pretty_sender, |
---|
827 | $m->login_extra, |
---|
828 | $self->format_time($m) |
---|
829 | ); |
---|
830 | } |
---|
831 | |
---|
832 | sub format_ping { |
---|
833 | my $self = shift; |
---|
834 | my $m = shift; |
---|
835 | return "\@b(PING) from \@b(" . $m->pretty_sender . ")"; |
---|
836 | } |
---|
837 | |
---|
838 | sub format_admin { |
---|
839 | my $self = shift; |
---|
840 | my $m = shift; |
---|
841 | return "\@bold(OWL ADMIN)\n" . $self->indent_body($m); |
---|
842 | } |
---|
843 | |
---|
844 | sub format_chat { |
---|
845 | my $self = shift; |
---|
846 | my $m = shift; |
---|
847 | my $header = $self->chat_header($m); |
---|
848 | return $header . "\n". $self->indent_body($m); |
---|
849 | } |
---|
850 | |
---|
851 | sub chat_header { |
---|
852 | my $self = shift; |
---|
853 | my $m = shift; |
---|
854 | my $header; |
---|
855 | if ( $m->is_personal ) { |
---|
856 | if ( $m->direction eq "out" ) { |
---|
857 | $header = ucfirst $m->type . " sent to " . $m->pretty_recipient; |
---|
858 | } else { |
---|
859 | $header = ucfirst $m->type . " from " . $m->pretty_sender; |
---|
860 | } |
---|
861 | } else { |
---|
862 | $header = $m->context; |
---|
863 | if(defined $m->subcontext) { |
---|
864 | $header .= ' / ' . $m->subcontext; |
---|
865 | } |
---|
866 | $header .= ' / @b{' . $m->pretty_sender . '}'; |
---|
867 | } |
---|
868 | |
---|
869 | if($m->opcode) { |
---|
870 | $header .= " [" . $m->opcode . "]"; |
---|
871 | } |
---|
872 | $header .= " " . $self->format_time($m); |
---|
873 | $header .= $self->format_sender($m); |
---|
874 | return $header; |
---|
875 | } |
---|
876 | |
---|
877 | sub format_sender { |
---|
878 | my $self = shift; |
---|
879 | my $m = shift; |
---|
880 | my $sender = $m->long_sender; |
---|
881 | $sender =~ s/\n.*$//s; |
---|
882 | return " (" . $sender . '@color[default]' . ")"; |
---|
883 | } |
---|
884 | |
---|
885 | sub indent_body |
---|
886 | { |
---|
887 | my $self = shift; |
---|
888 | my $m = shift; |
---|
889 | |
---|
890 | my $body = $m->body; |
---|
891 | if ($m->{should_wordwrap}) { |
---|
892 | $body = BarnOwl::wordwrap($body, BarnOwl::getnumcols()-8); |
---|
893 | } |
---|
894 | # replace newline followed by anything with |
---|
895 | # newline plus four spaces and that thing. |
---|
896 | $body =~ s/\n(.)/\n $1/g; |
---|
897 | # Trim trailing newlines. |
---|
898 | $body =~ s/\n*$//; |
---|
899 | return " ".$body; |
---|
900 | } |
---|
901 | |
---|
902 | package BarnOwl::Style::Basic; |
---|
903 | our @ISA=qw(BarnOwl::Style::Default); |
---|
904 | |
---|
905 | sub description {"Compatability alias for the default style";} |
---|
906 | |
---|
907 | BarnOwl::create_style("basic", "BarnOwl::Style::Basic"); |
---|
908 | |
---|
909 | package BarnOwl::Style::OneLine; |
---|
910 | # Inherit format_message to dispatch |
---|
911 | our @ISA = qw(BarnOwl::Style::Default); |
---|
912 | |
---|
913 | use constant BASE_FORMAT => '%s %-13.13s %-11.11s %-12.12s '; |
---|
914 | |
---|
915 | sub description {"Formats for one-line-per-message"} |
---|
916 | |
---|
917 | BarnOwl::create_style("oneline", "BarnOwl::Style::OneLine"); |
---|
918 | |
---|
919 | ################################################################################ |
---|
920 | |
---|
921 | sub format_login { |
---|
922 | my $self = shift; |
---|
923 | my $m = shift; |
---|
924 | return sprintf( |
---|
925 | BASE_FORMAT, |
---|
926 | '<', |
---|
927 | $m->type, |
---|
928 | uc( $m->login ), |
---|
929 | $m->pretty_sender) |
---|
930 | . ($m->login_extra ? "at ".$m->login_extra : ''); |
---|
931 | } |
---|
932 | |
---|
933 | sub format_ping { |
---|
934 | my $self = shift; |
---|
935 | my $m = shift; |
---|
936 | return sprintf( |
---|
937 | BASE_FORMAT, |
---|
938 | '<', |
---|
939 | $m->type, |
---|
940 | 'PING', |
---|
941 | $m->pretty_sender) |
---|
942 | } |
---|
943 | |
---|
944 | sub format_chat |
---|
945 | { |
---|
946 | my $self = shift; |
---|
947 | my $m = shift; |
---|
948 | my $dir = lc($m->{direction}); |
---|
949 | my $dirsym = '-'; |
---|
950 | if ($dir eq 'in') { |
---|
951 | $dirsym = '<'; |
---|
952 | } |
---|
953 | elsif ($dir eq 'out') { |
---|
954 | $dirsym = '>'; |
---|
955 | } |
---|
956 | |
---|
957 | my $line; |
---|
958 | if ($m->is_personal) { |
---|
959 | $line= sprintf(BASE_FORMAT, |
---|
960 | $dirsym, |
---|
961 | $m->type, |
---|
962 | '', |
---|
963 | ($dir eq 'out' |
---|
964 | ? $m->pretty_recipient |
---|
965 | : $m->pretty_sender)); |
---|
966 | } |
---|
967 | else { |
---|
968 | $line = sprintf(BASE_FORMAT, |
---|
969 | $dirsym, |
---|
970 | $m->context, |
---|
971 | $m->subcontext, |
---|
972 | ($dir eq 'out' |
---|
973 | ? $m->pretty_recipient |
---|
974 | : $m->pretty_sender)); |
---|
975 | } |
---|
976 | |
---|
977 | my $body = $m->{body}; |
---|
978 | $body =~ tr/\n/ /; |
---|
979 | $line .= $body; |
---|
980 | return $line; |
---|
981 | } |
---|
982 | |
---|
983 | # Format owl admin messages |
---|
984 | sub format_admin |
---|
985 | { |
---|
986 | my $self = shift; |
---|
987 | my $m = shift; |
---|
988 | my $line = sprintf(BASE_FORMAT, '<', 'ADMIN', '', ''); |
---|
989 | my $body = $m->{body}; |
---|
990 | $body =~ tr/\n/ /; |
---|
991 | return $line.$body; |
---|
992 | } |
---|
993 | |
---|
994 | package BarnOwl::Style; |
---|
995 | |
---|
996 | # This takes a zephyr to be displayed and modifies it to be displayed |
---|
997 | # entirely in bold. |
---|
998 | sub boldify |
---|
999 | { |
---|
1000 | local $_ = shift; |
---|
1001 | if ( !(/\)/) ) { |
---|
1002 | return '@b(' . $_ . ')'; |
---|
1003 | } elsif ( !(/\>/) ) { |
---|
1004 | return '@b<' . $_ . '>'; |
---|
1005 | } elsif ( !(/\}/) ) { |
---|
1006 | return '@b{' . $_ . '}'; |
---|
1007 | } elsif ( !(/\]/) ) { |
---|
1008 | return '@b[' . $_ . ']'; |
---|
1009 | } else { |
---|
1010 | my $txt = "\@b($_"; |
---|
1011 | $txt =~ s/\)/\)\@b\[\)\]\@b\(/g; |
---|
1012 | return $txt . ')'; |
---|
1013 | } |
---|
1014 | } |
---|
1015 | |
---|
1016 | sub style_command { |
---|
1017 | my $command = shift; |
---|
1018 | if(scalar @_ != 3 || $_[1] ne 'perl') { |
---|
1019 | die("Usage: style <name> perl <function>\n"); |
---|
1020 | } |
---|
1021 | my $name = shift; |
---|
1022 | my $perl = shift; |
---|
1023 | my $fn = shift; |
---|
1024 | { |
---|
1025 | no strict 'refs'; |
---|
1026 | unless(*{$fn}{CODE}) { |
---|
1027 | die("Unable to create style '$name': no perl function '$fn'\n"); |
---|
1028 | } |
---|
1029 | } |
---|
1030 | BarnOwl::create_style($name, BarnOwl::Style::Legacy->new($fn)); |
---|
1031 | } |
---|
1032 | |
---|
1033 | package BarnOwl::Style::Legacy; |
---|
1034 | |
---|
1035 | sub new { |
---|
1036 | my $class = shift; |
---|
1037 | my $func = shift; |
---|
1038 | my $desc = shift; |
---|
1039 | my $useglobals = shift; |
---|
1040 | $useglobals = 0 unless defined($useglobals); |
---|
1041 | return bless {function => $func, |
---|
1042 | description => $desc, |
---|
1043 | useglobals => $useglobals}, $class; |
---|
1044 | } |
---|
1045 | |
---|
1046 | sub description { |
---|
1047 | my $self = shift; |
---|
1048 | return $self->{description} || |
---|
1049 | ("User-defined perl style that calls " . $self->{function}); |
---|
1050 | }; |
---|
1051 | |
---|
1052 | sub format_message { |
---|
1053 | my $self = shift; |
---|
1054 | if($self->{useglobals}) { |
---|
1055 | $_[0]->legacy_populate_global(); |
---|
1056 | } |
---|
1057 | no strict 'refs'; |
---|
1058 | goto \&{$self->{function}}; |
---|
1059 | } |
---|
1060 | |
---|
1061 | |
---|
1062 | # switch to package main when we're done |
---|
1063 | package main; |
---|
1064 | |
---|
1065 | # Shove a bunch of fake entries into @INC so modules can use or |
---|
1066 | # require them without choking |
---|
1067 | $::INC{$_} = 1 for (qw(BarnOwl.pm BarnOwl/Hooks.pm |
---|
1068 | BarnOwl/Message.pm BarnOwl/Style.pm)); |
---|
1069 | |
---|
1070 | 1; |
---|
1071 | |
---|