1 /* cmd_neonserv_chanservsync.c - NeonServ v5.3
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "cmd_neonserv.h"
24 #define CHANSERVSYNC_END_TIMEOUT 5
26 static void neonserv_cmd_chanservsync_notice_listener(struct UserNode *user, struct UserNode *target, char *message);
27 static void neonserv_cmd_chanservsync_free_cache();
28 static AUTHLOOKUP_CALLBACK(neonserv_cmd_chanservsync_auth_lookup);
29 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);
31 struct neonserv_cmd_chanservsync_cache {
32 struct ClientSocket *client, *textclient;
33 struct UserNode *user;
34 struct ChanNode *chan;
39 struct neonserv_cmd_chanservsync_auth_cache {
40 struct ClientSocket *client, *textclient;
41 struct UserNode *user;
42 struct ChanNode *chan;
48 struct neonserv_cmd_chanservsync_cache *neonserv_cmd_chanservsync_used = NULL;
49 const char* neonserv_cmd_chanservsync_supported[] = {"ChanServ", NULL};
51 CMD_BIND(neonserv_cmd_chanservsync) {
52 if(neonserv_cmd_chanservsync_used && time(0) - neonserv_cmd_chanservsync_used->last_response < CHANSERVSYNC_END_TIMEOUT) {
53 reply(getTextBot(), user, "NS_CHANSERVSYNC_INUSE");
56 if(neonserv_cmd_chanservsync_used) {
57 neonserv_cmd_chanservsync_free_cache();
59 char *botnick = "ChanServ";
62 if(argv[0][0] == '!') {
73 for(tmp = user->auth; *tmp; tmp++)
74 seed = (seed * 0xEECE66DL ^ ((*tmp << 24) | (*tmp << 16) | (*tmp << 8) | *tmp));
75 for(tmp = chan->name; *tmp; tmp++)
76 seed = (seed * 0xEECE66DL ^ ((*tmp << 24) | (*tmp << 16) | (*tmp << 8) | *tmp));
77 for(tmp = botnick; *tmp; tmp++)
78 seed = (seed * 0xEECE66DL ^ ((*tmp << 24) | (*tmp << 16) | (*tmp << 8) | *tmp));
79 sprintf(synckey, "!%08x!", seed);
80 if(strcmp(synckey, key)) {
82 const char **supp = neonserv_cmd_chanservsync_supported;
84 if(!stricmp(*supp, botnick)) {
91 reply(getTextBot(), user, "NS_CHANSERVSYNC_UNSUPPORTED", botnick, client->user->nick);
93 reply(getTextBot(), user, "NS_CHANSERVSYNC_KEY", client->user->nick, botnick, botnick, synckey);
96 struct neonserv_cmd_chanservsync_cache *cache = malloc(sizeof(*cache));
98 perror("malloc() failed");
101 cache->client = client;
102 cache->textclient = getTextBot();
105 cache->botnick = strdup(botnick);
106 cache->last_response = time(0);
107 neonserv_cmd_chanservsync_used = cache;
108 putsock(client, "PRIVMSG %s :users %s", botnick, chan->name);
109 bind_privnotice(neonserv_cmd_chanservsync_notice_listener);
110 reply(getTextBot(), user, "NS_CHANSERVSYNC_SYNCHRONIZING", chan->name, botnick);
114 static void neonserv_cmd_chanservsync_notice_listener(struct UserNode *user, struct UserNode *target, char *message) {
115 if(neonserv_cmd_chanservsync_used && neonserv_cmd_chanservsync_used->client->user == target && !stricmp(user->nick, neonserv_cmd_chanservsync_used->botnick)) {
116 //we've got a notice from our bot...
117 //let's try parsing it....
119 char *tokens[MAXLEN];
121 while(*p == ' ') //skip leading spaces (airb0t)
126 if(*q < 32) *q = ' ';
129 while((q = strstr(p, " "))) {
133 } while(*q == ' ' || *q == '-');
135 tokens[tokensPos++] = p;
140 tokens[tokensPos++] = p;
145 //maybe a chip-like userlist
146 if(tokens[0][0] == '@') {
148 username = &tokens[0][1];
149 } else if(tokens[0][0] == '+') {
151 username = &tokens[0][1];
154 } else if(tokensPos >= 2) {
155 if(atoi(tokens[0]) > 0) {
156 caccess = atoi(tokens[0]);
157 username = tokens[1];
159 caccess = atoi(tokens[1]);
160 username = tokens[0];
164 if(caccess < 1 || caccess > 500) return;
166 time_t now = time(0);
167 time_t seen_time = now; //now - now = 0 (never)
168 neonserv_cmd_chanservsync_used->last_response = now;
169 if(strlen(username) < 3) return;
170 //ok we have access and username... maybe there is something else we can parse???
174 if(!stricmp("normal", tokens[2]) || !stricmp("suspended", tokens[2]) || !stricmp("bot", tokens[2])) {
177 seen = merge_argv(tokens, 3, tokensPos);
179 } else if (tokensPos > 3) {
180 if(!stricmp("normal", tokens[tokensPos-1]) || !stricmp("suspended", tokens[tokensPos-1]) || !stricmp("bot", tokens[tokensPos-1])) {
181 status = tokens[tokensPos-1];
182 seen = merge_argv(tokens, 2, tokensPos-1);
184 seen = merge_argv(tokens, 2, tokensPos);
187 seen = merge_argv(tokens, 2, tokensPos);
190 if(status && !stricmp(status, "suspended")) {
191 flags |= DB_CHANUSER_SUSPENDED;
194 if(!stricmp(seen, "here"))
196 else if(stricmp(seen, "never"))
197 seen_time = strToTime(user, seen);
199 seen_time = now - seen_time;
200 //we've collected all information now. synchronize the user (use the higher access if the user is already added)
204 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(username));
206 if ((row = mysql_fetch_row(res)) != NULL) {
207 userid = atoi(row[0]);
208 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);
209 } else if(!stricmp(user->nick, "chanserv")) {
210 printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(username));
211 userid = (int) mysql_insert_id(mysql_conn);
212 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);
215 struct neonserv_cmd_chanservsync_auth_cache *cache = malloc(sizeof(*cache));
217 perror("malloc() failed");
220 cache->client = neonserv_cmd_chanservsync_used->client;
221 cache->textclient = neonserv_cmd_chanservsync_used->textclient;
222 cache->user = neonserv_cmd_chanservsync_used->user;
223 cache->chan = neonserv_cmd_chanservsync_used->chan;
224 cache->caccess = caccess;
225 cache->seen = seen_time;
226 cache->flags = flags;
227 lookup_authname(username, neonserv_cmd_chanservsync_auth_lookup, cache);
232 static void neonserv_cmd_chanservsync_free_cache() {
233 free(neonserv_cmd_chanservsync_used->botnick);
234 free(neonserv_cmd_chanservsync_used);
235 unbind_privnotice(neonserv_cmd_chanservsync_notice_listener);
236 neonserv_cmd_chanservsync_used = NULL;
239 static AUTHLOOKUP_CALLBACK(neonserv_cmd_chanservsync_auth_lookup) {
240 struct neonserv_cmd_chanservsync_auth_cache *cache = data;
242 printf_mysql_query("INSERT INTO `users` (`user_user`) VALUES ('%s')", escape_string(auth));
243 int userid = (int) mysql_insert_id(mysql_conn);
244 neonserv_cmd_chanservsync_synchronize_user(cache->client, cache->textclient, cache->user, cache->chan, auth, userid, cache->caccess, cache->seen, cache->flags, 1);
249 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) {
250 //just sync the user with the given userid with the providet information
251 if(caccess == 500) caccess = 499;
254 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);
258 //check if already added
259 printf_mysql_query("SELECT `chanuser_access`, `chanuser_id`, `chanuser_seen` FROM `chanusers` WHERE `chanuser_cid` = '%d' AND `chanuser_uid` = '%d'", chan->channel_id, userid);
261 if ((row = mysql_fetch_row(res)) != NULL) {
263 if(atoi(row[0]) >= caccess) return;
264 if(atol(row[2]) > seen) seen = atol(row[2]);
265 printf_mysql_query("UPDATE `chanusers` SET `chanuser_access` = '%d', `chanuser_seen` = '%lu' WHERE `chanuser_id` = '%s'", caccess, (unsigned long) seen, row[1]);
267 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);
269 reply(textclient, user, "NS_CHANSERVSYNC_SYNCHRONIZED", username, caccess);