]> jfr.im git - irc/quakenet/newserv.git/blob - helpmod2/hcommand.c
Initial Import
[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 for (;*ptr && (*ptr)->level <= lvl;ptr = &(*ptr)->next);
18
19 tmp = *ptr;
20 *ptr = (hcommand*)malloc(sizeof (hcommand));
21
22 (*ptr)->name = getsstring(str, strlen(str));
23 (*ptr)->help = getsstring(command_help, strlen(command_help));
24 (*ptr)->level = lvl;
25 (*ptr)->function = func;
26 (*ptr)->next = tmp;
27
28 return *ptr;
29 }
30
31 int hcommand_del(const char *str)
32 {
33 hcommand **ptr = &hcommands;
34
35 assert(hcommand_get(str, H_NONE) != NULL);
36
37 for (;*ptr;ptr = &(*ptr)->next)
38 if (!ci_strcmp(str, (*ptr)->name->content))
39 {
40 hcommand *tmp = (*ptr)->next;
41 freesstring((*ptr)->name);
42 free(*ptr);
43 *ptr = tmp;
44
45 return 0;
46 }
47 return 0;
48 }
49
50 int hcommand_del_all(void)
51 {
52 while (hcommands)
53 hcommand_del(hcommands->name->content);
54 return 0;
55 }
56
57
58 hcommand* hcommand_get(const char *str, hlevel lvl)
59 {
60 hcommand *ptr = hcommands;
61
62 for (;ptr && ptr->level <= lvl;ptr = ptr->next)
63 if (!ci_strcmp(str, ptr->name->content))
64 return ptr;
65
66 return NULL;
67 }
68
69 hcommand* hcommand_list(hlevel lvl)
70 {
71 static hcommand *position = NULL;
72 hcommand *tmp;
73
74 if (position == NULL || position->level > lvl)
75 {
76 position = hcommands;
77 return NULL;
78 }
79
80 tmp = position;
81 position = position->next;
82
83 return tmp;
84 }
85
86 int hcommand_is_command(const char *str)
87 {
88 if (*str == '-' || *str == '?')
89 return 1;
90 if ((!strncmp(str, helpmodnick->nick, strlen(helpmodnick->nick)) && str[strlen(helpmodnick->nick)] == ' '))
91 return 1;
92 return 0;
93 }