Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / m_gline.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_gline.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 1, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $Id$
24  */
25
26 /*
27  * m_functions execute protocol messages on this server:
28  *
29  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
30  *            structure (with an open socket connected!). This
31  *            identifies the physical socket where the message
32  *            originated (or which caused the m_function to be
33  *            executed--some m_functions may call others...).
34  *
35  *    sptr    is the source of the message, defined by the
36  *            prefix part of the message if present. If not
37  *            or prefix not found, then sptr==cptr.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
63  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64  *            *cannot* be a local connection, unless it's
65  *            actually cptr!). [MyConnect(x) should probably
66  *            be defined as (x == x->from) --msa ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
73  *                    parv[0], sender (prefix string), if not present
74  *                            this points to an empty string.
75  *                    parv[1]...parv[parc-1]
76  *                            pointers to additional parameters
77  *                    parv[parc] == NULL, *always*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #include "config.h"
83
84 #if 0
85 /*
86  * No need to include handlers.h here the signatures must match
87  * and we don't need to force a rebuild of all the handlers everytime
88  * we add a new one to the list. --Bleep
89  */
90 #include "handlers.h"
91 #endif /* 0 */
92 #include "client.h"
93 #include "gline.h"
94 #include "hash.h"
95 #include "ircd.h"
96 #include "ircd_features.h"
97 #include "ircd_reply.h"
98 #include "ircd_string.h"
99 #include "match.h"
100 #include "msg.h"
101 #include "numeric.h"
102 #include "numnicks.h"
103 #include "s_conf.h"
104 #include "s_misc.h"
105 #include "send.h"
106 #include "support.h"
107
108 #include <assert.h>
109 #include <stdlib.h>
110 #include <string.h>
111
112 /*
113  * ms_gline - server message handler
114  *
115  * parv[0] = Sender prefix
116  * parv[1] = Target: server numeric
117  * parv[2] = (+|-)<G-line mask>
118  * parv[3] = G-line lifetime
119  *
120  * From Uworld:
121  *
122  * parv[4] = Comment
123  *
124  * From somewhere else:
125  *
126  * parv[4] = Last modification time
127  * parv[5] = Comment
128  *
129  */
130 int
131 ms_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
132 {
133   struct Client *acptr = 0;
134   struct Gline *agline;
135   unsigned int flags = 0;
136   time_t expire_off, lastmod = 0;
137   char *mask = parv[2], *target = parv[1], *reason;
138
139   if (*mask == '!') {
140     mask++;
141
142     if (HasPriv(sptr, PRIV_WIDE_GLINE))
143       flags |= GLINE_OPERFORCE;
144   }
145
146   if ((parc == 3 && *mask == '-') || parc == 5) {
147     if (!find_conf_byhost(cli_confs(cptr), cli_name(sptr), CONF_UWORLD))
148       return need_more_params(sptr, "GLINE");
149
150     reason = parv[4];
151     flags |= GLINE_FORCE;
152   } else if (parc > 5) {
153     lastmod = atoi(parv[4]);
154     reason = parv[5];
155   } else
156     return need_more_params(sptr, "GLINE");
157
158   if (!(target[0] == '*' && target[1] == '\0')) {
159     if (!(acptr = FindNServer(target)))
160       return 0; /* no such server */
161
162     if (!IsMe(acptr)) { /* manually propagate */
163       if (!lastmod)
164         sendcmdto_one(sptr, CMD_GLINE, acptr,
165                       (parc == 3) ? "%C %s" : "%C %s %s :%s", acptr, mask,
166                       parv[3], reason);
167       else
168         sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %s%s %s %s :%s", acptr,
169                       flags & GLINE_OPERFORCE ? "!" : "", mask, parv[3],
170                       parv[4], reason);
171
172       return 0;
173     }
174
175     flags |= GLINE_LOCAL;
176   }
177
178   if (*mask == '-')
179     mask++;
180   else if (*mask == '+') {
181     flags |= GLINE_ACTIVE;
182     mask++;
183   } else
184     flags |= GLINE_ACTIVE;
185
186   expire_off = parc < 5 ? 0 : atoi(parv[3]);
187
188   agline = gline_find(mask, GLINE_ANY | GLINE_EXACT);
189
190   if (agline) {
191     if (GlineIsLocal(agline) && !(flags & GLINE_LOCAL)) /* global over local */
192       gline_free(agline);
193     else if (!lastmod && ((flags & GLINE_ACTIVE) == GlineIsRemActive(agline)))
194       return gline_propagate(cptr, sptr, agline);
195     else if (!lastmod || GlineLastMod(agline) < lastmod) { /* new mod */
196       if (flags & GLINE_ACTIVE)
197         return gline_activate(cptr, sptr, agline, lastmod, flags);
198       else
199         return gline_deactivate(cptr, sptr, agline, lastmod, flags);
200     } else if (GlineLastMod(agline) == lastmod || IsBurstOrBurstAck(cptr))
201       return 0;
202     else
203       return gline_resend(cptr, agline); /* other server desynched WRT gline */
204   } else if (parc == 3 && !(flags & GLINE_ACTIVE)) {
205     /* U-lined server removing a G-line we don't have; propagate the removal
206      * anyway.
207      */
208     if (!(flags & GLINE_LOCAL))
209       sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* -%s", mask);
210     return 0;
211   } else if (parc < 5)
212     return need_more_params(sptr, "GLINE");
213
214   return gline_add(cptr, sptr, mask, reason, expire_off, lastmod, flags);
215 }
216
217 /*
218  * mo_gline - oper message handler
219  *
220  * parv[0] = Sender prefix
221  * parv[1] = [[+|-]<G-line mask>]
222  *
223  * Local (to me) style:
224  *
225  * parv[2] = [Expiration offset]
226  * parv[3] = [Comment]
227  *
228  * Global (or remote local) style:
229  *
230  * parv[2] = [target]
231  * parv[3] = [Expiration offset]
232  * parv[4] = [Comment]
233  *
234  */
235 int
236 mo_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
237 {
238   struct Client *acptr = 0;
239   struct Gline *agline;
240   unsigned int flags = 0;
241   time_t expire_off;
242   char *mask = parv[1], *target = 0, *reason;
243
244   if (parc < 2)
245     return gline_list(sptr, 0);
246
247   if (*mask == '!') {
248     mask++;
249
250     if (HasPriv(sptr, PRIV_WIDE_GLINE))
251       flags |= GLINE_OPERFORCE;
252   }
253
254   if (*mask == '+') {
255     flags |= GLINE_ACTIVE;
256     mask++;
257
258   } else if (*mask == '-')
259     mask++;
260   else
261     return gline_list(sptr, mask);
262
263   if (parc == 4) {
264     expire_off = atoi(parv[2]);
265     reason = parv[3];
266     flags |= GLINE_LOCAL;
267   } else if (parc > 4) {
268     target = parv[2];
269     expire_off = atoi(parv[3]);
270     reason = parv[4];
271   } else
272     return need_more_params(sptr, "GLINE");
273
274   if (target) {
275     if (!(target[0] == '*' && target[1] == '\0')) {
276       if (!(acptr = find_match_server(target)))
277         return send_reply(sptr, ERR_NOSUCHSERVER, target);
278
279       if (!IsMe(acptr)) { /* manually propagate, since we don't set it */
280         if (!feature_bool(FEAT_CONFIG_OPERCMDS))
281           return send_reply(sptr, ERR_DISABLED, "GLINE");
282
283         if (!HasPriv(sptr, PRIV_GLINE))
284           return send_reply(sptr, ERR_NOPRIVILEGES);
285
286         sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %s%c%s %s %Tu :%s", acptr,
287                       flags & GLINE_OPERFORCE ? "!" : "",
288                       flags & GLINE_ACTIVE ? '+' : '-', mask, parv[3],
289                       TStime(), reason);
290         return 0;
291       } else if (!HasPriv(sptr, PRIV_LOCAL_GLINE))
292         return send_reply(sptr, ERR_NOPRIVILEGES);
293
294       flags |= GLINE_LOCAL;
295     } else if (!HasPriv(sptr, PRIV_GLINE))
296       return send_reply(sptr, ERR_NOPRIVILEGES);
297   }
298
299   if (!(flags & GLINE_LOCAL) && !feature_bool(FEAT_CONFIG_OPERCMDS))
300     return send_reply(sptr, ERR_DISABLED, "GLINE");
301
302   agline = gline_find(mask, GLINE_ANY | GLINE_EXACT);
303
304   if (agline) {
305     if (GlineIsLocal(agline) && !(flags & GLINE_LOCAL)) /* global over local */
306       gline_free(agline);
307     else {
308       if (!GlineLastMod(agline)) /* force mods to Uworld-set G-lines local */
309         flags |= GLINE_LOCAL;
310
311       if (flags & GLINE_ACTIVE)
312         return gline_activate(cptr, sptr, agline,
313                               GlineLastMod(agline) ? TStime() : 0, flags);
314       else
315         return gline_deactivate(cptr, sptr, agline,
316                                 GlineLastMod(agline) ? TStime() : 0, flags);
317     }
318   }
319
320   return gline_add(cptr, sptr, mask, reason, expire_off, TStime(), flags);
321 }
322
323 /*
324  * m_gline - user message handler
325  *
326  * parv[0] = Sender prefix
327  * parv[1] = [<server name>]
328  *
329  */
330 int
331 m_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
332 {
333   if (parc < 2)
334     return send_reply(sptr, ERR_NOSUCHGLINE, "");
335
336   return gline_list(sptr, parv[1]);
337 }