1 |
pippijn |
1.1 |
/* |
2 |
|
|
* Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team |
3 |
|
|
* Copyright © 2005-2006 Atheme Development Group |
4 |
|
|
* Rights to this code are documented in doc/LICENSE. |
5 |
|
|
* |
6 |
|
|
* This file contains a generic help system implementation. |
7 |
|
|
* |
8 |
|
|
* $Id: help.C,v 1.1 2007-07-11 11:04:47 pippijn Exp $ |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
#include "atheme.h" |
12 |
|
|
|
13 |
|
|
static char const rcsid[] = "$Id: help.C,v 1.1 2007-07-11 11:04:47 pippijn Exp $"; |
14 |
|
|
|
15 |
|
|
struct help_eq |
16 |
|
|
{ |
17 |
|
|
help_eq () { } |
18 |
|
|
help_eq (char *cmd) : cmd (cmd) { } |
19 |
|
|
|
20 |
|
|
bool operator () (helpentry_t *h) |
21 |
|
|
{ |
22 |
|
|
return !strcasecmp (h->name, cmd); |
23 |
|
|
} |
24 |
|
|
|
25 |
|
|
/******/ |
26 |
|
|
|
27 |
|
|
bool operator () (char *h1, helpentry_t *h2) |
28 |
|
|
{ |
29 |
|
|
return !strcasecmp (h1, h2->name); |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
bool operator () (helpentry_t *h1, char *h2) |
33 |
|
|
{ |
34 |
|
|
return !strcasecmp (h1->name, h2); |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
bool operator () (helpentry_t *h1, helpentry_t *h2) |
38 |
|
|
{ |
39 |
|
|
return !strcasecmp (h1->name, h2->name); |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
private: |
43 |
|
|
char *cmd; |
44 |
|
|
}; |
45 |
|
|
|
46 |
|
|
static helpentry_t * |
47 |
|
|
help_cmd_find (sourceinfo_t *si, char *cmd, helpvec *list) |
48 |
|
|
{ |
49 |
|
|
helpvec::iterator he = std::find_if (list->begin (), list->end (), help_eq (cmd)); |
50 |
|
|
|
51 |
|
|
if (he != list->end ()) |
52 |
|
|
return *he; |
53 |
|
|
|
54 |
|
|
command_fail (si, fault_nosuch_target, _("No help available for \2%s\2."), cmd); |
55 |
|
|
return NULL; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
void |
59 |
|
|
help_display (sourceinfo_t *si, char *command, helpvec *list) |
60 |
|
|
{ |
61 |
|
|
helpentry_t *c; |
62 |
|
|
FILE *help_file; |
63 |
|
|
char buf[BUFSIZE]; |
64 |
|
|
|
65 |
|
|
/* take the command through the hash table */ |
66 |
|
|
if ((c = help_cmd_find (si, command, list))) |
67 |
|
|
{ |
68 |
|
|
if (c->file) |
69 |
|
|
{ |
70 |
|
|
if (*c->file == '/') |
71 |
|
|
help_file = fopen (c->file, "r"); |
72 |
|
|
else |
73 |
|
|
{ |
74 |
|
|
snprintf (buf, sizeof buf, "%s/%s", SHAREDIR, c->file); |
75 |
|
|
if (nicksvs.no_nick_ownership && !strncmp (c->file, "help/nickserv/", 14)) |
76 |
|
|
memcpy (buf + (sizeof (SHAREDIR) - 1) + 6, "userserv", 8); |
77 |
|
|
help_file = fopen (buf, "r"); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
if (!help_file) |
81 |
|
|
{ |
82 |
|
|
command_fail (si, fault_nosuch_target, _("Could not get help file for \2%s\2."), command); |
83 |
|
|
return; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
command_success_nodata (si, _("***** \2%s Help\2 *****"), si->service->name); |
87 |
|
|
|
88 |
|
|
while (fgets (buf, BUFSIZE, help_file)) |
89 |
|
|
{ |
90 |
|
|
strip (buf); |
91 |
|
|
|
92 |
|
|
replace (buf, sizeof (buf), "&nick&", si->service->disp); |
93 |
|
|
|
94 |
|
|
if (buf[0]) |
95 |
|
|
command_success_nodata (si, "%s", buf); |
96 |
|
|
else |
97 |
|
|
command_success_nodata (si, " "); |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
fclose (help_file); |
101 |
|
|
|
102 |
|
|
command_success_nodata (si, _("***** \2End of Help\2 *****")); |
103 |
|
|
} |
104 |
|
|
else if (c->func) |
105 |
|
|
{ |
106 |
|
|
command_success_nodata (si, _("***** \2%s Help\2 *****"), si->service->name); |
107 |
|
|
|
108 |
|
|
c->func (si); |
109 |
|
|
|
110 |
|
|
command_success_nodata (si, _("***** \2End of Help\2 *****")); |
111 |
|
|
} |
112 |
|
|
else |
113 |
|
|
command_fail (si, fault_nosuch_target, _("No help available for \2%s\2."), command); |
114 |
|
|
} |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
void |
118 |
|
|
help_addentry (helpvec *list, char *topic, char *fname, void (*func) (sourceinfo_t *si)) |
119 |
|
|
{ |
120 |
|
|
help_eq help_compare; |
121 |
|
|
helpentry_t *he; |
122 |
|
|
// helpvec::iterator heit; |
123 |
|
|
|
124 |
|
|
if (!list && !topic && !func && !fname) |
125 |
|
|
{ |
126 |
|
|
slog (LG_DEBUG, "help_addentry(): invalid params"); |
127 |
|
|
return; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
/* further paranoia */ |
131 |
|
|
if (!func && !fname) |
132 |
|
|
{ |
133 |
|
|
slog (LG_DEBUG, "help_addentry(): invalid params"); |
134 |
|
|
return; |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
#if 0 |
138 |
|
|
heit = std::find_if (list->begin (), list->end (), help_eq (topic)); |
139 |
|
|
if (heit != list->end ()) |
140 |
|
|
{ |
141 |
|
|
slog (LG_DEBUG, "help_addentry(): trying to add an existing helpentry: %s", topic); |
142 |
|
|
return; |
143 |
|
|
} |
144 |
|
|
#endif |
145 |
|
|
|
146 |
|
|
he = new helpentry_t; |
147 |
|
|
|
148 |
|
|
he->name = sstrdup (topic); |
149 |
|
|
|
150 |
|
|
if (func != NULL) |
151 |
|
|
he->func = func; |
152 |
|
|
else if (fname != NULL) |
153 |
|
|
he->file = sstrdup (fname); |
154 |
|
|
|
155 |
|
|
list->push_back (he); |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
void |
159 |
|
|
help_delentry (helpvec *list, char *name) |
160 |
|
|
{ |
161 |
|
|
help_eq help_compare; |
162 |
|
|
helpvec::iterator he = std::find_if (list->begin (), list->end (), help_eq (name)); |
163 |
|
|
|
164 |
|
|
#if 0 |
165 |
|
|
if (he == list->end ()) |
166 |
|
|
{ |
167 |
|
|
slog (LG_DEBUG, "help_delentry(): trying to delete a nonexistent help entry: %s", name); |
168 |
|
|
return; |
169 |
|
|
} |
170 |
|
|
#endif |
171 |
|
|
|
172 |
|
|
free ((*he)->name); |
173 |
|
|
|
174 |
|
|
if ((*he)->file != NULL) |
175 |
|
|
free ((*he)->file); |
176 |
|
|
|
177 |
|
|
(*he)->func = NULL; |
178 |
|
|
delete *he; |
179 |
|
|
|
180 |
|
|
list->erase (he); |
181 |
|
|
} |