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