+
+/*
+* argv[0] - usermask
+* argv[1] - min access
+* argv[2] - max access
+*/
+static USERLIST_CALLBACK(neonserv_cmd_resync_userlist_lookup);
+static void neonserv_cmd_resync_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *usermask, int min_access, int max_access);
+
+struct neonserv_cmd_resync_cache {
+ struct ClientSocket *client, *textclient;
+ struct UserNode *user;
+ char *usermask;
+ int min_access;
+ int max_access;
+};
+
+static CMD_BIND(neonserv_cmd_resync) {
+ int min_access = 1, max_access = 500;
+ char *usermask = NULL;
+ if(argc > 0)
+ usermask = argv[0];
+ if(argc > 2) {
+ min_access = atoi(argv[1]);
+ max_access = atoi(argv[2]);
+ }
+ struct neonserv_cmd_resync_cache *cache = malloc(sizeof(*cache));
+ if (!cache) {
+ perror("malloc() failed");
+ return;
+ }
+ cache->client = client;
+ cache->textclient = getTextBot();
+ cache->user = user;
+ cache->usermask = (usermask ? strdup(usermask) : NULL);
+ cache->min_access = min_access;
+ cache->max_access = max_access;
+ get_userlist_with_invisible(chan, neonserv_cmd_resync_userlist_lookup, cache);
+}
+
+static USERLIST_CALLBACK(neonserv_cmd_resync_userlist_lookup) {
+ struct neonserv_cmd_resync_cache *cache = data;
+ neonserv_cmd_resync_async1(cache->client, cache->textclient, cache->user, chan, cache->usermask, cache->min_access, cache->max_access);
+ if(cache->usermask)
+ free(cache->usermask);
+ free(cache);
+}
+
+static void neonserv_cmd_resync_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *usermask, int min_access, int max_access) {
+ MYSQL_RES *res;
+ MYSQL_ROW row, defaults;
+ int i;
+ int resync_op = 1;
+ int resync_voice = 1;
+ if(usermask && usermask[0] == '@') {
+ resync_voice = 0;
+ usermask++;
+ } else if(usermask && usermask[0] == '+') {
+ resync_op = 0;
+ usermask++;
+ }
+ struct ChanUser *chanuser;
+ int db_enfops, db_enfvoice;
+ printf_mysql_query("SELECT `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
+ row = mysql_fetch_row(mysql_use());
+ if(row[0] == NULL || row[1] == NULL) {
+ printf_mysql_query("SELECT `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_name` = 'defaults'");
+ defaults = mysql_fetch_row(mysql_use());
+ }
+ db_enfops = atoi((row[0] ? row[0] : defaults[0]));
+ db_enfvoice = atoi((row[1] ? row[1] : defaults[1]));
+ printf_mysql_query("SELECT `chanuser_access`, `user_user`, `chanuser_flags` FROM `chanusers` LEFT JOIN `users` ON `chanuser_uid` = `user_id` WHERE `chanuser_cid` = '%d' ORDER BY `chanuser_access` DESC, `user_user` ASC", chan->channel_id);
+ res = mysql_use();
+ char *db_users[mysql_num_rows(res)];
+ int db_access[mysql_num_rows(res)];
+ int db_flags[mysql_num_rows(res)];
+ int db_count = 0;
+ while ((row = mysql_fetch_row(res)) != NULL) {
+ db_users[db_count] = row[1];
+ db_access[db_count] = atoi(row[0]);
+ db_flags[db_count] = atoi(row[2]);
+ db_count++;
+ }
+ int caccess, cflags;
+ struct ModeBuffer *modeBuf;
+ modeBuf = initModeBuffer(client, chan);
+ for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
+ caccess = 0;
+ cflags = 0;
+ if((chanuser->user->flags & USERFLAG_ISAUTHED)) {
+ for(i = 0; i < db_count; i++) {
+ if(!stricmp(db_users[i], chanuser->user->auth)) {
+ caccess = db_access[i];
+ cflags = db_flags[i];
+ break;
+ }
+ }
+ }
+ if((usermask && match(usermask, row[1])) || caccess < min_access || caccess > max_access) continue;
+ if(caccess >= db_enfops) {
+ if(!(chanuser->flags & CHANUSERFLAG_OPPED) && resync_op)
+ modeBufferOp(modeBuf, chanuser->user->nick);
+ } else if(caccess >= db_enfvoice) {
+ if((chanuser->flags & CHANUSERFLAG_OPPED) && resync_op)
+ modeBufferDeop(modeBuf, chanuser->user->nick);
+ if(!(chanuser->flags & CHANUSERFLAG_VOICED) && resync_voice)
+ modeBufferVoice(modeBuf, chanuser->user->nick);
+ } else {
+ if((chanuser->flags & CHANUSERFLAG_OPPED) && resync_op)
+ modeBufferDeop(modeBuf, chanuser->user->nick);
+ if((chanuser->flags & CHANUSERFLAG_VOICED) && resync_voice)
+ modeBufferDevoice(modeBuf, chanuser->user->nick);
+ }
+
+ }
+ freeModeBuffer(modeBuf);
+ reply(textclient, user, "NS_RESYNC_DONE", chan->name);
+}