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