]>
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
) {
271 snprintf(buf
, sizeof(buf
), "Channel suspended: %s", rcp
->suspendreason
->content
);
272 localpartchannel(chanservnick
, cp
, buf
);
274 localpartchannel(chanservnick
, cp
, "Channel suspended");
278 /* Right, we are definately going to either join the channel or at least
279 * burst some modes onto it.
281 * We will try and burst our view of the world; if the timestamps are
282 * actually equal this will be mostly ignored and we will have to fix it
283 * up later. For modes we use the forced modes, plus the default channel
284 * modes (unless any of those are explicitly denied) */
286 /* By default, we set the forcemodes and the default modes, but never denymodes..
287 * OK, actually we should only set the default modes if our timestamp is older.*/
288 if (cp
->timestamp
> rcp
->ltimestamp
)
289 themodes
|= CHANMODE_DEFAULT
;
293 themodes
= (themodes
| rcp
->forcemodes
) & ~rcp
->denymodes
;
295 /* Now, if someone has just created a channel and we are going to set +i
296 * or +k on it, this will kick them off. This could be construed as a
297 * bit rude if it's their channel, so if there is only one person on the
298 * channel and they have a right to be there, burst with default modes
299 * only to avoid them being netrider kicked.
301 if (cp
->users
->totalusers
==1) {
302 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
303 if (cp
->users
->content
[i
] != nouser
) {
304 if ((np
=getnickbynumeric(cp
->users
->content
[i
]&CU_NUMERICMASK
)) &&
305 (rup
=getreguserfromnick(np
)) &&
306 (rcup
=findreguseronchannel(rcp
,rup
)) &&
308 /* OK, there was one user, and they are known on this channel.
309 * Don't burst with +i or +k */
310 themodes
&= ~(CHANMODE_INVITEONLY
| CHANMODE_KEY
);
316 /* We should be on the channel - join with our nick */
317 if (!CIsSuspended(rcp
) && CIsJoined(rcp
) && !getnumerichandlefromchanhash(cp
->users
, chanservnick
->numeric
)) {
318 /* If we pass the key parameter here but are not setting +k (see above)
319 * then localburstontochannel() will ignore the key */
320 localburstontochannel(cp
, chanservnick
, rcp
->ltimestamp
, themodes
,
321 rcp
->limit
, (rcp
->key
)?rcp
->key
->content
:NULL
);
324 /* We're not joining the channel - send the burst with no nick */
325 if (!CIsSuspended(rcp
) && !CIsJoined(rcp
) && (cp
->timestamp
> rcp
->ltimestamp
)) {
326 localburstontochannel(cp
, NULL
, rcp
->ltimestamp
, themodes
,
327 rcp
->limit
, (rcp
->key
)?rcp
->key
->content
:NULL
);
331 void chanservstdmessage(nick
*np
, int messageid
, ... ) {
338 char *message
, *messageargs
;
342 if (getreguserfromnick(np
) == NULL
) {
346 rup
=getreguserfromnick(np
);
352 language
=rup
->languageid
;
355 if (csmessages
[language
][messageid
]) {
356 message
=csmessages
[language
][messageid
]->content
;
357 } else if (csmessages
[0][messageid
]) {
358 message
=csmessages
[0][messageid
]->content
;
360 message
=defaultmessages
[messageid
*2];
363 messageargs
=defaultmessages
[messageid
*2+1];
365 va_start(va
,messageid
);
367 q9vsnprintf(buf
,5000,message
,messageargs
,va
);
372 for (bp
=buf
; ;bp
++) {
373 /* We send something if we hit a \n, fill the buffer or run out of source */
374 if (*bp
=='\n' || len
>490 || !*bp
) {
375 if (*bp
&& *bp
!='\n') {
381 if (chanservnick
&& *buf2
) {
383 sendnoticetouser(chanservnick
,np
,"%s",buf2
);
385 sendmessagetouser(chanservnick
,np
,"%s",buf2
);
389 /* If we ran out of buffer, get out here */
401 /* Special case: If it's a "not enough parameters" message, show the first line of help */
402 if (messageid
==QM_NOTENOUGHPARAMS
) {
403 char *command
=va_arg(va2
, char *);
404 cs_sendhelp(np
, command
, 1);
405 chanservstdmessage(np
, QM_TYPEHELPFORHELP
, command
);
411 void chanservsendmessage_real(nick
*np
, int oneline
, char *message
, ... ) {
412 char buf
[5010]; /* Very large buffer.. */
413 char buf2
[512], *bp
, *bp2
;
420 va_start(va
,message
);
421 vsnprintf(buf
,5000,message
,va
);
424 if (getreguserfromnick(np
) == NULL
) {
427 rup
=getreguserfromnick(np
);
437 for (bp
=buf
; ;bp
++) {
438 /* We send something if we hit a \n, fill the buffer or run out of source */
439 if (*bp
=='\n' || len
>490 || !*bp
) {
440 if (*bp
&& *bp
!='\n') {
446 if (chanservnick
&& *buf2
) {
448 sendnoticetouser(chanservnick
,np
,"%s",buf2
);
450 sendmessagetouser(chanservnick
,np
,"%s",buf2
);
454 /* If we ran out of buffer, get out here */
455 if (!*bp
|| (*bp
=='\n' && oneline
))
467 void chanservwallmessage(char *message
, ... ) {
468 char buf
[5010]; /* Very large buffer.. */
473 va_start(va
,message
);
474 vsnprintf(buf
,5000,message
,va
);
478 for (i
=0;i
<NICKHASHSIZE
;i
++) {
479 for (np
=nicktable
[i
];np
;np
=np
->next
) {
480 if (!IsOper(np
)) /* optimisation, if VIEWWALLMESSAGE changes change this */
483 if (!cs_privcheck(QPRIV_VIEWWALLMESSAGE
, np
))
486 chanservsendmessage(np
, "%s", buf
);
491 void chanservkillstdmessage(nick
*target
, int messageid
, ... ) {
494 char *message
, *messageargs
;
498 if (!(rup
=getreguserfromnick(target
)))
501 language
=rup
->languageid
;
503 if (csmessages
[language
][messageid
])
504 message
=csmessages
[language
][messageid
]->content
;
505 else if (csmessages
[0][messageid
])
506 message
=csmessages
[0][messageid
]->content
;
508 message
=defaultmessages
[messageid
*2];
510 messageargs
=defaultmessages
[messageid
*2+1];
511 va_start(va
, messageid
);
512 q9vsnprintf(buf
, 511, message
, messageargs
, va
);
514 killuser(chanservnick
, target
, buf
);
517 int checkpassword(reguser
*rup
, const char *pass
) {
518 if (!(*rup
->password
))
521 if (!strncmp(rup
->password
, pass
, PASSLEN
))
526 int checkresponse(reguser
*rup
, const unsigned char *entropy
, const char *response
, CRAlgorithm algorithm
) {
527 char usernamel
[NICKLEN
+1], *dp
, *up
;
529 if (!(*rup
->password
))
532 for(up
=rup
->username
,dp
=usernamel
;*up
;)
533 *dp
++ = ToLower(*up
++);
536 return algorithm(usernamel
, rup
->password
, cs_calcchallenge(entropy
), response
);
539 int checkhashpass(reguser
*rup
, const char *junk
, const char *hash
) {
540 char usernamel
[NICKLEN
+1], *dp
, *up
;
542 for(up
=rup
->username
,dp
=usernamel
;*up
;)
543 *dp
++ = ToLower(*up
++);
546 return cs_checkhashpass(usernamel
, rup
->password
, junk
, hash
);
549 int setpassword(reguser
*rup
, const char *pass
) {
550 strncpy(rup
->password
, pass
, PASSLEN
);
551 rup
->password
[PASSLEN
]='\0';
555 void cs_checknick(nick
*np
) {
558 char userhost
[USERLEN
+HOSTLEN
+3];
560 if (!(aup
=getactiveuserfromnick(np
))) {
562 np
->exts
[chanservnext
]=aup
;
565 assert(getactiveuserfromnick(np
));
567 if (IsAccount(np
) && np
->auth
) {
568 if (np
->auth
->exts
[chanservaext
]) {
569 rup
=getreguserfromnick(np
);
573 if (UIsDelayedGline(rup
)) {
574 /* delayed-gline - schedule the user's squelching */
575 deleteschedule(NULL
, &chanservdgline
, (void*)rup
); /* icky, but necessary unless we stick more stuff in reguser structure */
576 scheduleoneshot(time(NULL
)+rand()%900
, &chanservdgline
, (void*)rup
);
577 } else if (UIsGline(rup
)) {
578 /* instant-gline - lets be lazy and set a schedule expiring now :) */
579 deleteschedule(NULL
, &chanservdgline
, (void*)rup
); /* icky, but necessary unless we stick more stuff in reguser structure */
580 scheduleoneshot(time(NULL
), &chanservdgline
, (void*)rup
);
581 } else if(UHasSuspension(rup
)) {
582 chanservkillstdmessage(np
, QM_SUSPENDKILL
);
586 cs_doallautomodes(np
);
588 /* Auto create user.. */
591 rup
->ID
=np
->auth
->userid
;
592 if (rup
->ID
> lastuserID
)
594 strncpy(rup
->username
,np
->authname
,NICKLEN
); rup
->username
[NICKLEN
]='\0';
595 rup
->created
=time(NULL
);
597 rup
->lastemailchange
=0;
598 rup
->lastpasschange
=0;
599 rup
->flags
=QUFLAG_NOTICE
;
605 rup
->password
[0]='\0';
611 sprintf(userhost
,"%s@%s",np
->ident
,np
->host
->name
->content
);
612 rup
->lastuserhost
=getsstring(userhost
,USERLEN
+HOSTLEN
+1);
613 rup
->suspendreason
=NULL
;
619 addregusertohash(rup
);
621 csdb_createuser(rup
);
625 cs_checknickbans(np
);
628 void cs_checkchanmodes(channel
*cp
) {
631 localsetmodeinit (&changes
, cp
, chanservnick
);
632 cs_docheckchanmodes(cp
, &changes
);
633 localsetmodeflush (&changes
, 1);
636 void cs_docheckchanmodes(channel
*cp
, modechanges
*changes
) {
642 if ((rcp
=cp
->index
->exts
[chanservext
])==NULL
|| CIsSuspended(rcp
))
645 /* Take care of the simple modes */
646 localdosetmode_simple(changes
, rcp
->forcemodes
& ~(cp
->flags
), rcp
->denymodes
& cp
->flags
);
648 /* Check for forced key. Note that we ALWAYS call localdosetmode_key
649 * in case the wrong key is set atm */
650 if (rcp
->forcemodes
& CHANMODE_KEY
) {
652 Error("chanserv",ERR_WARNING
,"Key forced but no key specified for %s: cleared forcemode.",rcp
->index
->name
->content
);
653 rcp
->forcemodes
&= ~CHANMODE_KEY
;
654 csdb_updatechannel(rcp
);
656 localdosetmode_key(changes
, rcp
->key
->content
, MCB_ADD
);
660 /* Check for denied key */
661 if ((rcp
->denymodes
& CHANMODE_KEY
) && IsKey(cp
)) {
662 localdosetmode_key(changes
, NULL
, MCB_DEL
);
665 /* Check for forced limit. Always call in case of wrong limit */
666 if (rcp
->forcemodes
& CHANMODE_LIMIT
) {
667 localdosetmode_limit(changes
, rcp
->limit
, MCB_ADD
);
671 /* Slightly misnamed function that checks each USER on the channel against channel
672 * settings. Possible actions are opping/deopping, voicing/devoicing, and banning
673 * for chanflag +k or chanlev flag +b */
674 void cs_docheckopvoice(channel
*cp
, modechanges
*changes
) {
684 if ((rcp
=cp
->index
->exts
[chanservext
])==NULL
|| CIsSuspended(rcp
))
687 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
688 if (cp
->users
->content
[i
]==nouser
)
691 if ((np
=getnickbynumeric(cp
->users
->content
[i
]))==NULL
) {
692 Error("chanserv",ERR_ERROR
,"Found non-existent numeric %lu on channel %s",cp
->users
->content
[i
],
693 cp
->index
->name
->content
);
697 if ((rup
=getreguserfromnick(np
)))
698 rcup
=findreguseronchannel(rcp
,rup
);
702 /* Various things that might ban the user - don't apply these to +o, +k or +X users */
703 if (!IsService(np
) && !IsXOper(np
) && !IsOper(np
)) {
704 if (rcup
&& CUIsBanned(rcup
)) {
705 cs_banuser(changes
, cp
->index
, np
, NULL
);
709 /* chanflag +k checks; kick them if they "obviously" can't rejoin without a ban */
710 if (CIsKnownOnly(rcp
) && !(rcup
&& CUKnown(rcup
))) {
711 if (IsInviteOnly(cp
) || (IsRegOnly(cp
) && !IsAccount(np
))) {
712 localkickuser(chanservnick
,cp
,np
,"Authorised users only.");
714 cs_banuser(NULL
, cp
->index
, np
, "Authorised users only.");
720 if ((cp
->users
->content
[i
] & CUMODE_OP
) && !IsService(np
)) {
721 if ((CIsBitch(rcp
) && (!rcup
|| !CUHasOpPriv(rcup
))) ||
722 (rcup
&& CUIsDeny(rcup
)))
723 localdosetmode_nick(changes
, np
, MC_DEOP
);
725 if (rcup
&& (CUIsProtect(rcup
) || CIsProtect(rcp
)) && CUIsOp(rcup
) && !CUIsDeny(rcup
)) {
726 localdosetmode_nick(changes
, np
, MC_OP
);
727 cs_logchanop(rcp
, np
->nick
, rcup
->user
);
731 if (cp
->users
->content
[i
] & CUMODE_VOICE
) {
732 if (rcup
&& CUIsQuiet(rcup
))
733 localdosetmode_nick(changes
, np
, MC_DEVOICE
);
735 if (rcup
&& (CUIsProtect(rcup
) || CIsProtect(rcp
)) && CUIsVoice(rcup
) && !CUIsQuiet(rcup
) && !(cp
->users
->content
[i
] & CUMODE_OP
))
736 localdosetmode_nick(changes
, np
, MC_VOICE
);
741 void cs_doallautomodes(nick
*np
) {
747 if (!(rup
=getreguserfromnick(np
)))
750 for (rcup
=rup
->knownon
;rcup
;rcup
=rcup
->nextbyuser
) {
751 /* Skip suspended channels */
752 if (CIsSuspended(rcup
->chan
))
755 if (rcup
->chan
->index
->channel
) {
757 if ((lp
=getnumerichandlefromchanhash(rcup
->chan
->index
->channel
->users
, np
->numeric
))) {
758 /* User is on channel.. */
760 if (CUKnown(rcup
) && rcup
->chan
->index
->channel
->users
->totalusers
>= 3) {
761 /* This meets the channel use criteria, update. */
762 rcup
->chan
->lastactive
=time(NULL
);
764 /* Don't spam the DB though for channels with lots of joins */
765 if (rcup
->chan
->lastcountersync
< (time(NULL
) - COUNTERSYNCINTERVAL
)) {
766 csdb_updatechannelcounters(rcup
->chan
);
767 rcup
->chan
->lastcountersync
=time(NULL
);
771 /* Update last use time */
772 rcup
->usetime
=getnettime();
774 localsetmodeinit(&changes
, rcup
->chan
->index
->channel
, chanservnick
);
775 if (*lp
& CUMODE_OP
) {
776 if (!IsService(np
) && (CUIsDeny(rcup
) || (CIsBitch(rcup
->chan
) && !CUHasOpPriv(rcup
))))
777 localdosetmode_nick(&changes
, np
, MC_DEOP
);
779 if (!CUIsDeny(rcup
) && CUIsOp(rcup
) &&
780 (CUIsProtect(rcup
) || CIsProtect(rcup
->chan
) || CUIsAutoOp(rcup
))) {
781 localdosetmode_nick(&changes
, np
, MC_OP
);
782 cs_logchanop(rcup
->chan
, np
->nick
, rup
);
786 if (*lp
& CUMODE_VOICE
) {
788 localdosetmode_nick(&changes
, np
, MC_DEVOICE
);
790 if (!CUIsQuiet(rcup
) && CUIsVoice(rcup
) && !(*lp
& CUMODE_OP
) &&
791 (CUIsProtect(rcup
) || CIsProtect(rcup
->chan
) || CUIsAutoVoice(rcup
)))
792 localdosetmode_nick(&changes
, np
, MC_VOICE
);
794 localsetmodeflush(&changes
, 1);
796 /* Channel exists but user is not joined: invite if they are +j-b */
797 if (CUIsAutoInvite(rcup
) && CUKnown(rcup
) && !CUIsBanned(rcup
)) {
798 localinvite(chanservnick
, rcup
->chan
->index
->channel
, np
);
805 void cs_checknickbans(nick
*np
) {
810 if (IsService(np
) || IsOper(np
) || IsXOper(np
))
813 /* Avoid races: memcpy the channel array */
814 i
=np
->channels
->cursi
;
815 ca
=(channel
**)malloc(i
*sizeof(channel
*));
816 memcpy(ca
, np
->channels
->content
,i
*sizeof(channel
*));
819 if ((rcp
=ca
[j
]->index
->exts
[chanservext
]) && !CIsSuspended(rcp
) &&
820 CIsEnforce(rcp
) && nickbanned_visible(np
, ca
[j
]))
821 localkickuser(chanservnick
, ca
[j
], np
, "Banned.");
827 void cs_checkbans(channel
*cp
) {
839 if ((rcp
=cp
->index
->exts
[chanservext
])==NULL
|| CIsSuspended(rcp
))
844 localsetmodeinit(&changes
, cp
, chanservnick
);
846 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
847 if (cp
->users
->content
[i
]==nouser
)
850 if ((np
=getnickbynumeric(cp
->users
->content
[i
]))==NULL
) {
851 Error("chanserv",ERR_ERROR
,"Found numeric %lu on channel %s who doesn't exist.",
852 cp
->users
->content
[i
], cp
->index
->name
->content
);
856 if (IsService(np
) || IsOper(np
) || IsXOper(np
))
859 for (rbp
=rcp
->bans
;rbp
;rbp
=rbp
->next
) {
860 if (((!rbp
->expiry
) || (rbp
->expiry
<= now
)) &&
861 nickmatchban_visible(np
, rbp
->cbp
)) {
862 if (!nickbanned_visible(np
, cp
)) {
863 localdosetmode_ban(&changes
, bantostring(rbp
->cbp
), MCB_ADD
);
865 localkickuser(chanservnick
,cp
,np
,rbp
->reason
?rbp
->reason
->content
:"Banned.");
870 if (rbp
) /* If we broke out of above loop (due to kick) rbp will be set.. */
873 if (CIsEnforce(rcp
)) {
874 for (cbp
=cp
->bans
;cbp
;cbp
=cbp
->next
) {
875 if ((cbp
->timeset
>=rcp
->lastbancheck
) && nickmatchban_visible(np
, cbp
))
876 localkickuser(chanservnick
,cp
,np
,"Banned.");
878 rcp
->lastbancheck
=time(NULL
);
882 localsetmodeflush(&changes
,1);
887 * This function schedules an update check on a channel
889 void cs_schedupdate(chanindex
*cip
, int mintime
, int maxtime
) {
890 int delay
=mintime
+(rand()%(maxtime
-mintime
));
893 if (!(rcp
=cip
->exts
[chanservext
]) || CIsSuspended(rcp
))
896 if (rcp
->checksched
) {
897 deleteschedule(rcp
->checksched
, cs_timerfunc
, cip
);
900 rcp
->checksched
=scheduleoneshot(time(NULL
)+delay
, cs_timerfunc
, cip
);
905 * This function does several things:
906 * * Updates auto-limit and/or removes bans as necessary
907 * * Schedules the next timed call
908 * * Does any pending op/ban/mode checks
909 * It is safe to use either as a target for a schedule() call, or to call
910 * directly when parameters change (e.g. autolimit settings or ban expire
912 * To force a limit update, set rcp->limit to 0..
915 void cs_timerfunc(void *arg
) {
917 channel
*cp
=cip
->channel
;
922 time_t now
=time(NULL
);
925 if (!(rcp
=cip
->exts
[chanservext
]))
930 /* Always nullify the checksched even if the channel is suspended.. */
931 if (rcp
->checksched
) {
932 deleteschedule(rcp
->checksched
, cs_timerfunc
, arg
);
933 rcp
->checksched
=NULL
;
936 if (!cp
|| CIsSuspended(rcp
))
939 /* Check if we need to leave the channel first */
940 if (chanservnick
&& CIsJoined(rcp
) && cip
->channel
&&
941 cip
->channel
->users
->totalusers
==1 &&
942 getnumerichandlefromchanhash(cip
->channel
->users
, chanservnick
->numeric
)) {
944 /* Only chanserv left in this channel */
945 if (now
>= (rcp
->lastpart
+ LINGERTIME
)) {
947 localpartchannel(chanservnick
, cip
->channel
, "Empty Channel");
950 if (!nextsched
|| nextsched
> (rcp
->lastpart
+ LINGERTIME
))
951 nextsched
=rcp
->lastpart
+LINGERTIME
;
955 localsetmodeinit(&changes
, cp
, chanservnick
);
957 if (CIsAutoLimit(rcp
)) {
958 if (!rcp
->limit
|| rcp
->autoupdate
<= now
) {
959 /* Only update the limit if there are <= (autolimit/2) or
960 * >= (autolimit * 1.5) slots free */
962 if ((cp
->users
->totalusers
>= (rcp
->limit
- rcp
->autolimit
/2)) ||
963 (cp
->users
->totalusers
<= (rcp
->limit
- (3 * rcp
->autolimit
)/2))) {
964 rcp
->limit
=cp
->users
->totalusers
+rcp
->autolimit
;
965 rcp
->status
|= QCSTAT_MODECHECK
;
968 /* And set the schedule for the next update */
969 rcp
->autoupdate
= now
+ AUTOLIMIT_INTERVAL
;
972 if (!nextsched
|| rcp
->autoupdate
<= nextsched
)
973 nextsched
=rcp
->autoupdate
;
976 if (rcp
->status
& QCSTAT_OPCHECK
) {
977 rcp
->status
&= ~QCSTAT_OPCHECK
;
978 cs_docheckopvoice(cp
, &changes
);
981 if (rcp
->status
& QCSTAT_MODECHECK
) {
982 rcp
->status
&= ~QCSTAT_MODECHECK
;
983 cs_docheckchanmodes(cp
, &changes
);
986 if (rcp
->status
& QCSTAT_BANCHECK
) {
987 rcp
->status
&= ~QCSTAT_BANCHECK
;
991 /* This ban check is AFTER docheckopvoice in case checkopvoice set a
992 * ban which we need to automatically remove later.. */
994 if (rcp
->banduration
) {
995 for (cbp
=cp
->bans
;cbp
;cbp
=ncbp
) {
997 if (cbp
->timeset
+rcp
->banduration
<=now
) {
998 localdosetmode_ban(&changes
, bantostring(cbp
), MCB_DEL
);
1000 if (!nextsched
|| cbp
->timeset
+rcp
->banduration
<= nextsched
) {
1001 nextsched
=rcp
->banduration
+cbp
->timeset
;
1007 /* Check for expiry of registered bans */
1009 for (rbh
=&(rcp
->bans
);*rbh
;) {
1011 verifyregchanban(rbp
);
1012 if (rbp
->expiry
&& (rbp
->expiry
< now
)) {
1014 /* Remove ban if it's on the channel (localdosetmode_ban will silently
1015 * skip bans that don't exist) */
1016 localdosetmode_ban(&changes
, bantostring(rbp
->cbp
), MCB_DEL
);
1017 /* Remove from database */
1018 csdb_deleteban(rbp
);
1019 /* Remove from list */
1021 /* Free ban/string and update setby refcount, and free actual regban */
1022 freesstring(rbp
->reason
);
1023 freechanban(rbp
->cbp
);
1026 if (rbp
->expiry
&& (!nextsched
|| rbp
->expiry
<= nextsched
)) {
1027 nextsched
=rbp
->expiry
;
1035 rcp
->checksched
=scheduleoneshot(nextsched
, cs_timerfunc
, arg
);
1037 localsetmodeflush(&changes
, 1);
1040 void cs_removechannel(regchan
*rcp
) {
1043 regchanuser
*rcup
, *nrcup
;
1048 for (i
=0;i
<REGCHANUSERHASHSIZE
;i
++) {
1049 for (rcup
=rcp
->regusers
[i
];rcup
;rcup
=nrcup
) {
1050 nrcup
=rcup
->nextbychan
;
1051 delreguserfromchannel(rcp
, rcup
->user
);
1052 freeregchanuser(rcup
);
1056 for (rbp
=rcp
->bans
;rbp
;rbp
=nrbp
) {
1058 freesstring(rbp
->reason
);
1059 freechanban(rbp
->cbp
);
1065 if (rcp
->checksched
)
1066 deleteschedule(rcp
->checksched
, cs_timerfunc
, rcp
->index
);
1069 rcp
->flags
=QCFLAG_SUSPENDED
;
1070 chanservjoinchan(cip
->channel
); /* Force off the channel */
1073 csdb_deletechannel(rcp
);
1075 freesstring(rcp
->welcome
);
1076 freesstring(rcp
->topic
);
1077 freesstring(rcp
->key
);
1078 freesstring(rcp
->suspendreason
);
1079 freesstring(rcp
->comment
);
1083 cip
->exts
[chanservext
]=NULL
;
1084 releasechanindex(cip
);
1087 /* Sender is who the DELCHAN is attributed to.. */
1088 int cs_removechannelifempty(nick
*sender
, regchan
*rcp
) {
1092 for (i
=0;i
<REGCHANUSERHASHSIZE
;i
++) {
1093 for (rcup
=rcp
->regusers
[i
];rcup
;rcup
=rcup
->nextbychan
) {
1094 if (rcup
->flags
& ~(QCUFLAGS_PUNISH
))
1099 cs_log(sender
,"DELCHAN %s (Empty)",rcp
->index
->name
->content
);
1100 cs_removechannel(rcp
);
1105 void cs_removeuser(reguser
*rup
) {
1106 regchanuser
*rcup
, *nrcup
;
1108 struct authname
*anp
;
1110 /* Remove the user from all its channels */
1111 for (rcup
=rup
->knownon
;rcup
;rcup
=nrcup
) {
1112 nrcup
=rcup
->nextbyuser
;
1115 delreguserfromchannel(rcp
, rup
);
1116 cs_removechannelifempty(NULL
, rcp
);
1120 delreguserfrommaildomain(rup
,rup
->domain
);
1121 freesstring(rup
->localpart
);
1122 freesstring(rup
->email
);
1123 freesstring(rup
->lastemail
);
1124 freesstring(rup
->lastuserhost
);
1125 freesstring(rup
->suspendreason
);
1126 freesstring(rup
->comment
);
1127 freesstring(rup
->info
);
1129 csdb_deleteuser(rup
);
1131 removereguserfromhash(rup
);
1133 if ((anp
=findauthname(rup
->ID
)) && anp
->nicks
) {
1134 rup
->status
|= QUSTAT_DEAD
;
1140 int cs_bancheck(nick
*np
, channel
*cp
) {
1141 time_t now
=time(NULL
);
1143 modechanges changes
;
1146 if (!(rcp
=cp
->index
->exts
[chanservext
]))
1149 for (rbh
=&(rcp
->bans
);*rbh
;) {
1150 if ((*rbh
)->expiry
&& ((*rbh
)->expiry
< now
)) {
1152 csdb_deleteban(*rbh
);
1156 freesstring(rbp
->reason
);
1157 freechanban(rbp
->cbp
);
1159 } else if (nickmatchban_visible(np
,(*rbh
)->cbp
)) {
1160 /* This user matches this ban.. */
1161 if (!nickbanned_visible(np
,cp
)) {
1162 /* Only bother putting the ban on the channel if they're not banned already */
1163 /* (might be covered by this ban or a different one.. doesn't really matter */
1164 localsetmodeinit(&changes
, cp
, chanservnick
);
1165 localdosetmode_ban(&changes
, bantostring((*rbh
)->cbp
), MCB_ADD
);
1166 localsetmodeflush(&changes
, 1);
1167 cs_timerfunc(cp
->index
);
1169 localkickuser(chanservnick
, cp
, np
, (*rbh
)->reason
? (*rbh
)->reason
->content
: "Banned.");
1172 rbh
=&((*rbh
)->next
);
1179 void cs_setregban(chanindex
*cip
, regban
*rbp
) {
1180 modechanges changes
;
1187 localsetmodeinit(&changes
, cip
->channel
, chanservnick
);
1188 localdosetmode_ban(&changes
, bantostring(rbp
->cbp
), MCB_ADD
);
1189 localsetmodeflush(&changes
, 1);
1191 for (i
=0;(cip
->channel
) && i
<cip
->channel
->users
->hashsize
;i
++) {
1192 if (cip
->channel
->users
->content
[i
]!=nouser
&&
1193 (np
=getnickbynumeric(cip
->channel
->users
->content
[i
])) &&
1194 !IsService(np
) && !IsOper(np
) && !IsXOper(np
) &&
1195 nickmatchban_visible(np
, rbp
->cbp
))
1196 localkickuser(chanservnick
, cip
->channel
, np
, rbp
->reason
? rbp
->reason
->content
: "Banned.");
1202 void cs_banuser(modechanges
*changes
, chanindex
*cip
, nick
*np
, const char *reason
) {
1203 modechanges lchanges
;
1204 char banmask
[NICKLEN
+USERLEN
+HOSTLEN
+3];
1209 if (nickbanned_visible(np
, cip
->channel
)) {
1210 localkickuser(chanservnick
, cip
->channel
, np
, reason
?reason
:"Banned.");
1214 if (IsAccount(np
)) {
1215 /* Ban their account.. */
1216 sprintf(banmask
,"*!*@%s.%s",np
->authname
,HIS_HIDDENHOST
);
1217 } else if (IsSetHost(np
)) {
1218 /* sethosted: ban ident@host */
1219 sprintf(banmask
,"*!%s@%s",np
->shident
->content
,np
->sethost
->content
);
1220 } else if (np
->host
->clonecount
>3) {
1221 /* >3 clones, ban user@host */
1222 sprintf(banmask
,"*!%s@%s",np
->ident
,np
->host
->name
->content
);
1224 sprintf(banmask
,"*!*@%s",np
->host
->name
->content
);
1228 localsetmodeinit(&lchanges
, cip
->channel
, chanservnick
);
1229 localdosetmode_ban(&lchanges
, banmask
, MCB_ADD
);
1230 localsetmodeflush(&lchanges
, 1);
1232 localdosetmode_ban(changes
, banmask
, MCB_ADD
);
1235 localkickuser(chanservnick
, cip
->channel
, np
, reason
?reason
:"Banned.");
1239 * cs_sanitisechanlev: Removes impossible combinations from chanlev flags.
1240 * chanservuser.c is probably not the right file for this, but nowhere better
1241 * presented itself...
1243 flag_t
cs_sanitisechanlev(flag_t flags
) {
1244 /* +m or +n cannot have any "punishment" flags */
1245 if (flags
& (QCUFLAG_MASTER
| QCUFLAG_OWNER
))
1246 flags
&= ~(QCUFLAGS_PUNISH
);
1248 /* +d can't be +o */
1249 if (flags
& QCUFLAG_DENY
)
1250 flags
&= ~QCUFLAG_OP
;
1252 /* +q can't be +v */
1253 if (flags
& QCUFLAG_QUIET
)
1254 flags
&= ~QCUFLAG_VOICE
;
1256 /* +p trumps +a and +g */
1257 if (flags
& QCUFLAG_PROTECT
)
1258 flags
&= ~(QCUFLAG_AUTOOP
| QCUFLAG_AUTOVOICE
);
1260 /* -o can't be +a */
1261 if (!(flags
& QCUFLAG_OP
))
1262 flags
&= ~QCUFLAG_AUTOOP
;
1264 /* +a or -v can't be +g. +a implies +o at this stage (see above) */
1265 if (!(flags
& QCUFLAG_VOICE
) || (flags
& QCUFLAG_AUTOOP
))
1266 flags
&= ~QCUFLAG_AUTOVOICE
;
1268 /* +p requires +o or +v */
1269 if (!(flags
& (QCUFLAG_VOICE
| QCUFLAG_OP
)))
1270 flags
&= ~QCUFLAG_PROTECT
;
1272 /* The personal flags require one of +mnovk, as does +t */
1273 if (!(flags
& (QCUFLAG_OWNER
| QCUFLAG_MASTER
| QCUFLAG_OP
| QCUFLAG_VOICE
| QCUFLAG_KNOWN
)))
1274 flags
&= ~(QCUFLAGS_PERSONAL
| QCUFLAG_TOPIC
);
1281 * This function does the standard "nick or #user" lookup.
1282 * If "sender" is not NULL, a suitable error message will
1283 * be sent if the lookup fails.
1284 * "sender" MUST be sent when a user is requesting a lookup
1285 * as there is some policy code here.
1288 reguser
*findreguser(nick
*sender
, const char *str
) {
1289 reguser
*rup
, *vrup
= getreguserfromnick(sender
);;
1298 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1301 if (!(rup
=findreguserbynick(str
+1)) && sender
)
1302 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1303 } else if (*str
=='&' && vrup
&& UHasStaffPriv(vrup
)) {
1306 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1309 if (!(rup
=findreguserbyID(atoi(str
+1))) && sender
)
1310 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1312 if (!(np
=getnickbynick(str
))) {
1314 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1317 if (!(rup
=getreguserfromnick(np
)) && sender
)
1318 chanservstdmessage(sender
, QM_USERNOTAUTHED
, str
);
1321 /* removed the suspended check from here, I don't see the point... */
1322 if (rup
&& (rup
->status
& QUSTAT_DEAD
)) {
1323 chanservstdmessage(sender
, QM_USERHASBADAUTH
, rup
->username
);
1331 * Unbans a mask from a channel, including permbans if user has correct privs.
1333 * Return 0 if it works, 1 if it don't.
1335 int cs_unbanfn(nick
*sender
, chanindex
*cip
, UnbanFN fn
, void *arg
, int removepermbans
, int abortonfailure
) {
1337 chanban
**cbh
, *cbp
;
1339 modechanges changes
;
1342 rcp
=cip
->exts
[chanservext
];
1345 localsetmodeinit(&changes
, cip
->channel
, chanservnick
);
1347 for (rbh
=&(rcp
->bans
); *rbh
; ) {
1349 if (fn(arg
, rbp
->cbp
)) {
1350 banstr
=bantostring(rbp
->cbp
);
1351 /* Check perms and remove */
1352 if(!removepermbans
) {
1353 chanservstdmessage(sender
, QM_WARNNOTREMOVEDPERMBAN
, banstr
, cip
->name
->content
);
1355 } else if (!cs_checkaccess(sender
, NULL
, CA_MASTERPRIV
, cip
, NULL
, 0, 1)) {
1356 chanservstdmessage(sender
, QM_NOTREMOVEDPERMBAN
, banstr
, cip
->name
->content
);
1357 if (abortonfailure
) return 1; /* Just give up... */
1360 chanservstdmessage(sender
, QM_REMOVEDPERMBAN
, banstr
, cip
->name
->content
);
1362 localdosetmode_ban(&changes
, banstr
, MCB_DEL
);
1363 /* Remove from database */
1364 csdb_deleteban(rbp
);
1365 /* Remove from list */
1367 /* Free ban/string and update setby refcount, and free actual regban */
1368 freesstring(rbp
->reason
);
1369 freechanban(rbp
->cbp
);
1378 for (cbh
=&(cip
->channel
->bans
); *cbh
; ) {
1382 banstr
=bantostring(cbp
);
1383 chanservstdmessage(sender
, QM_REMOVEDCHANBAN
, banstr
, cip
->name
->content
);
1384 localdosetmode_ban(&changes
, banstr
, MCB_DEL
);
1389 localsetmodeflush(&changes
,1);
1395 /* Add entry to channel op history when someone gets ops. */
1396 void cs_logchanop(regchan
*rcp
, char *nick
, reguser
*rup
) {
1397 strncpy(rcp
->chanopnicks
[rcp
->chanoppos
], nick
, NICKLEN
);
1398 rcp
->chanopnicks
[rcp
->chanoppos
][NICKLEN
]='\0';
1399 rcp
->chanopaccts
[rcp
->chanoppos
]=rup
->ID
;
1400 rcp
->chanoppos
=(rcp
->chanoppos
+1)%CHANOPHISTORY
;
1403 int checkreason(nick
*np
, char *reason
) {
1404 if((strlen(reason
) < MIN_REASONLEN
) || !strchr(reason
, ' ')) {
1405 chanservstdmessage(np
, QM_REASONREQUIRED
);