]>
jfr.im git - irc/quakenet/newserv.git/blob - request/request_block.c
3 #include "../irc/irc.h"
4 #include "../lib/irc_string.h"
5 #include "request_block.h"
10 /* are we currently loading blocks? */
13 int rq_initblocks(void) {
14 array_init(&rqblocks
, sizeof(rq_block
));
15 array_setlim1(&rqblocks
, 5);
16 array_setlim2(&rqblocks
, 20);
22 rq_addblock("#qnet*", "Reserved for QuakeNet use only.", "request", 0, 0);
23 rq_addblock("#help*", "Reserved for QuakeNet use only.", "request", 0, 0);
28 void rq_finiblocks(void) {
32 for (i
= 0; i
< rqblocks
.cursi
; i
++) {
33 block
= ((rq_block
*)rqblocks
.content
)[i
];
35 freesstring(block
.pattern
);
36 freesstring(block
.reason
);
37 freesstring(block
.creator
);
40 array_free(&rqblocks
);
43 rq_block
*rq_findblock(const char *pattern
) {
47 for (i
= rqblocks
.cursi
- 1; i
>= 0; i
--) {
48 block
= ((rq_block
*)rqblocks
.content
)[i
];
50 if (match2strings(block
.pattern
->content
, pattern
)) {
51 if (block
.expires
!= 0 && block
.expires
< getnettime())
52 rq_removeblock(block
.pattern
->content
);
54 return &(((rq_block
*)rqblocks
.content
)[i
]);
61 void rq_addblock(const char *pattern
, const char *reason
, const char *creator
, time_t created
, time_t expires
) {
65 if (rq_findblock(pattern
) != NULL
)
68 slot
= array_getfreeslot(&rqblocks
);
70 block
= &(((rq_block
*)rqblocks
.content
)[slot
]);
72 block
->pattern
= getsstring(pattern
, CHANNELLEN
);
73 block
->reason
= getsstring(reason
, RQ_BLOCKLEN
);
74 block
->creator
= getsstring(creator
, ACCOUNTLEN
);
75 block
->created
= created
== 0 ? getnettime() : created
;
76 block
->expires
= expires
;
81 int rq_removeblock(const char *pattern
) {
85 for (i
= 0; i
< rqblocks
.cursi
; i
++) {
86 block
= ((rq_block
*)rqblocks
.content
)[i
];
88 if (ircd_strcmp(block
.pattern
->content
, pattern
) == 0) {
89 freesstring(block
.pattern
);
90 freesstring(block
.reason
);
91 freesstring(block
.creator
);
93 array_delslot(&rqblocks
, i
);
104 /* pattern reason creator created expires */
105 int rq_parseline(char *line
) {
106 char pattern
[CHANNELLEN
+1];
107 char reason
[RQ_BLOCKLEN
+1];
108 char creator
[ACCOUNTLEN
+1];
109 time_t created
, expires
;
111 if (sscanf(line
, "%s %s %lu %lu %[^\n]", pattern
, creator
, &created
, &expires
, reason
) < 2) /* \n won't be there anyway, but %s won't return the whole string */
112 return 0; /* invalid block */
114 /* tell rq_addblock that it should not save the blocks to disk this time */
116 rq_addblock(pattern
, reason
, creator
, created
, expires
);
122 int rq_loadblocks(void) {
127 rqdata
= fopen(RQ_BLOCKFILE
, "r");
134 while (!feof(rqdata
)) {
135 if (fgets(line
, sizeof(line
), rqdata
) == NULL
)
138 if (line
[strlen(line
) - 1] == '\n')
139 line
[strlen(line
) - 1] = '\0';
141 if (line
[strlen(line
) - 1] == '\r')
142 line
[strlen(line
) - 1] = '\0';
144 if (line
[0] != '\0') {
145 if (rq_parseline(line
))
155 int rq_saveblocks(void) {
159 /* don't save the blocks if we're currently loading them from the disk */
163 rqdata
= fopen(RQ_BLOCKFILE
, "w");
170 for (i
= 0; i
< rqblocks
.cursi
; i
++) {
171 block
= ((rq_block
*)rqblocks
.content
)[i
];
173 fprintf(rqdata
, "%s %s %lu %lu %s\n", block
.pattern
->content
, block
.creator
->content
, block
.created
, block
.expires
, block
.reason
->content
);