+static void neonserv_cmd_set_setting(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, struct Event *event, int setting, char *args) {
+ char *value;
+ char nameBuf[64];
+ //get current value
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+ printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", channel_settings[setting].chanfield, chan->channel_id);
+ res = mysql_use();
+ row = mysql_fetch_row(res);
+ if(row[0] == NULL) {
+ printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", channel_settings[setting].chanfield);
+ res = mysql_use();
+ row = mysql_fetch_row(res);
+ }
+ value = row[0];
+ if(args) {
+ //change the channel setting
+ //check the new argument
+ int valid = channel_settings[setting].valid;
+ if(valid & NS_VALID_STRING) {
+ if(!strcmp(args, "*")) {
+ args = "";
+ }
+ }
+ if(valid & NS_VALID_ACCESS) {
+ int caccess = atoi(args);
+ int max = ((valid & NS_VALID_NO501) ? 500 : 501);
+ if(caccess < 0 || caccess > max) {
+ reply(getTextBot(), user, "NS_INVALID_ACCESS", caccess);
+ return;
+ }
+ int uaccess = getChannelAccess(user, chan, 0);
+ if(uaccess == 500) uaccess++;
+ if(atoi(value) > uaccess) {
+ if(isGodMode(user)) {
+ event->flags |= CMDFLAG_OPLOG;
+ } else {
+ reply(getTextBot(), user, "NS_SET_CANNOT_SET");
+ return;
+ }
+ }
+ if(caccess > uaccess) {
+ if(isGodMode(user)) {
+ event->flags |= CMDFLAG_OPLOG;
+ } else {
+ reply(getTextBot(), user, "NS_SET_BADLEVEL");
+ return;
+ }
+ }
+ sprintf(nameBuf, "%d", caccess);
+ args = nameBuf;
+ }
+ if(valid & NS_VALID_OPTIONS) {
+ int options = atoi((char *) channel_settings[setting].parameter);
+ int coption = atoi(args);
+ if(coption < 0 || coption >= options) {
+ reply(getTextBot(), user, "NS_SET_INVALID_OPTION", coption);
+ int i;
+ int nameBufPos = 0;
+ if(valid & NS_HAS_OPT) {
+ for(i = 0; i < options; i++) {
+ sprintf(nameBuf, "NS_SET_OPTION_%s_%d", channel_settings[setting].setting, i);
+ reply(getTextBot(), user, "\002%d\002 - %s", i, get_language_string(user, nameBuf));
+ }
+ } else {
+ for(i = 0; i < options; i++) {
+ nameBufPos += sprintf(nameBuf + nameBufPos, "\002%d\002, ", i);
+ }
+ if(nameBufPos) {
+ nameBuf[nameBufPos-2] = '\0';
+ reply(getTextBot(), user, nameBuf);
+ }
+ }
+ return;
+ }
+ }
+ if(valid & NS_VALID_NUMERIC) {
+ sprintf(nameBuf, "%d", atoi(args));
+ args = nameBuf;
+ }
+ if(valid & NS_VALID_BOOLEAN) {
+ if(!strcmp(args, "0") || !stricmp(args, "off") || !stricmp(args, get_language_string(user, "NS_SET_OFF"))) {
+ args = "0";
+ } else if(!strcmp(args, "1") || !stricmp(args, "on") || !stricmp(args, get_language_string(user, "NS_SET_ON"))) {
+ args = "1";
+ } else {
+ reply(getTextBot(), user, "NS_SET_INVALID_BOOLEAN", args);
+ return;
+ }
+ }
+ //valid - set it
+ value = args;
+ printf_mysql_query("UPDATE `channels` SET `%s` = '%s' WHERE `channel_id` = '%d'", channel_settings[setting].chanfield, escape_string(value), chan->channel_id);
+ logEvent(event);
+ }
+ reply(getTextBot(), user, "\002%s\002 %s", channel_settings[setting].setting, value);
+ if(channel_settings[setting].valid & NS_HAS_HELP) {
+ sprintf(nameBuf, "NS_SET_HELP_%s", channel_settings[setting].setting);
+ reply(getTextBot(), user, " %s", get_language_string(user, nameBuf));
+ }