[NeoStats-Devel] [Commits] r2681 - in trunk: modules/perltest src
[email protected] Sat, 23 Jul 2005 01:52:08 +1000
Newsgroups
gmane.comp.neostats.devel
Message-ID
<[email protected] >
Author: Fish
Date: Fri Jul 22 23:52:01 2005
New Revision: 2681
Modified:
trunk/modules/perltest/test.pl
trunk/src/NeoStats.pm
trunk/src/commands.c
trunk/src/neostats.pm.h
trunk/src/perl.c
Log:
Add Command handler to perl modules..
Modified: trunk/modules/perltest/test.pl
==============================================================================
--- trunk/modules/perltest/test.pl (original)
+++ trunk/modules/perltest/test.pl Fri Jul 22 23:52:01 2005
@@ -328,12 +328,26 @@
$botinfo->{ident} = "fish";
$botinfo->{host} = "Host.com";
$botinfo->{gecos} = "My Gecos";
- NeoStats::AddBot($botinfo, NeoStats::BOT_FLAG_SERVICEBOT);
+ my $bot = 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});
-
+
+
+# add a command
+ my $cmd;
+ $cmd = {
+ cmd => 'test',
+ minparams => '1',
+ ulevel => '0',
+ flags => '0',
+ };
+ NeoStats::AddCmd($bot, $cmd, 'cmd_cb_test');
+}
+
+sub cmd_cb_test {
+ NeoStats::print("Got Test Command");
}
sub shutdownbot {
Modified: trunk/src/NeoStats.pm
==============================================================================
--- trunk/src/NeoStats.pm (original)
+++ trunk/src/NeoStats.pm Fri Jul 22 23:52:01 2005
@@ -149,7 +149,7 @@
my $bot = NeoStats::Internal::AddBot( $botinfo, $botflag, $data);
if ( defined ( $bot )) {
- return NeoStats::NS_SUCCESS;
+ return $bot;
} else {
return NeoStats::NS_FAILURE;
}
@@ -194,6 +194,47 @@
return NeoStats::Internal::FindChannel($name);
}
+ sub AddCmd {
+ if (@_ < 3) {
+ NeoStats::print("Invalid Number of arguments to AddCmd");
+ return NeoStats::NS_FAILURE;
+ }
+
+ my $bot = shift;
+ my $botcmd = shift;
+ my $callback = shift;
+ my $data = shift;
+ my ($package) = caller;
+ $callback = NeoStats::Embed::fix_callback( $package, $callback );
+
+ if (!ref( $botcmd ) eq 'HASH' ) {
+ NeoStats::print("Botcmd is not a hash");
+ return NeoStats::NS_FAILURE;
+ }
+ if ((!exists( $botcmd->{cmd} )) || (!defined( $botcmd->{cmd} ))) {
+ NeoStats::print("Botinfo->{cmd} not defined");
+ return NeoStats::NS_FAILURE;
+ }
+ if ((!exists( $botcmd->{minparams} )) || (!defined( $botcmd->{minparams} ))) {
+ NeoStats::print("Botinfo->{minparams} not defined");
+ return NeoStats::NS_FAILURE;
+ }
+ if ((!exists( $botcmd->{ulevel} )) || (!defined( $botcmd->{ulevel} ))) {
+ NeoStats::print("Bot->{ulevel} not defined");
+ return NeoStats::NS_FAILURE;
+ }
+#XXX TODO
+# if ((!exists( $botcmd->{helptext} )) || (!defined( $botcmd->{helptext} ))) {
+# NeoStats::print("Botinfo->{host} not defined");
+# return NeoStats::NS_FAILURE;
+# }
+ if ((!exists( $botcmd->{flags} )) || (!defined( $botcmd->{flags} ))) {
+ NeoStats::print("Botinfo->{flags} not defined");
+ return NeoStats::NS_FAILURE;
+ }
+ my $ret = NeoStats::Internal::AddCommand( $bot, $botcmd, $callback);
+ return $ret;
+ }
sub print {
Modified: trunk/src/commands.c
==============================================================================
--- trunk/src/commands.c (original)
+++ trunk/src/commands.c Fri Jul 22 23:52:01 2005
@@ -202,7 +202,7 @@
*
* @return NS_SUCCESS if succeeds, NS_FAILURE if not
*/
-static int add_bot_cmd( hash_t *cmd_hash, bot_cmd *cmd_ptr )
+int add_bot_cmd( hash_t *cmd_hash, bot_cmd *cmd_ptr )
{
bot_cmd *hashentry;
char confcmd[32];
@@ -247,7 +247,7 @@
*
* @return NS_SUCCESS if succeeds, NS_FAILURE if not
*/
-static int del_bot_cmd( hash_t *cmd_hash, bot_cmd *cmd_ptr )
+int del_bot_cmd( hash_t *cmd_hash, bot_cmd *cmd_ptr )
{
hnode_t *cmdnode;
@@ -257,6 +257,12 @@
dlog( DEBUG3, "deleting command %s from services bot",( ( bot_cmd* )hnode_get( cmdnode ) )->cmd );
hash_delete( cmd_hash, cmdnode );
hnode_destroy( cmdnode );
+#if USE_PERL
+ if (IS_PERL_MOD(cmd_ptr->modptr)) {
+ ns_free(cmd_ptr->cmd);
+ ns_free(cmd_ptr->moddata);
+ }
+#endif
return NS_SUCCESS;
}
return NS_FAILURE;
Modified: trunk/src/neostats.pm.h
==============================================================================
--- trunk/src/neostats.pm.h (original)
+++ trunk/src/neostats.pm.h Fri Jul 22 23:52:01 2005
@@ -149,7 +149,7 @@
"\n"
"my $bot = NeoStats::Internal::AddBot( $botinfo, $botflag, $data);\n"
"if ( defined ( $bot )) {\n"
-"return NeoStats::NS_SUCCESS;\n"
+"return $bot;\n"
"} else {\n"
"return NeoStats::NS_FAILURE;\n"
"}\n"
@@ -194,6 +194,47 @@
"return NeoStats::Internal::FindChannel($name);\n"
"}\n"
"\n"
+"sub AddCmd {\n"
+"if (@_ < 3) {\n"
+"NeoStats::print(\"Invalid Number of arguments to AddCmd\");\n"
+"return NeoStats::NS_FAILURE;\n"
+"}\n"
+"\n"
+"my $bot = shift;\n"
+"my $botcmd = shift;\n"
+"my $callback = shift;\n"
+"my $data = shift;\n"
+"my ($package) = caller;\n"
+"$callback = NeoStats::Embed::fix_callback( $package, $callback );\n"
+"\n"
+"if (!ref( $botcmd ) eq 'HASH' ) {\n"
+"NeoStats::print(\"Botcmd is not a hash\");\n"
+"return NeoStats::NS_FAILURE;\n"
+"}\n"
+"if ((!exists( $botcmd->{cmd} )) || (!defined( $botcmd->{cmd} ))) {\n"
+"NeoStats::print(\"Botinfo->{cmd} not defined\");\n"
+"return NeoStats::NS_FAILURE;\n"
+"} \n"
+"if ((!exists( $botcmd->{minparams} )) || (!defined( $botcmd->{minparams} ))) {\n"
+"NeoStats::print(\"Botinfo->{minparams} not defined\");\n"
+"return NeoStats::NS_FAILURE;\n"
+"} \n"
+"if ((!exists( $botcmd->{ulevel} )) || (!defined( $botcmd->{ulevel} ))) {\n"
+"NeoStats::print(\"Bot->{ulevel} not defined\");\n"
+"return NeoStats::NS_FAILURE;\n"
+"} \n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"if ((!exists( $botcmd->{flags} )) || (!defined( $botcmd->{flags} ))) {\n"
+"NeoStats::print(\"Botinfo->{flags} not defined\");\n"
+"return NeoStats::NS_FAILURE;\n"
+"} \n"
+"my $ret = NeoStats::Internal::AddCommand( $bot, $botcmd, $callback);\n"
+"return $ret;\n"
+"}\n"
"\n"
"sub print {\n"
"\n"
Modified: trunk/src/perl.c
==============================================================================
--- trunk/src/perl.c (original)
+++ trunk/src/perl.c Fri Jul 22 23:52:01 2005
@@ -297,6 +297,13 @@
return ret;
}
+int
+perl_command_cb(CmdParams *cmdparams) {
+
+
+}
+
+
/* encode a Client Structure into a perl Hash */
HV *perl_encode_client(Client *u) {
HV *client;
@@ -541,7 +548,7 @@
XSRETURN_EMPTY;
}
bot->botinfo = bi;
- XSRETURN_UV (PTR2UV (bot));
+ XSRETURN_PV (bot->name);
}
XSRETURN_EMPTY;
}
@@ -646,7 +653,7 @@
dXSARGS;
if (items != 1) {
- nlog(LOG_WARNING, "Usage: NeoStats::Internal::FindServer(name)");
+ nlog(LOG_WARNING, "Usage: NeoStats::Internal::FindChannel(name)");
} else {
SP -= items; /* remove args from the stack */
mod = GET_CUR_MODULE();
@@ -667,6 +674,66 @@
}
}
+static
+XS (XS_NeoStats_AddCommand)
+{
+ Module *mod;
+ HV * rethash;
+ SV *ret;
+ int flags;
+ SV *value;
+ bot_cmd *bc;
+ Bot *bot;
+ char *temp[] = { "Test Command", "Test Command Two", NULL};
+
+ dXSARGS;
+ mod = GET_CUR_MODULE();
+ if (items < 3) {
+ nlog(LOG_WARNING, "Usage: NeoStats:Internal:AddCommand(bot, botcmd, callback)");
+ } else {
+ ret = ST(1);
+ if(SvTYPE(SvRV(ret))!=SVt_PVHV) {
+ dlog(DEBUG1, "XS_NeoStats_AddCommand: unsuported input %lu, must %i", SvTYPE(SvRV(ret)), SVt_PVHV);
+ XSRETURN_EMPTY;
+ }
+ rethash = (HV*)SvRV(ret);
+ dump_hash(rethash);
+ bc = ns_malloc(sizeof(bot_cmd));
+ value = *hv_fetch(rethash, "cmd", strlen("cmd"), FALSE);
+ bc->cmd = malloc(SvLEN(value)+1);
+ strlcpy((char *)bc->cmd, SvPV_nolen(value), SvLEN(value)+1);
+ value = *hv_fetch(rethash, "minparams", strlen("minparams"), FALSE);
+ bc->minparams = SvIV(value);
+ value = *hv_fetch(rethash, "ulevel", strlen("ulevel"), FALSE);
+ bc->ulevel = SvIV(value);
+#if 0
+/* XXX TODO */
+ value = *hv_fetch(rethash, "helptext", strlen("helptext"), FALSE);
+ strlcpy(bc->helptext, SvPV_nolen(value), MAXHOST);
+#endif
+ bc->helptext = temp;
+ value = *hv_fetch(rethash, "flags", strlen("flags"), FALSE);
+ bc->flags = SvIV(value);
+
+ bc->moddata = malloc(SvLEN(ST(2))+1);
+ strlcpy(bc->moddata, SvPV_nolen(ST(2)), SvLEN(ST(2))+1);
+ bc->modptr = mod;
+ bc->handler = perl_command_cb;
+printf("Adding %s to bot %s\n", bc->cmd, SvPV_nolen(ST(0)));
+ bot = FindBot(SvPV_nolen(ST(0)));
+ if (bot) {
+ if (bot->botcmds == NULL) {
+ bot->botcmds = hash_create(-1, 0, 0);
+ }
+ XSRETURN_UV(add_bot_cmd(bot->botcmds, bc));
+ } else {
+printf("Empty\n");
+ XSRETURN_EMPTY;
+ }
+ }
+ XSRETURN_UV(NS_FAILURE);
+}
+
#if 0
static
@@ -788,6 +855,7 @@
newXS ("NeoStats::Internal::FindUser", XS_NeoStats_FindUser, __FILE__);
newXS ("NeoStats::Internal::FindServer", XS_NeoStats_FindServer, __FILE__);
newXS ("NeoStats::Internal::FindChannel", XS_NeoStats_FindChannel, __FILE__);
+ newXS ("NeoStats::Internal::AddCommand", XS_NeoStats_AddCommand, __FILE__);
stash = get_hv ("NeoStats::", TRUE);
if (stash == NULL) {