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