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