Author: Bleep <tomh@inxpress.net>
[ircu2.10.12-pk.git] / ircd / m_create.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_create.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 "channel.h"
91 #include "client.h"
92 #include "hash.h"
93 #include "ircd.h"
94 #include "ircd_reply.h"
95 #include "ircd_string.h"
96 #include "msg.h"
97 #include "numeric.h"
98 #include "numnicks.h"
99 #include "send.h"
100
101 #include <assert.h>
102 #include <stdlib.h>
103 #include <string.h>
104
105 /*
106  * m_create - generic message handler
107  */
108 int m_create(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
109 {
110   return 0;
111 }
112
113 /*
114  * ms_create - server message handler
115  */
116 int ms_create(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
117 {
118   char            cbuf[BUFSIZE];  /* Buffer for list with channels
119                                      that `sptr' really creates */
120   time_t          chanTS;         /* Creation time for all channels
121                                      in the comma seperated list */
122   char*           p;
123   char*           name;
124   struct Channel* chptr;
125   int             badop;
126
127   if (IsServer(sptr)) {
128     /* PROTOCOL WARNING */
129     /* bail, don't core */
130     return 0;
131   }
132   /* sanity checks: Only accept CREATE messages from servers */
133   if (!IsServer(cptr) || parc < 3 || *parv[2] == '\0')
134     return 0;
135
136   chanTS = atoi(parv[2]);
137
138   /* A create that didn't appear during a burst has that servers idea of
139    * the current time.  Use it for lag calculations.
140    */
141   if (!IsBurstOrBurstAck(sptr) && 0 != chanTS && MAGIC_REMOTE_JOIN_TS != chanTS)
142     sptr->user->server->serv->lag = TStime() - chanTS;
143
144   *cbuf = '\0';                 /* Start with empty buffer */
145
146   /* For each channel in the comma seperated list: */
147   for (name = ircd_strtok(&p, parv[1], ","); name; name = ircd_strtok(&p, 0, ","))
148   {
149     badop = 0;                  /* Default is to accept the op */
150     if ((chptr = FindChannel(name)))
151     {
152       name = chptr->chname;
153       if (TStime() - chanTS > TS_LAG_TIME)
154       {
155         /* A bounce would not be accepted anyway - if we get here something
156            is wrong with the TS clock syncing (or we have more then
157            TS_LAG_TIME lag, or an admin is hacking */
158         badop = 2;
159         /* This causes a HACK notice on all upstream servers: */
160         sendto_one(cptr, "%s " TOK_MODE " %s -o %s%s 0", NumServ(&me), name, NumNick(sptr));
161         /* This causes a WALLOPS on all downstream servers and a notice to our
162            own opers: */
163         parv[1] = name;         /* Corrupt parv[1], it is not used anymore anyway */
164         send_hack_notice(cptr, sptr, parc, parv, badop, 2);
165       }
166       else if (chptr->creationtime && chanTS > chptr->creationtime &&
167           chptr->creationtime != MAGIC_REMOTE_JOIN_TS)
168       {
169         /* We (try) to bounce the mode, because the CREATE is used on an older
170            channel, probably a net.ride */
171         badop = 1;
172         /* Send a deop upstream: */
173         sendto_one(cptr, "%s " TOK_MODE " %s -o %s%s " TIME_T_FMT, NumServ(&me),
174                    name, NumNick(sptr), chptr->creationtime);
175       }
176     }
177     else                        /* Channel doesn't exist: create it */
178       chptr = get_channel(sptr, name, CGT_CREATE);
179
180     /* Add and mark ops */
181     add_user_to_channel(chptr, sptr,
182         (badop || IsModelessChannel(name)) ? CHFL_DEOPPED : CHFL_CHANOP);
183
184     /* Send user join to the local clients (if any) */
185     sendto_channel_butserv(chptr, sptr, ":%s " MSG_JOIN " :%s", parv[0], name);
186
187     if (badop)                  /* handle badop: convert CREATE into JOIN */
188       sendto_serv_butone(cptr, "%s%s " TOK_JOIN " %s " TIME_T_FMT,
189           NumNick(sptr), name, chptr->creationtime);
190     else
191     {
192       /* Send the op to local clients:
193          (if any; extremely unlikely, but it CAN happen) */
194       if (!IsModelessChannel(name))
195         sendto_channel_butserv(chptr, sptr, ":%s MODE %s +o %s",
196             sptr->user->server->name, name, parv[0]);
197
198       /* Set/correct TS and add the channel to the
199          buffer for accepted channels: */
200       chptr->creationtime = chanTS;
201       if (*cbuf)
202         strcat(cbuf, ",");
203       strcat(cbuf, name);
204     }
205   }
206
207   if (*cbuf)                    /* Any channel accepted with ops ? */
208   {
209     sendto_serv_butone(cptr, "%s%s " TOK_CREATE " %s " TIME_T_FMT,
210         NumNick(sptr), cbuf, chanTS);
211   }
212
213   return 0;
214 }
215
216 #if 0
217 /*
218  * m_create
219  *
220  * parv[0] = sender prefix
221  * parv[1] = channel names
222  * parv[2] = channel time stamp
223  */
224 int m_create(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
225 {
226   char            cbuf[BUFSIZE];  /* Buffer for list with channels
227                                      that `sptr' really creates */
228   time_t          chanTS;         /* Creation time for all channels
229                                      in the comma seperated list */
230   char*           p;
231   char*           name;
232   struct Channel* chptr;
233   int             badop;
234
235   /* sanity checks: Only accept CREATE messages from servers */
236   if (!IsServer(cptr) || parc < 3 || *parv[2] == '\0')
237     return 0;
238
239   chanTS = atoi(parv[2]);
240
241   /* A create that didn't appear during a burst has that servers idea of
242    * the current time.  Use it for lag calculations.
243    */
244   if (!IsBurstOrBurstAck(sptr) && 0 != chanTS && MAGIC_REMOTE_JOIN_TS != chanTS)
245     sptr->user->server->serv->lag = TStime() - chanTS;
246
247   *cbuf = '\0';                 /* Start with empty buffer */
248
249   /* For each channel in the comma seperated list: */
250   for (name = ircd_strtok(&p, parv[1], ","); name; name = ircd_strtok(&p, 0, ","))
251   {
252     badop = 0;                  /* Default is to accept the op */
253     if ((chptr = FindChannel(name)))
254     {
255       name = chptr->chname;
256       if (TStime() - chanTS > TS_LAG_TIME)
257       {
258         /* A bounce would not be accepted anyway - if we get here something
259            is wrong with the TS clock syncing (or we have more then
260            TS_LAG_TIME lag, or an admin is hacking */
261         badop = 2;
262         /* This causes a HACK notice on all upstream servers: */
263         sendto_one(cptr, "%s " TOK_MODE " %s -o %s%s 0", NumServ(&me), name, NumNick(sptr));
264         /* This causes a WALLOPS on all downstream servers and a notice to our
265            own opers: */
266         parv[1] = name;         /* Corrupt parv[1], it is not used anymore anyway */
267         send_hack_notice(cptr, sptr, parc, parv, badop, 2);
268       }
269       else if (chptr->creationtime && chanTS > chptr->creationtime &&
270           chptr->creationtime != MAGIC_REMOTE_JOIN_TS)
271       {
272         /* We (try) to bounce the mode, because the CREATE is used on an older
273            channel, probably a net.ride */
274         badop = 1;
275         /* Send a deop upstream: */
276         sendto_one(cptr, "%s " TOK_MODE " %s -o %s%s " TIME_T_FMT, NumServ(&me),
277                    name, NumNick(sptr), chptr->creationtime);
278       }
279     }
280     else                        /* Channel doesn't exist: create it */
281       chptr = get_channel(sptr, name, CGT_CREATE);
282
283     /* Add and mark ops */
284     add_user_to_channel(chptr, sptr,
285         (badop || IsModelessChannel(name)) ? CHFL_DEOPPED : CHFL_CHANOP);
286
287     /* Send user join to the local clients (if any) */
288     sendto_channel_butserv(chptr, sptr, ":%s " MSG_JOIN " :%s", parv[0], name);
289
290     if (badop)                  /* handle badop: convert CREATE into JOIN */
291       sendto_serv_butone(cptr, "%s%s " TOK_JOIN " %s " TIME_T_FMT,
292           NumNick(sptr), name, chptr->creationtime);
293     else
294     {
295       /* Send the op to local clients:
296          (if any; extremely unlikely, but it CAN happen) */
297       if (!IsModelessChannel(name))
298         sendto_channel_butserv(chptr, sptr, ":%s MODE %s +o %s",
299             sptr->user->server->name, name, parv[0]);
300
301       /* Set/correct TS and add the channel to the
302          buffer for accepted channels: */
303       chptr->creationtime = chanTS;
304       if (*cbuf)
305         strcat(cbuf, ",");
306       strcat(cbuf, name);
307     }
308   }
309
310   if (*cbuf)                    /* Any channel accepted with ops ? */
311   {
312     sendto_serv_butone(cptr, "%s%s " TOK_CREATE " %s " TIME_T_FMT,
313         NumNick(sptr), cbuf, chanTS);
314   }
315
316   return 0;
317 }
318 #endif /* 0 */
319