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