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