]>
jfr.im git - irc/quakenet/newserv.git/blob - request/sqrequest.c
2 * S and Q request system!
4 * Depends on "chanstats" and "chanfix"
10 #include "request_block.h"
11 #include "../chanfix/chanfix.h"
12 #include "../chanstats/chanstats.h"
13 #include "../localuser/localuser.h"
14 #include "../lib/irc_string.h"
15 #include "../core/schedule.h"
16 #include "../core/config.h"
18 #include "../spamscan2/spamscan2.h"
25 #define QRLstate_IDLE 0x0 /* No request active */
26 #define QRLstate_AWAITINGCHAN 0x1 /* Awaiting "Users for channel.." */
27 #define QRLstate_AWAITINGUSER 0x2 /* Looking for our user in the list */
28 #define QRLstate_AWAITINGEND 0x3 /* Waiting for "End of chanlev" */
34 #define QR_SPAMSCAN 0x1
39 #define min(a,b) ((a > b) ? b : a)
41 typedef struct requestrec
{
42 unsigned int reqnumeric
; /* Who made the request */
43 chanindex
*cip
; /* Which channel the request is for */
44 int what
; /* Which service does the user want? */
45 int who
; /* Who are we talking to about CHANLEV? */
46 struct requestrec
*next
;
49 requestrec
*nextreql
, *lastreql
;
50 requestrec
*nextreqq
, *lastreqq
;
53 static requestrec
*nextqreq
, *lastqreq
;
72 /* Check whether the user is blocked */
73 int qr_blockcheck(requestrec
*req
) {
77 np
= getnickbynumeric(req
->reqnumeric
);
79 /* user is not online anymore */
83 block
= rq_findblock(np
->authname
);
86 return 1; /* user is blocked */
91 unsigned int rq_countchanusers(channel
*cp
) {
94 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
95 if (cp
->users
->content
[i
]==nouser
)
105 * Deal with outcome of a queued request. The request should be freed
106 * as part of the process.
109 static void qr_result(requestrec
*req
, int outcome
, char failcode
, char *message
, ...) __attribute__ ((format (printf
, 4, 5)));
110 static void qr_result(requestrec
*req
, int outcome
, char failcode
, char *message
, ...) {
111 sstring
*user
, *password
;
115 nick
*np
, *tnp
, *snp
;
118 unsigned int unique
, total
;
120 /* Delete the request from the list first.. */
121 for (rh
=&nextreql
;*rh
;rh
=&((*rh
)->next
)) {
128 for (rh
=&nextreqq
;*rh
;rh
=&((*rh
)->next
)) {
135 /* If this was the last request (unlikely),
136 * we need to fix the last pointer */
139 for (lastreql
=nextreql
;lastreql
->next
;lastreql
=lastreql
->next
)
147 for (lastreqq
=nextreqq
;lastreqq
->next
;lastreqq
=lastreqq
->next
)
153 /* Check that the nick is still here. If not, drop the request. */
154 if (!(tnp
=np
=getnickbynumeric(req
->reqnumeric
))) {
159 if (rq_logfd
!= NULL
) {
163 if (req
->cip
->channel
) {
164 unique
= countuniquehosts(req
->cip
->channel
);
165 total
= rq_countchanusers(req
->cip
->channel
);
171 strftime(now
, sizeof(now
), "%c", localtime(&now_ts
));
172 fprintf(rq_logfd
, "%s: request (%s) for %s (%d unique users, "
173 "%d total users) from %s: Request was %s (%c).\n", now
,
174 (req
->what
== QR_CSERVE
) ? RQ_QNICK
: RQ_SNICK
,
175 req
->cip
->name
->content
, unique
, total
, tnp
->nick
,
176 (outcome
== QR_OK
) ? "accepted" : "denied", failcode
);
180 if (outcome
==QR_OK
) {
181 if (req
->what
== QR_SPAMSCAN
) {
184 if (!(snp
=getnickbynick(RQ_SNICK
))) {
185 sendnoticetouser(rqnick
, tnp
,
186 "Error: Cannot find %s on the network. "
187 "Please request again later.", RQ_SNICK
);
193 sendnoticetouser(rqnick
, tnp
, "Requirements met, %s should be added. "
194 "Contact #help should further assistance be required.",
198 user
= (sstring
*)getcopyconfigitem("request", "user", "R", 30);
199 password
= (sstring
*)getcopyconfigitem("request", "password", "bla", 30);
200 sendmessagetouser(rqnick
, snp
, "AUTH %s %s", user
->content
, password
->content
);
202 freesstring(password
);
204 /* /msg S addchan <channel> default */
205 sendmessagetouser(rqnick
, snp
, "ADDCHAN %s default +o", req
->cip
->name
->content
);
209 spamscan_channelprofile *cp;
210 spamscan_channelsettings *cs;
211 spamscan_channelext *ce;
214 cs = spamscan_getchannelsettings(req->cip->name->content, 0);
217 cp = spamscan_getchannelprofile("Default", 0);
220 cs = spamscan_getchannelsettings(req->cip->name->content, 1);
224 cs->addedby = spamscan_getaccountsettings("R", 0);
225 cs->flags = SPAMSCAN_CF_IGNOREOPS;
227 sc_index = findorcreatechanindex(req->cip->name->content);
230 ce = spamscan_getchanext(sc_index, 1);
233 if ( s_nickname && !CFIsSuspended(cs) && sc_index->channel ) {
235 localjoinchannel(s_nickname, sc_index->channel);
236 localgetops(s_nickname, sc_index->channel);
240 spamscan_insertchanneldb(cs);
247 /* we do not put the request into another queue, so free it here */
256 lastqreq
=nextqreq
=req
;
262 /* Don't free, it's in new queue now */
264 /* Sort out the message.. */
265 va_start(va
, message
);
266 vsnprintf(msgbuf
,511,message
,va
);
269 sendnoticetouser(rqnick
, tnp
, "%s", msgbuf
);
270 /* This is a failure message. Add disclaimer. */
271 /*sendnoticetouser(rqnick, tnp, "Do not complain about this result in #help or #feds.");*/
280 * Checks that a channel is beeeeg enough for teh Q
283 static int qr_checksize(chanindex
*cip
, int what
, char *failcode
) {
287 int i
, avg
, tot
=0, authedcount
=0, count
=0, uniquecount
, avgcount
;
292 return 0; /* this shouldn't ever happen */
298 /* make sure we can actually add Q */
299 if (what
== QR_CSERVE
) {
300 qbot
= getnickbynick(RQ_QNICK
);
305 if (QR_MAXQCHANS
!= 0 && qbot
->channels
->cursi
> QR_MAXQCHANS
) {
308 return 0; /* no Q for you :p */
312 /* make sure that there are enough authed users */
313 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
314 if (cp
->users
->content
[i
] != nouser
) {
315 np
= getnickbynumeric(cp
->users
->content
[i
]);
326 if (what
== QR_CSERVE
) {
327 if ( count
<= QR_AUTHEDPCT_SCALE
) {
328 authedpct
= QR_AUTHEDPCT_CSERVE
;
329 } else if ( count
>= QR_AUTHEDPCT_SCALEMAX
) {
330 authedpct
= QR_AUTHEDPCT_CSERVEMIN
;
332 authedpct
= (QR_AUTHEDPCT_CSERVEMIN
+ (((QR_AUTHEDPCT_CSERVE
- QR_AUTHEDPCT_CSERVEMIN
) * (100 - (((count
- QR_AUTHEDPCT_SCALE
) * 100) / (QR_AUTHEDPCT_SCALEMAX
- QR_AUTHEDPCT_SCALE
)))) / 100));
336 if ( count
<= QR_AUTHEDPCT_SCALE
) {
337 authedpct
= QR_AUTHEDPCT_SPAMSCAN
;
338 } else if ( count
>= QR_AUTHEDPCT_SCALEMAX
) {
339 authedpct
= QR_AUTHEDPCT_SPAMSCANMIN
;
341 authedpct
= (QR_AUTHEDPCT_SPAMSCANMIN
+ (((QR_AUTHEDPCT_SPAMSCAN
- QR_AUTHEDPCT_SPAMSCANMIN
) * (100 - (((count
- QR_AUTHEDPCT_SCALE
) * 100) / (QR_AUTHEDPCT_SCALEMAX
- QR_AUTHEDPCT_SCALE
)))) / 100));
345 if (authedcount
* 100 / count
< authedpct
) {
348 return 0; /* too few authed users */
351 if (!(csp
=cip
->exts
[csext
]))
354 for (i
=0;i
<HISTORYDAYS
;i
++) {
355 tot
+= csp
->lastdays
[i
];
358 uniquecount
= countuniquehosts(cp
);
359 avgcount
= tot
/ HISTORYDAYS
/ 10;
361 /* chan needs at least QR_MINUSERPCT% of the avg usercount, and can't have
362 * more than QR_MAXUSERPCT% */
363 if ( what
== QR_CSERVE
) {
364 if ((avgcount
* QR_MINUSERSPCT
/ 100 > uniquecount
)) {
369 if ((avgcount
* QR_MAXUSERSPCT
/ 100 < uniquecount
)) {
376 avg
= (what
== QR_CSERVE
) ? QR_REQUIREDSIZE_CSERVE
: QR_REQUIREDSIZE_SPAMSCAN
;
378 if (tot
> (avg
* 140))
387 /* This function deals with notices from L: basically we track the
388 * responses to the L chanlev requests we've been making until we can
389 * decide what to do with the requests.
391 * Here's the L chanlev format:
392 * 11:12 -L(TheLBot@lightweight.quakenet.org)- Users for channel #twilightzone
393 * 11:12 -L(TheLBot@lightweight.quakenet.org)- Authname Access flags
394 * 11:12 -L(TheLBot@lightweight.quakenet.org)- -----------------------------
395 * 11:12 -L(TheLBot@lightweight.quakenet.org)- Bigfoot amno
396 * 11:12 -L(TheLBot@lightweight.quakenet.org)- End of chanlev for #twilightzone.
399 void qr_handle_notice(nick
*sender
, char *message
) {
402 requestrec
*rrp1
, *rrp2
;
404 int delrequest
= 0, state
, who
;
407 if (!ircd_strcmp(sender
->nick
, RQ_QNICK
) && nextqreq
) {
409 if (!ircd_strcmp(message
,"Done.")) {
410 /* Q added the channel: delete from L and tell the user. */
411 /* If L has conspired to vanish between the request and the outcome,
412 * we have a chan with Q and L... too bad. */
415 } else if (!ircd_strcmp(message
,"That channel already exists.")) {
420 /* For either of the two messages above we want to delete the request
421 * at the head of the queue. */
425 nextqreq
=nextqreq
->next
;
433 if (!ircd_strcmp(sender
->nick
, RQ_QNICK
)) {
435 state
= (who
== QR_Q
) ? rqstate
: rlstate
;
436 nextreq
= (who
== QR_Q
) ? nextreqq
: nextreql
;
438 /* Message from L or Q */
441 /* We're idle, do nothing */
444 case QRLstate_AWAITINGCHAN
:
445 /* We're waiting for conformation of the channel name */
446 if ((!ircd_strncmp(message
,"Users for",9) && who
== QR_L
) ||
447 (!ircd_strncmp(message
,"Known users on",14) && who
== QR_Q
)
449 /* Looks like the right message. Let's find a channel name */
450 for (ch
=message
;*ch
;ch
++)
455 Error("qrequest",ERR_WARNING
,
456 "Unable to parse channel name from L/Q message: %s",message
);
460 /* chop off any remaining words */
463 if (*chop
== ' ' || *chop
== ':') {
468 if (!(cip
=findchanindex(ch
))) {
469 Error("qrequest",ERR_WARNING
,
470 "Unable to find channel from L/Q message: %s",ch
);
474 if (cip
==nextreq
->cip
) {
475 /* Ok, this is the correct channel, everything is proceeding
476 * exactly as I had forseen */
478 rlstate
= QRLstate_AWAITINGUSER
;
480 rqstate
= QRLstate_AWAITINGUSER
;
483 /* Uh-oh, not the channel we wanted. Something is fucked
484 * here. I think the only possible way out of this mess is
485 * to skip through in case we find a match for a later channel..
487 for (rrp1
=nextreq
;rrp1
;rrp1
=rrp1
->next
)
492 /* OK, we found a match further down the chain. This means
493 * that something bad has happened to every request between
494 * the head of the list and the one we just found - send
497 * Note weird loop head: qr_result will free up requests from
498 * the list as it goes, so we can just keep picking off the first
501 for(rrp2
=nextreq
;rrp2
;) {
505 Error("qrequest",ERR_WARNING
,
506 "Lost response for channel %s; skipping.",
507 rrp2
->cip
->name
->content
);
509 qr_result(rrp2
, QR_FAILED
, 'X',
510 "Sorry, an error occurred while processing your request.");
512 rrp2
= nextreq
= (who
== QR_Q
) ? nextreqq
: nextreql
;
516 /* We seem to be back in sync. */
518 rlstate
= QRLstate_AWAITINGUSER
;
520 rqstate
= QRLstate_AWAITINGUSER
;
524 /* Some form of hole in the space time continuum exists
525 * if we get here. Unclear how to proceed. */
528 /* No match - let's just ignore this completely */
529 Error("qrequest",ERR_WARNING
,
530 "Ignoring L/Q response for spurious channel %s",
538 case QRLstate_AWAITINGUSER
:
539 if ((!ircd_strncmp(message
, "End of chanlev",14) && who
== QR_L
) ||
540 (!ircd_strncmp(message
, "End of list.",12) && who
== QR_Q
)) {
541 /* Oh dear, we got to the end of the chanlev in this state.
542 * This means that we didn't find the user.
544 qr_result(nextreq
, QR_FAILED
, 'X',
545 "Error: You are not known on %s.",
546 nextreq
->cip
->name
->content
);
548 /* need to reset nextreq .. just in case
549 * qr_result has cleaned up records */
551 nextreq
= (who
== QR_Q
) ? nextreqq
: nextreql
;
555 rlstate
= QRLstate_AWAITINGUSER
;
557 rqstate
= QRLstate_AWAITINGUSER
;
560 rlstate
= QRLstate_IDLE
;
562 rqstate
= QRLstate_IDLE
;
569 /* Brutalise the message :-) */
572 while (*message
== ' ')
576 if (!(ch
=strchr(message
, ' ')))
581 if (!(np
=getnickbynumeric(nextreq
->reqnumeric
)))
584 if (ircd_strcmp(message
, np
->authname
)) {
585 /* This is not the user you are looking for */
588 /* Check for owner flag. Both branches of this if will
589 * take the request off the list, one way or the other. */
591 /* chop off any remaining words */
598 /* chop off any remaining words */
601 if (*chop
== ' ' || *chop
== ':') {
607 if (strchr(ch
, 'n')) {
609 /* They iz teh +n! */
610 /* Note: We're checking for blocks kind of late, so the request
611 system gets a chance to send other error messages first (like
612 'no chanstats', 'not known on channel', etc.). This is required
613 so that the user doesn't notice that he's being blocked. */
614 if (qr_checksize(nextreq
->cip
, nextreq
->what
, &failcode
) && !qr_blockcheck(nextreq
)) {
615 qr_result(nextreq
, QR_OK
, '-', "OK");
617 qr_result(nextreq
, QR_FAILED
, failcode
,
618 "Error: Sorry, Your channel '%s' does not require %s. Please try again in a few days.", nextreq
->cip
->name
->content
, RQ_SNICK
);
623 qr_result(nextreq
, QR_FAILED
, 'X',
624 "Error: You don't hold the +n flag on %s.",
625 nextreq
->cip
->name
->content
);
630 /* OK, we found what we wanted so make sure we skip the rest */
632 rlstate
= QRLstate_AWAITINGEND
;
634 rqstate
= QRLstate_AWAITINGEND
;
640 case QRLstate_AWAITINGEND
:
641 if (!ircd_strncmp(message
, "End of chanlev",14) ||
642 !ircd_strncmp(message
, "End of list.",12)) {
643 /* Found end of list */
647 rlstate
= QRLstate_AWAITINGCHAN
;
649 rqstate
= QRLstate_AWAITINGCHAN
;
652 rlstate
= QRLstate_IDLE
;
654 rqstate
= QRLstate_IDLE
;
665 * This function deals with requests from users for Q.
666 * Some sanity checks are made and the request is
667 * added to the queue.
670 int qr_requestq(nick
*rqnick
, nick
*sender
, channel
*cp
, nick
*lnick
, nick
*qnick
) {
674 int qr_instantrequestq(nick
*sender
, channel
*cp
) {
675 requestrec
*fakerequest
;
680 regop
*rolist
[QR_TOPX
];
682 if (!qr_checksize(cp
->index
, QR_CSERVE
, &failcode
))
685 cf
= cf_findchanfix(cp
->index
);
690 rocount
= cf_getsortedregops(cf
, QR_TOPX
, rolist
);
694 for (i
= 0; i
< min(QR_TOPX
, rocount
); i
++) {
695 if (cf_cmpregopnick(rolist
[i
], sender
)) {
701 /* not in top 5 - we don't have to worry about that error here
702 as the L request code will detect it again and send the user
703 an appropriate message */
707 /* allocate a fake request */
708 fakerequest
= (requestrec
*)malloc(sizeof(requestrec
));
710 fakerequest
->reqnumeric
= sender
->numeric
;
711 fakerequest
->cip
= cp
->index
;
712 fakerequest
->what
= QR_CSERVE
;
713 fakerequest
->who
= QR_L
; /* pretend that we asked L about the chanlev */
715 /* add it to the queue */
716 if (nextreql
== NULL
) {
717 fakerequest
->next
= NULL
;
718 nextreql
= fakerequest
;
719 lastreql
= fakerequest
;
721 fakerequest
->next
= nextreql
;
722 nextreql
= fakerequest
;
725 qr_result(fakerequest
, QR_OK
, '-', "OK");
730 int qr_requests(nick
*rqnick
, nick
*sender
, channel
*cp
, nick
*qnick
) {
731 chanindex
*cip
= cp
->index
;
733 requestrec
*nextreq
, *lastreq
;
735 if (rq_isspam(sender
)) {
736 sendnoticetouser(rqnick
, sender
, "Error: Do not flood the request system."
737 " Try again in %s.", rq_longtoduration(rq_blocktime(sender
)));
742 /* check which service is on the channel */
743 if (getnumerichandlefromchanhash(cp
->users
, qnick
->numeric
) != NULL
) {
747 /* Request stats from Q */
748 sendmessagetouser(rqnick
, qnick
, "CHANLEV %s", cip
->name
->content
);
750 if (rqstate
== QRLstate_IDLE
)
751 rqstate
= QRLstate_AWAITINGCHAN
;
752 } /* 'else' cannot happen as R has already checked whether the user has L or Q */
757 /* Sort out a request record */
759 lastreq
->next
= (requestrec
*)malloc(sizeof(requestrec
));
760 lastreq
=lastreq
->next
;
762 lastreq
=nextreq
=(requestrec
*)malloc(sizeof(requestrec
));
765 lastreq
->next
= NULL
;
767 lastreq
->what
= QR_SPAMSCAN
;
768 lastreq
->reqnumeric
= sender
->numeric
;
773 sendnoticetouser(rqnick
, sender
,
774 "Checking your %s access. "
775 "This may take a while, please be patient...",
781 void qr_initrequest(void) {
782 nextreql
=lastreql
=NULL
;
783 nextreqq
=lastreqq
=NULL
;
784 nextqreq
=lastqreq
=NULL
;
787 void qr_finirequest(void) {
788 struct requestrec
*rp
;
792 nextreqq
=nextreqq
->next
;
798 nextreql
=nextreql
->next
;
804 nextqreq
=nextqreq
->next
;
809 void qr_requeststats(nick
*rqnick
, nick
*np
) {
810 sendnoticetouser(rqnick
, np
, "- Suspended (S): %d", qr_suspended
);
811 sendnoticetouser(rqnick
, np
, "- No chanstats (S): %d", qr_nohist
);
812 sendnoticetouser(rqnick
, np
, "- Too small (S): %d", qr_toosmall
);
813 sendnoticetouser(rqnick
, np
, "- User was not on chanlev (S): %d", qr_nochanlev
);
814 sendnoticetouser(rqnick
, np
, "- User was not the owner (S): %d", qr_notowner
);
815 sendnoticetouser(rqnick
, np
, "- A: %d", qr_a
);
816 sendnoticetouser(rqnick
, np
, "- B: %d", qr_b
);
817 sendnoticetouser(rqnick
, np
, "- C: %d", qr_c
);
818 sendnoticetouser(rqnick
, np
, "- D: %d", qr_d
);
819 sendnoticetouser(rqnick
, np
, "- E: %d", qr_e
);