]>
Commit | Line | Data |
---|---|---|
c86edd1d Q |
1 | /* |
2 | * chanservuser.c: | |
3 | * This file maintains the functions associated with the | |
4 | * user on the network relating to the channel service | |
5 | */ | |
6 | ||
7 | #include "chanserv.h" | |
8 | ||
32562540 | 9 | #include "../core/hooks.h" |
c86edd1d Q |
10 | #include "../core/schedule.h" |
11 | #include "../core/config.h" | |
12 | #include "../localuser/localuser.h" | |
13 | #include "../localuser/localuserchannel.h" | |
14 | #include "../irc/irc_config.h" | |
15 | #include "../lib/sstring.h" | |
16 | #include "../nick/nick.h" | |
17 | #include "../parser/parser.h" | |
18 | #include "../lib/splitline.h" | |
b7a95f03 | 19 | #include "../lib/irc_string.h" |
c86edd1d Q |
20 | |
21 | #include <string.h> | |
22 | #include <stdio.h> | |
23 | #include <stdarg.h> | |
24 | #include <stdlib.h> | |
25 | ||
26 | nick *chanservnick; | |
27 | CommandTree *cscommands; | |
28 | CommandTree *csctcpcommands; | |
29 | ||
30 | /* Local prototypes */ | |
31 | void chanservuserhandler(nick *target, int message, void **params); | |
32 | ||
33 | void chanservreguser(void *arg) { | |
6c4eca7b | 34 | sstring *csnick,*csuser,*cshost,*csrealname,*csaccount; |
c86edd1d Q |
35 | chanindex *cip; |
36 | regchan *rcp; | |
37 | int i; | |
38 | ||
39 | csnick=getcopyconfigitem("chanserv","nick","Q",NICKLEN); | |
40 | csuser=getcopyconfigitem("chanserv","user","TheQBot",USERLEN); | |
41 | cshost=getcopyconfigitem("chanserv","host","some.host",HOSTLEN); | |
42 | csrealname=getcopyconfigitem("chanserv","realname","ChannelService",REALLEN); | |
6c4eca7b | 43 | csaccount=getcopyconfigitem("chanserv","account",csnick&&csnick->content&&csnick->content[0]?csnick->content:"Q",ACCOUNTLEN); |
c86edd1d | 44 | |
316959c1 | 45 | Error("chanserv",ERR_INFO,"Connecting %s...",csnick->content); |
46 | ||
c86edd1d | 47 | chanservnick=registerlocaluser(csnick->content,csuser->content,cshost->content, |
6c4eca7b CP |
48 | csrealname->content,csaccount->content, |
49 | UMODE_INV|UMODE_SERVICE|UMODE_DEAF|UMODE_OPER|UMODE_ACCOUNT, | |
c86edd1d Q |
50 | &chanservuserhandler); |
51 | ||
52 | freesstring(csnick); | |
53 | freesstring(csuser); | |
54 | freesstring(cshost); | |
55 | freesstring(csrealname); | |
6c4eca7b CP |
56 | freesstring(csaccount); |
57 | ||
c86edd1d Q |
58 | /* Now join channels */ |
59 | for (i=0;i<CHANNELHASHSIZE;i++) { | |
60 | for (cip=chantable[i];cip;cip=cip->next) { | |
61 | if (cip->channel && (rcp=cip->exts[chanservext]) && !CIsSuspended(rcp)) { | |
316959c1 | 62 | /* This will do timestamp faffing even if it won't actually join */ |
63 | chanservjoinchan(cip->channel); | |
c86edd1d Q |
64 | /* Do a check at some future time.. */ |
65 | cs_schedupdate(cip, 1, 5); | |
66 | rcp->status |= (QCSTAT_OPCHECK | QCSTAT_MODECHECK | QCSTAT_BANCHECK); | |
67 | if (CIsForceTopic(rcp) || CIsTopicSave(rcp)) | |
68 | localsettopic(chanservnick, cip->channel, (rcp->topic) ? rcp->topic->content : ""); | |
69 | } | |
70 | } | |
71 | } | |
72 | ||
316959c1 | 73 | Error("chanserv",ERR_INFO,"Loaded and joined channels."); |
74 | ||
c86edd1d Q |
75 | if (chanserv_init_status == CS_INIT_NOUSER) { |
76 | /* If this is the first time, perform last init tasks */ | |
77 | chanserv_finalinit(); | |
78 | } | |
79 | } | |
80 | ||
81 | void chanservuserhandler(nick *target, int message, void **params) { | |
82 | nick *sender; | |
83 | char *msg; | |
84 | Command *cmd; | |
85 | char *cargv[30]; | |
86 | int cargc; | |
87 | reguser *rup; | |
88 | char *chp; | |
89 | ||
90 | switch(message) { | |
91 | case LU_KILLED: | |
92 | scheduleoneshot(time(NULL),&chanservreguser,NULL); | |
93 | chanservnick=NULL; | |
94 | break; | |
95 | ||
96 | case LU_PRIVMSG: | |
97 | case LU_SECUREMSG: | |
98 | sender=(nick *)params[0]; | |
99 | msg=(char *)params[1]; | |
100 | ||
101 | if (!(cargc=splitline(msg,cargv,30,0))) | |
102 | return; /* Ignore blank lines */ | |
103 | ||
104 | if (cargv[0][0]==1) { | |
105 | /* CTCP */ | |
106 | for (chp=cargv[0]+1;*chp;chp++) { | |
107 | if (*chp=='\001') { | |
108 | *chp='\0'; | |
109 | break; | |
110 | } | |
111 | } | |
112 | cmd=findcommandintree(csctcpcommands, cargv[0]+1, 1); | |
113 | if (cmd) { | |
114 | rejoinline(cargv[1],cargc-1); | |
115 | cmd->handler((void *)sender, cargc-1, &(cargv[1])); | |
116 | } | |
117 | } else { | |
118 | cmd=findcommandintree(cscommands, cargv[0], 1); | |
119 | ||
120 | if (!cmd) { | |
121 | chanservstdmessage(sender, QM_UNKNOWNCMD, cargv[0]); | |
122 | break; | |
123 | } | |
124 | ||
125 | if ((cmd->level & QCMD_SECURE) && (message != LU_SECUREMSG)) { | |
126 | chanservstdmessage(sender, QM_SECUREONLY, cargv[0], chanservnick->nick, myserver->content); | |
127 | break; | |
128 | } | |
129 | ||
130 | if ((cmd->level & QCMD_AUTHED)) { | |
131 | if (!(rup=getreguserfromnick(sender))) { | |
132 | chanservstdmessage(sender, QM_AUTHEDONLY, cargv[0]); | |
133 | break; | |
134 | } | |
135 | if (UIsSuspended(rup) || (rup->status & QUSTAT_DEAD)) { | |
136 | chanservstdmessage(sender, QM_BADAUTH, cargv[0]); | |
137 | break; | |
138 | } | |
139 | } | |
140 | ||
141 | if ((cmd->level & QCMD_NOTAUTHED) && (getreguserfromnick(sender))) { | |
142 | chanservstdmessage(sender, QM_UNAUTHEDONLY, cargv[0]); | |
143 | break; | |
144 | } | |
145 | ||
7be5f783 CP |
146 | if ((cmd->level & QCMD_STAFF) && |
147 | (!(rup=getreguserfromnick(sender)) || !UHasStaffPriv(rup))) { | |
148 | chanservstdmessage(sender, QM_NOACCESS, cargv[0]); | |
149 | break; | |
150 | } | |
151 | ||
c86edd1d Q |
152 | if ((cmd->level & QCMD_HELPER) && |
153 | (!(rup=getreguserfromnick(sender)) || !UHasHelperPriv(rup))) { | |
154 | chanservstdmessage(sender, QM_NOACCESS, cargv[0]); | |
155 | break; | |
156 | } | |
157 | ||
158 | if ((cmd->level & QCMD_OPER) && | |
159 | (!IsOper(sender) || !(rup=getreguserfromnick(sender)) || !UHasOperPriv(rup))) { | |
160 | chanservstdmessage(sender, QM_NOACCESS, cargv[0]); | |
161 | break; | |
162 | } | |
163 | ||
164 | if ((cmd->level & QCMD_ADMIN) && | |
165 | (!IsOper(sender) || !(rup=getreguserfromnick(sender)) || !UHasAdminPriv(rup))) { | |
166 | chanservstdmessage(sender, QM_NOACCESS, cargv[0]); | |
167 | break; | |
168 | } | |
169 | ||
170 | if ((cmd->level & QCMD_DEV) && | |
171 | (!IsOper(sender) || !(rup=getreguserfromnick(sender)) || !UIsDev(rup))) { | |
172 | chanservstdmessage(sender, QM_NOACCESS, cargv[0]); | |
173 | break; | |
174 | } | |
175 | ||
176 | if (cmd->maxparams < (cargc-1)) { | |
177 | rejoinline(cargv[cmd->maxparams],cargc-(cmd->maxparams)); | |
178 | cargc=(cmd->maxparams)+1; | |
179 | } | |
180 | ||
181 | cmd->handler((void *)sender, cargc-1, &(cargv[1])); | |
182 | } | |
183 | break; | |
184 | ||
185 | default: | |
186 | break; | |
187 | } | |
188 | } | |
189 | ||
190 | void chanservcommandinit() { | |
191 | cscommands=newcommandtree(); | |
192 | csctcpcommands=newcommandtree(); | |
193 | } | |
194 | ||
195 | void chanservcommandclose() { | |
196 | destroycommandtree(cscommands); | |
197 | destroycommandtree(csctcpcommands); | |
198 | } | |
199 | ||
1f0d7c8b | 200 | void chanservaddcommand(char *command, int flags, int maxparams, CommandHandler handler, char *description, const char *help) { |
c86edd1d Q |
201 | Command *newcmd; |
202 | cmdsummary *summary; | |
203 | ||
204 | newcmd=addcommandtotree(cscommands, command, flags, maxparams, handler); | |
205 | /* Allocate a summary record to put the description in */ | |
206 | summary=(cmdsummary *)malloc(sizeof(cmdsummary)); | |
207 | memset((void *)summary,0,sizeof(cmdsummary)); | |
208 | ||
209 | summary->def=getsstring(description, 250); | |
9c27e757 | 210 | summary->defhelp=(char *)help; /* Assume that help is a constant */ |
1f0d7c8b | 211 | |
c86edd1d Q |
212 | newcmd->ext=(void *)summary; |
213 | loadcommandsummary(newcmd); | |
214 | } | |
215 | ||
216 | void chanservaddctcpcommand(char *command, CommandHandler handler) { | |
217 | addcommandtotree(csctcpcommands, command, 0, 1, handler); | |
218 | } | |
219 | ||
220 | void chanservremovectcpcommand(char *command, CommandHandler handler) { | |
221 | deletecommandfromtree(csctcpcommands, command, handler); | |
222 | } | |
223 | ||
224 | void chanservremovecommand(char *command, CommandHandler handler) { | |
225 | Command *cmd; | |
226 | cmdsummary *summary; | |
227 | int i; | |
228 | ||
229 | if (!(cmd=findcommandintree(cscommands, command, 1))) { | |
230 | Error("chanserv",ERR_WARNING,"Tried to unregister unknown command %s",command); | |
231 | return; | |
232 | } | |
233 | ||
234 | summary=(cmdsummary *)cmd->ext; | |
235 | freesstring(summary->def); | |
236 | for (i=0;i<MAXLANG;i++) { | |
237 | if (summary->bylang[i]) | |
238 | freesstring(summary->bylang[i]); | |
239 | } | |
240 | ||
241 | free(summary); | |
242 | ||
243 | deletecommandfromtree(cscommands, command, handler); | |
244 | } | |
245 | ||
246 | void chanservjoinchan(channel *cp) { | |
247 | regchan *rcp; | |
2df6316a | 248 | unsigned int i; |
249 | nick *np; | |
250 | reguser *rup; | |
251 | regchanuser *rcup; | |
09cb349b | 252 | flag_t themodes = 0; |
af87643b | 253 | |
a54e438d | 254 | /* Skip unregistered channels */ |
255 | if (!(rcp=cp->index->exts[chanservext])) | |
256 | return; | |
257 | ||
0a53499c | 258 | /* Check for a new timestamp. But don't do anything stupid if the incoming timestamp is 0 */ |
259 | if (cp->timestamp && ((!rcp->ltimestamp) || (cp->timestamp < rcp->ltimestamp))) { | |
a54e438d | 260 | rcp->ltimestamp=cp->timestamp; |
261 | csdb_updatechanneltimestamp(rcp); | |
262 | } | |
c86edd1d | 263 | |
a54e438d | 264 | /* We won't be doing any joining or parting if we're not online */ |
265 | if (!chanservnick) | |
c86edd1d Q |
266 | return; |
267 | ||
268 | if ((CIsSuspended(rcp) || !CIsJoined(rcp)) && getnumerichandlefromchanhash(cp->users, chanservnick->numeric)) { | |
334b567e P |
269 | if(rcp->suspendreason) { |
270 | localpartchannel(chanservnick, cp, rcp->suspendreason->content); | |
271 | } else { | |
272 | localpartchannel(chanservnick, cp, NULL); | |
273 | } | |
c86edd1d | 274 | } |
2df6316a | 275 | |
276 | /* Right, we are definately going to either join the channel or at least | |
277 | * burst some modes onto it. | |
278 | * | |
a54e438d | 279 | * We will try and burst our view of the world; if the timestamps are |
280 | * actually equal this will be mostly ignored and we will have to fix it | |
af87643b | 281 | * up later. For modes we use the forced modes, plus the default channel |
282 | * modes (unless any of those are explicitly denied) */ | |
af87643b | 283 | |
43583524 | 284 | /* By default, we set the forcemodes and the default modes, but never denymodes.. |
285 | * OK, actually we should only set the default modes if our timestamp is older.*/ | |
286 | if (cp->timestamp > rcp->ltimestamp) | |
287 | themodes |= CHANMODE_DEFAULT; | |
288 | else | |
289 | themodes=0; | |
290 | ||
291 | themodes = (themodes | rcp->forcemodes) & ~rcp->denymodes; | |
2df6316a | 292 | |
293 | /* Now, if someone has just created a channel and we are going to set +i | |
294 | * or +k on it, this will kick them off. This could be construed as a | |
295 | * bit rude if it's their channel, so if there is only one person on the | |
296 | * channel and they have a right to be there, burst with default modes | |
297 | * only to avoid them being netrider kicked. | |
298 | */ | |
299 | if (cp->users->totalusers==1) { | |
300 | for (i=0;i<cp->users->hashsize;i++) { | |
301 | if (cp->users->content[i] != nouser) { | |
302 | if ((np=getnickbynumeric(cp->users->content[i]&CU_NUMERICMASK)) && | |
303 | (rup=getreguserfromnick(np)) && | |
304 | (rcup=findreguseronchannel(rcp,rup)) && | |
305 | CUKnown(rcup)) { | |
306 | /* OK, there was one user, and they are known on this channel. | |
307 | * Don't burst with +i or +k */ | |
308 | themodes &= ~(CHANMODE_INVITEONLY | CHANMODE_KEY); | |
af87643b | 309 | } |
310 | } | |
311 | } | |
2df6316a | 312 | } |
313 | ||
314 | /* We should be on the channel - join with our nick */ | |
315 | if (!CIsSuspended(rcp) && CIsJoined(rcp) && !getnumerichandlefromchanhash(cp->users, chanservnick->numeric)) { | |
af87643b | 316 | /* If we pass the key parameter here but are not setting +k (see above) |
317 | * then localburstontochannel() will ignore the key */ | |
318 | localburstontochannel(cp, chanservnick, rcp->ltimestamp, themodes, | |
319 | rcp->limit, (rcp->key)?rcp->key->content:NULL); | |
c86edd1d | 320 | } |
2df6316a | 321 | |
322 | /* We're not joining the channel - send the burst with no nick */ | |
b263aa79 | 323 | if (!CIsSuspended(rcp) && !CIsJoined(rcp) && (cp->timestamp > rcp->ltimestamp)) { |
2df6316a | 324 | localburstontochannel(cp, NULL, rcp->ltimestamp, themodes, |
325 | rcp->limit, (rcp->key)?rcp->key->content:NULL); | |
b263aa79 | 326 | } |
c86edd1d Q |
327 | } |
328 | ||
329 | void chanservstdmessage(nick *np, int messageid, ... ) { | |
330 | char buf[5010]; | |
331 | char buf2[512]; | |
332 | int notice; | |
333 | reguser *rup; | |
334 | int language; | |
dd258305 | 335 | va_list va, va2; |
b7ed259b | 336 | char *message, *messageargs; |
c86edd1d Q |
337 | char *bp2,*bp; |
338 | int len; | |
339 | ||
340 | if (getreguserfromnick(np) == NULL) { | |
341 | notice=1; | |
342 | language=0; | |
343 | } else { | |
344 | rup=getreguserfromnick(np); | |
345 | if(UIsNotice(rup)) { | |
346 | notice=1; | |
347 | } else { | |
348 | notice=0; | |
349 | } | |
350 | language=rup->languageid; | |
351 | } | |
352 | ||
353 | if (csmessages[language][messageid]) { | |
354 | message=csmessages[language][messageid]->content; | |
355 | } else if (csmessages[0][messageid]) { | |
356 | message=csmessages[0][messageid]->content; | |
357 | } else { | |
b7ed259b | 358 | message=defaultmessages[messageid*2]; |
c86edd1d Q |
359 | } |
360 | ||
b7ed259b CP |
361 | messageargs=defaultmessages[messageid*2+1]; |
362 | ||
c86edd1d | 363 | va_start(va,messageid); |
dd258305 | 364 | va_copy(va2, va); |
b7ed259b | 365 | q9vsnprintf(buf,5000,message,messageargs,va); |
c86edd1d Q |
366 | va_end(va); |
367 | ||
368 | len=0; | |
369 | bp2=buf2; | |
370 | for (bp=buf; ;bp++) { | |
371 | /* We send something if we hit a \n, fill the buffer or run out of source */ | |
372 | if (*bp=='\n' || len>490 || !*bp) { | |
373 | if (*bp && *bp!='\n') { | |
374 | bp--; | |
375 | } | |
376 | ||
377 | *bp2='\0'; | |
378 | ||
379 | if (chanservnick && *buf2) { | |
380 | if (notice) { | |
381 | sendnoticetouser(chanservnick,np,"%s",buf2); | |
382 | } else { | |
383 | sendmessagetouser(chanservnick,np,"%s",buf2); | |
384 | } | |
385 | } | |
386 | ||
387 | /* If we ran out of buffer, get out here */ | |
388 | if (!*bp) | |
389 | break; | |
390 | ||
391 | bp2=buf2; | |
392 | len=0; | |
393 | } else { | |
394 | len++; | |
395 | *bp2++=*bp; | |
396 | } | |
397 | } | |
dd258305 | 398 | |
399 | /* Special case: If it's a "not enough parameters" message, show the first line of help */ | |
400 | if (messageid==QM_NOTENOUGHPARAMS) { | |
401 | char *command=va_arg(va2, char *); | |
402 | cs_sendhelp(np, command, 1); | |
403 | chanservstdmessage(np, QM_TYPEHELPFORHELP, command); | |
404 | } | |
405 | ||
406 | va_end(va2); | |
c86edd1d Q |
407 | } |
408 | ||
dd258305 | 409 | void chanservsendmessage_real(nick *np, int oneline, char *message, ... ) { |
c86edd1d Q |
410 | char buf[5010]; /* Very large buffer.. */ |
411 | char buf2[512], *bp, *bp2; | |
412 | int notice; | |
413 | int len; | |
414 | reguser *rup; | |
415 | ||
416 | va_list va; | |
417 | ||
418 | va_start(va,message); | |
419 | vsnprintf(buf,5000,message,va); | |
420 | va_end(va); | |
421 | ||
422 | if (getreguserfromnick(np) == NULL) { | |
423 | notice=1; | |
424 | } else { | |
425 | rup=getreguserfromnick(np); | |
426 | if(UIsNotice(rup)) { | |
427 | notice=1; | |
428 | } else { | |
429 | notice=0; | |
430 | } | |
431 | } | |
432 | ||
433 | len=0; | |
434 | bp2=buf2; | |
435 | for (bp=buf; ;bp++) { | |
436 | /* We send something if we hit a \n, fill the buffer or run out of source */ | |
437 | if (*bp=='\n' || len>490 || !*bp) { | |
438 | if (*bp && *bp!='\n') { | |
439 | bp--; | |
440 | } | |
441 | ||
442 | *bp2='\0'; | |
443 | ||
444 | if (chanservnick && *buf2) { | |
445 | if (notice) { | |
446 | sendnoticetouser(chanservnick,np,"%s",buf2); | |
447 | } else { | |
448 | sendmessagetouser(chanservnick,np,"%s",buf2); | |
449 | } | |
450 | } | |
451 | ||
452 | /* If we ran out of buffer, get out here */ | |
dd258305 | 453 | if (!*bp || (*bp=='\n' && oneline)) |
c86edd1d Q |
454 | break; |
455 | ||
456 | bp2=buf2; | |
457 | len=0; | |
458 | } else { | |
459 | len++; | |
460 | *bp2++=*bp; | |
461 | } | |
462 | } | |
463 | } | |
464 | ||
465 | void chanservwallmessage(char *message, ... ) { | |
466 | char buf[5010]; /* Very large buffer.. */ | |
467 | va_list va; | |
468 | nick *np; | |
469 | unsigned int i=0; | |
470 | ||
471 | va_start(va,message); | |
472 | vsnprintf(buf,5000,message,va); | |
473 | va_end(va); | |
474 | ||
475 | /* Scan for users */ | |
476 | for (i=0;i<NICKHASHSIZE;i++) | |
477 | for (np=nicktable[i];np;np=np->next) | |
478 | if (IsOper(np)) | |
479 | chanservsendmessage(np, "%s", buf); | |
480 | } | |
481 | ||
482 | void chanservkillstdmessage(nick *target, int messageid, ... ) { | |
483 | char buf[512]; | |
484 | int language; | |
b7ed259b | 485 | char *message, *messageargs; |
c86edd1d Q |
486 | va_list va; |
487 | reguser *rup; | |
488 | ||
489 | if (!(rup=getreguserfromnick(target))) | |
490 | language=0; | |
491 | else | |
492 | language=rup->languageid; | |
493 | ||
494 | if (csmessages[language][messageid]) | |
495 | message=csmessages[language][messageid]->content; | |
496 | else if (csmessages[0][messageid]) | |
497 | message=csmessages[0][messageid]->content; | |
498 | else | |
b7ed259b | 499 | message=defaultmessages[messageid*2]; |
c86edd1d | 500 | |
b7ed259b | 501 | messageargs=defaultmessages[messageid*2+1]; |
c86edd1d | 502 | va_start(va, messageid); |
b7ed259b | 503 | q9vsnprintf(buf, 511, message, messageargs, va); |
c86edd1d Q |
504 | va_end(va); |
505 | killuser(chanservnick, target, buf); | |
506 | } | |
507 | ||
c86edd1d | 508 | int checkpassword(reguser *rup, const char *pass) { |
5cdb1f14 | 509 | if (!(*rup->password)) |
510 | return 0; | |
511 | ||
c86edd1d Q |
512 | if (!strncmp(rup->password, pass, PASSLEN)) |
513 | return 1; | |
514 | return 0; | |
515 | } | |
516 | ||
b7a95f03 CP |
517 | int checkresponse(reguser *rup, const unsigned char *entropy, const char *response, CRAlgorithm algorithm) { |
518 | char usernamel[NICKLEN+1], *dp, *up; | |
519 | ||
5cdb1f14 | 520 | if (!(*rup->password)) |
521 | return 0; | |
522 | ||
b7a95f03 CP |
523 | for(up=rup->username,dp=usernamel;*up;) |
524 | *dp++ = ToLower(*up++); | |
525 | *dp = '\0'; | |
526 | ||
527 | return algorithm(usernamel, rup->password, cs_calcchallenge(entropy), response); | |
528 | } | |
529 | ||
721cc8ce CP |
530 | int checkhashpass(reguser *rup, const char *junk, const char *hash) { |
531 | char usernamel[NICKLEN+1], *dp, *up; | |
532 | ||
533 | for(up=rup->username,dp=usernamel;*up;) | |
534 | *dp++ = ToLower(*up++); | |
535 | *dp = '\0'; | |
536 | ||
537 | return cs_checkhashpass(usernamel, rup->password, junk, hash); | |
538 | } | |
539 | ||
c86edd1d Q |
540 | int setpassword(reguser *rup, const char *pass) { |
541 | strncpy(rup->password, pass, PASSLEN); | |
542 | rup->password[PASSLEN]='\0'; | |
543 | return 1; | |
544 | } | |
545 | ||
546 | void cs_checknick(nick *np) { | |
547 | activeuser* aup; | |
548 | reguser *rup; | |
c86edd1d Q |
549 | char userhost[USERLEN+HOSTLEN+3]; |
550 | ||
551 | if (!(aup=getactiveuserfromnick(np))) { | |
552 | aup=getactiveuser(); | |
553 | np->exts[chanservnext]=aup; | |
554 | } | |
555 | ||
556 | assert(getactiveuserfromnick(np)); | |
557 | ||
23b85e10 | 558 | if (IsAccount(np) && np->auth) { |
6e3ceea8 | 559 | if (np->auth->exts[chanservaext]) { |
4b1a43ea | 560 | rup=getreguserfromnick(np); |
c0587316 | 561 | |
4b1a43ea | 562 | /* safe? */ |
c0587316 CP |
563 | if(rup) { |
564 | if (UIsDelayedGline(rup)) { | |
565 | /* delayed-gline - schedule the user's squelching */ | |
566 | deleteschedule(NULL, &chanservdgline, (void*)rup); /* icky, but necessary unless we stick more stuff in reguser structure */ | |
567 | scheduleoneshot(time(NULL)+rand()%900, &chanservdgline, (void*)rup); | |
568 | } else if (UIsGline(rup)) { | |
569 | /* instant-gline - lets be lazy and set a schedule expiring now :) */ | |
570 | deleteschedule(NULL, &chanservdgline, (void*)rup); /* icky, but necessary unless we stick more stuff in reguser structure */ | |
571 | scheduleoneshot(time(NULL), &chanservdgline, (void*)rup); | |
572 | } else if(UHasSuspension(rup)) { | |
573 | chanservkillstdmessage(np, QM_SUSPENDKILL); | |
574 | return; | |
575 | } | |
4b1a43ea | 576 | } |
c86edd1d Q |
577 | cs_doallautomodes(np); |
578 | } else { | |
c86edd1d Q |
579 | /* Auto create user.. */ |
580 | rup=getreguser(); | |
581 | rup->status=0; | |
23b85e10 | 582 | rup->ID=np->auth->userid; |
583 | if (rup->ID > lastuserID) | |
584 | lastuserID=rup->ID; | |
c86edd1d Q |
585 | strncpy(rup->username,np->authname,NICKLEN); rup->username[NICKLEN]='\0'; |
586 | rup->created=time(NULL); | |
7d27d0cd | 587 | rup->lastauth=0; |
c86edd1d | 588 | rup->lastemailchange=0; |
86d2bc73 | 589 | rup->lastpasschange=0; |
c86edd1d Q |
590 | rup->flags=QUFLAG_NOTICE; |
591 | rup->languageid=0; | |
592 | rup->suspendby=0; | |
fa350b40 | 593 | rup->suspendexp=0; |
88d191e3 | 594 | rup->suspendtime=0; |
e086bfee | 595 | rup->lockuntil=0; |
c86edd1d | 596 | rup->password[0]='\0'; |
c86edd1d | 597 | rup->email=NULL; |
9708f78f | 598 | rup->lastemail=NULL; |
e3805f60 P |
599 | rup->localpart=NULL; |
600 | rup->domain=NULL; | |
c86edd1d Q |
601 | rup->info=NULL; |
602 | sprintf(userhost,"%s@%s",np->ident,np->host->name->content); | |
603 | rup->lastuserhost=getsstring(userhost,USERLEN+HOSTLEN+1); | |
604 | rup->suspendreason=NULL; | |
605 | rup->comment=NULL; | |
606 | rup->knownon=NULL; | |
607 | rup->checkshd=NULL; | |
608 | rup->stealcount=0; | |
609 | rup->fakeuser=NULL; | |
c86edd1d Q |
610 | addregusertohash(rup); |
611 | ||
612 | csdb_createuser(rup); | |
c86edd1d | 613 | } |
c86edd1d Q |
614 | } |
615 | ||
616 | cs_checknickbans(np); | |
617 | } | |
618 | ||
619 | void cs_checkchanmodes(channel *cp) { | |
620 | modechanges changes; | |
621 | ||
622 | localsetmodeinit (&changes, cp, chanservnick); | |
623 | cs_docheckchanmodes(cp, &changes); | |
624 | localsetmodeflush (&changes, 1); | |
625 | } | |
626 | ||
627 | void cs_docheckchanmodes(channel *cp, modechanges *changes) { | |
628 | regchan *rcp; | |
629 | ||
630 | if (!cp) | |
631 | return; | |
632 | ||
633 | if ((rcp=cp->index->exts[chanservext])==NULL || CIsSuspended(rcp)) | |
634 | return; | |
635 | ||
636 | /* Take care of the simple modes */ | |
637 | localdosetmode_simple(changes, rcp->forcemodes & ~(cp->flags), rcp->denymodes & cp->flags); | |
638 | ||
639 | /* Check for forced key. Note that we ALWAYS call localdosetmode_key | |
640 | * in case the wrong key is set atm */ | |
641 | if (rcp->forcemodes & CHANMODE_KEY) { | |
642 | if (!rcp->key) { | |
643 | Error("chanserv",ERR_WARNING,"Key forced but no key specified for %s: cleared forcemode.",rcp->index->name->content); | |
644 | rcp->forcemodes &= ~CHANMODE_KEY; | |
a54e438d | 645 | csdb_updatechannel(rcp); |
c86edd1d Q |
646 | } else { |
647 | localdosetmode_key(changes, rcp->key->content, MCB_ADD); | |
648 | } | |
649 | } | |
650 | ||
651 | /* Check for denied key */ | |
652 | if ((rcp->denymodes & CHANMODE_KEY) && IsKey(cp)) { | |
653 | localdosetmode_key(changes, NULL, MCB_DEL); | |
654 | } | |
655 | ||
656 | /* Check for forced limit. Always call in case of wrong limit */ | |
657 | if (rcp->forcemodes & CHANMODE_LIMIT) { | |
658 | localdosetmode_limit(changes, rcp->limit, MCB_ADD); | |
659 | } | |
660 | } | |
661 | ||
5cde8af0 | 662 | /* Slightly misnamed function that checks each USER on the channel against channel |
663 | * settings. Possible actions are opping/deopping, voicing/devoicing, and banning | |
664 | * for chanflag +k or chanlev flag +b */ | |
c86edd1d Q |
665 | void cs_docheckopvoice(channel *cp, modechanges *changes) { |
666 | regchan *rcp; | |
667 | reguser *rup; | |
668 | regchanuser *rcup; | |
669 | nick *np; | |
670 | int i; | |
671 | ||
672 | if (!cp) | |
673 | return; | |
674 | ||
675 | if ((rcp=cp->index->exts[chanservext])==NULL || CIsSuspended(rcp)) | |
676 | return; | |
677 | ||
678 | for (i=0;i<cp->users->hashsize;i++) { | |
679 | if (cp->users->content[i]==nouser) | |
680 | continue; | |
681 | ||
682 | if ((np=getnickbynumeric(cp->users->content[i]))==NULL) { | |
16739dbe | 683 | Error("chanserv",ERR_ERROR,"Found non-existent numeric %lu on channel %s",cp->users->content[i], |
c86edd1d Q |
684 | cp->index->name->content); |
685 | continue; | |
686 | } | |
687 | ||
688 | if ((rup=getreguserfromnick(np))) | |
689 | rcup=findreguseronchannel(rcp,rup); | |
690 | else | |
691 | rcup=NULL; | |
692 | ||
5cdb1f14 | 693 | /* Various things that might ban the user - don't apply these to +o, +k or +X users */ |
694 | if (!IsService(np) && !IsXOper(np) && !IsOper(np)) { | |
695 | if (rcup && CUIsBanned(rcup)) { | |
696 | cs_banuser(changes, cp->index, np, NULL); | |
697 | continue; | |
698 | } | |
699 | ||
700 | /* chanflag +k checks; kick them if they "obviously" can't rejoin without a ban */ | |
9c3afcf2 | 701 | if (CIsKnownOnly(rcp) && !(rcup && CUKnown(rcup))) { |
5cdb1f14 | 702 | if (IsInviteOnly(cp) || (IsRegOnly(cp) && !IsAccount(np))) { |
703 | localkickuser(chanservnick,cp,np,"Authorised users only."); | |
704 | } else { | |
705 | cs_banuser(NULL, cp->index, np, "Authorised users only."); | |
706 | } | |
707 | continue; | |
5cde8af0 | 708 | } |
c86edd1d | 709 | } |
5cde8af0 | 710 | |
c86edd1d Q |
711 | if ((cp->users->content[i] & CUMODE_OP) && !IsService(np)) { |
712 | if ((CIsBitch(rcp) && (!rcup || !CUHasOpPriv(rcup))) || | |
713 | (rcup && CUIsDeny(rcup))) | |
714 | localdosetmode_nick(changes, np, MC_DEOP); | |
715 | } else { | |
1482fb78 | 716 | if (rcup && (CUIsProtect(rcup) || CIsProtect(rcp)) && CUIsOp(rcup) && !CUIsDeny(rcup)) { |
c86edd1d | 717 | localdosetmode_nick(changes, np, MC_OP); |
1482fb78 | 718 | cs_logchanop(rcp, np->nick, rcup->user); |
719 | } | |
c86edd1d Q |
720 | } |
721 | ||
722 | if (cp->users->content[i] & CUMODE_VOICE) { | |
723 | if (rcup && CUIsQuiet(rcup)) | |
724 | localdosetmode_nick(changes, np, MC_DEVOICE); | |
725 | } else { | |
726 | if (rcup && (CUIsProtect(rcup) || CIsProtect(rcp)) && CUIsVoice(rcup) && !CUIsQuiet(rcup) && !(cp->users->content[i] & CUMODE_OP)) | |
727 | localdosetmode_nick(changes, np, MC_VOICE); | |
728 | } | |
729 | } | |
730 | } | |
731 | ||
732 | void cs_doallautomodes(nick *np) { | |
733 | reguser *rup; | |
734 | regchanuser *rcup; | |
735 | unsigned long *lp; | |
736 | modechanges changes; | |
737 | ||
738 | if (!(rup=getreguserfromnick(np))) | |
739 | return; | |
740 | ||
741 | for (rcup=rup->knownon;rcup;rcup=rcup->nextbyuser) { | |
c30e08df | 742 | /* Skip suspended channels */ |
743 | if (CIsSuspended(rcup->chan)) | |
744 | continue; | |
745 | ||
c86edd1d Q |
746 | if (rcup->chan->index->channel) { |
747 | /* Channel exists */ | |
748 | if ((lp=getnumerichandlefromchanhash(rcup->chan->index->channel->users, np->numeric))) { | |
db804373 P |
749 | /* User is on channel.. */ |
750 | ||
82d904c0 | 751 | if (CUKnown(rcup) && rcup->chan->index->channel->users->totalusers >= 3) { |
752 | /* This meets the channel use criteria, update. */ | |
753 | rcup->chan->lastactive=time(NULL); | |
754 | ||
755 | /* Don't spam the DB though for channels with lots of joins */ | |
756 | if (rcup->chan->lastcountersync < (time(NULL) - COUNTERSYNCINTERVAL)) { | |
757 | csdb_updatechannelcounters(rcup->chan); | |
758 | rcup->chan->lastcountersync=time(NULL); | |
759 | } | |
760 | } | |
761 | ||
db804373 P |
762 | /* Update last use time */ |
763 | rcup->usetime=getnettime(); | |
764 | ||
c86edd1d Q |
765 | localsetmodeinit(&changes, rcup->chan->index->channel, chanservnick); |
766 | if (*lp & CUMODE_OP) { | |
767 | if (!IsService(np) && (CUIsDeny(rcup) || (CIsBitch(rcup->chan) && !CUHasOpPriv(rcup)))) | |
768 | localdosetmode_nick(&changes, np, MC_DEOP); | |
769 | } else { | |
770 | if (!CUIsDeny(rcup) && CUIsOp(rcup) && | |
1482fb78 | 771 | (CUIsProtect(rcup) || CIsProtect(rcup->chan) || CUIsAutoOp(rcup))) { |
c86edd1d | 772 | localdosetmode_nick(&changes, np, MC_OP); |
1482fb78 | 773 | cs_logchanop(rcup->chan, np->nick, rup); |
774 | } | |
c86edd1d Q |
775 | } |
776 | ||
777 | if (*lp & CUMODE_VOICE) { | |
778 | if (CUIsQuiet(rcup)) | |
779 | localdosetmode_nick(&changes, np, MC_DEVOICE); | |
780 | } else { | |
781 | if (!CUIsQuiet(rcup) && CUIsVoice(rcup) && !(*lp & CUMODE_OP) && | |
782 | (CUIsProtect(rcup) || CIsProtect(rcup->chan) || CUIsAutoVoice(rcup))) | |
783 | localdosetmode_nick(&changes, np, MC_VOICE); | |
784 | } | |
785 | localsetmodeflush(&changes, 1); | |
786 | } else { | |
787 | /* Channel exists but user is not joined: invite if they are +j-b */ | |
788 | if (CUIsAutoInvite(rcup) && CUKnown(rcup) && !CUIsBanned(rcup)) { | |
789 | localinvite(chanservnick, rcup->chan->index->channel, np); | |
790 | } | |
791 | } | |
792 | } | |
793 | } | |
794 | } | |
795 | ||
796 | void cs_checknickbans(nick *np) { | |
797 | channel **ca; | |
798 | regchan *rcp; | |
799 | int i,j; | |
800 | ||
5cdb1f14 | 801 | if (IsService(np) || IsOper(np) || IsXOper(np)) |
c86edd1d Q |
802 | return; |
803 | ||
804 | /* Avoid races: memcpy the channel array */ | |
805 | i=np->channels->cursi; | |
806 | ca=(channel **)malloc(i*sizeof(channel *)); | |
807 | memcpy(ca, np->channels->content,i*sizeof(channel *)); | |
808 | ||
809 | for (j=0;j<i;j++) { | |
810 | if ((rcp=ca[j]->index->exts[chanservext]) && !CIsSuspended(rcp) && | |
5cde8af0 | 811 | CIsEnforce(rcp) && nickbanned_visible(np, ca[j])) |
c86edd1d Q |
812 | localkickuser(chanservnick, ca[j], np, "Banned."); |
813 | } | |
814 | ||
815 | free(ca); | |
816 | } | |
817 | ||
818 | void cs_checkbans(channel *cp) { | |
819 | regchan *rcp; | |
820 | int i; | |
821 | nick *np; | |
822 | chanban *cbp; | |
823 | regban *rbp; | |
824 | time_t now; | |
825 | modechanges changes; | |
826 | ||
827 | if (!cp) | |
828 | return; | |
829 | ||
830 | if ((rcp=cp->index->exts[chanservext])==NULL || CIsSuspended(rcp)) | |
831 | return; | |
832 | ||
833 | now=time(NULL); | |
834 | ||
835 | localsetmodeinit(&changes, cp, chanservnick); | |
836 | ||
837 | for (i=0;i<cp->users->hashsize;i++) { | |
838 | if (cp->users->content[i]==nouser) | |
839 | continue; | |
840 | ||
841 | if ((np=getnickbynumeric(cp->users->content[i]))==NULL) { | |
16739dbe | 842 | Error("chanserv",ERR_ERROR,"Found numeric %lu on channel %s who doesn't exist.", |
c86edd1d Q |
843 | cp->users->content[i], cp->index->name->content); |
844 | continue; | |
845 | } | |
846 | ||
5cdb1f14 | 847 | if (IsService(np) || IsOper(np) || IsXOper(np)) |
c86edd1d Q |
848 | continue; |
849 | ||
850 | for (rbp=rcp->bans;rbp;rbp=rbp->next) { | |
851 | if (((!rbp->expiry) || (rbp->expiry <= now)) && | |
10771120 | 852 | nickmatchban_visible(np, rbp->cbp)) { |
853 | if (!nickbanned_visible(np, cp)) { | |
c86edd1d Q |
854 | localdosetmode_ban(&changes, bantostring(rbp->cbp), MCB_ADD); |
855 | } | |
856 | localkickuser(chanservnick,cp,np,rbp->reason?rbp->reason->content:"Banned."); | |
857 | break; | |
858 | } | |
859 | } | |
860 | ||
861 | if (rbp) /* If we broke out of above loop (due to kick) rbp will be set.. */ | |
862 | continue; | |
863 | ||
864 | if (CIsEnforce(rcp)) { | |
865 | for (cbp=cp->bans;cbp;cbp=cbp->next) { | |
10771120 | 866 | if ((cbp->timeset>=rcp->lastbancheck) && nickmatchban_visible(np, cbp)) |
c86edd1d Q |
867 | localkickuser(chanservnick,cp,np,"Banned."); |
868 | } | |
869 | rcp->lastbancheck=time(NULL); | |
870 | } | |
871 | } | |
872 | ||
873 | localsetmodeflush(&changes,1); | |
874 | } | |
875 | ||
876 | /* | |
877 | * cs_schedupdate: | |
878 | * This function schedules an update check on a channel | |
879 | */ | |
880 | void cs_schedupdate(chanindex *cip, int mintime, int maxtime) { | |
881 | int delay=mintime+(rand()%(maxtime-mintime)); | |
882 | regchan *rcp; | |
883 | ||
884 | if (!(rcp=cip->exts[chanservext]) || CIsSuspended(rcp)) | |
885 | return; | |
886 | ||
887 | if (rcp->checksched) { | |
888 | deleteschedule(rcp->checksched, cs_timerfunc, cip); | |
889 | } | |
890 | ||
891 | rcp->checksched=scheduleoneshot(time(NULL)+delay, cs_timerfunc, cip); | |
892 | } | |
893 | ||
894 | /* | |
895 | * cs_timerfunc: | |
896 | * This function does several things: | |
897 | * * Updates auto-limit and/or removes bans as necessary | |
898 | * * Schedules the next timed call | |
899 | * * Does any pending op/ban/mode checks | |
900 | * It is safe to use either as a target for a schedule() call, or to call | |
901 | * directly when parameters change (e.g. autolimit settings or ban expire | |
902 | * timers). | |
903 | * To force a limit update, set rcp->limit to 0.. | |
904 | */ | |
905 | ||
906 | void cs_timerfunc(void *arg) { | |
907 | chanindex *cip=arg; | |
908 | channel *cp=cip->channel; | |
909 | regchan *rcp; | |
910 | chanban *cbp, *ncbp; | |
911 | regban **rbh, *rbp; | |
912 | time_t nextsched=0; | |
913 | time_t now=time(NULL); | |
914 | modechanges changes; | |
915 | ||
916 | if (!(rcp=cip->exts[chanservext])) | |
917 | return; | |
918 | ||
919 | verifyregchan(rcp); | |
920 | ||
921 | /* Always nullify the checksched even if the channel is suspended.. */ | |
922 | if (rcp->checksched) { | |
923 | deleteschedule(rcp->checksched, cs_timerfunc, arg); | |
924 | rcp->checksched=NULL; | |
925 | } | |
926 | ||
927 | if (!cp || CIsSuspended(rcp)) | |
928 | return; | |
929 | ||
930 | /* Check if we need to leave the channel first */ | |
931 | if (chanservnick && CIsJoined(rcp) && cip->channel && | |
932 | cip->channel->users->totalusers==1 && | |
933 | getnumerichandlefromchanhash(cip->channel->users, chanservnick->numeric)) { | |
934 | ||
935 | /* Only chanserv left in this channel */ | |
936 | if (now >= (rcp->lastpart + LINGERTIME)) { | |
937 | /* Time to go */ | |
334b567e | 938 | localpartchannel(chanservnick, cip->channel, "Empty Channel"); |
c86edd1d Q |
939 | return; |
940 | } else { | |
941 | if (!nextsched || nextsched > (rcp->lastpart + LINGERTIME)) | |
942 | nextsched=rcp->lastpart+LINGERTIME; | |
943 | } | |
944 | } | |
945 | ||
946 | localsetmodeinit(&changes, cp, chanservnick); | |
947 | ||
948 | if (CIsAutoLimit(rcp)) { | |
949 | if (!rcp->limit || rcp->autoupdate <= now) { | |
cbdccc66 | 950 | /* Only update the limit if there are <= (autolimit/2) or |
951 | * >= (autolimit * 1.5) slots free */ | |
952 | ||
953 | if ((cp->users->totalusers >= (rcp->limit - rcp->autolimit/2)) || | |
954 | (cp->users->totalusers <= (rcp->limit - (3 * rcp->autolimit)/2))) { | |
955 | rcp->limit=cp->users->totalusers+rcp->autolimit; | |
956 | rcp->status |= QCSTAT_MODECHECK; | |
957 | } | |
c86edd1d Q |
958 | |
959 | /* And set the schedule for the next update */ | |
960 | rcp->autoupdate = now + AUTOLIMIT_INTERVAL; | |
961 | } | |
962 | ||
963 | if (!nextsched || rcp->autoupdate <= nextsched) | |
964 | nextsched=rcp->autoupdate; | |
965 | } | |
966 | ||
967 | if (rcp->status & QCSTAT_OPCHECK) { | |
968 | rcp->status &= ~QCSTAT_OPCHECK; | |
969 | cs_docheckopvoice(cp, &changes); | |
970 | } | |
971 | ||
972 | if (rcp->status & QCSTAT_MODECHECK) { | |
973 | rcp->status &= ~QCSTAT_MODECHECK; | |
974 | cs_docheckchanmodes(cp, &changes); | |
975 | } | |
976 | ||
977 | if (rcp->status & QCSTAT_BANCHECK) { | |
978 | rcp->status &= ~QCSTAT_BANCHECK; | |
979 | cs_checkbans(cp); | |
980 | } | |
981 | ||
982 | /* This ban check is AFTER docheckopvoice in case checkopvoice set a | |
983 | * ban which we need to automatically remove later.. */ | |
984 | ||
985 | if (rcp->banduration) { | |
986 | for (cbp=cp->bans;cbp;cbp=ncbp) { | |
987 | ncbp=cbp->next; | |
988 | if (cbp->timeset+rcp->banduration<=now) { | |
989 | localdosetmode_ban(&changes, bantostring(cbp), MCB_DEL); | |
990 | } else { | |
991 | if (!nextsched || cbp->timeset+rcp->banduration <= nextsched) { | |
992 | nextsched=rcp->banduration+cbp->timeset; | |
993 | } | |
994 | } | |
995 | } | |
996 | } | |
997 | ||
998 | /* Check for expiry of registered bans */ | |
999 | if (rcp->bans) { | |
1000 | for (rbh=&(rcp->bans);*rbh;) { | |
1001 | rbp=*rbh; | |
1002 | verifyregchanban(rbp); | |
1003 | if (rbp->expiry && (rbp->expiry < now)) { | |
1004 | /* Expired ban */ | |
1005 | /* Remove ban if it's on the channel (localdosetmode_ban will silently | |
1006 | * skip bans that don't exist) */ | |
1007 | localdosetmode_ban(&changes, bantostring(rbp->cbp), MCB_DEL); | |
1008 | /* Remove from database */ | |
1009 | csdb_deleteban(rbp); | |
1010 | /* Remove from list */ | |
1011 | (*rbh)=rbp->next; | |
1012 | /* Free ban/string and update setby refcount, and free actual regban */ | |
1013 | freesstring(rbp->reason); | |
1014 | freechanban(rbp->cbp); | |
1015 | freeregban(rbp); | |
1016 | } else { | |
1017 | if (rbp->expiry && (!nextsched || rbp->expiry <= nextsched)) { | |
1018 | nextsched=rbp->expiry; | |
1019 | } | |
1020 | rbh=&(rbp->next); | |
1021 | } | |
1022 | } | |
1023 | } | |
1024 | ||
1025 | if (nextsched) | |
1026 | rcp->checksched=scheduleoneshot(nextsched, cs_timerfunc, arg); | |
1027 | ||
1028 | localsetmodeflush(&changes, 1); | |
1029 | } | |
1030 | ||
1031 | void cs_removechannel(regchan *rcp) { | |
1032 | int i; | |
1033 | chanindex *cip; | |
1034 | regchanuser *rcup, *nrcup; | |
1035 | regban *rbp, *nrbp; | |
1036 | ||
1037 | cip=rcp->index; | |
1038 | ||
1039 | for (i=0;i<REGCHANUSERHASHSIZE;i++) { | |
1040 | for (rcup=rcp->regusers[i];rcup;rcup=nrcup) { | |
1041 | nrcup=rcup->nextbychan; | |
1042 | delreguserfromchannel(rcp, rcup->user); | |
1043 | freeregchanuser(rcup); | |
1044 | } | |
1045 | } | |
1046 | ||
1047 | for (rbp=rcp->bans;rbp;rbp=nrbp) { | |
1048 | nrbp=rbp->next; | |
1049 | freesstring(rbp->reason); | |
1050 | freechanban(rbp->cbp); | |
1051 | freeregban(rbp); | |
1052 | } | |
1053 | ||
1054 | rcp->bans=NULL; | |
1055 | ||
1056 | if (rcp->checksched) | |
1057 | deleteschedule(rcp->checksched, cs_timerfunc, rcp->index); | |
1058 | ||
1059 | if (cip->channel) { | |
1060 | rcp->flags=QCFLAG_SUSPENDED; | |
1061 | chanservjoinchan(cip->channel); /* Force off the channel */ | |
1062 | } | |
1063 | ||
1064 | csdb_deletechannel(rcp); | |
1065 | ||
1066 | freesstring(rcp->welcome); | |
1067 | freesstring(rcp->topic); | |
1068 | freesstring(rcp->key); | |
1069 | freesstring(rcp->suspendreason); | |
1070 | freesstring(rcp->comment); | |
1071 | ||
1072 | freeregchan(rcp); | |
1073 | ||
1074 | cip->exts[chanservext]=NULL; | |
1075 | releasechanindex(cip); | |
1076 | } | |
1077 | ||
5f7881b2 | 1078 | /* Sender is who the DELCHAN is attributed to.. */ |
1079 | int cs_removechannelifempty(nick *sender, regchan *rcp) { | |
1080 | unsigned int i; | |
1081 | regchanuser *rcup; | |
1082 | ||
1083 | for (i=0;i<REGCHANUSERHASHSIZE;i++) { | |
1084 | for (rcup=rcp->regusers[i];rcup;rcup=rcup->nextbychan) { | |
50ec21f3 | 1085 | if (rcup->flags & ~(QCUFLAGS_PUNISH)) |
5f7881b2 | 1086 | return 0; |
1087 | } | |
1088 | } | |
1089 | ||
8ba555be | 1090 | cs_log(sender,"DELCHAN %s (Empty)",rcp->index->name->content); |
1091 | cs_removechannel(rcp); | |
5f7881b2 | 1092 | |
1093 | return 1; | |
1094 | } | |
1095 | ||
c86edd1d | 1096 | void cs_removeuser(reguser *rup) { |
c86edd1d Q |
1097 | regchanuser *rcup, *nrcup; |
1098 | regchan *rcp; | |
23b85e10 | 1099 | struct authname *anp; |
c86edd1d Q |
1100 | |
1101 | /* Remove the user from all its channels */ | |
1102 | for (rcup=rup->knownon;rcup;rcup=nrcup) { | |
1103 | nrcup=rcup->nextbyuser; | |
c86edd1d Q |
1104 | rcp=rcup->chan; |
1105 | ||
1106 | delreguserfromchannel(rcp, rup); | |
5f7881b2 | 1107 | cs_removechannelifempty(NULL, rcp); |
c86edd1d Q |
1108 | } |
1109 | ||
e3805f60 P |
1110 | if(rup->domain) |
1111 | delreguserfrommaildomain(rup,rup->domain); | |
1112 | freesstring(rup->localpart); | |
c86edd1d | 1113 | freesstring(rup->email); |
9708f78f | 1114 | freesstring(rup->lastemail); |
c86edd1d Q |
1115 | freesstring(rup->lastuserhost); |
1116 | freesstring(rup->suspendreason); | |
1117 | freesstring(rup->comment); | |
1118 | freesstring(rup->info); | |
1119 | ||
1120 | csdb_deleteuser(rup); | |
1121 | ||
1122 | removereguserfromhash(rup); | |
1123 | ||
23b85e10 | 1124 | if ((anp=findauthname(rup->ID)) && anp->nicks) { |
c86edd1d Q |
1125 | rup->status |= QUSTAT_DEAD; |
1126 | } else { | |
1127 | freereguser(rup); | |
1128 | } | |
1129 | } | |
1130 | ||
1131 | int cs_bancheck(nick *np, channel *cp) { | |
1132 | time_t now=time(NULL); | |
1133 | regban **rbh, *rbp; | |
1134 | modechanges changes; | |
1135 | regchan *rcp; | |
1136 | ||
1137 | if (!(rcp=cp->index->exts[chanservext])) | |
1138 | return 0; | |
1139 | ||
1140 | for (rbh=&(rcp->bans);*rbh;) { | |
1141 | if ((*rbh)->expiry && ((*rbh)->expiry < now)) { | |
1142 | /* Expired ban */ | |
1143 | csdb_deleteban(*rbh); | |
1144 | rbp=*rbh; | |
1145 | (*rbh)=rbp->next; | |
1146 | ||
1147 | freesstring(rbp->reason); | |
1148 | freechanban(rbp->cbp); | |
1149 | freeregban(rbp); | |
10771120 | 1150 | } else if (nickmatchban_visible(np,(*rbh)->cbp)) { |
c86edd1d | 1151 | /* This user matches this ban.. */ |
5cde8af0 | 1152 | if (!nickbanned_visible(np,cp)) { |
c86edd1d Q |
1153 | /* Only bother putting the ban on the channel if they're not banned already */ |
1154 | /* (might be covered by this ban or a different one.. doesn't really matter */ | |
1155 | localsetmodeinit(&changes, cp, chanservnick); | |
1156 | localdosetmode_ban(&changes, bantostring((*rbh)->cbp), MCB_ADD); | |
1157 | localsetmodeflush(&changes, 1); | |
1158 | cs_timerfunc(cp->index); | |
1159 | } | |
1160 | localkickuser(chanservnick, cp, np, (*rbh)->reason ? (*rbh)->reason->content : "Banned."); | |
1161 | return 1; | |
1162 | } else { | |
1163 | rbh=&((*rbh)->next); | |
1164 | } | |
1165 | } | |
1166 | ||
1167 | return 0; | |
1168 | } | |
1169 | ||
1170 | void cs_setregban(chanindex *cip, regban *rbp) { | |
1171 | modechanges changes; | |
1172 | int i; | |
1173 | nick *np; | |
1174 | ||
1175 | if (!cip->channel) | |
1176 | return; | |
b263aa79 | 1177 | |
c86edd1d Q |
1178 | localsetmodeinit(&changes, cip->channel, chanservnick); |
1179 | localdosetmode_ban(&changes, bantostring(rbp->cbp), MCB_ADD); | |
1180 | localsetmodeflush(&changes, 1); | |
1181 | ||
1182 | for (i=0;(cip->channel) && i<cip->channel->users->hashsize;i++) { | |
1183 | if (cip->channel->users->content[i]!=nouser && | |
1184 | (np=getnickbynumeric(cip->channel->users->content[i])) && | |
5cdb1f14 | 1185 | !IsService(np) && !IsOper(np) && !IsXOper(np) && |
10771120 | 1186 | nickmatchban_visible(np, rbp->cbp)) |
c86edd1d Q |
1187 | localkickuser(chanservnick, cip->channel, np, rbp->reason ? rbp->reason->content : "Banned."); |
1188 | } | |
1189 | ||
1190 | cs_timerfunc(cip); | |
1191 | } | |
1192 | ||
1193 | void cs_banuser(modechanges *changes, chanindex *cip, nick *np, const char *reason) { | |
1194 | modechanges lchanges; | |
1195 | char banmask[NICKLEN+USERLEN+HOSTLEN+3]; | |
1196 | ||
1197 | if (!cip->channel) | |
1198 | return; | |
1199 | ||
5cde8af0 | 1200 | if (nickbanned_visible(np, cip->channel)) { |
c86edd1d Q |
1201 | localkickuser(chanservnick, cip->channel, np, reason?reason:"Banned."); |
1202 | return; | |
1203 | } | |
1204 | ||
1205 | if (IsAccount(np)) { | |
1206 | /* Ban their account.. */ | |
1207 | sprintf(banmask,"*!*@%s.%s",np->authname,HIS_HIDDENHOST); | |
1208 | } else if (IsSetHost(np)) { | |
1209 | /* sethosted: ban ident@host */ | |
1210 | sprintf(banmask,"*!%s@%s",np->shident->content,np->sethost->content); | |
1211 | } else if (np->host->clonecount>3) { | |
1212 | /* >3 clones, ban user@host */ | |
1213 | sprintf(banmask,"*!%s@%s",np->ident,np->host->name->content); | |
1214 | } else { | |
1215 | sprintf(banmask,"*!*@%s",np->host->name->content); | |
1216 | } | |
1217 | ||
1218 | if (!changes) { | |
1219 | localsetmodeinit(&lchanges, cip->channel, chanservnick); | |
1220 | localdosetmode_ban(&lchanges, banmask, MCB_ADD); | |
1221 | localsetmodeflush(&lchanges, 1); | |
1222 | } else { | |
1223 | localdosetmode_ban(changes, banmask, MCB_ADD); | |
1224 | } | |
1225 | ||
1226 | localkickuser(chanservnick, cip->channel, np, reason?reason:"Banned."); | |
1227 | } | |
95332d7b | 1228 | |
1229 | /* | |
1230 | * cs_sanitisechanlev: Removes impossible combinations from chanlev flags. | |
1231 | * chanservuser.c is probably not the right file for this, but nowhere better | |
1232 | * presented itself... | |
1233 | */ | |
1234 | flag_t cs_sanitisechanlev(flag_t flags) { | |
1235 | /* +m or +n cannot have any "punishment" flags */ | |
1236 | if (flags & (QCUFLAG_MASTER | QCUFLAG_OWNER)) | |
50ec21f3 | 1237 | flags &= ~(QCUFLAGS_PUNISH); |
95332d7b | 1238 | |
1239 | /* +d can't be +o */ | |
1240 | if (flags & QCUFLAG_DENY) | |
1241 | flags &= ~QCUFLAG_OP; | |
1242 | ||
1243 | /* +q can't be +v */ | |
1244 | if (flags & QCUFLAG_QUIET) | |
1245 | flags &= ~QCUFLAG_VOICE; | |
1246 | ||
1247 | /* +p trumps +a and +g */ | |
1248 | if (flags & QCUFLAG_PROTECT) | |
1249 | flags &= ~(QCUFLAG_AUTOOP | QCUFLAG_AUTOVOICE); | |
1250 | ||
1251 | /* -o can't be +a */ | |
1252 | if (!(flags & QCUFLAG_OP)) | |
1253 | flags &= ~QCUFLAG_AUTOOP; | |
1254 | ||
1255 | /* +a or -v can't be +g. +a implies +o at this stage (see above) */ | |
1256 | if (!(flags & QCUFLAG_VOICE) || (flags & QCUFLAG_AUTOOP)) | |
1257 | flags &= ~QCUFLAG_AUTOVOICE; | |
1258 | ||
1259 | /* +p requires +o or +v */ | |
1260 | if (!(flags & (QCUFLAG_VOICE | QCUFLAG_OP))) | |
1261 | flags &= ~QCUFLAG_PROTECT; | |
1262 | ||
e8e09b86 | 1263 | /* The personal flags require one of +mnovk, as does +t */ |
95332d7b | 1264 | if (!(flags & (QCUFLAG_OWNER | QCUFLAG_MASTER | QCUFLAG_OP | QCUFLAG_VOICE | QCUFLAG_KNOWN))) |
e8e09b86 | 1265 | flags &= ~(QCUFLAGS_PERSONAL | QCUFLAG_TOPIC); |
95332d7b | 1266 | |
1267 | return flags; | |
1268 | } | |
c86edd1d | 1269 | |
b263aa79 | 1270 | /* |
1271 | * findreguser: | |
1272 | * This function does the standard "nick or #user" lookup. | |
1273 | * If "sender" is not NULL, a suitable error message will | |
1274 | * be sent if the lookup fails. | |
4630ab32 CP |
1275 | * "sender" MUST be sent when a user is requesting a lookup |
1276 | * as there is some policy code here. | |
b263aa79 | 1277 | */ |
1278 | ||
1279 | reguser *findreguser(nick *sender, const char *str) { | |
4630ab32 | 1280 | reguser *rup, *vrup = getreguserfromnick(sender);; |
b263aa79 | 1281 | nick *np; |
1282 | ||
1283 | if (!str || !*str) | |
1284 | return NULL; | |
1285 | ||
1286 | if (*str=='#') { | |
1287 | if (str[1]=='\0') { | |
1288 | if (sender) | |
1289 | chanservstdmessage(sender, QM_UNKNOWNUSER, str); | |
1290 | return NULL; | |
1291 | } | |
1292 | if (!(rup=findreguserbynick(str+1)) && sender) | |
1293 | chanservstdmessage(sender, QM_UNKNOWNUSER, str); | |
372e4f1d | 1294 | } else if (*str=='&' && vrup && UHasStaffPriv(vrup)) { |
4630ab32 CP |
1295 | if (str[1]=='\0') { |
1296 | if (sender) | |
1297 | chanservstdmessage(sender, QM_UNKNOWNUSER, str); | |
1298 | return NULL; | |
1299 | } | |
1300 | if (!(rup=findreguserbyID(atoi(str+1))) && sender) | |
1301 | chanservstdmessage(sender, QM_UNKNOWNUSER, str); | |
b263aa79 | 1302 | } else { |
1303 | if (!(np=getnickbynick(str))) { | |
1304 | if (sender) | |
1305 | chanservstdmessage(sender, QM_UNKNOWNUSER, str); | |
1306 | return NULL; | |
1307 | } | |
1308 | if (!(rup=getreguserfromnick(np)) && sender) | |
1309 | chanservstdmessage(sender, QM_USERNOTAUTHED, str); | |
1310 | } | |
1311 | ||
4002da60 CP |
1312 | /* removed the suspended check from here, I don't see the point... */ |
1313 | if (rup && (rup->status & QUSTAT_DEAD)) { | |
b263aa79 | 1314 | chanservstdmessage(sender, QM_USERHASBADAUTH, rup->username); |
1315 | return NULL; | |
1316 | } | |
1317 | ||
1318 | return rup; | |
1319 | } | |
336765cd CP |
1320 | |
1321 | /* | |
1322 | * Unbans a mask from a channel, including permbans if user has correct privs. | |
0a53499c | 1323 | * |
1324 | * Return 0 if it works, 1 if it don't. | |
336765cd | 1325 | */ |
0a53499c | 1326 | int cs_unbanfn(nick *sender, chanindex *cip, UnbanFN fn, void *arg, int removepermbans, int abortonfailure) { |
336765cd CP |
1327 | regban **rbh, *rbp; |
1328 | chanban **cbh, *cbp; | |
1329 | regchan *rcp; | |
1330 | modechanges changes; | |
1331 | char *banstr; | |
1332 | ||
1333 | rcp=cip->exts[chanservext]; | |
1334 | ||
1335 | if (cip->channel) | |
1336 | localsetmodeinit(&changes, cip->channel, chanservnick); | |
1337 | ||
1338 | for (rbh=&(rcp->bans); *rbh; ) { | |
1339 | rbp=*rbh; | |
1340 | if (fn(arg, rbp->cbp)) { | |
1341 | banstr=bantostring(rbp->cbp); | |
1342 | /* Check perms and remove */ | |
1343 | if(!removepermbans) { | |
1344 | chanservstdmessage(sender, QM_WARNNOTREMOVEDPERMBAN, banstr, cip->name->content); | |
1345 | rbh=&(rbp->next); | |
1346 | } else if (!cs_checkaccess(sender, NULL, CA_MASTERPRIV, cip, NULL, 0, 1)) { | |
1347 | chanservstdmessage(sender, QM_NOTREMOVEDPERMBAN, banstr, cip->name->content); | |
0a53499c | 1348 | if (abortonfailure) return 1; /* Just give up... */ |
336765cd CP |
1349 | rbh=&(rbp->next); |
1350 | } else { | |
1351 | chanservstdmessage(sender, QM_REMOVEDPERMBAN, banstr, cip->name->content); | |
1352 | if (cip->channel) | |
1353 | localdosetmode_ban(&changes, banstr, MCB_DEL); | |
1354 | /* Remove from database */ | |
1355 | csdb_deleteban(rbp); | |
1356 | /* Remove from list */ | |
1357 | (*rbh)=rbp->next; | |
1358 | /* Free ban/string and update setby refcount, and free actual regban */ | |
1359 | freesstring(rbp->reason); | |
1360 | freechanban(rbp->cbp); | |
1361 | freeregban(rbp); | |
1362 | } | |
1363 | } else { | |
1364 | rbh=&(rbp->next); | |
1365 | } | |
1366 | } | |
1367 | ||
1368 | if (cip->channel) { | |
1369 | for (cbh=&(cip->channel->bans); *cbh; ) { | |
1370 | cbp=*cbh; | |
1371 | if (fn(arg, cbp)) { | |
1372 | /* Remove */ | |
1373 | banstr=bantostring(cbp); | |
1374 | chanservstdmessage(sender, QM_REMOVEDCHANBAN, banstr, cip->name->content); | |
1375 | localdosetmode_ban(&changes, banstr, MCB_DEL); | |
1376 | } else { | |
1377 | cbh=&(cbp->next); | |
1378 | } | |
1379 | } | |
1380 | localsetmodeflush(&changes,1); | |
1381 | } | |
0a53499c | 1382 | |
1383 | return 0; | |
336765cd | 1384 | } |
1482fb78 | 1385 | |
1386 | /* Add entry to channel op history when someone gets ops. */ | |
1387 | void cs_logchanop(regchan *rcp, char *nick, reguser *rup) { | |
1388 | strncpy(rcp->chanopnicks[rcp->chanoppos], nick, NICKLEN); | |
1389 | rcp->chanopnicks[rcp->chanoppos][NICKLEN]='\0'; | |
1390 | rcp->chanopaccts[rcp->chanoppos]=rup->ID; | |
1391 | rcp->chanoppos=(rcp->chanoppos+1)%CHANOPHISTORY; | |
1392 | } |