6 #define CHANSERVSYNC_END_TIMEOUT 5
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);
13 struct neonserv_cmd_chanservsync_cache {
14 struct ClientSocket *client, *textclient;
15 struct UserNode *user;
16 struct ChanNode *chan;
21 struct neonserv_cmd_chanservsync_auth_cache {
22 struct ClientSocket *client, *textclient;
23 struct UserNode *user;
24 struct ChanNode *chan;
30 struct neonserv_cmd_chanservsync_cache *neonserv_cmd_chanservsync_used = NULL;
31 const char* neonserv_cmd_chanservsync_supported[] = {"ChanServ", NULL};
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");
38 if(neonserv_cmd_chanservsync_used) {
39 neonserv_cmd_chanservsync_free_cache();
41 char *botnick = "ChanServ";
44 if(argv[0][0] == '!') {
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)) {
64 const char **supp = neonserv_cmd_chanservsync_supported;
66 if(!stricmp(*supp, botnick)) {
73 reply(getTextBot(), user, "NS_CHANSERVSYNC_UNSUPPORTED", botnick, client->user->nick);
75 reply(getTextBot(), user, "NS_CHANSERVSYNC_KEY", client->user->nick, botnick, botnick, synckey);
78 struct neonserv_cmd_chanservsync_cache *cache = malloc(sizeof(*cache));
80 perror("malloc() failed");
83 cache->client = client;
84 cache->textclient = getTextBot();
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);
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....
101 char *tokens[MAXLEN];
103 while(*p == ' ') //skip leading spaces (airb0t)
108 if(*q < 32) *q = ' ';
111 while((q = strstr(p, " "))) {
117 tokens[tokensPos++] = p;
122 tokens[tokensPos++] = p;
127 //maybe a chip-like userlist
128 if(tokens[0][0] == '@') {
130 username = &tokens[0][1];
131 } else if(tokens[0][0] == '+') {
133 username = &tokens[0][1];
136 } else if(tokensPos >= 2) {
137 caccess = atoi(tokens[0]);
138 username = tokens[1];
141 if(caccess < 1 || caccess > 500) return;
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???
151 if(!stricmp("normal", tokens[2]) || !stricmp("suspended", tokens[2]) || !stricmp("bot", tokens[2])) {
154 seen = merge_argv(tokens, 3, tokensPos);
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);
161 seen = merge_argv(tokens, 2, tokensPos);
164 seen = merge_argv(tokens, 2, tokensPos);
167 if(status && !stricmp(status, "suspended")) {
168 flags |= DB_CHANUSER_SUSPENDED;
171 if(!stricmp(seen, "here"))
173 else if(stricmp(seen, "never"))
174 seen_time = strToTime(user, seen);
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)
181 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(username));
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);
192 struct neonserv_cmd_chanservsync_auth_cache *cache = malloc(sizeof(*cache));
194 perror("malloc() failed");
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);
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;
216 static AUTHLOOKUP_CALLBACK(neonserv_cmd_chanservsync_auth_lookup) {
217 struct neonserv_cmd_chanservsync_auth_cache *cache = data;
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);
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;
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);
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);
238 if ((row = mysql_fetch_row(res)) != NULL) {
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]);
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);
246 reply(textclient, user, "NS_CHANSERVSYNC_SYNCHRONIZED", username, caccess);