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

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