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 "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_reply.h"
32 #include "ircd_string.h"
33 #include "match.h"
34 #include "msg.h"
35 #include "numeric.h"
36 #include "numnicks.h"
37 #include "s_bsd.h"
38 #include "s_misc.h"
39 #include "send.h"
40 #include "struct.h"
41 #include "support.h"
42 #include "sys.h"    /* FALSE bleah */
43
44 #include <assert.h>
45 #include <string.h>
46
47 static struct Jupe *GlobalJupeList = 0;
48
49 static struct Jupe *
50 make_jupe(char *server, char *reason, time_t expire, time_t lastmod,
51           unsigned int flags)
52 {
53   struct Jupe *ajupe;
54
55   ajupe = (struct Jupe*) MyMalloc(sizeof(struct Jupe)); /* alloc memory */
56   assert(0 != ajupe);
57
58   DupString(ajupe->ju_server, server); /* copy vital information */
59   DupString(ajupe->ju_reason, reason);
60   ajupe->ju_expire = expire;
61   ajupe->ju_lastmod = lastmod;
62   ajupe->ju_flags = flags & JUPE_MASK; /* set jupe flags */
63
64   ajupe->ju_next = GlobalJupeList; /* link it into the list */
65   ajupe->ju_prev_p = &GlobalJupeList;
66   if (GlobalJupeList)
67     GlobalJupeList->ju_prev_p = &ajupe->ju_next;
68   GlobalJupeList = ajupe;
69
70   return ajupe;
71 }
72
73 static int
74 do_jupe(struct Client *cptr, struct Client *sptr, struct Jupe *jupe)
75 {
76   struct Client *acptr;
77
78   if (!JupeIsActive(jupe)) /* no action to be taken on inactive jupes */
79     return 0;
80
81   acptr = FindServer(jupe->ju_server);
82
83   /* server isn't online or isn't local or is me */
84   if (!acptr || !MyConnect(acptr) || IsMe(acptr))
85     return 0;
86
87   return exit_client_msg(cptr, acptr, &me, "Juped: %s", jupe->ju_reason);
88 }
89
90 static void
91 propagate_jupe(struct Client *cptr, struct Client *sptr, struct Jupe *jupe)
92 {
93   if (JupeIsLocal(jupe)) /* don't propagate local jupes */
94     return;
95
96   sendcmdto_serv_butone(sptr, CMD_JUPE, cptr, "* %c%s %Tu %Tu :%s",
97                         JupeIsRemActive(jupe) ? '+' : '-', jupe->ju_server,
98                         jupe->ju_expire - CurrentTime, jupe->ju_lastmod,
99                         jupe->ju_reason);
100 }
101
102 int
103 jupe_add(struct Client *cptr, struct Client *sptr, char *server, char *reason,
104          time_t expire, time_t lastmod, unsigned int flags)
105 {
106   struct Jupe *ajupe;
107
108   assert(0 != server);
109   assert(0 != reason);
110
111   /*
112    * You cannot set a negative (or zero) expire time, nor can you set an
113    * expiration time for greater than JUPE_MAX_EXPIRE.
114    */
115   if (expire <= 0 || expire > JUPE_MAX_EXPIRE) {
116     if (!IsServer(cptr) && MyConnect(cptr))
117       send_reply(cptr, ERR_BADEXPIRE, expire);
118     return 0;
119   }
120
121   expire += CurrentTime; /* convert from lifetime to timestamp */
122
123   /* Inform ops and log it */
124   sendto_opmask_butone(0, SNO_NETWORK, "%s adding %sJUPE for %s, expiring at "
125                        "%Tu: %s", IsServer(sptr) ? cli_name(sptr) :
126                        cli_name(cli_user(sptr)->server),
127                        flags & JUPE_LOCAL ? "local " : "", server,
128                        expire + TSoffset, reason);
129
130   log_write(LS_JUPE, L_INFO, LOG_NOSNOTICE,
131             "%#C adding %sJUPE for %s, expiring at %Tu: %s", sptr,
132             flags & JUPE_LOCAL ? "local " : "", server, expire + TSoffset,
133             reason);
134
135   /* make the jupe */
136   ajupe = make_jupe(server, reason, expire, lastmod, flags);
137
138   propagate_jupe(cptr, sptr, ajupe);
139
140   return do_jupe(cptr, sptr, ajupe); /* remove server if necessary */
141 }
142
143 int
144 jupe_activate(struct Client *cptr, struct Client *sptr, struct Jupe *jupe,
145               time_t lastmod, unsigned int flags)
146 {
147   unsigned int saveflags = 0;
148
149   assert(0 != jupe);
150   assert(!JupeIsLocal(jupe));
151
152   saveflags = jupe->ju_flags;
153
154   if (flags & JUPE_LOCAL)
155     jupe->ju_flags &= ~JUPE_LDEACT;
156   else {
157     jupe->ju_flags |= JUPE_ACTIVE;
158
159     if (jupe->ju_lastmod >= lastmod) /* force lastmod to increase */
160       jupe->ju_lastmod++;
161     else
162       jupe->ju_lastmod = lastmod;
163   }
164
165   if ((saveflags & JUPE_ACTMASK) == JUPE_ACTIVE)
166     return 0; /* was active to begin with */
167
168   /* Inform ops and log it */
169   sendto_opmask_butone(0, SNO_NETWORK, "%s activating JUPE for %s, expiring "
170                        "at %Tu: %s",
171                        IsServer(sptr) ? cli_name(sptr) : cli_name(cli_user(sptr)->server),
172                        jupe->ju_server, jupe->ju_expire + TSoffset,
173                        jupe->ju_reason);
174
175   log_write(LS_JUPE, L_INFO, LOG_NOSNOTICE,
176             "%#C activating JUPE for %s, expiring at %Tu: %s",sptr,
177             jupe->ju_server, jupe->ju_expire + TSoffset, jupe->ju_reason);
178
179   if (!(flags & JUPE_LOCAL)) /* don't propagate local changes */
180     propagate_jupe(cptr, sptr, jupe);
181
182   return do_jupe(cptr, sptr, jupe);
183 }
184
185 int
186 jupe_deactivate(struct Client *cptr, struct Client *sptr, struct Jupe *jupe,
187                 time_t lastmod, unsigned int flags)
188 {
189   unsigned int saveflags = 0;
190
191   assert(0 != jupe);
192
193   saveflags = jupe->ju_flags;
194
195   if (!JupeIsLocal(jupe)) {
196     if (flags & JUPE_LOCAL)
197       jupe->ju_flags |= JUPE_LDEACT;
198     else {
199       jupe->ju_flags &= ~JUPE_ACTIVE;
200
201       if (jupe->ju_lastmod >= lastmod) /* force lastmod to increase */
202         jupe->ju_lastmod++;
203       else
204         jupe->ju_lastmod = lastmod;
205     }
206
207     if ((saveflags & JUPE_ACTMASK) != JUPE_ACTIVE)
208       return 0; /* was inactive to begin with */
209   }
210
211   /* Inform ops and log it */
212   sendto_opmask_butone(0, SNO_NETWORK, "%s %s JUPE for %s, expiring at %Tu: "
213                        "%s",
214                        IsServer(sptr) ? cli_name(sptr) : cli_name(cli_user(sptr)->server),
215                        JupeIsLocal(jupe) ? "removing local" : "deactivating",
216                        jupe->ju_server, jupe->ju_expire + TSoffset,
217                        jupe->ju_reason);
218
219   log_write(LS_JUPE, L_INFO, LOG_NOSNOTICE,
220             "%#C %s JUPE for %s, expiring at %Tu: %s", sptr,
221             JupeIsLocal(jupe) ? "removing local" : "deactivating",
222             jupe->ju_server, jupe->ju_expire + TSoffset, jupe->ju_reason);
223
224   if (JupeIsLocal(jupe))
225     jupe_free(jupe);
226   else if (!(flags & JUPE_LOCAL)) /* don't propagate local changes */
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(&me, CMD_JUPE, cptr, "* %c%s %Tu %Tu :%s",
277                     JupeIsRemActive(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(&me, CMD_JUPE, cptr, "* %c%s %Tu %Tu :%s",
290                 JupeIsRemActive(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_reply(sptr, ERR_NOSUCHJUPE, server);
306
307     /* send jupe information along */
308     send_reply(sptr, RPL_JUPELIST, jupe->ju_server, jupe->ju_expire + TSoffset,
309                JupeIsLocal(jupe) ? cli_name(&me) : "*",
310                JupeIsActive(jupe) ? '+' : '-', jupe->ju_reason);
311   } else {
312     for (jupe = GlobalJupeList; jupe; jupe = sjupe) { /* go through jupes */
313       sjupe = jupe->ju_next;
314
315       if (jupe->ju_expire <= CurrentTime) /* expire any that need expiring */
316         jupe_free(jupe);
317       else /* send jupe information along */
318         send_reply(sptr, RPL_JUPELIST, jupe->ju_server,
319                    jupe->ju_expire + TSoffset,
320                    JupeIsLocal(jupe) ? cli_name(&me) : "*",
321                    JupeIsActive(jupe) ? '+' : '-', jupe->ju_reason);
322     }
323   }
324
325   /* end of jupe information */
326   return send_reply(sptr, RPL_ENDOFJUPELIST);
327 }