source: perlwrap.pm @ ae47efb

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since ae47efb was ae47efb, checked in by Alex Vandiver <alexmv@mit.edu>, 17 years ago
Pretty recipients are useful, too
  • Property mode set to 100644
File size: 12.6 KB
Line 
1# $Id$
2#
3# This is all linked into the binary and evaluated when perl starts up...
4#
5#####################################################################
6#####################################################################
7
8use strict;
9use warnings;
10
11package BarnOwl;
12
13
14BEGIN {
15# bootstrap in C bindings and glue
16    *owl:: = \*BarnOwl::;
17    bootstrap BarnOwl 1.2;
18};
19
20use lib(get_data_dir()."/lib");
21use lib($::ENV{'HOME'}."/.owl/lib");
22
23our $configfile;
24
25$configfile ||= $::ENV{'HOME'}."/.owlconf";
26
27# populate global variable space for legacy owlconf files
28sub _format_msg_legacy_wrap {
29    my ($m) = @_;
30    $m->legacy_populate_global();
31    return &BarnOwl::format_msg($m);
32}
33
34# populate global variable space for legacy owlconf files
35sub _receive_msg_legacy_wrap {
36    my ($m) = @_;
37    $m->legacy_populate_global();
38    return &BarnOwl::Hooks::receive_msg($m);
39}
40
41# make BarnOwl::<command>("foo") be aliases to BarnOwl::command("<command> foo");
42sub AUTOLOAD {
43    our $AUTOLOAD;
44    my $called = $AUTOLOAD;
45    $called =~ s/.*:://;
46    $called =~ s/_/-/g;
47    return &BarnOwl::command("$called ".join(" ",@_));
48}
49
50=head2 new_command NAME FUNC [{ARGS}]
51
52Add a new owl command. When owl executes the command NAME, FUNC will
53be called with the arguments passed to the command, with NAME as the
54first argument.
55
56ARGS should be a hashref containing any or all of C<summary>,
57C<usage>, or C<description> keys.
58
59=cut
60
61sub new_command {
62    my $name = shift;
63    my $func = shift;
64    my $args = shift || {};
65    my %args = (
66        summary     => undef,
67        usage       => undef,
68        description => undef,
69        %{$args}
70    );
71
72    no warnings 'uninitialized';
73    BarnOwl::new_command_internal($name, $func, $args{summary}, $args{usage}, $args{description});
74}
75
76#####################################################################
77#####################################################################
78
79package BarnOwl::Message;
80
81sub new {
82    my $class = shift;
83    my %args = (@_);
84    if($class eq __PACKAGE__ && $args{type}) {
85        $class = "BarnOwl::Message::" . ucfirst $args{type};
86    }
87    return bless {%args}, $class;
88}
89
90sub type        { return shift->{"type"}; }
91sub direction   { return shift->{"direction"}; }
92sub time        { return shift->{"time"}; }
93sub id          { return shift->{"id"}; }
94sub body        { return shift->{"body"}; }
95sub sender      { return shift->{"sender"}; }
96sub recipient   { return shift->{"recipient"}; }
97sub login       { return shift->{"login"}; }
98sub is_private  { return shift->{"private"}; }
99
100sub is_login    { return shift->login eq "login"; }
101sub is_logout   { return shift->login eq "logout"; }
102sub is_loginout { my $m=shift; return ($m->is_login or $m->is_logout); }
103sub is_incoming { return (shift->{"direction"} eq "in"); }
104sub is_outgoing { return (shift->{"direction"} eq "out"); }
105
106sub is_deleted  { return shift->{"deleted"}; }
107
108sub is_admin    { return (shift->{"type"} eq "admin"); }
109sub is_generic  { return (shift->{"type"} eq "generic"); }
110sub is_zephyr   { return (shift->{"type"} eq "zephyr"); }
111sub is_aim      { return (shift->{"type"} eq "aim"); }
112sub is_jabber   { return (shift->{"type"} eq "jabber"); }
113sub is_icq      { return (shift->{"type"} eq "icq"); }
114sub is_yahoo    { return (shift->{"type"} eq "yahoo"); }
115sub is_msn      { return (shift->{"type"} eq "msn"); }
116sub is_loopback { return (shift->{"type"} eq "loopback"); }
117
118# These are overridden by appropriate message types
119sub is_ping     { return 0; }
120sub is_mail     { return 0; }
121sub is_personal { return shift->is_private; }
122sub class       { return undef; }
123sub instance    { return undef; }
124sub realm       { return undef; }
125sub opcode      { return undef; }
126sub header      { return undef; }
127sub host        { return undef; }
128sub hostname    { return undef; }
129sub auth        { return undef; }
130sub fields      { return undef; }
131sub zsig        { return undef; }
132sub zwriteline  { return undef; }
133sub login_host  { return undef; }
134sub login_tty   { return undef; }
135
136sub pretty_sender    { return shift->sender; }
137sub pretty_recipient { return shift->recipient; }
138
139sub delete {
140    my ($m) = @_;
141    &BarnOwl::command("delete --id ".$m->id);
142}
143
144sub undelete {
145    my ($m) = @_;
146    &BarnOwl::command("undelete --id ".$m->id);
147}
148
149# Serializes the message into something similar to the zwgc->vt format
150sub serialize {
151    my ($this) = @_;
152    my $s;
153    for my $f (keys %$this) {
154        my $val = $this->{$f};
155        if (ref($val) eq "ARRAY") {
156            for my $i (0..@$val-1) {
157                my $aval;
158                $aval = $val->[$i];
159                $aval =~ s/\n/\n$f.$i: /g;
160                $s .= "$f.$i: $aval\n";   
161            }
162        } else {
163            $val =~ s/\n/\n$f: /g;
164            $s .= "$f: $val\n";
165        }
166    }
167    return $s;
168}
169
170# Populate the annoying legacy global variables
171sub legacy_populate_global {
172    my ($m) = @_;
173    $BarnOwl::direction  = $m->direction ;
174    $BarnOwl::type       = $m->type      ;
175    $BarnOwl::id         = $m->id        ;
176    $BarnOwl::class      = $m->class     ;
177    $BarnOwl::instance   = $m->instance  ;
178    $BarnOwl::recipient  = $m->recipient ;
179    $BarnOwl::sender     = $m->sender    ;
180    $BarnOwl::realm      = $m->realm     ;
181    $BarnOwl::opcode     = $m->opcode    ;
182    $BarnOwl::zsig       = $m->zsig      ;
183    $BarnOwl::msg        = $m->body      ;
184    $BarnOwl::time       = $m->time      ;
185    $BarnOwl::host       = $m->host      ;
186    $BarnOwl::login      = $m->login     ;
187    $BarnOwl::auth       = $m->auth      ;
188    if ($m->fields) {
189        @BarnOwl::fields = @{$m->fields};
190        @main::fields = @{$m->fields};
191    } else {
192        @BarnOwl::fields = undef;
193        @main::fields = undef;
194    }
195}
196
197sub smartfilter {
198    die("smartfilter not supported for this message");
199}
200
201#####################################################################
202#####################################################################
203
204package BarnOwl::Message::Admin;
205
206use base qw( BarnOwl::Message );
207
208sub header       { return shift->{"header"}; }
209
210#####################################################################
211#####################################################################
212
213package BarnOwl::Message::Generic;
214
215use base qw( BarnOwl::Message );
216
217#####################################################################
218#####################################################################
219
220package BarnOwl::Message::AIM;
221
222use base qw( BarnOwl::Message );
223
224# all non-loginout AIM messages are personal for now...
225sub is_personal { 
226    return !(shift->is_loginout);
227}
228
229#####################################################################
230#####################################################################
231
232package BarnOwl::Message::Zephyr;
233
234use base qw( BarnOwl::Message );
235
236sub login_tty { 
237    my ($m) = @_;
238    return undef if (!$m->is_loginout);
239    return $m->fields->[2];
240}
241
242sub login_host { 
243    my ($m) = @_;
244    return undef if (!$m->is_loginout);
245    return $m->fields->[0];
246}
247
248sub zwriteline  { return shift->{"zwriteline"}; }
249
250sub is_ping     { return (lc(shift->opcode) eq "ping"); }
251
252sub is_personal { 
253    my ($m) = @_;
254    return ((lc($m->class) eq "message")
255            && (lc($m->instance) eq "personal")
256            && $m->is_private);
257}
258
259sub is_mail { 
260    my ($m) = @_;
261    return ((lc($m->class) eq "mail") && $m->is_private);
262}
263
264sub pretty_sender {
265    my ($m) = @_;
266    my $sender = $m->sender;
267    my $realm = BarnOwl::zephyr_getrealm();
268    $sender =~ s/\@$realm$//;
269    return $sender;
270}
271
272sub pretty_recipient {
273    my ($m) = @_;
274    my $recip = $m->recipient;
275    my $realm = BarnOwl::zephyr_getrealm();
276    $recip =~ s/\@$realm$//;
277    return $recip;
278}
279
280# These are arguably zephyr-specific
281sub class       { return shift->{"class"}; }
282sub instance    { return shift->{"instance"}; }
283sub realm       { return shift->{"realm"}; }
284sub opcode      { return shift->{"opcode"}; }
285sub host        { return shift->{"hostname"}; }
286sub hostname    { return shift->{"hostname"}; }
287sub header      { return shift->{"header"}; }
288sub auth        { return shift->{"auth"}; }
289sub fields      { return shift->{"fields"}; }
290sub zsig        { return shift->{"zsig"}; }
291
292#####################################################################
293#####################################################################
294################################################################################
295package owl;
296
297################################################################################
298# Mainloop hook
299################################################################################
300
301our $shutdown;
302$shutdown = 0;
303our $reload;
304$reload = 0;
305
306#Run this on start and reload. Adds modules
307sub onStart
308{
309    reload_init();
310    loadModules();
311}
312################################################################################
313# Reload Code, taken from /afs/sipb/user/jdaniel/project/owl/perl
314################################################################################
315sub reload_hook (@)
316{
317    BarnOwl::Hooks::startup();
318    return 1;
319}
320
321sub reload
322{
323    # Use $reload to tell modules that we're performing a reload.
324  {
325      local $reload = 1;
326      BarnOwl::mainloop_hook();
327  }
328   
329  @BarnOwl::Hooks::onMainLoop = ();
330  @BarnOwl::Hooks::onStartSubs = ();
331
332  # Do reload
333  package main;
334  if (-r $BarnOwl::configfile) {
335      undef $@;
336      do $BarnOwl::configfile;
337      owl::error("Error reloading $BarnOwl::configfile: $@") if $@;
338  }
339  BarnOwl::reload_hook(@_);
340  package owl;
341}
342
343sub reload_init () 
344{
345    BarnOwl::command('alias reload perl BarnOwl::reload()');
346    BarnOwl::command('bindkey global "C-x C-r" command reload');
347}
348
349################################################################################
350# Loads modules from ~/.owl/modules and owl's data directory
351################################################################################
352
353sub loadModules () {
354    my @modules;
355    my $rv;
356    foreach my $dir ( BarnOwl::get_data_dir() . "/modules",
357                      $ENV{HOME} . "/.owl/modules" )
358    {
359        opendir( MODULES, $dir );
360
361        # source ./modules/*.pl
362        @modules = sort grep( /\.pl$/, readdir(MODULES) );
363
364        foreach my $mod (@modules) {
365            unless ($rv = do "$dir/$mod") {
366                BarnOwl::error("Couldn't load $dir/$mod:\n $@") if $@;
367                BarnOwl::error("Couldn't run $dir/$mod:\n $!") unless defined $rv;
368            }
369        }
370        closedir(MODULES);
371    }
372}
373
374package BarnOwl::Hooks;
375
376# Arrays of subrefs to be called at specific times.
377our @onStartSubs = ();
378our @onReceiveMsg = ();
379our @onMainLoop = ();
380our @onGetBuddyList = ();
381
382# Functions to call hook lists
383sub runHook($@)
384{
385    my $hook = shift;
386    my @args = @_;
387    $_->(@args) for (@$hook);
388}
389
390sub runHook_accumulate($@)
391{
392    my $hook = shift;
393    my @args = @_;
394    return join("\n", map {$_->(@args)} @$hook);
395}
396
397################################################################################
398# Startup and Shutdown code
399################################################################################
400sub startup
401{
402    # Modern versions of owl provides a great place to have startup stuff.
403    # Put things in ~/.owl/startup
404
405    #So that the user's .owlconf can have startsubs, we don't clear
406    #onStartSubs; reload does however
407    @onReceiveMsg = ();
408    @onMainLoop = ();
409    @onGetBuddyList = ();
410
411    BarnOwl::onStart();
412
413    runHook(\@onStartSubs);
414
415    BarnOwl::startup() if *BarnOwl::startup{CODE};
416}
417
418sub shutdown
419{
420# Modern versions of owl provides a great place to have shutdown stuff.
421# Put things in ~/.owl/shutdown
422
423    # use $shutdown to tell modules that that's what we're doing.
424    $BarnOwl::shutdown = 1;
425    BarnOwl::mainloop_hook();
426
427    BarnOwl::shutdown() if *BarnOwl::shutdown{CODE};
428}
429
430sub mainloop_hook
431{
432    runHook(\@onMainLoop);
433    BarnOwl::mainlook_hook() if *BarnOwl::mainloop_hook{CODE};
434}
435
436################################################################################
437# Hooks into receive_msg()
438################################################################################
439
440sub receive_msg
441{
442    my $m = shift;
443    runHook(\@onReceiveMsg, $m);
444    BarnOwl::receive_msg($m) if *BarnOwl::receive_msg{CODE};
445}
446
447################################################################################
448# Hooks into get_blist()
449################################################################################
450
451sub get_blist
452{
453    return runHook_accumulate(\@onGetBuddyList);
454}
455
456# switch to package main when we're done
457package main;
458# alias the hooks
459{
460    no strict 'refs';
461    foreach my $hook  qw (onStartSubs
462                          onReceiveMsg
463                          onMainLoop
464                          onGetBuddyList ) {
465        *{"main::".$hook} = \*{"BarnOwl::Hooks::".$hook};
466        *{"owl::".$hook} = \*{"BarnOwl::Hooks::".$hook};
467    }
468}
469
470# load the config  file
471if (-r $BarnOwl::configfile) {
472    undef $@;
473    do $BarnOwl::configfile;
474    die $@ if $@;
475}
476
4771;
Note: See TracBrowser for help on using the repository browser.