*** VERSION 5.2.0 ***
[NeonServV5.git] / src / cmd_neonserv_chanservsync.c
1 /* cmd_neonserv_chanservsync.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] - 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(getTextBot(), 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(getTextBot(), user, "NS_CHANSERVSYNC_UNSUPPORTED", botnick, client->user->nick);
92         }
93         reply(getTextBot(), 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 = getTextBot();
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);
110     reply(getTextBot(), 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 == ' ');
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             caccess = atoi(tokens[0]);
156             username = tokens[1];
157         } else
158             return;
159         if(caccess < 1 || caccess > 500) return;
160         int flags = 0;
161         time_t now = time(0);
162         time_t seen_time = now; //now - now = 0 (never)
163         neonserv_cmd_chanservsync_used->last_response = now;
164         if(strlen(username) < 3) return;
165         //ok we have access and username... maybe there is something else we can parse???
166         char *seen = NULL;
167         char *status = NULL;
168         if(tokensPos > 2) {
169             if(!stricmp("normal", tokens[2]) || !stricmp("suspended", tokens[2]) || !stricmp("bot", tokens[2])) {
170                 status = tokens[2];
171                 if (tokensPos > 3) {
172                     seen = merge_argv(tokens, 3, tokensPos);
173                 }
174             } else if (tokensPos > 3) {
175                 if(!stricmp("normal", tokens[tokensPos-1]) || !stricmp("suspended", tokens[tokensPos-1]) || !stricmp("bot", tokens[tokensPos-1])) {
176                     status = tokens[tokensPos-1];
177                     seen = merge_argv(tokens, 2, tokensPos-1);
178                 } else {
179                     seen = merge_argv(tokens, 2, tokensPos);
180                 }
181             } else {
182                 seen = merge_argv(tokens, 2, tokensPos);
183             }
184         }
185         if(status && !stricmp(status, "suspended")) {
186             flags |= DB_CHANUSER_SUSPENDED;
187         }
188         if(seen) {
189             if(!stricmp(seen, "here"))
190                 seen_time = 0;
191             else if(stricmp(seen, "never"))
192                 seen_time = strToTime(user, seen);
193         }
194         seen_time = now - seen_time;
195         //we've collected all information now. synchronize the user (use the higher access if the user is already added)
196         MYSQL_RES *res;
197         MYSQL_ROW row;
198         int userid;
199         printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(username));
200         res = mysql_use();
201         if ((row = mysql_fetch_row(res)) != NULL) {
202             userid = atoi(row[0]);
203             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);
204         } else if(!stricmp(user->nick, "chanserv")) {
205             printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(username));
206             userid = (int) mysql_insert_id(mysql_conn);
207             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);
208         } else {
209             //lookup auth
210             struct neonserv_cmd_chanservsync_auth_cache *cache = malloc(sizeof(*cache));
211             if (!cache) {
212                 perror("malloc() failed");
213                 return;
214             }
215             cache->client = neonserv_cmd_chanservsync_used->client;
216             cache->textclient = neonserv_cmd_chanservsync_used->textclient;
217             cache->user = neonserv_cmd_chanservsync_used->user;
218             cache->chan = neonserv_cmd_chanservsync_used->chan;
219             cache->caccess = caccess;
220             cache->seen = seen_time;
221             cache->flags = flags;
222             lookup_authname(username, neonserv_cmd_chanservsync_auth_lookup, cache);
223         }
224     }
225 }
226
227 static void neonserv_cmd_chanservsync_free_cache() {
228     free(neonserv_cmd_chanservsync_used->botnick);
229     free(neonserv_cmd_chanservsync_used);
230     unbind_privnotice(neonserv_cmd_chanservsync_notice_listener);
231     neonserv_cmd_chanservsync_used = NULL;
232 }
233
234 static AUTHLOOKUP_CALLBACK(neonserv_cmd_chanservsync_auth_lookup) {
235     struct neonserv_cmd_chanservsync_auth_cache *cache = data;
236     if(exists) {
237         printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(auth));
238         int userid = (int) mysql_insert_id(mysql_conn);
239         neonserv_cmd_chanservsync_synchronize_user(cache->client, cache->textclient, cache->user, cache->chan, auth, userid, cache->caccess, cache->seen, cache->flags, 1);
240     }
241     free(cache);
242 }
243
244 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) {
245     //just sync the user with the given userid with the providet information
246     if(caccess == 500) caccess = 499;
247     if(new) {
248         //just add
249         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);
250     } else {
251         MYSQL_RES *res;
252         MYSQL_ROW row;
253         //check if already added
254         printf_mysql_query("SELECT `chanuser_access`, `chanuser_id`, `chanuser_seen` FROM `chanusers` WHERE `chanuser_cid` = '%d' AND `chanuser_uid` = '%d'", chan->channel_id, userid);
255         res = mysql_use();
256         if ((row = mysql_fetch_row(res)) != NULL) {
257             //clvl
258             if(atoi(row[0]) >= caccess) return;
259             if(atol(row[2]) > seen) seen = atol(row[2]);
260             printf_mysql_query("UPDATE `chanusers` SET `chanuser_access` = '%d', `chanuser_seen` = '%lu' WHERE `chanuser_id` = '%s'", caccess, (unsigned long) seen, row[1]);
261         } else 
262             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);
263     }
264     reply(textclient, user, "NS_CHANSERVSYNC_SYNCHRONIZED", username, caccess);
265 }