]> jfr.im git - irc/quakenet/newserv.git/blame - jupe/jupe.c
Jupe: Fix Whitespace in jupe module
[irc/quakenet/newserv.git] / jupe / jupe.c
CommitLineData
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 13jupe_t *jupes = NULL;
01250ca8
P
14
15int handlejupe(void *source, int cargc, char **cargv);
16void sendjupeburst(int hook, void *args);
17
18void _init() {
64bf1b4e 19 registerhook(HOOK_IRC_SENDBURSTBURSTS, &sendjupeburst);
01250ca8 20
64bf1b4e 21 registerserverhandler("JU", &handlejupe, 5);
b6c52dd6 22
64bf1b4e 23 irc_send("%s RB J", mynumeric->content);
01250ca8
P
24}
25
26void _fini() {
64bf1b4e 27 jupe_t *next;
01250ca8 28
64bf1b4e
P
29 while (jupes) {
30 /* keep a pointer to the next item */
31 next = jupes->ju_next;
01250ca8 32
64bf1b4e 33 free(jupes);
01250ca8 34
64bf1b4e
P
35 jupes = next;
36 }
01250ca8 37
64bf1b4e 38 deregisterhook(HOOK_IRC_SENDBURSTBURSTS, &sendjupeburst);
01250ca8 39
64bf1b4e 40 deregisterserverhandler("JU", &handlejupe);
01250ca8
P
41}
42
43int handlejupe(void *source, int cargc, char **cargv) {
64bf1b4e
P
44 char *server, *expire, *modtime, *reason;
45 jupe_t *jupe;
46 unsigned int flags;
01250ca8 47
64bf1b4e
P
48 if (cargc < 5)
49 return CMD_OK; /* local jupe or not a valid.. we don't care */
01250ca8 50
64bf1b4e
P
51 server = cargv[1];
52 expire = cargv[2];
53 modtime = cargv[3];
54 reason = cargv[4];
01250ca8 55
64bf1b4e
P
56 if (atoi(expire) > JUPE_MAX_EXPIRE || atoi(expire) <= 0)
57 return CMD_ERROR; /* jupe's expiry date is not valid */
01250ca8 58
64bf1b4e
P
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 }
01250ca8 65
64bf1b4e 66 jupe = jupe_find(server);
01250ca8 67
64bf1b4e
P
68 if (jupe != NULL && atoi(modtime) > jupe->ju_lastmod) {
69 jupe->ju_flags = flags;
70 jupe->ju_lastmod = atoi(modtime);
01250ca8 71
64bf1b4e
P
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 }
01250ca8 76
64bf1b4e 77 jupe = make_jupe(server, reason, getnettime() + atoi(expire), atoi(modtime), flags);
01250ca8 78
64bf1b4e
P
79 if (jupe == NULL)
80 return CMD_ERROR;
01250ca8 81
64bf1b4e
P
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");
01250ca8 84
64bf1b4e 85 return CMD_OK;
01250ca8
P
86}
87
88void sendjupeburst(int hook, void *args) {
64bf1b4e 89 jupe_t *jupe = jupes;
01250ca8 90
64bf1b4e
P
91 if (hook != HOOK_IRC_SENDBURSTBURSTS)
92 return;
01250ca8 93
64bf1b4e
P
94 while (jupe) {
95 jupe_propagate(jupe);
01250ca8 96
64bf1b4e
P
97 jupe = jupe->ju_next;
98 }
01250ca8
P
99}
100
101jupe_t *make_jupe(char *server, char *reason, time_t expirets, time_t lastmod, unsigned int flags) {
64bf1b4e 102 jupe_t *jupe;
01250ca8 103
64bf1b4e 104 jupe = (jupe_t*)malloc(sizeof(jupe_t));
01250ca8 105
64bf1b4e
P
106 if (jupe == NULL)
107 return NULL;
01250ca8 108
64bf1b4e
P
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;
01250ca8 115
64bf1b4e
P
116 /* add the jupe to our linked list */
117 jupe->ju_next = jupes;
118 jupes = jupe;
01250ca8 119
64bf1b4e 120 return jupe;
01250ca8
P
121}
122
123void jupe_propagate(jupe_t *jupe) {
64bf1b4e
P
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));
01250ca8
P
130}
131
132void jupe_expire(void) {
64bf1b4e 133 jupe_find(NULL);
01250ca8
P
134}
135
136jupe_t *jupe_find(char *server) {
64bf1b4e
P
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;
01250ca8 143
64bf1b4e
P
144 if (jupe->ju_next && jupe->ju_next->ju_expire < getnettime())
145 jupe_free(jupe->ju_next);
01250ca8 146
64bf1b4e
P
147 jupe = jupe->ju_next;
148 }
01250ca8 149
64bf1b4e
P
150 if (jupes && jupes->ju_expire < getnettime())
151 jupe_free(jupes);
01250ca8 152
64bf1b4e 153 return NULL;
01250ca8
P
154}
155
156void jupe_free(jupe_t *jupe) {
64bf1b4e
P
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);
01250ca8
P
175}
176
177void jupe_activate(jupe_t *jupe) {
64bf1b4e
P
178 if (jupe->ju_flags & JUPE_ACTIVE)
179 return;
01250ca8 180
64bf1b4e
P
181 jupe->ju_flags |= JUPE_ACTIVE;
182 jupe->ju_lastmod = getnettime();
01250ca8 183
64bf1b4e 184 jupe_propagate(jupe);
01250ca8
P
185}
186
187void jupe_deactivate(jupe_t *jupe) {
64bf1b4e
P
188 if ((jupe->ju_flags & JUPE_ACTIVE) == 0)
189 return;
01250ca8 190
64bf1b4e
P
191 jupe->ju_flags &= ~JUPE_ACTIVE;
192 jupe->ju_lastmod = getnettime();
01250ca8 193
64bf1b4e 194 jupe_propagate(jupe);
01250ca8
P
195}
196
197int jupe_add(char *server, char *reason, time_t duration, unsigned int flags) {
64bf1b4e 198 jupe_t *jupe;
01250ca8 199
64bf1b4e
P
200 if (jupe_find(server) != NULL)
201 return 0;
202
203 if (duration > JUPE_MAX_EXPIRE || duration <= 0)
204 return 0;
01250ca8 205
64bf1b4e 206 jupe = make_jupe(server, reason, getnettime() + duration, getnettime(), flags);
01250ca8 207
64bf1b4e
P
208 if (jupe == NULL)
209 return 0;
01250ca8 210
64bf1b4e 211 jupe_propagate(jupe);
01250ca8 212
64bf1b4e 213 return 1;
01250ca8 214}