7c218eb3218fc2f8571fb17cd7812db7148fada6
[NeonServV5.git] / src / cmd_neonserv_command.c
1 /* cmd_neonserv_command.c - NeonServ v5.1
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 * argv[0-1]     command
22 */
23 static int neonserv_cmd_command_chanaccess(struct cmd_binding *cbind, struct ChanNode *chan);
24 static int neonserv_cmd_command_operaccess(struct cmd_binding *cbind);
25
26 CMD_BIND(neonserv_cmd_command) {
27     char *ident;
28     MYSQL_RES *res;
29     MYSQL_ROW row;
30     struct cmd_binding *cbind = find_cmd_binding(client->botid, argv[0]);
31     if (!cbind) {
32         reply(getTextBot(), user, "NS_UNBIND_NOT_FOUND", argv[0]);
33         return;
34     }
35     ident = argv[0];
36     char parameters[MAXLEN];
37     if(cbind->paramcount) {
38         int i, parampos = 0;
39         for(i = 0; i < cbind->paramcount; i++) {
40             parampos += sprintf(parameters + parampos, (i ? " %s" : "%s"), cbind->parameters[i]);
41         }
42     } else
43         parameters[0] = '\0';
44     reply(getTextBot(), user, "NS_COMMAND_BINDING", cbind->cmd, cbind->func->name, parameters);
45     if(chan)
46         reply(getTextBot(), user, "NS_COMMAND_ACCESS", neonserv_cmd_command_chanaccess(cbind, chan), neonserv_cmd_command_operaccess(cbind));
47     printf_mysql_query("SELECT `user_lang` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
48     res = mysql_use();
49     char *lang;
50     if ((row = mysql_fetch_row(res)) != NULL)
51         lang = row[0];
52     else
53         lang = "en";
54     printf_mysql_query("SELECT `text` FROM `help` WHERE `lang` = '%s' AND `ident` = '%s'", escape_string(lang), escape_string(ident));
55     res = mysql_use();
56     if ((row = mysql_fetch_row(res)) == NULL) {
57         if(stricmp(lang, "en")) {
58             printf_mysql_query("SELECT `text` FROM `help` WHERE `lang` = 'en' AND `ident` = '%s'", escape_string(ident));
59             res = mysql_use();
60         }
61         if ((row = mysql_fetch_row(res)) == NULL) {
62             printf_mysql_query("SELECT `text` FROM `help` WHERE `lang` = '%s' AND `ident` = '%s'", escape_string(lang), escape_string(cbind->func->name));
63             res = mysql_use();
64             if ((row = mysql_fetch_row(res)) == NULL) {
65                 if(stricmp(lang, "en")) {
66                     printf_mysql_query("SELECT `text` FROM `help` WHERE `lang` = 'en' AND `ident` = '%s'", escape_string(cbind->func->name));
67                     res = mysql_use();
68                 }
69                 if ((row = mysql_fetch_row(res)) == NULL) {
70                     return;
71                 }
72             }
73         }
74     }
75     char sendBuf[MAXLEN];
76     int sendBufPos = 0;
77     int i;
78     for(i = 0; i < strlen(row[0]); i++) {
79         switch(row[0][i]) {
80             case '\n':
81                 if(sendBufPos) {
82                     sendBuf[sendBufPos] = '\0';
83                     reply(getTextBot(), user, "%s", sendBuf);
84                     sendBufPos = 0;
85                 }
86                 break;
87             case '$':
88                 switch(row[0][i+1]) {
89                     case 'b':
90                         sendBuf[sendBufPos++] = '\002';
91                         i++;
92                         break;
93                     case 'k':
94                         sendBuf[sendBufPos++] = '\003';
95                         i++;
96                         break;
97                     case 'u':
98                         sendBuf[sendBufPos++] = '\031';
99                         i++;
100                         break;
101                     case 'C':
102                     case 'S':
103                         sendBufPos += sprintf(sendBuf + sendBufPos, "%s", client->user->nick);
104                         i++;
105                         break;
106                     default:
107                         sendBuf[sendBufPos++] = '$';
108                         break;
109                 }
110                 break;
111             default:
112                 sendBuf[sendBufPos++] = row[0][i];
113                 break;
114         }
115     }
116     if(sendBufPos) {
117         sendBuf[sendBufPos] = '\0';
118         reply(getTextBot(), user, "%s", sendBuf);
119         sendBufPos = 0;
120     }
121 }
122
123 static int neonserv_cmd_command_chanaccess(struct cmd_binding *cbind, struct ChanNode *chan) {
124     char access_list[256];
125     int access_pos = 0;
126     int access_count = 0;
127     int minaccess = 0;
128     char *str_a, *str_b = cbind->func->channel_access, *str_c;
129     if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
130         str_b = cbind->channel_access;
131     access_list[0] = '\0';
132     if(str_b) {
133         str_c = strdup(str_b);
134         str_b = str_c;
135         while((str_a = str_b)) {
136             str_b = strstr(str_a, ",");
137             if(str_b) {
138                 *str_b = '\0';
139                 str_b++;
140             }
141             if(*str_a == '#') {
142                 str_a++;
143                 access_pos += sprintf(access_list+access_pos, (access_pos ? ", `%s`" : "`%s`"), str_a);
144                 access_count++;
145             } else {
146                if(atoi(str_a) > minaccess)
147                      minaccess = atoi(str_a);
148             }
149         }
150         free(str_c);
151     }
152     if(access_count) {
153         MYSQL_RES *res;
154         MYSQL_ROW row, defaults = NULL;
155         printf_mysql_query("SELECT %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
156         res = mysql_use();
157         if ((row = mysql_fetch_row(res)) != NULL) {
158             int i, caccess;
159             for(i = 0; i < access_count; i++) {
160                 if(!row[i] && !defaults) {
161                     printf_mysql_query("SELECT %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
162                     defaults = mysql_fetch_row(mysql_use());
163                 }
164                 caccess = (row[i] ? atoi(row[i]) : atoi(defaults[i]));
165                 if(caccess > minaccess)
166                      minaccess = caccess;
167             }
168         }
169     }
170     return minaccess;
171 }
172
173 static int neonserv_cmd_command_operaccess(struct cmd_binding *cbind) {
174     return ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
175 }