[NeoStats-Devel] [Commits] r2751 - in trunk: . modules/dccpartyline modules/quoteserv

[email protected] Fri, 19 Aug 2005 07:41:07 +1000
Newsgroups gmane.comp.neostats.devel
Message-ID <[email protected]>
Author: Mark
Date: Fri Aug 19 05:41:04 2005
New Revision: 2751

Modified:
   trunk/ChangeLog
   trunk/modules/dccpartyline/main.c
   trunk/modules/quoteserv/main.c
Log:
quoteserv - add sanity checks to db loads

Modified: trunk/ChangeLog
==============================================================================
--- trunk/ChangeLog	(original)
+++ trunk/ChangeLog	Fri Aug 19 05:41:04 2005
@@ -4,6 +4,7 @@
 Fish (F), Mark (M), DeadNotBuried (D)
 ===============================================================================
 * NeoStats * Version 3.0.a3-dev
+ - QuoteServ: add sanity checks to db loads. (M)
  - QuoteServ: fix win32 crash - strdup cannot be used in modules. (M)
  - Add strcasestr support function since not all systems support it. (M)
  - New macros NS_CMD_END() and NS_SETTING_END() for terminating command and set

Modified: trunk/modules/dccpartyline/main.c
==============================================================================
--- trunk/modules/dccpartyline/main.c	(original)
+++ trunk/modules/dccpartyline/main.c	Fri Aug 19 05:41:04 2005
@@ -59,7 +59,7 @@
 	0,
 };
 
-static int dccpartyline_event_dccchatmsg( CmdParams *cmdparams );
+static int dccpartyline_event_dccchatmsg( const CmdParams *cmdparams );
 
 /** Module Events */
 ModuleEvent module_events[] = 
@@ -110,7 +110,7 @@
 	return NS_SUCCESS;
 }
 
-static int dccpartyline_event_dccchatmsg( CmdParams *cmdparams )
+static int dccpartyline_event_dccchatmsg( const CmdParams *cmdparams )
 {
 	static char buf[BUFSIZE];
  

Modified: trunk/modules/quoteserv/main.c
==============================================================================
--- trunk/modules/quoteserv/main.c	(original)
+++ trunk/modules/quoteserv/main.c	Fri Aug 19 05:41:04 2005
@@ -22,7 +22,7 @@
 */
 
 /*  TODO:
- *  - Database sanity checking
+ *  - Nothing at present
  */
 
 #include "neostats.h"
@@ -172,9 +172,36 @@
 		strip(ptr);
 	}	
 	os_fclose( fp );
+	if( db->stringcount < 1)
+		return NS_FAILURE;
 	return NS_SUCCESS;
 }
 
+
+/** @brief qs_free_database
+ *
+ *  Free a database file
+ *
+ *  @param none
+ *
+ *  @return NS_SUCCESS if succeeds, else NS_FAILURE
+ */
+
+static int qs_free_database( database *db )
+{
+	int i;
+
+	ns_free( db->prefixstring );
+	ns_free( db->suffixstring );
+	for (i = 0; i < db->stringcount; i++) {
+		ns_free( db->stringlist[i] );
+	}
+	ns_free( db->stringlist );
+	ns_free( db );
+	return NS_SUCCESS;
+}
+
+
 /** @brief load_database
  *
  *  load database
@@ -190,8 +217,10 @@
 
 	db = ns_calloc( sizeof( database ) );
 	os_memcpy( &db->name, data, MAXNICK );
-	hnode_create_insert( qshash, db, db->name );
-	qs_read_database( db );
+	if( qs_read_database( db ) == NS_FAILURE )
+		qs_free_database( db );
+	else
+		hnode_create_insert( qshash, db, db->name );
 	return NS_FALSE;
 }
 
@@ -250,7 +279,6 @@
 	database *db;
 	hnode_t *hn;
 	hscan_t hs;
-	int i;
 
 	SET_SEGV_LOCATION();
 	hash_scan_begin( &hs, qshash );
@@ -258,13 +286,7 @@
 		db = ( ( database * )hnode_get( hn ) );
 		hash_delete( qshash, hn );
 		hnode_destroy( hn );
-		ns_free( db->prefixstring );
-		ns_free( db->suffixstring );
-		for (i = 0; i < db->stringcount; i++) {
-			ns_free( db->stringlist[i] );
-		}
-		ns_free( db->stringlist );
-		ns_free( db );
+		qs_free_database( db );
 	}
 	hash_destroy( qshash );
 	return NS_SUCCESS;
@@ -304,8 +326,13 @@
 	os_fclose( fp );
 	db = ns_calloc( sizeof( database ) );
 	strlcpy( db->name, cmdparams->av[0], MAXNICK );
+	if( qs_read_database( db ) == NS_FAILURE )
+	{
+		qs_free_database( db );
+		irc_prefmsg( qs_bot, cmdparams->source, "%s has no records, not added.", cmdparams->av[0] );
+		return NS_SUCCESS;
+	}
 	hnode_create_insert( qshash, db, db->name );
-	qs_read_database( db );
 	DBAStore( "databases", db->name,( void * )db->name, MAXNICK );
 	return NS_SUCCESS;
 }
@@ -429,10 +456,6 @@
 			irc_prefmsg( qs_bot, target, "not available" );
 		return NS_SUCCESS;
 	}
-	/* return if no records in selected database */
-	/* TODO: This should be checked at DB load time, not during command execution! */
-	if( db->stringcount < 1 )
-		return NS_FAILURE;
 	randno = hrand( db->stringcount, 1 );	
 	if( db->prefixstring )
 		flag |= 1 << 0;