Atrinik Client 2.5
gui/help.c
Go to the documentation of this file.
00001 /************************************************************************
00002 *            Atrinik, a Multiplayer Online Role Playing Game            *
00003 *                                                                       *
00004 *    Copyright (C) 2009-2011 Alex Tokar and Atrinik Development Team    *
00005 *                                                                       *
00006 * Fork from Daimonin (Massive Multiplayer Online Role Playing Game)     *
00007 * and Crossfire (Multiplayer game for X-windows).                       *
00008 *                                                                       *
00009 * This program is free software; you can redistribute it and/or modify  *
00010 * it under the terms of the GNU General Public License as published by  *
00011 * the Free Software Foundation; either version 2 of the License, or     *
00012 * (at your option) any later version.                                   *
00013 *                                                                       *
00014 * This program is distributed in the hope that it will be useful,       *
00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00017 * GNU General Public License for more details.                          *
00018 *                                                                       *
00019 * You should have received a copy of the GNU General Public License     *
00020 * along with this program; if not, write to the Free Software           *
00021 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             *
00022 *                                                                       *
00023 * The author can be reached at admin@atrinik.org                        *
00024 ************************************************************************/
00025 
00030 #include <global.h>
00031 
00034 void free_help_files()
00035 {
00036     help_files_struct *help_file_tmp, *help_file = help_files;
00037 
00038     while (help_file)
00039     {
00040         help_file_tmp = help_file->next;
00041         free(help_file);
00042         help_file = help_file_tmp;
00043     }
00044 
00045     help_files = NULL;
00046 }
00047 
00054 void read_help_files()
00055 {
00056     FILE *fp = server_file_open(SERVER_FILE_HFILES);
00057     char buf[HUGE_BUF], helpname[MAX_BUF], title[MAX_BUF], message[HUGE_BUF * 12];
00058     int end_marker = 0, dm_only = 0, autocomplete = 1;
00059 
00060     if (!fp)
00061     {
00062         return;
00063     }
00064 
00065     if (help_files)
00066     {
00067         free_help_files();
00068     }
00069 
00070     helpname[0] = title[0] = message[0] = '\0';
00071 
00072     /* Loop through the lines */
00073     while (fgets(buf, sizeof(buf), fp))
00074     {
00075         char *end = strchr(buf, '\n');
00076 
00077         if (!strncmp(buf, "Name: ", 6))
00078         {
00079             *end = '\0';
00080             strncpy(helpname, buf + 6, sizeof(helpname) - 1);
00081         }
00082         else if (!strncmp(buf, "Title: ", 7))
00083         {
00084             *end = '\0';
00085             strncpy(title, buf + 7, sizeof(title) - 1);
00086         }
00087         else if (!strcmp(buf, "DM: 1\n"))
00088         {
00089             dm_only = 1;
00090         }
00091         else if (!strcmp(buf, "Autocomplete: 0\n"))
00092         {
00093             autocomplete = 0;
00094         }
00095         else if (!strcmp(buf, "==========\n"))
00096         {
00097             end_marker = 1;
00098         }
00099         else
00100         {
00101             strncat(message, buf, sizeof(message) - strlen(message) - 1);
00102         }
00103 
00104         if (end_marker)
00105         {
00106             help_files_struct *help_files_tmp = (help_files_struct *) malloc(sizeof(help_files_struct));
00107 
00108             help_files_tmp->next = help_files;
00109             help_files = help_files_tmp;
00110 
00111             strncpy(help_files_tmp->helpname, helpname, sizeof(help_files_tmp->helpname) - 1);
00112             strncpy(help_files_tmp->title, title, sizeof(help_files_tmp->title) - 1);
00113             strncpy(help_files_tmp->message, message, sizeof(help_files_tmp->message) - 1);
00114             help_files_tmp->dm_only = dm_only;
00115             help_files_tmp->autocomplete = autocomplete;
00116 
00117             helpname[0] = title[0] = message[0] = '\0';
00118             dm_only = 0;
00119             autocomplete = 1;
00120 
00121             end_marker = 0;
00122         }
00123     }
00124 
00125     fclose(fp);
00126 }
00127 
00132 void show_help(const char *helpname)
00133 {
00134     int len;
00135     unsigned char *data;
00136     char message[HUGE_BUF * 16];
00137     help_files_struct *help_files_tmp;
00138 
00139     /* This will be the default message if we don't find the help we want */
00140     snprintf(message, sizeof(message), "<book=Help not found><t t=\"The specified help file could not be found.\">\n");
00141 
00142     /* Loop through the linked list of help files */
00143     for (help_files_tmp = help_files; help_files_tmp; help_files_tmp = help_files_tmp->next)
00144     {
00145         /* If title or message are empty or helpname doesn't match, just continue to the next item */
00146         if (help_files_tmp->title[0] == '\0' || help_files_tmp->message[0] == '\0' || strcmp(help_files_tmp->helpname, helpname))
00147         {
00148             continue;
00149         }
00150 
00151         /* Got what we wanted, replace it with the default message */
00152         snprintf(message, sizeof(message), "<book>%s</book><title>%s</title>\n%s", help_files_tmp->helpname, help_files_tmp->title, help_files_tmp->message);
00153         break;
00154     }
00155 
00156     data = (uint8 *) message;
00157 
00158     len = (int) strlen((char *) data);
00159 
00160     book_load((char *) data, len);
00161 }