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 #if 0
83 /*
84  * No need to include handlers.h here the signatures must match
85  * and we don't need to force a rebuild of all the handlers everytime
86  * we add a new one to the list. --Bleep
87  */
88 #include "handlers.h"
89 #endif /* 0 */
90 #include "client.h"
91 #include "gline.h"
92 #include "hash.h"
93 #include "ircd.h"
94 #include "ircd_features.h"
95 #include "ircd_reply.h"
96 #include "ircd_string.h"
97 #include "match.h"
98 #include "msg.h"
99 #include "numeric.h"
100 #include "numnicks.h"
101 #include "s_conf.h"
102 #include "s_misc.h"
103 #include "send.h"
104 #include "support.h"
105
106 #include <assert.h>
107 #include <stdlib.h>
108 #include <string.h>
109
110 /*
111  * ms_gline - server message handler
112  *
113  * parv[0] = Sender prefix
114  * parv[1] = Target: server numeric
115  * parv[2] = (+|-)<G-line mask>
116  * parv[3] = G-line lifetime
117  *
118  * From Uworld:
119  *
120  * parv[4] = Comment
121  *
122  * From somewhere else:
123  *
124  * parv[4] = Last modification time
125  * parv[5] = Comment
126  *
127  */
128 int
129 ms_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
130 {
131   struct Client *acptr = 0;
132   struct Gline *agline;
133   unsigned int flags = 0;
134   time_t expire_off, lastmod = 0;
135   char *mask = parv[2], *target = parv[1], *reason;
136
137   if ((parc == 3 && *mask == '-') || parc == 5) {
138     if (!find_conf_byhost(cli_confs(cptr), cli_name(sptr), CONF_UWORLD))
139       return need_more_params(sptr, "GLINE");
140
141     reason = parv[4];
142     flags |= GLINE_FORCE;
143   } else if (parc > 5) {
144     lastmod = atoi(parv[4]);
145     reason = parv[5];
146   } else
147     return need_more_params(sptr, "GLINE");
148
149   if (!(target[0] == '*' && target[1] == '\0')) {
150     if (!(acptr = FindNServer(target)))
151       return 0; /* no such server */
152
153     if (!IsMe(acptr)) { /* manually propagate */
154       if (!lastmod)
155         sendcmdto_one(sptr, CMD_GLINE, acptr,
156                       (parc == 3) ? "%C %s" : "%C %s %s :%s", acptr, mask,
157                       parv[3], reason);
158       else
159         sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %s %s %s :%s", acptr, mask,
160                       parv[3], parv[4], reason);
161
162       return 0;
163     }
164
165     flags |= GLINE_LOCAL;
166   }
167
168   if (*mask == '-')
169     mask++;
170   else if (*mask == '+') {
171     flags |= GLINE_ACTIVE;
172     mask++;
173   } else
174     flags |= GLINE_ACTIVE;
175
176   expire_off = parc < 5 ? 0 : atoi(parv[3]);
177
178   agline = gline_find(mask, GLINE_ANY | GLINE_EXACT);
179
180   if (agline) {
181     if (GlineIsLocal(agline) && !(flags & GLINE_LOCAL)) /* global over local */
182       gline_free(agline);
183     else if (!lastmod && ((flags & GLINE_ACTIVE) == GlineIsRemActive(agline)))
184       return gline_propagate(cptr, sptr, agline);
185     else if (!lastmod || GlineLastMod(agline) < lastmod) { /* new mod */
186       if (flags & GLINE_ACTIVE)
187         return gline_activate(cptr, sptr, agline, lastmod, flags);
188       else
189         return gline_deactivate(cptr, sptr, agline, lastmod, flags);
190     } else if (GlineLastMod(agline) == lastmod || IsBurstOrBurstAck(cptr))
191       return 0;
192     else
193       return gline_resend(cptr, agline); /* other server desynched WRT gline */
194   } else if (parc == 3 && !(flags & GLINE_ACTIVE)) {
195     /* U-lined server removing a G-line we don't have; propagate the removal
196      * anyway.
197      */
198     if (!(flags & GLINE_LOCAL))
199       sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* -%s", mask);
200     return 0;
201   } else if (parc < 5)
202     return need_more_params(sptr, "GLINE");
203
204   return gline_add(cptr, sptr, mask, reason, expire_off, lastmod, flags);
205 }
206
207 /*
208  * mo_gline - oper message handler
209  *
210  * parv[0] = Sender prefix
211  * parv[1] = [[+|-]<G-line mask>]
212  *
213  * Local (to me) style:
214  *
215  * parv[2] = [Expiration offset]
216  * parv[3] = [Comment]
217  *
218  * Global (or remote local) style:
219  *
220  * parv[2] = [target]
221  * parv[3] = [Expiration offset]
222  * parv[4] = [Comment]
223  *
224  */
225 int
226 mo_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
227 {
228   struct Client *acptr = 0;
229   struct Gline *agline;
230   unsigned int flags = 0;
231   time_t expire_off;
232   char *mask = parv[1], *target = 0, *reason;
233
234   if (parc < 2)
235     return gline_list(sptr, 0);
236
237   if (*mask == '+') {
238     flags |= GLINE_ACTIVE;
239     mask++;
240
241   } else if (*mask == '-')
242     mask++;
243   else
244     return gline_list(sptr, mask);
245
246   if (parc == 4) {
247     expire_off = atoi(parv[2]);
248     reason = parv[3];
249     flags |= GLINE_LOCAL;
250   } else if (parc > 4) {
251     target = parv[2];
252     expire_off = atoi(parv[3]);
253     reason = parv[4];
254   } else
255     return need_more_params(sptr, "GLINE");
256
257   if (target) {
258     if (!(target[0] == '*' && target[1] == '\0')) {
259       if (!(acptr = find_match_server(target)))
260         return send_reply(sptr, ERR_NOSUCHSERVER, target);
261
262       if (!IsMe(acptr)) { /* manually propagate, since we don't set it */
263         if (!feature_bool(FEAT_CONFIG_OPERCMDS))
264           return send_reply(sptr, ERR_DISABLED, "GLINE");
265
266         if (!HasPriv(sptr, PRIV_GLINE))
267           return send_reply(sptr, ERR_NOPRIVILEGES);
268
269         sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %c%s %s %Tu :%s", acptr,
270                       flags & GLINE_ACTIVE ? '+' : '-', mask, parv[3],
271                       TStime(), reason);
272         return 0;
273       } else if (!HasPriv(sptr, PRIV_LOCAL_GLINE))
274         return send_reply(sptr, ERR_NOPRIVILEGES);
275
276       flags |= GLINE_LOCAL;
277     } else if (!HasPriv(sptr, PRIV_GLINE))
278       return send_reply(sptr, ERR_NOPRIVILEGES);
279   }
280
281   if (!(flags & GLINE_LOCAL) && !feature_bool(FEAT_CONFIG_OPERCMDS))
282     return send_reply(sptr, ERR_DISABLED, "GLINE");
283
284   agline = gline_find(mask, GLINE_ANY | GLINE_EXACT);
285
286   if (agline) {
287     if (GlineIsLocal(agline) && !(flags & GLINE_LOCAL)) /* global over local */
288       gline_free(agline);
289     else {
290       if (!GlineLastMod(agline)) /* force mods to Uworld-set G-lines local */
291         flags |= GLINE_LOCAL;
292
293       if (flags & GLINE_ACTIVE)
294         return gline_activate(cptr, sptr, agline,
295                               GlineLastMod(agline) ? TStime() : 0, flags);
296       else
297         return gline_deactivate(cptr, sptr, agline,
298                                 GlineLastMod(agline) ? TStime() : 0, flags);
299     }
300   }
301
302   return gline_add(cptr, sptr, mask, reason, expire_off, TStime(), flags);
303 }
304
305 /*
306  * m_gline - user message handler
307  *
308  * parv[0] = Sender prefix
309  * parv[1] = [<server name>]
310  *
311  */
312 int
313 m_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
314 {
315   if (parc < 2)
316     return send_reply(sptr, ERR_NOSUCHGLINE, "");
317
318   return gline_list(sptr, parv[1]);
319 }