source: perlwrap.pm @ ca53b77

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