]> jfr.im git - irc/quakenet/newserv.git/blob - helpmod2/hcommand.c
Merged revisions 310,312-314,322-323,328-329 via svnmerge from
[irc/quakenet/newserv.git] / helpmod2 / hcommand.c
1 #include <assert.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 #include "../lib/sstring.h"
6
7 #include "helpmod.h"
8 #include "hcommand.h"
9 #include "hgen.h"
10
11 hcommand* hcommand_add(const char *str, hlevel lvl, hcommand_function func, const char *command_help)
12 {
13 hcommand *tmp, **ptr = &hcommands;
14
15 assert(hcommand_get(str, lvl) == NULL);
16
17 /* Find the position */
18 for (;*ptr && (*ptr)->level < lvl;ptr = &(*ptr)->next);
19 for (;*ptr && (*ptr)->level <= lvl && strcmp(str, (*ptr)->name->content) > 0;ptr = &(*ptr)->next);
20
21 tmp = *ptr;
22 *ptr = (hcommand*)malloc(sizeof (hcommand));
23
24 (*ptr)->name = getsstring(str, strlen(str));
25 (*ptr)->help = getsstring(command_help, strlen(command_help));
26 (*ptr)->level = lvl;
27 (*ptr)->function = func;
28 (*ptr)->next = tmp;
29
30 return *ptr;
31 }
32
33 int hcommand_del(const char *str)
34 {
35 hcommand **ptr = &hcommands;
36
37 assert(hcommand_get(str, H_NONE) != NULL);
38
39 for (;*ptr;ptr = &(*ptr)->next)
40 if (!ci_strcmp(str, (*ptr)->name->content))
41 {
42 hcommand *tmp = (*ptr)->next;
43 freesstring((*ptr)->name);
44 free(*ptr);
45 *ptr = tmp;
46
47 return 0;
48 }
49 return 0;
50 }
51
52 int hcommand_del_all(void)
53 {
54 while (hcommands)
55 hcommand_del(hcommands->name->content);
56 return 0;
57 }
58
59
60 hcommand* hcommand_get(const char *str, hlevel lvl)
61 {
62 hcommand *ptr = hcommands;
63
64 for (;ptr && ptr->level <= lvl;ptr = ptr->next)
65 if (!ci_strcmp(str, ptr->name->content))
66 return ptr;
67
68 return NULL;
69 }
70
71 hcommand* hcommand_list(hlevel lvl)
72 {
73 static hcommand *position = NULL;
74 hcommand *tmp;
75
76 if (position == NULL || position->level > lvl)
77 {
78 position = hcommands;
79 return NULL;
80 }
81
82 tmp = position;
83 position = position->next;
84
85 return tmp;
86 }
87
88 int hcommand_is_command(const char *str)
89 {
90 if (*str == '-' || *str == '?')
91 return 1;
92 if ((!strncmp(str, helpmodnick->nick, strlen(helpmodnick->nick)) && str[strlen(helpmodnick->nick)] == ' '))
93 return 1;
94 return 0;
95 }