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 | 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 | unshift @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(\®ister_keybindings); |
---|
45 | } |
---|
46 | |
---|
47 | sub 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 | |
---|
55 | sub 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 | |
---|
75 | sub squelch_redefine { |
---|
76 | my $warning = shift; |
---|
77 | warn $warning unless $warning =~ /^Subroutine .+ redefined at/; |
---|
78 | } |
---|
79 | |
---|
80 | 1; |
---|