]>
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 chanservpartchan(channel
*cp
, char *reason
) {
247 /* Sanity check that we exist and are on the channel.
249 * Note that we don't do any of the other usual sanity checks here; if
250 * this channel is unregged or suspended or whatever then all the more
251 * reason to get Q off it ASAP! */
252 if (!chanservnick
|| !cp
|| !cp
->users
|| !getnumerichandlefromchanhash(cp
->users
, chanservnick
->numeric
))
255 localpartchannel(chanservnick
, cp
, reason
);
258 void chanservjoinchan(channel
*cp
) {
266 /* Skip unregistered channels */
267 if (!(rcp
=cp
->index
->exts
[chanservext
]))
270 /* Check for a new timestamp. But don't do anything stupid if the incoming timestamp is 0 */
271 if (cp
->timestamp
&& ((!rcp
->ltimestamp
) || (cp
->timestamp
< rcp
->ltimestamp
))) {
272 rcp
->ltimestamp
=cp
->timestamp
;
273 csdb_updatechanneltimestamp(rcp
);
276 /* We won't be doing any joining or parting if we're not online */
280 /* In gerenal this function shouldn't be used any more as a reason to get
281 * Q to leave, but if it should be leaving then just part with no reason. */
282 if ((CIsSuspended(rcp
) || !CIsJoined(rcp
)) && getnumerichandlefromchanhash(cp
->users
, chanservnick
->numeric
)) {
283 localpartchannel(chanservnick
, cp
, NULL
);
286 /* Right, we are definately going to either join the channel or at least
287 * burst some modes onto it.
289 * We will try and burst our view of the world; if the timestamps are
290 * actually equal this will be mostly ignored and we will have to fix it
291 * up later. For modes we use the forced modes, plus the default channel
292 * modes (unless any of those are explicitly denied) */
294 /* By default, we set the forcemodes and the default modes, but never denymodes..
295 * OK, actually we should only set the default modes if our timestamp is older.*/
296 if (cp
->timestamp
> rcp
->ltimestamp
)
297 themodes
|= CHANMODE_DEFAULT
;
301 themodes
= (themodes
| rcp
->forcemodes
) & ~rcp
->denymodes
;
303 /* Now, if someone has just created a channel and we are going to set +i
304 * or +k on it, this will kick them off. This could be construed as a
305 * bit rude if it's their channel, so if there is only one person on the
306 * channel and they have a right to be there, burst with default modes
307 * only to avoid them being netrider kicked.
309 if (cp
->users
->totalusers
==1) {
310 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
311 if (cp
->users
->content
[i
] != nouser
) {
312 if ((np
=getnickbynumeric(cp
->users
->content
[i
]&CU_NUMERICMASK
)) &&
313 (rup
=getreguserfromnick(np
)) &&
314 (rcup
=findreguseronchannel(rcp
,rup
)) &&
316 /* OK, there was one user, and they are known on this channel.
317 * Don't burst with +i or +k */
318 themodes
&= ~(CHANMODE_INVITEONLY
| CHANMODE_KEY
);
324 /* We should be on the channel - join with our nick */
325 if (!CIsSuspended(rcp
) && CIsJoined(rcp
) && !getnumerichandlefromchanhash(cp
->users
, chanservnick
->numeric
)) {
326 /* If we pass the key parameter here but are not setting +k (see above)
327 * then localburstontochannel() will ignore the key */
328 localburstontochannel(cp
, chanservnick
, rcp
->ltimestamp
, themodes
,
329 rcp
->limit
, (rcp
->key
)?rcp
->key
->content
:NULL
);
332 /* We're not joining the channel - send the burst with no nick */
333 if (!CIsSuspended(rcp
) && !CIsJoined(rcp
) && (cp
->timestamp
> rcp
->ltimestamp
)) {
334 localburstontochannel(cp
, NULL
, rcp
->ltimestamp
, themodes
,
335 rcp
->limit
, (rcp
->key
)?rcp
->key
->content
:NULL
);
339 void chanservstdmessage(nick
*np
, int messageid
, ... ) {
346 char *message
, *messageargs
;
350 if (getreguserfromnick(np
) == NULL
) {
354 rup
=getreguserfromnick(np
);
360 language
=rup
->languageid
;
363 if (csmessages
[language
][messageid
]) {
364 message
=csmessages
[language
][messageid
]->content
;
365 } else if (csmessages
[0][messageid
]) {
366 message
=csmessages
[0][messageid
]->content
;
368 message
=defaultmessages
[messageid
*2];
371 messageargs
=defaultmessages
[messageid
*2+1];
373 va_start(va
,messageid
);
375 q9vsnprintf(buf
,5000,message
,messageargs
,va
);
380 for (bp
=buf
; ;bp
++) {
381 /* We send something if we hit a \n, fill the buffer or run out of source */
382 if (*bp
=='\n' || len
>490 || !*bp
) {
383 if (*bp
&& *bp
!='\n') {
389 if (chanservnick
&& *buf2
) {
391 sendnoticetouser(chanservnick
,np
,"%s",buf2
);
393 sendmessagetouser(chanservnick
,np
,"%s",buf2
);
397 /* If we ran out of buffer, get out here */
409 /* Special case: If it's a "not enough parameters" message, show the first line of help */
410 if (messageid
==QM_NOTENOUGHPARAMS
) {
411 char *command
=va_arg(va2
, char *);
412 cs_sendhelp(np
, command
, 1);
413 chanservstdmessage(np
, QM_TYPEHELPFORHELP
, command
);
419 void chanservsendmessage_real(nick
*np
, int oneline
, char *message
, ... ) {
420 char buf
[5010]; /* Very large buffer.. */
421 char buf2
[512], *bp
, *bp2
;
428 va_start(va
,message
);
429 vsnprintf(buf
,5000,message
,va
);
432 if (getreguserfromnick(np
) == NULL
) {
435 rup
=getreguserfromnick(np
);
445 for (bp
=buf
; ;bp
++) {
446 /* We send something if we hit a \n, fill the buffer or run out of source */
447 if (*bp
=='\n' || len
>490 || !*bp
) {
448 if (*bp
&& *bp
!='\n') {
454 if (chanservnick
&& *buf2
) {
456 sendnoticetouser(chanservnick
,np
,"%s",buf2
);
458 sendmessagetouser(chanservnick
,np
,"%s",buf2
);
462 /* If we ran out of buffer, get out here */
463 if (!*bp
|| (*bp
=='\n' && oneline
))
475 void chanservwallmessage(char *message
, ... ) {
476 char buf
[5010]; /* Very large buffer.. */
481 va_start(va
,message
);
482 vsnprintf(buf
,5000,message
,va
);
486 for (i
=0;i
<NICKHASHSIZE
;i
++) {
487 for (np
=nicktable
[i
];np
;np
=np
->next
) {
488 if (!IsOper(np
)) /* optimisation, if VIEWWALLMESSAGE changes change this */
491 if (!cs_privcheck(QPRIV_VIEWWALLMESSAGE
, np
))
494 chanservsendmessage(np
, "%s", buf
);
499 void chanservkillstdmessage(nick
*target
, int messageid
, ... ) {
502 char *message
, *messageargs
;
506 if (!(rup
=getreguserfromnick(target
)))
509 language
=rup
->languageid
;
511 if (csmessages
[language
][messageid
])
512 message
=csmessages
[language
][messageid
]->content
;
513 else if (csmessages
[0][messageid
])
514 message
=csmessages
[0][messageid
]->content
;
516 message
=defaultmessages
[messageid
*2];
518 messageargs
=defaultmessages
[messageid
*2+1];
519 va_start(va
, messageid
);
520 q9vsnprintf(buf
, 511, message
, messageargs
, va
);
522 killuser(chanservnick
, target
, buf
);
525 int checkpassword(reguser
*rup
, const char *pass
) {
526 if (!(*rup
->password
))
529 if (!strncmp(rup
->password
, pass
, PASSLEN
))
534 int checkresponse(reguser
*rup
, const unsigned char *entropy
, const char *response
, CRAlgorithm algorithm
) {
535 char usernamel
[NICKLEN
+1], *dp
, *up
;
537 if (!(*rup
->password
))
540 for(up
=rup
->username
,dp
=usernamel
;*up
;)
541 *dp
++ = ToLower(*up
++);
544 return algorithm(usernamel
, rup
->password
, cs_calcchallenge(entropy
), response
);
547 int checkhashpass(reguser
*rup
, const char *junk
, const char *hash
) {
548 char usernamel
[NICKLEN
+1], *dp
, *up
;
550 for(up
=rup
->username
,dp
=usernamel
;*up
;)
551 *dp
++ = ToLower(*up
++);
554 return cs_checkhashpass(usernamel
, rup
->password
, junk
, hash
);
557 int setpassword(reguser
*rup
, const char *pass
) {
558 strncpy(rup
->password
, pass
, PASSLEN
);
559 rup
->password
[PASSLEN
]='\0';
563 void cs_checknick(nick
*np
) {
566 char userhost
[USERLEN
+HOSTLEN
+3];
568 if (!(aup
=getactiveuserfromnick(np
))) {
570 np
->exts
[chanservnext
]=aup
;
573 assert(getactiveuserfromnick(np
));
575 if (IsAccount(np
) && np
->auth
) {
576 if (np
->auth
->exts
[chanservaext
]) {
577 rup
=getreguserfromnick(np
);
581 if (UIsDelayedGline(rup
)) {
582 /* delayed-gline - schedule the user's squelching */
583 deleteschedule(NULL
, &chanservdgline
, (void*)rup
); /* icky, but necessary unless we stick more stuff in reguser structure */
584 scheduleoneshot(time(NULL
)+rand()%900
, &chanservdgline
, (void*)rup
);
585 } else if (UIsGline(rup
)) {
586 /* instant-gline - lets be lazy and set a schedule expiring now :) */
587 deleteschedule(NULL
, &chanservdgline
, (void*)rup
); /* icky, but necessary unless we stick more stuff in reguser structure */
588 scheduleoneshot(time(NULL
), &chanservdgline
, (void*)rup
);
589 } else if(UHasSuspension(rup
)) {
590 chanservkillstdmessage(np
, QM_SUSPENDKILL
);
594 cs_doallautomodes(np
);
596 /* Auto create user.. */
599 rup
->ID
=np
->auth
->userid
;
600 if (rup
->ID
> lastuserID
)
602 strncpy(rup
->username
,np
->authname
,NICKLEN
); rup
->username
[NICKLEN
]='\0';
603 rup
->created
=time(NULL
);
605 rup
->lastemailchange
=0;
606 rup
->lastpasschange
=0;
607 rup
->flags
=QUFLAG_NOTICE
;
613 rup
->password
[0]='\0';
619 sprintf(userhost
,"%s@%s",np
->ident
,np
->host
->name
->content
);
620 rup
->lastuserhost
=getsstring(userhost
,USERLEN
+HOSTLEN
+1);
621 rup
->suspendreason
=NULL
;
627 addregusertohash(rup
);
629 csdb_createuser(rup
);
633 cs_checknickbans(np
);
636 void cs_checkchanmodes(channel
*cp
) {
639 localsetmodeinit (&changes
, cp
, chanservnick
);
640 cs_docheckchanmodes(cp
, &changes
);
641 localsetmodeflush (&changes
, 1);
644 void cs_docheckchanmodes(channel
*cp
, modechanges
*changes
) {
650 if ((rcp
=cp
->index
->exts
[chanservext
])==NULL
|| CIsSuspended(rcp
))
653 /* Take care of the simple modes */
654 localdosetmode_simple(changes
, rcp
->forcemodes
& ~(cp
->flags
), rcp
->denymodes
& cp
->flags
);
656 /* Check for forced key. Note that we ALWAYS call localdosetmode_key
657 * in case the wrong key is set atm */
658 if (rcp
->forcemodes
& CHANMODE_KEY
) {
660 Error("chanserv",ERR_WARNING
,"Key forced but no key specified for %s: cleared forcemode.",rcp
->index
->name
->content
);
661 rcp
->forcemodes
&= ~CHANMODE_KEY
;
662 csdb_updatechannel(rcp
);
664 localdosetmode_key(changes
, rcp
->key
->content
, MCB_ADD
);
668 /* Check for denied key */
669 if ((rcp
->denymodes
& CHANMODE_KEY
) && IsKey(cp
)) {
670 localdosetmode_key(changes
, NULL
, MCB_DEL
);
673 /* Check for forced limit. Always call in case of wrong limit */
674 if (rcp
->forcemodes
& CHANMODE_LIMIT
) {
675 localdosetmode_limit(changes
, rcp
->limit
, MCB_ADD
);
679 /* Slightly misnamed function that checks each USER on the channel against channel
680 * settings. Possible actions are opping/deopping, voicing/devoicing, and banning
681 * for chanflag +k or chanlev flag +b */
682 void cs_docheckopvoice(channel
*cp
, modechanges
*changes
) {
692 if ((rcp
=cp
->index
->exts
[chanservext
])==NULL
|| CIsSuspended(rcp
))
695 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
696 if (cp
->users
->content
[i
]==nouser
)
699 if ((np
=getnickbynumeric(cp
->users
->content
[i
]))==NULL
) {
700 Error("chanserv",ERR_ERROR
,"Found non-existent numeric %lu on channel %s",cp
->users
->content
[i
],
701 cp
->index
->name
->content
);
705 if ((rup
=getreguserfromnick(np
)))
706 rcup
=findreguseronchannel(rcp
,rup
);
710 /* Various things that might ban the user - don't apply these to +o, +k or +X users */
711 if (!IsService(np
) && !IsXOper(np
) && !IsOper(np
)) {
712 if (rcup
&& CUIsBanned(rcup
)) {
713 cs_banuser(changes
, cp
->index
, np
, NULL
);
717 /* chanflag +k checks; kick them if they "obviously" can't rejoin without a ban */
718 if (CIsKnownOnly(rcp
) && !(rcup
&& CUKnown(rcup
))) {
719 if (IsInviteOnly(cp
) || (IsRegOnly(cp
) && !IsAccount(np
))) {
720 localkickuser(chanservnick
,cp
,np
,"Authorised users only.");
722 cs_banuser(NULL
, cp
->index
, np
, "Authorised users only.");
728 if ((cp
->users
->content
[i
] & CUMODE_OP
) && !IsService(np
)) {
729 if ((CIsBitch(rcp
) && (!rcup
|| !CUHasOpPriv(rcup
))) ||
730 (rcup
&& CUIsDeny(rcup
)))
731 localdosetmode_nick(changes
, np
, MC_DEOP
);
733 if (rcup
&& (CUIsProtect(rcup
) || CIsProtect(rcp
)) && CUIsOp(rcup
) && !CUIsDeny(rcup
)) {
734 localdosetmode_nick(changes
, np
, MC_OP
);
735 cs_logchanop(rcp
, np
->nick
, rcup
->user
);
739 if (cp
->users
->content
[i
] & CUMODE_VOICE
) {
740 if (rcup
&& CUIsQuiet(rcup
))
741 localdosetmode_nick(changes
, np
, MC_DEVOICE
);
743 if (rcup
&& (CUIsProtect(rcup
) || CIsProtect(rcp
)) && CUIsVoice(rcup
) && !CUIsQuiet(rcup
) && !(cp
->users
->content
[i
] & CUMODE_OP
))
744 localdosetmode_nick(changes
, np
, MC_VOICE
);
749 void cs_doallautomodes(nick
*np
) {
755 if (!(rup
=getreguserfromnick(np
)))
758 for (rcup
=rup
->knownon
;rcup
;rcup
=rcup
->nextbyuser
) {
759 /* Skip suspended channels */
760 if (CIsSuspended(rcup
->chan
))
763 if (rcup
->chan
->index
->channel
) {
765 if ((lp
=getnumerichandlefromchanhash(rcup
->chan
->index
->channel
->users
, np
->numeric
))) {
766 /* User is on channel.. */
768 if (CUKnown(rcup
) && rcup
->chan
->index
->channel
->users
->totalusers
>= 3) {
769 /* This meets the channel use criteria, update. */
770 rcup
->chan
->lastactive
=time(NULL
);
772 /* Don't spam the DB though for channels with lots of joins */
773 if (rcup
->chan
->lastcountersync
< (time(NULL
) - COUNTERSYNCINTERVAL
)) {
774 csdb_updatechannelcounters(rcup
->chan
);
775 rcup
->chan
->lastcountersync
=time(NULL
);
779 /* Update last use time */
780 rcup
->usetime
=getnettime();
782 localsetmodeinit(&changes
, rcup
->chan
->index
->channel
, chanservnick
);
783 if (*lp
& CUMODE_OP
) {
784 if (!IsService(np
) && (CUIsDeny(rcup
) || (CIsBitch(rcup
->chan
) && !CUHasOpPriv(rcup
))))
785 localdosetmode_nick(&changes
, np
, MC_DEOP
);
787 if (!CUIsDeny(rcup
) && CUIsOp(rcup
) &&
788 (CUIsProtect(rcup
) || CIsProtect(rcup
->chan
) || CUIsAutoOp(rcup
))) {
789 localdosetmode_nick(&changes
, np
, MC_OP
);
790 cs_logchanop(rcup
->chan
, np
->nick
, rup
);
794 if (*lp
& CUMODE_VOICE
) {
796 localdosetmode_nick(&changes
, np
, MC_DEVOICE
);
798 if (!CUIsQuiet(rcup
) && CUIsVoice(rcup
) && !(*lp
& CUMODE_OP
) &&
799 (CUIsProtect(rcup
) || CIsProtect(rcup
->chan
) || CUIsAutoVoice(rcup
)))
800 localdosetmode_nick(&changes
, np
, MC_VOICE
);
802 localsetmodeflush(&changes
, 1);
804 /* Channel exists but user is not joined: invite if they are +j-b */
805 if (CUIsAutoInvite(rcup
) && CUKnown(rcup
) && !CUIsBanned(rcup
)) {
806 localinvite(chanservnick
, rcup
->chan
->index
->channel
, np
);
813 void cs_checknickbans(nick
*np
) {
818 if (IsService(np
) || IsOper(np
) || IsXOper(np
))
821 /* Avoid races: memcpy the channel array */
822 i
=np
->channels
->cursi
;
823 ca
=(channel
**)malloc(i
*sizeof(channel
*));
824 memcpy(ca
, np
->channels
->content
,i
*sizeof(channel
*));
827 if ((rcp
=ca
[j
]->index
->exts
[chanservext
]) && !CIsSuspended(rcp
) &&
828 CIsEnforce(rcp
) && nickbanned_visible(np
, ca
[j
]))
829 localkickuser(chanservnick
, ca
[j
], np
, "Banned.");
835 void cs_checkbans(channel
*cp
) {
847 if ((rcp
=cp
->index
->exts
[chanservext
])==NULL
|| CIsSuspended(rcp
))
852 localsetmodeinit(&changes
, cp
, chanservnick
);
854 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
855 if (cp
->users
->content
[i
]==nouser
)
858 if ((np
=getnickbynumeric(cp
->users
->content
[i
]))==NULL
) {
859 Error("chanserv",ERR_ERROR
,"Found numeric %lu on channel %s who doesn't exist.",
860 cp
->users
->content
[i
], cp
->index
->name
->content
);
864 if (IsService(np
) || IsOper(np
) || IsXOper(np
))
867 for (rbp
=rcp
->bans
;rbp
;rbp
=rbp
->next
) {
868 if (((!rbp
->expiry
) || (rbp
->expiry
<= now
)) &&
869 nickmatchban_visible(np
, rbp
->cbp
)) {
870 if (!nickbanned_visible(np
, cp
)) {
871 localdosetmode_ban(&changes
, bantostring(rbp
->cbp
), MCB_ADD
);
873 localkickuser(chanservnick
,cp
,np
,rbp
->reason
?rbp
->reason
->content
:"Banned.");
878 if (rbp
) /* If we broke out of above loop (due to kick) rbp will be set.. */
881 if (CIsEnforce(rcp
)) {
882 for (cbp
=cp
->bans
;cbp
;cbp
=cbp
->next
) {
883 if ((cbp
->timeset
>=rcp
->lastbancheck
) && nickmatchban_visible(np
, cbp
))
884 localkickuser(chanservnick
,cp
,np
,"Banned.");
886 rcp
->lastbancheck
=time(NULL
);
890 localsetmodeflush(&changes
,1);
895 * This function schedules an update check on a channel
897 void cs_schedupdate(chanindex
*cip
, int mintime
, int maxtime
) {
898 int delay
=mintime
+(rand()%(maxtime
-mintime
));
901 if (!(rcp
=cip
->exts
[chanservext
]) || CIsSuspended(rcp
))
904 if (rcp
->checksched
) {
905 deleteschedule(rcp
->checksched
, cs_timerfunc
, cip
);
908 rcp
->checksched
=scheduleoneshot(time(NULL
)+delay
, cs_timerfunc
, cip
);
913 * This function does several things:
914 * * Updates auto-limit and/or removes bans as necessary
915 * * Schedules the next timed call
916 * * Does any pending op/ban/mode checks
917 * It is safe to use either as a target for a schedule() call, or to call
918 * directly when parameters change (e.g. autolimit settings or ban expire
920 * To force a limit update, set rcp->limit to 0..
923 void cs_timerfunc(void *arg
) {
925 channel
*cp
=cip
->channel
;
930 time_t now
=time(NULL
);
933 if (!(rcp
=cip
->exts
[chanservext
]))
938 /* Always nullify the checksched even if the channel is suspended.. */
939 if (rcp
->checksched
) {
940 deleteschedule(rcp
->checksched
, cs_timerfunc
, arg
);
941 rcp
->checksched
=NULL
;
944 if (!cp
|| CIsSuspended(rcp
))
947 /* Check if we need to leave the channel first */
948 if (chanservnick
&& CIsJoined(rcp
) && cip
->channel
&&
949 cip
->channel
->users
->totalusers
==1 &&
950 getnumerichandlefromchanhash(cip
->channel
->users
, chanservnick
->numeric
)) {
952 /* Only chanserv left in this channel */
953 if (now
>= (rcp
->lastpart
+ LINGERTIME
)) {
955 localpartchannel(chanservnick
, cip
->channel
, "Empty Channel");
958 if (!nextsched
|| nextsched
> (rcp
->lastpart
+ LINGERTIME
))
959 nextsched
=rcp
->lastpart
+LINGERTIME
;
963 localsetmodeinit(&changes
, cp
, chanservnick
);
965 if (CIsAutoLimit(rcp
)) {
966 if (!rcp
->limit
|| rcp
->autoupdate
<= now
) {
967 /* Only update the limit if there are <= (autolimit/2) or
968 * >= (autolimit * 1.5) slots free */
970 if ((cp
->users
->totalusers
>= (rcp
->limit
- rcp
->autolimit
/2)) ||
971 (cp
->users
->totalusers
<= (rcp
->limit
- (3 * rcp
->autolimit
)/2))) {
972 rcp
->limit
=cp
->users
->totalusers
+rcp
->autolimit
;
973 rcp
->status
|= QCSTAT_MODECHECK
;
976 /* And set the schedule for the next update */
977 rcp
->autoupdate
= now
+ AUTOLIMIT_INTERVAL
;
980 if (!nextsched
|| rcp
->autoupdate
<= nextsched
)
981 nextsched
=rcp
->autoupdate
;
984 if (rcp
->status
& QCSTAT_OPCHECK
) {
985 rcp
->status
&= ~QCSTAT_OPCHECK
;
986 cs_docheckopvoice(cp
, &changes
);
989 if (rcp
->status
& QCSTAT_MODECHECK
) {
990 rcp
->status
&= ~QCSTAT_MODECHECK
;
991 cs_docheckchanmodes(cp
, &changes
);
994 if (rcp
->status
& QCSTAT_BANCHECK
) {
995 rcp
->status
&= ~QCSTAT_BANCHECK
;
999 /* This ban check is AFTER docheckopvoice in case checkopvoice set a
1000 * ban which we need to automatically remove later.. */
1002 if (rcp
->banduration
) {
1003 for (cbp
=cp
->bans
;cbp
;cbp
=ncbp
) {
1005 if (cbp
->timeset
+rcp
->banduration
<=now
) {
1006 localdosetmode_ban(&changes
, bantostring(cbp
), MCB_DEL
);
1008 if (!nextsched
|| cbp
->timeset
+rcp
->banduration
<= nextsched
) {
1009 nextsched
=rcp
->banduration
+cbp
->timeset
;
1015 /* Check for expiry of registered bans */
1017 for (rbh
=&(rcp
->bans
);*rbh
;) {
1019 verifyregchanban(rbp
);
1020 if (rbp
->expiry
&& (rbp
->expiry
< now
)) {
1022 /* Remove ban if it's on the channel (localdosetmode_ban will silently
1023 * skip bans that don't exist) */
1024 localdosetmode_ban(&changes
, bantostring(rbp
->cbp
), MCB_DEL
);
1025 /* Remove from database */
1026 csdb_deleteban(rbp
);
1027 /* Remove from list */
1029 /* Free ban/string and update setby refcount, and free actual regban */
1030 freesstring(rbp
->reason
);
1031 freechanban(rbp
->cbp
);
1034 if (rbp
->expiry
&& (!nextsched
|| rbp
->expiry
<= nextsched
)) {
1035 nextsched
=rbp
->expiry
;
1043 rcp
->checksched
=scheduleoneshot(nextsched
, cs_timerfunc
, arg
);
1045 localsetmodeflush(&changes
, 1);
1048 void cs_removechannel(regchan
*rcp
, char *reason
) {
1051 regchanuser
*rcup
, *nrcup
;
1056 for (i
=0;i
<REGCHANUSERHASHSIZE
;i
++) {
1057 for (rcup
=rcp
->regusers
[i
];rcup
;rcup
=nrcup
) {
1058 nrcup
=rcup
->nextbychan
;
1059 delreguserfromchannel(rcp
, rcup
->user
);
1060 freeregchanuser(rcup
);
1064 for (rbp
=rcp
->bans
;rbp
;rbp
=nrbp
) {
1066 freesstring(rbp
->reason
);
1067 freechanban(rbp
->cbp
);
1073 if (rcp
->checksched
)
1074 deleteschedule(rcp
->checksched
, cs_timerfunc
, rcp
->index
);
1077 chanservpartchan(cip
->channel
, reason
);
1080 csdb_deletechannel(rcp
);
1082 freesstring(rcp
->welcome
);
1083 freesstring(rcp
->topic
);
1084 freesstring(rcp
->key
);
1085 freesstring(rcp
->suspendreason
);
1086 freesstring(rcp
->comment
);
1090 cip
->exts
[chanservext
]=NULL
;
1091 releasechanindex(cip
);
1094 /* Sender is who the DELCHAN is attributed to.. */
1095 int cs_removechannelifempty(nick
*sender
, regchan
*rcp
) {
1099 for (i
=0;i
<REGCHANUSERHASHSIZE
;i
++) {
1100 for (rcup
=rcp
->regusers
[i
];rcup
;rcup
=rcup
->nextbychan
) {
1101 if (rcup
->flags
& ~(QCUFLAGS_PUNISH
))
1106 cs_log(sender
,"DELCHAN %s (Empty)",rcp
->index
->name
->content
);
1107 cs_removechannel(rcp
, "Last user removed - channel deleted.");
1112 void cs_removeuser(reguser
*rup
) {
1113 regchanuser
*rcup
, *nrcup
;
1115 struct authname
*anp
;
1117 /* Remove the user from all its channels */
1118 for (rcup
=rup
->knownon
;rcup
;rcup
=nrcup
) {
1119 nrcup
=rcup
->nextbyuser
;
1122 delreguserfromchannel(rcp
, rup
);
1123 cs_removechannelifempty(NULL
, rcp
);
1127 delreguserfrommaildomain(rup
,rup
->domain
);
1128 freesstring(rup
->localpart
);
1129 freesstring(rup
->email
);
1130 freesstring(rup
->lastemail
);
1131 freesstring(rup
->lastuserhost
);
1132 freesstring(rup
->suspendreason
);
1133 freesstring(rup
->comment
);
1134 freesstring(rup
->info
);
1136 csdb_deleteuser(rup
);
1138 removereguserfromhash(rup
);
1140 if ((anp
=findauthname(rup
->ID
)) && anp
->nicks
) {
1141 rup
->status
|= QUSTAT_DEAD
;
1147 int cs_bancheck(nick
*np
, channel
*cp
) {
1148 time_t now
=time(NULL
);
1150 modechanges changes
;
1153 if (!(rcp
=cp
->index
->exts
[chanservext
]))
1156 for (rbh
=&(rcp
->bans
);*rbh
;) {
1157 if ((*rbh
)->expiry
&& ((*rbh
)->expiry
< now
)) {
1159 csdb_deleteban(*rbh
);
1163 freesstring(rbp
->reason
);
1164 freechanban(rbp
->cbp
);
1166 } else if (nickmatchban_visible(np
,(*rbh
)->cbp
)) {
1167 /* This user matches this ban.. */
1168 if (!nickbanned_visible(np
,cp
)) {
1169 /* Only bother putting the ban on the channel if they're not banned already */
1170 /* (might be covered by this ban or a different one.. doesn't really matter */
1171 localsetmodeinit(&changes
, cp
, chanservnick
);
1172 localdosetmode_ban(&changes
, bantostring((*rbh
)->cbp
), MCB_ADD
);
1173 localsetmodeflush(&changes
, 1);
1174 cs_timerfunc(cp
->index
);
1176 localkickuser(chanservnick
, cp
, np
, (*rbh
)->reason
? (*rbh
)->reason
->content
: "Banned.");
1179 rbh
=&((*rbh
)->next
);
1186 void cs_setregban(chanindex
*cip
, regban
*rbp
) {
1187 modechanges changes
;
1194 localsetmodeinit(&changes
, cip
->channel
, chanservnick
);
1195 localdosetmode_ban(&changes
, bantostring(rbp
->cbp
), MCB_ADD
);
1196 localsetmodeflush(&changes
, 1);
1198 for (i
=0;(cip
->channel
) && i
<cip
->channel
->users
->hashsize
;i
++) {
1199 if (cip
->channel
->users
->content
[i
]!=nouser
&&
1200 (np
=getnickbynumeric(cip
->channel
->users
->content
[i
])) &&
1201 !IsService(np
) && !IsOper(np
) && !IsXOper(np
) &&
1202 nickmatchban_visible(np
, rbp
->cbp
))
1203 localkickuser(chanservnick
, cip
->channel
, np
, rbp
->reason
? rbp
->reason
->content
: "Banned.");
1209 void cs_banuser(modechanges
*changes
, chanindex
*cip
, nick
*np
, const char *reason
) {
1210 modechanges lchanges
;
1211 char banmask
[NICKLEN
+USERLEN
+HOSTLEN
+3];
1216 if (nickbanned_visible(np
, cip
->channel
)) {
1217 localkickuser(chanservnick
, cip
->channel
, np
, reason
?reason
:"Banned.");
1221 if (IsAccount(np
)) {
1222 /* Ban their account.. */
1223 sprintf(banmask
,"*!*@%s.%s",np
->authname
,HIS_HIDDENHOST
);
1224 } else if (IsSetHost(np
)) {
1225 /* sethosted: ban ident@host */
1226 sprintf(banmask
,"*!%s@%s",np
->shident
->content
,np
->sethost
->content
);
1227 } else if (np
->host
->clonecount
>3) {
1228 /* >3 clones, ban user@host */
1229 sprintf(banmask
,"*!%s@%s",np
->ident
,np
->host
->name
->content
);
1231 sprintf(banmask
,"*!*@%s",np
->host
->name
->content
);
1235 localsetmodeinit(&lchanges
, cip
->channel
, chanservnick
);
1236 localdosetmode_ban(&lchanges
, banmask
, MCB_ADD
);
1237 localsetmodeflush(&lchanges
, 1);
1239 localdosetmode_ban(changes
, banmask
, MCB_ADD
);
1242 localkickuser(chanservnick
, cip
->channel
, np
, reason
?reason
:"Banned.");
1246 * cs_sanitisechanlev: Removes impossible combinations from chanlev flags.
1247 * chanservuser.c is probably not the right file for this, but nowhere better
1248 * presented itself...
1250 flag_t
cs_sanitisechanlev(flag_t flags
) {
1251 /* +m or +n cannot have any "punishment" flags */
1252 if (flags
& (QCUFLAG_MASTER
| QCUFLAG_OWNER
))
1253 flags
&= ~(QCUFLAGS_PUNISH
);
1255 /* +d can't be +o */
1256 if (flags
& QCUFLAG_DENY
)
1257 flags
&= ~QCUFLAG_OP
;
1259 /* +q can't be +v */
1260 if (flags
& QCUFLAG_QUIET
)
1261 flags
&= ~QCUFLAG_VOICE
;
1263 /* +p trumps +a and +g */
1264 if (flags
& QCUFLAG_PROTECT
)
1265 flags
&= ~(QCUFLAG_AUTOOP
| QCUFLAG_AUTOVOICE
);
1267 /* -o can't be +a */
1268 if (!(flags
& QCUFLAG_OP
))
1269 flags
&= ~QCUFLAG_AUTOOP
;
1271 /* +a or -v can't be +g. +a implies +o at this stage (see above) */
1272 if (!(flags
& QCUFLAG_VOICE
) || (flags
& QCUFLAG_AUTOOP
))
1273 flags
&= ~QCUFLAG_AUTOVOICE
;
1275 /* +p requires +o or +v */
1276 if (!(flags
& (QCUFLAG_VOICE
| QCUFLAG_OP
)))
1277 flags
&= ~QCUFLAG_PROTECT
;
1279 /* The personal flags require one of +mnovk, as does +t */
1280 if (!(flags
& (QCUFLAG_OWNER
| QCUFLAG_MASTER
| QCUFLAG_OP
| QCUFLAG_VOICE
| QCUFLAG_KNOWN
)))
1281 flags
&= ~(QCUFLAGS_PERSONAL
| QCUFLAG_TOPIC
);
1288 * This function does the standard "nick or #user" lookup.
1289 * If "sender" is not NULL, a suitable error message will
1290 * be sent if the lookup fails.
1291 * "sender" MUST be sent when a user is requesting a lookup
1292 * as there is some policy code here.
1295 reguser
*findreguser(nick
*sender
, const char *str
) {
1296 reguser
*rup
, *vrup
= getreguserfromnick(sender
);;
1305 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1308 if (!(rup
=findreguserbynick(str
+1)) && sender
)
1309 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1310 } else if (*str
=='&' && vrup
&& UHasStaffPriv(vrup
)) {
1313 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1316 if (!(rup
=findreguserbyID(atoi(str
+1))) && sender
)
1317 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1319 if (!(np
=getnickbynick(str
))) {
1321 chanservstdmessage(sender
, QM_UNKNOWNUSER
, str
);
1324 if (!(rup
=getreguserfromnick(np
)) && sender
)
1325 chanservstdmessage(sender
, QM_USERNOTAUTHED
, str
);
1328 /* removed the suspended check from here, I don't see the point... */
1329 if (rup
&& (rup
->status
& QUSTAT_DEAD
)) {
1330 chanservstdmessage(sender
, QM_USERHASBADAUTH
, rup
->username
);
1338 * Unbans a mask from a channel, including permbans if user has correct privs.
1340 * Return 0 if it works, 1 if it don't.
1342 int cs_unbanfn(nick
*sender
, chanindex
*cip
, UnbanFN fn
, void *arg
, int removepermbans
, int abortonfailure
) {
1344 chanban
**cbh
, *cbp
;
1346 modechanges changes
;
1349 rcp
=cip
->exts
[chanservext
];
1352 localsetmodeinit(&changes
, cip
->channel
, chanservnick
);
1354 for (rbh
=&(rcp
->bans
); *rbh
; ) {
1356 if (fn(arg
, rbp
->cbp
)) {
1357 banstr
=bantostring(rbp
->cbp
);
1358 /* Check perms and remove */
1359 if(!removepermbans
) {
1360 chanservstdmessage(sender
, QM_WARNNOTREMOVEDPERMBAN
, banstr
, cip
->name
->content
);
1362 } else if (!cs_checkaccess(sender
, NULL
, CA_MASTERPRIV
, cip
, NULL
, 0, 1)) {
1363 chanservstdmessage(sender
, QM_NOTREMOVEDPERMBAN
, banstr
, cip
->name
->content
);
1364 if (abortonfailure
) return 1; /* Just give up... */
1367 chanservstdmessage(sender
, QM_REMOVEDPERMBAN
, banstr
, cip
->name
->content
);
1369 localdosetmode_ban(&changes
, banstr
, MCB_DEL
);
1370 /* Remove from database */
1371 csdb_deleteban(rbp
);
1372 /* Remove from list */
1374 /* Free ban/string and update setby refcount, and free actual regban */
1375 freesstring(rbp
->reason
);
1376 freechanban(rbp
->cbp
);
1385 for (cbh
=&(cip
->channel
->bans
); *cbh
; ) {
1389 banstr
=bantostring(cbp
);
1390 chanservstdmessage(sender
, QM_REMOVEDCHANBAN
, banstr
, cip
->name
->content
);
1391 localdosetmode_ban(&changes
, banstr
, MCB_DEL
);
1396 localsetmodeflush(&changes
,1);
1402 /* Add entry to channel op history when someone gets ops. */
1403 void cs_logchanop(regchan
*rcp
, char *nick
, reguser
*rup
) {
1404 strncpy(rcp
->chanopnicks
[rcp
->chanoppos
], nick
, NICKLEN
);
1405 rcp
->chanopnicks
[rcp
->chanoppos
][NICKLEN
]='\0';
1406 rcp
->chanopaccts
[rcp
->chanoppos
]=rup
->ID
;
1407 rcp
->chanoppos
=(rcp
->chanoppos
+1)%CHANOPHISTORY
;
1410 int checkreason(nick
*np
, char *reason
) {
1411 if((strlen(reason
) < MIN_REASONLEN
) || !strchr(reason
, ' ')) {
1412 chanservstdmessage(np
, QM_REASONREQUIRED
);