]>
Commit | Line | Data |
---|---|---|
c86edd1d Q |
1 | #include <stdio.h> |
2 | #include <string.h> | |
3 | #include <stdarg.h> | |
4 | #include <ctype.h> | |
5 | ||
6 | #include "helpmod_entries.h" | |
7 | #include "helpmod_user.h" | |
8 | #include "helpmod_alias.h" | |
9 | ||
10 | #include "../localuser/localuser.h" | |
11 | #include "../localuser/localuserchannel.h" | |
12 | #include "../control/control.h" | |
13 | #include "../channel/channel.h" | |
14 | #include "../nick/nick.h" | |
15 | #include "../core/config.h" | |
16 | #include "../core/schedule.h" | |
17 | ||
18 | #define HELPMOD_VERSION "1.05" | |
19 | ||
20 | helpmod_entry helpmod_base; | |
21 | helpmod_user helpmod_users; | |
22 | alias_tree aliases; | |
23 | nick *helpmodnick; | |
24 | char helpmod_db[128] = "helpmod/default"; | |
25 | long helpmod_usage; | |
26 | ||
27 | void helpreply(nick *target, char *message, ... ) { | |
28 | char buf[512]; | |
29 | va_list va; | |
30 | ||
31 | if (helpmodnick==NULL) { | |
32 | return; | |
33 | } | |
34 | ||
35 | va_start(va,message); | |
36 | vsnprintf(buf,512,message,va); | |
37 | va_end(va); | |
38 | ||
39 | sendmessagetouser(helpmodnick,target,"%s",buf); | |
40 | } | |
41 | ||
42 | void helpmod_send_help(nick* target, helpmod_user hlp_user) | |
43 | { | |
44 | int i; | |
45 | helpmod_usage++; | |
46 | for (i=0;i<hlp_user->state->text_lines;i++) | |
47 | helpreply(target, "%s", hlp_user->state->text[i]->content); | |
48 | for (i=0;i<hlp_user->state->option_count;i++) | |
49 | helpreply(target, "%d) %s", i+1, hlp_user->state->options[i]->description->content); | |
50 | if (!hlp_user->state->option_count) | |
51 | { | |
52 | helpreply(target, "This concludes the help for this topic, if you want to, you can restart the service with the 'help' command, or return to the previous entry by selecting 0"); | |
53 | } | |
54 | } | |
55 | ||
56 | void helpmod_cmd_help (nick* sender, char* arg) | |
57 | { | |
58 | helpmod_user hlp_user = helpmod_get_user(sender->numeric); | |
59 | int hlp_target; | |
60 | int i; | |
61 | ||
62 | if (!helpmod_base) | |
63 | { | |
64 | helpreply(sender, "The help service is not available at this time, please try again later"); | |
65 | return; | |
66 | } | |
67 | ||
68 | if (!arg) | |
69 | { | |
70 | hlp_user->state = helpmod_base; | |
71 | helpmod_send_help(sender, hlp_user); | |
72 | return; | |
73 | } | |
74 | else | |
75 | if (!sscanf(arg, "%d", &hlp_target)) | |
76 | { | |
77 | helpmod_entry tmp; | |
78 | for(i=0;i<strlen(arg);i++) | |
79 | if (isspace(arg[i])) | |
80 | { | |
81 | arg[i] = 0x00; | |
82 | break; | |
83 | } | |
84 | tmp = helpmod_get_alias(arg); | |
85 | if (!tmp) | |
86 | helpreply(sender, "Invalid value. Either use 'help' to restart or give an integer as a valid selection"); | |
87 | else | |
88 | { | |
89 | hlp_user->state = tmp; | |
90 | helpmod_send_help(sender, hlp_user); | |
91 | } | |
92 | return; | |
93 | } | |
94 | ||
95 | hlp_target--; | |
96 | if (!helpmod_valid_selection(hlp_user->state, hlp_target)) | |
97 | { | |
98 | if (!hlp_user->state->option_count) | |
99 | helpreply(sender, "There are no more options to choose from, you can restart the service by giving the 'help' command or select 0 to return to previous entry"); | |
100 | else if (!hlp_user->state->parent) | |
101 | helpreply(sender, "Bad selection, please enter your selection as an integer from %d to %d", 1, hlp_user->state->option_count); | |
102 | else | |
103 | helpreply(sender, "Bad selection, please enter your selection as an integer from %d to %d, selecting 0 will take you to the previous entry", 1, hlp_user->state->option_count); | |
104 | return; | |
105 | } | |
106 | ||
107 | hlp_user->state = helpmod_make_selection(hlp_user->state, hlp_target); | |
108 | helpmod_send_help(sender, hlp_user); | |
109 | ||
110 | return; | |
111 | } | |
112 | ||
113 | void helpmod_cmd_stats (nick *sender) | |
114 | { | |
115 | helpreply(sender, "Amount of users: %9ld", helpmod_user_count()); | |
116 | helpreply(sender, " entries: %9ld", helpmod_entry_count(helpmod_base)); | |
117 | helpreply(sender, " aliases: %9ld", helpmod_alias_count(aliases)); | |
118 | helpreply(sender, " service use: %9ld", helpmod_usage); | |
119 | helpreply(sender, "Using database: %s", helpmod_db); | |
120 | return; | |
121 | } | |
122 | ||
123 | void helpmod_cmd_load (void *sender, char* arg) | |
124 | { | |
125 | FILE *tmp_file; | |
126 | char buf[128] = "helpmod/default"; | |
127 | ||
128 | if (!arg) | |
129 | helpreply(sender, "Warning: No config specified, using default"); | |
130 | else | |
131 | { | |
132 | if (strlen(arg) >= 100) | |
133 | return; | |
134 | else | |
135 | sprintf(buf, "helpmod/%s", arg); | |
136 | } | |
137 | ||
138 | if (!(tmp_file = fopen(buf, "rt"))) | |
139 | { | |
140 | helpreply(sender, "File %s not found", buf); | |
141 | return; | |
142 | } | |
143 | else | |
144 | fclose(tmp_file); | |
145 | ||
146 | helpmod_clear_aliases(&aliases); | |
147 | helpmod_clear_all_entries(); | |
148 | helpmod_init_entry(&helpmod_base); | |
149 | helpmod_clear_users(); | |
150 | helpmod_load_entries(buf); | |
151 | strcpy(helpmod_db, buf); | |
152 | helpreply(sender, "New config loaded and system reset"); | |
153 | } | |
154 | ||
155 | void helpmod_cmd_showcommands (nick *sender) | |
156 | { | |
157 | helpreply(sender,"HelpMod %s Commands", HELPMOD_VERSION); | |
158 | helpreply(sender,"help Primary command, gives automated help to lusers"); | |
159 | helpreply(sender,"stats Shows some service statistics"); | |
160 | helpreply(sender,"aliases Gives a simple list of aliases currently in use"); | |
161 | helpreply(sender,"load Loads a new help config"); | |
162 | return; | |
163 | } | |
164 | ||
165 | void helpmod_cmd_aliases (nick *sender) | |
166 | { | |
167 | char buf[512]; | |
168 | int i = 0; | |
169 | void helpmod_list_aliases(alias_tree node) | |
170 | { | |
171 | if (i > 256) | |
172 | { | |
173 | helpreply(sender, buf); | |
174 | i = 0; | |
175 | } | |
176 | if (!node) | |
177 | return; | |
178 | sprintf(buf+i,"%.200s ",node->name->content); | |
179 | i+=(1+strlen(node->name->content)); | |
180 | helpmod_list_aliases(node->left); | |
181 | helpmod_list_aliases(node->right); | |
182 | } | |
183 | helpmod_list_aliases(aliases); | |
184 | if (i) | |
185 | helpreply(sender, buf); | |
186 | } | |
187 | ||
188 | void helpmodmessagehandler(nick *sender, int messagetype, void **args) | |
189 | { | |
190 | char* first_arg; | |
191 | char* second_arg = NULL; | |
192 | nick *target; | |
193 | int i, useless_var; | |
194 | ||
195 | if (messagetype != LU_PRIVMSG) | |
196 | return; | |
197 | ||
198 | target = getnickbynick((char*)args[0]); | |
199 | first_arg = args[1]; | |
200 | ||
201 | for (i=0;i<strlen(args[1]);i++) | |
202 | if (((char*)args[1])[i] == ' ') | |
203 | { | |
204 | second_arg = args[1] + i + 1; | |
205 | ((char*)args[1])[i] = 0x00; | |
206 | break; | |
207 | } | |
208 | if (!strcmp(first_arg, "help")) | |
209 | { | |
210 | helpmod_cmd_help(target, second_arg); | |
211 | return; | |
212 | } | |
213 | if (sscanf(first_arg, "%d", &useless_var)) | |
214 | { | |
215 | helpmod_cmd_help(target, first_arg); | |
216 | return; | |
217 | } | |
218 | if (!IsOper(target)) | |
219 | { | |
220 | helpreply(target, "To access the help service, just type 'help'"); | |
221 | return; | |
222 | } | |
223 | if (!strcmp(first_arg, "stats")) | |
224 | helpmod_cmd_stats(target); | |
225 | else if (!strcmp(first_arg, "load")) | |
226 | helpmod_cmd_load(target, second_arg); | |
227 | else if (!strcmp(first_arg,"aliases")) | |
228 | helpmod_cmd_aliases(target); | |
229 | else if (!strcmp(first_arg, "showcommands")) | |
230 | helpmod_cmd_showcommands(target); | |
231 | else if (!strcmp(first_arg, "help")) | |
232 | helpmod_cmd_help(target, first_arg); | |
233 | else | |
234 | helpreply(target, "Unknown command, see 'showcommands'"); | |
235 | } | |
236 | ||
237 | void helpconnect(void) { | |
238 | channel *cp; | |
239 | /* helpmod offers no channel features, it's merely a decoration */ | |
240 | char *helpmod_chans[] = {"#twilightzone", "#help", "#feds", "#qnet.help"}; | |
241 | int i,helpmod_chan_count = 4; | |
242 | ||
243 | helpmodnick=registerlocaluser("H", | |
244 | "help", | |
245 | "quakenet.org", | |
246 | "NewServ HelpMod, /msg H help", | |
247 | "H", | |
248 | UMODE_DEAF|UMODE_OPER|UMODE_ACCOUNT,&helpmodmessagehandler); | |
249 | ||
250 | helpmod_init_entry(&helpmod_base); | |
251 | helpmod_init_users(); | |
252 | helpmod_load_entries(NULL); | |
253 | /* join channels */ | |
254 | for (i=0;i < helpmod_chan_count;i++) | |
255 | { | |
256 | cp = findchannel(helpmod_chans[i]); | |
257 | if (!cp) | |
258 | localcreatechannel(helpmodnick, helpmod_chans[i]); | |
259 | else | |
260 | { | |
261 | localjoinchannel(helpmodnick, cp); | |
262 | localgetops(helpmodnick, cp); | |
263 | } | |
264 | } | |
265 | } | |
266 | ||
267 | ||
268 | void _init() | |
269 | { | |
270 | helpmod_usage = 0; | |
271 | helpmod_base = NULL; | |
272 | aliases = NULL; | |
273 | scheduleoneshot(time(NULL)+1,(ScheduleCallback)&helpconnect,NULL); | |
274 | schedulerecurring(time(NULL)+1,0,300,(ScheduleCallback)&helpmod_clear_inactives,NULL); | |
275 | } | |
276 | ||
277 | void _fini() | |
278 | { | |
279 | deregisterlocaluser(helpmodnick, "Module unloaded"); | |
280 | deleteallschedules((ScheduleCallback)&helpconnect); | |
281 | helpmod_clear_aliases(&aliases); | |
282 | helpmod_clear_all_entries(); | |
283 | helpmod_clear_users(); | |
284 | } |