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 #include "client.h"
85 #include "hash.h"
86 #include "ircd.h"
87 #include "ircd_features.h"
88 #include "ircd_reply.h"
89 #include "ircd_snprintf.h"
90 #include "ircd_string.h"
91 #include "list.h"
92 #include "msg.h"
93 #include "numeric.h"
94 #include "numnicks.h"
95 #include "s_user.h"
96 #include "send.h"
97 #include "struct.h"
98
99 #include <assert.h>
100 #include <stdlib.h>
101
102 /*
103  * ms_settime - server message handler
104  *
105  * parv[0] = sender prefix
106  * parv[1] = new time
107  * parv[2] = servername (Only used when sptr is an Oper).
108  */
109 int ms_settime(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
110 {
111   time_t t;
112   long int dt;
113   static char tbuf[11];
114   struct DLink *lp;
115
116   if (!IsPrivileged(sptr))
117     return 0;
118
119   if (parc < 2)
120     return need_more_params(sptr, "SETTIME");
121
122   if (parc == 2 && MyUser(sptr))
123     parv[parc++] = cli_name(&me);
124
125   t = atoi(parv[1]);
126   dt = TStime() - t;
127
128   if (t < OLDEST_TS || dt < -9000000)
129   {
130     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :SETTIME: Bad value", sptr);
131     return 0;
132   }
133
134   if (IsServer(sptr))           /* send to unlagged servers */
135   {
136     if (feature_bool(FEAT_RELIABLE_CLOCK)) {
137       ircd_snprintf(0, tbuf, sizeof(tbuf), "%Tu", TStime());
138       parv[1] = tbuf;
139     }
140
141     for (lp = cli_serv(&me)->down; lp; lp = lp->next)
142       if (cptr != lp->value.cptr && MsgQLength(&(cli_sendQ(lp->value.cptr))) < 8000)
143         sendcmdto_one(sptr, CMD_NOTICE, lp->value.cptr, "%s", parv[1]);
144   }
145   else
146   {
147     ircd_snprintf(0, tbuf, sizeof(tbuf), "%Tu", TStime());
148     parv[1] = tbuf;
149     if (hunt_server_prio_cmd(sptr, CMD_SETTIME, cptr, 1, "%s %C", 2, parc,
150                              parv) != HUNTED_ISME)
151       return 0;
152   }
153
154   if (feature_bool(FEAT_RELIABLE_CLOCK)) {
155     if ((dt > 600) || (dt < -600))
156       sendcmdto_serv_butone(&me, CMD_WALLOPS, 0, ":Bad SETTIME from %s: %Tu",
157                             cli_name(sptr), t);
158     if (IsUser(sptr)) {
159       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :clock is not set %ld "
160                     "seconds %s : RELIABLE_CLOCK is defined", sptr,
161                     (dt < 0) ? -dt : dt, (dt < 0) ? "forwards" : "backwards");
162     }
163   } else {
164     sendto_opmask_butone(0, SNO_OLDSNO, "SETTIME from %s, clock is set %ld "
165                          "seconds %s", cli_name(sptr), (dt < 0) ? -dt : dt,
166                          (dt < 0) ? "forwards" : "backwards");
167     TSoffset -= dt;
168     if (IsUser(sptr)) {
169       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :clock is set %ld seconds %s",
170                     sptr, (dt < 0) ? -dt : dt,
171                     (dt < 0) ? "forwards" : "backwards");
172     }
173   }
174
175   return 0;
176 }
177
178 /*
179  * mo_settime - oper message handler
180  *
181  * parv[0] = sender prefix
182  * parv[1] = new time
183  * parv[2] = servername (Only used when sptr is an Oper).
184  */
185 int mo_settime(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
186 {
187   time_t t;
188   long int dt;
189   static char tbuf[11];
190   struct DLink *lp;
191
192   if (!IsPrivileged(sptr))
193     return 0;
194
195   if (parc < 2)
196     return need_more_params(sptr, "SETTIME");
197
198   if (parc == 2 && MyUser(sptr))
199     parv[parc++] = cli_name(&me);
200
201   t = atoi(parv[1]);
202   dt = TStime() - t;
203
204   if (t < OLDEST_TS || dt < -9000000)
205   {
206     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :SETTIME: Bad value", sptr);
207     return 0;
208   }
209
210   if (IsServer(sptr))           /* send to unlagged servers */
211   {
212     if (feature_bool(FEAT_RELIABLE_CLOCK)) {
213       ircd_snprintf(0, tbuf, sizeof(tbuf), "%Tu", TStime());
214       parv[1] = tbuf;
215     }
216
217     for (lp = cli_serv(&me)->down; lp; lp = lp->next)
218       if (cptr != lp->value.cptr && MsgQLength(&(cli_sendQ(lp->value.cptr))) < 8000)
219         sendcmdto_prio_one(sptr, CMD_SETTIME, lp->value.cptr, "%s", parv[1]);
220   }
221   else
222   {
223     ircd_snprintf(0, tbuf, sizeof(tbuf), "%Tu", TStime());
224     parv[1] = tbuf;
225     if (hunt_server_prio_cmd(sptr, CMD_SETTIME, cptr, 1, "%s %C", 2, parc,
226                              parv) != HUNTED_ISME)
227       return 0;
228   }
229
230   if (feature_bool(FEAT_RELIABLE_CLOCK)) {
231     if ((dt > 600) || (dt < -600))
232       sendcmdto_serv_butone(&me, CMD_WALLOPS, 0, ":Bad SETTIME from %s: %Tu",
233                             cli_name(sptr), t);
234     if (IsUser(sptr)) {
235       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :clock is not set %ld "
236                     "seconds %s : RELIABLE_CLOCK is defined", sptr,
237                     (dt < 0) ? -dt : dt, (dt < 0) ? "forwards" : "backwards");
238     }
239   } else {
240     sendto_opmask_butone(0, SNO_OLDSNO, "SETTIME from %s, clock is set %ld "
241                          "seconds %s", cli_name(sptr), (dt < 0) ? -dt : dt,
242                          (dt < 0) ? "forwards" : "backwards");
243     TSoffset -= dt;
244     if (IsUser(sptr)) {
245       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :clock is set %ld seconds %s",
246                     sptr, (dt < 0) ? -dt : dt,
247                     (dt < 0) ? "forwards" : "backwards");
248     }
249   }
250
251   return 0;
252 }