*** VERSION 5.2.0 ***
[NeonServV5.git] / src / cmd_neonserv_rename.c
1 /* cmd_neonserv_rename.c - NeonServ v5.2
2  * Copyright (C) 2011  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "cmd_neonserv.h"
19
20 /*
21 * argv[0]   old auth
22 * argv[1]   new auth
23 */
24
25 struct neonserv_cmd_rename_cache {
26     struct ClientSocket *client, *textclient;
27     struct UserNode *user;
28     struct Event *event;
29     char *oldauth, *newauth;
30 };
31
32 static AUTHLOOKUP_CALLBACK(neonserv_cmd_rename_auth_lookup);
33 static void neonserv_cmd_rename_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct Event *event, char *oldauth, char *newauth);
34
35 CMD_BIND(neonserv_cmd_rename) {
36     struct neonserv_cmd_rename_cache *cache = malloc(sizeof(*cache));
37     if (!cache) {
38         perror("malloc() failed");
39         return;
40     }
41     cache->client = client;
42     cache->textclient = getTextBot();
43     cache->user = user;
44     cache->event = event;
45     cache->oldauth = strdup(argv[0]);
46     cache->newauth = strdup(argv[1]);
47     lookup_authname(argv[0], neonserv_cmd_rename_auth_lookup, cache);
48 }
49
50 static AUTHLOOKUP_CALLBACK(neonserv_cmd_rename_auth_lookup) {
51     struct neonserv_cmd_rename_cache *cache = data;
52     if(!exists) {
53         //AUTH_DOES_NOT_EXIST
54         reply(cache->textclient, cache->user, "NS_AUTH_UNKNOWN", cache->newauth);
55     } else
56         neonserv_cmd_rename_async1(cache->client, cache->textclient, cache->user, cache->event, cache->oldauth, auth);
57     free(cache->oldauth);
58     free(cache->newauth);
59     free(cache);
60 }
61
62 static void neonserv_cmd_rename_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct Event *event, char *oldauth, char *newauth) {
63     if(renameAccount(oldauth, newauth)) {
64         reply(textclient, user, "NS_RENAME_DONE", oldauth, newauth);
65     } else {
66         reply(textclient, user, "NS_RENAME_FAIL", oldauth);
67     }
68 }