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