Author: Isomer <isomer@coders.net>
[ircu2.10.12-pk.git] / ircd / m_pong.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_pong.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_string.h"
94 #include "msg.h"
95 #include "numeric.h"
96 #include "numnicks.h"
97 #include "s_user.h"
98 #include "send.h"
99
100 #include <assert.h>
101 #include <string.h>
102 #include <stdlib.h>
103
104 /*
105  * ms_pong - server message handler template
106  *
107  * parv[0] = sender prefix
108  * parv[1] = origin
109  * parv[2] = destination
110  */
111 int ms_pong(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
112 {
113   char*          origin;
114   char*          destination;
115   assert(0 != cptr);
116   assert(0 != sptr);
117   assert(IsServer(cptr));
118
119   if (parc < 2 || EmptyString(parv[1])) {
120 #if 0
121     /*
122      * ignore there is nothing the server sending it can do about it
123      */
124     sendto_one(sptr, err_str(ERR_NOORIGIN), me.name, parv[0]);
125 #endif
126     return 0;
127   }
128   origin      = parv[1];
129   destination = parv[2];
130   cptr->flags &= ~FLAGS_PINGSENT;
131   sptr->flags &= ~FLAGS_PINGSENT;
132   cptr->lasttime = CurrentTime;
133
134   if (!EmptyString(destination) && 0 != ircd_strcmp(destination, me.name)) {
135     struct Client* acptr;
136     if ((acptr = FindClient(destination))) {
137       if (MyUser(acptr))
138         sendto_one(acptr, ":%s PONG %s %s", sptr->name, origin, destination);
139       else
140         sendto_one(acptr, "%s " TOK_PONG " %s %s", NumServ(sptr), origin, destination);
141     }
142   }
143   return 0;
144 }
145
146 /*
147  * mr_pong - registration message handler
148  *
149  * parv[0] = sender prefix
150  * parv[1] = pong response echo
151  * NOTE: cptr is always unregistered here
152  */
153 int mr_pong(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
154 {
155   assert(0 != cptr);
156   assert(cptr == sptr);
157   assert(!IsRegistered(sptr));
158
159   cptr->flags &= ~FLAGS_PINGSENT;
160   cptr->lasttime = CurrentTime;
161   /*
162    * Check to see if this is a PONG :cookie reply from an
163    * unregistered user.  If so, process it. -record
164    */
165   if (0 != sptr->cookie && COOKIE_VERIFIED != sptr->cookie) {
166     if (parc > 1 && sptr->cookie == atol(parv[parc - 1])) {
167       sptr->cookie = COOKIE_VERIFIED;
168       if (sptr->user && *sptr->user->host && sptr->name[0])
169         /*
170          * NICK and USER OK
171          */
172         return register_user(cptr, sptr, sptr->name, sptr->user->username);
173     }
174     else  
175       sendto_one(sptr, ":%s %d %s :To connect, type /QUOTE PONG %u",
176                  me.name, ERR_BADPING, (sptr->name) ? sptr->name : "*",
177                  sptr->cookie);
178   }
179   return 0;
180 }
181
182 /*
183  * m_pong - normal message handler
184  *
185  * parv[0] = sender prefix
186  * parv[1] = origin
187  * parv[2] = destination
188  */
189 int m_pong(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
190 {
191   assert(0 != cptr);
192   assert(cptr == sptr);
193   cptr->flags &= ~FLAGS_PINGSENT;
194   cptr->lasttime = CurrentTime;
195   return 0;
196 }
197
198
199 #if 0
200 /*
201  * m_pong
202  *
203  * parv[0] = sender prefix
204  * parv[1] = origin
205  * parv[2] = destination
206  */
207 int m_pong(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
208 {
209   struct Client *acptr;
210   char *origin, *destination;
211
212   if (MyUser(sptr))
213     return 0;
214
215   /* Check to see if this is a PONG :cookie reply from an
216    * unregistered user.  If so, process it. -record       */
217
218   if ((!IsRegistered(sptr)) && (sptr->cookie != 0) &&
219       (sptr->cookie != COOKIE_VERIFIED) && (parc > 1))
220   {
221     if (atol(parv[parc - 1]) == (long)sptr->cookie)
222     {
223       sptr->cookie = COOKIE_VERIFIED;
224       if (sptr->user && *sptr->user->host && sptr->name[0])        /* NICK and
225                                                                    USER OK */
226         return register_user(cptr, sptr, sptr->name, sptr->user->username);
227     }
228     else
229       sendto_one(sptr, ":%s %d %s :To connect, type /QUOTE PONG %u",
230           me.name, ERR_BADPING, sptr->name, sptr->cookie);
231
232     return 0;
233   }
234
235   if (parc < 2 || *parv[1] == '\0')
236   {
237     sendto_one(sptr, err_str(ERR_NOORIGIN), me.name, parv[0]);
238     return 0;
239   }
240
241   origin = parv[1];
242   destination = parv[2];
243   cptr->flags &= ~FLAGS_PINGSENT;
244   sptr->flags &= ~FLAGS_PINGSENT;
245
246   if (!EmptyString(destination) && 0 != ircd_strcmp(destination, me.name))
247   {
248     if ((acptr = FindClient(destination)))
249       sendto_one(acptr, ":%s PONG %s %s", parv[0], origin, destination);
250     else
251     {
252       sendto_one(sptr, err_str(ERR_NOSUCHSERVER),
253           me.name, parv[0], destination);
254       return 0;
255     }
256   }
257 #ifdef        DEBUGMODE
258   else
259     Debug((DEBUG_NOTICE, "PONG: %s %s",
260         origin, destination ? destination : "*"));
261 #endif
262   return 0;
263 }
264 #endif /* 0 */