]> jfr.im git - irc/quakenet/newserv.git/blob - chanserv/chanservstdcmds.c
CHANSERV: Add support to count calls, and "showcommands -v" to show the
[irc/quakenet/newserv.git] / chanserv / chanservstdcmds.c
1 /*
2 * This contains Q9's "built in" commands and CTCP handlers
3 */
4
5 #include "chanserv.h"
6 #include "../core/schedule.h"
7 #include "../lib/irc_string.h"
8 #include "../dbapi/dbapi.h"
9
10 #include <string.h>
11 #include <stdio.h>
12
13 int cs_dorehash(void *source, int cargc, char **cargv) {
14 nick *sender=source;
15 Command *cmdlist[200];
16 int i,n;
17
18 /* Reload the response text first */
19 loadmessages();
20
21 /* Now the commands */
22 n=getcommandlist(cscommands, cmdlist, 200);
23
24 for(i=0;i<n;i++)
25 loadcommandsummary(cmdlist[i]);
26
27 chanservstdmessage(sender, QM_DONE);
28
29 return CMD_OK;
30 }
31
32 int cs_doquit(void *source, int cargc, char **cargv) {
33 char *reason="Leaving";
34 nick *sender=(nick *)source;
35
36 if (cargc>0) {
37 reason=cargv[0];
38 }
39
40 chanservstdmessage(sender, QM_DONE);
41
42 deregisterlocaluser(chanservnick, reason);
43 scheduleoneshot(time(NULL)+1,&chanservreguser,NULL);
44
45 return CMD_OK;
46 }
47
48 int cs_dosetquitreason(void *source, int cargc, char **cargv) {
49 nick *sender=(nick *)source;
50
51 if (cargc<0) {
52 chanservstdmessage(sender, QM_NOTENOUGHPARAMS, "setquitreason");
53 return CMD_ERROR;
54 }
55
56 if (cs_quitreason)
57 freesstring(cs_quitreason);
58
59 cs_quitreason=getsstring(cargv[0], 250);
60
61 chanservstdmessage(sender, QM_DONE);
62
63 return CMD_OK;
64 }
65
66 int cs_dorename(void *source, int cargc, char **cargv) {
67 char newnick[NICKLEN+1];
68 nick *sender=source;
69
70 if (cargc<1) {
71 chanservstdmessage(sender, QM_NOTENOUGHPARAMS, "rename");
72 return CMD_ERROR;
73 }
74
75 strncpy(newnick,cargv[0],NICKLEN);
76 newnick[NICKLEN]='\0';
77
78 renamelocaluser(chanservnick, newnick);
79 chanservstdmessage(sender, QM_DONE);
80
81 return CMD_OK;
82 }
83
84 int cs_doshowcommands(void *source, int cargc, char **cargv) {
85 nick *sender=source;
86 reguser *rup;
87 Command *cmdlist[200];
88 int i,n;
89 int lang;
90 char *message;
91 cmdsummary *summary;
92 char cmdbuf[50];
93 char *ct;
94 unsigned int dumpcount=0;
95
96 n=getcommandlist(cscommands, cmdlist, 200);
97 rup=getreguserfromnick(sender);
98
99 if (!rup)
100 lang=0;
101 else
102 lang=rup->languageid;
103
104 chanservstdmessage(sender, QM_COMMANDLIST);
105
106 if (cargc>0 && rup && UIsDev(rup) && !ircd_strcmp(cargv[0], "-v")) {
107 dumpcount=1;
108 }
109
110 for (i=0;i<n;i++) {
111 /* Generate the appropriate strings for the command name (including
112 * prefixes for privileged users) and the summary text.
113 *
114 * We do this before we're sure we will print the command to make things
115 * easier when we are doing -v */
116 summary=(cmdsummary *)cmdlist[i]->ext;
117
118 if (rup && UHasStaffPriv(rup)) {
119 if (cmdlist[i]->level & QCMD_DEV) {
120 sprintf(cmdbuf,"+d %s",cmdlist[i]->command->content);
121 } else if(cmdlist[i]->level & QCMD_ADMIN) {
122 sprintf(cmdbuf,"+a %s",cmdlist[i]->command->content);
123 } else if(cmdlist[i]->level & QCMD_OPER) {
124 sprintf(cmdbuf,"+o %s",cmdlist[i]->command->content);
125 } else if(cmdlist[i]->level & QCMD_HELPER) {
126 sprintf(cmdbuf,"+h %s",cmdlist[i]->command->content);
127 } else if(cmdlist[i]->level & QCMD_STAFF) {
128 sprintf(cmdbuf,"+q %s",cmdlist[i]->command->content);
129 } else {
130 sprintf(cmdbuf," %s",cmdlist[i]->command->content);
131 }
132 ct=cmdbuf;
133 } else {
134 ct=cmdlist[i]->command->content;
135 }
136
137 if (summary->bylang[lang]) {
138 message=summary->bylang[lang]->content;
139 } else if (summary->bylang[0]) {
140 message=summary->bylang[0]->content;
141 } else {
142 message=summary->def->content;
143 }
144
145 if (dumpcount) {
146 chanservsendmessage(sender,"%-20s %u", cmdbuf, cmdlist[i]->calls);
147 continue;
148 }
149
150 if (cargc>0 && !match2strings(cargv[0],cmdlist[i]->command->content))
151 continue;
152
153 /* Don't list aliases */
154 if (cmdlist[i]->level & QCMD_ALIAS)
155 continue;
156
157 /* Check that this user can use this command.. */
158 if ((cmdlist[i]->level & QCMD_AUTHED) && !rup)
159 continue;
160
161 if ((cmdlist[i]->level & QCMD_NOTAUTHED) && rup)
162 continue;
163
164 if ((cmdlist[i]->level & QCMD_STAFF) &&
165 (!rup || !UHasStaffPriv(rup)))
166 continue;
167
168 if ((cmdlist[i]->level & QCMD_HELPER) &&
169 (!rup || !UHasHelperPriv(rup)))
170 continue;
171
172 if ((cmdlist[i]->level & QCMD_OPER) &&
173 (!rup || !UHasOperPriv(rup) || !IsOper(sender)))
174 continue;
175
176 if ((cmdlist[i]->level & QCMD_ADMIN) &&
177 (!rup || !UHasAdminPriv(rup) || !IsOper(sender)))
178 continue;
179
180 if ((cmdlist[i]->level & QCMD_DEV) &&
181 (!rup || !UIsDev(rup) || !IsOper(sender)))
182 continue;
183
184 /* We passed all the checks, send the message */
185 chanservsendmessage(sender, "%-20s %s",ct, message);
186 }
187
188 chanservstdmessage(sender, QM_ENDOFLIST);
189
190 return CMD_OK;
191 }
192
193 int cs_sendhelp(nick *sender, char *thecmd, int oneline) {
194 Command *cmd;
195 cmdsummary *sum;
196 reguser *rup;
197
198 if (!(cmd=findcommandintree(cscommands, thecmd, 1))) {
199 chanservstdmessage(sender, QM_UNKNOWNCMD, thecmd);
200 return CMD_ERROR;
201 }
202
203 /* Disable database help for now - splidge
204 csdb_dohelp(sender, cmd); */
205
206 rup=getreguserfromnick(sender);
207
208 /* Don't showhelp for privileged users to others.. */
209 if (((cmd->level & QCMD_STAFF) && (!rup || !UHasStaffPriv(rup))) ||
210 ((cmd->level & QCMD_HELPER) && (!rup || !UHasHelperPriv(rup))) ||
211 ((cmd->level & QCMD_OPER) && (!rup || !UHasOperPriv(rup))) ||
212 ((cmd->level & QCMD_ADMIN) && (!rup || !UHasAdminPriv(rup))) ||
213 ((cmd->level & QCMD_DEV) && (!rup || !UIsDev(rup)))) {
214 chanservstdmessage(sender, QM_NOHELP, cmd->command->content);
215 return CMD_OK;
216 }
217
218 sum=cmd->ext;
219
220 if (sum->defhelp && *(sum->defhelp)) {
221 if (oneline) {
222 chanservsendmessageoneline(sender, sum->defhelp);
223 } else {
224 chanservsendmessage(sender, sum->defhelp);
225 }
226 } else {
227 if (!oneline)
228 chanservstdmessage(sender, QM_NOHELP, cmd->command->content);
229 }
230
231 return CMD_OK;
232 }
233
234
235 int cs_dohelp(void *source, int cargc, char **cargv) {
236 nick *sender=source;
237
238 if (cargc==0)
239 return cs_doshowcommands(source,cargc,cargv);
240
241 return cs_sendhelp(sender, cargv[0], 0);
242 }
243
244
245 int cs_doctcpping(void *source, int cargc, char **cargv) {
246 char *nullbuf="\001";
247
248 sendnoticetouser(chanservnick, source, "%cPING %s",
249 1, cargc?cargv[0]:nullbuf);
250
251 return CMD_OK;
252 }
253
254 int cs_doctcpversion(void *source, int cargc, char **cargv) {
255 sendnoticetouser(chanservnick, source, "\01VERSION Q9 version %s (Compiled on " __DATE__ ") (C) 2002-8 David Mansell (splidge) and others.\01", QVERSION);
256 sendnoticetouser(chanservnick, source, "\01VERSION Built on newserv. (C) 2002-8 David Mansell (splidge) and others.\01");
257
258 return CMD_OK;
259 }
260
261 int cs_doversion(void *source, int cargc, char **cargv) {
262 chanservsendmessage((nick *)source, "Q9 version %s (Compiled on " __DATE__ ") (C) 2002-8 David Mansell (splidge) and others.", QVERSION);
263 chanservsendmessage((nick *)source, "Built on newserv. (C) 2002-8 David Mansell (splidge) and others.");
264 return CMD_OK;
265 }
266
267 int cs_doctcpgender(void *source, int cargc, char **cargv) {
268 sendnoticetouser(chanservnick, source, "\1GENDER Anyone who has a bitch mode has to be female ;)\1");
269
270 return CMD_OK;
271 }
272
273 void csdb_dohelp_real(DBConn *, void *);
274
275 struct helpinfo {
276 unsigned int numeric;
277 sstring *commandname;
278 Command *cmd;
279 };
280
281 /* Help stuff */
282 void csdb_dohelp(nick *np, Command *cmd) {
283 struct helpinfo *hip;
284
285 hip=(struct helpinfo *)malloc(sizeof(struct helpinfo));
286
287 hip->numeric=np->numeric;
288 hip->commandname=getsstring(cmd->command->content, cmd->command->length);
289 hip->cmd=cmd;
290
291 q9asyncquery(csdb_dohelp_real, (void *)hip,
292 "SELECT languageID, fullinfo from chanserv.help where lower(command)=lower('%s')",cmd->command->content);
293 }
294
295 void csdb_dohelp_real(DBConn *dbconn, void *arg) {
296 struct helpinfo *hip=arg;
297 nick *np=getnickbynumeric(hip->numeric);
298 reguser *rup;
299 char *result;
300 DBResult *pgres;
301 int j,lang=0;
302
303 if(!dbconn) {
304 freesstring(hip->commandname);
305 free(hip);
306 return;
307 }
308
309 pgres=dbgetresult(dbconn);
310
311 if (!dbquerysuccessful(pgres)) {
312 Error("chanserv",ERR_ERROR,"Error loading help text.");
313 freesstring(hip->commandname);
314 free(hip);
315 return;
316 }
317
318 if (dbnumfields(pgres)!=2) {
319 Error("chanserv",ERR_ERROR,"Help text format error.");
320 dbclear(pgres);
321 freesstring(hip->commandname);
322 free(hip);
323 return;
324 }
325
326 if (!np) {
327 dbclear(pgres);
328 freesstring(hip->commandname);
329 free(hip);
330 return;
331 }
332
333 if ((rup=getreguserfromnick(np)))
334 lang=rup->languageid;
335
336 result=NULL;
337
338 while(dbfetchrow(pgres)) {
339 j=strtoul(dbgetvalue(pgres,0),NULL,10);
340 if ((j==0 && result==NULL) || (j==lang)) {
341 result=dbgetvalue(pgres,1);
342 if(strlen(result)==0)
343 result=NULL;
344 }
345 }
346
347 if (result) {
348 chanservsendmessage(np, result);
349 } else {
350 cmdsummary *sum=hip->cmd->ext;
351 if (sum->defhelp && *(sum->defhelp)) {
352 chanservsendmessage(np, sum->defhelp);
353 } else {
354 chanservstdmessage(np, QM_NOHELP, hip->commandname->content);
355 }
356 }
357
358 freesstring(hip->commandname);
359 free(hip);
360 dbclear(pgres);
361 }