source: perl/lib/BarnOwl/ModuleLoader.pm @ cdd3959

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since cdd3959 was 965e14d, checked in by Nelson Elhage <nelhage@mit.edu>, 16 years ago
Generate less ugly error spew if a module fails to load
  • Property mode set to 100644
File size: 2.6 KB
Line 
1use strict;
2use warnings;
3
4package BarnOwl::ModuleLoader;
5
6use lib (BarnOwl::get_data_dir() . "/modules/");
7use PAR (BarnOwl::get_data_dir() . "/modules/*.par");
8use PAR (BarnOwl::get_config_dir() . "/modules/*.par");
9
10sub load_all {
11    PAR::reload_libs();
12    PAR->import(BarnOwl::get_data_dir() . "/modules/*.par");
13    PAR->import(BarnOwl::get_config_dir() . "/modules/*.par");
14    my %modules;
15    my @modules;
16
17    my @moddirs = ();
18    push @moddirs, BarnOwl::get_data_dir() . "/modules";
19    push @moddirs, BarnOwl::get_config_dir() . "/modules";
20   
21    for my $dir (@moddirs) {
22        opendir(my $dh, $dir) or next;
23        while(defined(my $f = readdir($dh))) {
24            next if $f =~ /^\./;
25            if(-f "$dir/$f" && $f =~ /^(.+)\.par$/) {
26                $modules{$1} = 1;
27            } elsif(-d "$dir/$f" && -d "$dir/$f/lib") {
28                unshift @INC, "$dir/$f/lib" unless grep m{^$dir/$f/lib$}, @INC;
29                $modules{$f} = 1;
30            }
31        }
32        @modules = grep /\.par$/, readdir($dh);
33        closedir($dh);
34        for my $mod (@modules) {
35            my ($class) = ($mod =~ /^(.+)\.par$/);
36            $modules{$class} = 1;
37        }
38    }
39    for my $class (keys %modules) {
40        if(!defined eval "use BarnOwl::Module::$class") {
41            # BarnOwl::error("Unable to load module $class: $!") if $!;
42            BarnOwl::error("Unable to load module $class: \n$@\n") if $@;
43        }
44    }
45
46    $BarnOwl::Hooks::startup->add(\&register_keybindings);
47}
48
49sub register_keybindings {
50    BarnOwl::new_command('reload-modules', sub {BarnOwl::ModuleLoader->reload}, {
51                           summary => 'Reload all modules',
52                           usage   => 'reload-modules',
53                           description => q{Reloads all modules located in ~/.owl/modules and the system modules directory}
54                          });
55}
56
57sub reload {
58    my $class = shift;
59    for my $m (keys %INC) {
60        delete $INC{$m} if $m =~ m{^BarnOwl/};
61    }
62    # Restore core modules from perlwrap.pm
63    $INC{$_} = 1 for (qw(BarnOwl.pm BarnOwl/Hooks.pm
64                         BarnOwl/Message.pm BarnOwl/Style.pm));
65
66    $BarnOwl::Hooks::startup->clear;
67    $BarnOwl::Hooks::getBuddyList->clear;
68    $BarnOwl::Hooks::mainLoop->clear;
69    $BarnOwl::Hooks::shutdown->clear;
70    $BarnOwl::Hooks::receiveMessage->clear;
71    local $SIG{__WARN__} = \&squelch_redefine;
72    $class->load_all;
73    $BarnOwl::Hooks::startup->run(1);
74    BarnOwl::startup() if *BarnOwl::startup{CODE};
75}
76
77sub squelch_redefine {
78    my $warning = shift;
79    warn $warning unless $warning =~ /^Subroutine .+ redefined at/;
80}
81
821;
Note: See TracBrowser for help on using the repository browser.