Move check_if_ipmask() from support.* to match.*.
[ircu2.10.12-pk.git] / ircd / m_jupe.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_jupe.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
6  *
7  * See file AUTHORS in IRC package for additional names of
8  * the programmers.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 1, or (at your option)
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  * $Id$
25  */
26
27 /*
28  * m_functions execute protocol messages on this server:
29  *
30  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
31  *            structure (with an open socket connected!). This
32  *            identifies the physical socket where the message
33  *            originated (or which caused the m_function to be
34  *            executed--some m_functions may call others...).
35  *
36  *    sptr    is the source of the message, defined by the
37  *            prefix part of the message if present. If not
38  *            or prefix not found, then sptr==cptr.
39  *
40  *            (!IsServer(cptr)) => (cptr == sptr), because
41  *            prefixes are taken *only* from servers...
42  *
43  *            (IsServer(cptr))
44  *                    (sptr == cptr) => the message didn't
45  *                    have the prefix.
46  *
47  *                    (sptr != cptr && IsServer(sptr) means
48  *                    the prefix specified servername. (?)
49  *
50  *                    (sptr != cptr && !IsServer(sptr) means
51  *                    that message originated from a remote
52  *                    user (not local).
53  *
54  *            combining
55  *
56  *            (!IsServer(sptr)) means that, sptr can safely
57  *            taken as defining the target structure of the
58  *            message in this server.
59  *
60  *    *Always* true (if 'parse' and others are working correct):
61  *
62  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
63  *
64  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
65  *            *cannot* be a local connection, unless it's
66  *            actually cptr!). [MyConnect(x) should probably
67  *            be defined as (x == x->from) --msa ]
68  *
69  *    parc    number of variable parameter strings (if zero,
70  *            parv is allowed to be NULL)
71  *
72  *    parv    a NULL terminated list of parameter pointers,
73  *
74  *                    parv[0], sender (prefix string), if not present
75  *                            this points to an empty string.
76  *                    parv[1]...parv[parc-1]
77  *                            pointers to additional parameters
78  *                    parv[parc] == NULL, *always*
79  *
80  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
81  *                    non-NULL pointers.
82  */
83 #include "config.h"
84
85 #include "client.h"
86 #include "jupe.h"
87 #include "hash.h"
88 #include "ircd.h"
89 #include "ircd_features.h"
90 #include "ircd_reply.h"
91 #include "ircd_string.h"
92 #include "match.h"
93 #include "msg.h"
94 #include "numeric.h"
95 #include "numnicks.h"
96 #include "s_conf.h"
97 #include "s_misc.h"
98 #include "send.h"
99
100 #include <assert.h>
101 #include <stdlib.h>
102 #include <string.h>
103
104 /*
105  * ms_jupe - server message handler
106  *
107  * parv[0] = Send prefix
108  *
109  * From server:
110  *
111  * parv[1] = Target: server numeric or *
112  * parv[2] = (+|-)<server name>
113  * parv[3] = Expiration offset
114  * parv[4] = Last modification time
115  * parv[5] = Comment
116  *
117  */
118 int ms_jupe(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
119 {
120   struct Client *acptr = 0;
121   struct Jupe *ajupe;
122   unsigned int flags = 0;
123   time_t expire_off, lastmod;
124   char *server = parv[2], *target = parv[1], *reason = parv[5];
125
126   if (parc < 6)
127     return need_more_params(sptr, "JUPE");
128
129   if (!(target[0] == '*' && target[1] == '\0')) {
130     if (!(acptr = FindNServer(target)))
131       return 0; /* no such server */
132
133     if (!IsMe(acptr)) { /* manually propagate, since we don't set it */
134       sendcmdto_one(sptr, CMD_JUPE, acptr, "%s %s %s %s :%s", target, server,
135                     parv[3], parv[4], reason);
136       return 0;
137     }
138
139     flags |= JUPE_LOCAL;
140   }
141
142   if (*server == '-')
143     server++;
144   else if (*server == '+') {
145     flags |= JUPE_ACTIVE;
146     server++;
147   }
148
149   expire_off = atoi(parv[3]);
150   lastmod = atoi(parv[4]);
151
152   ajupe = jupe_find(server);
153
154   if (ajupe) {
155     if (JupeIsLocal(ajupe) && !(flags & JUPE_LOCAL)) /* global over local */
156       jupe_free(ajupe);
157     else if (JupeLastMod(ajupe) < lastmod) { /* new modification */
158       if (flags & JUPE_ACTIVE)
159         return jupe_activate(cptr, sptr, ajupe, lastmod, flags);
160       else
161         return jupe_deactivate(cptr, sptr, ajupe, lastmod, flags);
162     } else if (JupeLastMod(ajupe) == lastmod || IsBurstOrBurstAck(cptr))
163       return 0;
164     else
165       return jupe_resend(cptr, ajupe); /* other server desynched WRT jupes */
166   }
167
168   return jupe_add(cptr, sptr, server, reason, expire_off, lastmod, flags);
169 }
170
171 /*
172  * mo_jupe - oper message handler
173  *
174  * parv[0] = Send prefix
175  * parv[1] = [[+|-]<server name>]
176  *
177  * Local (to me) style:
178  *
179  * parv[2] = [Expiration offset]
180  * parv[3] = [Comment]
181  *
182  * Global (or remote local) style:
183  *
184  * parv[2] = [target]
185  * parv[3] = [Expiration offset]
186  * parv[4] = [Comment]
187  *
188  */
189 int mo_jupe(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
190 {
191   struct Client *acptr = 0;
192   struct Jupe *ajupe;
193   unsigned int flags = 0;
194   time_t expire_off;
195   char *server = parv[1], *target = 0, *reason;
196
197   if (parc < 2)
198     return jupe_list(sptr, 0);
199
200   if (*server == '+') {
201     flags |= JUPE_ACTIVE;
202     server++;
203   } else if (*server == '-')
204     server++;
205   else
206     return jupe_list(sptr, server);
207
208   if (!feature_bool(FEAT_CONFIG_OPERCMDS))
209     return send_reply(sptr, ERR_DISABLED, "JUPE");
210
211   if (parc == 4) {
212     expire_off = atoi(parv[2]);
213     reason = parv[3];
214     flags |= JUPE_LOCAL;
215   } else if (parc > 4) {
216     target = parv[2];
217     expire_off = atoi(parv[3]);
218     reason = parv[4];
219   } else
220     return need_more_params(sptr, "JUPE");
221
222   if (target) {
223     if (!(target[0] == '*' && target[1] == '\0')) {
224       if (!(acptr = find_match_server(target)))
225         return send_reply(sptr, ERR_NOSUCHSERVER, target);
226
227       if (!IsMe(acptr)) { /* manually propagate, since we don't set it */
228         if (!HasPriv(sptr, PRIV_GLINE))
229           return send_reply(sptr, ERR_NOPRIVILEGES);
230
231         sendcmdto_one(sptr, CMD_JUPE, acptr, "%C %c%s %s %Tu :%s", acptr,
232                       flags & JUPE_ACTIVE ? '+' : '-', server, parv[3],
233                       TStime(), reason);
234         return 0;
235       } else if (!HasPriv(sptr, PRIV_LOCAL_GLINE))
236         return send_reply(sptr, ERR_NOPRIVILEGES);
237
238       flags |= JUPE_LOCAL;
239     } else if (!HasPriv(sptr, PRIV_GLINE))
240       return send_reply(sptr, ERR_NOPRIVILEGES);
241   }
242
243   ajupe = jupe_find(server);
244
245   if (ajupe) {
246     if (JupeIsLocal(ajupe) && !(flags & JUPE_LOCAL)) /* global over local */
247       jupe_free(ajupe);
248     else {
249       if (flags & JUPE_ACTIVE)
250         return jupe_activate(cptr, sptr, ajupe, TStime(), flags);
251       else
252         return jupe_deactivate(cptr, sptr, ajupe, TStime(), flags);
253     }
254   }
255
256   return jupe_add(cptr, sptr, server, reason, expire_off, TStime(), flags);
257 }
258
259 /*
260  * m_jupe - user message handler
261  *
262  * parv[0] = Send prefix
263  *
264  * From user:
265  *
266  * parv[1] = [<server name>]
267  *
268  */
269 int m_jupe(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
270 {
271   if (parc < 2)
272     return jupe_list(sptr, 0);
273
274   return jupe_list(sptr, parv[1]);
275 }