*** VERSION 5.2.0 ***
[NeonServV5.git] / src / cmd_neonserv_help.c
1 /* cmd_neonserv_help.c - NeonServ v5.2
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-*]     index
22 */
23
24 CMD_BIND(neonserv_cmd_help) {
25     char *ident;
26     if(argc)
27         ident = merge_argv(argv, 0, argc);
28     else
29         ident = "0";
30     MYSQL_RES *res;
31     MYSQL_ROW row;
32     printf_mysql_query("SELECT `user_lang` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
33     res = mysql_use();
34     char *lang;
35     if ((row = mysql_fetch_row(res)) != NULL)
36         lang = row[0];
37     else
38         lang = "en";
39     printf_mysql_query("SELECT `text` FROM `help` WHERE `lang` = '%s' AND `ident` = '%s'", escape_string(lang), escape_string(ident));
40     res = mysql_use();
41     if ((row = mysql_fetch_row(res)) == NULL) {
42         if(stricmp(lang, "en")) {
43             printf_mysql_query("SELECT `text` FROM `help` WHERE `lang` = 'en' AND `ident` = '%s'", escape_string(ident));
44             res = mysql_use();
45         }
46         if ((row = mysql_fetch_row(res)) == NULL) {
47             reply(getTextBot(), user, "NS_HELP_TOPIC");
48             return;
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 }