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