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