added new multi log system
[NeonServV5.git] / src / modules / NeonServ.mod / cmd_neonserv_noregister.c
1 /* cmd_neonserv_noregister.c - NeonServ v5.6
2  * Copyright (C) 2011-2012  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]   *auth/#channel
22 * argv[1]   duration
23 * argv[2-*] reason
24 */
25
26 static AUTHLOOKUP_CALLBACK(neonserv_cmd_noregister_auth_lookup);
27 static USERAUTH_CALLBACK(neonserv_cmd_noregister_nick_lookup);
28 static void neonserv_cmd_noregister_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event,char *auth, int duration, char *reason);
29 static void neonserv_cmd_noregister_list(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char **argv, int argc);
30
31 struct neonserv_cmd_noregister_cache {
32     struct ClientSocket *client, *textclient;
33     struct UserNode *user;
34     struct ChanNode *chan;
35     struct Event *event;
36     int duration;
37     char *reason, *nick;
38 };
39
40 CMD_BIND(neonserv_cmd_noregister) {
41     if(argc < 3) {
42         neonserv_cmd_noregister_list(client, textclient, user, chan, event, argv, argc);
43         return;
44     }
45     int duration = strToTime(user, argv[1]);
46     if(duration == 0 && strcmp(argv[1], "0")) {
47         reply(textclient, user, "NS_NOREGISTER_INVALID_DURATION", argv[1]);
48         return;
49     }
50     char *reason = merge_argv(argv, 2, argc);
51     MYSQL_RES *res;
52     MYSQL_ROW row;
53     if(argv[0][0] == '#') {
54         printf_mysql_query("SELECT `botid` FROM `bot_channels` LEFT JOIN `channels` ON `bot_channels`.`chanid` = `channels`.`channel_id` WHERE `channel_name` = '%s'", escape_string(argv[0]));
55         res = mysql_use();
56         if ((row = mysql_fetch_row(res)) != NULL) {
57             reply(textclient, user, "NS_NOREGISTER_REGISTERED", argv[0]);
58             return;
59         }
60         neonserv_cmd_noregister_async1(client, textclient, user, chan, event, argv[0], duration, reason);
61     } else if(argv[0][0] == '*') {
62         //we've got an auth
63         argv[0]++;
64         printf_mysql_query("SELECT `user_user` FROM `users` WHERE `user_user` = '%s'", escape_string(argv[0]));
65         res = mysql_use();
66         if ((row = mysql_fetch_row(res)) != NULL) {
67             neonserv_cmd_noregister_async1(client, textclient, user, chan, event, row[0], duration, reason);
68         } else {
69             //we need to create a new user...
70             //but first lookup the auth to check if it really exists
71             struct neonserv_cmd_noregister_cache *cache = malloc(sizeof(*cache));
72             if (!cache) {
73                 printf_log("neonserv", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
74                 return;
75             }
76             cache->client = client;
77             cache->textclient = textclient;
78             cache->user = user;
79             cache->chan = chan;
80             cache->event = event;
81             cache->nick = strdup(argv[0]);
82             cache->duration = duration;
83             cache->reason = strdup(reason);
84             lookup_authname(argv[0], module_id, neonserv_cmd_noregister_auth_lookup, cache);
85         }
86     } else {
87         struct UserNode *cuser = getUserByNick(argv[0]);
88         if(!cuser) {
89             cuser = createTempUser(argv[0]);
90                         if(!cuser) {
91                 reply(textclient, user, "NS_USER_UNKNOWN", argv[0]);
92                 return;
93             }
94             cuser->flags |= USERFLAG_ISTMPUSER;
95         }
96         if(cuser->flags & USERFLAG_ISAUTHED) {
97             neonserv_cmd_noregister_async1(client, textclient, user, chan, event, cuser->auth, duration, reason);
98         } else {
99             struct neonserv_cmd_noregister_cache *cache = malloc(sizeof(*cache));
100             if (!cache) {
101                 printf_log("neonserv", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
102                 return;
103             }
104             cache->client = client;
105             cache->textclient = textclient;
106             cache->user = user;
107             cache->chan = chan;
108             cache->event = event;
109             cache->nick = strdup(user->nick);
110             cache->duration = duration;
111             cache->reason = strdup(reason);
112             get_userauth(cuser, module_id, neonserv_cmd_noregister_nick_lookup, cache);
113         }
114     }
115 }
116
117 static AUTHLOOKUP_CALLBACK(neonserv_cmd_noregister_auth_lookup) {
118     struct neonserv_cmd_noregister_cache *cache = data;
119     if(!exists) {
120         //AUTH_DOES_NOT_EXIST
121         reply(cache->textclient, cache->user, "NS_AUTH_UNKNOWN", cache->nick);
122     } else
123         neonserv_cmd_noregister_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, auth, cache->duration, cache->reason);
124     free(cache->reason);
125     free(cache->nick);
126     free(cache);
127 }
128
129 static USERAUTH_CALLBACK(neonserv_cmd_noregister_nick_lookup) {
130     struct neonserv_cmd_noregister_cache *cache = data;
131     if(!user) {
132         //USER_DOES_NOT_EXIST
133         reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
134     }
135     else if(!(user->flags & USERFLAG_ISAUTHED)) {
136         //USER_NOT_AUTHED
137         reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
138     }
139     else
140         neonserv_cmd_noregister_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, user->auth, cache->duration, cache->reason);
141     free(cache->reason);
142     free(cache->nick);
143     free(cache);
144 }
145
146 static void neonserv_cmd_noregister_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event,char *auth, int duration, char *reason) {
147     MYSQL_RES *res;
148     MYSQL_ROW row;
149     printf_mysql_query("SELECT `dnr_id` FROM `donotregister` WHERE `dnr_target` = '%s'", escape_string(auth));
150     res = mysql_use();
151     if((row = mysql_fetch_row(res)) != NULL) {
152         printf_mysql_query("DELETE FROM `donotregister` WHERE `dnr_id` = '%s'", row[0]);
153     }
154     int userid;
155     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
156     res = mysql_use();
157     if ((row = mysql_fetch_row(res)) != NULL)
158         userid = atoi(row[0]);
159     else
160         userid = 0;
161     printf_mysql_query("INSERT INTO `donotregister` (`dnr_target`, `dnr_timeout`, `dnr_user`, `dnr_reason`) VALUES ('%s', '%lu', '%d', '%s')", escape_string(auth), (duration ? (time(0)+duration) : 0), userid, escape_string(reason));
162     reply(textclient, user, "NS_NOREGISTER_DONE", auth);
163 }
164
165 static void neonserv_cmd_noregister_list(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char **argv, int argc) {
166     struct Table *table;
167     int entrys = 0;
168     MYSQL_RES *res;
169     MYSQL_ROW row;
170     printf_mysql_query("SELECT `dnr_target`, `dnr_timeout`, `user_user`, `dnr_reason` FROM `donotregister` LEFT JOIN `users` ON `dnr_user` = `user_id` ORDER BY `dnr_target` ASC");
171     res = mysql_use();
172     table = table_init(4, mysql_num_rows(res) + 1, 0);
173     char *content[4];
174     content[0] = get_language_string(user, "NS_DNR_TARGET");
175     content[1] = get_language_string(user, "NS_DNR_USER");
176     content[2] = get_language_string(user, "NS_DNR_EXPIRES");
177     content[3] = get_language_string(user, "NS_DNR_REASON");
178     table_add(table, content);
179     int duration;
180     char expires_str[MAXLEN];
181     while ((row = mysql_fetch_row(res)) != NULL) {
182         entrys++;
183         content[0] = row[0];
184         content[1] = row[2];
185         duration = atoi(row[1]);
186         content[2] = (duration ? timeToStr(user, (duration - time(0)), 2, expires_str) : get_language_string(user, "NS_USERS_SEEN_NEVER"));
187         content[3] = row[3];
188         table_add(table, content);
189     }
190     //send the table
191     char **table_lines = table_end(table);
192     int i;
193     for(i = 0; i < table->entrys; i++) {
194         reply(textclient, user, table_lines[i]);
195     }
196     if(!entrys)
197         reply(textclient, user, "NS_TABLE_NONE");
198     table_free(table);
199 }