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