[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 | |
---|
[5987370] | 10 | our %modules; |
---|
| 11 | |
---|
[0337203] | 12 | sub load_all { |
---|
[5987370] | 13 | my $class = shift; |
---|
| 14 | $class->rescan_modules; |
---|
| 15 | |
---|
| 16 | for my $mod (keys %modules) { |
---|
| 17 | if(!defined eval "use BarnOwl::Module::$mod") { |
---|
| 18 | BarnOwl::error("Unable to load module $mod: \n$@\n") if $@; |
---|
| 19 | } |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | $BarnOwl::Hooks::startup->add(\®ister_keybindings); |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | sub rescan_modules { |
---|
[9506feb] | 26 | PAR::reload_libs(); |
---|
[e005862] | 27 | PAR->import(BarnOwl::get_data_dir() . "/modules/*.par"); |
---|
| 28 | PAR->import(BarnOwl::get_config_dir() . "/modules/*.par"); |
---|
[0337203] | 29 | my @modules; |
---|
[be98ba5] | 30 | |
---|
[5987370] | 31 | %modules = (); |
---|
| 32 | |
---|
[be98ba5] | 33 | my @moddirs = (); |
---|
| 34 | push @moddirs, BarnOwl::get_data_dir() . "/modules"; |
---|
| 35 | push @moddirs, BarnOwl::get_config_dir() . "/modules"; |
---|
[0337203] | 36 | |
---|
[be98ba5] | 37 | for my $dir (@moddirs) { |
---|
[0337203] | 38 | opendir(my $dh, $dir) or next; |
---|
[836e6263] | 39 | while(defined(my $f = readdir($dh))) { |
---|
| 40 | next if $f =~ /^\./; |
---|
| 41 | if(-f "$dir/$f" && $f =~ /^(.+)\.par$/) { |
---|
| 42 | $modules{$1} = 1; |
---|
| 43 | } elsif(-d "$dir/$f" && -d "$dir/$f/lib") { |
---|
[b4fcc06] | 44 | unshift @INC, "$dir/$f/lib" unless grep m{^$dir/$f/lib$}, @INC; |
---|
[836e6263] | 45 | $modules{$f} = 1; |
---|
| 46 | } |
---|
| 47 | } |
---|
[0337203] | 48 | @modules = grep /\.par$/, readdir($dh); |
---|
| 49 | closedir($dh); |
---|
| 50 | for my $mod (@modules) { |
---|
| 51 | my ($class) = ($mod =~ /^(.+)\.par$/); |
---|
| 52 | $modules{$class} = 1; |
---|
| 53 | } |
---|
| 54 | } |
---|
[5987370] | 55 | } |
---|
| 56 | |
---|
| 57 | sub reload_module { |
---|
| 58 | my $class = shift; |
---|
| 59 | my $module = shift; |
---|
| 60 | |
---|
| 61 | $class->rescan_modules(); |
---|
| 62 | |
---|
| 63 | for my $m (keys %INC) { |
---|
| 64 | delete $INC{$m} if $m =~ m{^BarnOwl/Module/$module}; |
---|
[0337203] | 65 | } |
---|
[4c2ec6c] | 66 | |
---|
[5987370] | 67 | local $SIG{__WARN__} = \&squelch_redefine; |
---|
| 68 | if(!defined eval "use BarnOwl::Module::$module") { |
---|
| 69 | BarnOwl::error("Unable to load module $module: \n$@\n") if $@; |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | sub reload_cmd { |
---|
| 74 | my $class = shift; |
---|
| 75 | shift; # Command |
---|
| 76 | my $module = shift; |
---|
| 77 | unless(defined($module)) { |
---|
| 78 | die("Usage: reload [MODULE]"); |
---|
| 79 | }; |
---|
| 80 | $class->reload_module($module); |
---|
[4c2ec6c] | 81 | } |
---|
| 82 | |
---|
| 83 | sub register_keybindings { |
---|
| 84 | BarnOwl::new_command('reload-modules', sub {BarnOwl::ModuleLoader->reload}, { |
---|
| 85 | summary => 'Reload all modules', |
---|
[b0c8011] | 86 | usage => 'reload-modules', |
---|
[4c2ec6c] | 87 | description => q{Reloads all modules located in ~/.owl/modules and the system modules directory} |
---|
| 88 | }); |
---|
[5987370] | 89 | BarnOwl::new_command('reload', sub {BarnOwl::ModuleLoader->reload_cmd(@_)}, { |
---|
| 90 | summary => 'Reload one module', |
---|
| 91 | usage => 'reload [MODULE]', |
---|
| 92 | description => q{Reloads a single module located in ~/.owl/modules or the system modules directory} |
---|
| 93 | }); |
---|
[0337203] | 94 | } |
---|
| 95 | |
---|
[836e6263] | 96 | sub reload { |
---|
| 97 | my $class = shift; |
---|
| 98 | for my $m (keys %INC) { |
---|
| 99 | delete $INC{$m} if $m =~ m{^BarnOwl/}; |
---|
| 100 | } |
---|
| 101 | # Restore core modules from perlwrap.pm |
---|
| 102 | $INC{$_} = 1 for (qw(BarnOwl.pm BarnOwl/Hooks.pm |
---|
| 103 | BarnOwl/Message.pm BarnOwl/Style.pm)); |
---|
| 104 | |
---|
| 105 | $BarnOwl::Hooks::startup->clear; |
---|
[9de39410] | 106 | $BarnOwl::Hooks::getBuddyList->clear; |
---|
| 107 | $BarnOwl::Hooks::mainLoop->clear; |
---|
| 108 | $BarnOwl::Hooks::shutdown->clear; |
---|
| 109 | $BarnOwl::Hooks::receiveMessage->clear; |
---|
[836e6263] | 110 | local $SIG{__WARN__} = \&squelch_redefine; |
---|
| 111 | $class->load_all; |
---|
| 112 | $BarnOwl::Hooks::startup->run(1); |
---|
| 113 | BarnOwl::startup() if *BarnOwl::startup{CODE}; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | sub squelch_redefine { |
---|
| 117 | my $warning = shift; |
---|
[9729dba] | 118 | warn $warning unless $warning =~ /^Subroutine .+ redefined at/; |
---|
[836e6263] | 119 | } |
---|
| 120 | |
---|
[0337203] | 121 | 1; |
---|