source: perlwrap.pm @ c2bed55

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