Author: Kev <klmitch@mit.edu>
[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       if (IsServer(sptr))
141         sendto_one(acptr, "%s " TOK_JUPE " %s %s %s %s :%s", NumServ(sptr),
142                    target, server, parv[3], parv[4], reason);
143       else
144         sendto_one(acptr, "%s%s " TOK_JUPE " %s %s %s %s :%s", NumNick(sptr),
145                    target, server, parv[3], parv[4], reason);
146
147       return 0;
148     }
149
150     local = 1;
151   }
152
153   if (*server == '-') {
154     active = 0;
155     server++;
156   } else if (*server == '+') {
157     active = 1;
158     server++;
159   }
160
161   expire_off = atoi(parv[3]);
162   lastmod = atoi(parv[4]);
163
164   ajupe = jupe_find(server);
165
166   if (ajupe) {
167     if (JupeIsLocal(ajupe) && !local) /* global jupes override local ones */
168       jupe_free(ajupe);
169     else if (JupeLastMod(ajupe) < lastmod) { /* new modification */
170       if (active)
171         return jupe_activate(cptr, sptr, ajupe, lastmod);
172       else
173         return jupe_deactivate(cptr, sptr, ajupe, lastmod);
174     } else if (JupeLastMod(ajupe) == lastmod) /* no changes */
175       return 0;
176     else
177       return jupe_resend(cptr, ajupe); /* other server desynched WRT jupes */
178   }
179
180   return jupe_add(cptr, sptr, server, reason, expire_off, lastmod, local,
181                   active);
182 }
183
184 /*
185  * mo_jupe - oper message handler
186  *
187  * parv[0] = Send prefix
188  *
189  * From oper:
190  *
191  * parv[1] = [[+|-]<server name>]
192  * parv[2] = [target]
193  * parv[3] = [Expiration offset]
194  * parv[4] = [Comment]
195  *
196  */
197 int mo_jupe(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
198 {
199   struct Client *acptr = 0;
200   struct Jupe *ajupe;
201   int local = 0, active = 1;
202   time_t expire_off;
203   char *server = parv[1], *target = parv[2], *reason = parv[4];
204
205   if (parc < 2)
206     return jupe_list(sptr, 0);
207
208   if (*server == '+') {
209     active = 1;
210     server++;
211   } else if (*server == '-') {
212     active = 0;
213     server++;
214   } else
215     return jupe_list(sptr, server);
216
217   if (parc < 5)
218     return need_more_params(sptr, "JUPE");
219
220   if (!(target[0] == '*' && target[1] == '\0')) {
221     if (!(acptr = find_match_server(target))) {
222       sendto_one(sptr, err_str(ERR_NOSUCHSERVER), me.name, parv[0], target);
223       return 0;
224     }
225
226     if (!IsMe(acptr)) { /* manually propagate, since we don't set it */
227       if (!IsOper(sptr)) {
228         sendto_one(sptr, err_str(ERR_NOPRIVILEGES), me.name, parv[0]);
229         return 0;
230       }
231
232       sendto_one(acptr, "%s%s " TOK_JUPE " %s %c%s %s " TIME_T_FMT " :%s",
233                  NumNick(sptr), NumServ(acptr), active ? '+' : '-', server,
234                  parv[3], TStime(), reason);
235       return 0;
236     }
237
238     local = 1;
239   } else if (!IsOper(sptr)) {
240     sendto_one(sptr, err_str(ERR_NOPRIVILEGES), me.name, parv[0]);
241     return 0;
242   }
243
244   expire_off = atoi(parv[3]);
245
246   ajupe = jupe_find(server);
247
248   if (ajupe) {
249     if (JupeIsLocal(ajupe) && !local) /* global jupes override local ones */
250       jupe_free(ajupe);
251     else {
252       if (active)
253         return jupe_activate(cptr, sptr, ajupe, TStime());
254       else
255         return jupe_deactivate(cptr, sptr, ajupe, TStime());
256     }
257   }
258
259   return jupe_add(cptr, sptr, server, reason, expire_off, TStime(), local,
260                   active);
261 }
262
263 /*
264  * m_jupe - user message handler
265  *
266  * parv[0] = Send prefix
267  *
268  * From user:
269  *
270  * parv[1] = [<server name>]
271  *
272  */
273 int m_jupe(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
274 {
275   if (parc < 2)
276     return jupe_list(sptr, 0);
277
278   return jupe_list(sptr, parv[1]);
279 }