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