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