]>
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_STAFF
) &&
147 (!(rup
=getreguserfromnick(sender
)) || !UHasStaffPriv(rup
))) {
148 chanservstdmessage(sender
, QM_NOACCESS
, cargv
[0]);
152 if ((cmd
->level
& QCMD_HELPER
) &&
153 (!(rup
=getreguserfromnick(sender
)) || !UHasHelperPriv(rup
))) {
154 chanservstdmessage(sender
, QM_NOACCESS
, cargv
[0]);
158 if ((cmd
->level
& QCMD_OPER
) &&
159 (!IsOper(sender
) || !(rup
=getreguserfromnick(sender
)) || !UHasOperPriv(rup
))) {
160 chanservstdmessage(sender
, QM_NOACCESS
, cargv
[0]);
164 if ((cmd
->level
& QCMD_ADMIN
) &&
165 (!IsOper(sender
) || !(rup
=getreguserfromnick(sender
)) || !UHasAdminPriv(rup
))) {
166 chanservstdmessage(sender
, QM_NOACCESS
, cargv
[0]);
170 if ((cmd
->level
& QCMD_DEV
) &&
171 (!IsOper(sender
) || !(rup
=getreguserfromnick(sender
)) || !UIsDev(rup
))) {
172 chanservstdmessage(sender
, QM_NOACCESS
, cargv
[0]);
176 if (cmd
->maxparams
< (cargc
-1)) {
177 rejoinline(cargv
[cmd
->maxparams
],cargc
-(cmd
->maxparams
));
178 cargc
=(cmd
->maxparams
)+1;
181 cmd
->handler((void *)sender
, cargc
-1, &(cargv
[1]));
190 void chanservcommandinit() {
191 cscommands
=newcommandtree();
192 csctcpcommands
=newcommandtree();
195 void chanservcommandclose() {
196 destroycommandtree(cscommands
);
197 destroycommandtree(csctcpcommands
);
200 void chanservaddcommand(char *command
, int flags
, int maxparams
, CommandHandler handler
, char *description
, const char *help
) {
204 newcmd
=addcommandtotree(cscommands
, command
, flags
, maxparams
, handler
);
205 /* Allocate a summary record to put the description in */
206 summary
=(cmdsummary
*)malloc(sizeof(cmdsummary
));
207 memset((void *)summary
,0,sizeof(cmdsummary
));
209 summary
->def
=getsstring(description
, 250);
210 summary
->defhelp
=(char *)help
; /* Assume that help is a constant */
212 newcmd
->ext
=(void *)summary
;
213 loadcommandsummary(newcmd
);
216 void chanservaddctcpcommand(char *command
, CommandHandler handler
) {
217 addcommandtotree(csctcpcommands
, command
, 0, 1, handler
);
220 void chanservremovectcpcommand(char *command
, CommandHandler handler
) {
221 deletecommandfromtree(csctcpcommands
, command
, handler
);
224 void chanservremovecommand(char *command
, CommandHandler handler
) {
229 if (!(cmd
=findcommandintree(cscommands
, command
, 1))) {
230 Error("chanserv",ERR_WARNING
,"Tried to unregister unknown command %s",command
);
234 summary
=(cmdsummary
*)cmd
->ext
;
235 freesstring(summary
->def
);
236 for (i
=0;i
<MAXLANG
;i
++) {
237 if (summary
->bylang
[i
])
238 freesstring(summary
->bylang
[i
]);
243 deletecommandfromtree(cscommands
, command
, handler
);
246 void chanservjoinchan(channel
*cp
) {
254 /* Skip unregistered channels */
255 if (!(rcp
=cp
->index
->exts
[chanservext
]))
258 /* Check for a new timestamp. But don't do anything stupid if the incoming timestamp is 0 */
259 if (cp
->timestamp
&& ((!rcp
->ltimestamp
) || (cp
->timestamp
< rcp
->ltimestamp
))) {
260 rcp
->ltimestamp
=cp
->timestamp
;
261 csdb_updatechanneltimestamp(rcp
);
264 /* We won't be doing any joining or parting if we're not online */
268 if ((CIsSuspended(rcp
) || !CIsJoined(rcp
)) && getnumerichandlefromchanhash(cp
->users
, chanservnick
->numeric
)) {
269 if(rcp
->suspendreason
) {
270 localpartchannel(chanservnick
, cp
, rcp
->suspendreason
->content
);
272 localpartchannel(chanservnick
, cp
, NULL
);
276 /* Right, we are definately going to either join the channel or at least
277 * burst some modes onto it.
279 * We will try and burst our view of the world; if the timestamps are
280 * actually equal this will be mostly ignored and we will have to fix it
281 * up later. For modes we use the forced modes, plus the default channel
282 * modes (unless any of those are explicitly denied) */
284 /* By default, we set the forcemodes and the default modes, but never denymodes..
285 * OK, actually we should only set the default modes if our timestamp is older.*/
286 if (cp
->timestamp
> rcp
->ltimestamp
)
287 themodes
|= CHANMODE_DEFAULT
;
291 themodes
= (themodes
| rcp
->forcemodes
) & ~rcp
->denymodes
;
293 /* Now, if someone has just created a channel and we are going to set +i
294 * or +k on it, this will kick them off. This could be construed as a
295 * bit rude if it's their channel, so if there is only one person on the
296 * channel and they have a right to be there, burst with default modes
297 * only to avoid them being netrider kicked.
299 if (cp
->users
->totalusers
==1) {
300 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
301 if (cp
->users
->content
[i
] != nouser
) {
302 if ((np
=getnickbynumeric(cp
->users
->content
[i
]&CU_NUMERICMASK
)) &&
303 (rup
=getreguserfromnick(np
)) &&
304 (rcup
=findreguseronchannel(rcp
,rup
)) &&
306 /* OK, there was one user, and they are known on this channel.
307 * Don't burst with +i or +k */
308 themodes
&= ~(CHANMODE_INVITEONLY
| CHANMODE_KEY
);
314 /* We should be on the channel - join with our nick */
315 if (!CIsSuspended(rcp
) && CIsJoined(rcp
) && !getnumerichandlefromchanhash(cp
->users
, chanservnick
->numeric
)) {
316 /* If we pass the key parameter here but are not setting +k (see above)
317 * then localburstontochannel() will ignore the key */
318 localburstontochannel(cp
, chanservnick
, rcp
->ltimestamp
, themodes
,
319 rcp
->limit
, (rcp
->key
)?rcp
->key
->content
:NULL
);
322 /* We're not joining the channel - send the burst with no nick */
323 if (!CIsSuspended(rcp
) && !CIsJoined(rcp
) && (cp
->timestamp
> rcp
->ltimestamp
)) {
324 localburstontochannel(cp
, NULL
, rcp
->ltimestamp
, themodes
,
325 rcp
->limit
, (rcp
->key
)?rcp
->key
->content
:NULL
);
329 void chanservstdmessage(nick
*np
, int messageid
, ... ) {
336 char *message
, *messageargs
;
340 if (getreguserfromnick(np
) == NULL
) {
344 rup
=getreguserfromnick(np
);
350 language
=rup
->languageid
;
353 if (csmessages
[language
][messageid
]) {
354 message
=csmessages
[language
][messageid
]->content
;
355 } else if (csmessages
[0][messageid
]) {
356 message
=csmessages
[0][messageid
]->content
;
358 message
=defaultmessages
[messageid
*2];
361 messageargs
=defaultmessages
[messageid
*2+1];
363 va_start(va
,messageid
);
365 q9vsnprintf(buf
,5000,message
,messageargs
,va
);
370 for (bp
=buf
; ;bp
++) {
371 /* We send something if we hit a \n, fill the buffer or run out of source */
372 if (*bp
=='\n' || len
>490 || !*bp
) {
373 if (*bp
&& *bp
!='\n') {
379 if (chanservnick
&& *buf2
) {
381 sendnoticetouser(chanservnick
,np
,"%s",buf2
);
383 sendmessagetouser(chanservnick
,np
,"%s",buf2
);
387 /* If we ran out of buffer, get out here */
399 /* Special case: If it's a "not enough parameters" message, show the first line of help */
400 if (messageid
==QM_NOTENOUGHPARAMS
) {
401 char *command
=va_arg(va2
, char *);
402 cs_sendhelp(np
, command
, 1);
403 chanservstdmessage(np
, QM_TYPEHELPFORHELP
, command
);
409 void chanservsendmessage_real(nick
*np
, int oneline
, char *message
, ... ) {
410 char buf
[5010]; /* Very large buffer.. */
411 char buf2
[512], *bp
, *bp2
;
418 va_start(va
,message
);
419 vsnprintf(buf
,5000,message
,va
);
422 if (getreguserfromnick(np
) == NULL
) {
425 rup
=getreguserfromnick(np
);
435 for (bp
=buf
; ;bp
++) {
436 /* We send something if we hit a \n, fill the buffer or run out of source */
437 if (*bp
=='\n' || len
>490 || !*bp
) {
438 if (*bp
&& *bp
!='\n') {
444 if (chanservnick
&& *buf2
) {
446 sendnoticetouser(chanservnick
,np
,"%s",buf2
);
448 sendmessagetouser(chanservnick
,np
,"%s",buf2
);
452 /* If we ran out of buffer, get out here */
453 if (!*bp
|| (*bp
=='\n' && oneline
))
465 void chanservwallmessage(char *message
, ... ) {
466 char buf
[5010]; /* Very large buffer.. */
471 va_start(va
,message
);
472 vsnprintf(buf
,5000,message
,va
);
476 for (i
=0;i
<NICKHASHSIZE
;i
++)
477 for (np
=nicktable
[i
];np
;np
=np
->next
)
479 chanservsendmessage(np
, "%s", buf
);
482 void chanservkillstdmessage(nick
*target
, int messageid
, ... ) {
485 char *message
, *messageargs
;
489 if (!(rup
=getreguserfromnick(target
)))
492 language
=rup
->languageid
;
494 if (csmessages
[language
][messageid
])
495 message
=csmessages
[language
][messageid
]->content
;
496 else if (csmessages
[0][messageid
])
497 message
=csmessages
[0][messageid
]->content
;
499 message
=defaultmessages
[messageid
*2];
501 messageargs
=defaultmessages
[messageid
*2+1];
502 va_start(va
, messageid
);
503 q9vsnprintf(buf
, 511, message
, messageargs
, va
);
505 killuser(chanservnick
, target
, buf
);
508 int checkpassword(reguser
*rup
, const char *pass
) {
509 if (!(*rup
->password
))
512 if (!strncmp(rup
->password
, pass
, PASSLEN
))
517 int checkresponse(reguser
*rup
, const unsigned char *entropy
, const char *response
, CRAlgorithm algorithm
) {
518 char usernamel
[NICKLEN
+1], *dp
, *up
;
520 if (!(*rup
->password
))
523 for(up
=rup
->username
,dp
=usernamel
;*up
;)
524 *dp
++ = ToLower(*up
++);
527 return algorithm(usernamel
, rup
->password
, cs_calcchallenge(entropy
), response
);
530 int checkhashpass(reguser
*rup
, const char *junk
, const char *hash
) {
531 char usernamel
[NICKLEN
+1], *dp
, *up
;
533 for(up
=rup
->username
,dp
=usernamel
;*up
;)
534 *dp
++ = ToLower(*up
++);
537 return cs_checkhashpass(usernamel
, rup
->password
, junk
, hash
);
540 int setpassword(reguser
*rup
, const char *pass
) {
541 strncpy(rup
->password
, pass
, PASSLEN
);
542 rup
->password
[PASSLEN
]='\0';
546 void cs_checknick(nick
*np
) {
549 char userhost
[USERLEN
+HOSTLEN
+3];
551 if (!(aup
=getactiveuserfromnick(np
))) {
553 np
->exts
[chanservnext
]=aup
;
556 assert(getactiveuserfromnick(np
));
558 if (IsAccount(np
) && np
->auth
) {
559 if (np
->auth
->exts
[chanservaext
]) {
560 rup
=getreguserfromnick(np
);
564 if (UIsDelayedGline(rup
)) {
565 /* delayed-gline - schedule the user's squelching */
566 deleteschedule(NULL
, &chanservdgline
, (void*)rup
); /* icky, but necessary unless we stick more stuff in reguser structure */
567 scheduleoneshot(time(NULL
)+rand()%900
, &chanservdgline
, (void*)rup
);
568 } else if (UIsGline(rup
)) {
569 /* instant-gline - lets be lazy and set a schedule expiring now :) */
570 deleteschedule(NULL
, &chanservdgline
, (void*)rup
); /* icky, but necessary unless we stick more stuff in reguser structure */
571 scheduleoneshot(time(NULL
), &chanservdgline
, (void*)rup
);
572 } else if(UHasSuspension(rup
)) {
573 chanservkillstdmessage(np
, QM_SUSPENDKILL
);
577 cs_doallautomodes(np
);
579 /* Auto create user.. */
582 rup
->ID
=np
->auth
->userid
;
583 if (rup
->ID
> lastuserID
)
585 strncpy(rup
->username
,np
->authname
,NICKLEN
); rup
->username
[NICKLEN
]='\0';
586 rup
->created
=time(NULL
);
588 rup
->lastemailchange
=0;
589 rup
->flags
=QUFLAG_NOTICE
;
595 rup
->password
[0]='\0';
601 sprintf(userhost
,"%s@%s",np
->ident
,np
->host
->name
->content
);
602 rup
->lastuserhost
=getsstring(userhost
,USERLEN
+HOSTLEN
+1);
603 rup
->suspendreason
=NULL
;
609 addregusertohash(rup
);
611 csdb_createuser(rup
);
615 cs_checknickbans(np
);
618 void cs_checkchanmodes(channel
*cp
) {
621 localsetmodeinit (&changes
, cp
, chanservnick
);
622 cs_docheckchanmodes(cp
, &changes
);
623 localsetmodeflush (&changes
, 1);
626 void cs_docheckchanmodes(channel
*cp
, modechanges
*changes
) {
632 if ((rcp
=cp
->index
->exts
[chanservext
])==NULL
|| CIsSuspended(rcp
))
635 /* Take care of the simple modes */
636 localdosetmode_simple(changes
, rcp
->forcemodes
& ~(cp
->flags
), rcp
->denymodes
& cp
->flags
);
638 /* Check for forced key. Note that we ALWAYS call localdosetmode_key
639 * in case the wrong key is set atm */
640 if (rcp
->forcemodes
& CHANMODE_KEY
) {
642 Error("chanserv",ERR_WARNING
,"Key forced but no key specified for %s: cleared forcemode.",rcp
->index
->name
->content
);
643 rcp
->forcemodes
&= ~CHANMODE_KEY
;
644 csdb_updatechannel(rcp
);
646 localdosetmode_key(changes
, rcp
->key
->content
, MCB_ADD
);
650 /* Check for denied key */
651 if ((rcp
->denymodes
& CHANMODE_KEY
) && IsKey(cp
)) {
652 localdosetmode_key(changes
, NULL
, MCB_DEL
);
655 /* Check for forced limit. Always call in case of wrong limit */
656 if (rcp
->forcemodes
& CHANMODE_LIMIT
) {
657 localdosetmode_limit(changes
, rcp
->limit
, MCB_ADD
);
661 /* Slightly misnamed function that checks each USER on the channel against channel
662 * settings. Possible actions are opping/deopping, voicing/devoicing, and banning
663 * for chanflag +k or chanlev flag +b */
664 void cs_docheckopvoice(channel
*cp
, modechanges
*changes
) {
674 if ((rcp
=cp
->index
->exts
[chanservext
])==NULL
|| CIsSuspended(rcp
))
677 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
678 if (cp
->users
->content
[i
]==nouser
)
681 if ((np
=getnickbynumeric(cp
->users
->content
[i
]))==NULL
) {
682 Error("chanserv",ERR_ERROR
,"Found non-existent numeric %lu on channel %s",cp
->users
->content
[i
],
683 cp
->index
->name
->content
);
687 if ((rup
=getreguserfromnick(np
)))
688 rcup
=findreguseronchannel(rcp
,rup
);
692 /* Various things that might ban the user - don't apply these to +o, +k or +X users */
693 if (!IsService(np
) && !IsXOper(np
) && !IsOper(np
)) {
694 if (rcup
&& CUIsBanned(rcup
)) {
695 cs_banuser(changes
, cp
->index
, np
, NULL
);
699 /* chanflag +k checks; kick them if they "obviously" can't rejoin without a ban */
700 if (CIsKnownOnly(rcp
) && !(rcup
&& CUKnown(rcup
))) {
701 if (IsInviteOnly(cp
) || (IsRegOnly(cp
) && !IsAccount(np
))) {
702 localkickuser(chanservnick
,cp
,np
,"Authorised users only.");
704 cs_banuser(NULL
, cp
->index
, np
, "Authorised users only.");
710 if ((cp
->users
->content
[i
] & CUMODE_OP
) && !IsService(np
)) {
711 if ((CIsBitch(rcp
) && (!rcup
|| !CUHasOpPriv(rcup
))) ||
712 (rcup
&& CUIsDeny(rcup
)))
713 localdosetmode_nick(changes
, np
, MC_DEOP
);
715 if (rcup
&& (CUIsProtect(rcup
) || CIsProtect(rcp
)) && CUIsOp(rcup
) && !CUIsDeny(rcup
)) {
716 localdosetmode_nick(changes
, np
, MC_OP
);
717 cs_logchanop(rcp
, np
->nick
, rcup
->user
);
721 if (cp
->users
->content
[i
] & CUMODE_VOICE
) {
722 if (rcup
&& CUIsQuiet(rcup
))
723 localdosetmode_nick(changes
, np
, MC_DEVOICE
);
725 if (rcup
&& (CUIsProtect(rcup
) || CIsProtect(rcp
)) && CUIsVoice(rcup
) && !CUIsQuiet(rcup
) && !(cp
->users
->content
[i
] & CUMODE_OP
))
726 localdosetmode_nick(changes
, np
, MC_VOICE
);
731 void cs_doallautomodes(nick
*np
) {
737 if (!(rup
=getreguserfromnick(np
)))
740 for (rcup
=rup
->knownon
;rcup
;rcup
=rcup
->nextbyuser
) {
741 /* Skip suspended channels */
742 if (CIsSuspended(rcup
->chan
))
745 if (rcup
->chan
->index
->channel
) {
747 if ((lp
=getnumerichandlefromchanhash(rcup
->chan
->index
->channel
->users
, np
->numeric
))) {
748 /* User is on channel.. */
750 if (CUKnown(rcup
) && rcup
->chan
->index
->channel
->users
->totalusers
>= 3) {
751 /* This meets the channel use criteria, update. */
752 rcup
->chan
->lastactive
=time(NULL
);
754 /* Don't spam the DB though for channels with lots of joins */
755 if (rcup
->chan
->lastcountersync
< (time(NULL
) - COUNTERSYNCINTERVAL
)) {
756 csdb_updatechannelcounters(rcup
->chan
);
757 rcup
->chan
->lastcountersync
=time(NULL
);
761 /* Update last use time */
762 rcup
->usetime
=getnettime();
764 localsetmodeinit(&changes
, rcup
->chan
->index
->channel
, chanservnick
);
765 if (*lp
& CUMODE_OP
) {
766 if (!IsService(np
) && (CUIsDeny(rcup
) || (CIsBitch(rcup
->chan
) && !CUHasOpPriv(rcup
))))
767 localdosetmode_nick(&changes
, np
, MC_DEOP
);
769 if (!CUIsDeny(rcup
) && CUIsOp(rcup
) &&
770 (CUIsProtect(rcup
) || CIsProtect(rcup
->chan
) || CUIsAutoOp(rcup
))) {
771 localdosetmode_nick(&changes
, np
, MC_OP
);
772 cs_logchanop(rcup
->chan
, np
->nick
, rup
);
776 if (*lp
& CUMODE_VOICE
) {
778 localdosetmode_nick(&changes
, np
, MC_DEVOICE
);
780 if (!CUIsQuiet(rcup
) && CUIsVoice(rcup
) && !(*lp
& CUMODE_OP
) &&
781 (CUIsProtect(rcup
) || CIsProtect(rcup
->chan
) || CUIsAutoVoice(rcup
)))
782 localdosetmode_nick(&changes
, np
, MC_VOICE
);
784 localsetmodeflush(&changes
, 1);
786 /* Channel exists but user is not joined: invite if they are +j-b */
787 if (CUIsAutoInvite(rcup
) && CUKnown(rcup
) && !CUIsBanned(rcup
)) {
788 localinvite(chanservnick
, rcup
->chan
->index
->channel
, np
);
795 void cs_checknickbans(nick
*np
) {
800 if (IsService(np
) || IsOper(np
) || IsXOper(np
))
803 /* Avoid races: memcpy the channel array */
804 i
=np
->channels
->cursi
;
805 ca
=(channel
**)malloc(i
*sizeof(channel
*));
806 memcpy(ca
, np
->channels
->content
,i
*sizeof(channel
*));
809 if ((rcp
=ca
[j
]->index
->exts
[chanservext
]) && !CIsSuspended(rcp
) &&
810 CIsEnforce(rcp
) && nickbanned_visible(np
, ca
[j
]))
811 localkickuser(chanservnick
, ca
[j
], np
, "Banned.");
817 void cs_checkbans(channel
*cp
) {
829 if ((rcp
=cp
->index
->exts
[chanservext
])==NULL
|| CIsSuspended(rcp
))
834 localsetmodeinit(&changes
, cp
, chanservnick
);
836 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
837 if (cp
->users
->content
[i
]==nouser
)
840 if ((np
=getnickbynumeric(cp
->users
->content
[i
]))==NULL
) {
841 Error("chanserv",ERR_ERROR
,"Found numeric %lu on channel %s who doesn't exist.",
842 cp
->users
->content
[i
], cp
->index
->name
->content
);
846 if (IsService(np
) || IsOper(np
) || IsXOper(np
))
849 for (rbp
=rcp
->bans
;rbp
;rbp
=rbp
->next
) {
850 if (((!rbp
->expiry
) || (rbp
->expiry
<= now
)) &&
851 nickmatchban_visible(np
, rbp
->cbp
)) {
852 if (!nickbanned_visible(np
, cp
)) {
853 localdosetmode_ban(&changes
, bantostring(rbp
->cbp
), MCB_ADD
);
855 localkickuser(chanservnick
,cp
,np
,rbp
->reason
?rbp
->reason
->content
:"Banned.");
860 if (rbp
) /* If we broke out of above loop (due to kick) rbp will be set.. */
863 if (CIsEnforce(rcp
)) {
864 for (cbp
=cp
->bans
;cbp
;cbp
=cbp
->next
) {
865 if ((cbp
->timeset
>=rcp
->lastbancheck
) && nickmatchban_visible(np
, cbp
))
866 localkickuser(chanservnick
,cp
,np
,"Banned.");
868 rcp
->lastbancheck
=time(NULL
);
872 localsetmodeflush(&changes
,1);
877 * This function schedules an update check on a channel
879 void cs_schedupdate(chanindex
*cip
, int mintime
, int maxtime
) {
880 int delay
=mintime
+(rand()%(maxtime
-mintime
));
883 if (!(rcp
=cip
->exts
[chanservext
]) || CIsSuspended(rcp
))
886 if (rcp
->checksched
) {
887 deleteschedule(rcp
->checksched
, cs_timerfunc
, cip
);
890 rcp
->checksched
=scheduleoneshot(time(NULL
)+delay
, cs_timerfunc
, cip
);
895 * This function does several things:
896 * * Updates auto-limit and/or removes bans as necessary
897 * * Schedules the next timed call
898 * * Does any pending op/ban/mode checks
899 * It is safe to use either as a target for a schedule() call, or to call
900 * directly when parameters change (e.g. autolimit settings or ban expire
902 * To force a limit update, set rcp->limit to 0..
905 void cs_timerfunc(void *arg
) {
907 channel
*cp
=cip
->channel
;
912 time_t now
=time(NULL
);
915 if (!(rcp
=cip
->exts
[chanservext
]))
920 /* Always nullify the checksched even if the channel is suspended.. */
921 if (rcp
->checksched
) {
922 deleteschedule(rcp
->checksched
, cs_timerfunc
, arg
);
923 rcp
->checksched
=NULL
;
926 if (!cp
|| CIsSuspended(rcp
))
929 /* Check if we need to leave the channel first */
930 if (chanservnick
&& CIsJoined(rcp
) && cip
->channel
&&
931 cip
->channel
->users
->totalusers
==1 &&
932 getnumerichandlefromchanhash(cip
->channel
->users
, chanservnick
->numeric
)) {
934 /* Only chanserv left in this channel */
935 if (now
>= (rcp
->lastpart
+ LINGERTIME
)) {
937 localpartchannel(chanservnick
, cip
->channel
, "Empty Channel");
940 if (!nextsched
|| nextsched
> (rcp
->lastpart
+ LINGERTIME
))
941 nextsched
=rcp
->lastpart
+LINGERTIME
;
945 localsetmodeinit(&changes
, cp
, chanservnick
);
947 if (CIsAutoLimit(rcp
)) {
948 if (!rcp
->limit
|| rcp
->autoupdate
<= now
) {
949 /* Only update the limit if there are <= (autolimit/2) or
950 * >= (autolimit * 1.5) slots free */
952 if ((cp
->users
->totalusers
>= (rcp
->limit
- rcp
->autolimit
/2)) ||
953 (cp
->users
->totalusers
<= (rcp
->limit
- (3 * rcp
->autolimit
)/2))) {
954 rcp
->limit
=cp
->users
->totalusers
+rcp
->autolimit
;
955 rcp
->status
|= QCSTAT_MODECHECK
;
958 /* And set the schedule for the next update */
959 rcp
->autoupdate
= now
+ AUTOLIMIT_INTERVAL
;
962 if (!nextsched
|| rcp
->autoupdate
<= nextsched
)
963 nextsched
=rcp
->autoupdate
;
966 if (rcp
->status
& QCSTAT_OPCHECK
) {
967 rcp
->status
&= ~QCSTAT_OPCHECK
;
968 cs_docheckopvoice(cp
, &changes
);
971 if (rcp
->status
& QCSTAT_MODECHECK
) {
972 rcp
->status
&= ~QCSTAT_MODECHECK
;
973 cs_docheckchanmodes(cp
, &changes
);
976 if (rcp
->status
& QCSTAT_BANCHECK
) {
977 rcp
->status
&= ~QCSTAT_BANCHECK
;
981 /* This ban check is AFTER docheckopvoice in case checkopvoice set a
982 * ban which we need to automatically remove later.. */
984 if (rcp
->banduration
) {
985 for (cbp
=cp
->bans
;cbp
;cbp
=ncbp
) {
987 if (cbp
->timeset
+rcp
->banduration
<=now
) {
988 localdosetmode_ban(&changes
, bantostring(cbp
), MCB_DEL
);
990 if (!nextsched
|| cbp
->timeset
+rcp
->banduration
<= nextsched
) {
991 nextsched
=rcp
->banduration
+cbp
->timeset
;
997 /* Check for expiry of registered bans */
999 for (rbh
=&(rcp
->bans
);*rbh
;) {
1001 verifyregchanban(rbp
);
1002 if (rbp
->expiry
&& (rbp
->expiry
< now
)) {
1004 /* Remove ban if it's on the channel (localdosetmode_ban will silently
1005 * skip bans that don't exist) */
1006 localdosetmode_ban(&changes
, bantostring(rbp
->cbp
), MCB_DEL
);
1007 /* Remove from database */
1008 csdb_deleteban(rbp
);
1009 /* Remove from list */
1011 /* Free ban/string and update setby refcount, and free actual regban */
1012 freesstring(rbp
->reason
);
1013 freechanban(rbp
->cbp
);
1016 if (rbp
->expiry
&& (!nextsched
|| rbp
->expiry
<= nextsched
)) {
1017 nextsched
=rbp
->expiry
;
1025 rcp
->checksched
=scheduleoneshot(nextsched
, cs_timerfunc
, arg
);
1027 localsetmodeflush(&changes
, 1);
1030 void cs_removechannel(regchan
*rcp
) {
1033 regchanuser
*rcup
, *nrcup
;
1038 for (i
=0;i
<REGCHANUSERHASHSIZE
;i
++) {
1039 for (rcup
=rcp
->regusers
[i
];rcup
;rcup
=nrcup
) {
1040 nrcup
=rcup
->nextbychan
;
1041 delreguserfromchannel(rcp
, rcup
->user
);
1042 freeregchanuser(rcup
);
1046 for (rbp
=rcp
->bans
;rbp
;rbp
=nrbp
) {
1048 freesstring(rbp
->reason
);
1049 freechanban(rbp
->cbp
);
1055 if (rcp
->checksched
)
1056 deleteschedule(rcp
->checksched
, cs_timerfunc
, rcp
->index
);
1059 rcp
->flags
=QCFLAG_SUSPENDED
;
1060 chanservjoinchan(cip
->channel
); /* Force off the channel */
1063 csdb_deletechannel(rcp
);
1065 freesstring(rcp
->welcome
);
1066 freesstring(rcp
->topic
);
1067 freesstring(rcp
->key
);
1068 freesstring(rcp
->suspendreason
);
1069 freesstring(rcp
->comment
);
1073 cip
->exts
[chanservext
]=NULL
;
1074 releasechanindex(cip
);
1077 /* Sender is who the DELCHAN is attributed to.. */
1078 int cs_removechannelifempty(nick
*sender
, regchan
*rcp
) {
1082 for (i
=0;i
<REGCHANUSERHASHSIZE
;i
++) {
1083 for (rcup
=rcp
->regusers
[i
];rcup
;rcup
=rcup
->nextbychan
) {
1084 if (rcup
->flags
& ~(QCUFLAGS_PUNISH
))
1089 cs_log(sender
,"DELCHAN %s (Empty)",rcp
->index
->name
->content
);
1090 cs_removechannel(rcp
);
1095 void cs_removeuser(reguser
*rup
) {
1096 regchanuser
*rcup
, *nrcup
;
1098 struct authname
*anp
;
1100 /* Remove the user from all its channels */
1101 for (rcup
=rup
->knownon
;rcup
;rcup
=nrcup
) {
1102 nrcup
=rcup
->nextbyuser
;
1105 delreguserfromchannel(rcp
, rup
);
1106 cs_removechannelifempty(NULL
, rcp
);
1110 delreguserfrommaildomain(rup
,rup
->domain
);
1111 freesstring(rup
->localpart
);
1112 freesstring(rup
->email
);
1113 freesstring(rup
->lastemail
);
1114 freesstring(rup
->lastuserhost
);
1115 freesstring(rup
->suspendreason
);
1116 freesstring(rup
->comment
);
1117 freesstring(rup
->info
);
1119 csdb_deleteuser(rup
);
1121 removereguserfromhash(rup
);
1123 if ((anp
=findauthname(rup
->ID
)) && anp
->nicks
) {
1124 rup
->status
|= QUSTAT_DEAD
;
1130 int cs_bancheck(nick
*np
, channel
*cp
) {
1131 time_t now
=time(NULL
);
1133 modechanges changes
;
1136 if (!(rcp
=cp
->index
->exts
[chanservext
]))
1139 for (rbh
=&(rcp
->bans
);*rbh
;) {
1140 if ((*rbh
)->expiry
&& ((*rbh
)->expiry
< now
)) {
1142 csdb_deleteban(*rbh
);
1146 freesstring(rbp
->reason
);
1147 freechanban(rbp
->cbp
);
1149 } else if (nickmatchban_visible(np
,(*rbh
)->cbp
)) {
1150 /* This user matches this ban.. */
1151 if (!nickbanned_visible(np
,cp
)) {
1152 /* Only bother putting the ban on the channel if they're not banned already */
1153 /* (might be covered by this ban or a different one.. doesn't really matter */
1154 localsetmodeinit(&changes
, cp
, chanservnick
);
1155 localdosetmode_ban(&changes
, bantostring((*rbh
)->cbp
), MCB_ADD
);
1156 localsetmodeflush(&changes
, 1);
1157 cs_timerfunc(cp
->index
);
1159 localkickuser(chanservnick
, cp
, np
, (*rbh
)->reason
? (*rbh
)->reason
->content
: "Banned.");
1162 rbh
=&((*rbh
)->next
);
1169 void cs_setregban(chanindex
*cip
, regban
*rbp
) {
1170 modechanges changes
;
1177 localsetmodeinit(&changes
, cip
->channel
, chanservnick
);
1178 localdosetmode_ban(&changes
, bantostring(rbp
->cbp
), MCB_ADD
);
1179 localsetmodeflush(&changes
, 1);
1181 for (i
=0;(cip
->channel
) && i
<cip
->channel
->users
->hashsize
;i
++) {
1182 if (cip
->channel
->users
->content
[i
]!=nouser
&&
1183 (np
=getnickbynumeric(cip
->channel
->users
->content
[i
])) &&
1184 !IsService(np
) && !IsOper(np
) && !IsXOper(np
) &&
1185 nickmatchban_visible(np
, rbp
->cbp
))
1186 localkickuser(chanservnick
, cip
->channel
, np
, rbp
->reason
? rbp
->reason
->content
: "Banned.");
1192 void cs_banuser(modechanges
*changes
, chanindex
*cip
, nick
*np
, const char *reason
) {
1193 modechanges lchanges
;
1194 char banmask
[NICKLEN
+USERLEN
+HOSTLEN
+3];
1199 if (nickbanned_visible(np
, cip
->channel
)) {
1200 localkickuser(chanservnick
, cip
->channel
, np
, reason
?reason
:"Banned.");
1204 if (IsAccount(np
)) {
1205 /* Ban their account.. */
1206 sprintf(banmask
,"*!*@%s.%s",np
->authname
,HIS_HIDDENHOST
);
1207 } else if (IsSetHost(np
)) {
1208 /* sethosted: ban ident@host */
1209 sprintf(banmask
,"*!%s@%s",np
->shident
->content
,np
->sethost
->content
);
1210 } else if (np
->host
->clonecount
>3) {
1211 /* >3 clones, ban user@host */
1212 sprintf(banmask
,"*!%s@%s",np
->ident
,np
->host
->name
->content
);
1214 sprintf(banmask
,"*!*@%s",np
->host
->name
->content
);
1218 localsetmodeinit(&lchanges
, cip
->channel
, chanservnick
);
1219 localdosetmode_ban(&lchanges
, banmask
, MCB_ADD
);
1220 localsetmodeflush(&lchanges
, 1);
1222 localdosetmode_ban(changes
, banmask
, MCB_ADD
);
1225 localkickuser(chanservnick
, cip
->channel
, np
, reason
?reason
:"Banned.");
1229 * cs_sanitisechanlev: Removes impossible combinations from chanlev flags.
1230 * chanservuser.c is probably not the right file for this, but nowhere better
1231 * presented itself...
1233 flag_t
cs_sanitisechanlev(flag_t flags
) {
1234 /* +m or +n cannot have any "punishment" flags */
1235 if (flags
& (QCUFLAG_MASTER
| QCUFLAG_OWNER
))
1236 flags
&= ~(QCUFLAGS_PUNISH
);
1238 /* +d can't be +o */
1239 if (flags
& QCUFLAG_DENY
)
1240 flags
&= ~QCUFLAG_OP
;
1242 /* +q can't be +v */
1243 if (flags
& QCUFLAG_QUIET
)
1244 flags
&= ~QCUFLAG_VOICE
;
1246 /* +p trumps +a and +g */
1247 if (flags
& QCUFLAG_PROTECT
)
1248 flags
&= ~(QCUFLAG_AUTOOP
| QCUFLAG_AUTOVOICE
);
1250 /* -o can't be +a */
1251 if (!(flags
& QCUFLAG_OP
))
1252 flags
&= ~QCUFLAG_AUTOOP
;
1254 /* +a or -v can't be +g. +a implies +o at this stage (see above) */
1255 if (!(flags
& QCUFLAG_VOICE
) || (flags
& QCUFLAG_AUTOOP
))
1256 flags
&= ~QCUFLAG_AUTOVOICE
;
1258 /* +p requires +o or +v */
1259 if (!(flags
& (QCUFLAG_VOICE
| QCUFLAG_OP
)))
1260 flags
&= ~QCUFLAG_PROTECT
;
1262 /* The personal flags require one of +mnovk, as does +t */
1263 if (!(flags
& (QCUFLAG_OWNER
| QCUFLAG_MASTER
| QCUFLAG_OP
| QCUFLAG_VOICE
| QCUFLAG_KNOWN
)))
1264 flags
&= ~(QCUFLAGS_PERSONAL
| QCUFLAG_TOPIC
);
1271 * This function does the standard "nick or #user" lookup.
1272 * If "sender" is not NULL, a suitable error message will
1273 * be sent if the lookup fails.
1274 * "sender" MUST be sent when a user is requesting a lookup
1275 * as there is some policy code here.
1278 reguser
*findreguser(nick
*sender
, const char *str
) {
1279 reguser
*rup
, *vrup
= getreguserfromnick(sender
);;
1288 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1291 if (!(rup
=findreguserbynick(str
+1)) && sender
)
1292 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1293 } else if (*str
=='&' && vrup
&& UHasHelperPriv(vrup
)) {
1296 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1299 if (!(rup
=findreguserbyID(atoi(str
+1))) && sender
)
1300 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1302 if (!(np
=getnickbynick(str
))) {
1304 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1307 if (!(rup
=getreguserfromnick(np
)) && sender
)
1308 chanservstdmessage(sender
, QM_USERNOTAUTHED
, str
);
1311 /* removed the suspended check from here, I don't see the point... */
1312 if (rup
&& (rup
->status
& QUSTAT_DEAD
)) {
1313 chanservstdmessage(sender
, QM_USERHASBADAUTH
, rup
->username
);
1321 * Unbans a mask from a channel, including permbans if user has correct privs.
1323 * Return 0 if it works, 1 if it don't.
1325 int cs_unbanfn(nick
*sender
, chanindex
*cip
, UnbanFN fn
, void *arg
, int removepermbans
, int abortonfailure
) {
1327 chanban
**cbh
, *cbp
;
1329 modechanges changes
;
1332 rcp
=cip
->exts
[chanservext
];
1335 localsetmodeinit(&changes
, cip
->channel
, chanservnick
);
1337 for (rbh
=&(rcp
->bans
); *rbh
; ) {
1339 if (fn(arg
, rbp
->cbp
)) {
1340 banstr
=bantostring(rbp
->cbp
);
1341 /* Check perms and remove */
1342 if(!removepermbans
) {
1343 chanservstdmessage(sender
, QM_WARNNOTREMOVEDPERMBAN
, banstr
, cip
->name
->content
);
1345 } else if (!cs_checkaccess(sender
, NULL
, CA_MASTERPRIV
, cip
, NULL
, 0, 1)) {
1346 chanservstdmessage(sender
, QM_NOTREMOVEDPERMBAN
, banstr
, cip
->name
->content
);
1347 if (abortonfailure
) return 1; /* Just give up... */
1350 chanservstdmessage(sender
, QM_REMOVEDPERMBAN
, banstr
, cip
->name
->content
);
1352 localdosetmode_ban(&changes
, banstr
, MCB_DEL
);
1353 /* Remove from database */
1354 csdb_deleteban(rbp
);
1355 /* Remove from list */
1357 /* Free ban/string and update setby refcount, and free actual regban */
1358 freesstring(rbp
->reason
);
1359 freechanban(rbp
->cbp
);
1368 for (cbh
=&(cip
->channel
->bans
); *cbh
; ) {
1372 banstr
=bantostring(cbp
);
1373 chanservstdmessage(sender
, QM_REMOVEDCHANBAN
, banstr
, cip
->name
->content
);
1374 localdosetmode_ban(&changes
, banstr
, MCB_DEL
);
1379 localsetmodeflush(&changes
,1);
1385 /* Add entry to channel op history when someone gets ops. */
1386 void cs_logchanop(regchan
*rcp
, char *nick
, reguser
*rup
) {
1387 strncpy(rcp
->chanopnicks
[rcp
->chanoppos
], nick
, NICKLEN
);
1388 rcp
->chanopnicks
[rcp
->chanoppos
][NICKLEN
]='\0';
1389 rcp
->chanopaccts
[rcp
->chanoppos
]=rup
->ID
;
1390 rcp
->chanoppos
=(rcp
->chanoppos
+1)%CHANOPHISTORY
;