980f73693e57279c5eca1298cdbb25275877b6d3
[NeonServV5.git] / src / cmd_neonserv_register.c
1 /* cmd_neonserv_register.c - NeonServ v5.1
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] - channel
22 * argv[0/1] - nick / *auth
23 */
24 static AUTHLOOKUP_CALLBACK(neonserv_cmd_register_auth_lookup);
25 static USERAUTH_CALLBACK(neonserv_cmd_register_nick_lookup);
26 static void neonserv_cmd_register_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *channel, char *auth, int multibot);
27
28 struct neonserv_cmd_register_cache {
29     struct ClientSocket *client, *textclient;
30     struct UserNode *user;
31     struct ChanNode *chan;
32     struct Event *event;
33     char *nick;
34     char *channel;
35     int multibot;
36 };
37
38 CMD_BIND(neonserv_cmd_register) {
39     MYSQL_RES *res;
40     MYSQL_ROW row;
41     char *channel = argv[0];
42     int multibot = 0;
43     if(!is_valid_chan(channel)) {
44         reply(getTextBot(), user, "NS_INVALID_CHANNEL_NAME", argv[0]);
45         return;
46     }
47     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));
48     res = mysql_use();
49     while ((row = mysql_fetch_row(res)) != NULL) {
50         if(atoi(row[1]) == client->botid) {
51             reply(getTextBot(), user, "NS_REGISTER_ALREADY", argv[0], client->user->nick);
52             return;
53         } else
54             multibot = 1;
55     }
56     //if theres already another bot in the channel we don't need a owner parameter...
57     if(multibot && argc < 2) {
58         //skip all these owner check lines
59         multibot = 2;
60         neonserv_cmd_register_async1(client, getTextBot(), user, chan, event, channel, NULL, multibot);
61         return;
62     } else if(argc < 2) {
63         reply(getTextBot(), user, "MODCMD_LESS_PARAM_COUNT");
64         return;
65     }
66     //check own access
67     if(argv[1][0] == '*') {
68         //we've got an auth
69         argv[1]++;
70         printf_mysql_query("SELECT `user_user` FROM `users` WHERE `user_user` = '%s'", escape_string(argv[1]));
71         res = mysql_use();
72         if ((row = mysql_fetch_row(res)) != NULL) {
73             neonserv_cmd_register_async1(client, getTextBot(), user, chan, event, channel, row[0], multibot);
74         } else {
75             //we need to create a new user...
76             //but first lookup the auth to check if it really exists
77             struct neonserv_cmd_register_cache *cache = malloc(sizeof(*cache));
78             if (!cache) {
79                 perror("malloc() failed");
80                 return;
81             }
82             cache->client = client;
83             cache->textclient = getTextBot();
84             cache->user = user;
85             cache->chan = chan;
86             cache->event = event;
87             cache->nick = strdup(argv[1]);
88             cache->channel = strdup(channel);
89             cache->multibot = multibot;
90             lookup_authname(argv[1], neonserv_cmd_register_auth_lookup, cache);
91         }
92     } else {
93         struct UserNode *cuser = getUserByNick(argv[1]);
94         if(!cuser) {
95             cuser = createTempUser(argv[1]);
96             cuser->flags |= USERFLAG_ISTMPUSER;
97         }
98         if(cuser->flags & USERFLAG_ISAUTHED) {
99             neonserv_cmd_register_async1(client, getTextBot(), user, chan, event, channel, cuser->auth, multibot);
100         } else {
101             struct neonserv_cmd_register_cache *cache = malloc(sizeof(*cache));
102             if (!cache) {
103                 perror("malloc() failed");
104                 return;
105             }
106             cache->client = client;
107             cache->textclient = getTextBot();
108             cache->user = user;
109             cache->chan = chan;
110             cache->event = event;
111             cache->nick = strdup(argv[1]);
112             cache->channel = strdup(channel);
113             cache->multibot = multibot;
114             get_userauth(cuser, neonserv_cmd_register_nick_lookup, cache);
115         }
116     }
117 }
118
119 static AUTHLOOKUP_CALLBACK(neonserv_cmd_register_auth_lookup) {
120     struct neonserv_cmd_register_cache *cache = data;
121     if(!exists) {
122         //AUTH_DOES_NOT_EXIST
123         reply(cache->textclient, cache->user, "NS_AUTH_UNKNOWN", cache->nick);
124     } else
125         neonserv_cmd_register_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, cache->channel, auth, cache->multibot);
126     free(cache->channel);
127     free(cache->nick);
128     free(cache);
129 }
130
131 static USERAUTH_CALLBACK(neonserv_cmd_register_nick_lookup) {
132     struct neonserv_cmd_register_cache *cache = data;
133     if(!user) {
134         //USER_DOES_NOT_EXIST
135         reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
136     }
137     else if(!(user->flags & USERFLAG_ISAUTHED)) {
138         //USER_NOT_AUTHED
139         reply(cache->textclient, cache->user, "NS_USER_NEED_AUTH", cache->nick);
140     }
141     else
142         neonserv_cmd_register_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, cache->channel, user->auth, cache->multibot);
143     free(cache->channel);
144     free(cache->nick);
145     free(cache);
146 }
147
148 static void neonserv_cmd_register_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *channel, char *auth, int multibot) {
149     //we've got a valid auth now...
150     MYSQL_RES *res;
151     MYSQL_ROW row, row2;
152     int userid = 0, adminid;
153     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
154     res = mysql_use();
155     if ((row = mysql_fetch_row(res)) != NULL)
156         adminid = atoi(row[0]);
157     else
158         adminid = 0;
159     if(multibot != 2) {
160         printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
161         res = mysql_use();
162         if ((row = mysql_fetch_row(res)) != NULL) {
163             userid = atoi(row[0]);
164         } else {
165             printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(auth));
166             userid = (int) mysql_insert_id(mysql_conn);
167         }
168     }
169     printf_mysql_query("SELECT `id`, `max_channels`, `defaulttrigger` FROM `bots` WHERE `botclass` = '%d' ORDER BY `register_priority` DESC", client->botid);
170     res = mysql_use();
171     int botid = 0;
172     char *bottrigger;
173     while ((row = mysql_fetch_row(res)) != NULL) {
174         //check channel count
175         printf_mysql_query("SELECT COUNT(*) FROM `bot_channels` WHERE `botid` = '%s'", row[0]);
176         row2 = mysql_fetch_row(mysql_use());
177         if(atoi(row2[0]) < atoi(row[1])) {
178             botid = atoi(row[0]);
179             bottrigger = row[2];
180             break;
181         }
182     }
183     if(!botid) {
184         reply(textclient, user, "NS_REGISTER_FULL");
185         return;
186     }
187     int chanid;
188     printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(channel));
189     res = mysql_use();
190     if ((row = mysql_fetch_row(res)) != NULL) {
191         chanid = atoi(row[0]);
192         printf_mysql_query("UPDATE `channels` SET `channel_registered` = UNIX_TIMESTAMP(), `channel_registrator` = '%d' WHERE `channel_id` = '%d'", adminid, chanid);
193     } else {
194         printf_mysql_query("INSERT INTO `channels` (`channel_name`, `channel_registered`, `channel_registrator`) VALUES ('%s', UNIX_TIMESTAMP(), '%d')", escape_string(channel), adminid);
195         chanid = (int) mysql_insert_id(mysql_conn);
196     }
197     struct ClientSocket *bot;
198     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
199         if(bot->clientid == botid)
200             break;
201     }
202     if(bot) {
203         putsock(bot, "JOIN %s", channel);
204     } else
205         reply(textclient, user, "NS_REGISTER_DISCONNECTED");
206     printf_mysql_query("INSERT INTO `bot_channels` (`botid`, `chanid`, `trigger`) VALUES ('%d', '%d', '%s')", botid, chanid, bottrigger);
207     if(multibot != 2) {
208         if(multibot) {
209             printf_mysql_query("UPDATE `chanusers` SET `chanuser_access` = 499 WHERE `chanuser_cid` = '%d' AND `chanuser_access` = '500'", chanid);
210             printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_cid` = '%d' AND `chanuser_uid` = '%d'", chanid, userid);
211         } else
212             printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_cid` = '%d'", chanid);
213         printf_mysql_query("INSERT INTO `chanusers` (`chanuser_cid`, `chanuser_uid`, `chanuser_access`) VALUES ('%d', '%d', '%d')", chanid, userid, 500);
214         reply(textclient, user, "NS_REGISTER_DONE", channel, auth);
215     } else
216         reply(textclient, user, "NS_REGISTER_DONE_NOAUTH", channel);
217     logEvent(event);
218 }