added cmd_rename
[NeonServV5.git] / src / cmd_neonserv_rename.c
diff --git a/src/cmd_neonserv_rename.c b/src/cmd_neonserv_rename.c
new file mode 100644 (file)
index 0000000..cf06571
--- /dev/null
@@ -0,0 +1,68 @@
+/* cmd_neonserv_rename.c - NeonServ v5.0
+ * Copyright (C) 2011  Philipp Kreil (pk910)
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
+
+#include "cmd_neonserv.h"
+
+/*
+* argv[0]   old auth
+* argv[1]   new auth
+*/
+
+struct neonserv_cmd_rename_cache {
+    struct ClientSocket *client, *textclient;
+    struct UserNode *user;
+    struct Event *event;
+    char *oldauth, *newauth;
+};
+
+static AUTHLOOKUP_CALLBACK(neonserv_cmd_rename_auth_lookup);
+static void neonserv_cmd_rename_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct Event *event, char *oldauth, char *newauth);
+
+CMD_BIND(neonserv_cmd_rename) {
+    struct neonserv_cmd_rename_cache *cache = malloc(sizeof(*cache));
+    if (!cache) {
+        perror("malloc() failed");
+        return;
+    }
+    cache->client = client;
+    cache->textclient = getTextBot();
+    cache->user = user;
+    cache->event = event;
+    cache->oldauth = strdup(argv[0]);
+    cache->newauth = strdup(argv[1]);
+    lookup_authname(argv[0], neonserv_cmd_rename_auth_lookup, cache);
+}
+
+static AUTHLOOKUP_CALLBACK(neonserv_cmd_rename_auth_lookup) {
+    struct neonserv_cmd_rename_cache *cache = data;
+    if(!exists) {
+        //AUTH_DOES_NOT_EXIST
+        reply(cache->textclient, cache->user, "NS_AUTH_UNKNOWN", cache->newauth);
+    } else
+        neonserv_cmd_rename_async1(cache->client, cache->textclient, cache->user, cache->event, cache->oldauth, auth);
+    free(cache->oldauth);
+    free(cache->newauth);
+    free(cache);
+}
+
+static void neonserv_cmd_rename_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct Event *event, char *oldauth, char *newauth) {
+    if(renameAccount(oldauth, newauth)) {
+        reply(textclient, user, "NS_RENAME_DONE", oldauth, newauth);
+    } else {
+        reply(textclient, user, "NS_RENAME_FAIL", oldauth);
+    }
+}