dbe234b1a67b76daab6014999ab6825682103fbe
[ircu2.10.12-pk.git] / ircd / m_squit.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_squit.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 #include "config.h"
26
27 #if 0
28 /*
29  * No need to include handlers.h here the signatures must match
30  * and we don't need to force a rebuild of all the handlers everytime
31  * we add a new one to the list. --Bleep
32  */
33 #include "handlers.h"
34 #endif /* 0 */
35 #include "client.h"
36 #include "hash.h"
37 #include "ircd.h"
38 #include "ircd_chattr.h"
39 #include "ircd_reply.h"
40 #include "ircd_string.h"
41 #include "numeric.h"
42 #include "numnicks.h"
43 #include "match.h"
44 #include "s_debug.h"
45 #include "s_misc.h"
46 #include "s_user.h"
47 #include "send.h"
48
49 #include <assert.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53
54 /*
55  *  ms_squit (server)
56  *
57  *    parv[0] = sender prefix
58  *    parv[1] = server name
59  *    parv[2] = timestamp
60  *    parv[parc-1] = comment
61  *
62  * No longer supports wildcards from servers. 
63  * No longer squits a server that gave us an malformed squit message.
64  *    - Isomer 1999-12-18
65  * 
66  */
67 int ms_squit(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
68 {
69   const char* server = parv[1];
70   struct Client *acptr;
71   time_t timestamp;
72   char *comment = 0;
73   
74   if (parc < 2) 
75     return need_more_params(sptr, "SQUIT");
76
77   comment = parv[parc-1];
78   
79   if (BadPtr(parv[parc - 1]))
80         comment = cli_name(sptr);
81         
82   acptr = FindNServer(server);
83
84   if (!acptr)
85     acptr = FindServer(server);
86
87   if (!acptr) {
88     Debug((DEBUG_NOTICE, "Ignoring SQUIT to an unknown server"));
89     return 0;
90   }
91   
92   /* If they are squitting me, we reverse it */
93   if (IsMe(acptr)) {
94         cptr = acptr;
95         acptr = &me;
96   }
97         
98   timestamp = atoi(parv[2]);
99
100   /* If atoi(parv[2]) == 0 we must indeed squit !
101    * It will be our neighbour.
102    */
103   if ( timestamp != 0 && timestamp != cli_serv(acptr)->timestamp) {
104     Debug((DEBUG_NOTICE, "Ignoring SQUIT with the wrong timestamp"));
105     return 0;
106   }
107   
108   return exit_client(cptr, acptr, sptr, comment);
109 }
110
111 /*
112  *  mo_squit (oper)
113  *
114  *    parv[0] = sender prefix
115  *    parv[1] = server name
116  *    parv[2] = comment (optional)
117  *
118  */
119 int mo_squit(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
120 {
121   const char* server;
122   struct Client *acptr;
123   struct Client *acptr2;
124   char *comment;
125       
126   if (parc < 2) 
127     return need_more_params(sptr, "SQUIT");
128
129   if (parc < 3 || BadPtr(parv[2]))
130     comment = cli_name(sptr);
131   else
132     comment = parv[2];
133
134   server = parv[1];
135   /*
136    * The following allows wild cards in SQUIT. Only usefull
137    * when the command is issued by an oper.
138    */
139   for (acptr = GlobalClientList; (acptr = next_client(acptr, server));
140       acptr = cli_next(acptr)) {
141     if (IsServer(acptr) || IsMe(acptr))
142       break;
143   }
144   
145   /* Not found? Bugger. */
146   if (!acptr || IsMe(acptr))
147     return send_reply(sptr, ERR_NOSUCHSERVER, server);
148
149   /*
150    * Look for a matching server that is closer,
151    * that way we won't accidently squit two close
152    * servers like davis.* and davis-r.* when typing
153    * /SQUIT davis*
154    */
155   for (acptr2 = cli_serv(acptr)->up; acptr2 != &me;
156       acptr2 = cli_serv(acptr2)->up)
157     if (!match(server, cli_name(acptr2)))
158       acptr = acptr2;
159   
160   /* Disallow local opers to squit remote servers */
161   if (IsLocOp(sptr) && !MyConnect(acptr))
162     return send_reply(sptr, ERR_NOPRIVILEGES);
163
164   return exit_client(cptr, acptr, sptr, comment);
165 }
166
167