f49cb61391ce21606b3f1ae3a32a3789590b28aa
[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 #if 0
86 /*
87  * No need to include handlers.h here the signatures must match
88  * and we don't need to force a rebuild of all the handlers everytime
89  * we add a new one to the list. --Bleep
90  */
91 #include "handlers.h"
92 #endif /* 0 */
93 #include "client.h"
94 #include "jupe.h"
95 #include "hash.h"
96 #include "ircd.h"
97 #include "ircd_features.h"
98 #include "ircd_reply.h"
99 #include "ircd_string.h"
100 #include "match.h"
101 #include "msg.h"
102 #include "numeric.h"
103 #include "numnicks.h"
104 #include "s_conf.h"
105 #include "s_misc.h"
106 #include "send.h"
107 #include "support.h"
108
109 #include <assert.h>
110 #include <stdlib.h>
111 #include <string.h>
112
113 /*
114  * ms_jupe - server message handler
115  *
116  * parv[0] = Send prefix
117  *
118  * From server:
119  *
120  * parv[1] = Target: server numeric or *
121  * parv[2] = (+|-)<server name>
122  * parv[3] = Expiration offset
123  * parv[4] = Last modification time
124  * parv[5] = Comment
125  *
126  */
127 int ms_jupe(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
128 {
129   struct Client *acptr = 0;
130   struct Jupe *ajupe;
131   unsigned int flags = 0;
132   time_t expire_off, lastmod;
133   char *server = parv[2], *target = parv[1], *reason = parv[5];
134
135   if (parc < 6)
136     return need_more_params(sptr, "JUPE");
137
138   if (!(target[0] == '*' && target[1] == '\0')) {
139     if (!(acptr = FindNServer(target)))
140       return 0; /* no such server */
141
142     if (!IsMe(acptr)) { /* manually propagate, since we don't set it */
143       sendcmdto_one(sptr, CMD_JUPE, acptr, "%s %s %s %s :%s", target, server,
144                     parv[3], parv[4], reason);
145       return 0;
146     }
147
148     flags |= JUPE_LOCAL;
149   }
150
151   if (*server == '-')
152     server++;
153   else if (*server == '+') {
154     flags |= JUPE_ACTIVE;
155     server++;
156   }
157
158   expire_off = atoi(parv[3]);
159   lastmod = atoi(parv[4]);
160
161   ajupe = jupe_find(server);
162
163   if (ajupe) {
164     if (JupeIsLocal(ajupe) && !(flags & JUPE_LOCAL)) /* global over local */
165       jupe_free(ajupe);
166     else if (JupeLastMod(ajupe) < lastmod) { /* new modification */
167       if (flags & JUPE_ACTIVE)
168         return jupe_activate(cptr, sptr, ajupe, lastmod, flags);
169       else
170         return jupe_deactivate(cptr, sptr, ajupe, lastmod, flags);
171     } else if (JupeLastMod(ajupe) == lastmod || IsBurstOrBurstAck(cptr))
172       return 0;
173     else
174       return jupe_resend(cptr, ajupe); /* other server desynched WRT jupes */
175   }
176
177   return jupe_add(cptr, sptr, server, reason, expire_off, lastmod, flags);
178 }
179
180 /*
181  * mo_jupe - oper message handler
182  *
183  * parv[0] = Send prefix
184  * parv[1] = [[+|-]<server name>]
185  *
186  * Local (to me) style:
187  *
188  * parv[2] = [Expiration offset]
189  * parv[3] = [Comment]
190  *
191  * Global (or remote local) style:
192  *
193  * parv[2] = [target]
194  * parv[3] = [Expiration offset]
195  * parv[4] = [Comment]
196  *
197  */
198 int mo_jupe(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
199 {
200   struct Client *acptr = 0;
201   struct Jupe *ajupe;
202   unsigned int flags = 0;
203   time_t expire_off;
204   char *server = parv[1], *target = 0, *reason;
205
206   if (parc < 2)
207     return jupe_list(sptr, 0);
208
209   if (*server == '+') {
210     flags |= JUPE_ACTIVE;
211     server++;
212   } else if (*server == '-')
213     server++;
214   else
215     return jupe_list(sptr, server);
216
217   if (!feature_bool(FEAT_CONFIG_OPERCMDS))
218     return send_reply(sptr, ERR_DISABLED, "JUPE");
219
220   if (parc == 4) {
221     expire_off = atoi(parv[2]);
222     reason = parv[3];
223     flags |= JUPE_LOCAL;
224   } else if (parc > 4) {
225     target = parv[2];
226     expire_off = atoi(parv[3]);
227     reason = parv[4];
228   } else
229     return need_more_params(sptr, "JUPE");
230
231   if (target) {
232     if (!(target[0] == '*' && target[1] == '\0')) {
233       if (!(acptr = find_match_server(target)))
234         return send_reply(sptr, ERR_NOSUCHSERVER, target);
235
236       if (!IsMe(acptr)) { /* manually propagate, since we don't set it */
237         if (!HasPriv(sptr, PRIV_GLINE))
238           return send_reply(sptr, ERR_NOPRIVILEGES);
239
240         sendcmdto_one(sptr, CMD_JUPE, acptr, "%C %c%s %s %Tu :%s", acptr,
241                       flags & JUPE_ACTIVE ? '+' : '-', server, parv[3],
242                       TStime(), reason);
243         return 0;
244       } else if (!HasPriv(sptr, PRIV_LOCAL_GLINE))
245         return send_reply(sptr, ERR_NOPRIVILEGES);
246
247       flags |= JUPE_LOCAL;
248     } else if (!HasPriv(sptr, PRIV_GLINE))
249       return send_reply(sptr, ERR_NOPRIVILEGES);
250   }
251
252   ajupe = jupe_find(server);
253
254   if (ajupe) {
255     if (JupeIsLocal(ajupe) && !(flags & JUPE_LOCAL)) /* global over local */
256       jupe_free(ajupe);
257     else {
258       if (flags & JUPE_ACTIVE)
259         return jupe_activate(cptr, sptr, ajupe, TStime(), flags);
260       else
261         return jupe_deactivate(cptr, sptr, ajupe, TStime(), flags);
262     }
263   }
264
265   return jupe_add(cptr, sptr, server, reason, expire_off, TStime(), flags);
266 }
267
268 /*
269  * m_jupe - user message handler
270  *
271  * parv[0] = Send prefix
272  *
273  * From user:
274  *
275  * parv[1] = [<server name>]
276  *
277  */
278 int m_jupe(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
279 {
280   if (parc < 2)
281     return jupe_list(sptr, 0);
282
283   return jupe_list(sptr, parv[1]);
284 }