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