]>
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"
17 #include "../spamscan2/spamscan2.h"
23 #define QRLstate_IDLE 0x0 /* No request active */
24 #define QRLstate_AWAITINGCHAN 0x1 /* Awaiting "Users for channel.." */
25 #define QRLstate_AWAITINGUSER 0x2 /* Looking for our user in the list */
26 #define QRLstate_AWAITINGEND 0x3 /* Waiting for "End of chanlev" */
32 #define QR_SPAMSCAN 0x1
37 #define min(a,b) ((a > b) ? b : a)
39 typedef struct requestrec
{
40 unsigned int reqnumeric
; /* Who made the request */
41 chanindex
*cip
; /* Which channel the request is for */
42 int what
; /* Which service does the user want? */
43 int who
; /* Who are we talking to about CHANLEV? */
44 struct requestrec
*next
;
47 requestrec
*nextreql
, *lastreql
;
48 requestrec
*nextreqq
, *lastreqq
;
51 static requestrec
*nextqreq
, *lastqreq
;
70 /* Check whether the user is blocked */
71 int qr_blockcheck(requestrec
*req
) {
75 np
= getnickbynumeric(req
->reqnumeric
);
77 /* user is not online anymore */
81 block
= rq_findblock(np
->authname
);
84 return 1; /* user is blocked */
89 unsigned int rq_countchanusers(channel
*cp
) {
92 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
93 if (cp
->users
->content
[i
]==nouser
)
103 * Deal with outcome of a queued request. The request should be freed
104 * as part of the process.
107 static void qr_result(requestrec
*req
, int outcome
, char failcode
, char *message
, ...) {
108 sstring
*user
, *password
;
112 nick
*np
, *tnp
, *snp
;
115 unsigned int unique
, total
;
117 /* Delete the request from the list first.. */
118 for (rh
=&nextreql
;*rh
;rh
=&((*rh
)->next
)) {
125 for (rh
=&nextreqq
;*rh
;rh
=&((*rh
)->next
)) {
132 /* If this was the last request (unlikely),
133 * we need to fix the last pointer */
136 for (lastreql
=nextreql
;lastreql
->next
;lastreql
=lastreql
->next
)
144 for (lastreqq
=nextreqq
;lastreqq
->next
;lastreqq
=lastreqq
->next
)
150 /* Check that the nick is still here. If not, drop the request. */
151 if (!(tnp
=np
=getnickbynumeric(req
->reqnumeric
))) {
156 if (rq_logfd
!= NULL
) {
160 if (req
->cip
->channel
) {
161 unique
= countuniquehosts(req
->cip
->channel
);
162 total
= rq_countchanusers(req
->cip
->channel
);
168 strftime(now
, sizeof(now
), "%c", localtime(&now_ts
));
169 fprintf(rq_logfd
, "%s: request (%s) for %s (%d unique users, "
170 "%d total users) from %s: Request was %s (%c).\n", now
,
171 (req
->what
== QR_CSERVE
) ? RQ_QNICK
: RQ_SNICK
,
172 req
->cip
->name
->content
, unique
, total
, tnp
->nick
,
173 (outcome
== QR_OK
) ? "accepted" : "denied", failcode
);
177 if (outcome
==QR_OK
) {
178 if (req
->what
== QR_SPAMSCAN
) {
181 if (!(snp
=getnickbynick(RQ_SNICK
))) {
182 sendnoticetouser(rqnick
, tnp
,
183 "Error: Cannot find %s on the network. "
184 "Please request again later.", RQ_SNICK
);
190 sendnoticetouser(rqnick
, tnp
, "Requirements met, %s should be added. "
191 "Contact #help should further assistance be required.",
195 user
= (sstring
*)getcopyconfigitem("request", "user", "R", 30);
196 password
= (sstring
*)getcopyconfigitem("request", "password", "bla", 30);
197 sendmessagetouser(rqnick
, snp
, "AUTH %s %s", user
->content
, password
->content
);
199 freesstring(password
);
201 /* /msg S addchan <channel> default */
202 //sendmessagetouser(rqnick, snp, "ADDCHAN %s default +op", req->cip->name->content);
205 spamscan_channelprofile
*cp
;
206 spamscan_channelsettings
*cs
;
207 spamscan_channelext
*ce
;
210 cs
= spamscan_getchannelsettings(req
->cip
->name
->content
, 0);
213 cp
= spamscan_getchannelprofile("Default", 0);
216 cs
= spamscan_getchannelsettings(req
->cip
->name
->content
, 1);
220 cs
->addedby
= spamscan_getaccountsettings("R", 0);
221 cs
->flags
= SPAMSCAN_CF_IGNOREOPS
;
223 sc_index
= findorcreatechanindex(req
->cip
->name
->content
);
226 ce
= spamscan_getchanext(sc_index
, 1);
229 if ( s_nickname
&& !CFIsSuspended(cs
) && sc_index
->channel
) {
231 localjoinchannel(s_nickname
, sc_index
->channel
);
232 localgetops(s_nickname
, sc_index
->channel
);
236 spamscan_insertchanneldb(cs
);
242 /* we do not put the request into another queue, so free it here */
251 lastqreq
=nextqreq
=req
;
257 /* Don't free, it's in new queue now */
259 /* Sort out the message.. */
260 va_start(va
, message
);
261 vsnprintf(msgbuf
,511,message
,va
);
264 sendnoticetouser(rqnick
, tnp
, "%s", msgbuf
);
265 /* This is a failure message. Add disclaimer. */
266 /*sendnoticetouser(rqnick, tnp, "Do not complain about this result in #help or #feds.");*/
275 * Checks that a channel is beeeeg enough for teh Q
278 static int qr_checksize(chanindex
*cip
, int what
, char *failcode
) {
282 int i
, avg
, tot
=0, authedcount
=0, count
=0, uniquecount
, avgcount
;
287 return 0; /* this shouldn't ever happen */
293 /* make sure we can actually add Q */
294 if (what
== QR_CSERVE
) {
295 qbot
= getnickbynick(RQ_QNICK
);
300 if (QR_MAXQCHANS
!= 0 && qbot
->channels
->cursi
> QR_MAXQCHANS
) {
303 return 0; /* no Q for you :p */
307 /* make sure that there are enough authed users */
308 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
309 if (cp
->users
->content
[i
] != nouser
) {
310 np
= getnickbynumeric(cp
->users
->content
[i
]);
321 if (what
== QR_CSERVE
) {
322 if ( count
<= QR_AUTHEDPCT_SCALE
) {
323 authedpct
= QR_AUTHEDPCT_CSERVE
;
324 } else if ( count
>= QR_AUTHEDPCT_SCALEMAX
) {
325 authedpct
= QR_AUTHEDPCT_CSERVEMIN
;
327 authedpct
= (QR_AUTHEDPCT_CSERVEMIN
+ (((QR_AUTHEDPCT_CSERVE
- QR_AUTHEDPCT_CSERVEMIN
) * (100 - (((count
- QR_AUTHEDPCT_SCALE
) * 100) / (QR_AUTHEDPCT_SCALEMAX
- QR_AUTHEDPCT_SCALE
)))) / 100));
331 if ( count
<= QR_AUTHEDPCT_SCALE
) {
332 authedpct
= QR_AUTHEDPCT_SPAMSCAN
;
333 } else if ( count
>= QR_AUTHEDPCT_SCALEMAX
) {
334 authedpct
= QR_AUTHEDPCT_SPAMSCANMIN
;
336 authedpct
= (QR_AUTHEDPCT_SPAMSCANMIN
+ (((QR_AUTHEDPCT_SPAMSCAN
- QR_AUTHEDPCT_SPAMSCANMIN
) * (100 - (((count
- QR_AUTHEDPCT_SCALE
) * 100) / (QR_AUTHEDPCT_SCALEMAX
- QR_AUTHEDPCT_SCALE
)))) / 100));
340 if (authedcount
* 100 / count
< authedpct
) {
343 return 0; /* too few authed users */
346 if (!(csp
=cip
->exts
[csext
]))
349 for (i
=0;i
<HISTORYDAYS
;i
++) {
350 tot
+= csp
->lastdays
[i
];
353 uniquecount
= countuniquehosts(cp
);
354 avgcount
= tot
/ HISTORYDAYS
/ 10;
356 /* chan needs at least QR_MINUSERPCT% of the avg usercount, and can't have
357 * more than QR_MAXUSERPCT% */
358 if ( what
== QR_CSERVE
) {
359 if ((avgcount
* QR_MINUSERSPCT
/ 100 > uniquecount
)) {
364 if ((avgcount
* QR_MAXUSERSPCT
/ 100 < uniquecount
)) {
371 avg
= (what
== QR_CSERVE
) ? QR_REQUIREDSIZE_CSERVE
: QR_REQUIREDSIZE_SPAMSCAN
;
373 if (tot
> (avg
* 140))
382 /* This function deals with notices from L: basically we track the
383 * responses to the L chanlev requests we've been making until we can
384 * decide what to do with the requests.
386 * Here's the L chanlev format:
387 * 11:12 -L(TheLBot@lightweight.quakenet.org)- Users for channel #twilightzone
388 * 11:12 -L(TheLBot@lightweight.quakenet.org)- Authname Access flags
389 * 11:12 -L(TheLBot@lightweight.quakenet.org)- -----------------------------
390 * 11:12 -L(TheLBot@lightweight.quakenet.org)- Bigfoot amno
391 * 11:12 -L(TheLBot@lightweight.quakenet.org)- End of chanlev for #twilightzone.
394 void qr_handle_notice(nick
*sender
, char *message
) {
397 requestrec
*rrp1
, *rrp2
;
399 int delrequest
= 0, state
, who
;
402 if (!ircd_strcmp(sender
->nick
, RQ_QNICK
) && nextqreq
) {
404 if (!ircd_strcmp(message
,"Done.")) {
405 /* Q added the channel: delete from L and tell the user. */
406 /* If L has conspired to vanish between the request and the outcome,
407 * we have a chan with Q and L... too bad. */
410 } else if (!ircd_strcmp(message
,"That channel already exists.")) {
415 /* For either of the two messages above we want to delete the request
416 * at the head of the queue. */
420 nextqreq
=nextqreq
->next
;
428 if (!ircd_strcmp(sender
->nick
, RQ_QNICK
)) {
430 state
= (who
== QR_Q
) ? rqstate
: rlstate
;
431 nextreq
= (who
== QR_Q
) ? nextreqq
: nextreql
;
433 /* Message from L or Q */
436 /* We're idle, do nothing */
439 case QRLstate_AWAITINGCHAN
:
440 /* We're waiting for conformation of the channel name */
441 if ((!ircd_strncmp(message
,"Users for",9) && who
== QR_L
) ||
442 (!ircd_strncmp(message
,"Known users on",14) && who
== QR_Q
)
444 /* Looks like the right message. Let's find a channel name */
445 for (ch
=message
;*ch
;ch
++)
450 Error("qrequest",ERR_WARNING
,
451 "Unable to parse channel name from L/Q message: %s",message
);
455 /* chop off any remaining words */
458 if (*chop
== ' ' || *chop
== ':') {
463 if (!(cip
=findchanindex(ch
))) {
464 Error("qrequest",ERR_WARNING
,
465 "Unable to find channel from L/Q message: %s",ch
);
469 if (cip
==nextreq
->cip
) {
470 /* Ok, this is the correct channel, everything is proceeding
471 * exactly as I had forseen */
473 rlstate
= QRLstate_AWAITINGUSER
;
475 rqstate
= QRLstate_AWAITINGUSER
;
478 /* Uh-oh, not the channel we wanted. Something is fucked
479 * here. I think the only possible way out of this mess is
480 * to skip through in case we find a match for a later channel..
482 for (rrp1
=nextreq
;rrp1
;rrp1
=rrp1
->next
)
487 /* OK, we found a match further down the chain. This means
488 * that something bad has happened to every request between
489 * the head of the list and the one we just found - send
492 * Note weird loop head: qr_result will free up requests from
493 * the list as it goes, so we can just keep picking off the first
496 for(rrp2
=nextreq
;rrp2
;) {
500 Error("qrequest",ERR_WARNING
,
501 "Lost response for channel %s; skipping.",
502 rrp2
->cip
->name
->content
);
504 qr_result(rrp2
, QR_FAILED
, 'X',
505 "Sorry, an error occurred while processing your request.");
507 rrp2
= nextreq
= (who
== QR_Q
) ? nextreqq
: nextreql
;
511 /* We seem to be back in sync. */
513 rlstate
= QRLstate_AWAITINGUSER
;
515 rqstate
= QRLstate_AWAITINGUSER
;
519 /* Some form of hole in the space time continuum exists
520 * if we get here. Unclear how to proceed. */
523 /* No match - let's just ignore this completely */
524 Error("qrequest",ERR_WARNING
,
525 "Ignoring L/Q response for spurious channel %s",
533 case QRLstate_AWAITINGUSER
:
534 if ((!ircd_strncmp(message
, "End of chanlev",14) && who
== QR_L
) ||
535 (!ircd_strncmp(message
, "End of list.",12) && who
== QR_Q
)) {
536 /* Oh dear, we got to the end of the chanlev in this state.
537 * This means that we didn't find the user.
539 qr_result(nextreq
, QR_FAILED
, 'X',
540 "Error: You are not known on %s.",
541 nextreq
->cip
->name
->content
);
543 /* need to reset nextreq .. just in case
544 * qr_result has cleaned up records */
546 nextreq
= (who
== QR_Q
) ? nextreqq
: nextreql
;
550 rlstate
= QRLstate_AWAITINGUSER
;
552 rqstate
= QRLstate_AWAITINGUSER
;
555 rlstate
= QRLstate_IDLE
;
557 rqstate
= QRLstate_IDLE
;
564 /* Brutalise the message :-) */
567 while (*message
== ' ')
571 if (!(ch
=strchr(message
, ' ')))
576 if (!(np
=getnickbynumeric(nextreq
->reqnumeric
)))
579 if (ircd_strcmp(message
, np
->authname
)) {
580 /* This is not the user you are looking for */
583 /* Check for owner flag. Both branches of this if will
584 * take the request off the list, one way or the other. */
586 /* chop off any remaining words */
593 /* chop off any remaining words */
596 if (*chop
== ' ' || *chop
== ':') {
602 if (strchr(ch
, 'n')) {
604 /* They iz teh +n! */
605 /* Note: We're checking for blocks kind of late, so the request
606 system gets a chance to send other error messages first (like
607 'no chanstats', 'not known on channel', etc.). This is required
608 so that the user doesn't notice that he's being blocked. */
609 if (qr_checksize(nextreq
->cip
, nextreq
->what
, &failcode
) && !qr_blockcheck(nextreq
)) {
610 qr_result(nextreq
, QR_OK
, '-', "OK");
612 qr_result(nextreq
, QR_FAILED
, failcode
,
613 "Error: Sorry, Your channel '%s' does not require %s. Please try again in a few days.", nextreq
->cip
->name
->content
, RQ_SNICK
);
618 qr_result(nextreq
, QR_FAILED
, 'X',
619 "Error: You don't hold the +n flag on %s.",
620 nextreq
->cip
->name
->content
);
625 /* OK, we found what we wanted so make sure we skip the rest */
627 rlstate
= QRLstate_AWAITINGEND
;
629 rqstate
= QRLstate_AWAITINGEND
;
635 case QRLstate_AWAITINGEND
:
636 if (!ircd_strncmp(message
, "End of chanlev",14) ||
637 !ircd_strncmp(message
, "End of list.",12)) {
638 /* Found end of list */
642 rlstate
= QRLstate_AWAITINGCHAN
;
644 rqstate
= QRLstate_AWAITINGCHAN
;
647 rlstate
= QRLstate_IDLE
;
649 rqstate
= QRLstate_IDLE
;
660 * This function deals with requests from users for Q.
661 * Some sanity checks are made and the request is
662 * added to the queue.
665 int qr_requestq(nick
*rqnick
, nick
*sender
, channel
*cp
, nick
*lnick
, nick
*qnick
) {
669 int qr_instantrequestq(nick
*sender
, channel
*cp
) {
670 requestrec
*fakerequest
;
675 regop
*rolist
[QR_TOPX
];
677 if (!qr_checksize(cp
->index
, QR_CSERVE
, &failcode
))
680 cf
= cf_findchanfix(cp
->index
);
685 rocount
= cf_getsortedregops(cf
, QR_TOPX
, rolist
);
689 for (i
= 0; i
< min(QR_TOPX
, rocount
); i
++) {
690 if (cf_cmpregopnick(rolist
[i
], sender
)) {
696 /* not in top 5 - we don't have to worry about that error here
697 as the L request code will detect it again and send the user
698 an appropriate message */
702 /* allocate a fake request */
703 fakerequest
= (requestrec
*)malloc(sizeof(requestrec
));
705 fakerequest
->reqnumeric
= sender
->numeric
;
706 fakerequest
->cip
= cp
->index
;
707 fakerequest
->what
= QR_CSERVE
;
708 fakerequest
->who
= QR_L
; /* pretend that we asked L about the chanlev */
710 /* add it to the queue */
711 if (nextreql
== NULL
) {
712 fakerequest
->next
= NULL
;
713 nextreql
= fakerequest
;
714 lastreql
= fakerequest
;
716 fakerequest
->next
= nextreql
;
717 nextreql
= fakerequest
;
720 qr_result(fakerequest
, QR_OK
, '-', "OK");
725 int qr_requests(nick
*rqnick
, nick
*sender
, channel
*cp
, nick
*qnick
) {
726 chanindex
*cip
= cp
->index
;
728 requestrec
*nextreq
, *lastreq
;
730 if (rq_isspam(sender
)) {
731 sendnoticetouser(rqnick
, sender
, "Error: Do not flood the request system."
732 " Try again in %s.", rq_longtoduration(rq_blocktime(sender
)));
737 /* check which service is on the channel */
738 if (getnumerichandlefromchanhash(cp
->users
, qnick
->numeric
) != NULL
) {
742 /* Request stats from Q */
743 sendmessagetouser(rqnick
, qnick
, "CHANLEV %s", cip
->name
->content
);
745 if (rqstate
== QRLstate_IDLE
)
746 rqstate
= QRLstate_AWAITINGCHAN
;
747 } /* 'else' cannot happen as R has already checked whether the user has L or Q */
752 /* Sort out a request record */
754 lastreq
->next
= (requestrec
*)malloc(sizeof(requestrec
));
755 lastreq
=lastreq
->next
;
757 lastreq
=nextreq
=(requestrec
*)malloc(sizeof(requestrec
));
760 lastreq
->next
= NULL
;
762 lastreq
->what
= QR_SPAMSCAN
;
763 lastreq
->reqnumeric
= sender
->numeric
;
768 sendnoticetouser(rqnick
, sender
,
769 "Checking your %s access. "
770 "This may take a while, please be patient...",
776 void qr_initrequest(void) {
777 nextreql
=lastreql
=NULL
;
778 nextreqq
=lastreqq
=NULL
;
779 nextqreq
=lastqreq
=NULL
;
782 void qr_finirequest(void) {
783 struct requestrec
*rp
;
787 nextreqq
=nextreqq
->next
;
793 nextreql
=nextreql
->next
;
799 nextqreq
=nextqreq
->next
;
804 void qr_requeststats(nick
*rqnick
, nick
*np
) {
805 sendnoticetouser(rqnick
, np
, "- Suspended (S): %d", qr_suspended
);
806 sendnoticetouser(rqnick
, np
, "- No chanstats (S): %d", qr_nohist
);
807 sendnoticetouser(rqnick
, np
, "- Too small (S): %d", qr_toosmall
);
808 sendnoticetouser(rqnick
, np
, "- User was not on chanlev (S): %d", qr_nochanlev
);
809 sendnoticetouser(rqnick
, np
, "- User was not the owner (S): %d", qr_notowner
);
810 sendnoticetouser(rqnick
, np
, "- A: %d", qr_a
);
811 sendnoticetouser(rqnick
, np
, "- B: %d", qr_b
);
812 sendnoticetouser(rqnick
, np
, "- C: %d", qr_c
);
813 sendnoticetouser(rqnick
, np
, "- D: %d", qr_d
);
814 sendnoticetouser(rqnick
, np
, "- E: %d", qr_e
);