]>
Commit | Line | Data |
---|---|---|
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 | ||
13 | jupe_t *jupes = NULL; | |
14 | ||
15 | int handlejupe(void *source, int cargc, char **cargv); | |
16 | ||
17 | void _init() { | |
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(0); | |
22 | } | |
23 | ||
24 | registerserverhandler("JU", &handlejupe, 5); | |
25 | } | |
26 | ||
27 | void _fini() { | |
28 | jupe_t *next; | |
29 | ||
30 | while (jupes) { | |
31 | /* keep a pointer to the next item */ | |
32 | next = jupes->ju_next; | |
33 | ||
34 | free(jupes); | |
35 | ||
36 | jupes = next; | |
37 | } | |
38 | ||
39 | deregisterserverhandler("JU", &handlejupe); | |
40 | } | |
41 | ||
42 | int handlejupe(void *source, int cargc, char **cargv) { | |
43 | char *server, *expire, *modtime, *reason; | |
44 | jupe_t *jupe; | |
45 | unsigned int flags; | |
46 | ||
47 | if (cargc < 5) | |
48 | return CMD_OK; /* local jupe or not a valid.. we don't care */ | |
49 | ||
50 | server = cargv[1]; | |
51 | expire = cargv[2]; | |
52 | modtime = cargv[3]; | |
53 | reason = cargv[4]; | |
54 | ||
55 | if (atoi(expire) > JUPE_MAX_EXPIRE || atoi(expire) <= 0) | |
56 | return CMD_ERROR; /* jupe's expiry date is not valid */ | |
57 | ||
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 | } | |
64 | ||
65 | jupe = jupe_find(server); | |
66 | ||
67 | if (jupe != NULL && atoi(modtime) > jupe->ju_lastmod) { | |
68 | jupe->ju_flags = flags; | |
69 | jupe->ju_lastmod = atoi(modtime); | |
70 | ||
71 | Error("jupe", ERR_INFO, "jupe modified for %s (%s) expiring in %s," | |
72 | " lastmod: %s, active: %s", server, reason, expire, modtime, flags ? "yes" : "no"); | |
73 | return CMD_OK; | |
74 | } | |
75 | ||
76 | jupe = make_jupe(server, reason, getnettime() + atoi(expire), atoi(modtime), flags); | |
77 | ||
78 | if (jupe == NULL) | |
79 | return CMD_ERROR; | |
80 | ||
81 | Error("jupe", ERR_INFO, "jupe added for %s (%s) expiring in %s," | |
82 | " lastmod: %s, active: %s", server, reason, expire, modtime, flags ? "yes" : "no"); | |
83 | ||
84 | return CMD_OK; | |
85 | } | |
86 | ||
87 | jupe_t *make_jupe(char *server, char *reason, time_t expirets, time_t lastmod, unsigned int flags) { | |
88 | jupe_t *jupe; | |
89 | ||
90 | jupe = (jupe_t*)malloc(sizeof(jupe_t)); | |
91 | ||
92 | if (jupe == NULL) | |
93 | return NULL; | |
94 | ||
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; | |
101 | ||
102 | /* add the jupe to our linked list */ | |
103 | jupe->ju_next = jupes; | |
104 | jupes = jupe; | |
105 | ||
106 | return jupe; | |
107 | } | |
108 | ||
109 | void jupe_propagate(jupe_t *jupe) { | |
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)); | |
116 | } | |
117 | ||
118 | void jupe_expire(void) { | |
119 | jupe_find(NULL); | |
120 | } | |
121 | ||
122 | jupe_t *jupe_find(char *server) { | |
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; | |
129 | ||
130 | if (jupe->ju_next && jupe->ju_next->ju_expire < getnettime()) | |
131 | jupe_free(jupe->ju_next); | |
132 | ||
133 | jupe = jupe->ju_next; | |
134 | } | |
135 | ||
136 | if (jupes && jupes->ju_expire < getnettime()) | |
137 | jupe_free(jupes); | |
138 | ||
139 | return NULL; | |
140 | } | |
141 | ||
142 | void jupe_free(jupe_t *jupe) { | |
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); | |
161 | } | |
162 | ||
163 | void jupe_activate(jupe_t *jupe) { | |
164 | if (jupe->ju_flags & JUPE_ACTIVE) | |
165 | return; | |
166 | ||
167 | jupe->ju_flags |= JUPE_ACTIVE; | |
168 | jupe->ju_lastmod = getnettime(); | |
169 | ||
170 | jupe_propagate(jupe); | |
171 | } | |
172 | ||
173 | void jupe_deactivate(jupe_t *jupe) { | |
174 | if ((jupe->ju_flags & JUPE_ACTIVE) == 0) | |
175 | return; | |
176 | ||
177 | jupe->ju_flags &= ~JUPE_ACTIVE; | |
178 | jupe->ju_lastmod = getnettime(); | |
179 | ||
180 | jupe_propagate(jupe); | |
181 | } | |
182 | ||
183 | int jupe_add(char *server, char *reason, time_t duration, unsigned int flags) { | |
184 | jupe_t *jupe; | |
185 | ||
186 | if (jupe_find(server) != NULL) | |
187 | return 0; | |
188 | ||
189 | if (duration > JUPE_MAX_EXPIRE || duration <= 0) | |
190 | return 0; | |
191 | ||
192 | jupe = make_jupe(server, reason, getnettime() + duration, getnettime(), flags); | |
193 | ||
194 | if (jupe == NULL) | |
195 | return 0; | |
196 | ||
197 | jupe_propagate(jupe); | |
198 | ||
199 | return 1; | |
200 | } |