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 | PAR->import(BarnOwl::get_data_dir() . "/modules/*.par"); |
---|
13 | PAR->import(BarnOwl::get_config_dir() . "/modules/*.par"); |
---|
14 | my %modules; |
---|
15 | my @modules; |
---|
16 | |
---|
17 | my @moddirs = (); |
---|
18 | push @moddirs, BarnOwl::get_data_dir() . "/modules"; |
---|
19 | push @moddirs, BarnOwl::get_config_dir() . "/modules"; |
---|
20 | |
---|
21 | for my $dir (@moddirs) { |
---|
22 | opendir(my $dh, $dir) or next; |
---|
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") { |
---|
28 | unshift @INC, "$dir/$f/lib" unless grep m{^$dir/$f/lib$}, @INC; |
---|
29 | $modules{$f} = 1; |
---|
30 | } |
---|
31 | } |
---|
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) { |
---|
40 | if(!defined eval "use BarnOwl::Module::$class") { |
---|
41 | # BarnOwl::error("Unable to load module $class: $!") if $!; |
---|
42 | BarnOwl::error("Unable to load module $class: $@") if $@; |
---|
43 | } |
---|
44 | } |
---|
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', |
---|
52 | usage => 'reload-modules', |
---|
53 | description => q{Reloads all modules located in ~/.owl/modules and the system modules directory} |
---|
54 | }); |
---|
55 | } |
---|
56 | |
---|
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; |
---|
67 | $BarnOwl::Hooks::getBuddyList->clear; |
---|
68 | $BarnOwl::Hooks::mainLoop->clear; |
---|
69 | $BarnOwl::Hooks::shutdown->clear; |
---|
70 | $BarnOwl::Hooks::receiveMessage->clear; |
---|
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; |
---|
79 | warn $warning unless $warning =~ /^Subroutine .+ redefined at/; |
---|
80 | } |
---|
81 | |
---|
82 | 1; |
---|