[549] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

[D-O-H] r667 - in branches/par: . perl/lib perl/lib/BarnOwl

daemon@ATHENA.MIT.EDU (nelhage@MIT.EDU)
Thu Oct 29 18:07:19 2009

Resent-From: nelhage@mit.edu
Resent-To: barnowl-dev-mtg@charon.mit.edu
To: dirty-owl-hackers@mit.edu
From: nelhage@MIT.EDU
Reply-to: dirty-owl-hackers@MIT.EDU
Date: Fri, 16 Mar 2007 00:45:20 -0400 (EDT)

Author: nelhage
Date: 2007-03-16 00:45:19 -0400 (Fri, 16 Mar 2007)
New Revision: 667

Added:
   branches/par/perl/lib/BarnOwl/
   branches/par/perl/lib/BarnOwl/ModuleLoader.pm
Modified:
   branches/par/
   branches/par/BUGS
   branches/par/functions.c
   branches/par/owl.c
   branches/par/perlconfig.c
   branches/par/perlwrap.pm
Log:
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.
 



Property changes on: branches/par
___________________________________________________________________
Name: svk:merge
   - 06e3988a-d725-0410-af47-c5dd11e74598:/local/barnowl:1380
8baf6839-b125-0410-9df9-922793c80423:/local/barnowl:17717
8baf6839-b125-0410-9df9-922793c80423:/local/owl:15641
   + 06e3988a-d725-0410-af47-c5dd11e74598:/local/barnowl:1380
8baf6839-b125-0410-9df9-922793c80423:/local/barnowl:17717
8baf6839-b125-0410-9df9-922793c80423:/local/owl:15641
bb873fd7-8e23-0410-944a-99ec44c633eb:/local/d-o-h/branches/par:19594

Modified: branches/par/BUGS
===================================================================
--- branches/par/BUGS	2007-03-15 18:14:52 UTC (rev 666)
+++ branches/par/BUGS	2007-03-16 04:45:19 UTC (rev 667)
@@ -4,4 +4,3 @@
   long JIDs [nelhage]
 * reply to resource names from ichat (foo's computer) fails badly [hartmans]
 * viewuser doesn't work with AIM or Jabber
-* jmuc join'ing a MUC you're already in has weird behavior [nelhage]

Modified: branches/par/functions.c
===================================================================
--- branches/par/functions.c	2007-03-15 18:14:52 UTC (rev 666)
+++ branches/par/functions.c	2007-03-16 04:45:19 UTC (rev 667)
@@ -1034,7 +1034,7 @@
   }
 
   /* execute the commands in shutdown */
-  ret = owl_perlconfig_execute("BarnOwl::Hooks::shutdown();");
+  ret = owl_perlconfig_execute("BarnOwl::Hooks::_shutdown();");
   if (ret) owl_free(ret);
 
   /* signal our child process, if any */
@@ -3364,8 +3364,8 @@
 #endif
 
   if(aim && zephyr) {
-      if(owl_perlconfig_is_function("BarnOwl::Hooks::get_blist")) {
-          char * perlblist = owl_perlconfig_execute("BarnOwl::Hooks::get_blist()");
+      if(owl_perlconfig_is_function("BarnOwl::Hooks::_get_blist")) {
+          char * perlblist = owl_perlconfig_execute("BarnOwl::Hooks::_get_blist()");
           if(perlblist) {
               owl_fmtext_append_ztext(&fm, perlblist);
               owl_free(perlblist);

Modified: branches/par/owl.c
===================================================================
--- branches/par/owl.c	2007-03-15 18:14:52 UTC (rev 666)
+++ branches/par/owl.c	2007-03-16 04:45:19 UTC (rev 667)
@@ -317,7 +317,7 @@
 
   /* execute the startup function in the configfile */
   owl_function_debugmsg("startup: executing perl startup, if applicable");
-  perlout = owl_perlconfig_execute("BarnOwl::Hooks::startup();");
+  perlout = owl_perlconfig_execute("BarnOwl::Hooks::_startup();");
   if (perlout) owl_free(perlout);
 
   /* hold on to the window names for convenience */

Added: branches/par/perl/lib/BarnOwl/ModuleLoader.pm
===================================================================
--- branches/par/perl/lib/BarnOwl/ModuleLoader.pm	                        (rev 0)
+++ branches/par/perl/lib/BarnOwl/ModuleLoader.pm	2007-03-16 04:45:19 UTC (rev 667)
@@ -0,0 +1,31 @@
+use strict;
+use warnings;
+
+package BarnOwl::ModuleLoader;
+
+use lib (BarnOwl::get_data_dir() . "/modules/");
+use PAR (BarnOwl::get_data_dir() . "/modules/*.par");
+use PAR ($ENV{HOME} . "/.owl/modules/*.par");
+
+sub load_all {
+    my %modules;
+    my @modules;
+    
+    for my $dir ( BarnOwl::get_data_dir() . "/modules",
+                  $ENV{HOME} . "/.owl/modules" ) {
+        opendir(my $dh, $dir) or next;
+        @modules = grep /\.par$/, readdir($dh);
+        closedir($dh);
+        for my $mod (@modules) {
+            my ($class) = ($mod =~ /^(.+)\.par$/);
+            $modules{$class} = 1;
+        }
+    }
+    for my $class (keys %modules) {
+        if(defined eval "use BarnOwl::Module::$class") {
+            BarnOwl::error("Unable to load module $class: $@") if $@;
+        }
+    }
+}
+
+1;

Modified: branches/par/perlconfig.c
===================================================================
--- branches/par/perlconfig.c	2007-03-15 18:14:52 UTC (rev 666)
+++ branches/par/perlconfig.c	2007-03-16 04:45:19 UTC (rev 667)
@@ -421,7 +421,7 @@
     return ret;
   } else {
     char *ptr = NULL;
-    if (owl_perlconfig_is_function("BarnOwl::Hooks::receive_msg")) {
+    if (owl_perlconfig_is_function("BarnOwl::Hooks::_receive_msg")) {
       ptr = owl_perlconfig_call_with_message(subname?subname
 				       :"BarnOwl::_receive_msg_legacy_wrap", m);
     }
@@ -505,11 +505,11 @@
 
 void owl_perlconfig_mainloop()
 {
-  if (!owl_perlconfig_is_function("BarnOwl::Hooks::mainloop_hook"))
+  if (!owl_perlconfig_is_function("BarnOwl::Hooks::_mainloop_hook"))
     return;
   dSP ;
   PUSHMARK(SP) ;
-  call_pv("BarnOwl::Hooks::mainloop_hook", G_DISCARD|G_EVAL);
+  call_pv("BarnOwl::Hooks::_mainloop_hook", G_DISCARD|G_EVAL);
   if(SvTRUE(ERRSV)) {
     STRLEN n_a;
     owl_function_error("%s", SvPV(ERRSV, n_a));

Modified: branches/par/perlwrap.pm
===================================================================
--- branches/par/perlwrap.pm	2007-03-15 18:14:52 UTC (rev 666)
+++ branches/par/perlwrap.pm	2007-03-16 04:45:19 UTC (rev 667)
@@ -6,16 +6,17 @@
 #####################################################################
 # XXX NOTE: This file is sourced before almost any barnowl
 # architecture is loaded. This means, for example, that it cannot
-# execute any owl commands. Any code that needs to do so, should
-# create a function wrapping it and push it onto @onStartSubs
+# execute any owl commands. Any code that needs to do so should live
+# in BarnOwl::Hooks::_startup
 
-
 use strict;
 use warnings;
 
 package BarnOwl;
 
+package BarnOwl;
 
+
 BEGIN {
 # bootstrap in C bindings and glue
     *owl:: = \*BarnOwl::;
@@ -43,7 +44,7 @@
 sub _receive_msg_legacy_wrap {
     my ($m) = @_;
     $m->legacy_populate_global();
-    return &BarnOwl::Hooks::receive_msg($m);
+    return &BarnOwl::Hooks::_receive_msg($m);
 }
 
 # make BarnOwl::<command>("foo") be aliases to BarnOwl::command("<command> foo");
@@ -203,7 +204,7 @@
 }
 
 sub smartfilter {
-    die("smartfilter not supported for this message");
+    die("smartfilter not supported for this message\n");
 }
 
 # Display fields -- overridden by subclasses when needed
@@ -350,89 +351,51 @@
 #####################################################################
 #####################################################################
 ################################################################################
-package BarnOwl;
 
-################################################################################
-# Mainloop hook
-################################################################################
+package BarnOwl::Hook;
 
-our $shutdown;
-$shutdown = 0;
-our $reload;
-$reload = 0;
+sub new {
+    my $class = shift;
+    return bless [], $class;
+}
 
-#Run this on start and reload. Adds modules
-sub onStart
-{
-    _load_owlconf();
-    reload_init();
-    loadModules();
+sub run {
+    my $self = shift;
+    my @args = @_;
+    return map {$_->(@args)} @$self;
 }
-################################################################################
-# Reload Code, taken from /afs/sipb/user/jdaniel/project/owl/perl
-################################################################################
-sub reload_hook (@)
-{
-    BarnOwl::Hooks::startup();
-    return 1;
-}
 
-sub reload
-{
-    # Use $reload to tell modules that we're performing a reload.
-  {
-      local $reload = 1;
-      BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE};
-  }
-
-  @BarnOwl::Hooks::onMainLoop = ();
-  @BarnOwl::Hooks::onStartSubs = ();
-
-  # Do reload
-  package main;
-  if (-r $BarnOwl::configfile) {
-      undef $@;
-      do $BarnOwl::configfile;
-      BarnOwl::error("Error reloading $BarnOwl::configfile: $@") if $@;
-  }
-  BarnOwl::reload_hook(@_);
-  package BarnOwl;
+sub add {
+    my $self = shift;
+    my $func = shift;
+    die("Not a coderef!") unless ref($func) eq 'CODE';
+    push @$self, $func;
 }
 
-sub reload_init ()
-{
-    BarnOwl::command('alias reload perl BarnOwl::reload()');
-    BarnOwl::command('bindkey global "C-x C-r" command reload');
+sub clear {
+    my $self = shift;
+    @$self = ();
 }
 
-################################################################################
-# Loads modules from ~/.owl/modules and owl's data directory
-################################################################################
+package BarnOwl::Hooks;
 
-sub loadModules () {
-    my @modules;
-    my $rv;
-    foreach my $dir ( BarnOwl::get_data_dir() . "/modules",
-                      $ENV{HOME} . "/.owl/modules" )
-    {
-        opendir( MODULES, $dir );
+use Exporter;
 
-        # source ./modules/*.pl
-        @modules = sort grep( /\.pl$/, readdir(MODULES) );
+our @EXPORT_OK = qw($startup $shutdown
+                    $receiveMessage $mainLoop
+                    $getBuddyList);
 
-        foreach my $mod (@modules) {
-            unless ($rv = do "$dir/$mod") {
-                BarnOwl::error("Couldn't load $dir/$mod:\n $@") if $@;
-                BarnOwl::error("Couldn't run $dir/$mod:\n $!") unless defined $rv;
-            }
-        }
-        closedir(MODULES);
-    }
-}
+our %EXPORT_TAGS = (all => [@EXPORT_OK]);
 
+our $startup = BarnOwl::Hook->new;
+our $shutdown = BarnOwl::Hook->new;
+our $receiveMessage = BarnOwl::Hook->new;
+our $mainLoop = BarnOwl::Hook->new;
+our $getBuddyList = BarnOwl::Hook->new;
+
+# Internal startup/shutdown routines called by the C code
+
 sub _load_owlconf {
-    # Only do this the first time
-    return if $BarnOwl::reload;
     # load the config  file
     if ( -r $BarnOwl::configfile ) {
         undef $@;
@@ -450,86 +413,42 @@
     }
 }
 
-package BarnOwl::Hooks;
+sub _startup {
+    _load_owlconf();
 
-# Arrays of subrefs to be called at specific times.
-our @onStartSubs = ();
-our @onReceiveMsg = ();
-our @onMainLoop = ();
-our @onGetBuddyList = ();
-
-# Functions to call hook lists
-sub runHook($@)
-{
-    my $hook = shift;
-    my @args = @_;
-    $_->(@args) for (@$hook);
-}
-
-sub runHook_accumulate($@)
-{
-    my $hook = shift;
-    my @args = @_;
-    return join("\n", map {$_->(@args)} @$hook);
-}
-
-################################################################################
-# Startup and Shutdown code
-################################################################################
-sub startup
-{
-    # Modern versions of owl provides a great place to have startup stuff.
-    # Put things in ~/.owl/startup
-
-    #So that the user's .owlconf can have startsubs, we don't clear
-    #onStartSubs; reload does however
-    @onReceiveMsg = ();
-    @onMainLoop = ();
-    @onGetBuddyList = ();
-
-    BarnOwl::onStart();
-
-    runHook(\@onStartSubs);
-
+    if(eval {require BarnOwl::ModuleLoader}) {
+        eval {
+            BarnOwl::ModuleLoader->load_all;
+        };
+    } else {
+        BarnOwl::error("Can't load BarnOwl::ModuleLoader, loadable module support disabled:\n$@");
+    }
+    
+    $startup->run;
     BarnOwl::startup() if *BarnOwl::startup{CODE};
 }
 
-sub shutdown
-{
-# Modern versions of owl provides a great place to have shutdown stuff.
-# Put things in ~/.owl/shutdown
-
-    # use $shutdown to tell modules that that's what we're doing.
-    $BarnOwl::shutdown = 1;
-    BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE};
-
+sub _shutdown {
+    $shutdown->run;
+    
     BarnOwl::shutdown() if *BarnOwl::shutdown{CODE};
 }
 
-sub mainloop_hook
-{
-    runHook(\@onMainLoop);
-    BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE};
-}
-
-################################################################################
-# Hooks into receive_msg()
-################################################################################
-
-sub receive_msg
-{
+sub _receive_msg {
     my $m = shift;
-    runHook(\@onReceiveMsg, $m);
+
+    $receiveMessage->run($m);
+    
     BarnOwl::receive_msg($m) if *BarnOwl::receive_msg{CODE};
 }
 
-################################################################################
-# Hooks into get_blist()
-################################################################################
+sub _mainloop_hook {
+    $mainLoop->run;
+    BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE};
+}
 
-sub get_blist
-{
-    return runHook_accumulate(\@onGetBuddyList);
+sub _get_blist {
+    return join("\n", $getBuddyList->run);
 }
 
 ################################################################################
@@ -556,6 +475,8 @@
 
 BarnOwl::_create_style("default", "BarnOwl::Style::Default::format_message", "Default style");
 
+BarnOwl::_create_style("default", "BarnOwl::Style::Default::format_message", "Default style");
+
 ################################################################################
 
 sub time_hhmm {
@@ -643,16 +564,11 @@
 
 # switch to package main when we're done
 package main;
-# alias the hooks
-{
-    no strict 'refs';
-    foreach my $hook  qw (onStartSubs
-                          onReceiveMsg
-                          onMainLoop
-                          onGetBuddyList ) {
-        *{"main::".$hook} = \*{"BarnOwl::Hooks::".$hook};
-        *{"owl::".$hook} = \*{"BarnOwl::Hooks::".$hook};
-    }
-}
 
+# Shove a bunch of fake entries into @INC so modules can use or
+# require them without choking
+$::INC{$_} = 1 for (qw(BarnOwl.pm BarnOwl/Hooks.pm
+                       BarnOwl/Message.pm BarnOwl/Style.pm));
+
 1;
+


home help back first fref pref prev next nref lref last post