| 1 |
package Atheme::Init; |
| 2 |
|
| 3 |
=head1 init.pl -- perl initialisation for Atheme. |
| 4 |
|
| 5 |
This file is processed on initialising the perl interpreter in C<perl.so>. It is |
| 6 |
mainly concerned with setting up the Perl environment, maintaining the list of |
| 7 |
loaded perl scripts, and providing the means to load and unload them. |
| 8 |
|
| 9 |
=cut |
| 10 |
|
| 11 |
use strict; |
| 12 |
use warnings; |
| 13 |
|
| 14 |
use FindBin; |
| 15 |
use Symbol qw(delete_package); |
| 16 |
|
| 17 |
use lib "$FindBin::Bin"; |
| 18 |
|
| 19 |
use Atheme; |
| 20 |
use Atheme::Internal; |
| 21 |
|
| 22 |
our %Scripts; |
| 23 |
|
| 24 |
sub package_name_ify { |
| 25 |
my ($filename) = @_; |
| 26 |
|
| 27 |
$filename =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg; |
| 28 |
# second pass only for words starting with a digit |
| 29 |
$filename =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg; |
| 30 |
|
| 31 |
# Dress it up as a real package name |
| 32 |
$filename =~ s|/|::|g; |
| 33 |
return "Embed" . $filename; |
| 34 |
} |
| 35 |
|
| 36 |
sub load_script { |
| 37 |
my ($filename, $message, $nonfatal) = @_; |
| 38 |
my $packagename = package_name_ify($filename); |
| 39 |
|
| 40 |
if (defined $Scripts{$packagename}) |
| 41 |
{ |
| 42 |
if ($nonfatal) { |
| 43 |
$message->reply("$filename is already loaded"); |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
# This will always be called with G_EVAL, so this die can be translated |
| 48 |
# into a load error in the C module. |
| 49 |
die "$filename is already loaded."; |
| 50 |
} |
| 51 |
|
| 52 |
local *FH; |
| 53 |
open FH, $filename or die "Couldn't open $filename: $!"; |
| 54 |
local ($/) = undef; |
| 55 |
my $script_text = <FH>; |
| 56 |
close FH; |
| 57 |
|
| 58 |
# Must all be on one line here to avoid messing up line numbers in error |
| 59 |
# messages. |
| 60 |
my $eval = "package $packagename; our %Info; $script_text"; |
| 61 |
{ |
| 62 |
my ($filename, $packagename, $script_text); |
| 63 |
eval $eval; |
| 64 |
} |
| 65 |
if ($@) { |
| 66 |
delete_package $packagename; |
| 67 |
die $@; |
| 68 |
} |
| 69 |
|
| 70 |
$Scripts{$packagename} = $filename; |
| 71 |
return $packagename; |
| 72 |
} |
| 73 |
|
| 74 |
sub unload_script { |
| 75 |
my ($filename, $message) = @_; |
| 76 |
my $packagename = package_name_ify($filename); |
| 77 |
|
| 78 |
if (!defined $Scripts{$packagename}) |
| 79 |
{ |
| 80 |
die "$filename is not loaded"; |
| 81 |
} |
| 82 |
|
| 83 |
eval "${packagename}::unload(\$message)"; |
| 84 |
|
| 85 |
Atheme::Internal::unbind_commands($packagename); |
| 86 |
Atheme::Hooks::unbind_hooks($packagename); |
| 87 |
|
| 88 |
delete_package($packagename); |
| 89 |
delete $Scripts{$packagename}; |
| 90 |
} |
| 91 |
|
| 92 |
sub list_scripts { |
| 93 |
my ($si) = @_; |
| 94 |
$si->success("Loaded scripts:"); |
| 95 |
my $num = 0; |
| 96 |
foreach my $script (values %Scripts) { |
| 97 |
$num++; |
| 98 |
$si->success("$num: $script"); |
| 99 |
} |
| 100 |
$si->success("\x02$num\x02 scripts loaded."); |
| 101 |
} |
| 102 |
|
| 103 |
sub call_wrapper { |
| 104 |
my $sub = shift; |
| 105 |
my $saved_alarm=$SIG{ALRM}; |
| 106 |
$SIG{ALRM} = sub { die "Script used too much running time"; }; |
| 107 |
my $ret; |
| 108 |
eval { |
| 109 |
alarm 1; |
| 110 |
$ret = &$sub(@_); |
| 111 |
}; |
| 112 |
alarm 0; |
| 113 |
$SIG{ALRM} = $saved_alarm; |
| 114 |
die $@ if $@; |
| 115 |
return $ret; |
| 116 |
} |
| 117 |
|