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