ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/src/commandtree.C
Revision: 1.4
Committed: Wed Aug 29 08:04:33 2007 UTC (19 years, 1 month ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.3: +3 -40 lines
Log Message:
- fixed crashing bug
- transitioned myuser_t to new object system

File Contents

# Content
1 /*
2 * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team
3 * Rights to this code are as documented in doc/pod/gplicense.pod.
4 *
5 * Commandtree manipulation routines.
6 *
7 * $Id: commandtree.C,v 1.3 2007-08-28 17:08:12 pippijn Exp $
8 */
9
10 #include <algorithm>
11
12 #include "atheme.h"
13 #include "users.h"
14 #include "privs.h"
15
16 static char const rcsid[] = "$Id: commandtree.C,v 1.3 2007-08-28 17:08:12 pippijn Exp $";
17
18 cmdvec null_cmdvec;
19
20 struct cmd_eq
21 {
22 cmd_eq () : cmd (0) { }
23 cmd_eq (char const * const command) : cmd (command) { }
24 cmd_eq (command_t const &command) : cmd (command.name) { }
25
26 bool operator () (command_t const *c)
27 {
28 return !strcasecmp (c->name, cmd);
29 }
30
31 /******/
32
33 bool operator () (char const *c1, command_t const *c2)
34 {
35 return !strcasecmp (c1, c2->name);
36 }
37
38 bool operator () (command_t const *c1, char const *c2)
39 {
40 return !strcasecmp (c1->name, c2);
41 }
42
43 bool operator () (command_t const *c1, command_t const *c2)
44 {
45 return !strcasecmp (c1->name, c2->name);
46 }
47
48 private:
49 char const * const cmd;
50 } cmd_compare;
51
52 /*
53 * command_add_many()
54 *
55 * Inputs:
56 * array of commands to add, list to add them to.
57 *
58 * Output:
59 * none
60 *
61 * Side Effects:
62 * adds an array of commands to a command list,
63 * via command_add().
64 */
65 void
66 operator << (cmdvec &commandlist, command_t const &cmd)
67 {
68 if (std::find_if (commandlist.begin (), commandlist.end (), cmd_eq (cmd)) != commandlist.end ())
69 {
70 slog (LG_INFO, "command_add(): command %s already in the list", cmd.name);
71 return;
72 }
73
74 commandlist.push_back (&cmd);
75 }
76
77 void
78 operator << (cmdvec &commandlist, command_t const *cmd[])
79 {
80 unsigned int i;
81
82 for (i = 0; cmd[i] != NULL; i++)
83 commandlist << *cmd[i];
84 }
85
86 /*
87 * command_delete_many()
88 *
89 * Inputs:
90 * array of commands to delete, list to delete them from.
91 *
92 * Output:
93 * none
94 *
95 * Side Effects:
96 * deletes an array of commands from a command list,
97 * via command_delete().
98 */
99 void
100 operator >> (cmdvec &commandlist, command_t const &cmd)
101 {
102 commandlist.erase (std::find_if (commandlist.begin (), commandlist.end (), cmd_eq (cmd)));
103 }
104
105 void
106 operator >> (cmdvec &commandlist, command_t const *cmd[])
107 {
108 unsigned int i;
109
110 for (i = 0; cmd[i] != NULL; i++)
111 commandlist >> *cmd[i];
112 }
113
114 command_t const *
115 cmdvec::find (char const *command)
116 {
117 cmdvec::iterator et = end ();
118 cmdvec::iterator it = std::find_if (begin (), et, cmd_eq (command));
119
120 if (it != et)
121 return *it;
122
123 return NULL;
124 }
125
126 void
127 command_t::exec (service_t *svs, sourceinfo_t *si, int parc, char *parv[]) const
128 {
129 if (has_priv (si, access))
130 {
131 cmd (si, parc, parv);
132 return;
133 }
134
135 if (has_any_privs (si))
136 command_fail (si, fault_noprivs, _("You do not have %s privilege."), access);
137 else
138 command_fail (si, fault_noprivs, _("You are not authorized to perform this operation."));
139 /*snoop(_("DENIED CMD: \2%s\2 used %s %s"), origin, svs->name, cmd); */
140 }
141
142 void
143 command_exec_split (service_t *svs, sourceinfo_t *si, char const * const cmd, char const * const text, cmdvec &commandlist)
144 {
145 size_t i;
146 char *parv[20];
147 command_t const *c;
148
149 if ((c = commandlist.find (cmd)))
150 {
151 unsigned const parc = sjtoken (text, ' ', parv, c->maxparc);
152 for (i = parc; i < (sizeof (parv) / sizeof (parv[0])); i++)
153 parv[i] = NULL;
154 c->exec (svs, si, parc, parv);
155 }
156 else
157 notice (svs->name, si->su->nick, _("Invalid command. Use \2/%s%s help\2 for a command listing."), (ircd->uses_rcommand == false) ? "msg " : "", svs->disp);
158 }
159
160 /*
161 * command_help
162 * Iterates the command tree and lists available commands.
163 *
164 * inputs -
165 * si: The origin of the request.
166 * commandtree: The command tree being listed.
167 *
168 * outputs -
169 * A list of available commands.
170 */
171 struct render_help
172 {
173 render_help (sourceinfo_t *info)
174 : si (info)
175 {
176 }
177
178 void operator () (command_t const *c)
179 {
180 /* show only the commands we have access to
181 * (taken from command_exec())
182 */
183 if (has_priv (si, c->access))
184 command_success_nodata (si, "\2%-15s\2 %s", c->name, c->desc);
185 }
186
187 sourceinfo_t *si;
188 };
189
190 void
191 command_help (sourceinfo_t *si, cmdvec &commandlist)
192 {
193 if (si->service == NULL || si->service->cmdtree == &commandlist)
194 command_success_nodata (si, _("The following commands are available:"));
195 else
196 command_success_nodata (si, _("The following subcommands are available:"));
197
198 std::for_each (commandlist.begin (), commandlist.end (), render_help (si));
199 }
200
201 /* name1 name2 name3... */
202 static bool
203 string_in_list (char const *str, char const *name)
204 {
205 char *p;
206 int l;
207
208 if (str == NULL)
209 return false;
210 l = strlen (name);
211 while (*str != '\0')
212 {
213 p = strchr (str, ' ');
214 if (p != NULL ? p - str == l && !strncasecmp (str, name, p - str) : !strcasecmp (str, name))
215 return true;
216 if (p == NULL)
217 return false;
218 str = p;
219 while (*str == ' ')
220 str++;
221 }
222 return false;
223 }
224
225 /*
226 * command_help_short
227 * Iterates over the command tree and lists available commands.
228 *
229 * inputs -
230 * mynick: The nick of the services bot sending out the notices.
231 * origin: The origin of the request.
232 * commandtree: The command tree being listed.
233 * maincmds: The commands to list verbosely.
234 *
235 * outputs -
236 * A list of available commands.
237 */
238 struct render_help_short
239 {
240 render_help_short (sourceinfo_t *info, char const * const cmdlist)
241 : si (info), maincmds (cmdlist)
242 {
243 }
244
245 void operator () (command_t const *c)
246 {
247 /* show only the commands we have access to
248 * (taken from command_exec())
249 */
250 if (string_in_list (maincmds, c->name) && has_priv (si, c->access))
251 command_success_nodata (si, "\2%-15s\2 %s", c->name, c->desc);
252 }
253
254 private:
255 sourceinfo_t *si;
256 char const * const maincmds;
257 };
258
259 struct render_list
260 {
261 render_list (sourceinfo_t *info, dynstr &str, char const * const cmdlist, size_t length)
262 : si (info), buf (str), maincmds (cmdlist), indent (length)
263 {
264 }
265
266 void operator () (command_t const *c)
267 {
268 /* show only the commands we have access to
269 * (taken from command_exec())
270 */
271 size_t l = indent;
272 if (!string_in_list (maincmds, c->name) && has_priv (si, c->access))
273 {
274 if (buf.length () > l)
275 buf.append (", ", 2);
276 if (buf.length () > 55)
277 {
278 command_success_nodata (si, "%s", buf.c_str ());
279 buf.reset ();
280 while (--l > 0)
281 buf.append (' ');
282 buf.append (' ');
283 l = indent;
284 }
285 buf.append (c->name, strlen (c->name));
286 }
287 }
288
289 private:
290 sourceinfo_t *si;
291 dynstr &buf;
292 char const * const maincmds;
293 size_t indent;
294 };
295
296 void
297 command_help_short (sourceinfo_t *si, cmdvec &commandlist, char const * const maincmds)
298 {
299 size_t l;
300 char *lbuf;
301 dynstr buf (256);
302
303 if (si->service == NULL || si->service->cmdtree == &commandlist)
304 command_success_nodata (si, _("The following commands are available:"));
305 else
306 command_success_nodata (si, _("The following subcommands are available:"));
307
308 std::for_each (commandlist.begin (), commandlist.end (), render_help_short (si, maincmds));
309
310 command_success_nodata (si, " ");
311 lbuf = _("Other commands: ");
312 buf.append (lbuf, l = strlen (lbuf));
313
314 std::for_each (commandlist.begin (), commandlist.end (), render_list (si, buf, maincmds, l));
315
316 if (buf.length () > l)
317 command_success_nodata (si, "%s", buf.c_str ());
318 }