]>
Commit | Line | Data |
---|---|---|
25b7d0fa P |
1 | /* |
2 | * S and Q request system! | |
3 | * | |
4 | * Depends on "chanstats" and "chanfix" | |
5 | */ | |
6 | ||
052247fa | 7 | #include <stdio.h> |
25b7d0fa P |
8 | #include "request.h" |
9 | #include "sqrequest.h" | |
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" | |
f011b15a | 16 | #include "../core/config.h" |
a421a6dc | 17 | /* |
0096e09a | 18 | #include "../spamscan2/spamscan2.h" |
a421a6dc | 19 | */ |
25b7d0fa P |
20 | |
21 | #include <stdarg.h> | |
22 | #include <stdio.h> | |
23 | #include <string.h> | |
24 | ||
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" */ | |
29 | ||
30 | #define QR_FAILED 0x0 | |
31 | #define QR_OK 0x1 | |
32 | ||
33 | #define QR_CSERVE 0x0 | |
34 | #define QR_SPAMSCAN 0x1 | |
35 | ||
36 | #define QR_L 0x0 | |
37 | #define QR_Q 0x1 | |
38 | ||
39 | #define min(a,b) ((a > b) ? b : a) | |
40 | ||
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; | |
47 | } requestrec; | |
48 | ||
49 | requestrec *nextreql, *lastreql; | |
50 | requestrec *nextreqq, *lastreqq; | |
51 | ||
52 | ||
b98ba21f | 53 | static requestrec *nextqreq, *lastqreq; |
25b7d0fa P |
54 | |
55 | extern nick *rqnick; | |
56 | int rlstate; | |
57 | int rqstate; | |
58 | ||
59 | /* stats counters */ | |
60 | int qr_suspended = 0; | |
61 | int qr_nohist = 0; | |
62 | int qr_toosmall = 0; | |
63 | int qr_nochanlev = 0; | |
64 | int qr_notowner = 0; | |
65 | ||
0096e09a P |
66 | int qr_a = 0; |
67 | int qr_b = 0; | |
68 | int qr_c = 0; | |
69 | int qr_d = 0; | |
70 | int qr_e = 0; | |
71 | ||
25b7d0fa P |
72 | /* Check whether the user is blocked */ |
73 | int qr_blockcheck(requestrec *req) { | |
74 | nick *np; | |
75 | rq_block *block; | |
76 | ||
77 | np = getnickbynumeric(req->reqnumeric); | |
78 | ||
79 | /* user is not online anymore */ | |
80 | if (np == NULL) | |
81 | return 0; | |
82 | ||
83 | block = rq_findblock(np->authname); | |
84 | ||
85 | if (block != NULL) | |
86 | return 1; /* user is blocked */ | |
87 | else | |
88 | return 0; | |
89 | } | |
90 | ||
052247fa CP |
91 | unsigned int rq_countchanusers(channel *cp) { |
92 | int i, count=0; | |
93 | ||
94 | for (i=0;i<cp->users->hashsize;i++) { | |
95 | if (cp->users->content[i]==nouser) | |
96 | continue; | |
97 | ||
98 | count++; | |
99 | } | |
100 | ||
101 | return count; | |
102 | } | |
103 | ||
25b7d0fa P |
104 | /* |
105 | * Deal with outcome of a queued request. The request should be freed | |
106 | * as part of the process. | |
107 | */ | |
108 | ||
bb4b25ee | 109 | static void qr_result(requestrec *req, int outcome, char failcode, char *message, ...) __attribute__ ((format (printf, 4, 5))); |
b98ba21f | 110 | static void qr_result(requestrec *req, int outcome, char failcode, char *message, ...) { |
25b7d0fa P |
111 | sstring *user, *password; |
112 | requestrec **rh; | |
113 | char msgbuf[512]; | |
114 | va_list va; | |
680c487f | 115 | nick *np, *tnp, *snp; |
052247fa CP |
116 | char now[50]; |
117 | time_t now_ts; | |
118 | unsigned int unique, total; | |
119 | ||
25b7d0fa P |
120 | /* Delete the request from the list first.. */ |
121 | for (rh=&nextreql;*rh;rh=&((*rh)->next)) { | |
122 | if (*rh==req) { | |
123 | *rh=req->next; | |
124 | break; | |
125 | } | |
126 | } | |
127 | ||
128 | for (rh=&nextreqq;*rh;rh=&((*rh)->next)) { | |
129 | if (*rh==req) { | |
130 | *rh=req->next; | |
131 | break; | |
132 | } | |
133 | } | |
134 | ||
135 | /* If this was the last request (unlikely), | |
136 | * we need to fix the last pointer */ | |
137 | if (lastreql==req) { | |
138 | if (nextreql) | |
139 | for (lastreql=nextreql;lastreql->next;lastreql=lastreql->next) | |
140 | ; /* empty loop */ | |
141 | else | |
142 | lastreql=NULL; | |
143 | } | |
144 | ||
145 | if (lastreqq==req) { | |
146 | if (nextreqq) | |
147 | for (lastreqq=nextreqq;lastreqq->next;lastreqq=lastreqq->next) | |
148 | ; /* empty loop */ | |
149 | else | |
150 | lastreqq=NULL; | |
151 | } | |
152 | ||
153 | /* Check that the nick is still here. If not, drop the request. */ | |
154 | if (!(tnp=np=getnickbynumeric(req->reqnumeric))) { | |
155 | free(req); | |
156 | return; | |
157 | } | |
158 | ||
052247fa CP |
159 | if (rq_logfd != NULL) { |
160 | now[0] = '\0'; | |
161 | now_ts = time(NULL); | |
162 | ||
163 | if (req->cip->channel) { | |
164 | unique = countuniquehosts(req->cip->channel); | |
165 | total = rq_countchanusers(req->cip->channel); | |
166 | } else { | |
167 | unique = 0; | |
168 | total = 0; | |
169 | } | |
cbdd5aab | 170 | |
052247fa CP |
171 | strftime(now, sizeof(now), "%c", localtime(&now_ts)); |
172 | fprintf(rq_logfd, "%s: request (%s) for %s (%d unique users, " | |
e5363a30 | 173 | "%d total users) from %s!%s@%s%s%s: Request was %s (%c).\n", now, |
052247fa | 174 | (req->what == QR_CSERVE) ? RQ_QNICK : RQ_SNICK, |
e5363a30 C |
175 | req->cip->name->content, unique, total, |
176 | tnp->nick, tnp->ident, tnp->host->name->content, IsAccount(tnp)?"/":"", IsAccount(tnp)?tnp->authname:"", | |
0096e09a | 177 | (outcome == QR_OK) ? "accepted" : "denied", failcode); |
052247fa | 178 | fflush(rq_logfd); |
cbdd5aab P |
179 | } |
180 | ||
25b7d0fa | 181 | if (outcome==QR_OK) { |
95440fe7 | 182 | if (req->what == QR_SPAMSCAN) { |
25b7d0fa P |
183 | /* Add S */ |
184 | ||
185 | if (!(snp=getnickbynick(RQ_SNICK))) { | |
186 | sendnoticetouser(rqnick, tnp, | |
43d341bd C |
187 | "Cannot find %s on the network. " |
188 | "Please try your request again later.", RQ_SNICK); | |
25b7d0fa P |
189 | |
190 | free(req); | |
191 | return; | |
192 | } | |
193 | ||
43d341bd C |
194 | sendnoticetouser(rqnick, tnp, "Success! %s has been added to '%s' " |
195 | "(contact #help if you require further assistance).", | |
196 | RQ_SNICK, req->cip->name->content); | |
25b7d0fa P |
197 | |
198 | /* auth */ | |
052247fa CP |
199 | user = (sstring *)getcopyconfigitem("request", "user", "R", 30); |
200 | password = (sstring *)getcopyconfigitem("request", "password", "bla", 30); | |
25b7d0fa P |
201 | sendmessagetouser(rqnick, snp, "AUTH %s %s", user->content, password->content); |
202 | freesstring(user); | |
203 | freesstring(password); | |
204 | ||
205 | /* /msg S addchan <channel> default */ | |
a421a6dc | 206 | sendmessagetouser(rqnick, snp, "ADDCHAN %s default +o", req->cip->name->content); |
0096e09a | 207 | |
a421a6dc | 208 | /* |
0096e09a P |
209 | { |
210 | spamscan_channelprofile *cp; | |
211 | spamscan_channelsettings *cs; | |
212 | spamscan_channelext *ce; | |
213 | chanindex *sc_index; | |
214 | ||
215 | cs = spamscan_getchannelsettings(req->cip->name->content, 0); | |
216 | ||
217 | if ( !cs ) { | |
218 | cp = spamscan_getchannelprofile("Default", 0); | |
219 | ||
220 | if ( cp ) { | |
221 | cs = spamscan_getchannelsettings(req->cip->name->content, 1); | |
222 | ||
223 | if ( cs ) { | |
224 | cs->cp = cp; | |
225 | cs->addedby = spamscan_getaccountsettings("R", 0); | |
226 | cs->flags = SPAMSCAN_CF_IGNOREOPS; | |
227 | ||
228 | sc_index = findorcreatechanindex(req->cip->name->content); | |
229 | ||
230 | if ( sc_index ) { | |
231 | ce = spamscan_getchanext(sc_index, 1); | |
232 | ce->cs = cs; | |
233 | ||
234 | if ( s_nickname && !CFIsSuspended(cs) && sc_index->channel ) { | |
235 | ce->joinedchan = 1; | |
236 | localjoinchannel(s_nickname, sc_index->channel); | |
237 | localgetops(s_nickname, sc_index->channel); | |
238 | } | |
239 | } | |
240 | ||
241 | spamscan_insertchanneldb(cs); | |
242 | } | |
243 | } | |
244 | } | |
245 | } | |
a421a6dc | 246 | */ |
25b7d0fa P |
247 | |
248 | /* we do not put the request into another queue, so free it here */ | |
249 | free(req); | |
250 | ||
251 | return; | |
252 | } | |
253 | ||
254 | if (lastqreq) | |
255 | lastqreq->next=req; | |
256 | else | |
257 | lastqreq=nextqreq=req; | |
258 | ||
259 | req->next=NULL; | |
260 | ||
261 | rq_success++; | |
262 | ||
263 | /* Don't free, it's in new queue now */ | |
264 | } else { | |
265 | /* Sort out the message.. */ | |
266 | va_start(va, message); | |
267 | vsnprintf(msgbuf,511,message,va); | |
268 | va_end(va); | |
269 | ||
270 | sendnoticetouser(rqnick, tnp, "%s", msgbuf); | |
271 | /* This is a failure message. Add disclaimer. */ | |
272 | /*sendnoticetouser(rqnick, tnp, "Do not complain about this result in #help or #feds.");*/ | |
273 | free(req); | |
274 | ||
275 | rq_failed++; | |
276 | } | |
277 | } | |
278 | ||
279 | /* | |
280 | * qr_checksize: | |
281 | * Checks that a channel is beeeeg enough for teh Q | |
282 | */ | |
283 | ||
b98ba21f | 284 | static int qr_checksize(chanindex *cip, int what, char *failcode) { |
25b7d0fa P |
285 | chanstats *csp; |
286 | channel *cp; | |
052247fa CP |
287 | nick *np, *qbot; |
288 | int i , avg, tot=0, authedcount=0, count=0, uniquecount, avgcount; | |
25b7d0fa P |
289 | |
290 | cp = cip->channel; | |
291 | ||
292 | if (cp == NULL) | |
293 | return 0; /* this shouldn't ever happen */ | |
294 | ||
295 | #if QR_DEBUG | |
296 | return 1; | |
297 | #endif | |
298 | ||
052247fa CP |
299 | /* make sure we can actually add Q */ |
300 | if (what == QR_CSERVE) { | |
301 | qbot = getnickbynick(RQ_QNICK); | |
302 | ||
303 | if (!qbot) | |
304 | return 0; | |
305 | ||
0096e09a P |
306 | if (QR_MAXQCHANS != 0 && qbot->channels->cursi > QR_MAXQCHANS) { |
307 | qr_a++; | |
308 | *failcode = 'A'; | |
052247fa | 309 | return 0; /* no Q for you :p */ |
0096e09a | 310 | } |
052247fa CP |
311 | } |
312 | ||
25b7d0fa P |
313 | /* make sure that there are enough authed users */ |
314 | for (i=0;i<cp->users->hashsize;i++) { | |
315 | if (cp->users->content[i] != nouser) { | |
316 | np = getnickbynumeric(cp->users->content[i]); | |
317 | ||
318 | if (IsAccount(np)) | |
319 | authedcount++; | |
320 | ||
321 | count++; | |
322 | } | |
323 | } | |
324 | ||
0096e09a P |
325 | int authedpct; |
326 | ||
327 | if (what == QR_CSERVE) { | |
328 | if ( count <= QR_AUTHEDPCT_SCALE ) { | |
329 | authedpct = QR_AUTHEDPCT_CSERVE; | |
330 | } else if ( count >= QR_AUTHEDPCT_SCALEMAX ) { | |
331 | authedpct = QR_AUTHEDPCT_CSERVEMIN; | |
332 | } else { | |
333 | authedpct = (QR_AUTHEDPCT_CSERVEMIN + (((QR_AUTHEDPCT_CSERVE - QR_AUTHEDPCT_CSERVEMIN) * (100 - (((count - QR_AUTHEDPCT_SCALE) * 100) / (QR_AUTHEDPCT_SCALEMAX - QR_AUTHEDPCT_SCALE)))) / 100)); | |
334 | } | |
335 | } | |
336 | else { | |
337 | if ( count <= QR_AUTHEDPCT_SCALE ) { | |
338 | authedpct = QR_AUTHEDPCT_SPAMSCAN; | |
339 | } else if ( count >= QR_AUTHEDPCT_SCALEMAX ) { | |
340 | authedpct = QR_AUTHEDPCT_SPAMSCANMIN; | |
341 | } else { | |
342 | authedpct = (QR_AUTHEDPCT_SPAMSCANMIN + (((QR_AUTHEDPCT_SPAMSCAN - QR_AUTHEDPCT_SPAMSCANMIN) * (100 - (((count - QR_AUTHEDPCT_SCALE) * 100) / (QR_AUTHEDPCT_SCALEMAX - QR_AUTHEDPCT_SCALE)))) / 100)); | |
343 | } | |
344 | } | |
345 | ||
346 | if (authedcount * 100 / count < authedpct) { | |
347 | qr_b++; | |
348 | *failcode = 'B'; | |
25b7d0fa | 349 | return 0; /* too few authed users */ |
0096e09a | 350 | } |
25b7d0fa P |
351 | |
352 | if (!(csp=cip->exts[csext])) | |
353 | return 0; | |
354 | ||
355 | for (i=0;i<HISTORYDAYS;i++) { | |
356 | tot += csp->lastdays[i]; | |
357 | } | |
358 | ||
052247fa CP |
359 | uniquecount = countuniquehosts(cp); |
360 | avgcount = tot / HISTORYDAYS / 10; | |
361 | ||
362 | /* chan needs at least QR_MINUSERPCT% of the avg usercount, and can't have | |
363 | * more than QR_MAXUSERPCT% */ | |
0096e09a P |
364 | if ( what == QR_CSERVE ) { |
365 | if ((avgcount * QR_MINUSERSPCT / 100 > uniquecount)) { | |
366 | qr_c++; | |
367 | *failcode = 'C'; | |
368 | return 0; | |
369 | } | |
370 | if ((avgcount * QR_MAXUSERSPCT / 100 < uniquecount)) { | |
371 | qr_e++; | |
372 | *failcode = 'E'; | |
373 | return 0; | |
374 | } | |
375 | } | |
376 | ||
25b7d0fa P |
377 | avg = (what == QR_CSERVE) ? QR_REQUIREDSIZE_CSERVE : QR_REQUIREDSIZE_SPAMSCAN; |
378 | ||
379 | if (tot > (avg * 140)) | |
380 | return 1; | |
0096e09a P |
381 | else { |
382 | qr_d++; | |
383 | *failcode = 'D'; | |
052247fa | 384 | return 0; |
0096e09a | 385 | } |
25b7d0fa P |
386 | } |
387 | ||
388 | /* This function deals with notices from L: basically we track the | |
389 | * responses to the L chanlev requests we've been making until we can | |
390 | * decide what to do with the requests. | |
391 | * | |
392 | * Here's the L chanlev format: | |
393 | * 11:12 -L(TheLBot@lightweight.quakenet.org)- Users for channel #twilightzone | |
394 | * 11:12 -L(TheLBot@lightweight.quakenet.org)- Authname Access flags | |
395 | * 11:12 -L(TheLBot@lightweight.quakenet.org)- ----------------------------- | |
396 | * 11:12 -L(TheLBot@lightweight.quakenet.org)- Bigfoot amno | |
397 | * 11:12 -L(TheLBot@lightweight.quakenet.org)- End of chanlev for #twilightzone. | |
398 | */ | |
399 | ||
b98ba21f | 400 | void qr_handle_notice(nick *sender, char *message) { |
25b7d0fa P |
401 | char *ch, *chop; |
402 | chanindex *cip; | |
403 | requestrec *rrp1, *rrp2; | |
404 | nick *np; | |
405 | int delrequest = 0, state, who; | |
406 | requestrec *nextreq; | |
25b7d0fa | 407 | |
25b7d0fa P |
408 | if (!ircd_strcmp(sender->nick, RQ_QNICK) && nextqreq) { |
409 | /* Message from Q */ | |
410 | if (!ircd_strcmp(message,"Done.")) { | |
411 | /* Q added the channel: delete from L and tell the user. */ | |
412 | /* If L has conspired to vanish between the request and the outcome, | |
413 | * we have a chan with Q and L... too bad. */ | |
25b7d0fa P |
414 | |
415 | delrequest = 1; | |
416 | } else if (!ircd_strcmp(message,"That channel already exists.")) { | |
25b7d0fa P |
417 | |
418 | delrequest = 1; | |
25b7d0fa P |
419 | } |
420 | ||
421 | /* For either of the two messages above we want to delete the request | |
422 | * at the head of the queue. */ | |
423 | if (delrequest) { | |
424 | rrp1=nextqreq; | |
425 | ||
426 | nextqreq=nextqreq->next; | |
427 | if (!nextqreq) | |
428 | lastqreq=NULL; | |
429 | ||
430 | free(rrp1); | |
431 | } | |
432 | } | |
433 | ||
95440fe7 P |
434 | if (!ircd_strcmp(sender->nick, RQ_QNICK)) { |
435 | who = QR_Q; | |
25b7d0fa P |
436 | state = (who == QR_Q) ? rqstate : rlstate; |
437 | nextreq = (who == QR_Q) ? nextreqq : nextreql; | |
438 | ||
439 | /* Message from L or Q */ | |
440 | switch (state) { | |
441 | case QRLstate_IDLE: | |
442 | /* We're idle, do nothing */ | |
443 | return; | |
444 | ||
445 | case QRLstate_AWAITINGCHAN: | |
446 | /* We're waiting for conformation of the channel name */ | |
447 | if ((!ircd_strncmp(message,"Users for",9) && who == QR_L) || | |
448 | (!ircd_strncmp(message,"Known users on",14) && who == QR_Q) | |
449 | ) { | |
450 | /* Looks like the right message. Let's find a channel name */ | |
25b7d0fa P |
451 | for (ch=message;*ch;ch++) |
452 | if (*ch=='#') | |
453 | break; | |
454 | ||
455 | if (!*ch) { | |
456 | Error("qrequest",ERR_WARNING, | |
457 | "Unable to parse channel name from L/Q message: %s",message); | |
458 | return; | |
459 | } | |
460 | ||
461 | /* chop off any remaining words */ | |
462 | chop = ch; | |
463 | while (*(chop++)) { | |
95440fe7 | 464 | if (*chop == ' ' || *chop == ':') { |
25b7d0fa P |
465 | *chop = '\0'; |
466 | break; | |
467 | } | |
468 | } | |
25b7d0fa P |
469 | if (!(cip=findchanindex(ch))) { |
470 | Error("qrequest",ERR_WARNING, | |
471 | "Unable to find channel from L/Q message: %s",ch); | |
472 | return; | |
473 | } | |
474 | ||
475 | if (cip==nextreq->cip) { | |
476 | /* Ok, this is the correct channel, everything is proceeding | |
477 | * exactly as I had forseen */ | |
478 | if (who == QR_L) | |
479 | rlstate = QRLstate_AWAITINGUSER; | |
480 | else | |
481 | rqstate = QRLstate_AWAITINGUSER; | |
25b7d0fa P |
482 | return; |
483 | } else { | |
484 | /* Uh-oh, not the channel we wanted. Something is fucked | |
485 | * here. I think the only possible way out of this mess is | |
486 | * to skip through in case we find a match for a later channel.. | |
487 | */ | |
488 | for (rrp1=nextreq;rrp1;rrp1=rrp1->next) | |
489 | if (rrp1->cip==cip) | |
490 | break; | |
491 | ||
492 | if (rrp1) { | |
493 | /* OK, we found a match further down the chain. This means | |
494 | * that something bad has happened to every request between | |
495 | * the head of the list and the one we just found - send | |
496 | * error responses. | |
497 | * | |
498 | * Note weird loop head: qr_result will free up requests from | |
499 | * the list as it goes, so we can just keep picking off the first | |
500 | * entry | |
501 | */ | |
f3c45977 | 502 | for(rrp2=nextreq;rrp2;) { |
25b7d0fa P |
503 | if (rrp2==rrp1) |
504 | break; | |
505 | ||
506 | Error("qrequest",ERR_WARNING, | |
507 | "Lost response for channel %s; skipping.", | |
508 | rrp2->cip->name->content); | |
509 | ||
0096e09a | 510 | qr_result(rrp2, QR_FAILED, 'X', |
25b7d0fa | 511 | "Sorry, an error occurred while processing your request."); |
f3c45977 P |
512 | |
513 | rrp2 = nextreq = (who == QR_Q) ? nextreqq : nextreql; | |
25b7d0fa P |
514 | } |
515 | ||
516 | if (rrp2) { | |
517 | /* We seem to be back in sync. */ | |
518 | if (who == QR_L) | |
519 | rlstate = QRLstate_AWAITINGUSER; | |
520 | else | |
521 | rqstate = QRLstate_AWAITINGUSER; | |
522 | ||
523 | return; | |
524 | } | |
525 | /* Some form of hole in the space time continuum exists | |
526 | * if we get here. Unclear how to proceed. */ | |
527 | return; | |
528 | } else { | |
529 | /* No match - let's just ignore this completely */ | |
530 | Error("qrequest",ERR_WARNING, | |
531 | "Ignoring L/Q response for spurious channel %s", | |
532 | cip->name->content); | |
533 | return; | |
534 | } | |
535 | } | |
536 | } | |
537 | break; | |
538 | ||
539 | case QRLstate_AWAITINGUSER: | |
540 | if ((!ircd_strncmp(message, "End of chanlev",14) && who == QR_L) || | |
541 | (!ircd_strncmp(message, "End of list.",12) && who == QR_Q)) { | |
542 | /* Oh dear, we got to the end of the chanlev in this state. | |
543 | * This means that we didn't find the user. | |
544 | */ | |
0096e09a | 545 | qr_result(nextreq, QR_FAILED, 'X', |
25b7d0fa P |
546 | "Error: You are not known on %s.", |
547 | nextreq->cip->name->content); | |
548 | ||
549 | /* need to reset nextreq .. just in case | |
550 | * qr_result has cleaned up records */ | |
551 | ||
552 | nextreq = (who == QR_Q) ? nextreqq : nextreql; | |
553 | ||
554 | if (nextreq) { | |
555 | if (who == QR_L) | |
556 | rlstate = QRLstate_AWAITINGUSER; | |
557 | else | |
558 | rqstate = QRLstate_AWAITINGUSER; | |
559 | } else { | |
560 | if (who == QR_L) | |
561 | rlstate = QRLstate_IDLE; | |
562 | else | |
563 | rqstate = QRLstate_IDLE; | |
564 | } | |
565 | ||
566 | qr_nochanlev++; | |
567 | ||
568 | return; | |
569 | } else { | |
570 | /* Brutalise the message :-) */ | |
571 | ||
572 | if (who == QR_Q) { | |
573 | while (*message == ' ') | |
574 | message++; | |
575 | } | |
576 | ||
577 | if (!(ch=strchr(message, ' '))) | |
578 | return; | |
579 | ||
580 | *ch++='\0'; | |
581 | ||
582 | if (!(np=getnickbynumeric(nextreq->reqnumeric))) | |
583 | return; | |
584 | ||
585 | if (ircd_strcmp(message, np->authname)) { | |
586 | /* This is not the user you are looking for */ | |
587 | return; | |
588 | } | |
25b7d0fa P |
589 | /* Check for owner flag. Both branches of this if will |
590 | * take the request off the list, one way or the other. */ | |
95440fe7 P |
591 | |
592 | /* chop off any remaining words */ | |
593 | chop = ch; | |
594 | while (*(chop++)) { | |
595 | if (*chop != ' ' ) { | |
596 | break; | |
597 | } | |
598 | } | |
599 | /* chop off any remaining words */ | |
600 | ch = chop; | |
601 | while (*(chop++)) { | |
602 | if (*chop == ' ' || *chop == ':') { | |
603 | *chop = '\0'; | |
604 | break; | |
605 | } | |
606 | } | |
607 | ||
608 | if (strchr(ch, 'n')) { | |
0096e09a | 609 | char failcode; |
25b7d0fa | 610 | /* They iz teh +n! */ |
25b7d0fa P |
611 | /* Note: We're checking for blocks kind of late, so the request |
612 | system gets a chance to send other error messages first (like | |
613 | 'no chanstats', 'not known on channel', etc.). This is required | |
614 | so that the user doesn't notice that he's being blocked. */ | |
0096e09a P |
615 | if (qr_checksize(nextreq->cip, nextreq->what, &failcode) && !qr_blockcheck(nextreq)) { |
616 | qr_result(nextreq, QR_OK, '-', "OK"); | |
25b7d0fa | 617 | } else { |
0096e09a P |
618 | qr_result(nextreq, QR_FAILED, failcode, |
619 | "Error: Sorry, Your channel '%s' does not require %s. Please try again in a few days.", nextreq->cip->name->content, RQ_SNICK); | |
95440fe7 | 620 | |
25b7d0fa P |
621 | qr_toosmall++; |
622 | } | |
623 | } else { | |
0096e09a | 624 | qr_result(nextreq, QR_FAILED, 'X', |
25b7d0fa P |
625 | "Error: You don't hold the +n flag on %s.", |
626 | nextreq->cip->name->content); | |
627 | ||
628 | qr_notowner++; | |
629 | } | |
630 | ||
631 | /* OK, we found what we wanted so make sure we skip the rest */ | |
632 | if (who == QR_L) | |
633 | rlstate = QRLstate_AWAITINGEND; | |
634 | else | |
635 | rqstate = QRLstate_AWAITINGEND; | |
636 | ||
637 | return; | |
638 | } | |
639 | break; | |
640 | ||
641 | case QRLstate_AWAITINGEND: | |
642 | if (!ircd_strncmp(message, "End of chanlev",14) || | |
643 | !ircd_strncmp(message, "End of list.",12)) { | |
644 | /* Found end of list */ | |
645 | ||
646 | if (nextreq) { | |
647 | if (who == QR_L) | |
648 | rlstate = QRLstate_AWAITINGCHAN; | |
649 | else | |
650 | rqstate = QRLstate_AWAITINGCHAN; | |
651 | } else { | |
652 | if (who == QR_L) | |
653 | rlstate = QRLstate_IDLE; | |
654 | else | |
655 | rqstate = QRLstate_IDLE; | |
656 | } | |
657 | ||
658 | return; | |
659 | } | |
660 | break; | |
661 | } | |
662 | } | |
663 | } | |
664 | ||
665 | /* | |
666 | * This function deals with requests from users for Q. | |
667 | * Some sanity checks are made and the request is | |
668 | * added to the queue. | |
669 | */ | |
670 | ||
671 | int qr_requestq(nick *rqnick, nick *sender, channel *cp, nick *lnick, nick *qnick) { | |
25b7d0fa P |
672 | return RQ_UNKNOWN; |
673 | } | |
674 | ||
675 | int qr_instantrequestq(nick *sender, channel *cp) { | |
676 | requestrec *fakerequest; | |
677 | chanfix *cf; | |
678 | regop *ro; | |
679 | int rocount, i; | |
0096e09a | 680 | char failcode; |
25b7d0fa P |
681 | regop *rolist[QR_TOPX]; |
682 | ||
0096e09a | 683 | if (!qr_checksize(cp->index, QR_CSERVE, &failcode)) |
25b7d0fa P |
684 | return RQ_ERROR; |
685 | ||
686 | cf = cf_findchanfix(cp->index); | |
687 | ||
688 | if (cf == NULL) | |
689 | return RQ_ERROR; | |
690 | ||
691 | rocount = cf_getsortedregops(cf, QR_TOPX, rolist); | |
692 | ||
693 | ro = NULL; | |
694 | ||
695 | for (i = 0; i < min(QR_TOPX, rocount); i++) { | |
696 | if (cf_cmpregopnick(rolist[i], sender)) { | |
697 | ro = rolist[i]; | |
698 | break; | |
699 | } | |
700 | } | |
701 | ||
702 | /* not in top 5 - we don't have to worry about that error here | |
703 | as the L request code will detect it again and send the user | |
704 | an appropriate message */ | |
705 | if (ro == NULL) | |
706 | return RQ_ERROR; | |
707 | ||
708 | /* allocate a fake request */ | |
709 | fakerequest = (requestrec *)malloc(sizeof(requestrec)); | |
710 | ||
711 | fakerequest->reqnumeric = sender->numeric; | |
712 | fakerequest->cip = cp->index; | |
713 | fakerequest->what = QR_CSERVE; | |
714 | fakerequest->who = QR_L; /* pretend that we asked L about the chanlev */ | |
715 | ||
716 | /* add it to the queue */ | |
717 | if (nextreql == NULL) { | |
718 | fakerequest->next = NULL; | |
719 | nextreql = fakerequest; | |
720 | lastreql = fakerequest; | |
721 | } else { | |
722 | fakerequest->next = nextreql; | |
723 | nextreql = fakerequest; | |
724 | } | |
725 | ||
0096e09a | 726 | qr_result(fakerequest, QR_OK, '-', "OK"); |
25b7d0fa P |
727 | |
728 | return RQ_OK; | |
729 | } | |
730 | ||
95440fe7 | 731 | int qr_requests(nick *rqnick, nick *sender, channel *cp, nick *qnick) { |
25b7d0fa | 732 | chanindex *cip = cp->index; |
25b7d0fa P |
733 | requestrec *nextreq, *lastreq; |
734 | ||
25b7d0fa | 735 | /* check which service is on the channel */ |
95440fe7 | 736 | if (getnumerichandlefromchanhash(cp->users, qnick->numeric) != NULL) { |
25b7d0fa P |
737 | /* Request stats from Q */ |
738 | sendmessagetouser(rqnick, qnick, "CHANLEV %s", cip->name->content); | |
739 | ||
740 | if (rqstate == QRLstate_IDLE) | |
741 | rqstate = QRLstate_AWAITINGCHAN; | |
742 | } /* 'else' cannot happen as R has already checked whether the user has L or Q */ | |
743 | ||
95440fe7 P |
744 | lastreq = lastreqq; |
745 | nextreq = nextreqq; | |
25b7d0fa P |
746 | |
747 | /* Sort out a request record */ | |
748 | if (lastreq) { | |
749 | lastreq->next = (requestrec *)malloc(sizeof(requestrec)); | |
750 | lastreq=lastreq->next; | |
751 | } else { | |
752 | lastreq=nextreq=(requestrec *)malloc(sizeof(requestrec)); | |
753 | } | |
754 | ||
755 | lastreq->next = NULL; | |
756 | lastreq->cip = cip; | |
757 | lastreq->what = QR_SPAMSCAN; | |
758 | lastreq->reqnumeric = sender->numeric; | |
759 | ||
95440fe7 P |
760 | nextreqq = nextreq; |
761 | lastreqq = lastreq; | |
25b7d0fa | 762 | |
95440fe7 | 763 | sendnoticetouser(rqnick, sender, |
43d341bd | 764 | "Checking your %s access in '%s'. " |
25b7d0fa | 765 | "This may take a while, please be patient...", |
43d341bd | 766 | RQ_QNICK, cip->name->content); |
25b7d0fa P |
767 | |
768 | return RQ_UNKNOWN; | |
769 | } | |
770 | ||
771 | void qr_initrequest(void) { | |
772 | nextreql=lastreql=NULL; | |
773 | nextreqq=lastreqq=NULL; | |
774 | nextqreq=lastqreq=NULL; | |
775 | } | |
776 | ||
777 | void qr_finirequest(void) { | |
778 | struct requestrec *rp; | |
779 | ||
780 | while (nextreqq) { | |
781 | rp=nextreqq; | |
782 | nextreqq=nextreqq->next; | |
783 | free(rp); | |
784 | } | |
785 | ||
786 | while (nextreql) { | |
787 | rp=nextreql; | |
788 | nextreql=nextreql->next; | |
789 | free(rp); | |
790 | } | |
791 | ||
792 | while (nextqreq) { | |
793 | rp=nextqreq; | |
794 | nextqreq=nextqreq->next; | |
795 | free(rp); | |
796 | } | |
797 | } | |
798 | ||
799 | void qr_requeststats(nick *rqnick, nick *np) { | |
95440fe7 P |
800 | sendnoticetouser(rqnick, np, "- Suspended (S): %d", qr_suspended); |
801 | sendnoticetouser(rqnick, np, "- No chanstats (S): %d", qr_nohist); | |
802 | sendnoticetouser(rqnick, np, "- Too small (S): %d", qr_toosmall); | |
803 | sendnoticetouser(rqnick, np, "- User was not on chanlev (S): %d", qr_nochanlev); | |
804 | sendnoticetouser(rqnick, np, "- User was not the owner (S): %d", qr_notowner); | |
0096e09a P |
805 | sendnoticetouser(rqnick, np, "- A: %d", qr_a); |
806 | sendnoticetouser(rqnick, np, "- B: %d", qr_b); | |
807 | sendnoticetouser(rqnick, np, "- C: %d", qr_c); | |
808 | sendnoticetouser(rqnick, np, "- D: %d", qr_d); | |
809 | sendnoticetouser(rqnick, np, "- E: %d", qr_e); | |
810 | ||
25b7d0fa | 811 | } |