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