| 1 |
/*
|
| 2 |
* Copyright (C) 2000 Dizzy (dizzy@roedu.net)
|
| 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 <stdio.h>
|
| 20 |
#ifdef HAVE_STDDEF_H
|
| 21 |
# include <stddef.h>
|
| 22 |
#else
|
| 23 |
# ifndef NULL
|
| 24 |
# define NULL ((void *)0)
|
| 25 |
# endif
|
| 26 |
#endif
|
| 27 |
#ifdef STDC_HEADERS
|
| 28 |
# include <stdlib.h>
|
| 29 |
#else
|
| 30 |
# ifdef HAVE_MALLOC_H
|
| 31 |
# include <malloc.h>
|
| 32 |
# endif
|
| 33 |
#endif
|
| 34 |
#ifdef HAVE_STRING_H
|
| 35 |
# include <string.h>
|
| 36 |
#else
|
| 37 |
# ifdef HAVE_STRINGS_H
|
| 38 |
# include <strings.h>
|
| 39 |
# endif
|
| 40 |
#endif
|
| 41 |
#include "compat/strcasecmp.h"
|
| 42 |
#include <ctype.h>
|
| 43 |
#include <errno.h>
|
| 44 |
#include "compat/strerror.h"
|
| 45 |
#include "message.h"
|
| 46 |
#include "connection.h"
|
| 47 |
#include "common/util.h"
|
| 48 |
#include "common/eventlog.h"
|
| 49 |
#include "common/xalloc.h"
|
| 50 |
#include "helpfile.h"
|
| 51 |
#include "common/setup_after.h"
|
| 52 |
#include "account_wrap.h"
|
| 53 |
#include "command_groups.h"
|
| 54 |
|
| 55 |
|
| 56 |
static FILE* hfd=NULL; /* helpfile descriptor */
|
| 57 |
|
| 58 |
static int list_commands(t_connection *);
|
| 59 |
static int describe_command(t_connection *, char const *);
|
| 60 |
|
| 61 |
|
| 62 |
extern int helpfile_init(char const *filename)
|
| 63 |
{
|
| 64 |
if (!filename)
|
| 65 |
{
|
| 66 |
eventlog(eventlog_level_error,__FUNCTION__,"got NULL filename");
|
| 67 |
return -1;
|
| 68 |
}
|
| 69 |
if (!(hfd = fopen(filename,"r")))
|
| 70 |
{
|
| 71 |
eventlog(eventlog_level_error,__FUNCTION__,"could not open help file \"%s\" for reading (fopen: %s)",filename,pstrerror(errno));
|
| 72 |
return -1;
|
| 73 |
}
|
| 74 |
return 0;
|
| 75 |
}
|
| 76 |
|
| 77 |
|
| 78 |
extern int helpfile_unload(void)
|
| 79 |
{
|
| 80 |
if (hfd!=NULL)
|
| 81 |
{
|
| 82 |
if (fclose(hfd)<0)
|
| 83 |
eventlog(eventlog_level_error,__FUNCTION__,"could not close help file after reading (fclose: %s)",pstrerror(errno));
|
| 84 |
hfd = NULL;
|
| 85 |
}
|
| 86 |
return 0;
|
| 87 |
}
|
| 88 |
|
| 89 |
|
| 90 |
extern int handle_help_command(t_connection * c, char const * text)
|
| 91 |
{
|
| 92 |
unsigned int i,j;
|
| 93 |
char comm[MAX_COMMAND_LEN];
|
| 94 |
|
| 95 |
if (hfd == NULL)
|
| 96 |
{ /* an error ocured opening readonly the help file, helpfile_unload was called, or helpfile_init hasn't been called */
|
| 97 |
message_send_text(c,message_type_error,c,"Oops ! There is a problem with the help file. Please contact the administrator of the server.");
|
| 98 |
return 0;
|
| 99 |
}
|
| 100 |
|
| 101 |
rewind(hfd);
|
| 102 |
for (i=0; text[i]!=' ' && text[i]!='\0'; i++); /* skip command */
|
| 103 |
for (; text[i]==' '; i++);
|
| 104 |
if (text[i]=='/') /* skip / in front of command (if present) */
|
| 105 |
i++;
|
| 106 |
for (j=0; text[i]!=' ' && text[i]!='\0'; i++) /* get comm */
|
| 107 |
if (j<sizeof(comm)-1) comm[j++] = text[i];
|
| 108 |
comm[j] = '\0';
|
| 109 |
|
| 110 |
/* just read the whole file and dump only the commands */
|
| 111 |
if (comm[0]=='\0')
|
| 112 |
{
|
| 113 |
list_commands(c);
|
| 114 |
return 0;
|
| 115 |
}
|
| 116 |
|
| 117 |
if (describe_command(c,comm)==0) return 0;
|
| 118 |
/* no description was found for this command. inform the user */
|
| 119 |
message_send_text(c,message_type_error,c," no help available for that command");
|
| 120 |
return 0;
|
| 121 |
}
|
| 122 |
|
| 123 |
|
| 124 |
static int list_commands(t_connection * c)
|
| 125 |
{
|
| 126 |
char * line;
|
| 127 |
int i;
|
| 128 |
|
| 129 |
message_send_text(c,message_type_info,c,"Chat commands:");
|
| 130 |
while ((line=file_get_line(hfd))!=NULL)
|
| 131 |
{
|
| 132 |
for (i=0;line[i]==' ' && line[i]!='\0';i++); /* skip spaces in front of %command */
|
| 133 |
if (line[i]=='%') /* is this a command ? */
|
| 134 |
{
|
| 135 |
char *p,*buffer;
|
| 136 |
int al;
|
| 137 |
int skip;
|
| 138 |
unsigned int length,position;
|
| 139 |
|
| 140 |
/* ok. now we must see if there are any aliases */
|
| 141 |
length=MAX_COMMAND_LEN+1; position=0;
|
| 142 |
buffer=xmalloc(length+1); /* initial memory allocation = pretty fair */
|
| 143 |
p=line+i;
|
| 144 |
do
|
| 145 |
{
|
| 146 |
al=0;
|
| 147 |
skip = 0;
|
| 148 |
for (i=1;p[i]!=' ' && p[i]!='\0' && p[i]!='#';i++); /* skip command */
|
| 149 |
if (p[i]==' ') al=1; /* we have something after the command.. must remember that */
|
| 150 |
p[i]='\0'; /* end the string at the end of the command */
|
| 151 |
p[0]='/'; /* change the leading character (% or space) read from the help file to / */
|
| 152 |
if (!(command_get_group(p) & account_get_command_groups(conn_get_account(c)))) skip=1;
|
| 153 |
if (length<strlen(p)+position+1)
|
| 154 |
/* if we don't have enough space in the buffer then get some */
|
| 155 |
length=strlen(p)+position+1; /* the new length */
|
| 156 |
buffer=xrealloc(buffer,length+1);
|
| 157 |
buffer[position++]=' '; /* put a space before each alias */
|
| 158 |
/* add the alias to the output string */
|
| 159 |
strcpy(buffer+position,p); position+=strlen(p);
|
| 160 |
if (al)
|
| 161 |
{
|
| 162 |
for (;p[i+1]==' ' && p[i+1]!='\0' && p[i+1]!='#';i++); /* skip spaces */
|
| 163 |
if (p[i+1]=='\0' || p[i+1]=='#')
|
| 164 |
{
|
| 165 |
al=0; continue;
|
| 166 |
}
|
| 167 |
p+=i; /* jump to the next command */
|
| 168 |
}
|
| 169 |
} while (al);
|
| 170 |
if (!skip) message_send_text(c,message_type_info,c,buffer); /* print out the buffer */
|
| 171 |
xfree(buffer);
|
| 172 |
}
|
| 173 |
}
|
| 174 |
file_get_line(NULL); // clear file_get_line buffer
|
| 175 |
return 0;
|
| 176 |
}
|
| 177 |
|
| 178 |
|
| 179 |
static int describe_command(t_connection * c, char const * comm)
|
| 180 |
{
|
| 181 |
char * line;
|
| 182 |
int i;
|
| 183 |
|
| 184 |
/* ok. the client requested help for a specific command */
|
| 185 |
while ((line=file_get_line(hfd))!=NULL)
|
| 186 |
{
|
| 187 |
for (i=0;line[i]==' ' && line[i]!='\0';i++); /* skip spaces in front of %command */
|
| 188 |
if (line[i]=='%') /* is this a command ? */
|
| 189 |
{
|
| 190 |
char *p;
|
| 191 |
int al;
|
| 192 |
/* ok. now we must see if there are any aliases */
|
| 193 |
p=line+i;
|
| 194 |
do
|
| 195 |
{
|
| 196 |
al=0;
|
| 197 |
for (i=1;p[i]!=' ' && p[i]!='\0' && p[i]!='#';i++); /* skip command */
|
| 198 |
if (p[i]==' ') al=1; /* we have something after the command.. must remember that */
|
| 199 |
p[i]='\0'; /* end the string at the end of the command */
|
| 200 |
if (strcasecmp(comm,p+1)==0) /* is this the command the user asked for help ? */
|
| 201 |
{
|
| 202 |
while ((line=file_get_line(hfd))!=NULL)
|
| 203 |
{ /* write everything until we get another % or EOF */
|
| 204 |
for (i=0;line[i]==' ';i++); /* skip spaces in front of a possible % */
|
| 205 |
if (line[i]=='%')
|
| 206 |
{
|
| 207 |
break; /* we reached another command */
|
| 208 |
}
|
| 209 |
if (line[0]!='#')
|
| 210 |
{ /* is this a whole line comment ? */
|
| 211 |
/* truncate the line when a comment starts */
|
| 212 |
for (;line[i]!='\0' && line[i]!='#';i++);
|
| 213 |
if (line[i]=='#') line[i]='\0';
|
| 214 |
message_send_text(c,message_type_info,c,line);
|
| 215 |
}
|
| 216 |
}
|
| 217 |
return 0;
|
| 218 |
}
|
| 219 |
if (al)
|
| 220 |
{
|
| 221 |
for (;p[i+1]==' ' && p[i+1]!='\0';i++); /* skip spaces */
|
| 222 |
if (p[i+1]=='\0')
|
| 223 |
{
|
| 224 |
al=0; continue;
|
| 225 |
}
|
| 226 |
p+=i; /* jump to the next command */
|
| 227 |
}
|
| 228 |
} while (al);
|
| 229 |
}
|
| 230 |
}
|
| 231 |
file_get_line(NULL); // clear file_get_line buffer
|
| 232 |
|
| 233 |
return -1;
|
| 234 |
}
|
| 235 |
|