Debian Codegraph @

src/bnetd/helpfile.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2000 Dizzy
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (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, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #include "common/setup_before.h"cxx . . $ `<file>/src/common/setup_before.h`/
19 #include "helpfile.h"cxx . . $ `<file>/src/bnetd/helpfile.h`/
20
21 #include <cerrno>
22 #include <cstdio>
23 #include <cstring>
24 #include <map>
25
26 #include "compat/strcasecmp.h"cxx . . $ `<file>/src/compat/strcasecmp.h`/
27 #include "common/eventlog.h"cxx . . $ `<file>/src/common/eventlog.h`/
28 #include "common/util.h"cxx . . $ `<file>/src/common/util.h`/
29 #include "common/xalloc.h"cxx . . $ `<file>/src/common/xalloc.h`/
30 #include "common/xstring.h"cxx . . $ `<file>/src/common/xstring.h`/
31
32 #include "message.h"cxx . . $ `<file>/src/bnetd/message.h`/
33 #include "command_groups.h"cxx . . $ `<file>/src/bnetd/command_groups.h`/
34 #include "account_wrap.h"cxx . . $ `<file>/src/bnetd/account_wrap.h`/
35 #include "connection.h"cxx . . $ `<file>/src/bnetd/connection.h`/
36 #include "i18n.h"cxx . . $ `<file>/src/bnetd/i18n.h`/
37 #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/
38
39 namespace pvpgncxx . . $ pvpgn/
40 {
41
42 namespace bnetdcxx . . $ pvpgn/bnetd/
43 {
44 static stdcxx . . $ std/::mapcxx . . $ std/map#<t_gamelang, stdcxx . . $ std/::FILEcxx . . $ std/FILE#*> hfd_listcxx . . $ pvpgn/bnetd/hfd_list.;
45 static stdcxx . . $ std/::FILEcxx . . $ std/FILE#* hfdcxx . . $ pvpgn/bnetd/hfd. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!; /* helpfile descriptor */
46
47 static int list_commandscxx . . $ pvpgn/bnetd/list_commands(c4dfbc2386fe4fc3).(t_connectioncxx . . $ pvpgn/bnetd/t_connection# *);
48 static stdcxx . . $ std/::FILEcxx . . $ std/FILE#* get_hfdcxx . . $ pvpgn/bnetd/get_hfd(14e00bd4f1c8525f).(t_connectioncxx . . $ pvpgn/bnetd/t_connection# * c);
49
50 static stdcxx . . $ std/::FILEcxx . . $ std/FILE#* get_hfdcxx . . $ pvpgn/bnetd/get_hfd(14e00bd4f1c8525f).(t_connectioncxx . . $ pvpgn/bnetd/t_connection# * c)
51 {
52 t_gamelang lang = conn_get_gamelang_localizedcxx . . $ pvpgn/bnetd/conn_get_gamelang_localized(c9be5e844f0af884).(c);
53
54 stdcxx . . $ std/::mapcxx . . $ std/map#<t_gamelang, std::FILE*>::iteratorcxx . . $ std/map#iterator# it = hfd_listcxx . . $ pvpgn/bnetd/hfd_list..findcxx . . $ std/map#find(36f8d56706583706).(lang);
55 if (it !=cxx . . $ std/`operator!=`(be6a71a859368080). hfd_listcxx . . $ pvpgn/bnetd/hfd_list..endcxx . . $ std/map#end(7e4de33083b6e738).())
56 {
57 return it->cxx . . $ std/_Rb_tree_iterator#`operator->`(34d6acfd341ba7ab).secondcxx . . $ std/pair#second.;
58 }
59 // return enUS if language is not specified in language list
60 return hfd_listcxx . . $ pvpgn/bnetd/hfd_list.[cxx . . $ std/map#`operator[]`(bbd415b616967878).languagescxx . . $ pvpgn/bnetd/languages.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0].gamelangcxx . . $ pvpgn/bnetd/$anonymous_type_e9cf31f8eef22f11_0#gamelang.];
61 }
62
63 extern int helpfile_initcxx . . $ pvpgn/bnetd/helpfile_init(5c4306d8553b3696).(char const *filename)
64 {
65 if (!filename)
66 {
67 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL filename");
68 return -1;
69 }
70
71 // iterate language list
72 for (stdcxx . . $ std/::size_tcxx . . $ std/size_t# i = 0; i < languagescxx . . $ pvpgn/bnetd/languages..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); i++)
73 {
74 // get hfd of all localized help files
75 stdcxx . . $ std/::stringcxx . . $ std/string# helpfile = i18n_filenamecxx . . $ pvpgn/bnetd/i18n_filename(caf975da75400ef4).(filename, languagescxx . . $ pvpgn/bnetd/languages.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i].gamelangcxx . . $ pvpgn/bnetd/$anonymous_type_e9cf31f8eef22f11_0#gamelang.);
76 if (!(hfd_listcxx . . $ pvpgn/bnetd/hfd_list.[cxx . . $ std/map#`operator[]`(bbd415b616967878).languagescxx . . $ pvpgn/bnetd/languages.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i].gamelangcxx . . $ pvpgn/bnetd/$anonymous_type_e9cf31f8eef22f11_0#gamelang.] = stdcxx . . $ std/::fopencxx . . $ std/fopen(82ec0787cd37e0a3).(helpfile.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), "r")))
77 {
78 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not open help file \"{}\" for reading (std::fopen: {})", helpfile, stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
79 return -1;
80 }
81 }
82
83 return 0;
84 }
85
86
87 extern int helpfile_unloadcxx . . $ pvpgn/bnetd/helpfile_unload(b126dc7c1de90089).(void)
88 {
89 // destroy file handles
90 for (stdcxx . . $ std/::mapcxx . . $ std/map#<t_gamelang, std::FILE*>::iteratorcxx . . $ std/map#iterator# it = hfd_listcxx . . $ pvpgn/bnetd/hfd_list..begincxx . . $ std/map#begin(7e4de33083b6e738).(); it !=cxx . . $ std/`operator!=`(be6a71a859368080). hfd_listcxx . . $ pvpgn/bnetd/hfd_list..endcxx . . $ std/map#end(7e4de33083b6e738).(); ++cxx . . $ std/_Rb_tree_iterator#operator++(5b825426e5b44d2c).it)
91 {
92 if (it->cxx . . $ std/_Rb_tree_iterator#`operator->`(34d6acfd341ba7ab).secondcxx . . $ std/pair#second. != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!)
93 {
94 if (stdcxx . . $ std/::fclosecxx . . $ std/fclose(89bc1efcdac0ca22).(it->cxx . . $ std/_Rb_tree_iterator#`operator->`(34d6acfd341ba7ab).secondcxx . . $ std/pair#second.) < 0)
95 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not close help file after reading (std::fclose: {})", stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
96 it->cxx . . $ std/_Rb_tree_iterator#`operator->`(34d6acfd341ba7ab).secondcxx . . $ std/pair#second. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!;
97 }
98 }
99 // clear list
100 hfd_listcxx . . $ pvpgn/bnetd/hfd_list..clearcxx . . $ std/map#clear(ced63f7c635d850d).();
101
102 return 0;
103 }
104
105
106 extern int handle_help_commandcxx . . $ pvpgn/bnetd/handle_help_command(2e9022cda97b266d).(t_connectioncxx . . $ pvpgn/bnetd/t_connection# * c, char const * text)
107 {
108 stdcxx . . $ std/::FILEcxx . . $ std/FILE#* hfd = get_hfdcxx . . $ pvpgn/bnetd/get_hfd(14e00bd4f1c8525f).(c);
109 unsigned int i, j;
110 char cmd[MAX_COMMAND_LENcxx . . $ MAX_COMMAND_LEN.];
111
112 if (hfd == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!)
113 { /* an error ocured opening readonly the help file, helpfile_unload was called, or helpfile_init hasn't been called */
114 message_send_textcxx . . $ pvpgn/bnetd/message_send_text(6288036871507e5c).(c, message_type_errorcxx . . $ pvpgn/bnetd/message_type_error., c, localizecxx . . $ `src/bnetd/i18n.h:73:11`!(c, "Oops ! There is a problem with the help file. Please contact the administrator of the server."));
115 return -1;
116 }
117
118 for (i = 0; text[i] != ' ' && text[i] != '\0'; i++); /* skip command */
119 for (; text[i] == ' '; i++);
120 if (text[i] == '/') /* skip / in front of command (if present) */
121 i++;
122 for (j = 0; text[i] != ' ' && text[i] != '\0'; i++) /* get comm */
123 if (j < sizeof(cmd)-1) cmd[j++] = text[i];
124 cmd[j] = '\0';
125
126 /* just read the whole file and dump only the commands */
127 if (cmd[0] == '\0')
128 {
129 list_commandscxx . . $ pvpgn/bnetd/list_commands(c4dfbc2386fe4fc3).(c);
130 return 0;
131 }
132
133 describe_commandcxx . . $ pvpgn/bnetd/describe_command(2e9022cda97b266d).(c, cmd);
134 return 0;
135 }
136
137
138 static int list_commandscxx . . $ pvpgn/bnetd/list_commands(c4dfbc2386fe4fc3).(t_connectioncxx . . $ pvpgn/bnetd/t_connection# * c)
139 {
140 stdcxx . . $ std/::FILEcxx . . $ std/FILE#* hfd = get_hfdcxx . . $ pvpgn/bnetd/get_hfd(14e00bd4f1c8525f).(c);
141 char * line;
142 int i;
143
144 message_send_textcxx . . $ pvpgn/bnetd/message_send_text(6288036871507e5c).(c, message_type_infocxx . . $ pvpgn/bnetd/message_type_info., c, localizecxx . . $ `src/bnetd/i18n.h:73:11`!(c, "Chat commands : "));
145 stdcxx . . $ std/::rewindcxx . . $ std/rewind(8d4262d45fc644ab).(hfd);
146 while ((line = file_get_linecxx . . $ pvpgn/file_get_line(e31226b218f63160).(hfd)) != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!)
147 {
148 for (i = 0; line[i] == ' '; i++); /* skip spaces in front of %command */
149 if (line[i] == '%') /* is this a command ? */
150 {
151 char *p, *buffer;
152 int al;
153 int skip;
154 unsigned int length, position;
155
156 /* ok. now we must see if there are any aliases */
157 length = MAX_COMMAND_LENcxx . . $ MAX_COMMAND_LEN. + 1; position = 0;
158 buffer = (char*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(length + 1); /* initial memory allocation = pretty fair */
159 p = line + i;
160 do
161 {
162 al = 0;
163 skip = 0;
164 for (i = 1; p[i] != ' ' && p[i] != '\0' && p[i] != '#'; i++); /* skip command */
165 if (p[i] == ' ') al = 1; /* we have something after the command.. must remember that */
166 p[i] = '\0'; /* end the string at the end of the command */
167 p[0] = '/'; /* change the leading character (% or space) read from the help file to / */
168 if (!(command_get_groupcxx . . $ pvpgn/bnetd/command_get_group(c61c63047ce273de).(p) & account_get_command_groupscxx . . $ pvpgn/bnetd/account_get_command_groups(c7ab9369555c44b8).(conn_get_accountcxx . . $ pvpgn/bnetd/conn_get_account(8df8b24b7c17a7f3).(c))))
169 skip = 1;
170 if (length < stdcxx . . $ std/::strlen(p) + position + 1)
171 /* if we don't have enough space in the buffer then get some */
172 length = stdcxx . . $ std/::strlen(p) + position + 1; /* the new length */
173 buffer = (char*)xrealloccxx . . $ `src/common/xalloc.h:47:9`!(buffer, length + 1);
174 buffer[position++] = ' '; /* put a space before each alias */
175 /* add the alias to the output string */
176 stdcxx . . $ std/::strcpy(buffer + position, p); position += stdcxx . . $ std/::strlen(p);
177 if (al)
178 {
179 for (; p[i + 1] == ' '; i++); /* skip spaces */
180 if (p[i + 1] == '\0' || p[i + 1] == '#')
181 {
182 al = 0; continue;
183 }
184 p += i; /* jump to the next command */
185 }
186 } while (al);
187 if (!skip) message_send_textcxx . . $ pvpgn/bnetd/message_send_text(61daf7e3ba1862c4).(c, message_type_infocxx . . $ pvpgn/bnetd/message_type_info., c, buffer); /* print out the buffer */
188 xfreecxx . . $ `src/common/xalloc.h:51:9`!(buffer);
189 }
190 }
191 file_get_linecxx . . $ pvpgn/file_get_line(e31226b218f63160).(NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!); // clear file_get_line buffer
192 return 0;
193 }
194
195
196 extern int describe_commandcxx . . $ pvpgn/bnetd/describe_command(2e9022cda97b266d).(t_connectioncxx . . $ pvpgn/bnetd/t_connection# * c, char const * cmd)
197 {
198 stdcxx . . $ std/::FILEcxx . . $ std/FILE#* hfd = get_hfdcxx . . $ pvpgn/bnetd/get_hfd(14e00bd4f1c8525f).(c);
199 char * line;
200 int i;
201
202
203 /* ok. the client requested help for a specific command */
204 stdcxx . . $ std/::rewindcxx . . $ std/rewind(8d4262d45fc644ab).(hfd);
205 while ((line = file_get_linecxx . . $ pvpgn/file_get_line(e31226b218f63160).(hfd)) != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!)
206 {
207 for (i = 0; line[i] == ' '; i++); /* skip spaces in front of %command */
208 if (line[i] == '%') /* is this a command ? */
209 {
210 char *p;
211 int al;
212 /* ok. now we must see if there are any aliases */
213 p = line + i;
214 do
215 {
216 al = 0;
217 for (i = 1; p[i] != ' ' && p[i] != '\0' && p[i] != '#'; i++); /* skip command */
218 if (p[i] == ' ') al = 1; /* we have something after the command.. must remember that */
219 p[i] = '\0'; /* end the string at the end of the command */
220 if (strcasecmp(cmd, p + 1) == 0) /* is this the command the user asked for help ? */
221 {
222 while ((line = file_get_linecxx . . $ pvpgn/file_get_line(e31226b218f63160).(hfd)) != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!)
223 { /* write everything until we get another % or EOF */
224 for (i = 0; line[i] == ' '; i++); /* skip spaces in front of a possible % */
225 if (line[i] == '%')
226 {
227 break; /* we reached another command */
228 }
229 if (line[0] != '#' && line[i] != '\0')
230 { /* is this a whole line comment ? */
231 /* truncate the line when a comment starts */
232 for (; line[i] != '\0' && line[i] != '#'; i++);
233 if (line[i] == '#') line[i] = '\0';
234
235 // replace tabs with 3 spaces
236 char threeSpace[4] = " ";
237 char tabCharacter[3] = "\t";
238 char *tmp = str_replacecxx . . $ pvpgn/str_replace(a1df38fec57fccd9).(line, tabCharacter, threeSpace);
239 line = tmp;
240 // if text starts with slash then make it colored
241 int j = 0; for (; line[j] == ' ' || line[j] == '\t'; j++);
242
243 i18n_convertcxx . . $ pvpgn/bnetd/i18n_convert(c30c6622e93e4e71).(c, line);
244
245 if (line[j] == '/')
246 message_send_textcxx . . $ pvpgn/bnetd/message_send_text(61daf7e3ba1862c4).(c, message_type_errorcxx . . $ pvpgn/bnetd/message_type_error., c, line);
247 else
248 message_send_textcxx . . $ pvpgn/bnetd/message_send_text(61daf7e3ba1862c4).(c, message_type_infocxx . . $ pvpgn/bnetd/message_type_info., c, line);
249 }
250 }
251 return 0;
252 }
253 if (al)
254 {
255 for (; p[i + 1] == ' '; i++); /* skip spaces */
256 if (p[i + 1] == '\0')
257 {
258 al = 0; continue;
259 }
260 p += i; /* jump to the next command */
261 }
262 } while (al);
263 }
264 }
265 file_get_linecxx . . $ pvpgn/file_get_line(e31226b218f63160).(NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!); // clear file_get_line buffer
266
267 /* no description was found for this command. inform the user */
268 message_send_textcxx . . $ pvpgn/bnetd/message_send_text(6288036871507e5c).(c, message_type_errorcxx . . $ pvpgn/bnetd/message_type_error., c, localizecxx . . $ `src/bnetd/i18n.h:73:11`!(c, "No help available for that command"));
269 return -1;
270 }
271
272 }
273
274 }