2 * IRC - Internet Relay Chat, ircd/jupe.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Finland
5 * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 1, or (at your option)
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * @brief Implementation of juped server handling functions.
31 #include "ircd_alloc.h"
32 #include "ircd_features.h"
34 #include "ircd_reply.h"
35 #include "ircd_string.h"
44 #include "sys.h" /* FALSE bleah */
50 static struct Jupe *GlobalJupeList = 0;
52 /** Allocate a new jupe with the given parameters.
53 * @param[in] server Server name to jupe.
54 * @param[in] reason Reason for jupe.
55 * @param[in] expire Expiration time for jupe.
56 * @param[in] lastmod Last modification time for jupe.
57 * @param[in] flags Flags to set for the jupe.
60 make_jupe(char *server, char *reason, time_t expire, time_t lastmod,
65 ajupe = (struct Jupe*) MyMalloc(sizeof(struct Jupe)); /* alloc memory */
68 DupString(ajupe->ju_server, server); /* copy vital information */
69 DupString(ajupe->ju_reason, reason);
70 ajupe->ju_expire = expire;
71 ajupe->ju_lastmod = lastmod;
72 ajupe->ju_flags = flags & JUPE_MASK; /* set jupe flags */
74 ajupe->ju_next = GlobalJupeList; /* link it into the list */
75 ajupe->ju_prev_p = &GlobalJupeList;
77 GlobalJupeList->ju_prev_p = &ajupe->ju_next;
78 GlobalJupeList = ajupe;
84 * @param[in] cptr Local client that sent us the jupe.
85 * @param[in] sptr Originator of the jupe.
86 * @param[in] jupe Jupe to check.
89 do_jupe(struct Client *cptr, struct Client *sptr, struct Jupe *jupe)
93 if (!JupeIsActive(jupe)) /* no action to be taken on inactive jupes */
96 acptr = FindServer(jupe->ju_server);
98 /* server isn't online or isn't local or is me */
99 if (!acptr || !MyConnect(acptr) || IsMe(acptr))
102 return exit_client_msg(cptr, acptr, &me, "Juped: %s", jupe->ju_reason);
105 /** Forward a jupe to another server.
106 * @param[in] cptr Local client that sent us the jupe.
107 * @param[in] sptr Originator of the jupe.
108 * @param[in] jupe Jupe to forward.
111 propagate_jupe(struct Client *cptr, struct Client *sptr, struct Jupe *jupe)
113 if (JupeIsLocal(jupe)) /* don't propagate local jupes */
116 sendcmdto_serv_butone(sptr, CMD_JUPE, cptr, "* %c%s %Tu %Tu :%s",
117 JupeIsRemActive(jupe) ? '+' : '-', jupe->ju_server,
118 jupe->ju_expire - CurrentTime, jupe->ju_lastmod,
122 /** Add a new server jupe.
123 * @param[in] cptr Local client that sent us the jupe.
124 * @param[in] sptr Originator of the jupe.
125 * @param[in] server Server name to jupe.
126 * @param[in] reason Reason for the jupe.
127 * @param[in] expire Jupe duration in seconds.
128 * @param[in] lastmod Last modification timestamp (or NULL).
129 * @param[in] flags Flags to set on jupe.
130 * @return Zero, unless the jupe causes \a cptr to be SQUIT, in which
134 jupe_add(struct Client *cptr, struct Client *sptr, char *server, char *reason,
135 time_t expire, time_t lastmod, unsigned int flags)
143 * You cannot set a negative (or zero) expire time, nor can you set an
144 * expiration time for greater than JUPE_MAX_EXPIRE.
146 if (expire <= 0 || expire > JUPE_MAX_EXPIRE) {
147 if (!IsServer(cptr) && MyConnect(cptr))
148 send_reply(cptr, ERR_BADEXPIRE, expire);
152 expire += CurrentTime; /* convert from lifetime to timestamp */
154 /* Inform ops and log it */
155 sendto_opmask_butone(0, SNO_NETWORK, "%s adding %sJUPE for %s, expiring at "
157 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
159 cli_name((cli_user(sptr))->server),
160 flags & JUPE_LOCAL ? "local " : "", server,
161 expire + TSoffset, reason);
163 log_write(LS_JUPE, L_INFO, LOG_NOSNOTICE,
164 "%#C adding %sJUPE for %s, expiring at %Tu: %s", sptr,
165 flags & JUPE_LOCAL ? "local " : "", server, expire + TSoffset,
169 ajupe = make_jupe(server, reason, expire, lastmod, flags);
171 propagate_jupe(cptr, sptr, ajupe);
173 return do_jupe(cptr, sptr, ajupe); /* remove server if necessary */
176 /** Activate a jupe, optionally changing its lastmod and flags.
177 * @param[in] cptr Local client that sent us the jupe.
178 * @param[in] sptr Originator of the jupe.
179 * @param[in] jupe Jupe to activate.
180 * @param[in] lastmod New timestamp for last modification of the jupe.
181 * @param[in] flags Flags to set on the jupe.
182 * @return Zero, unless the jupe causes \a cptr to be SQUIT, in which
186 jupe_activate(struct Client *cptr, struct Client *sptr, struct Jupe *jupe,
187 time_t lastmod, unsigned int flags)
189 unsigned int saveflags = 0;
193 saveflags = jupe->ju_flags;
195 if (flags & JUPE_LOCAL)
196 jupe->ju_flags &= ~JUPE_LDEACT;
198 jupe->ju_flags |= JUPE_ACTIVE;
200 if (jupe->ju_lastmod >= lastmod) /* force lastmod to increase */
203 jupe->ju_lastmod = lastmod;
206 if ((saveflags & JUPE_ACTMASK) == JUPE_ACTIVE)
207 return 0; /* was active to begin with */
209 /* Inform ops and log it */
210 sendto_opmask_butone(0, SNO_NETWORK, "%s activating JUPE for %s, expiring "
212 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
214 cli_name((cli_user(sptr))->server),
215 jupe->ju_server, jupe->ju_expire + TSoffset,
218 log_write(LS_JUPE, L_INFO, LOG_NOSNOTICE,
219 "%#C activating JUPE for %s, expiring at %Tu: %s",sptr,
220 jupe->ju_server, jupe->ju_expire + TSoffset, jupe->ju_reason);
222 if (!(flags & JUPE_LOCAL)) /* don't propagate local changes */
223 propagate_jupe(cptr, sptr, jupe);
225 return do_jupe(cptr, sptr, jupe);
228 /** Deactivate a jupe.
229 * @param[in] cptr Local client that sent us the jupe.
230 * @param[in] sptr Originator of the jupe.
231 * @param[in] jupe Jupe to deactivate.
232 * @param[in] lastmod New timestamp for last modification of the jupe.
233 * @param[in] flags Flags to set on the jupe.
237 jupe_deactivate(struct Client *cptr, struct Client *sptr, struct Jupe *jupe,
238 time_t lastmod, unsigned int flags)
240 unsigned int saveflags = 0;
244 saveflags = jupe->ju_flags;
246 if (!JupeIsLocal(jupe)) {
247 if (flags & JUPE_LOCAL)
248 jupe->ju_flags |= JUPE_LDEACT;
250 jupe->ju_flags &= ~JUPE_ACTIVE;
252 if (jupe->ju_lastmod >= lastmod) /* force lastmod to increase */
255 jupe->ju_lastmod = lastmod;
258 if ((saveflags & JUPE_ACTMASK) != JUPE_ACTIVE)
259 return 0; /* was inactive to begin with */
262 /* Inform ops and log it */
263 sendto_opmask_butone(0, SNO_NETWORK, "%s %s JUPE for %s, expiring at %Tu: "
265 (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
267 cli_name((cli_user(sptr))->server),
268 JupeIsLocal(jupe) ? "removing local" : "deactivating",
269 jupe->ju_server, jupe->ju_expire + TSoffset,
272 log_write(LS_JUPE, L_INFO, LOG_NOSNOTICE,
273 "%#C %s JUPE for %s, expiring at %Tu: %s", sptr,
274 JupeIsLocal(jupe) ? "removing local" : "deactivating",
275 jupe->ju_server, jupe->ju_expire + TSoffset, jupe->ju_reason);
277 if (JupeIsLocal(jupe))
279 else if (!(flags & JUPE_LOCAL)) /* don't propagate local changes */
280 propagate_jupe(cptr, sptr, jupe);
285 /** Find a jupe by name.
286 * @param[in] server %Jupe name to search for.
287 * @return Matching jupe (or NULL if none match).
290 jupe_find(char *server)
295 for (jupe = GlobalJupeList; jupe; jupe = sjupe) { /* go through jupes */
296 sjupe = jupe->ju_next;
298 if (jupe->ju_expire <= CurrentTime) /* expire any that need expiring */
300 else if (0 == ircd_strcmp(server, jupe->ju_server)) /* found it yet? */
307 /** Unlink and free an unused jupe.
308 * @param[in] jupe Server jupe to free.
311 jupe_free(struct Jupe* jupe)
315 *jupe->ju_prev_p = jupe->ju_next; /* squeeze this jupe out */
317 jupe->ju_next->ju_prev_p = jupe->ju_prev_p;
319 MyFree(jupe->ju_server); /* and free up the memory */
320 MyFree(jupe->ju_reason);
324 /** Send the full list of active global jupes to \a cptr.
325 * @param[in] cptr Local server to send jupes to.
328 jupe_burst(struct Client *cptr)
333 for (jupe = GlobalJupeList; jupe; jupe = sjupe) { /* go through jupes */
334 sjupe = jupe->ju_next;
336 if (jupe->ju_expire <= CurrentTime) /* expire any that need expiring */
338 else if (!JupeIsLocal(jupe)) /* forward global jupes */
339 sendcmdto_one(&me, CMD_JUPE, cptr, "* %c%s %Tu %Tu :%s",
340 JupeIsRemActive(jupe) ? '+' : '-', jupe->ju_server,
341 jupe->ju_expire - CurrentTime, jupe->ju_lastmod,
346 /** Forward a jupe to another server.
347 * @param[in] cptr %Server to send jupe to.
348 * @param[in] jupe Jupe to forward.
351 jupe_resend(struct Client *cptr, struct Jupe *jupe)
353 if (JupeIsLocal(jupe)) /* don't propagate local jupes */
356 sendcmdto_one(&me, CMD_JUPE, cptr, "* %c%s %Tu %Tu :%s",
357 JupeIsRemActive(jupe) ? '+' : '-', jupe->ju_server,
358 jupe->ju_expire - CurrentTime, jupe->ju_lastmod,
364 /** Send a jupe (or a list of jupes) to a server.
365 * @param[in] sptr Client searching for jupes.
366 * @param[in] server Name of jupe to search for (if NULL, list all).
370 jupe_list(struct Client *sptr, char *server)
376 if (!(jupe = jupe_find(server))) /* no such jupe */
377 return send_reply(sptr, ERR_NOSUCHJUPE, server);
379 /* send jupe information along */
380 send_reply(sptr, RPL_JUPELIST, jupe->ju_server, jupe->ju_expire + TSoffset,
381 JupeIsLocal(jupe) ? cli_name(&me) : "*",
382 JupeIsActive(jupe) ? '+' : '-', jupe->ju_reason);
384 for (jupe = GlobalJupeList; jupe; jupe = sjupe) { /* go through jupes */
385 sjupe = jupe->ju_next;
387 if (jupe->ju_expire <= CurrentTime) /* expire any that need expiring */
389 else /* send jupe information along */
390 send_reply(sptr, RPL_JUPELIST, jupe->ju_server,
391 jupe->ju_expire + TSoffset,
392 JupeIsLocal(jupe) ? cli_name(&me) : "*",
393 JupeIsActive(jupe) ? '+' : '-', jupe->ju_reason);
397 /* end of jupe information */
398 return send_reply(sptr, RPL_ENDOFJUPELIST);
401 /** Count jupes and memory used by them.
402 * @param[out] ju_size Receives total number of bytes allocated for jupes.
403 * @return Number of jupes currently allocated.
406 jupe_memory_count(size_t *ju_size)
411 for (jupe = GlobalJupeList; jupe; jupe = jupe->ju_next)
414 *ju_size += sizeof(struct Jupe);
415 *ju_size += jupe->ju_server ? (strlen(jupe->ju_server) + 1) : 0;
416 *ju_size += jupe->ju_reason ? (strlen(jupe->ju_reason) + 1) : 0;