completed cmd_chanservsync
[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 }
94
95 static void neonserv_cmd_chanservsync_notice_listener(struct UserNode *user, struct UserNode *target, char *message) {
96     if(neonserv_cmd_chanservsync_used && neonserv_cmd_chanservsync_used->client->user == target && !stricmp(user->nick, neonserv_cmd_chanservsync_used->botnick)) {
97         //we've got a notice from our bot...
98         //let's try parsing it....
99         char *p = message;
100         char *tokens[MAXLEN];
101         int tokensPos = 0;
102         while(*p == ' ') //skip leading spaces (airb0t)
103             p++;
104         message = p;
105         char *q = p;
106         while(*q) {
107             if(*q < 32) *q = ' ';
108             q++;
109         }
110         while((q = strstr(p, " "))) {
111             *q = '\0';
112             do {
113                 q++;
114             } while(*q == ' ');
115             if(*p) {
116                 tokens[tokensPos++] = p;
117             }
118             p = q;
119         }
120         if(*p) {
121             tokens[tokensPos++] = p;
122         }
123         int caccess;
124         char *username;
125         if(tokensPos == 1) {
126             //maybe a chip-like userlist
127             if(tokens[0][0] == '@') {
128                 caccess = 200;
129                 username = &tokens[0][1];
130             } else if(tokens[0][0] == '+') {
131                 caccess = 100;
132                 username = &tokens[0][1];
133             } else
134                 return;
135         } else if(tokensPos >= 2) {
136             caccess = atoi(tokens[0]);
137             username = tokens[1];
138         } else
139             return;
140         if(caccess < 1 || caccess > 500) return;
141         int flags = 0;
142         time_t now = time(0);
143         time_t seen_time = now; //now - now = 0 (never)
144         neonserv_cmd_chanservsync_used->last_response = now;
145         if(strlen(username) < 3) return;
146         //ok we have access and username... maybe there is something else we can parse???
147         char *seen = NULL;
148         char *status = NULL;
149         if(tokensPos > 2) {
150             if(!stricmp("normal", tokens[2]) || !stricmp("suspended", tokens[2]) || !stricmp("bot", tokens[2])) {
151                 status = tokens[2];
152                 if (tokensPos > 3) {
153                     seen = merge_argv(tokens, 3, tokensPos);
154                 }
155             } else if (tokensPos > 3) {
156                 if(!stricmp("normal", tokens[tokensPos-1]) || !stricmp("suspended", tokens[tokensPos-1]) || !stricmp("bot", tokens[tokensPos-1])) {
157                     status = tokens[tokensPos-1];
158                     seen = merge_argv(tokens, 2, tokensPos-1);
159                 } else {
160                     seen = merge_argv(tokens, 2, tokensPos);
161                 }
162             } else {
163                 seen = merge_argv(tokens, 2, tokensPos);
164             }
165         }
166         if(status && !stricmp(status, "suspended")) {
167             flags |= DB_CHANUSER_SUSPENDED;
168         }
169         if(seen) {
170             if(!stricmp(seen, "here"))
171                 seen_time = 0;
172             else if(stricmp(seen, "never"))
173                 seen_time = strToTime(user, seen);
174         }
175         seen_time = now - seen_time;
176         //we've collected all information now. synchronize the user (use the higher access if the user is already added)
177         MYSQL_RES *res;
178         MYSQL_ROW row;
179         int userid;
180         printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(username));
181         res = mysql_use();
182         if ((row = mysql_fetch_row(res)) != NULL) {
183             userid = atoi(row[0]);
184             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);
185         } else if(!stricmp(user->nick, "chanserv")) {
186             printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(username));
187             userid = (int) mysql_insert_id(mysql_conn);
188             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);
189         } else {
190             //lookup auth
191             struct neonserv_cmd_chanservsync_auth_cache *cache = malloc(sizeof(*cache));
192             if (!cache) {
193                 perror("malloc() failed");
194                 return;
195             }
196             cache->client = neonserv_cmd_chanservsync_used->client;
197             cache->textclient = neonserv_cmd_chanservsync_used->textclient;
198             cache->user = neonserv_cmd_chanservsync_used->user;
199             cache->chan = neonserv_cmd_chanservsync_used->chan;
200             cache->caccess = caccess;
201             cache->seen = seen_time;
202             cache->flags = flags;
203             lookup_authname(username, neonserv_cmd_chanservsync_auth_lookup, cache);
204         }
205     }
206 }
207
208 static void neonserv_cmd_chanservsync_free_cache() {
209     free(neonserv_cmd_chanservsync_used->botnick);
210     free(neonserv_cmd_chanservsync_used);
211     unbind_privnotice(neonserv_cmd_chanservsync_notice_listener);
212     neonserv_cmd_chanservsync_used = NULL;
213 }
214
215 static AUTHLOOKUP_CALLBACK(neonserv_cmd_chanservsync_auth_lookup) {
216     struct neonserv_cmd_chanservsync_auth_cache *cache = data;
217     if(exists) {
218         printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(auth));
219         int userid = (int) mysql_insert_id(mysql_conn);
220         neonserv_cmd_chanservsync_synchronize_user(cache->client, cache->textclient, cache->user, cache->chan, auth, userid, cache->caccess, cache->seen, cache->flags, 1);
221     }
222     free(cache);
223 }
224
225 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) {
226     //just sync the user with the given userid with the providet information
227     if(caccess == 500) caccess = 499;
228     if(new) {
229         //just add
230         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);
231     } else {
232         MYSQL_RES *res;
233         MYSQL_ROW row;
234         //check if already added
235         printf_mysql_query("SELECT `chanuser_access`, `chanuser_id`, `chanuser_seen` FROM `chanusers` WHERE `chanuser_cid` = '%d' AND `chanuser_uid` = '%d'", chan->channel_id, userid);
236         res = mysql_use();
237         if ((row = mysql_fetch_row(res)) != NULL) {
238             //clvl
239             if(atoi(row[0]) >= caccess) return;
240             if(atol(row[2]) > seen) seen = atol(row[2]);
241             printf_mysql_query("UPDATE `chanusers` SET `chanuser_access` = '%d', `chanuser_seen` = '%lu' WHERE `chanuser_id` = '%s'", caccess, (unsigned long) seen, row[1]);
242         } else 
243             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);
244     }
245     reply(textclient, user, "NS_CHANSERVSYNC_SYNCHRONIZED", username, caccess);
246 }