1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* m_help.c: Provides help information to a user/operator. |
4 |
* |
5 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
* USA |
21 |
* |
22 |
* $Id$ |
23 |
*/ |
24 |
|
25 |
#include "stdinc.h" |
26 |
#include "handlers.h" |
27 |
#include "client.h" |
28 |
#include "ircd.h" |
29 |
#include "ircd_handler.h" |
30 |
#include "msg.h" |
31 |
#include "numeric.h" |
32 |
#include "send.h" |
33 |
#include "s_conf.h" |
34 |
#include "s_log.h" |
35 |
#include "parse.h" |
36 |
#include "modules.h" |
37 |
#include "irc_string.h" |
38 |
|
39 |
#define HELPLEN 400 |
40 |
|
41 |
static void dohelp(struct Client *, const char *, char *); |
42 |
static void sendhelpfile(struct Client *, const char *, const char *); |
43 |
|
44 |
|
45 |
/* |
46 |
* m_help - HELP message handler |
47 |
* parv[0] = sender prefix |
48 |
*/ |
49 |
static void |
50 |
m_help(struct Client *client_p, struct Client *source_p, |
51 |
int parc, char *parv[]) |
52 |
{ |
53 |
static time_t last_used = 0; |
54 |
|
55 |
/* HELP is always local */ |
56 |
if ((last_used + ConfigFileEntry.pace_wait_simple) > CurrentTime) |
57 |
{ |
58 |
/* safe enough to give this on a local connect only */ |
59 |
sendto_one(source_p,form_str(RPL_LOAD2HI), |
60 |
me.name, source_p->name); |
61 |
return; |
62 |
} |
63 |
|
64 |
last_used = CurrentTime; |
65 |
|
66 |
dohelp(source_p, UHPATH, parv[1]); |
67 |
} |
68 |
|
69 |
/* |
70 |
* mo_help - HELP message handler |
71 |
* parv[0] = sender prefix |
72 |
*/ |
73 |
static void |
74 |
mo_help(struct Client *client_p, struct Client *source_p, |
75 |
int parc, char *parv[]) |
76 |
{ |
77 |
dohelp(source_p, HPATH, parv[1]); |
78 |
} |
79 |
|
80 |
/* |
81 |
* mo_uhelp - HELP message handler |
82 |
* This is used so that opers can view the user help file without deopering |
83 |
* parv[0] = sender prefix |
84 |
*/ |
85 |
static void |
86 |
mo_uhelp(struct Client *client_p, struct Client *source_p, |
87 |
int parc, char *parv[]) |
88 |
{ |
89 |
dohelp(source_p, UHPATH, parv[1]); |
90 |
} |
91 |
|
92 |
static void |
93 |
dohelp(struct Client *source_p, const char *hpath, char *topic) |
94 |
{ |
95 |
char h_index[] = "index"; |
96 |
char path[PATH_MAX + 1]; |
97 |
struct stat sb; |
98 |
int i; |
99 |
|
100 |
if (topic != NULL) |
101 |
{ |
102 |
if (*topic == '\0') |
103 |
topic = h_index; |
104 |
else |
105 |
{ |
106 |
/* convert to lower case */ |
107 |
for (i = 0; topic[i] != '\0'; ++i) |
108 |
topic[i] = ToLower(topic[i]); |
109 |
} |
110 |
} |
111 |
else |
112 |
topic = h_index; /* list available help topics */ |
113 |
|
114 |
if (strpbrk(topic, "/\\")) |
115 |
{ |
116 |
sendto_one(source_p, form_str(ERR_HELPNOTFOUND), |
117 |
me.name, source_p->name, topic); |
118 |
return; |
119 |
} |
120 |
|
121 |
if (strlen(hpath) + strlen(topic) + 1 > PATH_MAX) |
122 |
{ |
123 |
sendto_one(source_p, form_str(ERR_HELPNOTFOUND), |
124 |
me.name, source_p->name, topic); |
125 |
return; |
126 |
} |
127 |
|
128 |
snprintf(path, sizeof(path), "%s/%s", hpath, topic); |
129 |
|
130 |
if (stat(path, &sb) < 0) |
131 |
{ |
132 |
sendto_one(source_p, form_str(ERR_HELPNOTFOUND), |
133 |
me.name, source_p->name, topic); |
134 |
return; |
135 |
} |
136 |
|
137 |
if (!S_ISREG(sb.st_mode)) |
138 |
{ |
139 |
sendto_one(source_p, form_str(ERR_HELPNOTFOUND), |
140 |
me.name, source_p->name, topic); |
141 |
return; |
142 |
} |
143 |
|
144 |
sendhelpfile(source_p, path, topic); |
145 |
} |
146 |
|
147 |
static void |
148 |
sendhelpfile(struct Client *source_p, const char *path, const char *topic) |
149 |
{ |
150 |
FBFILE *file; |
151 |
char line[HELPLEN]; |
152 |
char started = 0; |
153 |
int type; |
154 |
|
155 |
if ((file = fbopen(path, "r")) == NULL) |
156 |
{ |
157 |
sendto_one(source_p, form_str(ERR_HELPNOTFOUND), |
158 |
me.name, source_p->name, topic); |
159 |
return; |
160 |
} |
161 |
|
162 |
if (fbgets(line, sizeof(line), file) == NULL) |
163 |
{ |
164 |
sendto_one(source_p, form_str(ERR_HELPNOTFOUND), |
165 |
me.name, source_p->name, topic); |
166 |
return; |
167 |
} |
168 |
|
169 |
else if (line[0] != '#') |
170 |
{ |
171 |
line[strlen(line) - 1] = '\0'; |
172 |
sendto_one(source_p, form_str(RPL_HELPSTART), |
173 |
me.name, source_p->name, topic, line); |
174 |
started = 1; |
175 |
} |
176 |
|
177 |
while (fbgets(line, sizeof(line), file)) |
178 |
{ |
179 |
line[strlen(line) - 1] = '\0'; |
180 |
if (line[0] != '#') |
181 |
{ |
182 |
if (!started) |
183 |
{ |
184 |
type = RPL_HELPSTART; |
185 |
started = 1; |
186 |
} |
187 |
else |
188 |
type = RPL_HELPTXT; |
189 |
|
190 |
sendto_one(source_p, form_str(RPL_HELPTXT), |
191 |
me.name, source_p->name, topic, line); |
192 |
} |
193 |
} |
194 |
|
195 |
fbclose(file); |
196 |
sendto_one(source_p, form_str(RPL_HELPTXT), |
197 |
me.name, source_p->name, topic, ""); |
198 |
sendto_one(source_p, form_str(RPL_ENDOFHELP), |
199 |
me.name, source_p->name, topic); |
200 |
} |
201 |
|
202 |
static struct Message help_msgtab = { |
203 |
"HELP", 0, 0, 0, 0, MFLG_SLOW, 0, |
204 |
{m_unregistered, m_help, m_ignore, m_ignore, mo_help, m_ignore} |
205 |
}; |
206 |
|
207 |
static struct Message uhelp_msgtab = { |
208 |
"UHELP", 0, 0, 0, 0, MFLG_SLOW, 0, |
209 |
{m_unregistered, m_help, m_ignore, m_ignore, mo_uhelp, m_ignore} |
210 |
}; |
211 |
|
212 |
static void |
213 |
module_init(void) |
214 |
{ |
215 |
mod_add_cmd(&help_msgtab); |
216 |
mod_add_cmd(&uhelp_msgtab); |
217 |
} |
218 |
|
219 |
static void |
220 |
module_exit(void) |
221 |
{ |
222 |
mod_del_cmd(&help_msgtab); |
223 |
mod_del_cmd(&uhelp_msgtab); |
224 |
} |
225 |
|
226 |
struct module module_entry = { |
227 |
.node = { NULL, NULL, NULL }, |
228 |
.name = NULL, |
229 |
.version = "$Revision$", |
230 |
.handle = NULL, |
231 |
.modinit = module_init, |
232 |
.modexit = module_exit, |
233 |
.flags = 0 |
234 |
}; |