Changeset 0337203 for perlwrap.pm


Ignore:
Timestamp:
Mar 16, 2007, 12:45:19 AM (14 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, barnowl_perlaim, debian, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
4dd6e58
Parents:
69d66aa7
Message:
First phase of the module rewrite. Internals now (IMO) somewhat
cleaner.

 r19586@phanatique:  nelhage | 2007-03-14 20:35:39 -0400
 First pass at a cleaned up perlwrap.pm
 
 * Using a new hook style
 * Modules loaded by BarnOwl::ModuleLoader (not yet written)
 
 reload is unimplemented for now. If possible, I'd like it to live
 elsewhere.
 
 r19587@phanatique:  nelhage | 2007-03-14 20:36:58 -0400
 Switching to the new underscore internal hook names.
 r19592@phanatique:  nelhage | 2007-03-16 00:34:00 -0400
 Actually switch to _receive_msg
 
 r19593@phanatique:  nelhage | 2007-03-16 00:34:27 -0400
 Some minor cleanup of perlwrap.pm. Shoving fake entries into @INC.
 
 r19594@phanatique:  nelhage | 2007-03-16 00:34:47 -0400
 First revision of ModuleLoader.
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perlwrap.pm

    rbb2c60d r0337203  
    77# XXX NOTE: This file is sourced before almost any barnowl
    88# architecture is loaded. This means, for example, that it cannot
    9 # execute any owl commands. Any code that needs to do so, should
    10 # create a function wrapping it and push it onto @onStartSubs
    11 
     9# execute any owl commands. Any code that needs to do so should live
     10# in BarnOwl::Hooks::_startup
    1211
    1312use strict;
    1413use warnings;
     14
     15package BarnOwl;
    1516
    1617package BarnOwl;
     
    4445    my ($m) = @_;
    4546    $m->legacy_populate_global();
    46     return &BarnOwl::Hooks::receive_msg($m);
     47    return &BarnOwl::Hooks::_receive_msg($m);
    4748}
    4849
     
    204205
    205206sub smartfilter {
    206     die("smartfilter not supported for this message");
     207    die("smartfilter not supported for this message\n");
    207208}
    208209
     
    351352#####################################################################
    352353################################################################################
    353 package BarnOwl;
    354 
    355 ################################################################################
    356 # Mainloop hook
    357 ################################################################################
    358 
    359 our $shutdown;
    360 $shutdown = 0;
    361 our $reload;
    362 $reload = 0;
    363 
    364 #Run this on start and reload. Adds modules
    365 sub onStart
    366 {
    367     _load_owlconf();
    368     reload_init();
    369     loadModules();
    370 }
    371 ################################################################################
    372 # Reload Code, taken from /afs/sipb/user/jdaniel/project/owl/perl
    373 ################################################################################
    374 sub reload_hook (@)
    375 {
    376     BarnOwl::Hooks::startup();
    377     return 1;
    378 }
    379 
    380 sub reload
    381 {
    382     # Use $reload to tell modules that we're performing a reload.
    383   {
    384       local $reload = 1;
    385       BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE};
    386   }
    387 
    388   @BarnOwl::Hooks::onMainLoop = ();
    389   @BarnOwl::Hooks::onStartSubs = ();
    390 
    391   # Do reload
    392   package main;
    393   if (-r $BarnOwl::configfile) {
    394       undef $@;
    395       do $BarnOwl::configfile;
    396       BarnOwl::error("Error reloading $BarnOwl::configfile: $@") if $@;
    397   }
    398   BarnOwl::reload_hook(@_);
    399   package BarnOwl;
    400 }
    401 
    402 sub reload_init ()
    403 {
    404     BarnOwl::command('alias reload perl BarnOwl::reload()');
    405     BarnOwl::command('bindkey global "C-x C-r" command reload');
    406 }
    407 
    408 ################################################################################
    409 # Loads modules from ~/.owl/modules and owl's data directory
    410 ################################################################################
    411 
    412 sub loadModules () {
    413     my @modules;
    414     my $rv;
    415     foreach my $dir ( BarnOwl::get_data_dir() . "/modules",
    416                       $ENV{HOME} . "/.owl/modules" )
    417     {
    418         opendir( MODULES, $dir );
    419 
    420         # source ./modules/*.pl
    421         @modules = sort grep( /\.pl$/, readdir(MODULES) );
    422 
    423         foreach my $mod (@modules) {
    424             unless ($rv = do "$dir/$mod") {
    425                 BarnOwl::error("Couldn't load $dir/$mod:\n $@") if $@;
    426                 BarnOwl::error("Couldn't run $dir/$mod:\n $!") unless defined $rv;
    427             }
    428         }
    429         closedir(MODULES);
    430     }
    431 }
     354
     355package BarnOwl::Hook;
     356
     357sub new {
     358    my $class = shift;
     359    return bless [], $class;
     360}
     361
     362sub run {
     363    my $self = shift;
     364    my @args = @_;
     365    return map {$_->(@args)} @$self;
     366}
     367
     368sub add {
     369    my $self = shift;
     370    my $func = shift;
     371    die("Not a coderef!") unless ref($func) eq 'CODE';
     372    push @$self, $func;
     373}
     374
     375sub clear {
     376    my $self = shift;
     377    @$self = ();
     378}
     379
     380package BarnOwl::Hooks;
     381
     382use Exporter;
     383
     384our @EXPORT_OK = qw($startup $shutdown
     385                    $receiveMessage $mainLoop
     386                    $getBuddyList);
     387
     388our %EXPORT_TAGS = (all => [@EXPORT_OK]);
     389
     390our $startup = BarnOwl::Hook->new;
     391our $shutdown = BarnOwl::Hook->new;
     392our $receiveMessage = BarnOwl::Hook->new;
     393our $mainLoop = BarnOwl::Hook->new;
     394our $getBuddyList = BarnOwl::Hook->new;
     395
     396# Internal startup/shutdown routines called by the C code
    432397
    433398sub _load_owlconf {
    434     # Only do this the first time
    435     return if $BarnOwl::reload;
    436399    # load the config  file
    437400    if ( -r $BarnOwl::configfile ) {
     
    451414}
    452415
    453 package BarnOwl::Hooks;
    454 
    455 # Arrays of subrefs to be called at specific times.
    456 our @onStartSubs = ();
    457 our @onReceiveMsg = ();
    458 our @onMainLoop = ();
    459 our @onGetBuddyList = ();
    460 
    461 # Functions to call hook lists
    462 sub runHook($@)
    463 {
    464     my $hook = shift;
    465     my @args = @_;
    466     $_->(@args) for (@$hook);
    467 }
    468 
    469 sub runHook_accumulate($@)
    470 {
    471     my $hook = shift;
    472     my @args = @_;
    473     return join("\n", map {$_->(@args)} @$hook);
    474 }
    475 
    476 ################################################################################
    477 # Startup and Shutdown code
    478 ################################################################################
    479 sub startup
    480 {
    481     # Modern versions of owl provides a great place to have startup stuff.
    482     # Put things in ~/.owl/startup
    483 
    484     #So that the user's .owlconf can have startsubs, we don't clear
    485     #onStartSubs; reload does however
    486     @onReceiveMsg = ();
    487     @onMainLoop = ();
    488     @onGetBuddyList = ();
    489 
    490     BarnOwl::onStart();
    491 
    492     runHook(\@onStartSubs);
    493 
     416sub _startup {
     417    _load_owlconf();
     418
     419    if(eval {require BarnOwl::ModuleLoader}) {
     420        eval {
     421            BarnOwl::ModuleLoader->load_all;
     422        };
     423    } else {
     424        BarnOwl::error("Can't load BarnOwl::ModuleLoader, loadable module support disabled:\n$@");
     425    }
     426   
     427    $startup->run;
    494428    BarnOwl::startup() if *BarnOwl::startup{CODE};
    495429}
    496430
    497 sub shutdown
    498 {
    499 # Modern versions of owl provides a great place to have shutdown stuff.
    500 # Put things in ~/.owl/shutdown
    501 
    502     # use $shutdown to tell modules that that's what we're doing.
    503     $BarnOwl::shutdown = 1;
     431sub _shutdown {
     432    $shutdown->run;
     433   
     434    BarnOwl::shutdown() if *BarnOwl::shutdown{CODE};
     435}
     436
     437sub _receive_msg {
     438    my $m = shift;
     439
     440    $receiveMessage->run($m);
     441   
     442    BarnOwl::receive_msg($m) if *BarnOwl::receive_msg{CODE};
     443}
     444
     445sub _mainloop_hook {
     446    $mainLoop->run;
    504447    BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE};
    505 
    506     BarnOwl::shutdown() if *BarnOwl::shutdown{CODE};
    507 }
    508 
    509 sub mainloop_hook
    510 {
    511     runHook(\@onMainLoop);
    512     BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE};
    513 }
    514 
    515 ################################################################################
    516 # Hooks into receive_msg()
    517 ################################################################################
    518 
    519 sub receive_msg
    520 {
    521     my $m = shift;
    522     runHook(\@onReceiveMsg, $m);
    523     BarnOwl::receive_msg($m) if *BarnOwl::receive_msg{CODE};
    524 }
    525 
    526 ################################################################################
    527 # Hooks into get_blist()
    528 ################################################################################
    529 
    530 sub get_blist
    531 {
    532     return runHook_accumulate(\@onGetBuddyList);
     448}
     449
     450sub _get_blist {
     451    return join("\n", $getBuddyList->run);
    533452}
    534453
     
    554473    }
    555474}
     475
     476BarnOwl::_create_style("default", "BarnOwl::Style::Default::format_message", "Default style");
    556477
    557478BarnOwl::_create_style("default", "BarnOwl::Style::Default::format_message", "Default style");
     
    644565# switch to package main when we're done
    645566package main;
    646 # alias the hooks
    647 {
    648     no strict 'refs';
    649     foreach my $hook  qw (onStartSubs
    650                           onReceiveMsg
    651                           onMainLoop
    652                           onGetBuddyList ) {
    653         *{"main::".$hook} = \*{"BarnOwl::Hooks::".$hook};
    654         *{"owl::".$hook} = \*{"BarnOwl::Hooks::".$hook};
    655     }
    656 }
     567
     568# Shove a bunch of fake entries into @INC so modules can use or
     569# require them without choking
     570$::INC{$_} = 1 for (qw(BarnOwl.pm BarnOwl/Hooks.pm
     571                       BarnOwl/Message.pm BarnOwl/Style.pm));
    657572
    6585731;
     574
Note: See TracChangeset for help on using the changeset viewer.