[0337203] | 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"); |
---|
[be98ba5] | 8 | use PAR (BarnOwl::get_config_dir() . "/modules/*.par"); |
---|
[0337203] | 9 | |
---|
| 10 | sub load_all { |
---|
[9506feb] | 11 | PAR::reload_libs(); |
---|
[e005862] | 12 | PAR->import(BarnOwl::get_data_dir() . "/modules/*.par"); |
---|
| 13 | PAR->import(BarnOwl::get_config_dir() . "/modules/*.par"); |
---|
[0337203] | 14 | my %modules; |
---|
| 15 | my @modules; |
---|
[be98ba5] | 16 | |
---|
| 17 | my @moddirs = (); |
---|
| 18 | push @moddirs, BarnOwl::get_data_dir() . "/modules"; |
---|
| 19 | push @moddirs, BarnOwl::get_config_dir() . "/modules"; |
---|
[0337203] | 20 | |
---|
[be98ba5] | 21 | for my $dir (@moddirs) { |
---|
[0337203] | 22 | opendir(my $dh, $dir) or next; |
---|
[836e6263] | 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") { |
---|
[b4fcc06] | 28 | unshift @INC, "$dir/$f/lib" unless grep m{^$dir/$f/lib$}, @INC; |
---|
[836e6263] | 29 | $modules{$f} = 1; |
---|
| 30 | } |
---|
| 31 | } |
---|
[0337203] | 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) { |
---|
[2622450] | 40 | if(!defined eval "use BarnOwl::Module::$class") { |
---|
[9506feb] | 41 | # BarnOwl::error("Unable to load module $class: $!") if $!; |
---|
[0337203] | 42 | BarnOwl::error("Unable to load module $class: $@") if $@; |
---|
| 43 | } |
---|
| 44 | } |
---|
[4c2ec6c] | 45 | |
---|
| 46 | $BarnOwl::Hooks::startup->add(\®ister_keybindings); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | sub register_keybindings { |
---|
| 50 | BarnOwl::new_command('reload-modules', sub {BarnOwl::ModuleLoader->reload}, { |
---|
| 51 | summary => 'Reload all modules', |
---|
[b0c8011] | 52 | usage => 'reload-modules', |
---|
[4c2ec6c] | 53 | description => q{Reloads all modules located in ~/.owl/modules and the system modules directory} |
---|
| 54 | }); |
---|
[0337203] | 55 | } |
---|
| 56 | |
---|
[836e6263] | 57 | sub 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; |
---|
[9de39410] | 67 | $BarnOwl::Hooks::getBuddyList->clear; |
---|
| 68 | $BarnOwl::Hooks::mainLoop->clear; |
---|
| 69 | $BarnOwl::Hooks::shutdown->clear; |
---|
| 70 | $BarnOwl::Hooks::receiveMessage->clear; |
---|
[836e6263] | 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 | |
---|
| 77 | sub squelch_redefine { |
---|
| 78 | my $warning = shift; |
---|
[9729dba] | 79 | warn $warning unless $warning =~ /^Subroutine .+ redefined at/; |
---|
[836e6263] | 80 | } |
---|
| 81 | |
---|
[0337203] | 82 | 1; |
---|