[NeoStats-Devel] [Commits] r2675 - in trunk: include modules/perltest src
[email protected] Tue, 19 Jul 2005 19:32:28 +1000
Newsgroups
gmane.comp.neostats.devel
Message-ID
<[email protected] >
Author: Fish
Date: Tue Jul 19 17:32:20 2005
New Revision: 2675
Modified:
trunk/include/neostats.h
trunk/modules/perltest/test.pl
trunk/src/NeoStats.pm
trunk/src/bots.c
trunk/src/modules.c
trunk/src/neostats.pm.h
trunk/src/perl.c
Log:
Perl Modules have Bots!
Modified: trunk/include/neostats.h
==============================================================================
--- trunk/include/neostats.h (original)
+++ trunk/include/neostats.h Tue Jul 19 17:32:20 2005
@@ -1139,6 +1139,8 @@
int set_ulevel;
/* Link back to user struct associated with this bot*/
Client *u;
+ /* link back to BotInfo struct so if we have a module that needs to free it, it can be referenced */
+ BotInfo *botinfo;
/* pointer for module use */
void *moddata;
}_Bot;
Modified: trunk/modules/perltest/test.pl
==============================================================================
--- trunk/modules/perltest/test.pl (original)
+++ trunk/modules/perltest/test.pl Tue Jul 19 17:32:20 2005
@@ -8,7 +8,7 @@
# First thing that you must ensure happens (ie, first command) is to register this script
# Using the following command:
-NeoStats::register( "Test", "1.0", "Test Script 1 description" );
+NeoStats::register( "Test", "1.0", "Test Script 1 description", "setupbot", "shutdownbot");
# Events make up the core communications of NeoStats, here you register the events your
# Interested in, and the function to call. A optional third arguement ($options) allows you
@@ -315,14 +315,21 @@
sub setupbot {
my $botinfo;
+ NeoStats::print("Setup");
$botinfo->{nick} = "Fishy";
$botinfo->{altnick} = "Fishy2";
$botinfo->{ident} = "fish";
$botinfo->{host} = "Host.com";
$botinfo->{gecos} = "My Gecos";
- NeoStats::AddBot($botinfo, 0);
+ NeoStats::AddBot($botinfo, NeoStats::BOT_FLAG_SERVICEBOT);
+ $botinfo->{nick} = "fishy2";
+ NeoStats::AddBot($botinfo, NeoStats::BOT_FLAG_SERVICEBOT);
+ NeoStats::print("Added Second Bot $botinfo->{nick}");
+ NeoStats::DelBot($botinfo->{nick});
+
}
+sub shutdownbot {
+ NeoStats::Print("Shutdown");
+}
-
-setupbot();
Modified: trunk/src/NeoStats.pm
==============================================================================
--- trunk/src/NeoStats.pm (original)
+++ trunk/src/NeoStats.pm Tue Jul 19 17:32:20 2005
@@ -43,19 +43,27 @@
our @EXPORT_OK = @{$EXPORT_TAGS{all}};
sub register {
+ if (@_ != 5) {
+ NeoStats::print("Invalid Number of arguments to register");
+ return NeoStats::NS_FAILURE;
+ }
my ($package) = caller;
my $pkg_info = NeoStats::Embed::pkg_info( $package );
my $filename = $pkg_info->{filename};
- my ($name, $version, $description, $callback) = @_;
+ my ($name, $version, $description, $startupcb, $shutdowncb) = @_;
$description = "" unless defined $description;
- $pkg_info->{shutdown} = $callback;
+
$pkg_info->{name} = $name;
$pkg_info->{version} = $version;
$pkg_info->{description} = $description;
$pkg_info->{gui_entry} =
NeoStats::Internal::register( $pkg_info->{name}, $pkg_info->{version}, $pkg_info->{description});
+ $startupcb = NeoStats::Embed::fix_callback( $package, $startupcb );
+ $shutdowncb = NeoStats::Embed::fix_callback( $package, $shutdowncb );
+ $pkg_info->{shutdown} = $shutdowncb;
+ $pkg_info->{startup} = $startupcb;
# keep with old behavior
return NeoStats::NS_SUCCESS;
@@ -64,7 +72,10 @@
sub hook_event {
- return undef unless @_ >= 2;
+ if (@_ < 2) {
+ NeoStats::print("Invalid Number of arguments to hook_event");
+ return NeoStats::NS_FAILURE;
+ }
my $event = shift;
my $callback = shift;
@@ -95,7 +106,10 @@
# AddBot(botinfo, botflag)
sub AddBot {
- return undef unless @_ >= 2;
+ if (@_ < 2) {
+ NeoStats::print("Invalid Number of arguments to AddBot");
+ return NeoStats::NS_FAILURE;
+ }
my $botinfo = shift;
my $botflag = shift;
@@ -127,16 +141,25 @@
return NeoStats::NS_FAILURE;
}
- my $pkg_info = NeoStats::Embed::pkg_info( $package );
my $bot = NeoStats::Internal::AddBot( $botinfo, $botflag, $data);
if ( defined ( $bot )) {
- push @{$pkg_info->{bots}}, $bot;
return NeoStats::NS_SUCCESS;
} else {
return NeoStats::NS_FAILURE;
}
}
-
+ sub DelBot {
+ if (@_ < 1) {
+ NeoStats::print("Invalid Number of arguments to DelBot");
+ return NeoStats::NS_FAILURE;
+ }
+ my $botname = shift;
+ my $reason = shift;
+ if (!defined($reason)) {
+ $reason = "Unknown";
+ }
+ return NeoStats::Internal::DelBot($botname, $reason);
+ }
sub hook_server {
return undef unless @_ >= 2;
@@ -624,20 +647,21 @@
return NeoStats::NS_SUCCESS;
}
-# sub auto_load {
-
-# my $dir = NeoStats::get_info( "NeoStatsdirfs" ) || NeoStats::get_info( "NeoStatsdir" );
-# if( opendir my $dir_handle, $dir ) {
-# my @files = readdir $dir_handle;
-
-# for( @files ) {
-# my $fullpath = File::Spec->catfile( $dir, $_ );
-# load( $fullpath ) if $fullpath =~ m/\.pl$/i;
-# }
-# closedir $dir_handle;
-# }
-
-# }
+ sub sync {
+ my $file = shift @_;
+ my $package = file2pkg( $file );
+ my $pkg_info = pkg_info( $package );
+ if( exists $pkg_info->{startup} ) {
+ if( ref $pkg_info->{startup} eq 'CODE' ) {
+ $pkg_info->{startup}->();
+ } elsif( $pkg_info->{startup} ) {
+ eval {
+ no strict 'refs';
+ &{$pkg_info->{startup}};
+ };
+ }
+ }
+ }
}
Modified: trunk/src/bots.c
==============================================================================
--- trunk/src/bots.c (original)
+++ trunk/src/bots.c Tue Jul 19 17:32:20 2005
@@ -472,9 +472,9 @@
}
irc_prefmsg( ns_botptr, cmdparams->source, __( "Bot: %s", cmdparams->source ), botptr->name );
cm = list_first( botptr->u->user->chans );
- irc_chanalert( ns_botptr, _( "Channels:" ) );
+ irc_prefmsg( ns_botptr, cmdparams->source, __( "Channels:", cmdparams->source ) );
while( cm ) {
- irc_chanalert( ns_botptr, " %s", ( char * ) lnode_get( cm ) );
+ irc_prefmsg( ns_botptr, cmdparams->source, " %s", ( char * ) lnode_get( cm ) );
cm = list_next( botptr->u->user->chans, cm );
}
}
Modified: trunk/src/modules.c
==============================================================================
--- trunk/src/modules.c (original)
+++ trunk/src/modules.c Tue Jul 19 17:32:20 2005
@@ -100,6 +100,8 @@
SET_SEGV_LOCATION();
module_ptr->synched = 1;
#ifdef USE_PERL
+ } else {
+ perl_sync_module(module_ptr);
}
#endif
return err;
Modified: trunk/src/neostats.pm.h
==============================================================================
--- trunk/src/neostats.pm.h (original)
+++ trunk/src/neostats.pm.h Tue Jul 19 17:32:20 2005
@@ -43,19 +43,27 @@
"our @EXPORT_OK = @{$EXPORT_TAGS{all}};\n"
"\n"
"sub register {\n"
+"if (@_ != 5) {\n"
+"NeoStats::print(\"Invalid Number of arguments to register\");\n"
+"return NeoStats::NS_FAILURE;\n"
+"}\n"
"my ($package) = caller;\n"
"my $pkg_info = NeoStats::Embed::pkg_info( $package );\n"
"my $filename = $pkg_info->{filename};\n"
"\n"
-"my ($name, $version, $description, $callback) = @_;\n"
+"my ($name, $version, $description, $startupcb, $shutdowncb) = @_;\n"
"$description = \"\" unless defined $description;\n"
"\n"
-"$pkg_info->{shutdown} = $callback;\n"
+"\n"
"$pkg_info->{name} = $name;\n"
"$pkg_info->{version} = $version;\n"
"$pkg_info->{description} = $description;\n"
"$pkg_info->{gui_entry} =\n"
"NeoStats::Internal::register( $pkg_info->{name}, $pkg_info->{version}, $pkg_info->{description});\n"
+"$startupcb = NeoStats::Embed::fix_callback( $package, $startupcb );\n"
+"$shutdowncb = NeoStats::Embed::fix_callback( $package, $shutdowncb );\n"
+"$pkg_info->{shutdown} = $shutdowncb;\n"
+"$pkg_info->{startup} = $startupcb;\n"
"\n"
"\n"
"return NeoStats::NS_SUCCESS;\n"
@@ -64,7 +72,10 @@
"\n"
"\n"
"sub hook_event {\n"
-"return undef unless @_ >= 2;\n"
+"if (@_ < 2) {\n"
+"NeoStats::print(\"Invalid Number of arguments to hook_event\");\n"
+"return NeoStats::NS_FAILURE;\n"
+"}\n"
"\n"
"my $event = shift;\n"
"my $callback = shift;\n"
@@ -95,7 +106,10 @@
"\n"
"\n"
"sub AddBot {\n"
-"return undef unless @_ >= 2;\n"
+"if (@_ < 2) {\n"
+"NeoStats::print(\"Invalid Number of arguments to AddBot\");\n"
+"return NeoStats::NS_FAILURE;\n"
+"}\n"
"\n"
"my $botinfo = shift;\n"
"my $botflag = shift;\n"
@@ -127,16 +141,25 @@
"return NeoStats::NS_FAILURE;\n"
"} \n"
"\n"
-"my $pkg_info = NeoStats::Embed::pkg_info( $package );\n"
"my $bot = NeoStats::Internal::AddBot( $botinfo, $botflag, $data);\n"
"if ( defined ( $bot )) {\n"
-"push @{$pkg_info->{bots}}, $bot;\n"
"return NeoStats::NS_SUCCESS;\n"
"} else {\n"
"return NeoStats::NS_FAILURE;\n"
"}\n"
"}\n"
-"\n"
+"sub DelBot {\n"
+"if (@_ < 1) {\n"
+"NeoStats::print(\"Invalid Number of arguments to DelBot\");\n"
+"return NeoStats::NS_FAILURE;\n"
+"}\n"
+"my $botname = shift;\n"
+"my $reason = shift;\n"
+"if (!defined($reason)) {\n"
+"$reason = \"Unknown\";\n"
+"}\n"
+"return NeoStats::Internal::DelBot($botname, $reason);\n"
+"} \n"
"\n"
"sub hook_server {\n"
"return undef unless @_ >= 2;\n"
@@ -624,20 +647,21 @@
"return NeoStats::NS_SUCCESS;\n"
"}\n"
"\n"
-"\n"
-"\n"
-"\n"
-"\n"
-"\n"
-"\n"
-"\n"
-"\n"
-"\n"
-"\n"
-"\n"
-"\n"
-"\n"
-"\n"
+"sub sync {\n"
+"my $file = shift @_;\n"
+"my $package = file2pkg( $file );\n"
+"my $pkg_info = pkg_info( $package );\n"
+"if( exists $pkg_info->{startup} ) {\n"
+"if( ref $pkg_info->{startup} eq 'CODE' ) {\n"
+"$pkg_info->{startup}->();\n"
+"} elsif( $pkg_info->{startup} ) {\n"
+"eval {\n"
+"no strict 'refs';\n"
+"&{$pkg_info->{startup}};\n"
+"};\n"
+"}\n"
+"}\n"
+"} \n"
"\n"
"\n"
"}\n"
Modified: trunk/src/perl.c
==============================================================================
--- trunk/src/perl.c (original)
+++ trunk/src/perl.c Tue Jul 19 17:32:20 2005
@@ -115,6 +115,14 @@
}
int
+perl_sync_module(Module *mod) {
+ mod->insynch = 1;
+ execute_perl (mod, sv_2mortal (newSVpv ("NeoStats::Embed::sync", 0)),1, mod->pm->filename);
+ mod->synched = 1;
+ return NS_SUCCESS;
+}
+
+int
perl_event_cb(Event evt, CmdParams *cmdparams, Module *mod_ptr) {
int ret = NS_FAILURE;
switch (evt) {
@@ -604,6 +612,8 @@
SV *ret;
int flags;
SV *value;
+ BotInfo *bi;
+ Bot *bot;
dXSARGS;
mod = GET_CUR_MODULE();
@@ -617,13 +627,27 @@
XSRETURN_EMPTY;
}
rethash = (HV*)SvRV(ret);
+ dump_hash(rethash);
+ bi = ns_malloc(sizeof(BotInfo));
value = *hv_fetch(rethash, "nick", strlen("nick"), FALSE);
- printf("Nick: %s\n", SvPV_nolen(value));
+ strncpy(bi->nick, SvPV_nolen(value), MAXNICK);
value = *hv_fetch(rethash, "altnick", strlen("altnick"), FALSE);
- printf("AltNick: %s\n", SvPV_nolen(value));
- dump_hash(rethash);
-
-
+ strncpy(bi->altnick, SvPV_nolen(value), MAXNICK);
+ value = *hv_fetch(rethash, "ident", strlen("ident"), FALSE);
+ strncpy(bi->user, SvPV_nolen(value), MAXUSER);
+ value = *hv_fetch(rethash, "host", strlen("host"), FALSE);
+ strncpy(bi->host, SvPV_nolen(value), MAXHOST);
+ value = *hv_fetch(rethash, "gecos", strlen("gecos"), FALSE);
+ strncpy(bi->realname, SvPV_nolen(value), MAXREALNAME);
+ bi->flags = (int) SvIV (ST (1));
+ bi->bot_cmd_list = NULL;
+ bi->bot_setting_list = NULL;
+ if ((bot = AddBot(bi)) == NULL) {
+ free(bi);
+ XSRETURN_EMPTY;
+ }
+ bot->botinfo = bi;
+ XSRETURN_UV (PTR2UV (bot));
}
exit(-1);
XSRETURN_EMPTY;
@@ -646,6 +670,29 @@
}
+static
+XS (XS_NeoStats_DelBot)
+{
+ Module *mod;
+ dXSARGS;
+ Bot *bot;
+
+
+ if (items != 2) {
+ nlog(LOG_WARNING, "Usage: NeoStats::Internal::DelBot(botname, quitreason)");
+ } else {
+ mod = GET_CUR_MODULE();
+ if (!mod) {
+ nlog(LOG_WARNING, "Current Mod Stack for Perl Mods is screwed");
+ XSRETURN_EMPTY;
+ }
+ bot = FindBot(SvPV_nolen(ST(0)));
+ ns_free(bot->botinfo);
+ irc_quit(bot, SvPV_nolen(ST(1)));
+ XSRETURN_UV( NS_SUCCESS);
+ }
+}
+
@@ -968,6 +1015,7 @@
newXS ("NeoStats::Internal::hook_event", XS_NeoStats_hook_event, __FILE__);
newXS ("NeoStats::Internal::unhook_event", XS_NeoStats_unhook_event, __FILE__);
newXS ("NeoStats::Internal::AddBot", XS_NeoStats_AddBot, __FILE__);
+ newXS ("NeoStats::Internal::DelBot", XS_NeoStats_DelBot, __FILE__);
stash = get_hv ("NeoStats::", TRUE);
if (stash == NULL) {
exit (1);
@@ -1022,6 +1070,22 @@
newCONSTSUB (stash, "EVENT_DCCCHATMSG", newSViv (EVENT_DCCCHATMSG));
newCONSTSUB (stash, "EVENT_ADDBAN", newSViv (EVENT_ADDBAN));
newCONSTSUB (stash, "EVENT_DELBAN", newSViv (EVENT_DELBAN));
+
+ newCONSTSUB (stash, "EVENT_FLAG_DISABLED", newSViv (EVENT_FLAG_DISABLED));
+ newCONSTSUB (stash, "EVENT_FLAG_IGNORE_SYNCH", newSViv (EVENT_FLAG_IGNORE_SYNCH));
+ newCONSTSUB (stash, "EVENT_FLAG_USE_EXCLUDE", newSViv (EVENT_FLAG_USE_EXCLUDE));
+ newCONSTSUB (stash, "EVENT_FLAG_EXCLUDE_ME", newSViv (EVENT_FLAG_EXCLUDE_ME));
+ newCONSTSUB (stash, "EVENT_FLAG_EXCLUDE_MODME", newSViv (EVENT_FLAG_EXCLUDE_MODME));
+
+
+ newCONSTSUB (stash, "BOT_FLAG_ONLY_OPERS", newSViv (BOT_FLAG_ONLY_OPERS));
+ newCONSTSUB (stash, "BOT_FLAG_RESTRICT_OPERS", newSViv (BOT_FLAG_RESTRICT_OPERS));
+ newCONSTSUB (stash, "BOT_FLAG_DEAF", newSViv (BOT_FLAG_DEAF));
+ newCONSTSUB (stash, "BOT_FLAG_SERVICEBOT", newSViv (BOT_FLAG_SERVICEBOT));
+ newCONSTSUB (stash, "BOT_FLAG_PERSIST", newSViv (BOT_FLAG_PERSIST));
+ newCONSTSUB (stash, "BOT_FLAG_NOINTRINSICLEVELS", newSViv (BOT_FLAG_NOINTRINSICLEVELS));
+ newCONSTSUB (stash, "BOT_COMMON_HOST", newSVpv (BOT_COMMON_HOST, strlen(BOT_COMMON_HOST)));
+
newCONSTSUB (stash, "NS_SUCCESS", newSViv (NS_SUCCESS));
@@ -1086,13 +1150,9 @@
perl_definition array.
*/
eval_pv (perl_definitions, TRUE);
- mod->insynch = 1;
+ mod->insynch = 0;
if (!execute_perl (mod, sv_2mortal (newSVpv ("NeoStats::Embed::load", 0)),
1, (char *)filename)) {
-#if 0
- if (!execute_perl (mod, sv_2mortal (newSVpv ("pkg_load", 0)),
- 3, (char *)filename, (char *)filename, (char *)filename)) {
-#endif
/* XXX if we are here, check that pm->mod->info has something, otherwise the script didnt register */
if (!mod->info->name[0]) {
load_module_error(u, __("Perl Module %s didn't register. Unloading", u), filename);
@@ -1101,7 +1161,6 @@
return NULL;
}
/* it loaded ok */
- mod->synched = 1;
} else {
load_module_error(u, __("Errors in Perl Module %s", u), filename);
unload_perlmod(mod);
@@ -1109,7 +1168,9 @@
return NULL;
}
assign_mod_number(mod);
-
+ SET_RUN_LEVEL(mod);
+ DBAOpenDatabase();
+ RESET_RUN_LEVEL();
insert_module(mod);
cmd = ns_calloc (sizeof(CmdParams));