removed some doubled checks & added cmd_topic with ADVANCEDTOPIC
[NeonServV5.git] / cmd_neonserv_topic.c
1
2 /*
3 * ADVANCEDTOPIC enabled
4 * argv[0]    topic id
5 * argv[1-*]  topic
6 *
7 * ADVANCEDTOPIC disabled
8 * argv[0-*]  topic
9 */
10
11 #define ADVANCEDTOPIC_MAXID 9
12
13 static CMD_BIND(neonserv_cmd_topic) {
14     check_mysql();
15     MYSQL_RES *res;
16     MYSQL_ROW row, default_row = NULL;
17     int advanced_topic, i;
18     char *newtopic;
19     char *a,*b;
20     
21     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);
22     res = mysql_use();
23     row = mysql_fetch_row(res);
24     if(!row[0] || !row[3] || !row[4]) {
25         printf_mysql_query("SELECT `channel_exttopic`, `channel_enftopic`, `channel_topicsnarf` FROM `channels` WHERE `channel_name` = 'defaults'");
26         default_row = mysql_fetch_row(mysql_use());
27     }
28     
29     if(row[0] == NULL) {
30         advanced_topic = atoi(default_row[0]);
31     } else
32         advanced_topic = atoi(row[0]);
33     if(argc == 0) {
34         //default topic!
35         putsock(client, "TOPIC %s :%s", chan->name, row[5]);
36         reply(getTextBot(), user, "NS_TOPIC_DONE", row[5]);
37         return;
38     }
39     int uaccess = getChannelAccess(user, chan, 1);
40     if(uaccess >= atoi((row[3] ? row[3] : default_row[1]))) {
41         //just set the topic
42         newtopic = merge_argv(argv, 0, argc);
43         if(uaccess >= atoi((row[4] ? row[4] : default_row[2]))) {
44             //set the default topic
45             printf_mysql_query("UPDATE `channels` SET `channel_defaulttopic` = '%s' WHERE `channel_id` = '%d'", escape_string(newtopic), chan->channel_id);
46         }
47         putsock(client, "TOPIC %s :%s", chan->name, newtopic);
48         reply(getTextBot(), user, "NS_TOPIC_DONE", newtopic);
49         return;
50     }
51     if(advanced_topic) {
52         char *advtopics[ADVANCEDTOPIC_MAXID];
53         int topic_id = 0;
54         topic_id = atoi(argv[0]);
55         if(!topic_id || topic_id > ADVANCEDTOPIC_MAXID) {
56             reply(getTextBot(), user, "NS_EXTTOPIC_INVALID_ID", argv[0]);
57             return;
58         }
59         //parse topics
60         i = 0;
61         b = row[1];
62         while((a = strstr(b, "\n")) != NULL) {
63             *a = '\0';
64             if(i == ADVANCEDTOPIC_MAXID-1) break;
65             advtopics[i++] = b;
66             b = a+1;
67         }
68         advtopics[i++] = b;
69         for(;i < ADVANCEDTOPIC_MAXID;i++)
70             advtopics[i] = "";
71         if(argc < 2) {
72             //just show the topic with this id
73             reply(getTextBot(), user, "NS_EXTTOPIC_TOPICID", topic_id, advtopics[topic_id-1]);
74             return;
75         }
76         newtopic = merge_argv(argv, 1, argc);
77         advtopics[topic_id-1] = newtopic;
78         char topiclist[MAXLEN*2];
79         topiclist[0] = '\0';
80         int topiclistpos = 0;
81         for(i = 0; i < ADVANCEDTOPIC_MAXID; i++) {
82             if(topiclistpos + strlen(advtopics[i]) + 2 >= MAXLEN) break;
83             topiclistpos += sprintf(topiclist+topiclistpos, (i ? "\n%s" : "%s"), advtopics[i]);
84         }
85         printf_mysql_query("UPDATE `channels` SET `channel_exttopic_topic` = '%s' WHERE `channel_id` = '%d'", escape_string(topiclist), chan->channel_id);
86         //now build the new topic and set it...
87         topiclistpos = 0;
88         b = row[2];
89         while((a = strstr(b, "%")) != NULL) {
90             *a = '\0';
91             if(isdigit(a[1]) && a[1] - 48 > 0) {
92                 topiclistpos += sprintf(topiclist + topiclistpos, "%s%s", b, advtopics[a[1] - 49]);
93                 b = a+2;
94             } else {
95                 topiclistpos += sprintf(topiclist + topiclistpos, "%s%%", b);
96                 b = a+1;
97             }
98         }
99         topiclistpos += sprintf(topiclist + topiclistpos, "%s", b);
100         if(topiclistpos > MAXLEN)
101             topiclist[MAXLEN] = '\0';
102         putsock(client, "TOPIC %s :%s", chan->name, topiclist);
103         reply(getTextBot(), user, "NS_TOPIC_DONE", topiclist);
104     } else {
105         newtopic = merge_argv(argv, 0, argc);
106         char topiclist[MAXLEN*2];
107         topiclist[0] = '\0';
108         int topiclistpos = 0;
109          b = row[2];
110         while((a = strstr(b, "*")) != NULL) {
111             *a = '\0';
112             topiclistpos += sprintf(topiclist + topiclistpos, "%s%s", b, newtopic);
113             b = a+1;
114         }
115         topiclistpos += sprintf(topiclist + topiclistpos, "%s", b);
116         if(topiclistpos > MAXLEN)
117             topiclist[MAXLEN] = '\0';
118         putsock(client, "TOPIC %s :%s", chan->name, topiclist);
119         reply(getTextBot(), user, "NS_TOPIC_DONE", topiclist);
120     }
121 }
122
123 #undef ADVANCEDTOPIC_MAXID