| 1 | use strict; |
|---|
| 2 | use warnings; |
|---|
| 3 | |
|---|
| 4 | package BarnOwl::ModuleLoader; |
|---|
| 5 | |
|---|
| 6 | use lib (BarnOwl::get_data_dir() . "/modules/"); |
|---|
| 7 | use PAR (BarnOwl::get_data_dir() . "/modules/*.par"); |
|---|
| 8 | use PAR ($ENV{HOME} . "/.owl/modules/*.par"); |
|---|
| 9 | |
|---|
| 10 | sub load_all { |
|---|
| 11 | my %modules; |
|---|
| 12 | my @modules; |
|---|
| 13 | |
|---|
| 14 | for my $dir ( BarnOwl::get_data_dir() . "/modules", |
|---|
| 15 | $ENV{HOME} . "/.owl/modules" ) { |
|---|
| 16 | opendir(my $dh, $dir) or next; |
|---|
| 17 | while(defined(my $f = readdir($dh))) { |
|---|
| 18 | next if $f =~ /^\./; |
|---|
| 19 | if(-f "$dir/$f" && $f =~ /^(.+)\.par$/) { |
|---|
| 20 | $modules{$1} = 1; |
|---|
| 21 | } elsif(-d "$dir/$f" && -d "$dir/$f/lib") { |
|---|
| 22 | push @INC, "$dir/$f/lib" unless grep m{^$dir/$f/lib$}, @INC; |
|---|
| 23 | $modules{$f} = 1; |
|---|
| 24 | } |
|---|
| 25 | } |
|---|
| 26 | @modules = grep /\.par$/, readdir($dh); |
|---|
| 27 | closedir($dh); |
|---|
| 28 | for my $mod (@modules) { |
|---|
| 29 | my ($class) = ($mod =~ /^(.+)\.par$/); |
|---|
| 30 | $modules{$class} = 1; |
|---|
| 31 | } |
|---|
| 32 | } |
|---|
| 33 | for my $class (keys %modules) { |
|---|
| 34 | if(!defined eval "use BarnOwl::Module::$class") { |
|---|
| 35 | BarnOwl::error("Unable to load module $class: $!") if $!; |
|---|
| 36 | BarnOwl::error("Unable to load module $class: $@") if $@; |
|---|
| 37 | } |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | $BarnOwl::Hooks::startup->add(\®ister_keybindings); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | sub register_keybindings { |
|---|
| 44 | BarnOwl::new_command('reload-modules', sub {BarnOwl::ModuleLoader->reload}, { |
|---|
| 45 | summary => 'Reload all modules', |
|---|
| 46 | usage => 'reload', |
|---|
| 47 | description => q{Reloads all modules located in ~/.owl/modules and the system modules directory} |
|---|
| 48 | }); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | sub reload { |
|---|
| 52 | my $class = shift; |
|---|
| 53 | for my $m (keys %INC) { |
|---|
| 54 | delete $INC{$m} if $m =~ m{^BarnOwl/}; |
|---|
| 55 | } |
|---|
| 56 | # Restore core modules from perlwrap.pm |
|---|
| 57 | $INC{$_} = 1 for (qw(BarnOwl.pm BarnOwl/Hooks.pm |
|---|
| 58 | BarnOwl/Message.pm BarnOwl/Style.pm)); |
|---|
| 59 | |
|---|
| 60 | $BarnOwl::Hooks::startup->clear; |
|---|
| 61 | $BarnOwl::Hooks::getBuddyList->clear; |
|---|
| 62 | $BarnOwl::Hooks::mainLoop->clear; |
|---|
| 63 | $BarnOwl::Hooks::shutdown->clear; |
|---|
| 64 | $BarnOwl::Hooks::receiveMessage->clear; |
|---|
| 65 | local $SIG{__WARN__} = \&squelch_redefine; |
|---|
| 66 | $class->load_all; |
|---|
| 67 | $BarnOwl::Hooks::startup->run(1); |
|---|
| 68 | BarnOwl::startup() if *BarnOwl::startup{CODE}; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | sub squelch_redefine { |
|---|
| 72 | my $warning = shift; |
|---|
| 73 | warn $warning unless $warning =~ /^Subroutine .+ redefined at/; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | 1; |
|---|