1 /*
2  *  ircd-ratbox: A slightly useful ircd.
3  *  m_knock.c: Requests to be invited to a channel.
4  *
5  *  Copyright (C) 1996-2002 Hybrid Development Team
6  *  Copyright (C) 2002-2012 ircd-ratbox development team
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
21  *  USA
22  *
23  *  $Id: m_knock.c 27371 2012-03-16 05:33:15Z dubkat $
24  */
25 
26 #include "stdinc.h"
27 #include "struct.h"
28 #include "channel.h"
29 #include "client.h"
30 #include "hash.h"
31 #include "match.h"
32 #include "ircd.h"
33 #include "numeric.h"
34 #include "send.h"
35 #include "s_conf.h"
36 #include "parse.h"
37 #include "modules.h"
38 #include "s_serv.h"
39 
40 static int m_knock(struct Client *, struct Client *, int, const char **);
41 
42 struct Message knock_msgtab = {
43 	"KNOCK", 0, 0, 0, MFLG_SLOW,
44 	{mg_unreg, {m_knock, 2}, {m_knock, 2}, mg_ignore, mg_ignore, {m_knock, 2}}
45 };
46 
47 mapi_clist_av1 knock_clist[] = { &knock_msgtab, NULL };
48 
49 DECLARE_MODULE_AV1(knock, NULL, NULL, knock_clist, NULL, NULL, "$Revision: 27371 $");
50 
51 /* m_knock
52  *    parv[0] = sender prefix
53  *    parv[1] = channel
54  *
55  *  The KNOCK command has the following syntax:
56  *   :<sender> KNOCK <channel>
57  *
58  *  If a user is not banned from the channel they can use the KNOCK
59  *  command to have the server NOTICE the channel operators notifying
60  *  they would like to join.  Helpful if the channel is invite-only, the
61  *  key is forgotten, or the channel is full (INVITE can bypass each one
62  *  of these conditions.  Concept by Dianora <db@db.net> and written by
63  *  <anonymous>
64  */
65 static int
m_knock(struct Client * client_p,struct Client * source_p,int parc,const char * parv[])66 m_knock(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
67 {
68 	struct Channel *chptr;
69 	char *p, *name;
70 
71 	if(MyClient(source_p) && ConfigChannel.use_knock == 0)
72 	{
73 		sendto_one(source_p, form_str(ERR_KNOCKDISABLED), me.name, source_p->name);
74 		return 0;
75 	}
76 
77 	name = LOCAL_COPY(parv[1]);
78 
79 	/* dont allow one knock to multiple chans */
80 	if((p = strchr(name, ',')))
81 		*p = '\0';
82 
83 	if(!IsChannelName(name))
84 	{
85 		sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL, form_str(ERR_NOSUCHCHANNEL), name);
86 		return 0;
87 	}
88 
89 	if((chptr = find_channel(name)) == NULL)
90 	{
91 		sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL, form_str(ERR_NOSUCHCHANNEL), name);
92 		return 0;
93 	}
94 
95 	if(IsMember(source_p, chptr))
96 	{
97 		if(MyClient(source_p))
98 			sendto_one(source_p, form_str(ERR_KNOCKONCHAN),
99 				   me.name, source_p->name, name);
100 		return 0;
101 	}
102 
103 	if(!((chptr->mode.mode & MODE_INVITEONLY) || (*chptr->mode.key) ||
104 	     (chptr->mode.limit &&
105 	      rb_dlink_list_length(&chptr->members) >= (unsigned long)chptr->mode.limit)))
106 	{
107 		sendto_one_numeric(source_p, ERR_CHANOPEN, form_str(ERR_CHANOPEN), name);
108 		return 0;
109 	}
110 
111 	/* cant knock to a +p channel */
112 	if(HiddenChannel(chptr))
113 	{
114 		sendto_one_numeric(source_p, ERR_CANNOTSENDTOCHAN,
115 				   form_str(ERR_CANNOTSENDTOCHAN), name);
116 		return 0;
117 	}
118 
119 
120 	if(MyClient(source_p))
121 	{
122 		/* don't allow a knock if the user is banned */
123 		if(is_banned(chptr, source_p, NULL, NULL, NULL) == CHFL_BAN)
124 		{
125 			sendto_one_numeric(source_p, ERR_CANNOTSENDTOCHAN,
126 					   form_str(ERR_CANNOTSENDTOCHAN), name);
127 			return 0;
128 		}
129 
130 		/* local flood protection:
131 		 * allow one knock per user per knock_delay
132 		 * allow one knock per channel per knock_delay_channel
133 		 */
134 		if(!IsOper(source_p) &&
135 		   (source_p->localClient->last_knock + ConfigChannel.knock_delay) >
136 		   rb_current_time())
137 		{
138 			sendto_one(source_p, form_str(ERR_TOOMANYKNOCK),
139 				   me.name, source_p->name, name, "user");
140 			return 0;
141 		}
142 		else if((chptr->last_knock + ConfigChannel.knock_delay_channel) > rb_current_time())
143 		{
144 			sendto_one(source_p, form_str(ERR_TOOMANYKNOCK),
145 				   me.name, source_p->name, name, "channel");
146 			return 0;
147 		}
148 
149 		/* ok, we actually can send the knock, tell client */
150 		source_p->localClient->last_knock = rb_current_time();
151 
152 		sendto_one(source_p, form_str(RPL_KNOCKDLVR), me.name, source_p->name, name);
153 	}
154 
155 	chptr->last_knock = rb_current_time();
156 
157 	if(ConfigChannel.use_knock)
158 		sendto_channel_local(ONLY_CHANOPS, chptr, form_str(RPL_KNOCK),
159 				     me.name, name, name, source_p->name,
160 				     source_p->username, source_p->host);
161 
162 	sendto_server(client_p, chptr, CAP_KNOCK | CAP_TS6, NOCAPS,
163 		      ":%s KNOCK %s", use_id(source_p), name);
164 	sendto_server(client_p, chptr, CAP_KNOCK, CAP_TS6, ":%s KNOCK %s", source_p->name, name);
165 	return 0;
166 }
167