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