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(cli_confs(cptr), cli_name(sptr), 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 || IsBurstOrBurstAck(cptr))
188       return 0;
189     else
190       return gline_resend(cptr, agline); /* other server desynched WRT gline */
191   } else if (parc < 5)
192     return need_more_params(sptr, "GLINE");
193
194   return gline_add(cptr, sptr, mask, reason, expire_off, lastmod, flags);
195 }
196
197 /*
198  * mo_gline - oper message handler
199  *
200  * parv[0] = Sender prefix
201  * parv[1] = [[+|-]<G-line mask>]
202  *
203  * Local (to me) style:
204  *
205  * parv[2] = [Expiration offset]
206  * parv[3] = [Comment]
207  *
208  * Global (or remote local) style:
209  *
210  * parv[2] = [target]
211  * parv[3] = [Expiration offset]
212  * parv[4] = [Comment]
213  *
214  */
215 int
216 mo_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
217 {
218   struct Client *acptr = 0;
219   struct Gline *agline;
220   unsigned int flags = 0;
221   time_t expire_off;
222   char *mask = parv[1], *target = 0, *reason;
223
224   if (parc < 2)
225     return gline_list(sptr, 0);
226
227   if (*mask == '+') {
228     flags |= GLINE_ACTIVE;
229     mask++;
230   } else if (*mask == '-')
231     mask++;
232   else
233     return gline_list(sptr, mask);
234
235 #ifndef LOCOP_LGLINE
236   if (!IsOper(sptr))
237     return send_reply(sptr, ERR_NOPRIVILEGES);
238 #endif
239
240   if (parc == 4) {
241     expire_off = atoi(parv[2]);
242     reason = parv[3];
243     flags |= GLINE_LOCAL;
244   } else if (parc > 4) {
245     target = parv[2];
246     expire_off = atoi(parv[3]);
247     reason = parv[4];
248   } else
249     return need_more_params(sptr, "GLINE");
250
251   if (target) {
252     if (!(target[0] == '*' && target[1] == '\0')) {
253       if (!(acptr = find_match_server(target)))
254         return send_reply(sptr, ERR_NOSUCHSERVER, target);
255
256       if (!IsMe(acptr)) { /* manually propagate, since we don't set it */
257 #ifndef CONFIG_OPERCMDS
258         return send_reply(sptr, ERR_DISABLED, "GLINE");
259 #else
260         if (!IsOper(sptr))
261           return send_reply(sptr, ERR_NOPRIVILEGES);
262
263         sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %c%s %s %Tu :%s", acptr,
264                       flags & GLINE_ACTIVE ? '?' : '-', mask, parv[3],
265                       TStime(), reason);
266         return 0;
267 #endif
268       }
269
270       flags |= GLINE_LOCAL;
271     } else if (!IsOper(sptr))
272       return send_reply(sptr, ERR_NOPRIVILEGES);
273   }
274
275 #ifndef CONFIG_OPERCMDS
276   if (!(flags & GLINE_LOCAL))
277     return send_reply(sptr, ERR_DISABLED, "GLINE");
278 #endif /* CONFIG_OPERCMDS */
279
280   agline = gline_find(mask, GLINE_ANY | GLINE_EXACT);
281
282   if (agline) {
283     if (GlineIsLocal(agline) && !(flags & GLINE_LOCAL)) /* global over local */
284       gline_free(agline);
285     else {
286       if (!GlineLastMod(agline)) /* force mods to Uworld-set G-lines local */
287         flags |= GLINE_LOCAL;
288
289       if (flags & GLINE_ACTIVE)
290         return gline_activate(cptr, sptr, agline,
291                               GlineLastMod(agline) ? TStime() : 0, flags);
292       else
293         return gline_deactivate(cptr, sptr, agline,
294                                 GlineLastMod(agline) ? TStime() : 0, flags);
295     }
296   }
297
298   return gline_add(cptr, sptr, mask, reason, expire_off, TStime(), flags);
299 }
300
301 /*
302  * m_gline - user message handler
303  *
304  * parv[0] = Sender prefix
305  * parv[1] = [<server name>]
306  *
307  */
308 int
309 m_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
310 {
311   if (parc < 2)
312     return send_reply(sptr, ERR_NOSUCHGLINE, "");
313
314   return gline_list(sptr, parv[1]);
315 }