]> jfr.im git - irc/quakenet/newserv.git/blame_incremental - jupe/jupe.c
Jupe: Fix Whitespace in jupe module
[irc/quakenet/newserv.git] / jupe / jupe.c
... / ...
CommitLineData
1#include <stdlib.h>
2#include <stdint.h>
3#include <sys/time.h>
4
5#include "../core/hooks.h"
6#include "../lib/sstring.h"
7#include "../server/server.h"
8#include "../irc/irc.h"
9#include "../core/error.h"
10#include "../lib/irc_string.h"
11#include "jupe.h"
12
13jupe_t *jupes = NULL;
14
15int handlejupe(void *source, int cargc, char **cargv);
16void sendjupeburst(int hook, void *args);
17
18void _init() {
19 registerhook(HOOK_IRC_SENDBURSTBURSTS, &sendjupeburst);
20
21 registerserverhandler("JU", &handlejupe, 5);
22
23 irc_send("%s RB J", mynumeric->content);
24}
25
26void _fini() {
27 jupe_t *next;
28
29 while (jupes) {
30 /* keep a pointer to the next item */
31 next = jupes->ju_next;
32
33 free(jupes);
34
35 jupes = next;
36 }
37
38 deregisterhook(HOOK_IRC_SENDBURSTBURSTS, &sendjupeburst);
39
40 deregisterserverhandler("JU", &handlejupe);
41}
42
43int handlejupe(void *source, int cargc, char **cargv) {
44 char *server, *expire, *modtime, *reason;
45 jupe_t *jupe;
46 unsigned int flags;
47
48 if (cargc < 5)
49 return CMD_OK; /* local jupe or not a valid.. we don't care */
50
51 server = cargv[1];
52 expire = cargv[2];
53 modtime = cargv[3];
54 reason = cargv[4];
55
56 if (atoi(expire) > JUPE_MAX_EXPIRE || atoi(expire) <= 0)
57 return CMD_ERROR; /* jupe's expiry date is not valid */
58
59 if (server[0] != '+' && server[0] != '-')
60 return CMD_OK; /* not a valid jupe either */
61 else {
62 flags = (server[0] == '+') ? JUPE_ACTIVE : 0;
63 server++;
64 }
65
66 jupe = jupe_find(server);
67
68 if (jupe != NULL && atoi(modtime) > jupe->ju_lastmod) {
69 jupe->ju_flags = flags;
70 jupe->ju_lastmod = atoi(modtime);
71
72 Error("jupe", ERR_WARNING, "jupe modified for %s (%s) expiring in %s,"
73 " lastmod: %s, active: %s", server, reason, expire, modtime, flags ? "yes" : "no");
74 return CMD_OK;
75 }
76
77 jupe = make_jupe(server, reason, getnettime() + atoi(expire), atoi(modtime), flags);
78
79 if (jupe == NULL)
80 return CMD_ERROR;
81
82 Error("jupe", ERR_WARNING, "jupe added for %s (%s) expiring in %s,"
83 " lastmod: %s, active: %s", server, reason, expire, modtime, flags ? "yes" : "no");
84
85 return CMD_OK;
86}
87
88void sendjupeburst(int hook, void *args) {
89 jupe_t *jupe = jupes;
90
91 if (hook != HOOK_IRC_SENDBURSTBURSTS)
92 return;
93
94 while (jupe) {
95 jupe_propagate(jupe);
96
97 jupe = jupe->ju_next;
98 }
99}
100
101jupe_t *make_jupe(char *server, char *reason, time_t expirets, time_t lastmod, unsigned int flags) {
102 jupe_t *jupe;
103
104 jupe = (jupe_t*)malloc(sizeof(jupe_t));
105
106 if (jupe == NULL)
107 return NULL;
108
109 jupe->ju_server = getsstring(server, HOSTLEN);
110 jupe->ju_reason = getsstring(reason, TOPICLEN);
111 jupe->ju_expire = expirets;
112 jupe->ju_lastmod = lastmod;
113 jupe->ju_flags = flags;
114 jupe->ju_next = NULL;
115
116 /* add the jupe to our linked list */
117 jupe->ju_next = jupes;
118 jupes = jupe;
119
120 return jupe;
121}
122
123void jupe_propagate(jupe_t *jupe) {
124 irc_send("%s JU * %c%s %jd %jd :%s", mynumeric->content,
125 JupeIsRemActive(jupe) ? '+' : '-',
126 JupeServer(jupe),
127 (intmax_t)(jupe->ju_expire - getnettime()),
128 (intmax_t)JupeLastMod(jupe),
129 JupeReason(jupe));
130}
131
132void jupe_expire(void) {
133 jupe_find(NULL);
134}
135
136jupe_t *jupe_find(char *server) {
137 jupe_t *jupe = jupes;
138
139 while (jupe) {
140 /* server == NULL if jupe_find() is used by jupe_expire */
141 if (server && ircd_strcmp(server, JupeServer(jupe)) == 0)
142 return jupe;
143
144 if (jupe->ju_next && jupe->ju_next->ju_expire < getnettime())
145 jupe_free(jupe->ju_next);
146
147 jupe = jupe->ju_next;
148 }
149
150 if (jupes && jupes->ju_expire < getnettime())
151 jupe_free(jupes);
152
153 return NULL;
154}
155
156void jupe_free(jupe_t *jupe) {
157 jupe_t *trav = jupes;
158
159 if (jupe == jupes)
160 jupes = jupe->ju_next;
161 else {
162 while (trav) {
163 if (trav->ju_next == jupe) {
164 trav->ju_next = jupe->ju_next;
165 break;
166 }
167
168 trav = trav->ju_next;
169 }
170 }
171
172 freesstring(jupe->ju_server);
173 freesstring(jupe->ju_reason);
174 free(jupe);
175}
176
177void jupe_activate(jupe_t *jupe) {
178 if (jupe->ju_flags & JUPE_ACTIVE)
179 return;
180
181 jupe->ju_flags |= JUPE_ACTIVE;
182 jupe->ju_lastmod = getnettime();
183
184 jupe_propagate(jupe);
185}
186
187void jupe_deactivate(jupe_t *jupe) {
188 if ((jupe->ju_flags & JUPE_ACTIVE) == 0)
189 return;
190
191 jupe->ju_flags &= ~JUPE_ACTIVE;
192 jupe->ju_lastmod = getnettime();
193
194 jupe_propagate(jupe);
195}
196
197int jupe_add(char *server, char *reason, time_t duration, unsigned int flags) {
198 jupe_t *jupe;
199
200 if (jupe_find(server) != NULL)
201 return 0;
202
203 if (duration > JUPE_MAX_EXPIRE || duration <= 0)
204 return 0;
205
206 jupe = make_jupe(server, reason, getnettime() + duration, getnettime(), flags);
207
208 if (jupe == NULL)
209 return 0;
210
211 jupe_propagate(jupe);
212
213 return 1;
214}