ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/src/commandtree.C
Revision: 1.5
Committed: Wed Aug 29 21:01:18 2007 UTC (19 years ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.4: +3 -5 lines
Log Message:
- reduced ifdefs by moving __GNUC__ and friends to svsconfig.h
- #define to enum { } in tools.h
- corrected log levels a bit
- made timersub an inline function instead of a macro
- added a simple garbage collection mechanism for postponed freeing of lost
  memory chunks
- enhanced type_traits
- merged inspircd1.2 support with upstream
- reformatting
- renamed TTP to a more "standard" PRItime and STP to PRIsize

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.4 2007-08-29 08:04:33 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.4 2007-08-29 08:04:33 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] = { 0 };
147 command_t const *c;
148
149 if ((c = commandlist.find (cmd)))
150 {
151 unsigned const parc = sjtoken (text, ' ', parv, c->maxparc);
152 c->exec (svs, si, parc, parv);
153 }
154 else
155 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);
156 }
157
158 /*
159 * command_help
160 * Iterates the command tree and lists available commands.
161 *
162 * inputs -
163 * si: The origin of the request.
164 * commandtree: The command tree being listed.
165 *
166 * outputs -
167 * A list of available commands.
168 */
169 struct render_help
170 {
171 render_help (sourceinfo_t *info)
172 : si (info)
173 {
174 }
175
176 void operator () (command_t const *c)
177 {
178 /* show only the commands we have access to
179 * (taken from command_exec())
180 */
181 if (has_priv (si, c->access))
182 command_success_nodata (si, "\2%-15s\2 %s", c->name, c->desc);
183 }
184
185 sourceinfo_t *si;
186 };
187
188 void
189 command_help (sourceinfo_t *si, cmdvec &commandlist)
190 {
191 if (si->service == NULL || si->service->cmdtree == &commandlist)
192 command_success_nodata (si, _("The following commands are available:"));
193 else
194 command_success_nodata (si, _("The following subcommands are available:"));
195
196 std::for_each (commandlist.begin (), commandlist.end (), render_help (si));
197 }
198
199 /* name1 name2 name3... */
200 static bool
201 string_in_list (char const *str, char const *name)
202 {
203 char *p;
204 int l;
205
206 if (str == NULL)
207 return false;
208 l = strlen (name);
209 while (*str != '\0')
210 {
211 p = strchr (str, ' ');
212 if (p != NULL ? p - str == l && !strncasecmp (str, name, p - str) : !strcasecmp (str, name))
213 return true;
214 if (p == NULL)
215 return false;
216 str = p;
217 while (*str == ' ')
218 str++;
219 }
220 return false;
221 }
222
223 /*
224 * command_help_short
225 * Iterates over the command tree and lists available commands.
226 *
227 * inputs -
228 * mynick: The nick of the services bot sending out the notices.
229 * origin: The origin of the request.
230 * commandtree: The command tree being listed.
231 * maincmds: The commands to list verbosely.
232 *
233 * outputs -
234 * A list of available commands.
235 */
236 struct render_help_short
237 {
238 render_help_short (sourceinfo_t *info, char const * const cmdlist)
239 : si (info), maincmds (cmdlist)
240 {
241 }
242
243 void operator () (command_t const *c)
244 {
245 /* show only the commands we have access to
246 * (taken from command_exec())
247 */
248 if (string_in_list (maincmds, c->name) && has_priv (si, c->access))
249 command_success_nodata (si, "\2%-15s\2 %s", c->name, c->desc);
250 }
251
252 private:
253 sourceinfo_t *si;
254 char const * const maincmds;
255 };
256
257 struct render_list
258 {
259 render_list (sourceinfo_t *info, dynstr &str, char const * const cmdlist, size_t length)
260 : si (info), buf (str), maincmds (cmdlist), indent (length)
261 {
262 }
263
264 void operator () (command_t const *c)
265 {
266 /* show only the commands we have access to
267 * (taken from command_exec())
268 */
269 size_t l = indent;
270 if (!string_in_list (maincmds, c->name) && has_priv (si, c->access))
271 {
272 if (buf.length () > l)
273 buf.append (", ", 2);
274 if (buf.length () > 55)
275 {
276 command_success_nodata (si, "%s", buf.c_str ());
277 buf.reset ();
278 while (--l > 0)
279 buf.append (' ');
280 buf.append (' ');
281 l = indent;
282 }
283 buf.append (c->name, strlen (c->name));
284 }
285 }
286
287 private:
288 sourceinfo_t *si;
289 dynstr &buf;
290 char const * const maincmds;
291 size_t indent;
292 };
293
294 void
295 command_help_short (sourceinfo_t *si, cmdvec &commandlist, char const * const maincmds)
296 {
297 size_t l;
298 char *lbuf;
299 dynstr buf (256);
300
301 if (si->service == NULL || si->service->cmdtree == &commandlist)
302 command_success_nodata (si, _("The following commands are available:"));
303 else
304 command_success_nodata (si, _("The following subcommands are available:"));
305
306 std::for_each (commandlist.begin (), commandlist.end (), render_help_short (si, maincmds));
307
308 command_success_nodata (si, " ");
309 lbuf = _("Other commands: ");
310 buf.append (lbuf, l = strlen (lbuf));
311
312 std::for_each (commandlist.begin (), commandlist.end (), render_list (si, buf, maincmds, l));
313
314 if (buf.length () > l)
315 command_success_nodata (si, "%s", buf.c_str ());
316 }