+
+/*
+* ADVANCEDTOPIC enabled
+* argv[0] topic id
+* argv[1-*] topic
+*
+* ADVANCEDTOPIC disabled
+* argv[0-*] topic
+*/
+
+#define ADVANCEDTOPIC_MAXID 9
+
+static CMD_BIND(neonserv_cmd_topic) {
+ check_mysql();
+ MYSQL_RES *res;
+ MYSQL_ROW row, default_row = NULL;
+ int advanced_topic, i;
+ char *newtopic;
+ char *a,*b;
+
+ printf_mysql_query("SELECT `channel_exttopic`, `channel_exttopic_topic`, `channel_topicmask`, `channel_enftopic`, `channel_topicsnarf`, `channel_defaulttopic` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
+ res = mysql_use();
+ row = mysql_fetch_row(res);
+ if(!row[0] || !row[3] || !row[4]) {
+ printf_mysql_query("SELECT `channel_exttopic`, `channel_enftopic`, `channel_topicsnarf` FROM `channels` WHERE `channel_name` = 'defaults'");
+ default_row = mysql_fetch_row(mysql_use());
+ }
+
+ if(row[0] == NULL) {
+ advanced_topic = atoi(default_row[0]);
+ } else
+ advanced_topic = atoi(row[0]);
+ if(argc == 0) {
+ //default topic!
+ putsock(client, "TOPIC %s :%s", chan->name, row[5]);
+ reply(getTextBot(), user, "NS_TOPIC_DONE", row[5]);
+ return;
+ }
+ int uaccess = getChannelAccess(user, chan, 1);
+ if(uaccess >= atoi((row[3] ? row[3] : default_row[1]))) {
+ //just set the topic
+ newtopic = merge_argv(argv, 0, argc);
+ if(uaccess >= atoi((row[4] ? row[4] : default_row[2]))) {
+ //set the default topic
+ printf_mysql_query("UPDATE `channels` SET `channel_defaulttopic` = '%s' WHERE `channel_id` = '%d'", escape_string(newtopic), chan->channel_id);
+ }
+ putsock(client, "TOPIC %s :%s", chan->name, newtopic);
+ reply(getTextBot(), user, "NS_TOPIC_DONE", newtopic);
+ return;
+ }
+ if(advanced_topic) {
+ char *advtopics[ADVANCEDTOPIC_MAXID];
+ int topic_id = 0;
+ topic_id = atoi(argv[0]);
+ if(!topic_id || topic_id > ADVANCEDTOPIC_MAXID) {
+ reply(getTextBot(), user, "NS_EXTTOPIC_INVALID_ID", argv[0]);
+ return;
+ }
+ //parse topics
+ i = 0;
+ b = row[1];
+ while((a = strstr(b, "\n")) != NULL) {
+ *a = '\0';
+ if(i == ADVANCEDTOPIC_MAXID-1) break;
+ advtopics[i++] = b;
+ b = a+1;
+ }
+ advtopics[i++] = b;
+ for(;i < ADVANCEDTOPIC_MAXID;i++)
+ advtopics[i] = "";
+ if(argc < 2) {
+ //just show the topic with this id
+ reply(getTextBot(), user, "NS_EXTTOPIC_TOPICID", topic_id, advtopics[topic_id-1]);
+ return;
+ }
+ newtopic = merge_argv(argv, 1, argc);
+ advtopics[topic_id-1] = newtopic;
+ char topiclist[MAXLEN*2];
+ topiclist[0] = '\0';
+ int topiclistpos = 0;
+ for(i = 0; i < ADVANCEDTOPIC_MAXID; i++) {
+ if(topiclistpos + strlen(advtopics[i]) + 2 >= MAXLEN) break;
+ topiclistpos += sprintf(topiclist+topiclistpos, (i ? "\n%s" : "%s"), advtopics[i]);
+ }
+ printf_mysql_query("UPDATE `channels` SET `channel_exttopic_topic` = '%s' WHERE `channel_id` = '%d'", escape_string(topiclist), chan->channel_id);
+ //now build the new topic and set it...
+ topiclistpos = 0;
+ b = row[2];
+ while((a = strstr(b, "%")) != NULL) {
+ *a = '\0';
+ if(isdigit(a[1]) && a[1] - 48 > 0) {
+ topiclistpos += sprintf(topiclist + topiclistpos, "%s%s", b, advtopics[a[1] - 49]);
+ b = a+2;
+ } else {
+ topiclistpos += sprintf(topiclist + topiclistpos, "%s%%", b);
+ b = a+1;
+ }
+ }
+ topiclistpos += sprintf(topiclist + topiclistpos, "%s", b);
+ if(topiclistpos > MAXLEN)
+ topiclist[MAXLEN] = '\0';
+ putsock(client, "TOPIC %s :%s", chan->name, topiclist);
+ reply(getTextBot(), user, "NS_TOPIC_DONE", topiclist);
+ } else {
+ newtopic = merge_argv(argv, 0, argc);
+ char topiclist[MAXLEN*2];
+ topiclist[0] = '\0';
+ int topiclistpos = 0;
+ b = row[2];
+ while((a = strstr(b, "*")) != NULL) {
+ *a = '\0';
+ topiclistpos += sprintf(topiclist + topiclistpos, "%s%s", b, newtopic);
+ b = a+1;
+ }
+ topiclistpos += sprintf(topiclist + topiclistpos, "%s", b);
+ if(topiclistpos > MAXLEN)
+ topiclist[MAXLEN] = '\0';
+ putsock(client, "TOPIC %s :%s", chan->name, topiclist);
+ reply(getTextBot(), user, "NS_TOPIC_DONE", topiclist);
+ }
+}
+
+#undef ADVANCEDTOPIC_MAXID
\ No newline at end of file