Author: Kev <klmitch@undernet.org>
[ircu2.10.12-pk.git] / ircd / m_jupe.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_jupe.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
6  *
7  * See file AUTHORS in IRC package for additional names of
8  * the programmers.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 1, or (at your option)
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  * $Id$
25  */
26
27 /*
28  * m_functions execute protocol messages on this server:
29  *
30  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
31  *            structure (with an open socket connected!). This
32  *            identifies the physical socket where the message
33  *            originated (or which caused the m_function to be
34  *            executed--some m_functions may call others...).
35  *
36  *    sptr    is the source of the message, defined by the
37  *            prefix part of the message if present. If not
38  *            or prefix not found, then sptr==cptr.
39  *
40  *            (!IsServer(cptr)) => (cptr == sptr), because
41  *            prefixes are taken *only* from servers...
42  *
43  *            (IsServer(cptr))
44  *                    (sptr == cptr) => the message didn't
45  *                    have the prefix.
46  *
47  *                    (sptr != cptr && IsServer(sptr) means
48  *                    the prefix specified servername. (?)
49  *
50  *                    (sptr != cptr && !IsServer(sptr) means
51  *                    that message originated from a remote
52  *                    user (not local).
53  *
54  *            combining
55  *
56  *            (!IsServer(sptr)) means that, sptr can safely
57  *            taken as defining the target structure of the
58  *            message in this server.
59  *
60  *    *Always* true (if 'parse' and others are working correct):
61  *
62  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
63  *
64  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
65  *            *cannot* be a local connection, unless it's
66  *            actually cptr!). [MyConnect(x) should probably
67  *            be defined as (x == x->from) --msa ]
68  *
69  *    parc    number of variable parameter strings (if zero,
70  *            parv is allowed to be NULL)
71  *
72  *    parv    a NULL terminated list of parameter pointers,
73  *
74  *                    parv[0], sender (prefix string), if not present
75  *                            this points to an empty string.
76  *                    parv[1]...parv[parc-1]
77  *                            pointers to additional parameters
78  *                    parv[parc] == NULL, *always*
79  *
80  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
81  *                    non-NULL pointers.
82  */
83 #if 0
84 /*
85  * No need to include handlers.h here the signatures must match
86  * and we don't need to force a rebuild of all the handlers everytime
87  * we add a new one to the list. --Bleep
88  */
89 #include "handlers.h"
90 #endif /* 0 */
91 #include "client.h"
92 #include "jupe.h"
93 #include "hash.h"
94 #include "ircd.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_jupe - server message handler
112  *
113  * parv[0] = Send prefix
114  *
115  * From server:
116  *
117  * parv[1] = Target: server numeric or *
118  * parv[2] = (+|-)<server name>
119  * parv[3] = Expiration offset
120  * parv[4] = Last modification time
121  * parv[5] = Comment
122  *
123  */
124 int ms_jupe(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
125 {
126   struct Client *acptr = 0;
127   struct Jupe *ajupe;
128   int local = 0, active = 1;
129   time_t expire_off, lastmod;
130   char *server = parv[2], *target = parv[1], *reason = parv[5];
131
132   if (parc < 6)
133     return need_more_params(sptr, "JUPE");
134
135   if (!(target[0] == '*' && target[1] == '\0')) {
136     if (!(acptr = FindNServer(target)))
137       return 0; /* no such server */
138
139     if (!IsMe(acptr)) { /* manually propagate, since we don't set it */
140       sendcmdto_one(acptr, CMD_JUPE, sptr, "%s %s %s %s :%s", target, server,
141                     parv[3], parv[4], reason);
142       return 0;
143     }
144
145     local = 1;
146   }
147
148   if (*server == '-') {
149     active = 0;
150     server++;
151   } else if (*server == '+') {
152     active = 1;
153     server++;
154   }
155
156   expire_off = atoi(parv[3]);
157   lastmod = atoi(parv[4]);
158
159   ajupe = jupe_find(server);
160
161   if (ajupe) {
162     if (JupeIsLocal(ajupe) && !local) /* global jupes override local ones */
163       jupe_free(ajupe);
164     else if (JupeLastMod(ajupe) < lastmod) { /* new modification */
165       if (active)
166         return jupe_activate(cptr, sptr, ajupe, lastmod);
167       else
168         return jupe_deactivate(cptr, sptr, ajupe, lastmod);
169     } else if (JupeLastMod(ajupe) == lastmod) /* no changes */
170       return 0;
171     else
172       return jupe_resend(cptr, ajupe); /* other server desynched WRT jupes */
173   }
174
175   return jupe_add(cptr, sptr, server, reason, expire_off, lastmod, local,
176                   active);
177 }
178
179 /*
180  * mo_jupe - oper message handler
181  *
182  * parv[0] = Send prefix
183  *
184  * From oper:
185  *
186  * parv[1] = [[+|-]<server name>]
187  * parv[2] = [target]
188  * parv[3] = [Expiration offset]
189  * parv[4] = [Comment]
190  *
191  */
192 #ifdef CONFIG_OPERCMDS
193 int mo_jupe(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
194 {
195   struct Client *acptr = 0;
196   struct Jupe *ajupe;
197   int local = 0, active = 1;
198   time_t expire_off;
199   char *server = parv[1], *target = parv[2], *reason = parv[4];
200
201   if (parc < 2)
202     return jupe_list(sptr, 0);
203
204   if (*server == '+') {
205     active = 1;
206     server++;
207   } else if (*server == '-') {
208     active = 0;
209     server++;
210   } else
211     return jupe_list(sptr, server);
212
213   if (parc < 5)
214     return need_more_params(sptr, "JUPE");
215
216   if (!(target[0] == '*' && target[1] == '\0')) {
217     if (!(acptr = find_match_server(target)))
218       return send_error_to_client(sptr, ERR_NOSUCHSERVER, target);
219
220     if (!IsMe(acptr)) { /* manually propagate, since we don't set it */
221       if (!IsOper(sptr))
222         return send_error_to_client(sptr, ERR_NOPRIVILEGES);
223
224       sendcmdto_one(acptr, CMD_JUPE, sptr, "%C %c%s %s %Tu :%s", acptr,
225                     active ? '+' : '-', server, parv[3], TStime(), reason);
226       return 0;
227     }
228
229     local = 1;
230   } else if (!IsOper(sptr))
231     return send_error_to_client(sptr, ERR_NOPRIVILEGES);
232
233   expire_off = atoi(parv[3]);
234
235   ajupe = jupe_find(server);
236
237   if (ajupe) {
238     if (JupeIsLocal(ajupe) && !local) /* global jupes override local ones */
239       jupe_free(ajupe);
240     else {
241       if (active)
242         return jupe_activate(cptr, sptr, ajupe, TStime());
243       else
244         return jupe_deactivate(cptr, sptr, ajupe, TStime());
245     }
246   }
247
248   return jupe_add(cptr, sptr, server, reason, expire_off, TStime(), local,
249                   active);
250 }
251 #endif /* CONFIG_OPERCMDS */
252
253 /*
254  * m_jupe - user message handler
255  *
256  * parv[0] = Send prefix
257  *
258  * From user:
259  *
260  * parv[1] = [<server name>]
261  *
262  */
263 int m_jupe(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
264 {
265   if (parc < 2)
266     return jupe_list(sptr, 0);
267
268   return jupe_list(sptr, parv[1]);
269 }