]> jfr.im git - irc/quakenet/newserv.git/blob - qabot/qabot_commands.c
b4ad3d4b10138a535e11c73c6045ab5e9c33c277
[irc/quakenet/newserv.git] / qabot / qabot_commands.c
1 #include <stdio.h>
2 #include <time.h>
3
4 #include "../nick/nick.h"
5 #include "../localuser/localuserchannel.h"
6 #include "../core/hooks.h"
7 #include "../core/schedule.h"
8 #include "../lib/array.h"
9 #include "../lib/base64.h"
10 #include "../lib/irc_string.h"
11 #include "../lib/splitline.h"
12
13 #include "qabot.h"
14
15 int qabot_doshowcommands(void* sender, int cargc, char** cargv) {
16 nick* np = (nick*)sender;
17 Command* cmdlist[150];
18 int i, j;
19
20 sendnoticetouser(qabot_nick, np, "The following commands are registered at present:");
21 for (i = 0, j = getcommandlist(qabot_commands, cmdlist, 150); i < j; i++) {
22 if (cmdlist[i]->level)
23 sendnoticetouser(qabot_nick, np, "%s (%s)", cmdlist[i]->command->content, qabot_uflagtostr(cmdlist[i]->level));
24 else
25 sendnoticetouser(qabot_nick, np, "%s", cmdlist[i]->command->content);
26 }
27 sendnoticetouser(qabot_nick, np, "End of list.");
28
29 return CMD_OK;
30 }
31
32 int qabot_dohelp(void* sender, int cargc, char** cargv) {
33 nick* np = (nick*)sender;
34
35 if (cargc < 1) {
36 sendnoticetouser(qabot_nick, np, "Syntax: help <command>");
37 return CMD_ERROR;
38 }
39
40 return qabot_showhelp(np, cargv[0]);
41 }
42
43 int qabot_dohello(void* sender, int cargc, char** cargv) {
44 nick* np = (nick*)sender;
45
46 if (!IsOper(np)) {
47 sendnoticetouser(qabot_nick, np, "You must be opered to use this command.");
48 return CMD_ERROR;
49 }
50
51 if (qabot_users) {
52 sendnoticetouser(qabot_nick, np, "This command cannot be used once the initial account has been created.");
53 return CMD_ERROR;
54 }
55
56 qabot_adduser(np->authname, QAUFLAG_STAFF|QAUFLAG_ADMIN|QAUFLAG_DEVELOPER, time(0));
57 qabot_savedb();
58 sendnoticetouser(qabot_nick, np, "An account has been created with admin privileges.");
59
60 return CMD_OK;
61 }
62
63 int qabot_dosave(void* sender, int cargc, char** cargv) {
64 nick* np = (nick*)sender;
65
66 sendnoticetouser(qabot_nick, np, "Saving...");
67 qabot_savedb();
68 sendnoticetouser(qabot_nick, np, "Done.");
69
70 return CMD_OK;
71 }
72
73 int qabot_dolistbots(void* sender, int cargc, char** cargv) {
74 nick* np = (nick*)sender;
75 qab_bot* b;
76
77 if (!qab_bots) {
78 sendnoticetouser(qabot_nick, np, "There are no bots currently added.");
79 return CMD_OK;
80 }
81
82 sendnoticetouser(qabot_nick, np, "The following bots are added:");
83 for (b = qab_bots; b; b = b->next)
84 sendnoticetouser(qabot_nick, np, "%s (%s@%s)", b->nick, b->user, b->host);
85 sendnoticetouser(qabot_nick, np, "End of list.");
86
87 return CMD_OK;
88 }
89
90 int qabot_dolistusers(void* sender, int cargc, char** cargv) {
91 nick* np = (nick*)sender;
92 qab_user* u;
93
94 if (!qabot_users) {
95 sendnoticetouser(qabot_nick, np, "There are no users currently added.");
96 return CMD_OK;
97 }
98
99 sendnoticetouser(qabot_nick, np, "The following users are added:");
100 for (u = qabot_users; u; u = u->next)
101 sendnoticetouser(qabot_nick, np, "%-15s (%s)", u->authname, qabot_uflagtostr(u->flags));
102 sendnoticetouser(qabot_nick, np, "End of list.");
103
104 return CMD_OK;
105 }
106
107 int qabot_doshowbot(void* sender, int cargc, char** cargv) {
108 nick* np = (nick*)sender;
109 qab_bot* bot;
110 nick* botnick;
111
112 if (cargc < 1) {
113 sendnoticetouser(qabot_nick, np, "Syntax: showbot <nick>");
114 return CMD_ERROR;
115 }
116
117 if (!(botnick = getnickbynick(cargv[0]))) {
118 sendnoticetouser(qabot_nick, np, "No such nickname.");
119 return CMD_ERROR;
120 }
121
122 bot = botnick->exts[qabot_nickext];
123 if (!bot) {
124 sendnoticetouser(qabot_nick, np, "%s is not a QABot.", botnick->nick);
125 return CMD_ERROR;
126 }
127
128 sendnoticetouser(qabot_nick, np, "- Information for %s ---", bot->nick);
129 sendnoticetouser(qabot_nick, np, "User: %s", bot->user);
130 sendnoticetouser(qabot_nick, np, "Host: %s", bot->host);
131 sendnoticetouser(qabot_nick, np, "Public chan: %s", bot->public_chan->name->content);
132 sendnoticetouser(qabot_nick, np, "Question chan: %s", bot->question_chan->name->content);
133 sendnoticetouser(qabot_nick, np, "Staff chan: %s", bot->staff_chan->name->content);
134 sendnoticetouser(qabot_nick, np, "Spam interval: %d", bot->spam_interval);
135 sendnoticetouser(qabot_nick, np, "Nick block interval: %d", bot->ask_wait);
136 sendnoticetouser(qabot_nick, np, "Queued question interval: %d", bot->queued_question_interval);
137 sendnoticetouser(qabot_nick, np, "Lines spammed: %d", bot->spammed);
138 sendnoticetouser(qabot_nick, np, "Questions asked: %d", bot->lastquestionID);
139 sendnoticetouser(qabot_nick, np, "Questions answered: %d", bot->answered);
140 sendnoticetouser(qabot_nick, np, "Blocks: %d", bot->block_count);
141 sendnoticetouser(qabot_nick, np, "Block control chars: %s", (bot->flags & QAB_CONTROLCHAR) ? "Yes" : "No");
142 sendnoticetouser(qabot_nick, np, "Block colour: %s", (bot->flags & QAB_COLOUR) ? "Yes" : "No");
143 sendnoticetouser(qabot_nick, np, "Authed users only: %s", (bot->flags & QAB_AUTHEDONLY) ? "Yes" : "No");
144 sendnoticetouser(qabot_nick, np, "Line break: %s", (bot->flags & QAB_LINEBREAK) ? "Yes" : "No");
145 sendnoticetouser(qabot_nick, np, "Question flood detection: %s", (bot->flags & QAB_FLOODDETECT) ? "Yes" : "No");
146 sendnoticetouser(qabot_nick, np, "Question flood blocking: %s", (bot->flags & QAB_FLOODSTOP) ? "Yes" : "No");
147 sendnoticetouser(qabot_nick, np, "Blocks mark as spam: %s", (bot->flags & QAB_BLOCKMARK) ? "Yes" : "No");
148 if (bot->micnumeric) {
149 nick* mnick = getnickbynumeric(bot->micnumeric);
150
151 sendnoticetouser(qabot_nick, np, "Mic: Enabled (%s)", mnick ? mnick->nick : "UNKNOWN");
152 }
153 else
154 sendnoticetouser(qabot_nick, np, "Mic: Disabled");
155 sendnoticetouser(qabot_nick, sender, "Mic timeout: %d", bot->mic_timeout);
156 sendnoticetouser(qabot_nick, np, "End of list.");
157
158 return CMD_OK;
159 }
160
161 int qabot_doaddbot(void* sender, int cargc, char** cargv) {
162 nick* np = (nick*)sender;
163
164 if (cargc < 3) {
165 sendnoticetouser(qabot_nick, np, "Syntax: addbot <nick> <user> <host> <public channel> <question channel> <staff channel>");
166 return CMD_ERROR;
167 }
168
169 if (!IsOper(np)) {
170 sendnoticetouser(qabot_nick, np, "You must be opered to use this command.");
171 return CMD_ERROR;
172 }
173
174 if (!qabot_addbot(cargv[0], cargv[1], cargv[2], cargv[3], cargv[4], cargv[5], DEFAULTBOTFLAGS, SPAMINTERVAL, ASKWAIT, QUEUEDQUESTIONINTERVAL, np))
175 return CMD_ERROR;
176
177 sendnoticetouser(qabot_nick, np, "Created %s!%s@%s (%s, %s, %s).", cargv[0], cargv[1], cargv[2], cargv[3], cargv[4], cargv[5]);
178
179 return CMD_OK;
180 }
181
182 int qabot_dodelbot(void* sender, int cargc, char** cargv) {
183 nick* np = (nick*)sender;
184 nick* botnick;
185 qab_bot* bot;
186
187 if (cargc < 1) {
188 sendnoticetouser(qabot_nick, np, "Syntax: delbot <nick>");
189 return CMD_ERROR;
190 }
191
192 if (!IsOper(np)) {
193 sendnoticetouser(qabot_nick, np, "You must be opered to use this command.");
194 return CMD_ERROR;
195 }
196
197 if (!(botnick = getnickbynick(cargv[0]))) {
198 sendnoticetouser(qabot_nick, np, "No such nickname.");
199 return CMD_ERROR;
200 }
201
202 bot = botnick->exts[qabot_nickext];
203 if (!bot) {
204 sendnoticetouser(qabot_nick, np, "%s is not a QABot.", botnick->nick);
205 return CMD_ERROR;
206 }
207
208 qabot_delbot(bot);
209 sendnoticetouser(qabot_nick, np, "Bot deleted.");
210
211 return CMD_OK;
212 }
213
214 int qabot_doadduser(void* sender, int cargc, char** cargv) {
215 nick* np = (nick*)sender;
216 nick* target = NULL;
217 char* ch;
218 flag_t flags = 0;
219 char* victim;
220
221 if (cargc < 2) {
222 sendnoticetouser(qabot_nick, np, "Syntax: adduser <nick> <flags>");
223 return CMD_ERROR;
224 }
225
226 if (!(victim = qabot_getvictim(np, cargv[0])))
227 return CMD_ERROR;
228
229 if (qabot_getuser(victim)) {
230 sendnoticetouser(qabot_nick, np, "Account %s already exists.", target->authname);
231 return CMD_ERROR;
232 }
233
234 for (ch = cargv[1]; *ch; ch++) {
235 switch (*ch) {
236 case '+':
237 break;
238
239 case 'a':
240 flags |= (QAUFLAG_ADMIN|QAUFLAG_STAFF);
241 break;
242
243 case 'd':
244 flags |= (QAUFLAG_DEVELOPER|QAUFLAG_ADMIN|QAUFLAG_STAFF);
245
246 case 's':
247 flags |= QAUFLAG_STAFF;
248 break;
249
250 default:
251 sendnoticetouser(qabot_nick, np, "Unknown flag '%c'.", *ch);
252 return CMD_ERROR;
253 }
254 }
255
256 qabot_adduser(victim, flags, time(0));
257 sendnoticetouser(qabot_nick, np, "Account created.");
258
259 return CMD_OK;
260 }
261
262 int qabot_dodeluser(void* sender, int cargc, char** cargv) {
263 nick* np = (nick*)sender;
264 qab_user* u;
265 char* victim;
266
267 if (cargc < 1) {
268 sendnoticetouser(qabot_nick, np, "Syntax: <account>");
269 return CMD_ERROR;
270 }
271
272 if (!(victim = qabot_getvictim(np, cargv[0])))
273 return CMD_ERROR;
274
275 if (!(u = qabot_getuser(victim))) {
276 sendnoticetouser(qabot_nick, np, "Account does not exist.");
277 return CMD_ERROR;
278 }
279
280 if (!ircd_strcmp(u->authname, np->authname)) {
281 sendnoticetouser(qabot_nick, np, "You cannot delete your own account.");
282 return CMD_ERROR;
283 }
284
285 qabot_squelchuser(u);
286 sendnoticetouser(qabot_nick, np, "Account deleted.");
287
288 return CMD_OK;
289 }
290
291 int qabot_dochangelev(void* sender, int cargc, char** cargv) {
292 nick* np = (nick*)sender;
293 char* victim;
294 qab_user* u;
295
296 if (cargc < 1) {
297 sendnoticetouser(qabot_nick, np, "Syntax: changelev <nick|#authname> <flags>");
298 return CMD_ERROR;
299 }
300
301 if (!(victim = qabot_getvictim(np, cargv[0])))
302 return CMD_ERROR;
303
304 if (!(u = qabot_getuser(victim))) {
305 sendnoticetouser(qabot_nick, np, "Account does not exist.");
306 return CMD_ERROR;
307 }
308
309 sendnoticetouser(qabot_nick, np, "TODO: Finish this! :)");
310
311 return CMD_OK;
312 }
313
314 int qabot_dowhois(void* sender, int cargc, char** cargv) {
315 nick* np = (nick*)sender;
316 char* victim;
317 qab_user* u;
318
319 if (cargc < 1) {
320 sendnoticetouser(qabot_nick, np, "Syntax: whois <nick|#authname>");
321 return CMD_ERROR;
322 }
323
324 if (!(victim = qabot_getvictim(np, cargv[0])))
325 return CMD_ERROR;
326
327 if (!(u = qabot_getuser(victim))) {
328 sendnoticetouser(qabot_nick, np, "Account does not exist.");
329 return CMD_ERROR;
330 }
331
332 sendnoticetouser(qabot_nick, np, "Account: %s", u->authname);
333 sendnoticetouser(qabot_nick, np, "Flags: %s", qabot_uflagtostr(u->flags));
334 sendnoticetouser(qabot_nick, np, "Created: %s", qabot_formattime(u->created));
335
336 return CMD_OK;
337 }
338
339 int qabot_dostatus(void* sender, int cargc, char** cargv) {
340 nick* np = (nick*)sender;
341 int botc = 0, blockc = 0, userc = 0, spamc = 0, qc = 0, qac = 0;
342 qab_bot* b;
343 qab_user* u;
344
345 for (b = qab_bots; b; b = b->next) {
346 botc++;
347 blockc += b->block_count;
348 spamc += b->spammed;
349 qc += b->lastquestionID;
350 qac += b->answered;
351 }
352
353 for (u = qabot_users; u; u = u->next)
354 userc++;
355
356 sendnoticetouser(qabot_nick, np, "Up since: %s", qabot_formattime(qab_startime));
357 sendnoticetouser(qabot_nick, np, "Bots: %d", botc);
358 sendnoticetouser(qabot_nick, np, "Blocks: %d", blockc);
359 sendnoticetouser(qabot_nick, np, "Users: %d", userc);
360 sendnoticetouser(qabot_nick, np, "Total lines spammed: %d", spamc);
361 sendnoticetouser(qabot_nick, np, "Total questions asked: %d", qc);
362 sendnoticetouser(qabot_nick, np, "Total questions answered: %d", qac);
363
364 return CMD_OK;
365 }