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