[NeoStats-Devel] [Commits] r2625 - in trunk: . src

[email protected]
Newsgroups gmane.comp.neostats.devel
Message-ID <[email protected]>
Author: DNB
Date: Fri May 27 09:02:53 2005
New Revision: 2625

Modified:
   trunk/ChangeLog
   trunk/src/bots.c
   trunk/src/exclude.c
Log:
check if exclusions are already included in list before adding
attempt to find bot unused nickname when both nick and altnick in use


Modified: trunk/ChangeLog
==============================================================================
--- trunk/ChangeLog	(original)
+++ trunk/ChangeLog	Fri May 27 09:02:53 2005
@@ -4,6 +4,8 @@
 Fish (F), Mark (M), DeadNotBuried (D)
 ===============================================================================
 * NeoStats * Version 3.0.a3-dev
+ - check if exclusions are already included in list before adding (D)
+ - attempt to find bot unused nickname when both nick and altnick in use (D)
  - QuoteServ: add option to use global excludes (D)
  - QuoteServ: check signon quote setting before sending signon quote (D)
  - QuoteServ: fix crash when quotes requested but no databases defined (D)

Modified: trunk/src/bots.c
==============================================================================
--- trunk/src/bots.c	(original)
+++ trunk/src/bots.c	Fri May 27 09:02:53 2005
@@ -539,6 +539,43 @@
 	return botptr;
 }
 
+/** @brief FindBotNick
+ *
+ *  find a new nick based on the passed nick
+ *  Bot subsystem use only.
+ *
+ *  @param botinfo pointer to bot description
+ *  @param pointer to nick buffer
+ *
+ *  @return nick or NULL if failed
+ */
+
+static char *FindBotNick( char *nickbuf )
+{
+	int tstnicklen;
+
+	/* find free nick from Bot nick */
+	/* if room, add random number between 0 and 9 */
+	tstnicklen = strlen( nickbuf );
+	if( ( tstnicklen + 1 ) < MAXNICK )
+	{
+		nickbuf[tstnicklen] = ( ( rand() % 10 ) + 48 );
+		nickbuf[tstnicklen + 1] = '\0';
+	}
+	/* if room, add random letter */
+	tstnicklen = strlen( nickbuf );
+	if( ( tstnicklen + 1 ) < MAXNICK )
+	{
+		nickbuf[tstnicklen] = ( ( rand() % 26 ) + 97 );
+		nickbuf[tstnicklen + 1] = '\0';
+	}
+	if( FindUser( nickbuf ) ) {
+		nlog( LOG_WARNING, "Bot test nick %s already in use", nickbuf );
+		return NULL;
+	}
+	return nickbuf;
+}
+
 /** @brief GetBotNick
  *
  *  check the requested nick
@@ -552,26 +589,69 @@
 
 static char *GetBotNick( BotInfo *botinfo, char *nickbuf )
 {
+	int checkcycle;
 	char* nick;
+	char *tstnick;
 
-	/* Check primary nick */
-	nick = botinfo->nick;
-	if( FindUser( nick ) ) {
-		nlog( LOG_WARNING, "Bot nick %s already in use", nick );
-		/* Check alternate nick */
-		if( botinfo->altnick ) {
-			nick = botinfo->altnick;
-			if( FindUser( nick ) ) {
-				nlog( LOG_WARNING, "Bot alt nick %s already in use", nick );
-				/* TODO: try and find a free nick */
+	tstnick = ns_calloc(MAXNICK);
+	for ( checkcycle = 0 ; checkcycle < 13 ; checkcycle++ )
+	{
+		switch (checkcycle)
+		{
+			case 0:
+				/* Check primary nick */
+				nick = botinfo->nick;
+				if( FindUser( nick ) ) {
+					nlog( LOG_WARNING, "Bot nick %s already in use", nick );
+				} else {
+					checkcycle = 13;
+				}
+				break;
+			case 1:
+				/* Check alternate nick */
+				if( botinfo->altnick ) {
+					nick = botinfo->altnick;
+					if( FindUser( nick ) ) {
+						nlog( LOG_WARNING, "Bot alt nick %s already in use", nick );
+					} else {
+						checkcycle = 13;
+					}
+				}
+				break;
+			case 2:
+			case 3:
+			case 4:
+			case 5:
+			case 6:
+				strlcpy(tstnick, botinfo->nick, MAXNICK);
+				nick = FindBotNick( tstnick );
+				if( nick != NULL ) {
+					checkcycle = 13;
+					break;
+				}
+				break;
+			case 7:
+			case 8:
+			case 9:
+			case 10:
+			case 11:
+				if( botinfo->altnick ) {
+					strlcpy(tstnick, botinfo->altnick, MAXNICK);
+					nick = FindBotNick( tstnick );
+					if( nick != NULL ) {
+						checkcycle = 13;
+						break;
+					}
+				}
+				break;
+			default:
+				/* failed to find free nickname on 12 attempts */
+				ns_free(tstnick);
 				return NULL;
-			}
-		} else {
-			/* TODO: try and find a free nick */
-			return NULL;
 		}
 	} 
 	strlcpy( nickbuf, nick, MAXNICK );
+	ns_free(tstnick);
 	return nickbuf;
 }
 

Modified: trunk/src/exclude.c
==============================================================================
--- trunk/src/exclude.c	(original)
+++ trunk/src/exclude.c	Fri May 27 09:02:53 2005
@@ -25,7 +25,6 @@
  */
 
 /*  TODO:
- *  - Add check for existing exclusions during add
  *  - Real time exclusions??? possibly optional.
  */
 
@@ -126,7 +125,8 @@
 {
 	NS_EXCLUDE type;
 	char *buf;
-	Exclude *e;
+	Exclude *e, *etst;
+	lnode_t *ln;
 	
 	if (cmdparams->ac < 4) {
 		return NS_ERR_NEED_MORE_PARAMS;
@@ -135,8 +135,6 @@
 		irc_prefmsg (cmdparams->bot, cmdparams->source, "Error, Exception list is full");
 		return NS_SUCCESS;
 	}
-	/* we dont do any checking to see if a similar entry already exists... oh well, thats upto the user */
-
 	if (!ircstrcasecmp("HOST", cmdparams->av[1])) {
 		if (!index(cmdparams->av[2], '.')) {
 			irc_prefmsg (cmdparams->bot, cmdparams->source, "Invalid host name");
@@ -165,6 +163,18 @@
 		irc_prefmsg (cmdparams->bot, cmdparams->source, "Invalid exclude type");
 		return NS_SUCCESS;
 	}
+	ln = list_first(elist);
+	while( ln != NULL  ) 
+	{
+		etst = lnode_get(ln);
+		if( etst->type == type ) {
+			if( match( etst->pattern, cmdparams->av[2] ) ) {
+				irc_prefmsg (cmdparams->bot, cmdparams->source, "Mask already matched by %s", etst->pattern);
+				return NS_SUCCESS;
+			}
+		}
+		ln = list_next(elist, ln);
+	}
 	e = ns_calloc (sizeof(Exclude));
 	e->type = type;
 	e->addedon = me.now;
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.