f918d272439edbaee0642ca7c7a9836aaef06590
[NeonServV5.git] / src / modules / NeonServ.mod / cmd_neonserv_chanservsync.c
1 /* cmd_neonserv_chanservsync.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] - botnick
22 * argv[1] - key
23 */
24 #define CHANSERVSYNC_END_TIMEOUT 5
25
26 static void neonserv_cmd_chanservsync_notice_listener(struct UserNode *user, struct UserNode *target, char *message);
27 static void neonserv_cmd_chanservsync_free_cache();
28 static AUTHLOOKUP_CALLBACK(neonserv_cmd_chanservsync_auth_lookup);
29 static void neonserv_cmd_chanservsync_synchronize_user(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *username, int userid, int caccess, time_t seen, int flags, int new);
30
31 struct neonserv_cmd_chanservsync_cache {
32     struct ClientSocket *client, *textclient;
33     struct UserNode *user;
34     struct ChanNode *chan;
35     char *botnick;
36     time_t last_response;
37 };
38
39 struct neonserv_cmd_chanservsync_auth_cache {
40     struct ClientSocket *client, *textclient;
41     struct UserNode *user;
42     struct ChanNode *chan;
43     int caccess;
44     time_t seen;
45     int flags;
46 };
47
48 struct neonserv_cmd_chanservsync_cache *neonserv_cmd_chanservsync_used = NULL;
49 const char* neonserv_cmd_chanservsync_supported[] = {"ChanServ", NULL};
50
51 CMD_BIND(neonserv_cmd_chanservsync) {
52     if(neonserv_cmd_chanservsync_used && time(0) - neonserv_cmd_chanservsync_used->last_response < CHANSERVSYNC_END_TIMEOUT) {
53         reply(textclient, user, "NS_CHANSERVSYNC_INUSE");
54         return;
55     }
56     if(neonserv_cmd_chanservsync_used) {
57         neonserv_cmd_chanservsync_free_cache();
58     }
59     char *botnick = "ChanServ";
60     char *key = "";
61     if(argc) {
62         if(argv[0][0] == '!') {
63             key = argv[0];
64         } else {
65             botnick = argv[0];
66             if(argc > 1)
67                 key = argv[1];
68         }
69     }
70     int seed = 0;
71     char *tmp;
72     char synckey[18];
73     for(tmp = user->auth; *tmp; tmp++)
74         seed = (seed * 0xEECE66DL ^ ((*tmp << 24) | (*tmp << 16) | (*tmp << 8) | *tmp));
75     for(tmp = chan->name; *tmp; tmp++)
76         seed = (seed * 0xEECE66DL ^ ((*tmp << 24) | (*tmp << 16) | (*tmp << 8) | *tmp));
77     for(tmp = botnick; *tmp; tmp++)
78         seed = (seed * 0xEECE66DL ^ ((*tmp << 24) | (*tmp << 16) | (*tmp << 8) | *tmp));
79     sprintf(synckey, "!%08x!", seed);
80     if(strcmp(synckey, key)) {
81         int f = 0;
82         const char **supp = neonserv_cmd_chanservsync_supported;
83         while(*supp) {
84             if(!stricmp(*supp, botnick)) {
85                 f = 1;
86                 break;
87             }
88             supp++;
89         }
90         if(!f) {
91             reply(textclient, user, "NS_CHANSERVSYNC_UNSUPPORTED", botnick, client->user->nick);
92         }
93         reply(textclient, user, "NS_CHANSERVSYNC_KEY", client->user->nick, botnick, botnick, synckey);
94         return;
95     }
96     struct neonserv_cmd_chanservsync_cache *cache = malloc(sizeof(*cache));
97     if (!cache) {
98         perror("malloc() failed");
99         return;
100     }
101     cache->client = client;
102     cache->textclient = textclient;
103     cache->user = user;
104     cache->chan = chan;
105     cache->botnick = strdup(botnick);
106     cache->last_response = time(0);
107     neonserv_cmd_chanservsync_used = cache;
108     putsock(client, "PRIVMSG %s :users %s", botnick, chan->name);
109     bind_privnotice(neonserv_cmd_chanservsync_notice_listener, module_id);
110     reply(textclient, user, "NS_CHANSERVSYNC_SYNCHRONIZING", chan->name, botnick);
111     logEvent(event);
112 }
113
114 static void neonserv_cmd_chanservsync_notice_listener(struct UserNode *user, struct UserNode *target, char *message) {
115     if(neonserv_cmd_chanservsync_used && neonserv_cmd_chanservsync_used->client->user == target && !stricmp(user->nick, neonserv_cmd_chanservsync_used->botnick)) {
116         //we've got a notice from our bot...
117         //let's try parsing it....
118         char *p = message;
119         char *tokens[MAXLEN];
120         int tokensPos = 0;
121         while(*p == ' ') //skip leading spaces (airb0t)
122             p++;
123         message = p;
124         char *q = p;
125         while(*q) {
126             if(*q < 32) *q = ' ';
127             q++;
128         }
129         while((q = strstr(p, " "))) {
130             *q = '\0';
131             do {
132                 q++;
133             } while(*q == ' ' || *q == '-');
134             if(*p) {
135                 tokens[tokensPos++] = p;
136             }
137             p = q;
138         }
139         if(*p) {
140             tokens[tokensPos++] = p;
141         }
142         int caccess;
143         char *username;
144         if(tokensPos == 1) {
145             //maybe a chip-like userlist
146             if(tokens[0][0] == '@') {
147                 caccess = 200;
148                 username = &tokens[0][1];
149             } else if(tokens[0][0] == '+') {
150                 caccess = 100;
151                 username = &tokens[0][1];
152             } else
153                 return;
154         } else if(tokensPos >= 2) {
155             if(atoi(tokens[0]) > 0) {
156                 caccess = atoi(tokens[0]);
157                 username = tokens[1];
158             } else {
159                 caccess = atoi(tokens[1]);
160                 username = tokens[0];
161             }
162         } else
163             return;
164         if(caccess < 1 || caccess > 500) return;
165         int flags = 0;
166         time_t now = time(0);
167         time_t seen_time = now; //now - now = 0 (never)
168         neonserv_cmd_chanservsync_used->last_response = now;
169         if(strlen(username) < 3) return;
170         //ok we have access and username... maybe there is something else we can parse???
171         char *seen = NULL;
172         char *status = NULL;
173         if(tokensPos > 2) {
174             if(!stricmp("normal", tokens[2]) || !stricmp("suspended", tokens[2]) || !stricmp("bot", tokens[2])) {
175                 status = tokens[2];
176                 if (tokensPos > 3) {
177                     seen = merge_argv(tokens, 3, tokensPos);
178                 }
179             } else if (tokensPos > 3) {
180                 if(!stricmp("normal", tokens[tokensPos-1]) || !stricmp("suspended", tokens[tokensPos-1]) || !stricmp("bot", tokens[tokensPos-1])) {
181                     status = tokens[tokensPos-1];
182                     seen = merge_argv(tokens, 2, tokensPos-1);
183                 } else {
184                     seen = merge_argv(tokens, 2, tokensPos);
185                 }
186             } else {
187                 seen = merge_argv(tokens, 2, tokensPos);
188             }
189         }
190         if(status && !stricmp(status, "suspended")) {
191             flags |= DB_CHANUSER_SUSPENDED;
192         }
193         if(seen) {
194             if(!stricmp(seen, "here"))
195                 seen_time = 0;
196             else if(stricmp(seen, "never"))
197                 seen_time = strToTime(user, seen);
198         }
199         seen_time = now - seen_time;
200         //we've collected all information now. synchronize the user (use the higher access if the user is already added)
201         MYSQL_RES *res;
202         MYSQL_ROW row;
203         int userid;
204         printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(username));
205         res = mysql_use();
206         if ((row = mysql_fetch_row(res)) != NULL) {
207             userid = atoi(row[0]);
208             neonserv_cmd_chanservsync_synchronize_user(neonserv_cmd_chanservsync_used->client, neonserv_cmd_chanservsync_used->textclient, neonserv_cmd_chanservsync_used->user, neonserv_cmd_chanservsync_used->chan, username, userid, caccess, seen_time, flags, 0);
209         } else if(!stricmp(user->nick, "chanserv")) {
210             printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(username));
211             userid = (int) mysql_insert_id(get_mysql_conn());
212             neonserv_cmd_chanservsync_synchronize_user(neonserv_cmd_chanservsync_used->client, neonserv_cmd_chanservsync_used->textclient, neonserv_cmd_chanservsync_used->user, neonserv_cmd_chanservsync_used->chan, username, userid, caccess, seen_time, flags, 1);
213         } else {
214             //lookup auth
215             struct neonserv_cmd_chanservsync_auth_cache *cache = malloc(sizeof(*cache));
216             if (!cache) {
217                 perror("malloc() failed");
218                 return;
219             }
220             cache->client = neonserv_cmd_chanservsync_used->client;
221             cache->textclient = neonserv_cmd_chanservsync_used->textclient;
222             cache->user = neonserv_cmd_chanservsync_used->user;
223             cache->chan = neonserv_cmd_chanservsync_used->chan;
224             cache->caccess = caccess;
225             cache->seen = seen_time;
226             cache->flags = flags;
227             lookup_authname(username, module_id, neonserv_cmd_chanservsync_auth_lookup, cache);
228         }
229     }
230 }
231
232 static void neonserv_cmd_chanservsync_free_cache() {
233     free(neonserv_cmd_chanservsync_used->botnick);
234     free(neonserv_cmd_chanservsync_used);
235     unbind_privnotice(neonserv_cmd_chanservsync_notice_listener);
236     neonserv_cmd_chanservsync_used = NULL;
237 }
238
239 static AUTHLOOKUP_CALLBACK(neonserv_cmd_chanservsync_auth_lookup) {
240     struct neonserv_cmd_chanservsync_auth_cache *cache = data;
241     if(exists) {
242         printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(auth));
243         int userid = (int) mysql_insert_id(get_mysql_conn());
244         neonserv_cmd_chanservsync_synchronize_user(cache->client, cache->textclient, cache->user, cache->chan, auth, userid, cache->caccess, cache->seen, cache->flags, 1);
245     }
246     free(cache);
247 }
248
249 static void neonserv_cmd_chanservsync_synchronize_user(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *username, int userid, int caccess, time_t seen, int flags, int new) {
250     //just sync the user with the given userid with the providet information
251     if(caccess == 500) caccess = 499;
252     if(new) {
253         //just add
254         printf_mysql_query("INSERT INTO `chanusers` (`chanuser_cid`, `chanuser_uid`, `chanuser_access`, `chanuser_seen`, `chanuser_flags`) VALUES ('%d', '%d', '%d', '%lu', '%d')", chan->channel_id, userid, caccess, (unsigned long) seen, flags);
255     } else {
256         MYSQL_RES *res;
257         MYSQL_ROW row;
258         //check if already added
259         printf_mysql_query("SELECT `chanuser_access`, `chanuser_id`, `chanuser_seen` FROM `chanusers` WHERE `chanuser_cid` = '%d' AND `chanuser_uid` = '%d'", chan->channel_id, userid);
260         res = mysql_use();
261         if ((row = mysql_fetch_row(res)) != NULL) {
262             //clvl
263             if(atoi(row[0]) >= caccess) return;
264             if(atol(row[2]) > seen) seen = atol(row[2]);
265             printf_mysql_query("UPDATE `chanusers` SET `chanuser_access` = '%d', `chanuser_seen` = '%lu' WHERE `chanuser_id` = '%s'", caccess, (unsigned long) seen, row[1]);
266         } else 
267             printf_mysql_query("INSERT INTO `chanusers` (`chanuser_cid`, `chanuser_uid`, `chanuser_access`, `chanuser_seen`, `chanuser_flags`) VALUES ('%d', '%d', '%d', '%lu', '%d')", chan->channel_id, userid, caccess, (unsigned long) seen, flags);
268     }
269     reply(textclient, user, "NS_CHANSERVSYNC_SYNCHRONIZED", username, caccess);
270 }