[NeoStats-Devel] [Commits] rev 2364 - trunk/modules/textserv

[email protected]
Newsgroups gmane.comp.neostats.devel
Message-ID <[email protected]>
Author: Mark
Date: Fri Mar 18 07:58:27 2005
New Revision: 2364

Modified:
   trunk/modules/textserv/help.c
   trunk/modules/textserv/main.c
   trunk/modules/textserv/textserv.h
Log:
Add initial framework for textserv

Modified: trunk/modules/textserv/help.c
==============================================================================
--- trunk/modules/textserv/help.c	(original)
+++ trunk/modules/textserv/help.c	Fri Mar 18 07:58:27 2005
@@ -29,3 +29,32 @@
 	NULL
 };
 
+const char ts_help_add_oneline[] ="Add a database";
+const char ts_help_del_oneline[] ="Delete a database";
+const char ts_help_list_oneline[] ="List databases";
+
+const char *ts_help_add[] = {
+	"Syntax: \2ADD <database> <nick> <channel>\2",
+	"",
+	"Register a database with textserv.",
+	"<database> is the name of the database to load",
+	"<nick> is the optional name of the bot you want to use. If not",
+	"specified, the database name is used as the nick",
+	"<channel> is the optional channel you want the bot to join. If not",
+	"specified, the bot will not join a channel",
+	NULL
+};
+
+const char *ts_help_del[] = {
+	"Syntax: \2DEL <database>\2",
+	"",
+	"Delete a database.",
+	NULL
+};
+
+const char *ts_help_list[] = {
+	"Syntax: \2LIST\2",
+	"",
+	"Lists loaded databases.",
+	NULL
+};

Modified: trunk/modules/textserv/main.c
==============================================================================
--- trunk/modules/textserv/main.c	(original)
+++ trunk/modules/textserv/main.c	Fri Mar 18 07:58:27 2005
@@ -24,7 +24,25 @@
 #include "neostats.h"
 #include "textserv.h"
 
+typedef struct dbentry {
+	char name[MAXNICK];
+	char nick[MAXNICK];
+	char channel[MAXCHANLEN];
+} dbentry;
+
+typedef struct dbbot {
+	dbentry database;
+	BotInfo botinfo;
+	Bot *botptr;
+}dbbot;
+
 /** Bot command function prototypes */
+static int ts_cmd_add( CmdParams *cmdparams );
+static int ts_cmd_list( CmdParams *cmdparams );
+static int ts_cmd_del( CmdParams *cmdparams );
+
+/** hash to store database and bot info */
+static hash_t *tshash;
 
 /** Bot pointer */
 static Bot *ts_bot;
@@ -53,10 +71,25 @@
 /** Bot comand table */
 static bot_cmd ts_commands[]=
 {
-	{NULL,			NULL,			0, 	0,	NULL, 				NULL}
+	{"ADD",		ts_cmd_add,		1,	NS_ULEVEL_ADMIN,	ts_help_add,	ts_help_add_oneline },
+	{"DEL",		ts_cmd_del,		1, 	NS_ULEVEL_ADMIN,	ts_help_del,	ts_help_del_oneline },
+	{"LIST",	ts_cmd_list,	0, 	NS_ULEVEL_ADMIN,	ts_help_list,	ts_help_list_oneline },
+	{NULL,		NULL,			0, 	0,					NULL, 			NULL}
+};
+
+/** Bot setting table */
+static bot_setting ts_settings[]=
+{
+	{NULL,		NULL,			0,			0, 0, 		0,				 NULL,		NULL,			NULL	},
+};
+
+/** Sub bot setting table */
+static bot_setting ts_settingstemplate[]=
+{
+	{NULL,		NULL,			0,			0, 0, 		0,				 NULL,		NULL,			NULL	},
 };
 
-/** BotInfo */
+/** TextServ BotInfo */
 static BotInfo ts_botinfo = 
 {
 	"TextServ", 
@@ -69,6 +102,34 @@
 	NULL,
 };
 
+/** @brief BuildBot
+ *
+ *  load dbentry
+ *
+ *  @param none
+ *
+ *  @return none
+ */
+
+/** @brief load_dbentry
+ *
+ *  load dbentry
+ *
+ *  @param none
+ *
+ *  @return none
+ */
+
+static int load_dbentry( void *data, int size )
+{
+	dbbot *db;
+
+	db = ns_calloc( sizeof( dbbot ) );
+	os_memcpy( &db->database, data, sizeof( dbentry ) );
+	hnode_create_insert( tshash, db, db->database.name );
+	return NS_FALSE;
+}
+
 /** @brief ModInit
  *
  *  Init handler
@@ -80,6 +141,13 @@
 
 int ModInit( void )
 {
+	tshash = hash_create( -1, 0, 0 );
+	if( !tshash ) {
+		nlog( LOG_CRITICAL, "Unable to create database hash" );
+		return -1;
+	}
+	DBAFetchRows( "databases", load_dbentry );
+	ModuleConfig( ts_settings );
 	return NS_SUCCESS;
 }
 
@@ -112,7 +180,137 @@
  *  @return NS_SUCCESS if suceeds else NS_FAILURE
  */
 
-int ModFini (void)
+int ModFini( void )
+{
+	dbbot *db;
+	hnode_t *hn;
+	hscan_t hs;
+
+	SET_SEGV_LOCATION();
+	hash_scan_begin( &hs, tshash );
+	while( ( hn = hash_scan_next( &hs ) ) != NULL ) {
+		db =( ( dbbot * )hnode_get( hn ) );
+		hash_delete( tshash, hn );
+		hnode_destroy( hn );
+		ns_free( db );
+	}
+	hash_destroy( tshash );
+	return NS_SUCCESS;
+}
+
+/** @brief ts_cmd_add
+ *
+ *  Command handler for ADD
+ *
+ *  @param cmdparams
+ *    cmdparams->av[0] = database
+ *    cmdparams->av[1] = nick
+ *    cmdparams->av[2] = optional channel
+ *
+ *  @return NS_SUCCESS if succeeds, else NS_FAILURE
+ */
+
+static int ts_cmd_add( CmdParams *cmdparams )
 {
+	dbbot *db;
+
+	SET_SEGV_LOCATION();
+	if( hash_lookup( tshash, cmdparams->av[0] ) != NULL ) {
+		irc_prefmsg( ts_bot, cmdparams->source, 
+			"%s already exists in the database list", cmdparams->av[0] );
+		return NS_SUCCESS;
+	}
+	if( cmdparams->ac > 1 && ValidateNick( cmdparams->av[1] ) != NS_SUCCESS )
+	{
+		irc_prefmsg( ts_bot, cmdparams->source, 
+			"%s is an invalid nick", cmdparams->av[1] );
+		return NS_SUCCESS;
+	}
+	if( cmdparams->ac > 2 && ValidateChannel( cmdparams->av[2] ) != NS_SUCCESS )
+	{
+		irc_prefmsg( ts_bot, cmdparams->source, 
+			"%s is an invalid channel", cmdparams->av[2] );
+		return NS_SUCCESS;
+	}
+	db = ns_calloc( sizeof( dbbot ) );
+	strlcpy( db->database.name, cmdparams->av[0], MAXNICK );
+	if( cmdparams->ac > 1 )
+		strlcpy( db->database.nick, cmdparams->av[1], MAXNICK );
+	else
+		strlcpy( db->database.nick, cmdparams->av[0], MAXNICK );
+	if( cmdparams->ac > 2 )
+		strlcpy( db->database.channel, cmdparams->av[2], MAXCHANLEN );
+	hnode_create_insert( tshash, db, db->database.name );
+	DBAStore( "databases", db->database.name,( void * )db, sizeof( dbentry ) );
+	return NS_SUCCESS;
+}
+
+/** @brief ts_cmd_list
+ *
+ *  Command handler for LIST
+ *
+ *  @param cmdparams
+ *
+ *  @return NS_SUCCESS if succeeds, else NS_FAILURE
+ */
+
+static int ts_cmd_list( CmdParams *cmdparams )
+{
+	dbbot *db;
+	hnode_t *hn;
+	hscan_t hs;
+	int i = 1;
+
+	SET_SEGV_LOCATION();
+	if( hash_count( tshash ) == 0 ) {
+		irc_prefmsg( ts_bot, cmdparams->source, "No databases are defined." );
+		return NS_SUCCESS;
+	}
+	hash_scan_begin( &hs, tshash );
+	irc_prefmsg( ts_bot, cmdparams->source, "Databases" );
+	while( ( hn = hash_scan_next( &hs ) ) != NULL ) {
+		db =( ( dbbot * )hnode_get( hn ) );
+		irc_prefmsg( ts_bot, cmdparams->source, "%d - %s %s %s", i, db->database.name, db->database.nick, db->database.channel );
+		i++;
+	}
+	irc_prefmsg( ts_bot, cmdparams->source, "End of list." );
+	return NS_SUCCESS;
+}
+
+/** @brief ts_cmd_del
+ *
+ *  Command handler for DEL
+ *    cmdparams->av[0] = database to delete
+ *
+ *  @param cmdparams
+ *
+ *  @return NS_SUCCESS if succeeds, else NS_FAILURE
+ */
+
+static int ts_cmd_del( CmdParams *cmdparams )
+{
+	dbbot *db;
+	hnode_t *hn;
+	hscan_t hs;
+
+	SET_SEGV_LOCATION();
+	hash_scan_begin( &hs, tshash );
+	while( ( hn = hash_scan_next( &hs ) ) != NULL ) {
+		db =( dbbot * )hnode_get( hn );
+		if( ircstrcasecmp( db->database.name, cmdparams->av[0] ) == 0 ) {
+			hash_scan_delete( tshash, hn );
+			irc_prefmsg( ts_bot, cmdparams->source, 
+				"Deleted %s from the database list", cmdparams->av[0] );
+			CommandReport( ts_bot, "%s deleted %s from the database list",
+				cmdparams->source->name, cmdparams->av[0] );
+			nlog( LOG_NOTICE, "%s deleted %s from the database list",
+				cmdparams->source->name, cmdparams->av[0] );
+			hnode_destroy( hn );
+			DBADelete( "databases", db->database.name );
+			ns_free( db );
+			return NS_SUCCESS;
+		}
+	}
+	irc_prefmsg( ts_bot, cmdparams->source, "No entry for %s", cmdparams->av[1] );
 	return NS_SUCCESS;
 }

Modified: trunk/modules/textserv/textserv.h
==============================================================================
--- trunk/modules/textserv/textserv.h	(original)
+++ trunk/modules/textserv/textserv.h	Fri Mar 18 07:58:27 2005
@@ -21,4 +21,12 @@
 ** $Id$
 */
 
-extern const char *ts_about[];
\ No newline at end of file
+extern const char *ts_about[];
+
+extern const char ts_help_add_oneline[];
+extern const char ts_help_del_oneline[];
+extern const char ts_help_list_oneline[];
+
+extern const char *ts_help_add[];
+extern const char *ts_help_del[];
+extern const char *ts_help_list[];
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.