added cmd_modcmd
[NeonServV5.git] / src / cmd_global_modcmd.c
1 /* cmd_global_modcmd.c - NeonServ v5.3
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_global.h"
19
20 /*
21 * argv[0]     command
22 * argv[1]     setting
23 * argv[2]     value
24 */
25
26 static int global_cmd_modcmd_params(struct UserNode *user, struct cmd_binding *cbind, char *value);
27 static int global_cmd_modcmd_caccess(struct UserNode *user, struct cmd_binding *cbind, char *value);
28 static int global_cmd_modcmd_oaccess(struct UserNode *user, struct cmd_binding *cbind, char *value);
29
30 CMD_BIND(global_cmd_modcmd) {
31     MYSQL_RES *res;
32     MYSQL_ROW row;
33     struct cmd_binding *cbind = find_botwise_cmd_binding(client->botid, client->clientid, argv[0]);
34     if (!cbind) {
35         reply(getTextBot(), user, "NS_UNBIND_NOT_FOUND", argv[0]);
36         return;
37     }
38     int uaccess = 0;
39     printf_mysql_query("SELECT `user_access` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
40     res = mysql_use();
41     if ((row = mysql_fetch_row(res)) != NULL) {
42         uaccess = atoi(row[0]);
43     }
44     int gaccess = ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
45     if(gaccess > uaccess) {
46         reply(getTextBot(), user, "NS_MODCMD_OUTRANKED", cbind->cmd, gaccess);
47         return;
48     }
49     if(argc > 1) {
50         char *value;
51         if(argc > 2) {
52             value = merge_argv(argv, 2, argc);
53         } else
54             value = NULL;
55         int log_event = 0;
56         if(!stricmp(argv[1], "caccess")) log_event = global_cmd_modcmd_caccess(user, cbind, value);
57         else if(!stricmp(argv[1], "oaccess")) log_event = global_cmd_modcmd_oaccess(user, cbind, value);
58         else if(!stricmp(argv[1], "parameters")) log_event = global_cmd_modcmd_params(user, cbind, value);
59         else {
60             reply(getTextBot(), user, "NS_MODCMD_SETTING", argv[1]);
61         }
62         if(log_event) {
63             logEvent(event);
64         }
65     } else {
66         reply(getTextBot(), user, "NS_MODCMD_HEADER", cbind->cmd);
67         global_cmd_modcmd_params(user, cbind, NULL);
68         global_cmd_modcmd_caccess(user, cbind, NULL);
69         global_cmd_modcmd_oaccess(user, cbind, NULL);
70     }
71 }
72
73 static int global_cmd_modcmd_params(struct UserNode *user, struct cmd_binding *cbind, char *value) {
74     char parameters[MAXLEN];
75     int ret = 0;
76     if(cbind->paramcount) {
77         int i, parampos = 0;
78         for(i = 0; i < cbind->paramcount; i++) {
79             parampos += sprintf(parameters + parampos, (i ? " %s" : "%s"), cbind->parameters[i]);
80         }
81     } else
82         parameters[0] = '\0';
83     if(value) {
84         if(!strcmp(value, "*")) 
85             value = NULL;
86         bind_botwise_set_parameters(cbind->botid, cbind->clientid, cbind->cmd, value);
87         if(cbind->botid == 0)
88             printf_mysql_query("UPDATE `bot_binds` SET `parameters` = '%s' WHERE `botclass` = '0' AND `botid` = '%d' AND `command` = '%s'", (value ? escape_string(value) : ""), cbind->clientid, escape_string(cbind->cmd));
89         else
90             printf_mysql_query("UPDATE `bot_binds` SET `parameters` = '%s' WHERE `botclass` = '%d' AND `command` = '%s'", (value ? escape_string(value) : ""), cbind->botid, escape_string(cbind->cmd));
91         strcpy(parameters, (value ? value : ""));
92         ret = 1;
93     }
94     reply(getTextBot(), user, "\002PARAMETERS \002 %s", parameters);
95     return ret;
96 }
97
98 static int global_cmd_modcmd_caccess(struct UserNode *user, struct cmd_binding *cbind, char *value) {
99     char caccess[MAXLEN];
100     int ret = 0;
101     if(value) {
102         if(!strcmp(value, "*")) 
103             value = NULL;
104         bind_botwise_set_channel_access(cbind->botid, cbind->clientid, cbind->cmd, value);
105         if(cbind->botid == 0)
106             printf_mysql_query("UPDATE `bot_binds` SET `chan_access` = %s%s%s WHERE `botclass` = '0' AND `botid` = '%d' AND `command` = '%s'", (value ? "'" : ""), (value ? escape_string(value) : "NULL"), (value ? "'" : ""), cbind->clientid, escape_string(cbind->cmd));
107         else
108             printf_mysql_query("UPDATE `bot_binds` SET `chan_access` = %s%s%s WHERE `botclass` = '%d' AND `command` = '%s'", (value ? "'" : ""), (value ? escape_string(value) : "NULL"), (value ? "'" : ""), cbind->botid, escape_string(cbind->cmd));
109         
110         ret = 1;
111     }
112     if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
113         sprintf(caccess, "%s", cbind->channel_access);
114     else
115         sprintf(caccess, "\00314%s\003", (cbind->func->channel_access ? cbind->func->channel_access : "0"));
116     reply(getTextBot(), user, "\002CACCESS    \002 %s", caccess);
117     return ret;
118 }
119
120 static int global_cmd_modcmd_oaccess(struct UserNode *user, struct cmd_binding *cbind, char *value) {
121     char oaccess[MAXLEN];
122     int ret = 0;
123     if(value) {
124         if(!strcmp(value, "*")) 
125             value = NULL;
126         bind_botwise_set_global_access(cbind->botid, cbind->clientid, cbind->cmd, atoi(value));
127         if(cbind->botid == 0)
128             printf_mysql_query("UPDATE `bot_binds` SET `global_access` = %s%s%s WHERE `botclass` = '0' AND `botid` = '%d' AND `command` = '%s'", (value ? "'" : ""), (value ? escape_string(value) : "NULL"), (value ? "'" : ""), cbind->clientid, escape_string(cbind->cmd));
129         else
130             printf_mysql_query("UPDATE `bot_binds` SET `global_access` = %s%s%s WHERE `botclass` = '%d' AND `command` = '%s'", (value ? "'" : ""), (value ? escape_string(value) : "NULL"), (value ? "'" : ""), cbind->botid, escape_string(cbind->cmd));
131         
132         ret = 1;
133     }
134     if(cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS)
135         sprintf(oaccess, "%d", cbind->global_access);
136     else
137         sprintf(oaccess, "\00314%d\003", (cbind->func->global_access ? cbind->func->global_access : 0));
138     reply(getTextBot(), user, "\002OACCESS    \002 %s", oaccess);
139     return ret;
140 }
141