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