Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / jupe.c
1 /*
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>
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
20  *
21  * $Id$
22  */
23 #include "jupe.h"
24 #include "client.h"
25 #include "hash.h"
26 #include "ircd.h"
27 #include "ircd_alloc.h"
28 #include "ircd_reply.h"
29 #include "ircd_string.h"
30 #include "match.h"
31 #include "msg.h"
32 #include "numeric.h"
33 #include "numnicks.h"
34 #include "s_bsd.h"
35 #include "s_misc.h"
36 #include "send.h"
37 #include "struct.h"
38 #include "support.h"
39 #include "sys.h"    /* FALSE bleah */
40
41 #include <assert.h>
42
43 static struct Jupe *GlobalJupeList = 0;
44
45 static struct Jupe *
46 make_jupe(char *server, char *reason, time_t expire, time_t lastmod,
47           unsigned int flags)
48 {
49   struct Jupe *ajupe;
50
51   ajupe = (struct Jupe*) MyMalloc(sizeof(struct Jupe)); /* alloc memory */
52   assert(0 != ajupe);
53
54   DupString(ajupe->ju_server, server);        /* copy vital information */
55   DupString(ajupe->ju_reason, reason);
56   ajupe->ju_expire = expire;
57   ajupe->ju_lastmod = lastmod;
58   ajupe->ju_flags = flags;        /* set jupe flags */
59
60   ajupe->ju_next = GlobalJupeList;       /* link it into the list */
61   ajupe->ju_prev_p = &GlobalJupeList;
62   if (GlobalJupeList)
63     GlobalJupeList->ju_prev_p = &ajupe->ju_next;
64   GlobalJupeList = ajupe;
65
66   return ajupe;
67 }
68
69 static int
70 do_jupe(struct Client *cptr, struct Client *sptr, struct Jupe *jupe)
71 {
72   struct Client *acptr;
73
74   if (!JupeIsActive(jupe)) /* no action to be taken on inactive jupes */
75     return 0;
76
77   acptr = FindServer(jupe->ju_server);
78
79   /* server isn't online or isn't local or is me */
80   if (!acptr || !MyConnect(acptr) || IsMe(acptr))
81     return 0;
82
83   return exit_client_msg(cptr, acptr, &me, "Juped: %s", jupe->ju_reason);
84 }
85
86 static void
87 propagate_jupe(struct Client *cptr, struct Client *sptr, struct Jupe *jupe)
88 {
89   if (JupeIsLocal(jupe)) /* don't propagate local jupes */
90     return;
91
92   sendcmdto_serv_butone(cptr, CMD_JUPE, sptr, "* %c%s %Tu %Tu :%s",
93                         JupeIsActive(jupe) ? '+' : '-', jupe->ju_server,
94                         jupe->ju_expire - CurrentTime, jupe->ju_lastmod,
95                         jupe->ju_reason);
96 }
97
98 int
99 jupe_add(struct Client *cptr, struct Client *sptr, char *server, char *reason,
100          time_t expire, time_t lastmod, int local, int active)
101 {
102   struct Jupe *ajupe;
103   unsigned int flags = 0;
104
105   assert(0 != server);
106   assert(0 != reason);
107
108   /*
109    * You cannot set a negative (or zero) expire time, nor can you set an
110    * expiration time for greater than JUPE_MAX_EXPIRE.
111    */
112   if (expire <= 0 || expire > JUPE_MAX_EXPIRE) {
113     if (!IsServer(cptr) && MyConnect(cptr))
114       send_error_to_client(cptr, ERR_BADEXPIRE, expire);
115     return 0;
116   }
117
118   expire += CurrentTime; /* convert from lifetime to timestamp */
119
120   /* Inform ops and log it */
121     sendto_op_mask(SNO_NETWORK, "%s adding %sJUPE for %s, expiring at "
122                    TIME_T_FMT ": %s",
123                    IsServer(sptr) ? sptr->name : sptr->user->server->name,
124                    local ? "local " : "", server, expire + TSoffset, reason);
125
126 #ifdef JPATH
127   if (IsServer(sptr))
128     write_log(JPATH, TIME_T_FMT " %s adding %sJUPE for %s, expiring at "
129               TIME_T_FMT ": %s\n", TStime(), sptr->name,
130               local ? "local " : "", server, expire + TSoffset, reason);
131   else
132     write_log(JPATH, TIME_T_FMT, " %s!%s@%s adding %sJUPE for %s, expiring at "
133               TIME_T_FMT ": %s\n", TStime(), sptr->name, sptr->user->username,
134               sptr->user->host, local ? "local " : "", server,
135               expire + TSoffset, reason);
136 #endif /* JPATH */
137
138   if (active) /* compute initial flags */
139     flags |= JUPE_ACTIVE;
140   if (local)
141     flags |= JUPE_LOCAL;
142
143   /* make the jupe */
144   ajupe = make_jupe(server, reason, expire, lastmod, flags);
145
146   propagate_jupe(cptr, sptr, ajupe);
147
148   return do_jupe(cptr, sptr, ajupe); /* remove server if necessary */
149 }
150
151 int
152 jupe_activate(struct Client *cptr, struct Client *sptr, struct Jupe *jupe,
153               time_t lastmod)
154 {
155   assert(0 != jupe);
156   assert(!JupeIsLocal(jupe));
157
158   jupe->ju_flags |= JUPE_ACTIVE;
159
160   if (jupe->ju_lastmod >= lastmod) /* force lastmod to increase */
161     jupe->ju_lastmod++;
162   else
163     jupe->ju_lastmod = lastmod;
164
165   /* Inform ops and log it */
166   sendto_op_mask(SNO_NETWORK, "%s activating JUPE for %s, expiring at "
167                  TIME_T_FMT ": %s",
168                  IsServer(sptr) ? sptr->name : sptr->user->server->name,
169                  jupe->ju_server, jupe->ju_expire + TSoffset, jupe->ju_reason);
170
171 #ifdef JPATH
172   if (IsServer(sptr))
173     write_log(JPATH, TIME_T_FMT " %s activating JUPE for %s, expiring at "
174               TIME_T_FMT ": %s\n", TStime(), sptr->name, jupe->ju_server,
175               jupe->ju_expire + TSoffset, jupe->ju_reason);
176   else
177     write_log(JPATH, TIME_T_FMT, " %s!%s@%s activating JUPE for %s, "
178               "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
179               sptr->user->username, sptr->user->host, jupe->ju_server,
180               jupe->ju_expire + TSoffset, jupe->ju_reason);
181 #endif /* JPATH */
182
183   propagate_jupe(cptr, sptr, jupe);
184
185   return do_jupe(cptr, sptr, jupe);
186 }
187
188 int
189 jupe_deactivate(struct Client *cptr, struct Client *sptr, struct Jupe *jupe,
190                 time_t lastmod)
191 {
192   assert(0 != jupe);
193
194   if (!JupeIsLocal(jupe)) {
195     jupe->ju_flags &= ~JUPE_ACTIVE;
196
197     if (jupe->ju_lastmod >= lastmod) /* force lastmod to increase */
198       jupe->ju_lastmod++;
199     else
200       jupe->ju_lastmod = lastmod;
201   }
202
203   /* Inform ops and log it */
204   sendto_op_mask(SNO_NETWORK, "%s %s JUPE for %s, expiring at " TIME_T_FMT
205                  ": %s",
206                  IsServer(sptr) ? sptr->name : sptr->user->server->name,
207                  JupeIsLocal(jupe) ? "removing local" : "deactivating",
208                  jupe->ju_server, jupe->ju_expire + TSoffset, jupe->ju_reason);
209
210 #ifdef JPATH
211   if (IsServer(sptr))
212     write_log(JPATH, TIME_T_FMT " %s %s JUPE for %s, expiring at " TIME_T_FMT
213               ": %s\n", TStime(), sptr->name,
214               JupeIsLocal(jupe) ? "removing local" : "deactivating",
215               jupe->ju_server, jupe->ju_expire + TSoffset, jupe->ju_reason);
216   else
217     write_log(JPATH, TIME_T_FMT, " %s!%s@%s %s JUPE for %s, "
218               "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
219               sptr->user->username, sptr->user->host,
220               JupeIsLocal(jupe) ? "removing local" : "deactivating",
221               jupe->ju_server, jupe->ju_expire + TSoffset, jupe->ju_reason);
222 #endif /* JPATH */
223
224   if (JupeIsLocal(jupe))
225     jupe_free(jupe);
226   else
227     propagate_jupe(cptr, sptr, jupe);
228
229   return 0;
230 }
231
232 struct Jupe *
233 jupe_find(char *server)
234 {
235   struct Jupe* jupe;
236   struct Jupe* sjupe;
237
238   for (jupe = GlobalJupeList; jupe; jupe = sjupe) { /* go through jupes */
239     sjupe = jupe->ju_next;
240
241     if (jupe->ju_expire <= CurrentTime) /* expire any that need expiring */
242       jupe_free(jupe);
243     else if (0 == ircd_strcmp(server, jupe->ju_server)) /* found it yet? */
244       return jupe;
245   }
246
247   return 0;
248 }
249
250 void
251 jupe_free(struct Jupe* jupe)
252 {
253   assert(0 != jupe);
254
255   *jupe->ju_prev_p = jupe->ju_next; /* squeeze this jupe out */
256   if (jupe->ju_next)
257     jupe->ju_next->ju_prev_p = jupe->ju_prev_p;
258
259   MyFree(jupe->ju_server);  /* and free up the memory */
260   MyFree(jupe->ju_reason);
261   MyFree(jupe);
262 }
263
264 void
265 jupe_burst(struct Client *cptr)
266 {
267   struct Jupe *jupe;
268   struct Jupe *sjupe;
269
270   for (jupe = GlobalJupeList; jupe; jupe = sjupe) { /* go through jupes */
271     sjupe = jupe->ju_next;
272
273     if (jupe->ju_expire <= CurrentTime) /* expire any that need expiring */
274       jupe_free(jupe);
275     else if (!JupeIsLocal(jupe)) /* forward global jupes */
276       sendcmdto_one(cptr, CMD_JUPE, &me, "* %c%s %Tu %Tu :%s",
277                     JupeIsActive(jupe) ? '+' : '-', jupe->ju_server,
278                     jupe->ju_expire - CurrentTime, jupe->ju_lastmod,
279                     jupe->ju_reason);
280   }
281 }
282
283 int
284 jupe_resend(struct Client *cptr, struct Jupe *jupe)
285 {
286   if (JupeIsLocal(jupe)) /* don't propagate local jupes */
287     return 0;
288
289   sendcmdto_one(cptr, CMD_JUPE, &me, "* %c%s %Tu %Tu :%s",
290                 JupeIsActive(jupe) ? '+' : '-', jupe->ju_server,
291                 jupe->ju_expire - CurrentTime, jupe->ju_lastmod,
292                 jupe->ju_reason);
293
294   return 0;
295 }
296
297 int
298 jupe_list(struct Client *sptr, char *server)
299 {
300   struct Jupe *jupe;
301   struct Jupe *sjupe;
302
303   if (server) {
304     if (!(jupe = jupe_find(server))) /* no such jupe */
305       return send_error_to_client(sptr, ERR_NOSUCHJUPE, server);
306
307     /* send jupe information along */
308     sendto_one(sptr, rpl_str(RPL_JUPELIST), me.name, sptr->name,
309                jupe->ju_server, jupe->ju_expire + TSoffset,
310                JupeIsLocal(jupe) ? me.name : "*",
311                JupeIsActive(jupe) ? '+' : '-', jupe->ju_reason);
312   } else {
313     for (jupe = GlobalJupeList; jupe; jupe = sjupe) { /* go through jupes */
314       sjupe = jupe->ju_next;
315
316       if (jupe->ju_expire <= CurrentTime) /* expire any that need expiring */
317         jupe_free(jupe);
318       else /* send jupe information along */
319         sendto_one(sptr, rpl_str(RPL_JUPELIST), me.name, sptr->name,
320                    jupe->ju_server, jupe->ju_expire + TSoffset,
321                    JupeIsLocal(jupe) ? me.name : "*",
322                    JupeIsActive(jupe) ? '+' : '-', jupe->ju_reason);
323     }
324   }
325
326   /* end of jupe information */
327   sendto_one(sptr, rpl_str(RPL_ENDOFJUPELIST), me.name, sptr->name);
328   return 0;
329 }