Переглянути джерело

* DRY set_fish_key(target, key). empty to remove key.

ducch 14 роки тому
батько
коміт
acb581c409
3 змінених файлів з 33 додано та 21 видалено
  1. 27 0
      src/chanprog.c
  2. 1 0
      src/chanprog.h
  3. 5 21
      src/mod/server.mod/cmdsserv.c

+ 27 - 0
src/chanprog.c

@@ -932,3 +932,30 @@ void keyx(const bd::String &target) {
   fishData->timestamp = now;
   fishData->timestamp = now;
   FishKeys[target] = fishData;
   FishKeys[target] = fishData;
 }
 }
+
+void set_fish_key(char *target, char *key)
+{
+  fish_data_t* fishData = NULL;
+  fishData = FishKeys[target];
+
+  if (!key || !key[0]) { //remove key
+    FishKeys.remove(target);
+    delete fishData;
+  } else { //set key
+    fishData = new fish_data_t;
+
+    if (!strcmp(key, "rand")) {
+      // Set a RANDOM key
+      char *rand_key = (char*)my_calloc(1, 32+1);
+      make_rand_str(rand_key, 32);
+      fishData->sharedKey = rand_key;
+      free(rand_key);
+    } else {
+      fishData->sharedKey = key;
+    }
+
+    // Set the key
+    fishData->timestamp = now;
+    FishKeys[target] = fishData;
+  }
+}

+ 1 - 0
src/chanprog.h

@@ -34,6 +34,7 @@ void setup_HQ(int);
 void privmsg(bd::String target, bd::String msg, int idx);
 void privmsg(bd::String target, bd::String msg, int idx);
 void notice(bd::String target, bd::String msg, int idx);
 void notice(bd::String target, bd::String msg, int idx);
 void keyx(const bd::String& target);
 void keyx(const bd::String& target);
+void set_fish_key(char *, char *);
 
 
 extern struct chanset_t		*chanset, *chanset_default;
 extern struct chanset_t		*chanset, *chanset_default;
 extern char			admin[], origbotnick[HANDLEN + 1], origbotname[NICKLEN], jupenick[NICKLEN], botname[NICKLEN], *def_chanset;
 extern char			admin[], origbotnick[HANDLEN + 1], origbotname[NICKLEN], jupenick[NICKLEN], botname[NICKLEN], *def_chanset;

+ 5 - 21
src/mod/server.mod/cmdsserv.c

@@ -132,43 +132,27 @@ static void cmd_setkey(int idx, char *par) {
   putlog(LOG_CMDS, "*", "#%s# setkey %s", dcc[idx].nick, par);
   putlog(LOG_CMDS, "*", "#%s# setkey %s", dcc[idx].nick, par);
 
 
   if (!par[0]) {
   if (!par[0]) {
-    dprintf(idx, "Usage: setkey <nick|channel> [key|rand]\n");
+    dprintf(idx, "Usage: setkey <nick> [key|rand]\n");
     return;
     return;
   }
   }
 
 
   char *target = newsplit(&par);
   char *target = newsplit(&par);
   char *newkey = newsplit(&par);
   char *newkey = newsplit(&par);
   bool have_key = FishKeys.contains(target);
   bool have_key = FishKeys.contains(target);
-  fish_data_t* fishData = NULL;
 
 
   if (!newkey[0]) {
   if (!newkey[0]) {
     // Clear the key
     // Clear the key
     if (have_key) {
     if (have_key) {
-      fishData = FishKeys[target];
-      FishKeys.remove(target);
-      delete fishData;
+      set_fish_key(target, "");
       dprintf(idx, "Key cleared for '%s'\n", target);
       dprintf(idx, "Key cleared for '%s'\n", target);
     } else {
     } else {
       dprintf(idx, "No key found for '%s'\n", target);
       dprintf(idx, "No key found for '%s'\n", target);
     }
     }
   } else {
   } else {
     // Set a new key
     // Set a new key
-    fishData = new fish_data_t;
-
-    if (!strcmp(newkey, "rand")) {
-      // Set a RANDOM key
-      rand_key = (char*)my_calloc(1, 32+1);
-      make_rand_str(rand_key, 32);
-      fishData->sharedKey = rand_key;
-      free(rand_key);
-    } else {
-      fishData->sharedKey = newkey;
-    }
-
-    // Set the key
-    fishData->timestamp = now;
-    FishKeys[target] = fishData;
-    dprintf(idx, "Set key for '%s' to: %s", target, fishData->sharedKey);
+    set_fish_key(target, newkey);
+    fish_data_t* fishData = FishKeys[target];
+    dprintf(idx, "Set key for '%s' to: %s", target, fishData->sharedKey.c_str());
   }
   }
   return;
   return;
 }
 }