]>
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);
114 rejoinline(cargv
[1],cargc
-1);
115 cmd
->handler((void *)sender
, cargc
-1, &(cargv
[1]));
118 cmd
=findcommandintree(cscommands
, cargv
[0], 1);
121 chanservstdmessage(sender
, QM_UNKNOWNCMD
, cargv
[0]);
125 if ((cmd
->level
& QCMD_SECURE
) && (message
!= LU_SECUREMSG
)) {
126 chanservstdmessage(sender
, QM_SECUREONLY
, cargv
[0], chanservnick
->nick
, myserver
->content
);
130 if ((cmd
->level
& QCMD_AUTHED
)) {
131 if (!(rup
=getreguserfromnick(sender
))) {
132 chanservstdmessage(sender
, QM_AUTHEDONLY
, cargv
[0]);
135 if (UIsSuspended(rup
) || (rup
->status
& QUSTAT_DEAD
)) {
136 chanservstdmessage(sender
, QM_BADAUTH
, cargv
[0]);
141 if ((cmd
->level
& QCMD_NOTAUTHED
) && (getreguserfromnick(sender
))) {
142 chanservstdmessage(sender
, QM_UNAUTHEDONLY
, cargv
[0]);
146 if ((cmd
->level
& QCMD_HELPER
) &&
147 (!(rup
=getreguserfromnick(sender
)) || !UHasHelperPriv(rup
))) {
148 chanservstdmessage(sender
, QM_NOACCESS
, cargv
[0]);
152 if ((cmd
->level
& QCMD_OPER
) &&
153 (!IsOper(sender
) || !(rup
=getreguserfromnick(sender
)) || !UHasOperPriv(rup
))) {
154 chanservstdmessage(sender
, QM_NOACCESS
, cargv
[0]);
158 if ((cmd
->level
& QCMD_ADMIN
) &&
159 (!IsOper(sender
) || !(rup
=getreguserfromnick(sender
)) || !UHasAdminPriv(rup
))) {
160 chanservstdmessage(sender
, QM_NOACCESS
, cargv
[0]);
164 if ((cmd
->level
& QCMD_DEV
) &&
165 (!IsOper(sender
) || !(rup
=getreguserfromnick(sender
)) || !UIsDev(rup
))) {
166 chanservstdmessage(sender
, QM_NOACCESS
, cargv
[0]);
170 if (cmd
->maxparams
< (cargc
-1)) {
171 rejoinline(cargv
[cmd
->maxparams
],cargc
-(cmd
->maxparams
));
172 cargc
=(cmd
->maxparams
)+1;
175 cmd
->handler((void *)sender
, cargc
-1, &(cargv
[1]));
184 void chanservcommandinit() {
185 cscommands
=newcommandtree();
186 csctcpcommands
=newcommandtree();
189 void chanservcommandclose() {
190 destroycommandtree(cscommands
);
191 destroycommandtree(csctcpcommands
);
194 void chanservaddcommand(char *command
, int flags
, int maxparams
, CommandHandler handler
, char *description
, const char *help
) {
198 newcmd
=addcommandtotree(cscommands
, command
, flags
, maxparams
, handler
);
199 /* Allocate a summary record to put the description in */
200 summary
=(cmdsummary
*)malloc(sizeof(cmdsummary
));
201 memset((void *)summary
,0,sizeof(cmdsummary
));
203 summary
->def
=getsstring(description
, 250);
204 summary
->defhelp
=(char *)help
; /* Assume that help is a constant */
206 newcmd
->ext
=(void *)summary
;
207 loadcommandsummary(newcmd
);
210 void chanservaddctcpcommand(char *command
, CommandHandler handler
) {
211 addcommandtotree(csctcpcommands
, command
, 0, 1, handler
);
214 void chanservremovectcpcommand(char *command
, CommandHandler handler
) {
215 deletecommandfromtree(csctcpcommands
, command
, handler
);
218 void chanservremovecommand(char *command
, CommandHandler handler
) {
223 if (!(cmd
=findcommandintree(cscommands
, command
, 1))) {
224 Error("chanserv",ERR_WARNING
,"Tried to unregister unknown command %s",command
);
228 summary
=(cmdsummary
*)cmd
->ext
;
229 freesstring(summary
->def
);
230 for (i
=0;i
<MAXLANG
;i
++) {
231 if (summary
->bylang
[i
])
232 freesstring(summary
->bylang
[i
]);
237 deletecommandfromtree(cscommands
, command
, handler
);
240 void chanservjoinchan(channel
*cp
) {
248 /* Skip unregistered channels */
249 if (!(rcp
=cp
->index
->exts
[chanservext
]))
252 /* Check for a new timestamp. But don't do anything stupid if the incoming timestamp is 0 */
253 if (cp
->timestamp
&& ((!rcp
->ltimestamp
) || (cp
->timestamp
< rcp
->ltimestamp
))) {
254 rcp
->ltimestamp
=cp
->timestamp
;
255 csdb_updatechanneltimestamp(rcp
);
258 /* We won't be doing any joining or parting if we're not online */
262 if ((CIsSuspended(rcp
) || !CIsJoined(rcp
)) && getnumerichandlefromchanhash(cp
->users
, chanservnick
->numeric
)) {
263 if(rcp
->suspendreason
) {
264 localpartchannel(chanservnick
, cp
, rcp
->suspendreason
->content
);
266 localpartchannel(chanservnick
, cp
, NULL
);
270 /* Right, we are definately going to either join the channel or at least
271 * burst some modes onto it.
273 * We will try and burst our view of the world; if the timestamps are
274 * actually equal this will be mostly ignored and we will have to fix it
275 * up later. For modes we use the forced modes, plus the default channel
276 * modes (unless any of those are explicitly denied) */
278 /* By default, we set the forcemodes and the default modes, but never denymodes..
279 * OK, actually we should only set the default modes if our timestamp is older.*/
280 if (cp
->timestamp
> rcp
->ltimestamp
)
281 themodes
|= CHANMODE_DEFAULT
;
285 themodes
= (themodes
| rcp
->forcemodes
) & ~rcp
->denymodes
;
287 /* Now, if someone has just created a channel and we are going to set +i
288 * or +k on it, this will kick them off. This could be construed as a
289 * bit rude if it's their channel, so if there is only one person on the
290 * channel and they have a right to be there, burst with default modes
291 * only to avoid them being netrider kicked.
293 if (cp
->users
->totalusers
==1) {
294 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
295 if (cp
->users
->content
[i
] != nouser
) {
296 if ((np
=getnickbynumeric(cp
->users
->content
[i
]&CU_NUMERICMASK
)) &&
297 (rup
=getreguserfromnick(np
)) &&
298 (rcup
=findreguseronchannel(rcp
,rup
)) &&
300 /* OK, there was one user, and they are known on this channel.
301 * Don't burst with +i or +k */
302 themodes
&= ~(CHANMODE_INVITEONLY
| CHANMODE_KEY
);
308 /* We should be on the channel - join with our nick */
309 if (!CIsSuspended(rcp
) && CIsJoined(rcp
) && !getnumerichandlefromchanhash(cp
->users
, chanservnick
->numeric
)) {
310 /* If we pass the key parameter here but are not setting +k (see above)
311 * then localburstontochannel() will ignore the key */
312 localburstontochannel(cp
, chanservnick
, rcp
->ltimestamp
, themodes
,
313 rcp
->limit
, (rcp
->key
)?rcp
->key
->content
:NULL
);
316 /* We're not joining the channel - send the burst with no nick */
317 if (!CIsSuspended(rcp
) && !CIsJoined(rcp
) && (cp
->timestamp
> rcp
->ltimestamp
)) {
318 localburstontochannel(cp
, NULL
, rcp
->ltimestamp
, themodes
,
319 rcp
->limit
, (rcp
->key
)?rcp
->key
->content
:NULL
);
323 void chanservstdmessage(nick
*np
, int messageid
, ... ) {
330 char *message
, *messageargs
;
334 if (getreguserfromnick(np
) == NULL
) {
338 rup
=getreguserfromnick(np
);
344 language
=rup
->languageid
;
347 if (csmessages
[language
][messageid
]) {
348 message
=csmessages
[language
][messageid
]->content
;
349 } else if (csmessages
[0][messageid
]) {
350 message
=csmessages
[0][messageid
]->content
;
352 message
=defaultmessages
[messageid
*2];
355 messageargs
=defaultmessages
[messageid
*2+1];
357 va_start(va
,messageid
);
359 q9vsnprintf(buf
,5000,message
,messageargs
,va
);
364 for (bp
=buf
; ;bp
++) {
365 /* We send something if we hit a \n, fill the buffer or run out of source */
366 if (*bp
=='\n' || len
>490 || !*bp
) {
367 if (*bp
&& *bp
!='\n') {
373 if (chanservnick
&& *buf2
) {
375 sendnoticetouser(chanservnick
,np
,"%s",buf2
);
377 sendmessagetouser(chanservnick
,np
,"%s",buf2
);
381 /* If we ran out of buffer, get out here */
393 /* Special case: If it's a "not enough parameters" message, show the first line of help */
394 if (messageid
==QM_NOTENOUGHPARAMS
) {
395 char *command
=va_arg(va2
, char *);
396 cs_sendhelp(np
, command
, 1);
397 chanservstdmessage(np
, QM_TYPEHELPFORHELP
, command
);
403 void chanservsendmessage_real(nick
*np
, int oneline
, char *message
, ... ) {
404 char buf
[5010]; /* Very large buffer.. */
405 char buf2
[512], *bp
, *bp2
;
412 va_start(va
,message
);
413 vsnprintf(buf
,5000,message
,va
);
416 if (getreguserfromnick(np
) == NULL
) {
419 rup
=getreguserfromnick(np
);
429 for (bp
=buf
; ;bp
++) {
430 /* We send something if we hit a \n, fill the buffer or run out of source */
431 if (*bp
=='\n' || len
>490 || !*bp
) {
432 if (*bp
&& *bp
!='\n') {
438 if (chanservnick
&& *buf2
) {
440 sendnoticetouser(chanservnick
,np
,"%s",buf2
);
442 sendmessagetouser(chanservnick
,np
,"%s",buf2
);
446 /* If we ran out of buffer, get out here */
447 if (!*bp
|| (*bp
=='\n' && oneline
))
459 void chanservwallmessage(char *message
, ... ) {
460 char buf
[5010]; /* Very large buffer.. */
465 va_start(va
,message
);
466 vsnprintf(buf
,5000,message
,va
);
470 for (i
=0;i
<NICKHASHSIZE
;i
++)
471 for (np
=nicktable
[i
];np
;np
=np
->next
)
473 chanservsendmessage(np
, "%s", buf
);
476 void chanservkillstdmessage(nick
*target
, int messageid
, ... ) {
479 char *message
, *messageargs
;
483 if (!(rup
=getreguserfromnick(target
)))
486 language
=rup
->languageid
;
488 if (csmessages
[language
][messageid
])
489 message
=csmessages
[language
][messageid
]->content
;
490 else if (csmessages
[0][messageid
])
491 message
=csmessages
[0][messageid
]->content
;
493 message
=defaultmessages
[messageid
*2];
495 messageargs
=defaultmessages
[messageid
*2+1];
496 va_start(va
, messageid
);
497 q9vsnprintf(buf
, 511, message
, messageargs
, va
);
499 killuser(chanservnick
, target
, buf
);
502 int checkpassword(reguser
*rup
, const char *pass
) {
503 if (!(*rup
->password
))
506 if (!strncmp(rup
->password
, pass
, PASSLEN
))
511 int checkresponse(reguser
*rup
, const unsigned char *entropy
, const char *response
, CRAlgorithm algorithm
) {
512 char usernamel
[NICKLEN
+1], *dp
, *up
;
514 if (!(*rup
->password
))
517 for(up
=rup
->username
,dp
=usernamel
;*up
;)
518 *dp
++ = ToLower(*up
++);
521 return algorithm(usernamel
, rup
->password
, cs_calcchallenge(entropy
), response
);
524 int checkhashpass(reguser
*rup
, const char *junk
, const char *hash
) {
525 char usernamel
[NICKLEN
+1], *dp
, *up
;
527 for(up
=rup
->username
,dp
=usernamel
;*up
;)
528 *dp
++ = ToLower(*up
++);
531 return cs_checkhashpass(usernamel
, rup
->password
, junk
, hash
);
534 int setpassword(reguser
*rup
, const char *pass
) {
535 strncpy(rup
->password
, pass
, PASSLEN
);
536 rup
->password
[PASSLEN
]='\0';
540 void cs_checknick(nick
*np
) {
543 char userhost
[USERLEN
+HOSTLEN
+3];
545 if (!(aup
=getactiveuserfromnick(np
))) {
547 np
->exts
[chanservnext
]=aup
;
550 assert(getactiveuserfromnick(np
));
552 if (IsAccount(np
) && np
->auth
) {
553 if (np
->auth
->exts
[chanservaext
]) {
554 rup
=getreguserfromnick(np
);
558 if (UIsDelayedGline(rup
)) {
559 /* delayed-gline - schedule the user's squelching */
560 deleteschedule(NULL
, &chanservdgline
, (void*)rup
); /* icky, but necessary unless we stick more stuff in reguser structure */
561 scheduleoneshot(time(NULL
)+rand()%900
, &chanservdgline
, (void*)rup
);
562 } else if (UIsGline(rup
)) {
563 /* instant-gline - lets be lazy and set a schedule expiring now :) */
564 deleteschedule(NULL
, &chanservdgline
, (void*)rup
); /* icky, but necessary unless we stick more stuff in reguser structure */
565 scheduleoneshot(time(NULL
), &chanservdgline
, (void*)rup
);
566 } else if(UHasSuspension(rup
)) {
567 chanservkillstdmessage(np
, QM_SUSPENDKILL
);
571 cs_doallautomodes(np
);
573 /* Auto create user.. */
576 rup
->ID
=np
->auth
->userid
;
577 if (rup
->ID
> lastuserID
)
579 strncpy(rup
->username
,np
->authname
,NICKLEN
); rup
->username
[NICKLEN
]='\0';
580 rup
->created
=time(NULL
);
582 rup
->lastemailchange
=0;
583 rup
->flags
=QUFLAG_NOTICE
;
589 rup
->password
[0]='\0';
595 sprintf(userhost
,"%s@%s",np
->ident
,np
->host
->name
->content
);
596 rup
->lastuserhost
=getsstring(userhost
,USERLEN
+HOSTLEN
+1);
597 rup
->suspendreason
=NULL
;
603 addregusertohash(rup
);
605 csdb_createuser(rup
);
609 cs_checknickbans(np
);
612 void cs_checkchanmodes(channel
*cp
) {
615 localsetmodeinit (&changes
, cp
, chanservnick
);
616 cs_docheckchanmodes(cp
, &changes
);
617 localsetmodeflush (&changes
, 1);
620 void cs_docheckchanmodes(channel
*cp
, modechanges
*changes
) {
626 if ((rcp
=cp
->index
->exts
[chanservext
])==NULL
|| CIsSuspended(rcp
))
629 /* Take care of the simple modes */
630 localdosetmode_simple(changes
, rcp
->forcemodes
& ~(cp
->flags
), rcp
->denymodes
& cp
->flags
);
632 /* Check for forced key. Note that we ALWAYS call localdosetmode_key
633 * in case the wrong key is set atm */
634 if (rcp
->forcemodes
& CHANMODE_KEY
) {
636 Error("chanserv",ERR_WARNING
,"Key forced but no key specified for %s: cleared forcemode.",rcp
->index
->name
->content
);
637 rcp
->forcemodes
&= ~CHANMODE_KEY
;
638 csdb_updatechannel(rcp
);
640 localdosetmode_key(changes
, rcp
->key
->content
, MCB_ADD
);
644 /* Check for denied key */
645 if ((rcp
->denymodes
& CHANMODE_KEY
) && IsKey(cp
)) {
646 localdosetmode_key(changes
, NULL
, MCB_DEL
);
649 /* Check for forced limit. Always call in case of wrong limit */
650 if (rcp
->forcemodes
& CHANMODE_LIMIT
) {
651 localdosetmode_limit(changes
, rcp
->limit
, MCB_ADD
);
655 /* Slightly misnamed function that checks each USER on the channel against channel
656 * settings. Possible actions are opping/deopping, voicing/devoicing, and banning
657 * for chanflag +k or chanlev flag +b */
658 void cs_docheckopvoice(channel
*cp
, modechanges
*changes
) {
668 if ((rcp
=cp
->index
->exts
[chanservext
])==NULL
|| CIsSuspended(rcp
))
671 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
672 if (cp
->users
->content
[i
]==nouser
)
675 if ((np
=getnickbynumeric(cp
->users
->content
[i
]))==NULL
) {
676 Error("chanserv",ERR_ERROR
,"Found non-existent numeric %lu on channel %s",cp
->users
->content
[i
],
677 cp
->index
->name
->content
);
681 if ((rup
=getreguserfromnick(np
)))
682 rcup
=findreguseronchannel(rcp
,rup
);
686 /* Various things that might ban the user - don't apply these to +o, +k or +X users */
687 if (!IsService(np
) && !IsXOper(np
) && !IsOper(np
)) {
688 if (rcup
&& CUIsBanned(rcup
)) {
689 cs_banuser(changes
, cp
->index
, np
, NULL
);
693 /* chanflag +k checks; kick them if they "obviously" can't rejoin without a ban */
694 if (CIsKnownOnly(rcp
) && !(rcup
&& CUKnown(rcup
))) {
695 if (IsInviteOnly(cp
) || (IsRegOnly(cp
) && !IsAccount(np
))) {
696 localkickuser(chanservnick
,cp
,np
,"Authorised users only.");
698 cs_banuser(NULL
, cp
->index
, np
, "Authorised users only.");
704 if ((cp
->users
->content
[i
] & CUMODE_OP
) && !IsService(np
)) {
705 if ((CIsBitch(rcp
) && (!rcup
|| !CUHasOpPriv(rcup
))) ||
706 (rcup
&& CUIsDeny(rcup
)))
707 localdosetmode_nick(changes
, np
, MC_DEOP
);
709 if (rcup
&& (CUIsProtect(rcup
) || CIsProtect(rcp
)) && CUIsOp(rcup
) && !CUIsDeny(rcup
)) {
710 localdosetmode_nick(changes
, np
, MC_OP
);
711 cs_logchanop(rcp
, np
->nick
, rcup
->user
);
715 if (cp
->users
->content
[i
] & CUMODE_VOICE
) {
716 if (rcup
&& CUIsQuiet(rcup
))
717 localdosetmode_nick(changes
, np
, MC_DEVOICE
);
719 if (rcup
&& (CUIsProtect(rcup
) || CIsProtect(rcp
)) && CUIsVoice(rcup
) && !CUIsQuiet(rcup
) && !(cp
->users
->content
[i
] & CUMODE_OP
))
720 localdosetmode_nick(changes
, np
, MC_VOICE
);
725 void cs_doallautomodes(nick
*np
) {
731 if (!(rup
=getreguserfromnick(np
)))
734 for (rcup
=rup
->knownon
;rcup
;rcup
=rcup
->nextbyuser
) {
735 /* Skip suspended channels */
736 if (CIsSuspended(rcup
->chan
))
739 if (rcup
->chan
->index
->channel
) {
741 if ((lp
=getnumerichandlefromchanhash(rcup
->chan
->index
->channel
->users
, np
->numeric
))) {
742 /* User is on channel.. */
744 if (CUKnown(rcup
) && rcup
->chan
->index
->channel
->users
->totalusers
>= 3) {
745 /* This meets the channel use criteria, update. */
746 rcup
->chan
->lastactive
=time(NULL
);
748 /* Don't spam the DB though for channels with lots of joins */
749 if (rcup
->chan
->lastcountersync
< (time(NULL
) - COUNTERSYNCINTERVAL
)) {
750 csdb_updatechannelcounters(rcup
->chan
);
751 rcup
->chan
->lastcountersync
=time(NULL
);
755 /* Update last use time */
756 rcup
->usetime
=getnettime();
758 localsetmodeinit(&changes
, rcup
->chan
->index
->channel
, chanservnick
);
759 if (*lp
& CUMODE_OP
) {
760 if (!IsService(np
) && (CUIsDeny(rcup
) || (CIsBitch(rcup
->chan
) && !CUHasOpPriv(rcup
))))
761 localdosetmode_nick(&changes
, np
, MC_DEOP
);
763 if (!CUIsDeny(rcup
) && CUIsOp(rcup
) &&
764 (CUIsProtect(rcup
) || CIsProtect(rcup
->chan
) || CUIsAutoOp(rcup
))) {
765 localdosetmode_nick(&changes
, np
, MC_OP
);
766 cs_logchanop(rcup
->chan
, np
->nick
, rup
);
770 if (*lp
& CUMODE_VOICE
) {
772 localdosetmode_nick(&changes
, np
, MC_DEVOICE
);
774 if (!CUIsQuiet(rcup
) && CUIsVoice(rcup
) && !(*lp
& CUMODE_OP
) &&
775 (CUIsProtect(rcup
) || CIsProtect(rcup
->chan
) || CUIsAutoVoice(rcup
)))
776 localdosetmode_nick(&changes
, np
, MC_VOICE
);
778 localsetmodeflush(&changes
, 1);
780 /* Channel exists but user is not joined: invite if they are +j-b */
781 if (CUIsAutoInvite(rcup
) && CUKnown(rcup
) && !CUIsBanned(rcup
)) {
782 localinvite(chanservnick
, rcup
->chan
->index
->channel
, np
);
789 void cs_checknickbans(nick
*np
) {
794 if (IsService(np
) || IsOper(np
) || IsXOper(np
))
797 /* Avoid races: memcpy the channel array */
798 i
=np
->channels
->cursi
;
799 ca
=(channel
**)malloc(i
*sizeof(channel
*));
800 memcpy(ca
, np
->channels
->content
,i
*sizeof(channel
*));
803 if ((rcp
=ca
[j
]->index
->exts
[chanservext
]) && !CIsSuspended(rcp
) &&
804 CIsEnforce(rcp
) && nickbanned_visible(np
, ca
[j
]))
805 localkickuser(chanservnick
, ca
[j
], np
, "Banned.");
811 void cs_checkbans(channel
*cp
) {
823 if ((rcp
=cp
->index
->exts
[chanservext
])==NULL
|| CIsSuspended(rcp
))
828 localsetmodeinit(&changes
, cp
, chanservnick
);
830 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
831 if (cp
->users
->content
[i
]==nouser
)
834 if ((np
=getnickbynumeric(cp
->users
->content
[i
]))==NULL
) {
835 Error("chanserv",ERR_ERROR
,"Found numeric %lu on channel %s who doesn't exist.",
836 cp
->users
->content
[i
], cp
->index
->name
->content
);
840 if (IsService(np
) || IsOper(np
) || IsXOper(np
))
843 for (rbp
=rcp
->bans
;rbp
;rbp
=rbp
->next
) {
844 if (((!rbp
->expiry
) || (rbp
->expiry
<= now
)) &&
845 nickmatchban_visible(np
, rbp
->cbp
)) {
846 if (!nickbanned_visible(np
, cp
)) {
847 localdosetmode_ban(&changes
, bantostring(rbp
->cbp
), MCB_ADD
);
849 localkickuser(chanservnick
,cp
,np
,rbp
->reason
?rbp
->reason
->content
:"Banned.");
854 if (rbp
) /* If we broke out of above loop (due to kick) rbp will be set.. */
857 if (CIsEnforce(rcp
)) {
858 for (cbp
=cp
->bans
;cbp
;cbp
=cbp
->next
) {
859 if ((cbp
->timeset
>=rcp
->lastbancheck
) && nickmatchban_visible(np
, cbp
))
860 localkickuser(chanservnick
,cp
,np
,"Banned.");
862 rcp
->lastbancheck
=time(NULL
);
866 localsetmodeflush(&changes
,1);
871 * This function schedules an update check on a channel
873 void cs_schedupdate(chanindex
*cip
, int mintime
, int maxtime
) {
874 int delay
=mintime
+(rand()%(maxtime
-mintime
));
877 if (!(rcp
=cip
->exts
[chanservext
]) || CIsSuspended(rcp
))
880 if (rcp
->checksched
) {
881 deleteschedule(rcp
->checksched
, cs_timerfunc
, cip
);
884 rcp
->checksched
=scheduleoneshot(time(NULL
)+delay
, cs_timerfunc
, cip
);
889 * This function does several things:
890 * * Updates auto-limit and/or removes bans as necessary
891 * * Schedules the next timed call
892 * * Does any pending op/ban/mode checks
893 * It is safe to use either as a target for a schedule() call, or to call
894 * directly when parameters change (e.g. autolimit settings or ban expire
896 * To force a limit update, set rcp->limit to 0..
899 void cs_timerfunc(void *arg
) {
901 channel
*cp
=cip
->channel
;
906 time_t now
=time(NULL
);
909 if (!(rcp
=cip
->exts
[chanservext
]))
914 /* Always nullify the checksched even if the channel is suspended.. */
915 if (rcp
->checksched
) {
916 deleteschedule(rcp
->checksched
, cs_timerfunc
, arg
);
917 rcp
->checksched
=NULL
;
920 if (!cp
|| CIsSuspended(rcp
))
923 /* Check if we need to leave the channel first */
924 if (chanservnick
&& CIsJoined(rcp
) && cip
->channel
&&
925 cip
->channel
->users
->totalusers
==1 &&
926 getnumerichandlefromchanhash(cip
->channel
->users
, chanservnick
->numeric
)) {
928 /* Only chanserv left in this channel */
929 if (now
>= (rcp
->lastpart
+ LINGERTIME
)) {
931 localpartchannel(chanservnick
, cip
->channel
, "Empty Channel");
934 if (!nextsched
|| nextsched
> (rcp
->lastpart
+ LINGERTIME
))
935 nextsched
=rcp
->lastpart
+LINGERTIME
;
939 localsetmodeinit(&changes
, cp
, chanservnick
);
941 if (CIsAutoLimit(rcp
)) {
942 if (!rcp
->limit
|| rcp
->autoupdate
<= now
) {
943 /* Only update the limit if there are <= (autolimit/2) or
944 * >= (autolimit * 1.5) slots free */
946 if ((cp
->users
->totalusers
>= (rcp
->limit
- rcp
->autolimit
/2)) ||
947 (cp
->users
->totalusers
<= (rcp
->limit
- (3 * rcp
->autolimit
)/2))) {
948 rcp
->limit
=cp
->users
->totalusers
+rcp
->autolimit
;
949 rcp
->status
|= QCSTAT_MODECHECK
;
952 /* And set the schedule for the next update */
953 rcp
->autoupdate
= now
+ AUTOLIMIT_INTERVAL
;
956 if (!nextsched
|| rcp
->autoupdate
<= nextsched
)
957 nextsched
=rcp
->autoupdate
;
960 if (rcp
->status
& QCSTAT_OPCHECK
) {
961 rcp
->status
&= ~QCSTAT_OPCHECK
;
962 cs_docheckopvoice(cp
, &changes
);
965 if (rcp
->status
& QCSTAT_MODECHECK
) {
966 rcp
->status
&= ~QCSTAT_MODECHECK
;
967 cs_docheckchanmodes(cp
, &changes
);
970 if (rcp
->status
& QCSTAT_BANCHECK
) {
971 rcp
->status
&= ~QCSTAT_BANCHECK
;
975 /* This ban check is AFTER docheckopvoice in case checkopvoice set a
976 * ban which we need to automatically remove later.. */
978 if (rcp
->banduration
) {
979 for (cbp
=cp
->bans
;cbp
;cbp
=ncbp
) {
981 if (cbp
->timeset
+rcp
->banduration
<=now
) {
982 localdosetmode_ban(&changes
, bantostring(cbp
), MCB_DEL
);
984 if (!nextsched
|| cbp
->timeset
+rcp
->banduration
<= nextsched
) {
985 nextsched
=rcp
->banduration
+cbp
->timeset
;
991 /* Check for expiry of registered bans */
993 for (rbh
=&(rcp
->bans
);*rbh
;) {
995 verifyregchanban(rbp
);
996 if (rbp
->expiry
&& (rbp
->expiry
< now
)) {
998 /* Remove ban if it's on the channel (localdosetmode_ban will silently
999 * skip bans that don't exist) */
1000 localdosetmode_ban(&changes
, bantostring(rbp
->cbp
), MCB_DEL
);
1001 /* Remove from database */
1002 csdb_deleteban(rbp
);
1003 /* Remove from list */
1005 /* Free ban/string and update setby refcount, and free actual regban */
1006 freesstring(rbp
->reason
);
1007 freechanban(rbp
->cbp
);
1010 if (rbp
->expiry
&& (!nextsched
|| rbp
->expiry
<= nextsched
)) {
1011 nextsched
=rbp
->expiry
;
1019 rcp
->checksched
=scheduleoneshot(nextsched
, cs_timerfunc
, arg
);
1021 localsetmodeflush(&changes
, 1);
1024 void cs_removechannel(regchan
*rcp
) {
1027 regchanuser
*rcup
, *nrcup
;
1032 for (i
=0;i
<REGCHANUSERHASHSIZE
;i
++) {
1033 for (rcup
=rcp
->regusers
[i
];rcup
;rcup
=nrcup
) {
1034 nrcup
=rcup
->nextbychan
;
1035 delreguserfromchannel(rcp
, rcup
->user
);
1036 freeregchanuser(rcup
);
1040 for (rbp
=rcp
->bans
;rbp
;rbp
=nrbp
) {
1042 freesstring(rbp
->reason
);
1043 freechanban(rbp
->cbp
);
1049 if (rcp
->checksched
)
1050 deleteschedule(rcp
->checksched
, cs_timerfunc
, rcp
->index
);
1053 rcp
->flags
=QCFLAG_SUSPENDED
;
1054 chanservjoinchan(cip
->channel
); /* Force off the channel */
1057 csdb_deletechannel(rcp
);
1059 freesstring(rcp
->welcome
);
1060 freesstring(rcp
->topic
);
1061 freesstring(rcp
->key
);
1062 freesstring(rcp
->suspendreason
);
1063 freesstring(rcp
->comment
);
1067 cip
->exts
[chanservext
]=NULL
;
1068 releasechanindex(cip
);
1071 /* Sender is who the DELCHAN is attributed to.. */
1072 int cs_removechannelifempty(nick
*sender
, regchan
*rcp
) {
1076 for (i
=0;i
<REGCHANUSERHASHSIZE
;i
++) {
1077 for (rcup
=rcp
->regusers
[i
];rcup
;rcup
=rcup
->nextbychan
) {
1078 if (rcup
->flags
& ~(QCUFLAGS_PUNISH
))
1083 cs_log(sender
,"DELCHAN %s (Empty)",rcp
->index
->name
->content
);
1084 cs_removechannel(rcp
);
1089 void cs_removeuser(reguser
*rup
) {
1090 regchanuser
*rcup
, *nrcup
;
1092 struct authname
*anp
;
1094 /* Remove the user from all its channels */
1095 for (rcup
=rup
->knownon
;rcup
;rcup
=nrcup
) {
1096 nrcup
=rcup
->nextbyuser
;
1099 delreguserfromchannel(rcp
, rup
);
1100 cs_removechannelifempty(NULL
, rcp
);
1104 delreguserfrommaildomain(rup
,rup
->domain
);
1105 freesstring(rup
->localpart
);
1106 freesstring(rup
->email
);
1107 freesstring(rup
->lastemail
);
1108 freesstring(rup
->lastuserhost
);
1109 freesstring(rup
->suspendreason
);
1110 freesstring(rup
->comment
);
1111 freesstring(rup
->info
);
1113 csdb_deleteuser(rup
);
1115 removereguserfromhash(rup
);
1117 if ((anp
=findauthname(rup
->ID
)) && anp
->nicks
) {
1118 rup
->status
|= QUSTAT_DEAD
;
1124 int cs_bancheck(nick
*np
, channel
*cp
) {
1125 time_t now
=time(NULL
);
1127 modechanges changes
;
1130 if (!(rcp
=cp
->index
->exts
[chanservext
]))
1133 for (rbh
=&(rcp
->bans
);*rbh
;) {
1134 if ((*rbh
)->expiry
&& ((*rbh
)->expiry
< now
)) {
1136 csdb_deleteban(*rbh
);
1140 freesstring(rbp
->reason
);
1141 freechanban(rbp
->cbp
);
1143 } else if (nickmatchban_visible(np
,(*rbh
)->cbp
)) {
1144 /* This user matches this ban.. */
1145 if (!nickbanned_visible(np
,cp
)) {
1146 /* Only bother putting the ban on the channel if they're not banned already */
1147 /* (might be covered by this ban or a different one.. doesn't really matter */
1148 localsetmodeinit(&changes
, cp
, chanservnick
);
1149 localdosetmode_ban(&changes
, bantostring((*rbh
)->cbp
), MCB_ADD
);
1150 localsetmodeflush(&changes
, 1);
1151 cs_timerfunc(cp
->index
);
1153 localkickuser(chanservnick
, cp
, np
, (*rbh
)->reason
? (*rbh
)->reason
->content
: "Banned.");
1156 rbh
=&((*rbh
)->next
);
1163 void cs_setregban(chanindex
*cip
, regban
*rbp
) {
1164 modechanges changes
;
1171 localsetmodeinit(&changes
, cip
->channel
, chanservnick
);
1172 localdosetmode_ban(&changes
, bantostring(rbp
->cbp
), MCB_ADD
);
1173 localsetmodeflush(&changes
, 1);
1175 for (i
=0;(cip
->channel
) && i
<cip
->channel
->users
->hashsize
;i
++) {
1176 if (cip
->channel
->users
->content
[i
]!=nouser
&&
1177 (np
=getnickbynumeric(cip
->channel
->users
->content
[i
])) &&
1178 !IsService(np
) && !IsOper(np
) && !IsXOper(np
) &&
1179 nickmatchban_visible(np
, rbp
->cbp
))
1180 localkickuser(chanservnick
, cip
->channel
, np
, rbp
->reason
? rbp
->reason
->content
: "Banned.");
1186 void cs_banuser(modechanges
*changes
, chanindex
*cip
, nick
*np
, const char *reason
) {
1187 modechanges lchanges
;
1188 char banmask
[NICKLEN
+USERLEN
+HOSTLEN
+3];
1193 if (nickbanned_visible(np
, cip
->channel
)) {
1194 localkickuser(chanservnick
, cip
->channel
, np
, reason
?reason
:"Banned.");
1198 if (IsAccount(np
)) {
1199 /* Ban their account.. */
1200 sprintf(banmask
,"*!*@%s.%s",np
->authname
,HIS_HIDDENHOST
);
1201 } else if (IsSetHost(np
)) {
1202 /* sethosted: ban ident@host */
1203 sprintf(banmask
,"*!%s@%s",np
->shident
->content
,np
->sethost
->content
);
1204 } else if (np
->host
->clonecount
>3) {
1205 /* >3 clones, ban user@host */
1206 sprintf(banmask
,"*!%s@%s",np
->ident
,np
->host
->name
->content
);
1208 sprintf(banmask
,"*!*@%s",np
->host
->name
->content
);
1212 localsetmodeinit(&lchanges
, cip
->channel
, chanservnick
);
1213 localdosetmode_ban(&lchanges
, banmask
, MCB_ADD
);
1214 localsetmodeflush(&lchanges
, 1);
1216 localdosetmode_ban(changes
, banmask
, MCB_ADD
);
1219 localkickuser(chanservnick
, cip
->channel
, np
, reason
?reason
:"Banned.");
1223 * cs_sanitisechanlev: Removes impossible combinations from chanlev flags.
1224 * chanservuser.c is probably not the right file for this, but nowhere better
1225 * presented itself...
1227 flag_t
cs_sanitisechanlev(flag_t flags
) {
1228 /* +m or +n cannot have any "punishment" flags */
1229 if (flags
& (QCUFLAG_MASTER
| QCUFLAG_OWNER
))
1230 flags
&= ~(QCUFLAGS_PUNISH
);
1232 /* +d can't be +o */
1233 if (flags
& QCUFLAG_DENY
)
1234 flags
&= ~QCUFLAG_OP
;
1236 /* +q can't be +v */
1237 if (flags
& QCUFLAG_QUIET
)
1238 flags
&= ~QCUFLAG_VOICE
;
1240 /* +p trumps +a and +g */
1241 if (flags
& QCUFLAG_PROTECT
)
1242 flags
&= ~(QCUFLAG_AUTOOP
| QCUFLAG_AUTOVOICE
);
1244 /* -o can't be +a */
1245 if (!(flags
& QCUFLAG_OP
))
1246 flags
&= ~QCUFLAG_AUTOOP
;
1248 /* +a or -v can't be +g. +a implies +o at this stage (see above) */
1249 if (!(flags
& QCUFLAG_VOICE
) || (flags
& QCUFLAG_AUTOOP
))
1250 flags
&= ~QCUFLAG_AUTOVOICE
;
1252 /* +p requires +o or +v */
1253 if (!(flags
& (QCUFLAG_VOICE
| QCUFLAG_OP
)))
1254 flags
&= ~QCUFLAG_PROTECT
;
1256 /* The personal flags require one of +mnovk, as does +t */
1257 if (!(flags
& (QCUFLAG_OWNER
| QCUFLAG_MASTER
| QCUFLAG_OP
| QCUFLAG_VOICE
| QCUFLAG_KNOWN
)))
1258 flags
&= ~(QCUFLAGS_PERSONAL
| QCUFLAG_TOPIC
);
1265 * This function does the standard "nick or #user" lookup.
1266 * If "sender" is not NULL, a suitable error message will
1267 * be sent if the lookup fails.
1268 * "sender" MUST be sent when a user is requesting a lookup
1269 * as there is some policy code here.
1272 reguser
*findreguser(nick
*sender
, const char *str
) {
1273 reguser
*rup
, *vrup
= getreguserfromnick(sender
);;
1282 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1285 if (!(rup
=findreguserbynick(str
+1)) && sender
)
1286 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1287 } else if (*str
=='&' && vrup
&& UHasHelperPriv(vrup
)) {
1290 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1293 if (!(rup
=findreguserbyID(atoi(str
+1))) && sender
)
1294 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1296 if (!(np
=getnickbynick(str
))) {
1298 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1301 if (!(rup
=getreguserfromnick(np
)) && sender
)
1302 chanservstdmessage(sender
, QM_USERNOTAUTHED
, str
);
1305 /* removed the suspended check from here, I don't see the point... */
1306 if (rup
&& (rup
->status
& QUSTAT_DEAD
)) {
1307 chanservstdmessage(sender
, QM_USERHASBADAUTH
, rup
->username
);
1315 * Unbans a mask from a channel, including permbans if user has correct privs.
1317 * Return 0 if it works, 1 if it don't.
1319 int cs_unbanfn(nick
*sender
, chanindex
*cip
, UnbanFN fn
, void *arg
, int removepermbans
, int abortonfailure
) {
1321 chanban
**cbh
, *cbp
;
1323 modechanges changes
;
1326 rcp
=cip
->exts
[chanservext
];
1329 localsetmodeinit(&changes
, cip
->channel
, chanservnick
);
1331 for (rbh
=&(rcp
->bans
); *rbh
; ) {
1333 if (fn(arg
, rbp
->cbp
)) {
1334 banstr
=bantostring(rbp
->cbp
);
1335 /* Check perms and remove */
1336 if(!removepermbans
) {
1337 chanservstdmessage(sender
, QM_WARNNOTREMOVEDPERMBAN
, banstr
, cip
->name
->content
);
1339 } else if (!cs_checkaccess(sender
, NULL
, CA_MASTERPRIV
, cip
, NULL
, 0, 1)) {
1340 chanservstdmessage(sender
, QM_NOTREMOVEDPERMBAN
, banstr
, cip
->name
->content
);
1341 if (abortonfailure
) return 1; /* Just give up... */
1344 chanservstdmessage(sender
, QM_REMOVEDPERMBAN
, banstr
, cip
->name
->content
);
1346 localdosetmode_ban(&changes
, banstr
, MCB_DEL
);
1347 /* Remove from database */
1348 csdb_deleteban(rbp
);
1349 /* Remove from list */
1351 /* Free ban/string and update setby refcount, and free actual regban */
1352 freesstring(rbp
->reason
);
1353 freechanban(rbp
->cbp
);
1362 for (cbh
=&(cip
->channel
->bans
); *cbh
; ) {
1366 banstr
=bantostring(cbp
);
1367 chanservstdmessage(sender
, QM_REMOVEDCHANBAN
, banstr
, cip
->name
->content
);
1368 localdosetmode_ban(&changes
, banstr
, MCB_DEL
);
1373 localsetmodeflush(&changes
,1);
1379 /* Add entry to channel op history when someone gets ops. */
1380 void cs_logchanop(regchan
*rcp
, char *nick
, reguser
*rup
) {
1381 strncpy(rcp
->chanopnicks
[rcp
->chanoppos
], nick
, NICKLEN
);
1382 rcp
->chanopnicks
[rcp
->chanoppos
][NICKLEN
]='\0';
1383 rcp
->chanopaccts
[rcp
->chanoppos
]=rup
->ID
;
1384 rcp
->chanoppos
=(rcp
->chanoppos
+1)%CHANOPHISTORY
;