| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use FindBin; |
| 7 |
|
| 8 |
my %hooks; |
| 9 |
my %arg_types; |
| 10 |
|
| 11 |
# XXX: Types we haven't exposed to perl yet. Remove these if they do become supported. |
| 12 |
# THIS LIST IS NOT A SUBSTITUTE FOR ACTUALLY DEFINING HOOK STRUCTURES. IT IS FOR STRUCTURES |
| 13 |
# WITH MEMBERS THAT WE CAN'T SUPPORT YET. |
| 14 |
my @unsupported_types = ( 'database_handle_t', 'sasl_message_t', |
| 15 |
'hook_module_load_t', 'hook_myentity_req_t', 'hook_host_request_t', |
| 16 |
'hook_channel_acl_req_t', 'hook_email_canonicalize_t', 'mygroup_t' ); |
| 17 |
|
| 18 |
# Types that need special handling. Define the dispatch for these, but the handler |
| 19 |
# functions themselves are hand-written. |
| 20 |
my @special_types = ( 'hook_expiry_req_t' ); |
| 21 |
|
| 22 |
# XXX: Duplication here with the typedefs in Atheme.xs. |
| 23 |
my %perl_api_types = ( |
| 24 |
sourceinfo_t => 'Atheme::Sourceinfo', |
| 25 |
user_t => 'Atheme::User', |
| 26 |
channel_t => 'Atheme::Channel', |
| 27 |
chanuser_t => 'Atheme::ChanUser', |
| 28 |
server_t => 'Atheme::Server', |
| 29 |
service_t => 'Atheme::Service', |
| 30 |
myuser_t => 'Atheme::Account', |
| 31 |
mynick_t => 'Atheme::NickRegistration', |
| 32 |
mychan_t => 'Atheme::ChannelRegistration', |
| 33 |
chanacs_t => 'Atheme::ChanAcs', |
| 34 |
|
| 35 |
'char *' => [ sub { "sv_setpv($_[0], $_[1]);" }, sub { die "Don't know how to unmarshal a read-write string"; } ], |
| 36 |
'const char *' => [ sub { "sv_setpv($_[0], $_[1]);" }, sub { die "Don't know how to unmarshal a read-write string"; } ], |
| 37 |
'int' => [ sub { "sv_setiv($_[0], $_[1]);" }, sub { "$_[1] = SvIV($_[0]);" } ], |
| 38 |
'time_t' => [ sub { "sv_setiv($_[0], $_[1]);" }, sub { "$_[1]= SvIV($_[0]);" } ], |
| 39 |
); |
| 40 |
|
| 41 |
# Define the mapping from C structures to perl hashrefs. |
| 42 |
# Each structure is a hash of (member => definition). |
| 43 |
# definition can either be a string type (pointers are implicit for supported structure types), |
| 44 |
# or an arrayref of [type, name], if the name on the perl side is to be different from the |
| 45 |
# member name in the structure. Prefix name with '+' if this value may be modified by the hook. |
| 46 |
my %hook_structs = ( |
| 47 |
hook_channel_joinpart_t => { |
| 48 |
cu => [ 'chanuser_t', '+chanuser' ] |
| 49 |
}, |
| 50 |
hook_cmessage_data_t => { |
| 51 |
u => [ 'user_t', 'user' ], |
| 52 |
c => [ 'channel_t', 'channel' ], |
| 53 |
msg => [ 'char *', 'message' ], |
| 54 |
}, |
| 55 |
hook_channel_topic_check_t => { |
| 56 |
u => [ 'user_t', 'user' ], |
| 57 |
s => [ 'server_t', 'server' ], |
| 58 |
c => [ 'channel_t', 'channel' ], |
| 59 |
setter => 'char *', |
| 60 |
ts => 'time_t', |
| 61 |
topic => [ 'char *', 'topic' ], |
| 62 |
approved => [ 'int', '+approved' ], |
| 63 |
}, |
| 64 |
hook_channel_req_t => { |
| 65 |
mc => [ 'mychan_t', 'channel' ], |
| 66 |
si => [ 'sourceinfo_t', 'source' ], |
| 67 |
}, |
| 68 |
hook_channel_succession_req_t => { |
| 69 |
mc => [ 'mychan_t', 'channel' ], |
| 70 |
mu => [ 'myuser_t', '+account' ], |
| 71 |
}, |
| 72 |
hook_channel_register_check_t => { |
| 73 |
si => [ 'sourceinfo_t', 'source' ], |
| 74 |
name => 'const char *', |
| 75 |
chan => [ 'channel_t', 'channel' ], |
| 76 |
approved => [ 'int', '+approved' ], |
| 77 |
}, |
| 78 |
hook_user_req_t => { |
| 79 |
si => [ 'sourceinfo_t', 'source' ], |
| 80 |
mu => [ 'myuser_t', 'account' ], |
| 81 |
mn => [ 'mynick_t', 'nick' ], |
| 82 |
}, |
| 83 |
hook_user_register_check_t => { |
| 84 |
si => [ 'sourceinfo_t', 'source' ], |
| 85 |
account => 'const char *', |
| 86 |
email => 'const char *', |
| 87 |
password => 'const char *', |
| 88 |
approved => [ 'int', '+approved' ] |
| 89 |
}, |
| 90 |
hook_nick_enforce_t => { |
| 91 |
u => [ 'user_t', 'user' ], |
| 92 |
mn => [ 'mynick_t', 'nick' ], |
| 93 |
}, |
| 94 |
hook_metadata_change_t => { |
| 95 |
target => 'myuser_t', |
| 96 |
name => 'const char *', |
| 97 |
value => 'char *', |
| 98 |
}, |
| 99 |
hook_user_rename_t => { |
| 100 |
mu => [ 'myuser_t', 'account' ], |
| 101 |
oldname => 'const char *', |
| 102 |
}, |
| 103 |
hook_server_delete_t => { |
| 104 |
s => [ 'server_t', 'server' ], |
| 105 |
}, |
| 106 |
hook_user_nick_t => { |
| 107 |
u => [ 'user_t', '+user' ], |
| 108 |
oldnick => 'const char *', |
| 109 |
}, |
| 110 |
hook_channel_mode_t => { |
| 111 |
u => [ 'user_t', 'user' ], |
| 112 |
c => [ 'channel_t', 'channel' ], |
| 113 |
}, |
| 114 |
hook_channel_mode_change_t => { |
| 115 |
cu => [ 'chanuser_t', 'chanuser' ], |
| 116 |
mchar => 'int', |
| 117 |
mvalue => 'int', |
| 118 |
}, |
| 119 |
hook_user_delete_t => { |
| 120 |
u => [ 'user_t', 'user' ], |
| 121 |
comment => 'const char *', |
| 122 |
}, |
| 123 |
hook_sasl_may_impersonate_t => { |
| 124 |
source_mu => [ 'myuser_t', 'source' ], |
| 125 |
target_mu => [ 'myuser_t', 'target' ], |
| 126 |
allowed => [ 'int', '+allowed' ] |
| 127 |
}, |
| 128 |
hook_info_noexist_req_t => { |
| 129 |
si => [ 'sourceinfo_t', 'source' ], |
| 130 |
nick => 'const char *' |
| 131 |
}, |
| 132 |
hook_user_needforce_t => { |
| 133 |
si => [ 'sourceinfo_t', 'source' ], |
| 134 |
mu => [ 'myuser_t', 'account' ], |
| 135 |
allowed => [ 'int', '+allowed' ] |
| 136 |
}, |
| 137 |
); |
| 138 |
|
| 139 |
sub c_var_to_sv { |
| 140 |
my ($varname, $typename, $svname) = @_; |
| 141 |
|
| 142 |
my $type = $perl_api_types{$typename}; |
| 143 |
|
| 144 |
if (ref $type eq 'ARRAY') { |
| 145 |
return $type->[0]->($svname, $varname); |
| 146 |
} else { |
| 147 |
return "$svname = bless_pointer_to_package($varname, \"$type\");"; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
sub c_var_from_sv { |
| 152 |
my ($varname, $typename, $svname) = @_; |
| 153 |
|
| 154 |
my $type = $perl_api_types{$typename}; |
| 155 |
|
| 156 |
if (ref $type eq 'ARRAY') { |
| 157 |
return $type->[1]->($svname, $varname); |
| 158 |
} else { |
| 159 |
return "if (!SvTRUE($svname)) $varname = NULL;"; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
open my $hooktypes, "$FindBin::Bin/../../../../include/hooktypes.in" |
| 164 |
or die "Couldn't open hooktypes.in: $!"; |
| 165 |
|
| 166 |
while (<$hooktypes>) { |
| 167 |
next if /^#/ or /^$/; |
| 168 |
/^(\w+)\s+(.+)$/; |
| 169 |
my ($hookname, $arg_type) = ($1, $2); |
| 170 |
$arg_type =~ s/ \*$//; |
| 171 |
chomp $arg_type; |
| 172 |
if (!$hookname || !$arg_type) { |
| 173 |
warn "Couldn't parse hooktypes.in line: '$_'\n"; |
| 174 |
next; |
| 175 |
} |
| 176 |
next if grep { $_ eq $arg_type } @unsupported_types; |
| 177 |
unless ($perl_api_types{$arg_type} || |
| 178 |
$hook_structs{$arg_type} || |
| 179 |
(grep { $_ eq $arg_type } @special_types) || |
| 180 |
$arg_type eq "void") { |
| 181 |
warn "Hook type '$arg_type' is neither specified for use nor marked as currently unsupported\n"; |
| 182 |
next; |
| 183 |
} |
| 184 |
|
| 185 |
$hooks{$hookname} = $arg_type; |
| 186 |
$arg_types{$arg_type} = 1 unless grep { $_ eq $arg_type } @special_types; |
| 187 |
} |
| 188 |
close $hooktypes; |
| 189 |
|
| 190 |
open my $outfile, '>', "perl_hooks.c" or die "Couldn't open perl_hooks.h: $!"; |
| 191 |
|
| 192 |
print $outfile <<EOF; |
| 193 |
/* |
| 194 |
* THIS IS A GENERATED FILE -- DO NOT EDIT. |
| 195 |
* |
| 196 |
* Change and re-run make_perl_hooks.pl instead. |
| 197 |
*/ |
| 198 |
#include "atheme_perl.h" |
| 199 |
#include "perl_hooks.h" |
| 200 |
|
| 201 |
typedef enum { |
| 202 |
PERL_HOOK_TO_PERL, |
| 203 |
PERL_HOOK_FROM_PERL |
| 204 |
} perl_hook_marshal_direction_t; |
| 205 |
|
| 206 |
#include "perl_hooks_extra.h" |
| 207 |
|
| 208 |
EOF |
| 209 |
|
| 210 |
# |
| 211 |
# Write the hook-data marshalling functions. |
| 212 |
# |
| 213 |
|
| 214 |
foreach my $arg_type (sort keys %arg_types) { |
| 215 |
next if ($arg_type eq 'void'); |
| 216 |
|
| 217 |
if (defined $perl_api_types{$arg_type}) { |
| 218 |
# Simple type. Straightforward marshaller. |
| 219 |
|
| 220 |
my $conv_code = c_var_to_sv("data", $arg_type, "*psv"); |
| 221 |
|
| 222 |
print $outfile <<"EOF"; |
| 223 |
static void perl_hook_marshal_$arg_type (perl_hook_marshal_direction_t dir, $arg_type * data, SV ** psv) |
| 224 |
{ if (dir == PERL_HOOK_TO_PERL) $conv_code } |
| 225 |
|
| 226 |
EOF |
| 227 |
} else { |
| 228 |
# Hook struct. Turn it into a hashref. |
| 229 |
|
| 230 |
my $struct = $hook_structs{$arg_type}; |
| 231 |
|
| 232 |
if (!$struct) { |
| 233 |
warn "Found unknown argument type '$arg_type' in hooktypes.in\n"; |
| 234 |
next; |
| 235 |
} |
| 236 |
|
| 237 |
next if grep { $_ eq $arg_type } @special_types; |
| 238 |
|
| 239 |
my @members; |
| 240 |
|
| 241 |
foreach my $member (sort keys %$struct) { |
| 242 |
my $definition = $struct->{$member}; |
| 243 |
my ($type, $pretty_name, $rw); |
| 244 |
|
| 245 |
if (ref $definition eq 'ARRAY') { |
| 246 |
$type = $definition->[0]; |
| 247 |
$pretty_name = $definition->[1]; |
| 248 |
$rw = 1 if $pretty_name =~ s/^\+//; |
| 249 |
} else { |
| 250 |
$type = $definition; |
| 251 |
$pretty_name = $member; |
| 252 |
$rw = 0; |
| 253 |
} |
| 254 |
push @members, [ $member, $type, $pretty_name, $rw ]; |
| 255 |
} |
| 256 |
|
| 257 |
print $outfile <<"EOF"; |
| 258 |
static void perl_hook_marshal_$arg_type (perl_hook_marshal_direction_t dir, $arg_type * data, SV ** psv) |
| 259 |
{ |
| 260 |
if (dir == PERL_HOOK_TO_PERL) |
| 261 |
{ |
| 262 |
HV *hash = newHV(); |
| 263 |
SV *sv_tmp = NULL; |
| 264 |
EOF |
| 265 |
|
| 266 |
foreach my $member (@members) { |
| 267 |
my ($name, $type, $pretty_name) = @$member; |
| 268 |
my $namelen = length $pretty_name; |
| 269 |
my $conv_code = c_var_to_sv("data->$name", $type, "sv_tmp"); |
| 270 |
|
| 271 |
print $outfile <<"EOF"; |
| 272 |
$conv_code |
| 273 |
hv_store(hash, "$pretty_name", $namelen, sv_tmp, 0); |
| 274 |
EOF |
| 275 |
} |
| 276 |
|
| 277 |
print $outfile <<"EOF"; |
| 278 |
|
| 279 |
*psv = newRV_noinc((SV*)hash); |
| 280 |
} else { |
| 281 |
return_if_fail(SvROK(*psv) && SvTYPE(SvRV(*psv)) == SVt_PVHV); |
| 282 |
HV *hash = (HV*)SvRV(*psv); |
| 283 |
SV **psv_tmp = NULL; |
| 284 |
EOF |
| 285 |
foreach my $member (@members) { |
| 286 |
my ($name, $type, $pretty_name, $rw) = @$member; |
| 287 |
my $namelen = length $pretty_name; |
| 288 |
|
| 289 |
next unless $rw; |
| 290 |
|
| 291 |
my $convcode = c_var_from_sv("data->$name", $type, "*psv_tmp"); |
| 292 |
|
| 293 |
print $outfile <<"EOF"; |
| 294 |
psv_tmp = hv_fetch(hash, "$pretty_name", $namelen, 0); |
| 295 |
$convcode |
| 296 |
EOF |
| 297 |
} |
| 298 |
|
| 299 |
print $outfile <<"EOF"; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
EOF |
| 304 |
|
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
# |
| 309 |
# Now write the actual hook handlers. |
| 310 |
# |
| 311 |
|
| 312 |
foreach my $hookname (sort keys %hooks) { |
| 313 |
my $arg_type = $hooks{$hookname}; |
| 314 |
next if grep { $_ eq $arg_type } @special_types; |
| 315 |
print $outfile <<"EOF"; |
| 316 |
static void perl_hook_$hookname ($arg_type * data) |
| 317 |
{ |
| 318 |
SV *arg; |
| 319 |
perl_hook_marshal_$arg_type(PERL_HOOK_TO_PERL, data, &arg); |
| 320 |
|
| 321 |
dSP; |
| 322 |
ENTER; |
| 323 |
SAVETMPS; |
| 324 |
PUSHMARK(SP); |
| 325 |
|
| 326 |
XPUSHs(newRV_noinc((SV*)get_cv("Atheme::Hooks::call_hooks", 0))); |
| 327 |
XPUSHs(sv_2mortal(newSVpv("$hookname", 0))); |
| 328 |
XPUSHs(arg); |
| 329 |
PUTBACK; |
| 330 |
call_pv("Atheme::Init::call_wrapper", G_EVAL | G_DISCARD); |
| 331 |
|
| 332 |
SPAGAIN; |
| 333 |
|
| 334 |
if (SvTRUE(ERRSV)) |
| 335 |
{ |
| 336 |
slog(LG_ERROR, "Calling perl hook $hookname raised unexpected error %s", SvPV_nolen(ERRSV)); |
| 337 |
} |
| 338 |
|
| 339 |
FREETMPS; |
| 340 |
LEAVE; |
| 341 |
|
| 342 |
perl_hook_marshal_$arg_type(PERL_HOOK_FROM_PERL, data, &arg); |
| 343 |
SvREFCNT_dec(arg); |
| 344 |
invalidate_object_references(); |
| 345 |
} |
| 346 |
|
| 347 |
EOF |
| 348 |
|
| 349 |
} |
| 350 |
|
| 351 |
# Now, the functions that Perl will call to enable/disable a hook handler. |
| 352 |
|
| 353 |
print $outfile <<EOF; |
| 354 |
|
| 355 |
void enable_perl_hook_handler(const char *hookname) |
| 356 |
{ |
| 357 |
EOF |
| 358 |
foreach my $hookname (sort keys %hooks) { |
| 359 |
print $outfile <<"EOF"; |
| 360 |
if (0 == strcmp(hookname, "$hookname")) { |
| 361 |
hook_add_$hookname(perl_hook_$hookname); |
| 362 |
return; |
| 363 |
} |
| 364 |
EOF |
| 365 |
} |
| 366 |
print $outfile <<EOF; |
| 367 |
|
| 368 |
dTHX; |
| 369 |
Perl_croak(aTHX_ "Attempted to enable unknown hook name %s", hookname); |
| 370 |
} |
| 371 |
|
| 372 |
void disable_perl_hook_handler(const char *hookname) |
| 373 |
{ |
| 374 |
EOF |
| 375 |
foreach my $hookname (sort keys %hooks) { |
| 376 |
print $outfile <<"EOF"; |
| 377 |
if (0 == strcmp(hookname, "$hookname")) { |
| 378 |
hook_del_$hookname(perl_hook_$hookname); |
| 379 |
return; |
| 380 |
} |
| 381 |
EOF |
| 382 |
} |
| 383 |
print $outfile <<EOF; |
| 384 |
|
| 385 |
dTHX; |
| 386 |
Perl_croak(aTHX_ "Attempted to disable unknown hook name %s", hookname); |
| 387 |
} |
| 388 |
|
| 389 |
EOF |
| 390 |
|
| 391 |
close $outfile; |