Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / m_settime.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_settime.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 "hash.h"
92 #include "ircd.h"
93 #include "ircd_reply.h"
94 #include "ircd_string.h"
95 #include "list.h"
96 #include "msg.h"
97 #include "numeric.h"
98 #include "numnicks.h"
99 #include "s_user.h"
100 #include "send.h"
101 #include "struct.h"
102
103 #include <assert.h>
104 #include <stdlib.h>
105
106 /*
107  * ms_settime - server message handler
108  *
109  * parv[0] = sender prefix
110  * parv[1] = new time
111  * parv[2] = servername (Only used when sptr is an Oper).
112  */
113 int ms_settime(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
114 {
115   time_t t;
116   long int dt;
117   static char tbuf[11];
118   struct DLink *lp;
119
120   if (!IsPrivileged(sptr))
121     return 0;
122
123   if (parc < 2)
124     return need_more_params(sptr, "SETTIME");
125
126   if (parc == 2 && MyUser(sptr))
127     parv[parc++] = me.name;
128
129   t = atoi(parv[1]);
130   dt = TStime() - t;
131
132   if (t < OLDEST_TS || dt < -9000000)
133   {
134     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :SETTIME: Bad value", sptr);
135     return 0;
136   }
137
138   if (IsServer(sptr))           /* send to unlagged servers */
139   {
140 #ifdef RELIABLE_CLOCK
141     sprintf_irc(tbuf, TIME_T_FMT, TStime());
142     parv[1] = tbuf;
143 #endif
144     for (lp = me.serv->down; lp; lp = lp->next)
145       if (cptr != lp->value.cptr && MsgQLength(&lp->value.cptr->sendQ) < 8000)
146         sendcmdto_one(sptr, CMD_NOTICE, lp->value.cptr, "%s", parv[1]);
147   }
148   else
149   {
150     sprintf_irc(tbuf, TIME_T_FMT, TStime());
151     parv[1] = tbuf;
152     if (hunt_server_cmd(sptr, CMD_SETTIME, cptr, 1, "%s %C", 2, parc, parv) !=
153         HUNTED_ISME)
154       return 0;
155   }
156
157 #ifdef RELIABLE_CLOCK
158   if ((dt > 600) || (dt < -600))
159     sendcmdto_serv_butone(&me, CMD_WALLOPS, 0, ":Bad SETTIME from %s: %Tu",
160                           sptr->name, t);
161   if (IsUser(sptr))
162   {
163     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :clock is not set %ld seconds %s "
164                   ": RELIABLE_CLOCK is defined", sptr, (dt < 0) ? -dt : dt,
165                   (dt < 0) ? "forwards" : "backwards");
166   }
167 #else
168   sendto_opmask_butone(0, SNO_OLDSNO, "SETTIME from %s, clock is set %ld "
169                        "seconds %s", sptr->name, (dt < 0) ? -dt : dt,
170                        (dt < 0) ? "forwards" : "backwards");
171   TSoffset -= dt;
172   if (IsUser(sptr))
173   {
174     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :clock is set %ld seconds %s",
175                   sptr, (dt < 0) ? -dt : dt,
176                   (dt < 0) ? "forwards" : "backwards");
177   }
178 #endif
179   return 0;
180 }
181
182 /*
183  * mo_settime - oper message handler
184  *
185  * parv[0] = sender prefix
186  * parv[1] = new time
187  * parv[2] = servername (Only used when sptr is an Oper).
188  */
189 int mo_settime(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
190 {
191   time_t t;
192   long int dt;
193   static char tbuf[11];
194   struct DLink *lp;
195
196   if (!IsPrivileged(sptr))
197     return 0;
198
199   if (parc < 2)
200     return need_more_params(sptr, "SETTIME");
201
202   if (parc == 2 && MyUser(sptr))
203     parv[parc++] = me.name;
204
205   t = atoi(parv[1]);
206   dt = TStime() - t;
207
208   if (t < OLDEST_TS || dt < -9000000)
209   {
210     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :SETTIME: Bad value", sptr);
211     return 0;
212   }
213
214   if (IsServer(sptr))           /* send to unlagged servers */
215   {
216 #ifdef RELIABLE_CLOCK
217     sprintf_irc(tbuf, TIME_T_FMT, TStime());
218     parv[1] = tbuf;
219 #endif
220     for (lp = me.serv->down; lp; lp = lp->next)
221       if (cptr != lp->value.cptr && MsgQLength(&lp->value.cptr->sendQ) < 8000)
222         sendcmdto_one(sptr, CMD_SETTIME, lp->value.cptr, "%s", parv[1]);
223   }
224   else
225   {
226     sprintf_irc(tbuf, TIME_T_FMT, TStime());
227     parv[1] = tbuf;
228     if (hunt_server_cmd(sptr, CMD_SETTIME, cptr, 1, "%s %C", 2, parc, parv) !=
229         HUNTED_ISME)
230       return 0;
231   }
232
233 #ifdef RELIABLE_CLOCK
234   if ((dt > 600) || (dt < -600))
235     sendcmdto_serv_butone(&me, CMD_WALLOPS, 0, ":Bad SETTIME from %s: %Tu",
236                           sptr->name, t);
237   if (IsUser(sptr))
238   {
239     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :clock is not set %ld seconds %s "
240                   ": RELIABLE_CLOCK is defined", sptr, (dt < 0) ? -dt : dt,
241                   (dt < 0) ? "forwards" : "backwards");
242   }
243 #else
244   sendto_opmask_butone(0, SNO_OLDSNO, "SETTIME from %s, clock is set %ld "
245                        "seconds %s", sptr->name, (dt < 0) ? -dt : dt,
246                        (dt < 0) ? "forwards" : "backwards");
247   TSoffset -= dt;
248   if (IsUser(sptr))
249   {
250     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :clock is set %ld seconds %s",
251                   sptr, (dt < 0) ? -dt : dt,
252                   (dt < 0) ? "forwards" : "backwards");
253   }
254 #endif
255   return 0;
256 }
257
258   
259 #if 0
260 /*
261  * m_settime
262  *
263  * parv[0] = sender prefix
264  * parv[1] = new time
265  * parv[2] = servername (Only used when sptr is an Oper).
266  */
267 int m_settime(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
268 {
269   time_t t;
270   long int dt;
271   static char tbuf[11];
272   struct DLink *lp;
273
274   if (!IsPrivileged(sptr))
275     return 0;
276
277   if (parc < 2)
278     return need_more_params(sptr, "SETTIME");
279
280   if (parc == 2 && MyUser(sptr))
281     parv[parc++] = me.name;
282
283   t = atoi(parv[1]);
284   dt = TStime() - t;
285
286   if (t < OLDEST_TS || dt < -9000000)
287   {
288     sendto_one(sptr, ":%s NOTICE %s :SETTIME: Bad value", me.name, parv[0]); /* XXX DEAD */
289     return 0;
290   }
291
292   if (IsServer(sptr))           /* send to unlagged servers */
293   {
294 #ifdef RELIABLE_CLOCK
295     sprintf_irc(tbuf, TIME_T_FMT, TStime());
296     parv[1] = tbuf;
297 #endif
298     for (lp = me.serv->down; lp; lp = lp->next)
299       if (cptr != lp->value.cptr && DBufLength(&lp->value.cptr->sendQ) < 8000) /* XXX DEAD */
300         sendto_one(lp->value.cptr, ":%s SETTIME %s", parv[0], parv[1]); /* XXX DEAD */
301   }
302   else
303   {
304     sprintf_irc(tbuf, TIME_T_FMT, TStime());
305     parv[1] = tbuf;
306     if (hunt_server(1, cptr, sptr, "%s%s " TOK_SETTIME " %s %s", 2, parc, parv) != /* XXX DEAD */
307         HUNTED_ISME)
308       return 0;
309   }
310
311 #ifdef RELIABLE_CLOCK
312   if ((dt > 600) || (dt < -600))
313     sendto_serv_butone(0, ":%s " TOK_WALLOPS " :Bad SETTIME from %s: " TIME_T_FMT, /* XXX DEAD */
314                        me.name, sptr->name, t);
315   if (IsUser(sptr))
316   {
317     if (MyUser(sptr) || Protocol(cptr) < 10)
318       sendto_one(sptr, ":%s NOTICE %s :clock is not set %ld seconds %s : " /* XXX DEAD */
319                  "RELIABLE_CLOCK is defined", me.name, parv[0],
320                  (dt < 0) ? -dt : dt, (dt < 0) ? "forwards" : "backwards");
321     else
322       sendto_one(sptr, "%s NOTICE %s%s :clock is not set %ld seconds %s : " /* XXX DEAD */
323                  "RELIABLE_CLOCK is defined", NumServ(&me), NumNick(sptr),
324                  (dt < 0) ? -dt : dt, (dt < 0) ? "forwards" : "backwards");
325   }
326 #else
327   sendto_ops("SETTIME from %s, clock is set %ld seconds %s", /* XXX DEAD */
328              sptr->name, (dt < 0) ? -dt : dt,
329              (dt < 0) ? "forwards" : "backwards");
330   TSoffset -= dt;
331   if (IsUser(sptr))
332   {
333     if (MyUser(sptr) || Protocol(cptr) < 10)
334       sendto_one(sptr, ":%s NOTICE %s :clock is set %ld seconds %s", me.name, /* XXX DEAD */
335                  parv[0], (dt < 0) ? -dt : dt, (dt < 0) ? "forwards" : "backwards");
336     else
337       sendto_one(sptr, "%s NOTICE %s%s :clock is set %ld seconds %s", /* XXX DEAD */
338                  NumServ(&me), NumNick(sptr),
339                  (dt < 0) ? -dt : dt, (dt < 0) ? "forwards" : "backwards");
340   }
341 #endif
342   return 0;
343 }
344 #endif /* 0 */