]>
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]);
177 if ((cmd
->level
& QCMD_ACHIEVEMENTS
) && !UIsDev(rup
) &&
178 ((time(NULL
) < ACHIEVEMENTS_START
) ||
179 ((time(NULL
) > ACHIEVEMENTS_END
) && !UIsAchievements(rup
)))) {
180 chanservstdmessage(sender
, QM_UNKNOWNCMD
, cargv
[0]);
184 if ((cmd
->level
& QCMD_TITLES
) && !UIsDev(rup
) &&
185 ((time(NULL
) < ACHIEVEMENTS_START
) ||
186 (time(NULL
) > ACHIEVEMENTS_END
))) {
187 chanservstdmessage(sender
, QM_UNKNOWNCMD
, cargv
[0]);
193 if (cmd
->maxparams
< (cargc
-1)) {
194 rejoinline(cargv
[cmd
->maxparams
],cargc
-(cmd
->maxparams
));
195 cargc
=(cmd
->maxparams
)+1;
198 cmd
->handler((void *)sender
, cargc
-1, &(cargv
[1]));
200 triggerhook(HOOK_CHANSERV_CMD
, sender
);
209 void chanservcommandinit() {
210 cscommands
=newcommandtree();
211 csctcpcommands
=newcommandtree();
214 void chanservcommandclose() {
215 destroycommandtree(cscommands
);
216 destroycommandtree(csctcpcommands
);
219 void chanservaddcommand(char *command
, int flags
, int maxparams
, CommandHandler handler
, char *description
, const char *help
) {
223 newcmd
=addcommandtotree(cscommands
, command
, flags
, maxparams
, handler
);
224 /* Allocate a summary record to put the description in */
225 summary
=(cmdsummary
*)malloc(sizeof(cmdsummary
));
226 memset((void *)summary
,0,sizeof(cmdsummary
));
228 summary
->def
=getsstring(description
, 250);
229 summary
->defhelp
=(char *)help
; /* Assume that help is a constant */
231 newcmd
->ext
=(void *)summary
;
232 loadcommandsummary(newcmd
);
235 void chanservaddctcpcommand(char *command
, CommandHandler handler
) {
236 addcommandtotree(csctcpcommands
, command
, 0, 1, handler
);
239 void chanservremovectcpcommand(char *command
, CommandHandler handler
) {
240 deletecommandfromtree(csctcpcommands
, command
, handler
);
243 void chanservremovecommand(char *command
, CommandHandler handler
) {
248 if (!(cmd
=findcommandintree(cscommands
, command
, 1))) {
249 Error("chanserv",ERR_WARNING
,"Tried to unregister unknown command %s",command
);
253 summary
=(cmdsummary
*)cmd
->ext
;
254 freesstring(summary
->def
);
255 for (i
=0;i
<MAXLANG
;i
++) {
256 if (summary
->bylang
[i
])
257 freesstring(summary
->bylang
[i
]);
262 deletecommandfromtree(cscommands
, command
, handler
);
265 void chanservpartchan(channel
*cp
, char *reason
) {
266 /* Sanity check that we exist and are on the channel.
268 * Note that we don't do any of the other usual sanity checks here; if
269 * this channel is unregged or suspended or whatever then all the more
270 * reason to get Q off it ASAP! */
271 if (!chanservnick
|| !cp
|| !cp
->users
|| !getnumerichandlefromchanhash(cp
->users
, chanservnick
->numeric
))
274 localpartchannel(chanservnick
, cp
, reason
);
277 void chanservjoinchan(channel
*cp
) {
285 /* Skip unregistered channels */
286 if (!(rcp
=cp
->index
->exts
[chanservext
]))
289 /* Check for a new timestamp. But don't do anything stupid if the incoming timestamp is 0 */
290 if (cp
->timestamp
&& ((!rcp
->ltimestamp
) || (cp
->timestamp
< rcp
->ltimestamp
))) {
291 rcp
->ltimestamp
=cp
->timestamp
;
292 csdb_updatechanneltimestamp(rcp
);
295 /* We won't be doing any joining or parting if we're not online */
299 /* In gerenal this function shouldn't be used any more as a reason to get
300 * Q to leave, but if it should be leaving then just part with no reason. */
301 if ((CIsSuspended(rcp
) || !CIsJoined(rcp
)) && getnumerichandlefromchanhash(cp
->users
, chanservnick
->numeric
)) {
302 localpartchannel(chanservnick
, cp
, NULL
);
305 /* Right, we are definately going to either join the channel or at least
306 * burst some modes onto it.
308 * We will try and burst our view of the world; if the timestamps are
309 * actually equal this will be mostly ignored and we will have to fix it
310 * up later. For modes we use the forced modes, plus the default channel
311 * modes (unless any of those are explicitly denied) */
313 /* By default, we set the forcemodes and the default modes, but never denymodes..
314 * OK, actually we should only set the default modes if our timestamp is older.*/
315 if (cp
->timestamp
> rcp
->ltimestamp
)
316 themodes
|= CHANMODE_DEFAULT
;
320 themodes
= (themodes
| rcp
->forcemodes
) & ~rcp
->denymodes
;
322 /* Now, if someone has just created a channel and we are going to set +i
323 * or +k on it, this will kick them off. This could be construed as a
324 * bit rude if it's their channel, so if there is only one person on the
325 * channel and they have a right to be there, burst with default modes
326 * only to avoid them being netrider kicked.
328 if (cp
->users
->totalusers
==1) {
329 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
330 if (cp
->users
->content
[i
] != nouser
) {
331 if ((np
=getnickbynumeric(cp
->users
->content
[i
]&CU_NUMERICMASK
)) &&
332 (rup
=getreguserfromnick(np
)) &&
333 (rcup
=findreguseronchannel(rcp
,rup
)) &&
335 /* OK, there was one user, and they are known on this channel.
336 * Don't burst with +i or +k */
337 themodes
&= ~(CHANMODE_INVITEONLY
| CHANMODE_KEY
);
343 /* We should be on the channel - join with our nick */
344 if (!CIsSuspended(rcp
) && CIsJoined(rcp
) && !getnumerichandlefromchanhash(cp
->users
, chanservnick
->numeric
)) {
345 /* If we pass the key parameter here but are not setting +k (see above)
346 * then localburstontochannel() will ignore the key */
347 localburstontochannel(cp
, chanservnick
, rcp
->ltimestamp
, themodes
,
348 rcp
->limit
, (rcp
->key
)?rcp
->key
->content
:NULL
);
351 /* We're not joining the channel - send the burst with no nick */
352 if (!CIsSuspended(rcp
) && !CIsJoined(rcp
) && (cp
->timestamp
> rcp
->ltimestamp
)) {
353 localburstontochannel(cp
, NULL
, rcp
->ltimestamp
, themodes
,
354 rcp
->limit
, (rcp
->key
)?rcp
->key
->content
:NULL
);
358 void chanservstdmessage(nick
*np
, int messageid
, ... ) {
365 char *message
, *messageargs
;
369 if (getreguserfromnick(np
) == NULL
) {
373 rup
=getreguserfromnick(np
);
379 language
=rup
->languageid
;
382 if (csmessages
[language
][messageid
]) {
383 message
=csmessages
[language
][messageid
]->content
;
384 } else if (csmessages
[0][messageid
]) {
385 message
=csmessages
[0][messageid
]->content
;
387 message
=defaultmessages
[messageid
*2];
390 messageargs
=defaultmessages
[messageid
*2+1];
392 va_start(va
,messageid
);
394 q9vsnprintf(buf
,5000,message
,messageargs
,va
);
399 for (bp
=buf
; ;bp
++) {
400 /* We send something if we hit a \n, fill the buffer or run out of source */
401 if (*bp
=='\n' || len
>490 || !*bp
) {
402 if (*bp
&& *bp
!='\n') {
408 if (chanservnick
&& *buf2
) {
410 sendnoticetouser(chanservnick
,np
,"%s",buf2
);
412 sendmessagetouser(chanservnick
,np
,"%s",buf2
);
416 /* If we ran out of buffer, get out here */
428 /* Special case: If it's a "not enough parameters" message, show the first line of help */
429 if (messageid
==QM_NOTENOUGHPARAMS
) {
430 char *command
=va_arg(va2
, char *);
431 cs_sendhelp(np
, command
, 1);
432 chanservstdmessage(np
, QM_TYPEHELPFORHELP
, command
);
438 void chanservsendmessage_real(nick
*np
, int oneline
, char *message
, ... ) {
439 char buf
[5010]; /* Very large buffer.. */
440 char buf2
[512], *bp
, *bp2
;
447 va_start(va
,message
);
448 vsnprintf(buf
,5000,message
,va
);
451 if (getreguserfromnick(np
) == NULL
) {
454 rup
=getreguserfromnick(np
);
464 for (bp
=buf
; ;bp
++) {
465 /* We send something if we hit a \n, fill the buffer or run out of source */
466 if (*bp
=='\n' || len
>490 || !*bp
) {
467 if (*bp
&& *bp
!='\n') {
473 if (chanservnick
&& *buf2
) {
475 sendnoticetouser(chanservnick
,np
,"%s",buf2
);
477 sendmessagetouser(chanservnick
,np
,"%s",buf2
);
481 /* If we ran out of buffer, get out here */
482 if (!*bp
|| (*bp
=='\n' && oneline
))
494 void chanservwallmessage(char *message
, ... ) {
495 char buf
[5010]; /* Very large buffer.. */
500 va_start(va
,message
);
501 vsnprintf(buf
,5000,message
,va
);
505 for (i
=0;i
<NICKHASHSIZE
;i
++) {
506 for (np
=nicktable
[i
];np
;np
=np
->next
) {
507 if (!IsOper(np
)) /* optimisation, if VIEWWALLMESSAGE changes change this */
510 if (!cs_privcheck(QPRIV_VIEWWALLMESSAGE
, np
))
513 chanservsendmessage(np
, "%s", buf
);
518 void chanservkillstdmessage(nick
*target
, int messageid
, ... ) {
521 char *message
, *messageargs
;
525 if (!(rup
=getreguserfromnick(target
)))
528 language
=rup
->languageid
;
530 if (csmessages
[language
][messageid
])
531 message
=csmessages
[language
][messageid
]->content
;
532 else if (csmessages
[0][messageid
])
533 message
=csmessages
[0][messageid
]->content
;
535 message
=defaultmessages
[messageid
*2];
537 messageargs
=defaultmessages
[messageid
*2+1];
538 va_start(va
, messageid
);
539 q9vsnprintf(buf
, 511, message
, messageargs
, va
);
541 killuser(chanservnick
, target
, "%s", buf
);
544 int checkpassword(reguser
*rup
, const char *pass
) {
545 if (!(*rup
->password
))
548 if (!strncmp(rup
->password
, pass
, PASSLEN
))
553 int checkresponse(reguser
*rup
, const unsigned char *entropy
, const char *response
, CRAlgorithm algorithm
) {
554 char usernamel
[NICKLEN
+1], *dp
, *up
;
556 if (!(*rup
->password
))
559 for(up
=rup
->username
,dp
=usernamel
;*up
;)
560 *dp
++ = ToLower(*up
++);
563 return algorithm(usernamel
, rup
->password
, cs_calcchallenge(entropy
), response
);
566 int checkhashpass(reguser
*rup
, const char *junk
, const char *hash
) {
567 char usernamel
[NICKLEN
+1], *dp
, *up
;
569 for(up
=rup
->username
,dp
=usernamel
;*up
;)
570 *dp
++ = ToLower(*up
++);
573 return cs_checkhashpass(usernamel
, rup
->password
, junk
, hash
);
576 int setpassword(reguser
*rup
, const char *pass
) {
577 strncpy(rup
->password
, pass
, PASSLEN
);
578 rup
->password
[PASSLEN
]='\0';
582 void cs_checknick(nick
*np
) {
585 char userhost
[USERLEN
+HOSTLEN
+3];
587 if (!(aup
=getactiveuserfromnick(np
))) {
589 np
->exts
[chanservnext
]=aup
;
592 assert(getactiveuserfromnick(np
));
594 if (IsAccount(np
) && np
->auth
) {
595 if (np
->auth
->exts
[chanservaext
]) {
596 rup
=getreguserfromnick(np
);
600 if (UIsDelayedGline(rup
)) {
601 /* delayed-gline - schedule the user's squelching */
602 deleteschedule(NULL
, &chanservdgline
, (void*)rup
); /* icky, but necessary unless we stick more stuff in reguser structure */
603 scheduleoneshot(time(NULL
)+rand()%900
, &chanservdgline
, (void*)rup
);
604 } else if (UIsGline(rup
)) {
605 /* instant-gline - lets be lazy and set a schedule expiring now :) */
606 deleteschedule(NULL
, &chanservdgline
, (void*)rup
); /* icky, but necessary unless we stick more stuff in reguser structure */
607 scheduleoneshot(time(NULL
), &chanservdgline
, (void*)rup
);
608 } else if(UHasSuspension(rup
)) {
609 chanservkillstdmessage(np
, QM_SUSPENDKILL
);
613 cs_doallautomodes(np
);
615 /* Auto create user.. */
618 rup
->ID
=np
->auth
->userid
;
619 if (rup
->ID
> lastuserID
)
621 strncpy(rup
->username
,np
->authname
,NICKLEN
); rup
->username
[NICKLEN
]='\0';
622 rup
->created
=time(NULL
);
624 rup
->lastemailchange
=0;
625 rup
->lastpasschange
=0;
626 rup
->flags
=QUFLAG_NOTICE
;
632 rup
->password
[0]='\0';
638 sprintf(userhost
,"%s@%s",np
->ident
,np
->host
->name
->content
);
639 rup
->lastuserhost
=getsstring(userhost
,USERLEN
+HOSTLEN
+1);
640 rup
->suspendreason
=NULL
;
646 addregusertohash(rup
);
648 csdb_createuser(rup
);
652 cs_checknickbans(np
);
655 void cs_checkchanmodes(channel
*cp
) {
658 localsetmodeinit (&changes
, cp
, chanservnick
);
659 cs_docheckchanmodes(cp
, &changes
);
660 localsetmodeflush (&changes
, 1);
663 void cs_docheckchanmodes(channel
*cp
, modechanges
*changes
) {
669 if ((rcp
=cp
->index
->exts
[chanservext
])==NULL
|| CIsSuspended(rcp
))
672 /* Take care of the simple modes */
673 localdosetmode_simple(changes
, rcp
->forcemodes
& ~(cp
->flags
), rcp
->denymodes
& cp
->flags
);
675 /* Check for forced key. Note that we ALWAYS call localdosetmode_key
676 * in case the wrong key is set atm */
677 if (rcp
->forcemodes
& CHANMODE_KEY
) {
679 Error("chanserv",ERR_WARNING
,"Key forced but no key specified for %s: cleared forcemode.",rcp
->index
->name
->content
);
680 rcp
->forcemodes
&= ~CHANMODE_KEY
;
681 csdb_updatechannel(rcp
);
683 localdosetmode_key(changes
, rcp
->key
->content
, MCB_ADD
);
687 /* Check for denied key */
688 if ((rcp
->denymodes
& CHANMODE_KEY
) && IsKey(cp
)) {
689 localdosetmode_key(changes
, NULL
, MCB_DEL
);
692 /* Check for forced limit. Always call in case of wrong limit */
693 if (rcp
->forcemodes
& CHANMODE_LIMIT
) {
694 localdosetmode_limit(changes
, rcp
->limit
, MCB_ADD
);
698 /* Slightly misnamed function that checks each USER on the channel against channel
699 * settings. Possible actions are opping/deopping, voicing/devoicing, and banning
700 * for chanflag +k or chanlev flag +b */
701 void cs_docheckopvoice(channel
*cp
, modechanges
*changes
) {
711 if ((rcp
=cp
->index
->exts
[chanservext
])==NULL
|| CIsSuspended(rcp
))
714 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
715 if (cp
->users
->content
[i
]==nouser
)
718 if ((np
=getnickbynumeric(cp
->users
->content
[i
]))==NULL
) {
719 Error("chanserv",ERR_ERROR
,"Found non-existent numeric %lu on channel %s",cp
->users
->content
[i
],
720 cp
->index
->name
->content
);
724 if ((rup
=getreguserfromnick(np
)))
725 rcup
=findreguseronchannel(rcp
,rup
);
729 /* Various things that might ban the user - don't apply these to +o, +k or +X users */
730 if (!IsService(np
) && !IsXOper(np
) && !IsOper(np
)) {
731 if (rcup
&& CUIsBanned(rcup
)) {
732 cs_banuser(changes
, cp
->index
, np
, NULL
);
736 /* chanflag +k checks; kick them if they "obviously" can't rejoin without a ban */
737 if (CIsKnownOnly(rcp
) && !(rcup
&& CUKnown(rcup
))) {
738 if (IsInviteOnly(cp
) || (IsRegOnly(cp
) && !IsAccount(np
))) {
739 localkickuser(chanservnick
,cp
,np
,"Authorised users only.");
741 cs_banuser(NULL
, cp
->index
, np
, "Authorised users only.");
747 if ((cp
->users
->content
[i
] & CUMODE_OP
) && !IsService(np
)) {
748 if ((CIsBitch(rcp
) && (!rcup
|| !CUHasOpPriv(rcup
))) ||
749 (rcup
&& CUIsDeny(rcup
)))
750 localdosetmode_nick(changes
, np
, MC_DEOP
);
752 if (rcup
&& (CUIsProtect(rcup
) || CIsProtect(rcp
)) && CUIsOp(rcup
) && !CUIsDeny(rcup
)) {
753 localdosetmode_nick(changes
, np
, MC_OP
);
754 cs_logchanop(rcp
, np
->nick
, rcup
->user
);
758 if (cp
->users
->content
[i
] & CUMODE_VOICE
) {
759 if (rcup
&& CUIsQuiet(rcup
))
760 localdosetmode_nick(changes
, np
, MC_DEVOICE
);
762 if (rcup
&& (CUIsProtect(rcup
) || CIsProtect(rcp
)) && CUIsVoice(rcup
) && !CUIsQuiet(rcup
) && !(cp
->users
->content
[i
] & CUMODE_OP
))
763 localdosetmode_nick(changes
, np
, MC_VOICE
);
768 void cs_doallautomodes(nick
*np
) {
774 if (!(rup
=getreguserfromnick(np
)))
777 for (rcup
=rup
->knownon
;rcup
;rcup
=rcup
->nextbyuser
) {
778 /* Skip suspended channels */
779 if (CIsSuspended(rcup
->chan
))
782 if (rcup
->chan
->index
->channel
) {
784 if ((lp
=getnumerichandlefromchanhash(rcup
->chan
->index
->channel
->users
, np
->numeric
))) {
785 /* User is on channel.. */
787 /* Update last use time. Do early in case of ban. */
788 rcup
->usetime
=getnettime();
790 if (CUIsBanned(rcup
)) {
791 cs_banuser(NULL
, rcup
->chan
->index
, np
, NULL
);
795 if (CUHasOpPriv(rcup
) && cs_ischannelactive(rcup
->chan
->index
->channel
, NULL
)) {
796 /* This meets the channel use criteria, update. */
797 rcup
->chan
->lastactive
=time(NULL
);
799 /* Don't spam the DB though for channels with lots of joins */
800 if (rcup
->chan
->lastcountersync
< (time(NULL
) - COUNTERSYNCINTERVAL
)) {
801 csdb_updatechannelcounters(rcup
->chan
);
802 rcup
->chan
->lastcountersync
=time(NULL
);
806 localsetmodeinit(&changes
, rcup
->chan
->index
->channel
, chanservnick
);
807 if (*lp
& CUMODE_OP
) {
808 if (!IsService(np
) && (CUIsDeny(rcup
) || (CIsBitch(rcup
->chan
) && !CUHasOpPriv(rcup
))))
809 localdosetmode_nick(&changes
, np
, MC_DEOP
);
811 if (!CUIsDeny(rcup
) && CUIsOp(rcup
) &&
812 (CUIsProtect(rcup
) || CIsProtect(rcup
->chan
) || CUIsAutoOp(rcup
))) {
813 localdosetmode_nick(&changes
, np
, MC_OP
);
814 cs_logchanop(rcup
->chan
, np
->nick
, rup
);
818 if (*lp
& CUMODE_VOICE
) {
820 localdosetmode_nick(&changes
, np
, MC_DEVOICE
);
822 if (!CUIsQuiet(rcup
) && CUIsVoice(rcup
) && !(*lp
& CUMODE_OP
) &&
823 (CUIsProtect(rcup
) || CIsProtect(rcup
->chan
) || CUIsAutoVoice(rcup
)))
824 localdosetmode_nick(&changes
, np
, MC_VOICE
);
826 localsetmodeflush(&changes
, 1);
828 /* Channel exists but user is not joined: invite if they are +j-b */
829 if (CUIsAutoInvite(rcup
) && CUKnown(rcup
) && !CUIsBanned(rcup
)) {
830 localinvite(chanservnick
, rcup
->chan
->index
, np
);
833 } /* if (rcup->chan->index->channel) */ else {
834 /* Channel doesn't currently exist - send invite anyway for +j */
835 if (CUIsAutoInvite(rcup
) && CUKnown(rcup
) && !CUIsBanned(rcup
)) {
836 localinvite(chanservnick
, rcup
->chan
->index
, np
);
842 void cs_checknickbans(nick
*np
) {
847 if (IsService(np
) || IsOper(np
) || IsXOper(np
))
850 /* Avoid races: memcpy the channel array */
851 i
=np
->channels
->cursi
;
852 ca
=(channel
**)malloc(i
*sizeof(channel
*));
853 memcpy(ca
, np
->channels
->content
,i
*sizeof(channel
*));
856 if ((rcp
=ca
[j
]->index
->exts
[chanservext
]) && !CIsSuspended(rcp
) &&
857 CIsEnforce(rcp
) && nickbanned(np
, ca
[j
], 1))
858 localkickuser(chanservnick
, ca
[j
], np
, "Banned.");
864 void cs_checkbans(channel
*cp
) {
876 if ((rcp
=cp
->index
->exts
[chanservext
])==NULL
|| CIsSuspended(rcp
))
881 localsetmodeinit(&changes
, cp
, chanservnick
);
883 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
884 if (cp
->users
->content
[i
]==nouser
)
887 if ((np
=getnickbynumeric(cp
->users
->content
[i
]))==NULL
) {
888 Error("chanserv",ERR_ERROR
,"Found numeric %lu on channel %s who doesn't exist.",
889 cp
->users
->content
[i
], cp
->index
->name
->content
);
893 if (IsService(np
) || IsOper(np
) || IsXOper(np
))
896 for (rbp
=rcp
->bans
;rbp
;rbp
=rbp
->next
) {
897 if (((!rbp
->expiry
) || (rbp
->expiry
<= now
)) &&
898 nickmatchban(np
, rbp
->cbp
, 1)) {
899 if (!nickbanned(np
, cp
, 1)) {
900 localdosetmode_ban(&changes
, bantostring(rbp
->cbp
), MCB_ADD
);
902 localkickuser(chanservnick
,cp
,np
,rbp
->reason
?rbp
->reason
->content
:"Banned.");
907 if (rbp
) /* If we broke out of above loop (due to kick) rbp will be set.. */
910 if (CIsEnforce(rcp
)) {
911 for (cbp
=cp
->bans
;cbp
;cbp
=cbp
->next
) {
912 if ((cbp
->timeset
>=rcp
->lastbancheck
) && nickmatchban(np
, cbp
, 1))
913 localkickuser(chanservnick
,cp
,np
,"Banned.");
915 rcp
->lastbancheck
=time(NULL
);
919 localsetmodeflush(&changes
,1);
924 * This function schedules an update check on a channel
926 void cs_schedupdate(chanindex
*cip
, int mintime
, int maxtime
) {
927 int delay
=mintime
+(rand()%(maxtime
-mintime
));
930 if (!(rcp
=cip
->exts
[chanservext
]) || CIsSuspended(rcp
))
933 if (rcp
->checksched
) {
934 deleteschedule(rcp
->checksched
, cs_timerfunc
, cip
);
937 rcp
->checksched
=scheduleoneshot(time(NULL
)+delay
, cs_timerfunc
, cip
);
942 * This function does several things:
943 * * Updates auto-limit and/or removes bans as necessary
944 * * Schedules the next timed call
945 * * Does any pending op/ban/mode checks
946 * It is safe to use either as a target for a schedule() call, or to call
947 * directly when parameters change (e.g. autolimit settings or ban expire
949 * To force a limit update, set rcp->limit to 0..
952 void cs_timerfunc(void *arg
) {
954 channel
*cp
=cip
->channel
;
959 time_t now
=time(NULL
);
962 if (!(rcp
=cip
->exts
[chanservext
]))
967 /* Always nullify the checksched even if the channel is suspended.. */
968 if (rcp
->checksched
) {
969 deleteschedule(rcp
->checksched
, cs_timerfunc
, arg
);
970 rcp
->checksched
=NULL
;
973 if (!cp
|| CIsSuspended(rcp
))
976 /* Check if we need to leave the channel first */
977 if (chanservnick
&& CIsJoined(rcp
) && cip
->channel
&&
978 cip
->channel
->users
->totalusers
==1 &&
979 getnumerichandlefromchanhash(cip
->channel
->users
, chanservnick
->numeric
)) {
981 /* Only chanserv left in this channel */
982 if (now
>= (rcp
->lastpart
+ LINGERTIME
)) {
984 localpartchannel(chanservnick
, cip
->channel
, "Empty Channel");
987 if (!nextsched
|| nextsched
> (rcp
->lastpart
+ LINGERTIME
))
988 nextsched
=rcp
->lastpart
+LINGERTIME
;
992 localsetmodeinit(&changes
, cp
, chanservnick
);
994 if (CIsAutoLimit(rcp
)) {
995 if (!rcp
->limit
|| rcp
->autoupdate
<= now
) {
996 /* Only update the limit if there are <= (autolimit/2) or
997 * >= (autolimit * 1.5) slots free */
999 if ((cp
->users
->totalusers
>= (rcp
->limit
- rcp
->autolimit
/2)) ||
1000 (cp
->users
->totalusers
<= (rcp
->limit
- (3 * rcp
->autolimit
)/2))) {
1001 rcp
->limit
=cp
->users
->totalusers
+rcp
->autolimit
;
1002 rcp
->status
|= QCSTAT_MODECHECK
;
1005 /* And set the schedule for the next update */
1006 rcp
->autoupdate
= now
+ AUTOLIMIT_INTERVAL
;
1009 if (!nextsched
|| rcp
->autoupdate
<= nextsched
)
1010 nextsched
=rcp
->autoupdate
;
1013 if (rcp
->status
& QCSTAT_OPCHECK
) {
1014 rcp
->status
&= ~QCSTAT_OPCHECK
;
1015 cs_docheckopvoice(cp
, &changes
);
1018 if (rcp
->status
& QCSTAT_MODECHECK
) {
1019 rcp
->status
&= ~QCSTAT_MODECHECK
;
1020 cs_docheckchanmodes(cp
, &changes
);
1023 if (rcp
->status
& QCSTAT_BANCHECK
) {
1024 rcp
->status
&= ~QCSTAT_BANCHECK
;
1028 /* This ban check is AFTER docheckopvoice in case checkopvoice set a
1029 * ban which we need to automatically remove later.. */
1031 if (rcp
->banduration
) {
1032 for (cbp
=cp
->bans
;cbp
;cbp
=ncbp
) {
1034 if (cbp
->timeset
+rcp
->banduration
<=now
) {
1035 localdosetmode_ban(&changes
, bantostring(cbp
), MCB_DEL
);
1037 if (!nextsched
|| cbp
->timeset
+rcp
->banduration
<= nextsched
) {
1038 nextsched
=rcp
->banduration
+cbp
->timeset
;
1044 /* Check for expiry of registered bans */
1046 for (rbh
=&(rcp
->bans
);*rbh
;) {
1048 verifyregchanban(rbp
);
1049 if (rbp
->expiry
&& (rbp
->expiry
< now
)) {
1051 /* Remove ban if it's on the channel (localdosetmode_ban will silently
1052 * skip bans that don't exist) */
1053 localdosetmode_ban(&changes
, bantostring(rbp
->cbp
), MCB_DEL
);
1054 /* Remove from database */
1055 csdb_deleteban(rbp
);
1056 /* Remove from list */
1058 /* Free ban/string and update setby refcount, and free actual regban */
1059 freesstring(rbp
->reason
);
1060 freechanban(rbp
->cbp
);
1063 if (rbp
->expiry
&& (!nextsched
|| rbp
->expiry
<= nextsched
)) {
1064 nextsched
=rbp
->expiry
;
1072 rcp
->checksched
=scheduleoneshot(nextsched
, cs_timerfunc
, arg
);
1074 localsetmodeflush(&changes
, 1);
1077 void cs_removechannel(regchan
*rcp
, char *reason
) {
1080 regchanuser
*rcup
, *nrcup
;
1085 for (i
=0;i
<REGCHANUSERHASHSIZE
;i
++) {
1086 for (rcup
=rcp
->regusers
[i
];rcup
;rcup
=nrcup
) {
1087 nrcup
=rcup
->nextbychan
;
1088 delreguserfromchannel(rcp
, rcup
->user
);
1089 freeregchanuser(rcup
);
1093 for (rbp
=rcp
->bans
;rbp
;rbp
=nrbp
) {
1095 freesstring(rbp
->reason
);
1096 freechanban(rbp
->cbp
);
1102 if (rcp
->checksched
)
1103 deleteschedule(rcp
->checksched
, cs_timerfunc
, rcp
->index
);
1106 chanservpartchan(cip
->channel
, reason
);
1109 csdb_deletechannel(rcp
);
1111 freesstring(rcp
->welcome
);
1112 freesstring(rcp
->topic
);
1113 freesstring(rcp
->key
);
1114 freesstring(rcp
->suspendreason
);
1115 freesstring(rcp
->comment
);
1119 cip
->exts
[chanservext
]=NULL
;
1120 releasechanindex(cip
);
1123 /* Sender is who the DELCHAN is attributed to.. */
1124 int cs_removechannelifempty(nick
*sender
, regchan
*rcp
) {
1128 for (i
=0;i
<REGCHANUSERHASHSIZE
;i
++) {
1129 for (rcup
=rcp
->regusers
[i
];rcup
;rcup
=rcup
->nextbychan
) {
1130 if (rcup
->flags
& ~(QCUFLAGS_PUNISH
))
1135 cs_log(sender
,"DELCHAN %s (Empty)",rcp
->index
->name
->content
);
1136 cs_removechannel(rcp
, "Last user removed - channel deleted.");
1141 void cs_removeuser(reguser
*rup
) {
1142 regchanuser
*rcup
, *nrcup
;
1144 struct authname
*anp
;
1146 /* Remove the user from all its channels */
1147 for (rcup
=rup
->knownon
;rcup
;rcup
=nrcup
) {
1148 nrcup
=rcup
->nextbyuser
;
1151 delreguserfromchannel(rcp
, rup
);
1152 cs_removechannelifempty(NULL
, rcp
);
1156 delreguserfrommaildomain(rup
,rup
->domain
);
1157 freesstring(rup
->localpart
);
1158 freesstring(rup
->email
);
1159 freesstring(rup
->lastemail
);
1160 freesstring(rup
->lastuserhost
);
1161 freesstring(rup
->suspendreason
);
1162 freesstring(rup
->comment
);
1163 freesstring(rup
->info
);
1165 csdb_deleteuser(rup
);
1167 removereguserfromhash(rup
);
1169 if ((anp
=findauthname(rup
->ID
)) && anp
->nicks
) {
1170 rup
->status
|= QUSTAT_DEAD
;
1176 int cs_bancheck(nick
*np
, channel
*cp
) {
1177 time_t now
=time(NULL
);
1179 modechanges changes
;
1182 if (!(rcp
=cp
->index
->exts
[chanservext
]))
1185 for (rbh
=&(rcp
->bans
);*rbh
;) {
1186 if ((*rbh
)->expiry
&& ((*rbh
)->expiry
< now
)) {
1188 csdb_deleteban(*rbh
);
1192 freesstring(rbp
->reason
);
1193 freechanban(rbp
->cbp
);
1195 } else if (nickmatchban(np
,(*rbh
)->cbp
,1)) {
1196 /* This user matches this ban.. */
1197 if (!nickbanned(np
,cp
,1)) {
1198 /* Only bother putting the ban on the channel if they're not banned already */
1199 /* (might be covered by this ban or a different one.. doesn't really matter */
1200 localsetmodeinit(&changes
, cp
, chanservnick
);
1201 localdosetmode_ban(&changes
, bantostring((*rbh
)->cbp
), MCB_ADD
);
1202 localsetmodeflush(&changes
, 1);
1203 cs_timerfunc(cp
->index
);
1205 localkickuser(chanservnick
, cp
, np
, (*rbh
)->reason
? (*rbh
)->reason
->content
: "Banned.");
1208 rbh
=&((*rbh
)->next
);
1215 void cs_setregban(chanindex
*cip
, regban
*rbp
) {
1216 modechanges changes
;
1223 localsetmodeinit(&changes
, cip
->channel
, chanservnick
);
1224 localdosetmode_ban(&changes
, bantostring(rbp
->cbp
), MCB_ADD
);
1225 localsetmodeflush(&changes
, 1);
1227 for (i
=0;(cip
->channel
) && i
<cip
->channel
->users
->hashsize
;i
++) {
1228 if (cip
->channel
->users
->content
[i
]!=nouser
&&
1229 (np
=getnickbynumeric(cip
->channel
->users
->content
[i
])) &&
1230 !IsService(np
) && !IsOper(np
) && !IsXOper(np
) &&
1231 nickmatchban(np
, rbp
->cbp
, 1))
1232 localkickuser(chanservnick
, cip
->channel
, np
, rbp
->reason
? rbp
->reason
->content
: "Banned.");
1238 void cs_banuser(modechanges
*changes
, chanindex
*cip
, nick
*np
, const char *reason
) {
1239 modechanges lchanges
;
1240 char banmask
[NICKLEN
+USERLEN
+HOSTLEN
+3];
1245 if (nickbanned(np
, cip
->channel
, 1)) {
1246 localkickuser(chanservnick
, cip
->channel
, np
, reason
?reason
:"Banned.");
1250 if (IsAccount(np
)) {
1251 /* Ban their account.. */
1252 sprintf(banmask
,"*!*@%s.%s",np
->authname
,HIS_HIDDENHOST
);
1253 } else if (IsSetHost(np
)) {
1254 /* sethosted: ban ident@host */
1255 sprintf(banmask
,"*!%s@%s",np
->shident
->content
,np
->sethost
->content
);
1256 } else if (np
->host
->clonecount
>3) {
1257 /* >3 clones, ban user@host */
1258 sprintf(banmask
,"*!%s@%s",np
->ident
,np
->host
->name
->content
);
1260 sprintf(banmask
,"*!*@%s",np
->host
->name
->content
);
1264 localsetmodeinit(&lchanges
, cip
->channel
, chanservnick
);
1265 localdosetmode_ban(&lchanges
, banmask
, MCB_ADD
);
1266 localsetmodeflush(&lchanges
, 1);
1268 localdosetmode_ban(changes
, banmask
, MCB_ADD
);
1271 localkickuser(chanservnick
, cip
->channel
, np
, reason
?reason
:"Banned.");
1275 * cs_sanitisechanlev: Removes impossible combinations from chanlev flags.
1276 * chanservuser.c is probably not the right file for this, but nowhere better
1277 * presented itself...
1279 flag_t
cs_sanitisechanlev(flag_t flags
) {
1280 /* +m or +n cannot have any "punishment" flags */
1281 if (flags
& (QCUFLAG_MASTER
| QCUFLAG_OWNER
))
1282 flags
&= ~(QCUFLAGS_PUNISH
);
1284 /* +d can't be +o */
1285 if (flags
& QCUFLAG_DENY
)
1286 flags
&= ~QCUFLAG_OP
;
1288 /* +q can't be +v */
1289 if (flags
& QCUFLAG_QUIET
)
1290 flags
&= ~QCUFLAG_VOICE
;
1292 /* +p trumps +a and +g */
1293 if (flags
& QCUFLAG_PROTECT
)
1294 flags
&= ~(QCUFLAG_AUTOOP
| QCUFLAG_AUTOVOICE
);
1296 /* -o can't be +a */
1297 if (!(flags
& QCUFLAG_OP
))
1298 flags
&= ~QCUFLAG_AUTOOP
;
1300 /* +a or -v can't be +g. +a implies +o at this stage (see above) */
1301 if (!(flags
& QCUFLAG_VOICE
) || (flags
& QCUFLAG_AUTOOP
))
1302 flags
&= ~QCUFLAG_AUTOVOICE
;
1304 /* +p requires +o or +v */
1305 if (!(flags
& (QCUFLAG_VOICE
| QCUFLAG_OP
)))
1306 flags
&= ~QCUFLAG_PROTECT
;
1308 /* The personal flags require one of +mnovk, as does +t */
1309 if (!(flags
& (QCUFLAG_OWNER
| QCUFLAG_MASTER
| QCUFLAG_OP
| QCUFLAG_VOICE
| QCUFLAG_KNOWN
)))
1310 flags
&= ~(QCUFLAGS_PERSONAL
| QCUFLAG_TOPIC
);
1317 * This function does the standard "nick or #user" lookup.
1318 * If "sender" is not NULL, a suitable error message will
1319 * be sent if the lookup fails.
1320 * "sender" MUST be sent when a user is requesting a lookup
1321 * as there is some policy code here.
1324 reguser
*findreguser(nick
*sender
, const char *str
) {
1325 reguser
*rup
, *vrup
= getreguserfromnick(sender
);;
1334 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1337 if (!(rup
=findreguserbynick(str
+1)) && sender
)
1338 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1339 } else if (*str
=='&' && vrup
&& UHasStaffPriv(vrup
)) {
1342 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1345 if (!(rup
=findreguserbyID(atoi(str
+1))) && sender
)
1346 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1348 if (!(np
=getnickbynick(str
))) {
1350 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1353 if (!(rup
=getreguserfromnick(np
)) && sender
)
1354 chanservstdmessage(sender
, QM_USERNOTAUTHED
, str
);
1357 /* removed the suspended check from here, I don't see the point... */
1358 if (rup
&& (rup
->status
& QUSTAT_DEAD
)) {
1359 chanservstdmessage(sender
, QM_USERHASBADAUTH
, rup
->username
);
1367 * Unbans a mask from a channel, including permbans if user has correct privs.
1369 * Return 0 if it works, 1 if it don't.
1371 int cs_unbanfn(nick
*sender
, chanindex
*cip
, int (*fn
)(void *arg
, struct chanban
*ban
), void *arg
, int removepermbans
, int abortonfailure
) {
1373 chanban
**cbh
, *cbp
;
1375 modechanges changes
;
1378 rcp
=cip
->exts
[chanservext
];
1381 localsetmodeinit(&changes
, cip
->channel
, chanservnick
);
1383 for (rbh
=&(rcp
->bans
); *rbh
; ) {
1385 if (fn(arg
, rbp
->cbp
)) {
1386 banstr
=bantostring(rbp
->cbp
);
1387 /* Check perms and remove */
1388 if(!removepermbans
) {
1389 chanservstdmessage(sender
, QM_WARNNOTREMOVEDPERMBAN
, banstr
, cip
->name
->content
);
1391 } else if (!cs_checkaccess(sender
, NULL
, CA_MASTERPRIV
, cip
, NULL
, 0, 1)) {
1392 chanservstdmessage(sender
, QM_NOTREMOVEDPERMBAN
, banstr
, cip
->name
->content
);
1393 if (abortonfailure
) return 1; /* Just give up... */
1396 chanservstdmessage(sender
, QM_REMOVEDPERMBAN
, banstr
, cip
->name
->content
);
1398 localdosetmode_ban(&changes
, banstr
, MCB_DEL
);
1399 /* Remove from database */
1400 csdb_deleteban(rbp
);
1401 /* Remove from list */
1403 /* Free ban/string and update setby refcount, and free actual regban */
1404 freesstring(rbp
->reason
);
1405 freechanban(rbp
->cbp
);
1414 for (cbh
=&(cip
->channel
->bans
); *cbh
; ) {
1418 banstr
=bantostring(cbp
);
1419 chanservstdmessage(sender
, QM_REMOVEDCHANBAN
, banstr
, cip
->name
->content
);
1420 localdosetmode_ban(&changes
, banstr
, MCB_DEL
);
1425 localsetmodeflush(&changes
,1);
1431 /* Add entry to channel op history when someone gets ops. */
1432 void cs_logchanop(regchan
*rcp
, char *nick
, reguser
*rup
) {
1433 strncpy(rcp
->chanopnicks
[rcp
->chanoppos
], nick
, NICKLEN
);
1434 rcp
->chanopnicks
[rcp
->chanoppos
][NICKLEN
]='\0';
1435 rcp
->chanopaccts
[rcp
->chanoppos
]=rup
->ID
;
1436 rcp
->chanoppos
=(rcp
->chanoppos
+1)%CHANOPHISTORY
;
1439 int checkreason(nick
*np
, char *reason
) {
1440 if((strlen(reason
) < MIN_REASONLEN
) || !strchr(reason
, ' ')) {
1441 chanservstdmessage(np
, QM_REASONREQUIRED
);