Another year is about to end... So we have to update these damn copyright information :P
[NeonServV5.git] / src / cmd_global_register.c
1 /* cmd_global_register.c - NeonServ v5.3
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_global.h"
19
20 /*
21 * argv[0] - channel
22 * argv[1] - nick / *auth
23 * argv[2] - (optional) bot nick
24 */
25 static AUTHLOOKUP_CALLBACK(global_cmd_register_auth_lookup);
26 static USERAUTH_CALLBACK(global_cmd_register_nick_lookup);
27 static void global_cmd_register_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *channel, char *auth, int multibot, char *botname);
28
29 struct global_cmd_register_cache {
30     struct ClientSocket *client, *textclient;
31     struct UserNode *user;
32     struct ChanNode *chan;
33     struct Event *event;
34     char *nick;
35     char *channel;
36     char *botname;
37     int multibot;
38 };
39
40 CMD_BIND(global_cmd_register) {
41     MYSQL_RES *res;
42     MYSQL_ROW row;
43     char *channel = argv[0];
44     char *botname = (argc > 2 ? argv[2] : NULL);
45     int multibot = 0;
46     if(!is_valid_chan(channel)) {
47         reply(getTextBot(), user, "NS_INVALID_CHANNEL_NAME", argv[0]);
48         return;
49     }
50     printf_mysql_query("SELECT `botid`, `botclass` FROM `bot_channels` LEFT JOIN `bots` ON `bot_channels`.`botid` = `bots`.`id` LEFT JOIN `channels` ON `bot_channels`.`chanid` = `channels`.`channel_id` WHERE `channel_name` = '%s'", escape_string(channel));
51     res = mysql_use();
52     while ((row = mysql_fetch_row(res)) != NULL) {
53         if(atoi(row[1]) == client->botid && (client->botid || client->clientid == atoi(row[0]))) {
54             reply(getTextBot(), user, "NS_REGISTER_ALREADY", argv[0], client->user->nick);
55             return;
56         } else
57             multibot = 1;
58     }
59     printf_mysql_query("SELECT `user_user`, `dnr_timeout`, `dnr_reason`, `dnr_id` FROM `donotregister` LEFT JOIN `users` ON `dnr_user` = `user_id` WHERE `dnr_target` = '%s'", escape_string(channel));
60     res = mysql_use();
61     if((row = mysql_fetch_row(res)) != NULL) {
62         int expire_time = atoi(row[1]);
63         if(expire_time) {
64             if(expire_time - time(0) <= 0) {
65                 printf_mysql_query("DELETE FROM `donotregister` WHERE `dnr_id` = '%s'", row[3]);
66             } else {
67                 char expireBuf[MAXLEN];
68                 reply(getTextBot(), user, "NS_DNR_SET_EXPIRES", channel, row[0], timeToStr(user, (expire_time - time(0)), 2, expireBuf), row[2]);
69                 return;
70             }
71         } else {
72             reply(getTextBot(), user, "NS_DNR_SET", channel, row[0], row[2]);
73             return;
74         }
75     }
76     //if theres already another bot in the channel we don't need a owner parameter...
77     if(multibot && argc < 2) {
78         //skip all these owner check lines
79         multibot = 2;
80         global_cmd_register_async1(client, getTextBot(), user, chan, event, channel, NULL, multibot, botname);
81         return;
82     } else if(argc < 2) {
83         global_cmd_register_async1(client, getTextBot(), user, chan, event, channel, user->auth, multibot, botname);
84         return;
85     }
86     //check own access
87     if(argv[1][0] == '*') {
88         //we've got an auth
89         argv[1]++;
90         printf_mysql_query("SELECT `user_user` FROM `users` WHERE `user_user` = '%s'", escape_string(argv[1]));
91         res = mysql_use();
92         if ((row = mysql_fetch_row(res)) != NULL) {
93             global_cmd_register_async1(client, getTextBot(), user, chan, event, channel, row[0], multibot, botname);
94         } else {
95             //we need to create a new user...
96             //but first lookup the auth to check if it really exists
97             struct global_cmd_register_cache *cache = malloc(sizeof(*cache));
98             if (!cache) {
99                 perror("malloc() failed");
100                 return;
101             }
102             cache->client = client;
103             cache->textclient = getTextBot();
104             cache->user = user;
105             cache->chan = chan;
106             cache->event = event;
107             cache->nick = strdup(argv[1]);
108             cache->channel = strdup(channel);
109             cache->multibot = multibot;
110             cache->botname = (botname ? strdup(botname) : NULL);
111             lookup_authname(argv[1], global_cmd_register_auth_lookup, cache);
112         }
113     } else {
114         struct UserNode *cuser = getUserByNick(argv[1]);
115         if(!cuser) {
116             cuser = createTempUser(argv[1]);
117             cuser->flags |= USERFLAG_ISTMPUSER;
118         }
119         if(cuser->flags & USERFLAG_ISAUTHED) {
120             global_cmd_register_async1(client, getTextBot(), user, chan, event, channel, cuser->auth, multibot, botname);
121         } else {
122             struct global_cmd_register_cache *cache = malloc(sizeof(*cache));
123             if (!cache) {
124                 perror("malloc() failed");
125                 return;
126             }
127             cache->client = client;
128             cache->textclient = getTextBot();
129             cache->user = user;
130             cache->chan = chan;
131             cache->event = event;
132             cache->nick = strdup(argv[1]);
133             cache->channel = strdup(channel);
134             cache->multibot = multibot;
135             cache->botname = (botname ? strdup(botname) : NULL);
136             get_userauth(cuser, global_cmd_register_nick_lookup, cache);
137         }
138     }
139 }
140
141 static AUTHLOOKUP_CALLBACK(global_cmd_register_auth_lookup) {
142     struct global_cmd_register_cache *cache = data;
143     if(!exists) {
144         //AUTH_DOES_NOT_EXIST
145         reply(cache->textclient, cache->user, "NS_AUTH_UNKNOWN", cache->nick);
146     } else
147         global_cmd_register_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, cache->channel, auth, cache->multibot, cache->botname);
148     if(cache->botname)
149         free(cache->botname);
150     free(cache->channel);
151     free(cache->nick);
152     free(cache);
153 }
154
155 static USERAUTH_CALLBACK(global_cmd_register_nick_lookup) {
156     struct global_cmd_register_cache *cache = data;
157     if(!user) {
158         //USER_DOES_NOT_EXIST
159         reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
160     }
161     else if(!(user->flags & USERFLAG_ISAUTHED)) {
162         //USER_NOT_AUTHED
163         reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
164     }
165     else
166         global_cmd_register_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, cache->channel, user->auth, cache->multibot, cache->botname);
167     if(cache->botname)
168         free(cache->botname);
169     free(cache->channel);
170     free(cache->nick);
171     free(cache);
172 }
173
174 static void global_cmd_register_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *channel, char *auth, int multibot, char *botname) {
175     //we've got a valid auth now...
176     MYSQL_RES *res;
177     MYSQL_ROW row, row2;
178     int userid = 0, adminid;
179     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
180     res = mysql_use();
181     if ((row = mysql_fetch_row(res)) != NULL)
182         adminid = atoi(row[0]);
183     else
184         adminid = 0;
185     if(multibot != 2) {
186         printf_mysql_query("SELECT `user_user`, `dnr_timeout`, `dnr_reason`, `dnr_id` FROM `donotregister` LEFT JOIN `users` ON `dnr_user` = `user_id` WHERE `dnr_target` = '%s'", escape_string(auth));
187         res = mysql_use();
188         if((row = mysql_fetch_row(res)) != NULL) {
189             int expire_time = atoi(row[1]);
190             if(expire_time) {
191                 if(expire_time - time(0) <= 0) {
192                     printf_mysql_query("DELETE FROM `donotregister` WHERE `dnr_id` = '%s'", row[3]);
193                 } else {
194                     char expireBuf[MAXLEN];
195                     reply(getTextBot(), user, "NS_DNR_SET_EXPIRES", auth, row[0], timeToStr(user, (expire_time - time(0)), 2, expireBuf), row[2]);
196                     return;
197                 }
198             } else {
199                 reply(getTextBot(), user, "NS_DNR_SET", auth, row[0], row[2]);
200                 return;
201             }
202         }
203         printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
204         res = mysql_use();
205         if ((row = mysql_fetch_row(res)) != NULL) {
206             userid = atoi(row[0]);
207         } else {
208             printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(auth));
209             userid = (int) mysql_insert_id(mysql_conn);
210         }
211     }
212     if(client->botid)
213         printf_mysql_query("SELECT `id`, `max_channels`, `defaulttrigger`, `nick` FROM `bots` WHERE `botclass` = '%d' AND `active` = '1' ORDER BY `register_priority` DESC", client->botid);
214     else
215         printf_mysql_query("SELECT `id`, `max_channels`, `defaulttrigger`, `nick` FROM `bots` WHERE `id` = '%d' AND `active` = '1'", client->clientid);
216     res = mysql_use();
217     int botid = 0;
218     while ((row = mysql_fetch_row(res)) != NULL) {
219         //check channel count
220         printf_mysql_query("SELECT COUNT(*) FROM `bot_channels` WHERE `botid` = '%s'", row[0]);
221         row2 = mysql_fetch_row(mysql_use());
222         if(atoi(row2[0]) < atoi(row[1]) && (!botname || !stricmp(botname, row[3]))) {
223             botid = atoi(row[0]);
224             break;
225         }
226     }
227     if(!botid) {
228         reply(textclient, user, "NS_REGISTER_FULL");
229         return;
230     }
231     int chanid;
232     printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(channel));
233     res = mysql_use();
234     if ((row = mysql_fetch_row(res)) != NULL) {
235         chanid = atoi(row[0]);
236         printf_mysql_query("UPDATE `channels` SET `channel_registered` = UNIX_TIMESTAMP(), `channel_registrator` = '%d' WHERE `channel_id` = '%d'", adminid, chanid);
237     } else {
238         printf_mysql_query("INSERT INTO `channels` (`channel_name`, `channel_registered`, `channel_registrator`) VALUES ('%s', UNIX_TIMESTAMP(), '%d')", escape_string(channel), adminid);
239         chanid = (int) mysql_insert_id(mysql_conn);
240     }
241     struct ClientSocket *bot;
242     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
243         if(bot->clientid == botid)
244             break;
245     }
246     if(bot) {
247         putsock(bot, "JOIN %s", channel);
248     } else
249         reply(textclient, user, "NS_REGISTER_DISCONNECTED", channel);
250     printf_mysql_query("INSERT INTO `bot_channels` (`botid`, `chanid`, `trigger`) VALUES ('%d', '%d', NULL)", botid, chanid);
251     if(multibot != 2) {
252         if(multibot) {
253             printf_mysql_query("UPDATE `chanusers` SET `chanuser_access` = 499 WHERE `chanuser_cid` = '%d' AND `chanuser_access` = '500'", chanid);
254             printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_cid` = '%d' AND `chanuser_uid` = '%d'", chanid, userid);
255         } else
256             printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_cid` = '%d'", chanid);
257         printf_mysql_query("INSERT INTO `chanusers` (`chanuser_cid`, `chanuser_uid`, `chanuser_access`) VALUES ('%d', '%d', '%d')", chanid, userid, 500);
258         reply(textclient, user, "NS_REGISTER_DONE", channel, auth);
259     } else
260         reply(textclient, user, "NS_REGISTER_DONE_NOAUTH", channel);
261     logEvent(event);
262 }