Changeset 8203afd for perlwrap.pm


Ignore:
Timestamp:
Jan 9, 2007, 11:33:44 PM (18 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, barnowl_perlaim, debian, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
d609dd6
Parents:
ffe70f9
git-author:
Nelson Elhage <nelhage@mit.edu> (01/09/07 18:50:40)
git-committer:
Nelson Elhage <nelhage@mit.edu> (01/09/07 23:33:44)
Message:
* Renaming package owl to BarnOwl::
* Moving hooks into the BarnOwl::Hooks package, and restoring support
  for .owlconf files that use sub startup {} and so on.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perlwrap.pm

    r3354cea5 r8203afd  
    99use warnings;
    1010
    11 package owl;
     11package BarnOwl;
    1212
    1313
    1414BEGIN {
    1515# bootstrap in C bindings and glue
    16     bootstrap owl 1.2;
     16    *owl:: = \*BarnOwl::;
     17    bootstrap BarnOwl 1.2;
    1718};
    1819
     
    2829    my ($m) = @_;
    2930    $m->legacy_populate_global();
    30     return &owl::format_msg($m);
     31    return &BarnOwl::format_msg($m);
    3132}
    3233
     
    3536    my ($m) = @_;
    3637    $m->legacy_populate_global();
    37     return &owl::receive_msg($m);
    38 }
    39 
    40 # make owl::<command>("foo") be aliases to owl::command("<command> foo");
     38    return &BarnOwl::Hooks::receive_msg($m);
     39}
     40
     41# make BarnOwl::<command>("foo") be aliases to BarnOwl::command("<command> foo");
    4142sub AUTOLOAD {
    4243    our $AUTOLOAD;
     
    4445    $called =~ s/.*:://;
    4546    $called =~ s/_/-/g;
    46     return &owl::command("$called ".join(" ",@_));
     47    return &BarnOwl::command("$called ".join(" ",@_));
    4748}
    4849
     
    7071
    7172    no warnings 'uninitialized';
    72     owl::new_command_internal($name, $func, $args{summary}, $args{usage}, $args{description});
    73 }
    74 
    75 #####################################################################
    76 #####################################################################
    77 
    78 package owl::Message;
     73    BarnOwl::new_command_internal($name, $func, $args{summary}, $args{usage}, $args{description});
     74}
     75
     76#####################################################################
     77#####################################################################
     78
     79package BarnOwl::Message;
    7980
    8081sub new {
     
    8283    my %args = (@_);
    8384    if($class eq __PACKAGE__ && $args{type}) {
    84         $class = "owl::Message::" . ucfirst $args{type};
     85        $class = "BarnOwl::Message::" . ucfirst $args{type};
    8586    }
    8687    return bless {%args}, $class;
     
    137138sub delete {
    138139    my ($m) = @_;
    139     &owl::command("delete --id ".$m->id);
     140    &BarnOwl::command("delete --id ".$m->id);
    140141}
    141142
    142143sub undelete {
    143144    my ($m) = @_;
    144     &owl::command("undelete --id ".$m->id);
     145    &BarnOwl::command("undelete --id ".$m->id);
    145146}
    146147
     
    169170sub legacy_populate_global {
    170171    my ($m) = @_;
    171     $owl::direction  = $m->direction ;
    172     $owl::type       = $m->type      ;
    173     $owl::id         = $m->id        ;
    174     $owl::class      = $m->class     ;
    175     $owl::instance   = $m->instance  ;
    176     $owl::recipient  = $m->recipient ;
    177     $owl::sender     = $m->sender    ;
    178     $owl::realm      = $m->realm     ;
    179     $owl::opcode     = $m->opcode    ;
    180     $owl::zsig       = $m->zsig      ;
    181     $owl::msg        = $m->body      ;
    182     $owl::time       = $m->time      ;
    183     $owl::host       = $m->host      ;
    184     $owl::login      = $m->login     ;
    185     $owl::auth       = $m->auth      ;
     172    $BarnOwl::direction  = $m->direction ;
     173    $BarnOwl::type       = $m->type      ;
     174    $BarnOwl::id         = $m->id        ;
     175    $BarnOwl::class      = $m->class     ;
     176    $BarnOwl::instance   = $m->instance  ;
     177    $BarnOwl::recipient  = $m->recipient ;
     178    $BarnOwl::sender     = $m->sender    ;
     179    $BarnOwl::realm      = $m->realm     ;
     180    $BarnOwl::opcode     = $m->opcode    ;
     181    $BarnOwl::zsig       = $m->zsig      ;
     182    $BarnOwl::msg        = $m->body      ;
     183    $BarnOwl::time       = $m->time      ;
     184    $BarnOwl::host       = $m->host      ;
     185    $BarnOwl::login      = $m->login     ;
     186    $BarnOwl::auth       = $m->auth      ;
    186187    if ($m->fields) {
    187         @owl::fields = @{$m->fields};
     188        @BarnOwl::fields = @{$m->fields};
    188189        @main::fields = @{$m->fields};
    189190    } else {
    190         @owl::fields = undef;
     191        @BarnOwl::fields = undef;
    191192        @main::fields = undef;
    192193    }
     
    200201#####################################################################
    201202
    202 package owl::Message::Admin;
    203 
    204 use base qw( owl::Message );
     203package BarnOwl::Message::Admin;
     204
     205use base qw( BarnOwl::Message );
    205206
    206207sub header       { return shift->{"header"}; }
     
    209210#####################################################################
    210211
    211 package owl::Message::Generic;
    212 
    213 use base qw( owl::Message );
    214 
    215 #####################################################################
    216 #####################################################################
    217 
    218 package owl::Message::AIM;
    219 
    220 use base qw( owl::Message );
     212package BarnOwl::Message::Generic;
     213
     214use base qw( BarnOwl::Message );
     215
     216#####################################################################
     217#####################################################################
     218
     219package BarnOwl::Message::AIM;
     220
     221use base qw( BarnOwl::Message );
    221222
    222223# all non-loginout AIM messages are personal for now...
     
    228229#####################################################################
    229230
    230 package owl::Message::Zephyr;
    231 
    232 use base qw( owl::Message );
     231package BarnOwl::Message::Zephyr;
     232
     233use base qw( BarnOwl::Message );
    233234
    234235sub login_tty {
     
    263264    my ($m) = @_;
    264265    my $sender = $m->sender;
    265     my $realm = owl::zephyr_getrealm();
     266    my $realm = BarnOwl::zephyr_getrealm();
    266267    $sender =~ s/\@$realm$//;
    267268    return $sender;
     
    285286package owl;
    286287
     288################################################################################
     289# Mainloop hook
     290################################################################################
     291
     292our $shutdown;
     293$shutdown = 0;
     294our $reload;
     295$reload = 0;
     296
     297#Run this on start and reload. Adds modules
     298sub onStart
     299{
     300    reload_init();
     301    loadModules();
     302}
     303################################################################################
     304# Reload Code, taken from /afs/sipb/user/jdaniel/project/owl/perl
     305################################################################################
     306sub reload_hook (@)
     307{
     308    BarnOwl::Hooks::startup();
     309    return 1;
     310}
     311
     312sub reload
     313{
     314    # Use $reload to tell modules that we're performing a reload.
     315  {
     316      local $reload = 1;
     317      BarnOwl::mainloop_hook();
     318  }
     319   
     320  @BarnOwl::Hooks::onMainLoop = ();
     321  @BarnOwl::Hooks::onStartSubs = ();
     322
     323  # Do reload
     324  package main;
     325  if (do "$ENV{HOME}/.owlconf" && BarnOwl::reload_hook(@_))
     326  {
     327      return "owlconf reloaded";
     328  }
     329  else
     330  {
     331      return "$ENV{HOME}/.owlconf load attempted, but error encountered:\n$@";
     332  }
     333  package owl;
     334}
     335
     336sub reload_init ()
     337{
     338    BarnOwl::command('alias reload perl BarnOwl::reload()');
     339    BarnOwl::command('bindkey global "C-x C-r" command reload');
     340}
     341
     342################################################################################
     343# Loads modules from ~/.owl/modules and owl's data directory
     344################################################################################
     345
     346sub loadModules () {
     347    my @modules;
     348    my $rv;
     349    foreach my $dir ( BarnOwl::get_data_dir() . "/modules",
     350                      $ENV{HOME} . "/.owl/modules" )
     351    {
     352        opendir( MODULES, $dir );
     353
     354        # source ./modules/*.pl
     355        @modules = grep( /\.pl$/, readdir(MODULES) );
     356
     357        foreach my $mod (@modules) {
     358            owl::error("Loading $dir/$mod...");
     359            unless ($rv = do "$dir/$mod") {
     360                BarnOwl::error("Couldn't load $dir/$mod:\n $@") if $@;
     361                BarnOwl::error("Couldn't run $dir/$mod:\n $!") unless defined $rv;
     362            }
     363        }
     364        closedir(MODULES);
     365    }
     366}
     367
     368package BarnOwl::Hooks;
     369
    287370# Arrays of subrefs to be called at specific times.
    288371our @onStartSubs = ();
     
    291374our @onGetBuddyList = ();
    292375
    293 ################################################################################
    294 # Mainloop hook
    295 ################################################################################
    296 
    297 our $shutdown;
    298 $shutdown = 0;
    299 our $reload;
    300 $reload = 0;
    301 
    302376# Functions to call hook lists
    303377sub runHook($@)
     
    315389}
    316390
    317 sub mainloop_hook
    318 {
    319     runHook(\@onMainLoop);
    320 }
    321 
    322391################################################################################
    323392# Startup and Shutdown code
     
    325394sub startup
    326395{
    327 # Modern versions of owl provides a great place to have startup stuff.
    328 # Put things in ~/.owl/startup
    329     onStart();
    330 }
    331 
    332 sub shutdown
    333 {
    334 # Modern versions of owl provides a great place to have shutdown stuff.
    335 # Put things in ~/.owl/shutdown
    336 
    337     # use $shutdown to tell modules that that's what we're doing.
    338     $shutdown = 1;
    339     mainloop_hook();
    340 }
    341 
    342 #Run this on start and reload. Adds modules and runs their startup functions.
    343 sub onStart
    344 {
    345     reload_init();
     396    # Modern versions of owl provides a great place to have startup stuff.
     397    # Put things in ~/.owl/startup
     398
    346399    #So that the user's .owlconf can have startsubs, we don't clear
    347400    #onStartSubs; reload does however
     
    350403    @onGetBuddyList = ();
    351404
    352     loadModules();
     405    BarnOwl::onStart();
     406
    353407    runHook(\@onStartSubs);
    354 }
    355 ################################################################################
    356 # Reload Code, taken from /afs/sipb/user/jdaniel/project/owl/perl
    357 ################################################################################
    358 sub reload_hook (@)
    359 {
    360     onStart();
    361     return 1;
    362 }
    363 
    364 sub reload
    365 {
    366     # Use $reload to tell modules that we're performing a reload.
    367     $reload = 1;
    368     owl::mainloop_hook();
    369     $reload = 0;
    370     @onMainLoop = ();
    371     @onStartSubs = ();
    372 
    373     # Do reload
    374     package main;
    375     if (do "$ENV{HOME}/.owlconf" && owl::reload_hook(@_))
    376     {
    377         return "owlconf reloaded";
    378     }
    379     else
    380     {
    381         return "$ENV{HOME}/.owlconf load attempted, but error encountered:\n$@";
    382     }
    383     package owl;
    384 }
    385 
    386 sub reload_init ()
    387 {
    388     owl::command('alias reload perl owl::reload()');
    389     owl::command('bindkey global "C-x C-r" command reload');
    390 }
    391 
    392 ################################################################################
    393 # Loads modules from ~/.owl/modules and owl's data directory
    394 ################################################################################
    395 
    396 sub loadModules () {
    397     my @modules;
    398     my $rv;
    399     foreach my $dir ( owl::get_data_dir() . "/modules",
    400                       $ENV{HOME} . "/.owl/modules" )
    401     {
    402         opendir( MODULES, $dir );
    403 
    404         # source ./modules/*.pl
    405         @modules = grep( /\.pl$/, readdir(MODULES) );
    406 
    407         foreach my $mod (@modules) {
    408             unless ($rv = do "$dir/$mod") {
    409                 owl::error("Couldn't load $dir/$mod:\n $@") if $@;
    410                 owl::error("Couldn't run $dir/$mod:\n $!") unless defined $rv;
    411             }
    412         }
    413         closedir(MODULES);
    414     }
    415 }
    416 
    417 
     408
     409    BarnOwl::startup() if *BarnOwl::startup{CODE};
     410}
     411
     412sub shutdown
     413{
     414# Modern versions of owl provides a great place to have shutdown stuff.
     415# Put things in ~/.owl/shutdown
     416
     417    # use $shutdown to tell modules that that's what we're doing.
     418    $BarnOwl::shutdown = 1;
     419    BarnOwl::mainloop_hook();
     420
     421    BarnOwl::shutdown() if *BarnOwl::shutdown{CODE};
     422}
     423
     424sub mainloop_hook
     425{
     426    runHook(\@onMainLoop);
     427    BarnOwl::mainlook_hook() if *BarnOwl::mainloop_hook{CODE};
     428}
    418429
    419430################################################################################
     
    425436    my $m = shift;
    426437    runHook(\@onReceiveMsg, $m);
     438    BarnOwl::receive_msg($m) if *BarnOwl::receive_msg{CODE};
    427439}
    428440
     
    433445sub get_blist
    434446{
    435     my $m = shift;
    436     return runHook_accumulate(\@onGetBuddyList, $m);
     447    return runHook_accumulate(\@onGetBuddyList);
    437448}
    438449
     
    446457                          onMainLoop
    447458                          onGetBuddyList ) {
    448         *{"main::".$hook} = \*{"owl::".$hook};
     459        *{"main::".$hook} = \*{"BarnOwl::Hooks::".$hook};
     460        *{"owl::".$hook} = \*{"BarnOwl::Hooks::".$hook};
    449461    }
    450462}
    451463
    452464# load the config  file
    453 if (-r $owl::configfile) {
     465if (-r $BarnOwl::configfile) {
    454466    undef $@;
    455     do $owl::configfile;
     467    do $BarnOwl::configfile;
    456468    die $@ if $@;
    457469}
Note: See TracChangeset for help on using the changeset viewer.