1
0
Эх сурвалжийг харах

* Rewrote the S_RANDSERVERS code.

svn: 413
Bryan Drewery 22 жил өмнө
parent
commit
eebfa65ae7

+ 56 - 0
src/misc.c

@@ -2117,3 +2117,59 @@ Context;
   if (helpstr[0]) dumplots(idx, "", helpstr);
 }
 
+/* Arrange the N elements of ARRAY in random order. */
+static void shuffleArray(char *array[], int n)
+{
+  int i;
+  for (i = 0; i < n; i++) {
+    int j = i + random() / (RAND_MAX / (n - i) + 1);
+    char *t = array[j];
+    array[j] = array[i];
+    array[i] = t;
+  }
+}
+
+static void str2array(const char *string, char *array[], char *delim, int *len)
+{
+  char *p, *work = nmalloc(strlen(string) + 1);
+
+  strcpy(work, string);
+  if ((p = strtok(work, delim))) {
+    array[(*len)] = nmalloc(strlen(p) + 1);
+    strcpy(array[(*len)], p);
+    (*len)++;
+    while (p && *p) {
+      if ((p = strtok(NULL, delim))) {
+        array[(*len)] = nmalloc(strlen(p) + 1);
+        strcpy(array[(*len)], p);
+        (*len)++;
+      } else
+        array[(*len)] = 0;
+    }
+  }
+  nfree(work);
+}
+
+static void array2str(char **array, char *string, char *delim, int len)
+{
+  int i = 0;
+  string[0] = 0;
+  for (i = 0; i < len; i++) {
+    strcat(string, array[i]);
+    strcat(string, delim);
+    nfree(array[i]);
+  }
+  string[strlen(string) - 1] = 0;               /* remove trailing comma */
+}
+
+void shuffle(char *string, char *delim)
+{
+  char *array[501];
+  int len = 0;
+//printf("SHUFFLING: %s\n", string);
+  str2array(string, array, delim, &len);
+  shuffleArray(array, len);
+  array2str(array, string, delim, len);
+//printf("NOW: %s\n", string);
+}
+

+ 1 - 1
src/mod/module.h

@@ -137,7 +137,7 @@
 #define botnet_send_chat ((void(*)(int,char*,char*))global[28])
 #define server_lag (*(int *)global[29])
 #define remove_crlf ((void (*)(char **))global[30])
-/* UNUSED 31 */
+#define shuffle ((void (*)(char *, char *))global[31])
 /* 32 - 35 */
 #define botnet_send_join_idx ((void(*)(int,int))global[32])
 #define botnet_send_part_idx ((void(*)(int,char *))global[33])

+ 6 - 86
src/mod/server.mod/server.c

@@ -1004,126 +1004,46 @@ void servers_describe(struct cfg_entry * entry, int idx) {
 void servers6_describe(struct cfg_entry * entry, int idx) {
 }
 
-int count(const char *s, const char *delim)
-{
-  char *ele;
-  int i = 0;
-  char work[2000];
-  
-  strncpyz(work, s, sizeof work);
-  ele = strtok(work, delim);
-  while(ele && *ele)
-  {
-    i++;
-    ele = strtok((char*) NULL, delim);
-  }
-  return (i - 1);
-}
-
-int rrand(int a, int b)
-{
-  b++;
-  return ((random()%(b - a))+a);
-}
-
-char *randomize(char *line, char **new)
-{
-  char *str, *words[2000], *bak;
-  int i, o, b, r, u = 0;
-  bak = nmalloc(strlen(line) + 1);
-  strcpy(bak, line);
-  i = count(line, ",");
-
-  b = i + 1;
-  str = strtok(line, ",");
-  o = 0;
-  while(str && *str)
-  {
-    words[o] = str;
-    o++;
-    str=strtok((char*)NULL, ",");
-  }
-
-  while (b) {
-
-    if (u > 200) { //some problem, bail out
-      sprintf(*new, "%s", bak);
-      nfree(bak);
-      return *new;
-    }
-
-    r = rrand(0,i);
-
-    if (strstr(*new,words[r]) == NULL) {
-      if (b == i + 1) sprintf(*new,"%s",words[r]);
-      else sprintf(*new,"%s,%s", *new, words[r]);
-      b--;
-    } else 
-      u++;
-
-  }
-  nfree(bak);
-  return *new;
-}
-
 void servers_changed(struct cfg_entry * entry, char * olddata, int * valid) {
 #ifdef LEAF
   char *slist, *p;
-#ifdef S_RANDSERVERS
-  char *new;
 
   if (hostname6[0] || myip6[0]) //we want to use the servers6 entry.
     return;
-#endif /* S_RANDSERVERS */
 
   slist = (char *) (entry->ldata ? entry->ldata : (entry->gdata ? entry->gdata : ""));
   if (serverlist) {
     clearq(serverlist);
     serverlist = NULL;
   }
-  p=nmalloc(strlen(slist)+1);
-  strcpy(p, slist);
+  p = nmalloc(strlen(slist) + 1);
 #ifdef S_RANDSERVERS
-  new = nmalloc(strlen(slist)+1);
-  randomize(p,&new);
-  strcpy(p, new);
- 
+  shuffle(slist, ",");
 #endif /* S_RANDSERVERS */
+  strcpy(p, slist);
   add_server(p);
   nfree(p);
-#ifdef S_RANDSERVERS
-  nfree(new);
-#endif /* S_RANDSERVERS */
 #endif /* LEAF */
 }
 
 void servers6_changed(struct cfg_entry * entry, char * olddata, int * valid) {
 #ifdef LEAF
   char *slist, *p;
-#ifdef S_RANDSERVERS
-  char *new;
 
   if (!hostname6[0] && !myip6[0]) //we probably want to use the normal server list..
     return;
-#endif /* S_RANDSERVERS */
   slist = (char *) (entry->ldata ? entry->ldata : (entry->gdata ? entry->gdata : ""));
   if (serverlist) {
     clearq(serverlist);
     serverlist = NULL;
   }
-  p=nmalloc(strlen(slist)+1);
-  strcpy(p, slist);
+  p = nmalloc(strlen(slist) + 1);
 #ifdef S_RANDSERVERS
-  new = nmalloc(strlen(slist)+1);
-  randomize(p,&new);
-  strcpy(p, new);
- 
+  shuffle(slist, ",");
 #endif /* S_RANDSERVERS */
+  strcpy(p, slist);
   add_server(p);
   nfree(p);
-#ifdef S_RANDSERVERS
-  nfree(new);
-#endif /* S_RANDSERVERS */
 #endif /* LEAF */
 }
 

+ 1 - 1
src/modules.c

@@ -193,7 +193,7 @@ Function global_table[] =
   (Function) botnet_send_chat,
   (Function) & server_lag, /* int					*/
   (Function) remove_crlf,
-  (Function) 0,
+  (Function) shuffle,
   /* 32 - 35 */
   (Function) botnet_send_join_idx,
   (Function) botnet_send_part_idx,

+ 1 - 0
src/proto.h

@@ -229,6 +229,7 @@ void set_cmd_pass(char *, int);
 #endif /* S_DCCPASS */
 
 /* misc.c */
+void shuffle(char *, char *);
 void showhelp(int, struct flag_record *, char *);
 char *btoh(const unsigned char *, int);
 void local_check_should_lock();