]>
jfr.im git - irc/quakenet/newserv.git/blob - chanserv/chanservuser.c
3 * This file maintains the functions associated with the
4 * user on the network relating to the channel service
9 #include "../core/hooks.h"
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"
19 #include "../lib/irc_string.h"
27 CommandTree
*cscommands
;
28 CommandTree
*csctcpcommands
;
30 /* Local prototypes */
31 void chanservuserhandler(nick
*target
, int message
, void **params
);
33 void chanservreguser(void *arg
) {
34 sstring
*csnick
,*csuser
,*cshost
,*csrealname
,*csaccount
;
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
);
43 csaccount
=getcopyconfigitem("chanserv","account",csnick
&&csnick
->content
&&csnick
->content
[0]?csnick
->content
:"Q",ACCOUNTLEN
);
45 Error("chanserv",ERR_INFO
,"Connecting %s...",csnick
->content
);
47 chanservnick
=registerlocaluser(csnick
->content
,csuser
->content
,cshost
->content
,
48 csrealname
->content
,csaccount
->content
,
49 UMODE_INV
|UMODE_SERVICE
|UMODE_DEAF
|UMODE_OPER
|UMODE_ACCOUNT
,
50 &chanservuserhandler
);
55 freesstring(csrealname
);
56 freesstring(csaccount
);
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
)) {
62 /* This will do timestamp faffing even if it won't actually join */
63 chanservjoinchan(cip
->channel
);
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
: "");
73 Error("chanserv",ERR_INFO
,"Loaded and joined channels.");
75 if (chanserv_init_status
== CS_INIT_NOUSER
) {
76 /* If this is the first time, perform last init tasks */
81 void chanservuserhandler(nick
*target
, int message
, void **params
) {
92 scheduleoneshot(time(NULL
),&chanservreguser
,NULL
);
98 sender
=(nick
*)params
[0];
99 msg
=(char *)params
[1];
101 if (!(cargc
=splitline(msg
,cargv
,30,0)))
102 return; /* Ignore blank lines */
104 if (cargv
[0][0]==1) {
106 for (chp
=cargv
[0]+1;*chp
;chp
++) {
112 cmd
=findcommandintree(csctcpcommands
, cargv
[0]+1, 1);
115 rejoinline(cargv
[1],cargc
-1);
116 cmd
->handler((void *)sender
, cargc
-1, &(cargv
[1]));
119 cmd
=findcommandintree(cscommands
, cargv
[0], 1);
122 chanservstdmessage(sender
, QM_UNKNOWNCMD
, cargv
[0]);
126 if ((cmd
->level
& QCMD_SECURE
) && (message
!= LU_SECUREMSG
)) {
127 chanservstdmessage(sender
, QM_SECUREONLY
, cargv
[0], chanservnick
->nick
, myserver
->content
);
131 if ((cmd
->level
& QCMD_AUTHED
)) {
132 if (!(rup
=getreguserfromnick(sender
))) {
133 chanservstdmessage(sender
, QM_AUTHEDONLY
, cargv
[0]);
136 if (UIsSuspended(rup
) || (rup
->status
& QUSTAT_DEAD
)) {
137 chanservstdmessage(sender
, QM_BADAUTH
, cargv
[0]);
142 if ((cmd
->level
& QCMD_NOTAUTHED
) && (getreguserfromnick(sender
))) {
143 chanservstdmessage(sender
, QM_UNAUTHEDONLY
, cargv
[0]);
147 if ((cmd
->level
& QCMD_STAFF
) &&
148 (!(rup
=getreguserfromnick(sender
)) || !UHasStaffPriv(rup
))) {
149 chanservstdmessage(sender
, QM_NOACCESS
, cargv
[0]);
153 if ((cmd
->level
& QCMD_HELPER
) &&
154 (!(rup
=getreguserfromnick(sender
)) || !UHasHelperPriv(rup
))) {
155 chanservstdmessage(sender
, QM_NOACCESS
, cargv
[0]);
159 if ((cmd
->level
& QCMD_OPER
) &&
160 (!IsOper(sender
) || !(rup
=getreguserfromnick(sender
)) || !UHasOperPriv(rup
))) {
161 chanservstdmessage(sender
, QM_NOACCESS
, cargv
[0]);
165 if ((cmd
->level
& QCMD_ADMIN
) &&
166 (!IsOper(sender
) || !(rup
=getreguserfromnick(sender
)) || !UHasAdminPriv(rup
))) {
167 chanservstdmessage(sender
, QM_NOACCESS
, cargv
[0]);
171 if ((cmd
->level
& QCMD_DEV
) &&
172 (!IsOper(sender
) || !(rup
=getreguserfromnick(sender
)) || !UIsDev(rup
))) {
173 chanservstdmessage(sender
, QM_NOACCESS
, cargv
[0]);
179 if (cmd
->maxparams
< (cargc
-1)) {
180 rejoinline(cargv
[cmd
->maxparams
],cargc
-(cmd
->maxparams
));
181 cargc
=(cmd
->maxparams
)+1;
184 cmd
->handler((void *)sender
, cargc
-1, &(cargv
[1]));
193 void chanservcommandinit() {
194 cscommands
=newcommandtree();
195 csctcpcommands
=newcommandtree();
198 void chanservcommandclose() {
199 destroycommandtree(cscommands
);
200 destroycommandtree(csctcpcommands
);
203 void chanservaddcommand(char *command
, int flags
, int maxparams
, CommandHandler handler
, char *description
, const char *help
) {
207 newcmd
=addcommandtotree(cscommands
, command
, flags
, maxparams
, handler
);
208 /* Allocate a summary record to put the description in */
209 summary
=(cmdsummary
*)malloc(sizeof(cmdsummary
));
210 memset((void *)summary
,0,sizeof(cmdsummary
));
212 summary
->def
=getsstring(description
, 250);
213 summary
->defhelp
=(char *)help
; /* Assume that help is a constant */
215 newcmd
->ext
=(void *)summary
;
216 loadcommandsummary(newcmd
);
219 void chanservaddctcpcommand(char *command
, CommandHandler handler
) {
220 addcommandtotree(csctcpcommands
, command
, 0, 1, handler
);
223 void chanservremovectcpcommand(char *command
, CommandHandler handler
) {
224 deletecommandfromtree(csctcpcommands
, command
, handler
);
227 void chanservremovecommand(char *command
, CommandHandler handler
) {
232 if (!(cmd
=findcommandintree(cscommands
, command
, 1))) {
233 Error("chanserv",ERR_WARNING
,"Tried to unregister unknown command %s",command
);
237 summary
=(cmdsummary
*)cmd
->ext
;
238 freesstring(summary
->def
);
239 for (i
=0;i
<MAXLANG
;i
++) {
240 if (summary
->bylang
[i
])
241 freesstring(summary
->bylang
[i
]);
246 deletecommandfromtree(cscommands
, command
, handler
);
249 void chanservpartchan(channel
*cp
, char *reason
) {
250 /* Sanity check that we exist and are on the channel.
252 * Note that we don't do any of the other usual sanity checks here; if
253 * this channel is unregged or suspended or whatever then all the more
254 * reason to get Q off it ASAP! */
255 if (!chanservnick
|| !cp
|| !cp
->users
|| !getnumerichandlefromchanhash(cp
->users
, chanservnick
->numeric
))
258 localpartchannel(chanservnick
, cp
, reason
);
261 void chanservjoinchan(channel
*cp
) {
269 /* Skip unregistered channels */
270 if (!(rcp
=cp
->index
->exts
[chanservext
]))
273 /* Check for a new timestamp. But don't do anything stupid if the incoming timestamp is 0 */
274 if (cp
->timestamp
&& ((!rcp
->ltimestamp
) || (cp
->timestamp
< rcp
->ltimestamp
))) {
275 rcp
->ltimestamp
=cp
->timestamp
;
276 csdb_updatechanneltimestamp(rcp
);
279 /* We won't be doing any joining or parting if we're not online */
283 /* In gerenal this function shouldn't be used any more as a reason to get
284 * Q to leave, but if it should be leaving then just part with no reason. */
285 if ((CIsSuspended(rcp
) || !CIsJoined(rcp
)) && getnumerichandlefromchanhash(cp
->users
, chanservnick
->numeric
)) {
286 localpartchannel(chanservnick
, cp
, NULL
);
289 /* Right, we are definately going to either join the channel or at least
290 * burst some modes onto it.
292 * We will try and burst our view of the world; if the timestamps are
293 * actually equal this will be mostly ignored and we will have to fix it
294 * up later. For modes we use the forced modes, plus the default channel
295 * modes (unless any of those are explicitly denied) */
297 /* By default, we set the forcemodes and the default modes, but never denymodes..
298 * OK, actually we should only set the default modes if our timestamp is older.*/
299 if (cp
->timestamp
> rcp
->ltimestamp
)
300 themodes
|= CHANMODE_DEFAULT
;
304 themodes
= (themodes
| rcp
->forcemodes
) & ~rcp
->denymodes
;
306 /* Now, if someone has just created a channel and we are going to set +i
307 * or +k on it, this will kick them off. This could be construed as a
308 * bit rude if it's their channel, so if there is only one person on the
309 * channel and they have a right to be there, burst with default modes
310 * only to avoid them being netrider kicked.
312 if (cp
->users
->totalusers
==1) {
313 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
314 if (cp
->users
->content
[i
] != nouser
) {
315 if ((np
=getnickbynumeric(cp
->users
->content
[i
]&CU_NUMERICMASK
)) &&
316 (rup
=getreguserfromnick(np
)) &&
317 (rcup
=findreguseronchannel(rcp
,rup
)) &&
319 /* OK, there was one user, and they are known on this channel.
320 * Don't burst with +i or +k */
321 themodes
&= ~(CHANMODE_INVITEONLY
| CHANMODE_KEY
);
327 /* We should be on the channel - join with our nick */
328 if (!CIsSuspended(rcp
) && CIsJoined(rcp
) && !getnumerichandlefromchanhash(cp
->users
, chanservnick
->numeric
)) {
329 /* If we pass the key parameter here but are not setting +k (see above)
330 * then localburstontochannel() will ignore the key */
331 localburstontochannel(cp
, chanservnick
, rcp
->ltimestamp
, themodes
,
332 rcp
->limit
, (rcp
->key
)?rcp
->key
->content
:NULL
);
335 /* We're not joining the channel - send the burst with no nick */
336 if (!CIsSuspended(rcp
) && !CIsJoined(rcp
) && (cp
->timestamp
> rcp
->ltimestamp
)) {
337 localburstontochannel(cp
, NULL
, rcp
->ltimestamp
, themodes
,
338 rcp
->limit
, (rcp
->key
)?rcp
->key
->content
:NULL
);
342 void chanservstdmessage(nick
*np
, int messageid
, ... ) {
349 char *message
, *messageargs
;
353 if (getreguserfromnick(np
) == NULL
) {
357 rup
=getreguserfromnick(np
);
363 language
=rup
->languageid
;
366 if (csmessages
[language
][messageid
]) {
367 message
=csmessages
[language
][messageid
]->content
;
368 } else if (csmessages
[0][messageid
]) {
369 message
=csmessages
[0][messageid
]->content
;
371 message
=defaultmessages
[messageid
*2];
374 messageargs
=defaultmessages
[messageid
*2+1];
376 va_start(va
,messageid
);
378 q9vsnprintf(buf
,5000,message
,messageargs
,va
);
383 for (bp
=buf
; ;bp
++) {
384 /* We send something if we hit a \n, fill the buffer or run out of source */
385 if (*bp
=='\n' || len
>490 || !*bp
) {
386 if (*bp
&& *bp
!='\n') {
392 if (chanservnick
&& *buf2
) {
394 sendnoticetouser(chanservnick
,np
,"%s",buf2
);
396 sendmessagetouser(chanservnick
,np
,"%s",buf2
);
400 /* If we ran out of buffer, get out here */
412 /* Special case: If it's a "not enough parameters" message, show the first line of help */
413 if (messageid
==QM_NOTENOUGHPARAMS
) {
414 char *command
=va_arg(va2
, char *);
415 cs_sendhelp(np
, command
, 1);
416 chanservstdmessage(np
, QM_TYPEHELPFORHELP
, command
);
422 void chanservsendmessage_real(nick
*np
, int oneline
, char *message
, ... ) {
423 char buf
[5010]; /* Very large buffer.. */
424 char buf2
[512], *bp
, *bp2
;
431 va_start(va
,message
);
432 vsnprintf(buf
,5000,message
,va
);
435 if (getreguserfromnick(np
) == NULL
) {
438 rup
=getreguserfromnick(np
);
448 for (bp
=buf
; ;bp
++) {
449 /* We send something if we hit a \n, fill the buffer or run out of source */
450 if (*bp
=='\n' || len
>490 || !*bp
) {
451 if (*bp
&& *bp
!='\n') {
457 if (chanservnick
&& *buf2
) {
459 sendnoticetouser(chanservnick
,np
,"%s",buf2
);
461 sendmessagetouser(chanservnick
,np
,"%s",buf2
);
465 /* If we ran out of buffer, get out here */
466 if (!*bp
|| (*bp
=='\n' && oneline
))
478 void chanservwallmessage(char *message
, ... ) {
479 char buf
[5010]; /* Very large buffer.. */
484 va_start(va
,message
);
485 vsnprintf(buf
,5000,message
,va
);
489 for (i
=0;i
<NICKHASHSIZE
;i
++) {
490 for (np
=nicktable
[i
];np
;np
=np
->next
) {
491 if (!IsOper(np
)) /* optimisation, if VIEWWALLMESSAGE changes change this */
494 if (!cs_privcheck(QPRIV_VIEWWALLMESSAGE
, np
))
497 chanservsendmessage(np
, "%s", buf
);
502 void chanservkillstdmessage(nick
*target
, int messageid
, ... ) {
505 char *message
, *messageargs
;
509 if (!(rup
=getreguserfromnick(target
)))
512 language
=rup
->languageid
;
514 if (csmessages
[language
][messageid
])
515 message
=csmessages
[language
][messageid
]->content
;
516 else if (csmessages
[0][messageid
])
517 message
=csmessages
[0][messageid
]->content
;
519 message
=defaultmessages
[messageid
*2];
521 messageargs
=defaultmessages
[messageid
*2+1];
522 va_start(va
, messageid
);
523 q9vsnprintf(buf
, 511, message
, messageargs
, va
);
525 killuser(chanservnick
, target
, "%s", buf
);
528 int checkpassword(reguser
*rup
, const char *pass
) {
529 if (!(*rup
->password
))
532 if (!strncmp(rup
->password
, pass
, PASSLEN
))
537 int checkresponse(reguser
*rup
, const unsigned char *entropy
, const char *response
, CRAlgorithm algorithm
) {
538 char usernamel
[NICKLEN
+1], *dp
, *up
;
540 if (!(*rup
->password
))
543 for(up
=rup
->username
,dp
=usernamel
;*up
;)
544 *dp
++ = ToLower(*up
++);
547 return algorithm(usernamel
, rup
->password
, cs_calcchallenge(entropy
), response
);
550 int checkhashpass(reguser
*rup
, const char *junk
, const char *hash
) {
551 char usernamel
[NICKLEN
+1], *dp
, *up
;
553 for(up
=rup
->username
,dp
=usernamel
;*up
;)
554 *dp
++ = ToLower(*up
++);
557 return cs_checkhashpass(usernamel
, rup
->password
, junk
, hash
);
560 int setpassword(reguser
*rup
, const char *pass
) {
561 strncpy(rup
->password
, pass
, PASSLEN
);
562 rup
->password
[PASSLEN
]='\0';
566 void cs_checknick(nick
*np
) {
569 char userhost
[USERLEN
+HOSTLEN
+3];
571 if (!(aup
=getactiveuserfromnick(np
))) {
573 np
->exts
[chanservnext
]=aup
;
576 assert(getactiveuserfromnick(np
));
578 if (IsAccount(np
) && np
->auth
) {
579 if (np
->auth
->exts
[chanservaext
]) {
580 rup
=getreguserfromnick(np
);
584 if (UIsDelayedGline(rup
)) {
585 /* delayed-gline - schedule the user's squelching */
586 deleteschedule(NULL
, &chanservdgline
, (void*)rup
); /* icky, but necessary unless we stick more stuff in reguser structure */
587 scheduleoneshot(time(NULL
)+rand()%900
, &chanservdgline
, (void*)rup
);
588 } else if (UIsGline(rup
)) {
589 /* instant-gline - lets be lazy and set a schedule expiring now :) */
590 deleteschedule(NULL
, &chanservdgline
, (void*)rup
); /* icky, but necessary unless we stick more stuff in reguser structure */
591 scheduleoneshot(time(NULL
), &chanservdgline
, (void*)rup
);
592 } else if(UHasSuspension(rup
)) {
593 chanservkillstdmessage(np
, QM_SUSPENDKILL
);
597 cs_doallautomodes(np
);
599 /* Auto create user.. */
602 rup
->ID
=np
->auth
->userid
;
603 if (rup
->ID
> lastuserID
)
605 strncpy(rup
->username
,np
->authname
,NICKLEN
); rup
->username
[NICKLEN
]='\0';
606 rup
->created
=time(NULL
);
608 rup
->lastemailchange
=0;
609 rup
->lastpasschange
=0;
610 rup
->flags
=QUFLAG_NOTICE
;
616 rup
->password
[0]='\0';
622 sprintf(userhost
,"%s@%s",np
->ident
,np
->host
->name
->content
);
623 rup
->lastuserhost
=getsstring(userhost
,USERLEN
+HOSTLEN
+1);
624 rup
->suspendreason
=NULL
;
630 addregusertohash(rup
);
632 csdb_createuser(rup
);
636 cs_checknickbans(np
);
639 void cs_checkchanmodes(channel
*cp
) {
642 localsetmodeinit (&changes
, cp
, chanservnick
);
643 cs_docheckchanmodes(cp
, &changes
);
644 localsetmodeflush (&changes
, 1);
647 void cs_docheckchanmodes(channel
*cp
, modechanges
*changes
) {
653 if ((rcp
=cp
->index
->exts
[chanservext
])==NULL
|| CIsSuspended(rcp
))
656 /* Take care of the simple modes */
657 localdosetmode_simple(changes
, rcp
->forcemodes
& ~(cp
->flags
), rcp
->denymodes
& cp
->flags
);
659 /* Check for forced key. Note that we ALWAYS call localdosetmode_key
660 * in case the wrong key is set atm */
661 if (rcp
->forcemodes
& CHANMODE_KEY
) {
663 Error("chanserv",ERR_WARNING
,"Key forced but no key specified for %s: cleared forcemode.",rcp
->index
->name
->content
);
664 rcp
->forcemodes
&= ~CHANMODE_KEY
;
665 csdb_updatechannel(rcp
);
667 localdosetmode_key(changes
, rcp
->key
->content
, MCB_ADD
);
671 /* Check for denied key */
672 if ((rcp
->denymodes
& CHANMODE_KEY
) && IsKey(cp
)) {
673 localdosetmode_key(changes
, NULL
, MCB_DEL
);
676 /* Check for forced limit. Always call in case of wrong limit */
677 if (rcp
->forcemodes
& CHANMODE_LIMIT
) {
678 localdosetmode_limit(changes
, rcp
->limit
, MCB_ADD
);
682 /* Slightly misnamed function that checks each USER on the channel against channel
683 * settings. Possible actions are opping/deopping, voicing/devoicing, and banning
684 * for chanflag +k or chanlev flag +b */
685 void cs_docheckopvoice(channel
*cp
, modechanges
*changes
) {
695 if ((rcp
=cp
->index
->exts
[chanservext
])==NULL
|| CIsSuspended(rcp
))
698 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
699 if (cp
->users
->content
[i
]==nouser
)
702 if ((np
=getnickbynumeric(cp
->users
->content
[i
]))==NULL
) {
703 Error("chanserv",ERR_ERROR
,"Found non-existent numeric %lu on channel %s",cp
->users
->content
[i
],
704 cp
->index
->name
->content
);
708 if ((rup
=getreguserfromnick(np
)))
709 rcup
=findreguseronchannel(rcp
,rup
);
713 /* Various things that might ban the user - don't apply these to +o, +k or +X users */
714 if (!IsService(np
) && !IsXOper(np
) && !IsOper(np
)) {
715 if (rcup
&& CUIsBanned(rcup
)) {
716 cs_banuser(changes
, cp
->index
, np
, NULL
);
720 /* chanflag +k checks; kick them if they "obviously" can't rejoin without a ban */
721 if (CIsKnownOnly(rcp
) && !(rcup
&& CUKnown(rcup
))) {
722 if (IsInviteOnly(cp
) || (IsRegOnly(cp
) && !IsAccount(np
))) {
723 localkickuser(chanservnick
,cp
,np
,"Authorised users only.");
725 cs_banuser(NULL
, cp
->index
, np
, "Authorised users only.");
731 if ((cp
->users
->content
[i
] & CUMODE_OP
) && !IsService(np
)) {
732 if ((CIsBitch(rcp
) && (!rcup
|| !CUHasOpPriv(rcup
))) ||
733 (rcup
&& CUIsDeny(rcup
)))
734 localdosetmode_nick(changes
, np
, MC_DEOP
);
736 if (rcup
&& (CUIsProtect(rcup
) || CIsProtect(rcp
)) && CUIsOp(rcup
) && !CUIsDeny(rcup
)) {
737 localdosetmode_nick(changes
, np
, MC_OP
);
738 cs_logchanop(rcp
, np
->nick
, rcup
->user
);
742 if (cp
->users
->content
[i
] & CUMODE_VOICE
) {
743 if (rcup
&& CUIsQuiet(rcup
))
744 localdosetmode_nick(changes
, np
, MC_DEVOICE
);
746 if (rcup
&& (CUIsProtect(rcup
) || CIsProtect(rcp
)) && CUIsVoice(rcup
) && !CUIsQuiet(rcup
) && !(cp
->users
->content
[i
] & CUMODE_OP
))
747 localdosetmode_nick(changes
, np
, MC_VOICE
);
752 void cs_doallautomodes(nick
*np
) {
758 if (!(rup
=getreguserfromnick(np
)))
761 for (rcup
=rup
->knownon
;rcup
;rcup
=rcup
->nextbyuser
) {
762 /* Skip suspended channels */
763 if (CIsSuspended(rcup
->chan
))
766 if (rcup
->chan
->index
->channel
) {
768 if ((lp
=getnumerichandlefromchanhash(rcup
->chan
->index
->channel
->users
, np
->numeric
))) {
769 /* User is on channel.. */
771 if (CUKnown(rcup
) && rcup
->chan
->index
->channel
->users
->totalusers
>= 3) {
772 /* This meets the channel use criteria, update. */
773 rcup
->chan
->lastactive
=time(NULL
);
775 /* Don't spam the DB though for channels with lots of joins */
776 if (rcup
->chan
->lastcountersync
< (time(NULL
) - COUNTERSYNCINTERVAL
)) {
777 csdb_updatechannelcounters(rcup
->chan
);
778 rcup
->chan
->lastcountersync
=time(NULL
);
782 /* Update last use time */
783 rcup
->usetime
=getnettime();
785 localsetmodeinit(&changes
, rcup
->chan
->index
->channel
, chanservnick
);
786 if (*lp
& CUMODE_OP
) {
787 if (!IsService(np
) && (CUIsDeny(rcup
) || (CIsBitch(rcup
->chan
) && !CUHasOpPriv(rcup
))))
788 localdosetmode_nick(&changes
, np
, MC_DEOP
);
790 if (!CUIsDeny(rcup
) && CUIsOp(rcup
) &&
791 (CUIsProtect(rcup
) || CIsProtect(rcup
->chan
) || CUIsAutoOp(rcup
))) {
792 localdosetmode_nick(&changes
, np
, MC_OP
);
793 cs_logchanop(rcup
->chan
, np
->nick
, rup
);
797 if (*lp
& CUMODE_VOICE
) {
799 localdosetmode_nick(&changes
, np
, MC_DEVOICE
);
801 if (!CUIsQuiet(rcup
) && CUIsVoice(rcup
) && !(*lp
& CUMODE_OP
) &&
802 (CUIsProtect(rcup
) || CIsProtect(rcup
->chan
) || CUIsAutoVoice(rcup
)))
803 localdosetmode_nick(&changes
, np
, MC_VOICE
);
805 localsetmodeflush(&changes
, 1);
807 /* Channel exists but user is not joined: invite if they are +j-b */
808 if (CUIsAutoInvite(rcup
) && CUKnown(rcup
) && !CUIsBanned(rcup
)) {
809 localinvite(chanservnick
, rcup
->chan
->index
->channel
, np
);
816 void cs_checknickbans(nick
*np
) {
821 if (IsService(np
) || IsOper(np
) || IsXOper(np
))
824 /* Avoid races: memcpy the channel array */
825 i
=np
->channels
->cursi
;
826 ca
=(channel
**)malloc(i
*sizeof(channel
*));
827 memcpy(ca
, np
->channels
->content
,i
*sizeof(channel
*));
830 if ((rcp
=ca
[j
]->index
->exts
[chanservext
]) && !CIsSuspended(rcp
) &&
831 CIsEnforce(rcp
) && nickbanned(np
, ca
[j
], 1))
832 localkickuser(chanservnick
, ca
[j
], np
, "Banned.");
838 void cs_checkbans(channel
*cp
) {
850 if ((rcp
=cp
->index
->exts
[chanservext
])==NULL
|| CIsSuspended(rcp
))
855 localsetmodeinit(&changes
, cp
, chanservnick
);
857 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
858 if (cp
->users
->content
[i
]==nouser
)
861 if ((np
=getnickbynumeric(cp
->users
->content
[i
]))==NULL
) {
862 Error("chanserv",ERR_ERROR
,"Found numeric %lu on channel %s who doesn't exist.",
863 cp
->users
->content
[i
], cp
->index
->name
->content
);
867 if (IsService(np
) || IsOper(np
) || IsXOper(np
))
870 for (rbp
=rcp
->bans
;rbp
;rbp
=rbp
->next
) {
871 if (((!rbp
->expiry
) || (rbp
->expiry
<= now
)) &&
872 nickmatchban(np
, rbp
->cbp
, 1)) {
873 if (!nickbanned(np
, cp
, 1)) {
874 localdosetmode_ban(&changes
, bantostring(rbp
->cbp
), MCB_ADD
);
876 localkickuser(chanservnick
,cp
,np
,rbp
->reason
?rbp
->reason
->content
:"Banned.");
881 if (rbp
) /* If we broke out of above loop (due to kick) rbp will be set.. */
884 if (CIsEnforce(rcp
)) {
885 for (cbp
=cp
->bans
;cbp
;cbp
=cbp
->next
) {
886 if ((cbp
->timeset
>=rcp
->lastbancheck
) && nickmatchban(np
, cbp
, 1))
887 localkickuser(chanservnick
,cp
,np
,"Banned.");
889 rcp
->lastbancheck
=time(NULL
);
893 localsetmodeflush(&changes
,1);
898 * This function schedules an update check on a channel
900 void cs_schedupdate(chanindex
*cip
, int mintime
, int maxtime
) {
901 int delay
=mintime
+(rand()%(maxtime
-mintime
));
904 if (!(rcp
=cip
->exts
[chanservext
]) || CIsSuspended(rcp
))
907 if (rcp
->checksched
) {
908 deleteschedule(rcp
->checksched
, cs_timerfunc
, cip
);
911 rcp
->checksched
=scheduleoneshot(time(NULL
)+delay
, cs_timerfunc
, cip
);
916 * This function does several things:
917 * * Updates auto-limit and/or removes bans as necessary
918 * * Schedules the next timed call
919 * * Does any pending op/ban/mode checks
920 * It is safe to use either as a target for a schedule() call, or to call
921 * directly when parameters change (e.g. autolimit settings or ban expire
923 * To force a limit update, set rcp->limit to 0..
926 void cs_timerfunc(void *arg
) {
928 channel
*cp
=cip
->channel
;
933 time_t now
=time(NULL
);
936 if (!(rcp
=cip
->exts
[chanservext
]))
941 /* Always nullify the checksched even if the channel is suspended.. */
942 if (rcp
->checksched
) {
943 deleteschedule(rcp
->checksched
, cs_timerfunc
, arg
);
944 rcp
->checksched
=NULL
;
947 if (!cp
|| CIsSuspended(rcp
))
950 /* Check if we need to leave the channel first */
951 if (chanservnick
&& CIsJoined(rcp
) && cip
->channel
&&
952 cip
->channel
->users
->totalusers
==1 &&
953 getnumerichandlefromchanhash(cip
->channel
->users
, chanservnick
->numeric
)) {
955 /* Only chanserv left in this channel */
956 if (now
>= (rcp
->lastpart
+ LINGERTIME
)) {
958 localpartchannel(chanservnick
, cip
->channel
, "Empty Channel");
961 if (!nextsched
|| nextsched
> (rcp
->lastpart
+ LINGERTIME
))
962 nextsched
=rcp
->lastpart
+LINGERTIME
;
966 localsetmodeinit(&changes
, cp
, chanservnick
);
968 if (CIsAutoLimit(rcp
)) {
969 if (!rcp
->limit
|| rcp
->autoupdate
<= now
) {
970 /* Only update the limit if there are <= (autolimit/2) or
971 * >= (autolimit * 1.5) slots free */
973 if ((cp
->users
->totalusers
>= (rcp
->limit
- rcp
->autolimit
/2)) ||
974 (cp
->users
->totalusers
<= (rcp
->limit
- (3 * rcp
->autolimit
)/2))) {
975 rcp
->limit
=cp
->users
->totalusers
+rcp
->autolimit
;
976 rcp
->status
|= QCSTAT_MODECHECK
;
979 /* And set the schedule for the next update */
980 rcp
->autoupdate
= now
+ AUTOLIMIT_INTERVAL
;
983 if (!nextsched
|| rcp
->autoupdate
<= nextsched
)
984 nextsched
=rcp
->autoupdate
;
987 if (rcp
->status
& QCSTAT_OPCHECK
) {
988 rcp
->status
&= ~QCSTAT_OPCHECK
;
989 cs_docheckopvoice(cp
, &changes
);
992 if (rcp
->status
& QCSTAT_MODECHECK
) {
993 rcp
->status
&= ~QCSTAT_MODECHECK
;
994 cs_docheckchanmodes(cp
, &changes
);
997 if (rcp
->status
& QCSTAT_BANCHECK
) {
998 rcp
->status
&= ~QCSTAT_BANCHECK
;
1002 /* This ban check is AFTER docheckopvoice in case checkopvoice set a
1003 * ban which we need to automatically remove later.. */
1005 if (rcp
->banduration
) {
1006 for (cbp
=cp
->bans
;cbp
;cbp
=ncbp
) {
1008 if (cbp
->timeset
+rcp
->banduration
<=now
) {
1009 localdosetmode_ban(&changes
, bantostring(cbp
), MCB_DEL
);
1011 if (!nextsched
|| cbp
->timeset
+rcp
->banduration
<= nextsched
) {
1012 nextsched
=rcp
->banduration
+cbp
->timeset
;
1018 /* Check for expiry of registered bans */
1020 for (rbh
=&(rcp
->bans
);*rbh
;) {
1022 verifyregchanban(rbp
);
1023 if (rbp
->expiry
&& (rbp
->expiry
< now
)) {
1025 /* Remove ban if it's on the channel (localdosetmode_ban will silently
1026 * skip bans that don't exist) */
1027 localdosetmode_ban(&changes
, bantostring(rbp
->cbp
), MCB_DEL
);
1028 /* Remove from database */
1029 csdb_deleteban(rbp
);
1030 /* Remove from list */
1032 /* Free ban/string and update setby refcount, and free actual regban */
1033 freesstring(rbp
->reason
);
1034 freechanban(rbp
->cbp
);
1037 if (rbp
->expiry
&& (!nextsched
|| rbp
->expiry
<= nextsched
)) {
1038 nextsched
=rbp
->expiry
;
1046 rcp
->checksched
=scheduleoneshot(nextsched
, cs_timerfunc
, arg
);
1048 localsetmodeflush(&changes
, 1);
1051 void cs_removechannel(regchan
*rcp
, char *reason
) {
1054 regchanuser
*rcup
, *nrcup
;
1059 for (i
=0;i
<REGCHANUSERHASHSIZE
;i
++) {
1060 for (rcup
=rcp
->regusers
[i
];rcup
;rcup
=nrcup
) {
1061 nrcup
=rcup
->nextbychan
;
1062 delreguserfromchannel(rcp
, rcup
->user
);
1063 freeregchanuser(rcup
);
1067 for (rbp
=rcp
->bans
;rbp
;rbp
=nrbp
) {
1069 freesstring(rbp
->reason
);
1070 freechanban(rbp
->cbp
);
1076 if (rcp
->checksched
)
1077 deleteschedule(rcp
->checksched
, cs_timerfunc
, rcp
->index
);
1080 chanservpartchan(cip
->channel
, reason
);
1083 csdb_deletechannel(rcp
);
1085 freesstring(rcp
->welcome
);
1086 freesstring(rcp
->topic
);
1087 freesstring(rcp
->key
);
1088 freesstring(rcp
->suspendreason
);
1089 freesstring(rcp
->comment
);
1093 cip
->exts
[chanservext
]=NULL
;
1094 releasechanindex(cip
);
1097 /* Sender is who the DELCHAN is attributed to.. */
1098 int cs_removechannelifempty(nick
*sender
, regchan
*rcp
) {
1102 for (i
=0;i
<REGCHANUSERHASHSIZE
;i
++) {
1103 for (rcup
=rcp
->regusers
[i
];rcup
;rcup
=rcup
->nextbychan
) {
1104 if (rcup
->flags
& ~(QCUFLAGS_PUNISH
))
1109 cs_log(sender
,"DELCHAN %s (Empty)",rcp
->index
->name
->content
);
1110 cs_removechannel(rcp
, "Last user removed - channel deleted.");
1115 void cs_removeuser(reguser
*rup
) {
1116 regchanuser
*rcup
, *nrcup
;
1118 struct authname
*anp
;
1120 /* Remove the user from all its channels */
1121 for (rcup
=rup
->knownon
;rcup
;rcup
=nrcup
) {
1122 nrcup
=rcup
->nextbyuser
;
1125 delreguserfromchannel(rcp
, rup
);
1126 cs_removechannelifempty(NULL
, rcp
);
1130 delreguserfrommaildomain(rup
,rup
->domain
);
1131 freesstring(rup
->localpart
);
1132 freesstring(rup
->email
);
1133 freesstring(rup
->lastemail
);
1134 freesstring(rup
->lastuserhost
);
1135 freesstring(rup
->suspendreason
);
1136 freesstring(rup
->comment
);
1137 freesstring(rup
->info
);
1139 csdb_deleteuser(rup
);
1141 removereguserfromhash(rup
);
1143 if ((anp
=findauthname(rup
->ID
)) && anp
->nicks
) {
1144 rup
->status
|= QUSTAT_DEAD
;
1150 int cs_bancheck(nick
*np
, channel
*cp
) {
1151 time_t now
=time(NULL
);
1153 modechanges changes
;
1156 if (!(rcp
=cp
->index
->exts
[chanservext
]))
1159 for (rbh
=&(rcp
->bans
);*rbh
;) {
1160 if ((*rbh
)->expiry
&& ((*rbh
)->expiry
< now
)) {
1162 csdb_deleteban(*rbh
);
1166 freesstring(rbp
->reason
);
1167 freechanban(rbp
->cbp
);
1169 } else if (nickmatchban(np
,(*rbh
)->cbp
,1)) {
1170 /* This user matches this ban.. */
1171 if (!nickbanned(np
,cp
,1)) {
1172 /* Only bother putting the ban on the channel if they're not banned already */
1173 /* (might be covered by this ban or a different one.. doesn't really matter */
1174 localsetmodeinit(&changes
, cp
, chanservnick
);
1175 localdosetmode_ban(&changes
, bantostring((*rbh
)->cbp
), MCB_ADD
);
1176 localsetmodeflush(&changes
, 1);
1177 cs_timerfunc(cp
->index
);
1179 localkickuser(chanservnick
, cp
, np
, (*rbh
)->reason
? (*rbh
)->reason
->content
: "Banned.");
1182 rbh
=&((*rbh
)->next
);
1189 void cs_setregban(chanindex
*cip
, regban
*rbp
) {
1190 modechanges changes
;
1197 localsetmodeinit(&changes
, cip
->channel
, chanservnick
);
1198 localdosetmode_ban(&changes
, bantostring(rbp
->cbp
), MCB_ADD
);
1199 localsetmodeflush(&changes
, 1);
1201 for (i
=0;(cip
->channel
) && i
<cip
->channel
->users
->hashsize
;i
++) {
1202 if (cip
->channel
->users
->content
[i
]!=nouser
&&
1203 (np
=getnickbynumeric(cip
->channel
->users
->content
[i
])) &&
1204 !IsService(np
) && !IsOper(np
) && !IsXOper(np
) &&
1205 nickmatchban(np
, rbp
->cbp
, 1))
1206 localkickuser(chanservnick
, cip
->channel
, np
, rbp
->reason
? rbp
->reason
->content
: "Banned.");
1212 void cs_banuser(modechanges
*changes
, chanindex
*cip
, nick
*np
, const char *reason
) {
1213 modechanges lchanges
;
1214 char banmask
[NICKLEN
+USERLEN
+HOSTLEN
+3];
1219 if (nickbanned(np
, cip
->channel
, 1)) {
1220 localkickuser(chanservnick
, cip
->channel
, np
, reason
?reason
:"Banned.");
1224 if (IsAccount(np
)) {
1225 /* Ban their account.. */
1226 sprintf(banmask
,"*!*@%s.%s",np
->authname
,HIS_HIDDENHOST
);
1227 } else if (IsSetHost(np
)) {
1228 /* sethosted: ban ident@host */
1229 sprintf(banmask
,"*!%s@%s",np
->shident
->content
,np
->sethost
->content
);
1230 } else if (np
->host
->clonecount
>3) {
1231 /* >3 clones, ban user@host */
1232 sprintf(banmask
,"*!%s@%s",np
->ident
,np
->host
->name
->content
);
1234 sprintf(banmask
,"*!*@%s",np
->host
->name
->content
);
1238 localsetmodeinit(&lchanges
, cip
->channel
, chanservnick
);
1239 localdosetmode_ban(&lchanges
, banmask
, MCB_ADD
);
1240 localsetmodeflush(&lchanges
, 1);
1242 localdosetmode_ban(changes
, banmask
, MCB_ADD
);
1245 localkickuser(chanservnick
, cip
->channel
, np
, reason
?reason
:"Banned.");
1249 * cs_sanitisechanlev: Removes impossible combinations from chanlev flags.
1250 * chanservuser.c is probably not the right file for this, but nowhere better
1251 * presented itself...
1253 flag_t
cs_sanitisechanlev(flag_t flags
) {
1254 /* +m or +n cannot have any "punishment" flags */
1255 if (flags
& (QCUFLAG_MASTER
| QCUFLAG_OWNER
))
1256 flags
&= ~(QCUFLAGS_PUNISH
);
1258 /* +d can't be +o */
1259 if (flags
& QCUFLAG_DENY
)
1260 flags
&= ~QCUFLAG_OP
;
1262 /* +q can't be +v */
1263 if (flags
& QCUFLAG_QUIET
)
1264 flags
&= ~QCUFLAG_VOICE
;
1266 /* +p trumps +a and +g */
1267 if (flags
& QCUFLAG_PROTECT
)
1268 flags
&= ~(QCUFLAG_AUTOOP
| QCUFLAG_AUTOVOICE
);
1270 /* -o can't be +a */
1271 if (!(flags
& QCUFLAG_OP
))
1272 flags
&= ~QCUFLAG_AUTOOP
;
1274 /* +a or -v can't be +g. +a implies +o at this stage (see above) */
1275 if (!(flags
& QCUFLAG_VOICE
) || (flags
& QCUFLAG_AUTOOP
))
1276 flags
&= ~QCUFLAG_AUTOVOICE
;
1278 /* +p requires +o or +v */
1279 if (!(flags
& (QCUFLAG_VOICE
| QCUFLAG_OP
)))
1280 flags
&= ~QCUFLAG_PROTECT
;
1282 /* The personal flags require one of +mnovk, as does +t */
1283 if (!(flags
& (QCUFLAG_OWNER
| QCUFLAG_MASTER
| QCUFLAG_OP
| QCUFLAG_VOICE
| QCUFLAG_KNOWN
)))
1284 flags
&= ~(QCUFLAGS_PERSONAL
| QCUFLAG_TOPIC
);
1291 * This function does the standard "nick or #user" lookup.
1292 * If "sender" is not NULL, a suitable error message will
1293 * be sent if the lookup fails.
1294 * "sender" MUST be sent when a user is requesting a lookup
1295 * as there is some policy code here.
1298 reguser
*findreguser(nick
*sender
, const char *str
) {
1299 reguser
*rup
, *vrup
= getreguserfromnick(sender
);;
1308 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1311 if (!(rup
=findreguserbynick(str
+1)) && sender
)
1312 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1313 } else if (*str
=='&' && vrup
&& UHasStaffPriv(vrup
)) {
1316 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1319 if (!(rup
=findreguserbyID(atoi(str
+1))) && sender
)
1320 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1322 if (!(np
=getnickbynick(str
))) {
1324 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1327 if (!(rup
=getreguserfromnick(np
)) && sender
)
1328 chanservstdmessage(sender
, QM_USERNOTAUTHED
, str
);
1331 /* removed the suspended check from here, I don't see the point... */
1332 if (rup
&& (rup
->status
& QUSTAT_DEAD
)) {
1333 chanservstdmessage(sender
, QM_USERHASBADAUTH
, rup
->username
);
1341 * Unbans a mask from a channel, including permbans if user has correct privs.
1343 * Return 0 if it works, 1 if it don't.
1345 int cs_unbanfn(nick
*sender
, chanindex
*cip
, UnbanFN fn
, void *arg
, int removepermbans
, int abortonfailure
) {
1347 chanban
**cbh
, *cbp
;
1349 modechanges changes
;
1352 rcp
=cip
->exts
[chanservext
];
1355 localsetmodeinit(&changes
, cip
->channel
, chanservnick
);
1357 for (rbh
=&(rcp
->bans
); *rbh
; ) {
1359 if (fn(arg
, rbp
->cbp
)) {
1360 banstr
=bantostring(rbp
->cbp
);
1361 /* Check perms and remove */
1362 if(!removepermbans
) {
1363 chanservstdmessage(sender
, QM_WARNNOTREMOVEDPERMBAN
, banstr
, cip
->name
->content
);
1365 } else if (!cs_checkaccess(sender
, NULL
, CA_MASTERPRIV
, cip
, NULL
, 0, 1)) {
1366 chanservstdmessage(sender
, QM_NOTREMOVEDPERMBAN
, banstr
, cip
->name
->content
);
1367 if (abortonfailure
) return 1; /* Just give up... */
1370 chanservstdmessage(sender
, QM_REMOVEDPERMBAN
, banstr
, cip
->name
->content
);
1372 localdosetmode_ban(&changes
, banstr
, MCB_DEL
);
1373 /* Remove from database */
1374 csdb_deleteban(rbp
);
1375 /* Remove from list */
1377 /* Free ban/string and update setby refcount, and free actual regban */
1378 freesstring(rbp
->reason
);
1379 freechanban(rbp
->cbp
);
1388 for (cbh
=&(cip
->channel
->bans
); *cbh
; ) {
1392 banstr
=bantostring(cbp
);
1393 chanservstdmessage(sender
, QM_REMOVEDCHANBAN
, banstr
, cip
->name
->content
);
1394 localdosetmode_ban(&changes
, banstr
, MCB_DEL
);
1399 localsetmodeflush(&changes
,1);
1405 /* Add entry to channel op history when someone gets ops. */
1406 void cs_logchanop(regchan
*rcp
, char *nick
, reguser
*rup
) {
1407 strncpy(rcp
->chanopnicks
[rcp
->chanoppos
], nick
, NICKLEN
);
1408 rcp
->chanopnicks
[rcp
->chanoppos
][NICKLEN
]='\0';
1409 rcp
->chanopaccts
[rcp
->chanoppos
]=rup
->ID
;
1410 rcp
->chanoppos
=(rcp
->chanoppos
+1)%CHANOPHISTORY
;
1413 int checkreason(nick
*np
, char *reason
) {
1414 if((strlen(reason
) < MIN_REASONLEN
) || !strchr(reason
, ' ')) {
1415 chanservstdmessage(np
, QM_REASONREQUIRED
);