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