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);
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....
100 char *tokens[MAXLEN];
102 while(*p == ' ') //skip leading spaces (airb0t)
107 if(*q < 32) *q = ' ';
110 while((q = strstr(p, " "))) {
116 tokens[tokensPos++] = p;
121 tokens[tokensPos++] = p;
126 //maybe a chip-like userlist
127 if(tokens[0][0] == '@') {
129 username = &tokens[0][1];
130 } else if(tokens[0][0] == '+') {
132 username = &tokens[0][1];
135 } else if(tokensPos >= 2) {
136 caccess = atoi(tokens[0]);
137 username = tokens[1];
140 if(caccess < 1 || caccess > 500) return;
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???
150 if(!stricmp("normal", tokens[2]) || !stricmp("suspended", tokens[2]) || !stricmp("bot", tokens[2])) {
153 seen = merge_argv(tokens, 3, tokensPos);
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);
160 seen = merge_argv(tokens, 2, tokensPos);
163 seen = merge_argv(tokens, 2, tokensPos);
166 if(status && !stricmp(status, "suspended")) {
167 flags |= DB_CHANUSER_SUSPENDED;
170 if(!stricmp(seen, "here"))
172 else if(stricmp(seen, "never"))
173 seen_time = strToTime(user, seen);
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)
180 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(username));
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);
191 struct neonserv_cmd_chanservsync_auth_cache *cache = malloc(sizeof(*cache));
193 perror("malloc() failed");
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);
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;
215 static AUTHLOOKUP_CALLBACK(neonserv_cmd_chanservsync_auth_lookup) {
216 struct neonserv_cmd_chanservsync_auth_cache *cache = data;
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);
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;
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);
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);
237 if ((row = mysql_fetch_row(res)) != NULL) {
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]);
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);
245 reply(textclient, user, "NS_CHANSERVSYNC_SYNCHRONIZED", username, caccess);