| 1 |
/* |
| 2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
| 3 |
* |
| 4 |
* Copyright (c) 1999-2020 ircd-hybrid development team |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 19 |
* USA |
| 20 |
*/ |
| 21 |
|
| 22 |
/*! \file m_help.c |
| 23 |
* \brief Includes required functions for processing the HELP command. |
| 24 |
* \version $Id$ |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "stdinc.h" |
| 28 |
#include "client.h" |
| 29 |
#include "ircd.h" |
| 30 |
#include "numeric.h" |
| 31 |
#include "send.h" |
| 32 |
#include "conf.h" |
| 33 |
#include "parse.h" |
| 34 |
#include "modules.h" |
| 35 |
#include "irc_string.h" |
| 36 |
|
| 37 |
|
| 38 |
enum { HELPLEN = 400 }; |
| 39 |
|
| 40 |
static void |
| 41 |
sendhelpfile(struct Client *source_p, const char *path, const char *topic) |
| 42 |
{ |
| 43 |
FILE *file = NULL; |
| 44 |
char *p = NULL; |
| 45 |
char line[HELPLEN] = ""; |
| 46 |
|
| 47 |
if ((file = fopen(path, "r")) == NULL) |
| 48 |
{ |
| 49 |
sendto_one_numeric(source_p, &me, ERR_HELPNOTFOUND, topic); |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
if (fgets(line, sizeof(line), file) == NULL) |
| 54 |
{ |
| 55 |
sendto_one_numeric(source_p, &me, ERR_HELPNOTFOUND, topic); |
| 56 |
fclose(file); |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
if ((p = strpbrk(line, "\r\n"))) |
| 61 |
*p = '\0'; |
| 62 |
|
| 63 |
sendto_one_numeric(source_p, &me, RPL_HELPSTART, topic, line); |
| 64 |
|
| 65 |
while (fgets(line, sizeof(line), file)) |
| 66 |
{ |
| 67 |
if ((p = strpbrk(line, "\r\n"))) |
| 68 |
*p = '\0'; |
| 69 |
|
| 70 |
sendto_one_numeric(source_p, &me, RPL_HELPTXT, topic, line); |
| 71 |
} |
| 72 |
|
| 73 |
fclose(file); |
| 74 |
sendto_one_numeric(source_p, &me, RPL_ENDOFHELP, topic); |
| 75 |
} |
| 76 |
|
| 77 |
static void |
| 78 |
do_help(struct Client *source_p, char *topic) |
| 79 |
{ |
| 80 |
char h_index[] = "index"; |
| 81 |
struct stat sb; |
| 82 |
|
| 83 |
if (EmptyString(topic)) |
| 84 |
topic = h_index; |
| 85 |
else |
| 86 |
for (char *p = topic; *p; ++p) |
| 87 |
*p = ToLower(*p); |
| 88 |
|
| 89 |
if (strpbrk(topic, "/\\")) |
| 90 |
{ |
| 91 |
sendto_one_numeric(source_p, &me, ERR_HELPNOTFOUND, topic); |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
char path[sizeof(HPATH) + IRCD_BUFSIZE + 1]; /* +1 for / */ |
| 96 |
snprintf(path, sizeof(path), "%s/%s", HPATH, topic); |
| 97 |
|
| 98 |
if (stat(path, &sb) < 0) |
| 99 |
{ |
| 100 |
sendto_one_numeric(source_p, &me, ERR_HELPNOTFOUND, topic); |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
if (!S_ISREG(sb.st_mode)) |
| 105 |
{ |
| 106 |
sendto_one_numeric(source_p, &me, ERR_HELPNOTFOUND, topic); |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
sendhelpfile(source_p, path, topic); |
| 111 |
} |
| 112 |
|
| 113 |
/*! \brief HELP command handler |
| 114 |
* |
| 115 |
* \param source_p Pointer to allocated Client struct from which the message |
| 116 |
* originally comes from. This can be a local or remote client. |
| 117 |
* \param parc Integer holding the number of supplied arguments. |
| 118 |
* \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL |
| 119 |
* pointers. |
| 120 |
* \note Valid arguments for this command are: |
| 121 |
* - parv[0] = command |
| 122 |
* - parv[1] = help topic |
| 123 |
*/ |
| 124 |
static void |
| 125 |
m_help(struct Client *source_p, int parc, char *parv[]) |
| 126 |
{ |
| 127 |
static uintmax_t last_used = 0; |
| 128 |
|
| 129 |
if ((last_used + ConfigGeneral.pace_wait_simple) > event_base->time.sec_monotonic) |
| 130 |
{ |
| 131 |
sendto_one_numeric(source_p, &me, RPL_LOAD2HI, "HELP"); |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
last_used = event_base->time.sec_monotonic; |
| 136 |
|
| 137 |
do_help(source_p, parv[1]); |
| 138 |
} |
| 139 |
|
| 140 |
/*! \brief HELP command handler |
| 141 |
* |
| 142 |
* \param source_p Pointer to allocated Client struct from which the message |
| 143 |
* originally comes from. This can be a local or remote client. |
| 144 |
* \param parc Integer holding the number of supplied arguments. |
| 145 |
* \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL |
| 146 |
* pointers. |
| 147 |
* \note Valid arguments for this command are: |
| 148 |
* - parv[0] = command |
| 149 |
* - parv[1] = help topic |
| 150 |
*/ |
| 151 |
static void |
| 152 |
mo_help(struct Client *source_p, int parc, char *parv[]) |
| 153 |
{ |
| 154 |
do_help(source_p, parv[1]); |
| 155 |
} |
| 156 |
|
| 157 |
static struct Message help_msgtab = |
| 158 |
{ |
| 159 |
.cmd = "HELP", |
| 160 |
.args_max = MAXPARA, |
| 161 |
.handlers[UNREGISTERED_HANDLER] = m_unregistered, |
| 162 |
.handlers[CLIENT_HANDLER] = m_help, |
| 163 |
.handlers[SERVER_HANDLER] = m_ignore, |
| 164 |
.handlers[ENCAP_HANDLER] = m_ignore, |
| 165 |
.handlers[OPER_HANDLER] = mo_help |
| 166 |
}; |
| 167 |
|
| 168 |
static void |
| 169 |
module_init(void) |
| 170 |
{ |
| 171 |
mod_add_cmd(&help_msgtab); |
| 172 |
} |
| 173 |
|
| 174 |
static void |
| 175 |
module_exit(void) |
| 176 |
{ |
| 177 |
mod_del_cmd(&help_msgtab); |
| 178 |
} |
| 179 |
|
| 180 |
struct module module_entry = |
| 181 |
{ |
| 182 |
.version = "$Revision$", |
| 183 |
.modinit = module_init, |
| 184 |
.modexit = module_exit, |
| 185 |
}; |