| 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" |
| 19 | #include "helpfile.h" |
| 20 | |
| 21 | #include <cerrno> |
| 22 | #include <cstdio> |
| 23 | #include <cstring> |
| 24 | #include <map> |
| 25 | |
| 26 | #include "compat/strcasecmp.h" |
| 27 | #include "common/eventlog.h" |
| 28 | #include "common/util.h" |
| 29 | #include "common/xalloc.h" |
| 30 | #include "common/xstring.h" |
| 31 | |
| 32 | #include "message.h" |
| 33 | #include "command_groups.h" |
| 34 | #include "account_wrap.h" |
| 35 | #include "connection.h" |
| 36 | #include "i18n.h" |
| 37 | #include "common/setup_after.h" |
| 38 | |
| 39 | namespace pvpgn |
| 40 | { |
| 41 | |
| 42 | namespace bnetd |
| 43 | { |
| 44 | static std::map<t_gamelang, std::FILE*> hfd_list; |
| 45 | static std::FILE* hfd = NULL; /* helpfile descriptor */ |
| 46 | |
| 47 | static int list_commands(t_connection *); |
| 48 | static std::FILE* get_hfd(t_connection * c); |
| 49 | |
| 50 | static std::FILE* get_hfd(t_connection * c) |
| 51 | { |
| 52 | t_gamelang lang = conn_get_gamelang_localized(c); |
| 53 | |
| 54 | std::map<t_gamelang, std::FILE*>::iterator it = hfd_list.find(lang); |
| 55 | if (it != hfd_list.end()) |
| 56 | { |
| 57 | return it->second; |
| 58 | } |
| 59 | // return enUS if language is not specified in language list |
| 60 | return hfd_list[languages[0].gamelang]; |
| 61 | } |
| 62 | |
| 63 | extern int helpfile_init(char const *filename) |
| 64 | { |
| 65 | if (!filename) |
| 66 | { |
| 67 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL filename"); |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | // iterate language list |
| 72 | for (std::size_t i = 0; i < languages.size(); i++) |
| 73 | { |
| 74 | // get hfd of all localized help files |
| 75 | std::string helpfile = i18n_filename(filename, languages[i].gamelang); |
| 76 | if (!(hfd_list[languages[i].gamelang] = std::fopen(helpfile.c_str(), "r"))) |
| 77 | { |
| 78 | eventlog(eventlog_level_error, __FUNCTION__, "could not open help file \"{}\" for reading (std::fopen: {})", helpfile, std::strerror(errno)); |
| 79 | return -1; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | extern int helpfile_unload(void) |
| 88 | { |
| 89 | // destroy file handles |
| 90 | for (std::map<t_gamelang, std::FILE*>::iterator it = hfd_list.begin(); it != hfd_list.end(); ++it) |
| 91 | { |
| 92 | if (it->second != NULL) |
| 93 | { |
| 94 | if (std::fclose(it->second) < 0) |
| 95 | eventlog(eventlog_level_error, __FUNCTION__, "could not close help file after reading (std::fclose: {})", std::strerror(errno)); |
| 96 | it->second = NULL; |
| 97 | } |
| 98 | } |
| 99 | // clear list |
| 100 | hfd_list.clear(); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | extern int handle_help_command(t_connection * c, char const * text) |
| 107 | { |
| 108 | std::FILE* hfd = get_hfd(c); |
| 109 | unsigned int i, j; |
| 110 | char cmd[MAX_COMMAND_LEN]; |
| 111 | |
| 112 | if (hfd == NULL) |
| 113 | { /* an error ocured opening readonly the help file, helpfile_unload was called, or helpfile_init hasn't been called */ |
| 114 | message_send_text(c, message_type_error, c, localize(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_commands(c); |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | describe_command(c, cmd); |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | static int list_commands(t_connection * c) |
| 139 | { |
| 140 | std::FILE* hfd = get_hfd(c); |
| 141 | char * line; |
| 142 | int i; |
| 143 | |
| 144 | message_send_text(c, message_type_info, c, localize(c, "Chat commands : ")); |
| 145 | std::rewind(hfd); |
| 146 | while ((line = file_get_line(hfd)) != NULL) |
| 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_LEN + 1; position = 0; |
| 158 | buffer = (char*)xmalloc(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_group(p) & account_get_command_groups(conn_get_account(c)))) |
| 169 | skip = 1; |
| 170 | if (length < std::strlen(p) + position + 1) |
| 171 | /* if we don't have enough space in the buffer then get some */ |
| 172 | length = std::strlen(p) + position + 1; /* the new length */ |
| 173 | buffer = (char*)xrealloc(buffer, length + 1); |
| 174 | buffer[position++] = ' '; /* put a space before each alias */ |
| 175 | /* add the alias to the output string */ |
| 176 | std::strcpy(buffer + position, p); position += 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_text(c, message_type_info, c, buffer); /* print out the buffer */ |
| 188 | xfree(buffer); |
| 189 | } |
| 190 | } |
| 191 | file_get_line(NULL); // clear file_get_line buffer |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | extern int describe_command(t_connection * c, char const * cmd) |
| 197 | { |
| 198 | std::FILE* hfd = get_hfd(c); |
| 199 | char * line; |
| 200 | int i; |
| 201 | |
| 202 | |
| 203 | /* ok. the client requested help for a specific command */ |
| 204 | std::rewind(hfd); |
| 205 | while ((line = file_get_line(hfd)) != NULL) |
| 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_line(hfd)) != NULL) |
| 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_replace(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_convert(c, line); |
| 244 | |
| 245 | if (line[j] == '/') |
| 246 | message_send_text(c, message_type_error, c, line); |
| 247 | else |
| 248 | message_send_text(c, 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_line(NULL); // clear file_get_line buffer |
| 266 | |
| 267 | /* no description was found for this command. inform the user */ |
| 268 | message_send_text(c, message_type_error, c, localize(c, "No help available for that command")); |
| 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | } |
| 273 | |
| 274 | } |