*** VERSION 5.2.0 ***
[NeonServV5.git] / src / cmd_neonserv_topic.c
1 /* cmd_neonserv_topic.c - NeonServ v5.2
2  * Copyright (C) 2011  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "cmd_neonserv.h"
19
20 /*
21 * ADVANCEDTOPIC enabled
22 * argv[0]    topic id
23 * argv[1-*]  topic
24 *
25 * ADVANCEDTOPIC disabled
26 * argv[0-*]  topic
27 */
28
29 #define ADVANCEDTOPIC_MAXID 9
30
31 CMD_BIND(neonserv_cmd_topic) {
32     MYSQL_RES *res;
33     MYSQL_ROW row, default_row = NULL;
34     int advanced_topic, i;
35     char *newtopic;
36     char *a,*b;
37     
38     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);
39     res = mysql_use();
40     row = mysql_fetch_row(res);
41     if(!row[0] || !row[3] || !row[4]) {
42         printf_mysql_query("SELECT `channel_exttopic`, `channel_enftopic`, `channel_topicsnarf` FROM `channels` WHERE `channel_name` = 'defaults'");
43         default_row = mysql_fetch_row(mysql_use());
44     }
45     
46     if(row[0] == NULL) {
47         advanced_topic = atoi(default_row[0]);
48     } else
49         advanced_topic = atoi(row[0]);
50     if(argc == 0) {
51         //default topic!
52         putsock(client, "TOPIC %s :%s", chan->name, row[5]);
53         reply(getTextBot(), user, "NS_TOPIC_DONE", row[5]);
54         logEvent(event);
55         return;
56     }
57     int uaccess = getChannelAccess(user, chan, 0);
58     if(uaccess >= atoi((row[3] ? row[3] : default_row[1]))) {
59         //just set the topic
60         newtopic = merge_argv(argv, 0, argc);
61         if(uaccess >= atoi((row[4] ? row[4] : default_row[2]))) {
62             //set the default topic
63             printf_mysql_query("UPDATE `channels` SET `channel_defaulttopic` = '%s' WHERE `channel_id` = '%d'", escape_string(newtopic), chan->channel_id);
64         }
65         putsock(client, "TOPIC %s :%s", chan->name, newtopic);
66         reply(getTextBot(), user, "NS_TOPIC_DONE", newtopic);
67         logEvent(event);
68         return;
69     }
70     if(advanced_topic) {
71         char *advtopics[ADVANCEDTOPIC_MAXID];
72         int topic_id = 0;
73         topic_id = atoi(argv[0]);
74         if(!topic_id || topic_id > ADVANCEDTOPIC_MAXID) {
75             reply(getTextBot(), user, "NS_EXTTOPIC_INVALID_ID", argv[0]);
76             return;
77         }
78         //parse topics
79         i = 0;
80         b = row[1];
81         while((a = strstr(b, "\n")) != NULL) {
82             *a = '\0';
83             if(i == ADVANCEDTOPIC_MAXID-1) break;
84             advtopics[i++] = b;
85             b = a+1;
86         }
87         advtopics[i++] = b;
88         for(;i < ADVANCEDTOPIC_MAXID;i++)
89             advtopics[i] = "";
90         if(argc < 2) {
91             //just show the topic with this id
92             reply(getTextBot(), user, "NS_EXTTOPIC_TOPICID", topic_id, advtopics[topic_id-1]);
93             return;
94         }
95         newtopic = merge_argv(argv, 1, argc);
96         if(!strcmp(newtopic, "*")) 
97             newtopic = "";
98         advtopics[topic_id-1] = newtopic;
99         char topiclist[MAXLEN*2];
100         topiclist[0] = '\0';
101         int topiclistpos = 0;
102         for(i = 0; i < ADVANCEDTOPIC_MAXID; i++) {
103             if(topiclistpos + strlen(advtopics[i]) + 2 >= MAXLEN) break;
104             topiclistpos += sprintf(topiclist+topiclistpos, (i ? "\n%s" : "%s"), advtopics[i]);
105         }
106         printf_mysql_query("UPDATE `channels` SET `channel_exttopic_topic` = '%s' WHERE `channel_id` = '%d'", escape_string(topiclist), chan->channel_id);
107         //now build the new topic and set it...
108         topiclistpos = 0;
109         b = row[2];
110         char *topicpart, *debugbb;
111         while((a = strstr(b, "%")) != NULL) {
112             *a = '\0';
113             if(isdigit(a[1]) && a[1] - 48 > 0) {
114                 topicpart = advtopics[a[1] - 49];
115                 if(isdigit(topicpart[0]) && isdigit(b[strlen(b)-1])) 
116                     debugbb = "\002\002"; //double bold to prevent following digits used as color code
117                 else
118                     debugbb = "";
119                 topiclistpos += sprintf(topiclist + topiclistpos, "%s%s%s", b, debugbb, topicpart);
120                 b = a+2;
121             } else {
122                 topiclistpos += sprintf(topiclist + topiclistpos, "%s%%", b);
123                 b = a+1;
124             }
125         }
126         topiclistpos += sprintf(topiclist + topiclistpos, "%s", b);
127         if(topiclistpos > MAXLEN)
128             topiclist[MAXLEN] = '\0';
129         putsock(client, "TOPIC %s :%s", chan->name, topiclist);
130         reply(getTextBot(), user, "NS_TOPIC_DONE", topiclist);
131         logEvent(event);
132     } else {
133         newtopic = merge_argv(argv, 0, argc);
134         char topiclist[MAXLEN*2];
135         topiclist[0] = '\0';
136         int topiclistpos = 0;
137         b = row[2];
138         char *debugbb;
139         while((a = strstr(b, "*")) != NULL) {
140             *a = '\0';
141             if(isdigit(newtopic[0]) && isdigit(b[strlen(b)-1])) 
142                 debugbb = "\002\002"; //double bold to prevent following digits used as color code
143             else
144                 debugbb = "";
145             topiclistpos += sprintf(topiclist + topiclistpos, "%s%s%s", b, debugbb, newtopic);
146             b = a+1;
147         }
148         topiclistpos += sprintf(topiclist + topiclistpos, "%s", b);
149         if(topiclistpos > MAXLEN)
150             topiclist[MAXLEN] = '\0';
151         putsock(client, "TOPIC %s :%s", chan->name, topiclist);
152         reply(getTextBot(), user, "NS_TOPIC_DONE", topiclist);
153         logEvent(event);
154     }
155 }
156
157 #undef ADVANCEDTOPIC_MAXID