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