]> jfr.im git - irc/rqf/shadowircd.git/blob - modules/m_accept.c
legacy irc sprintf gone
[irc/rqf/shadowircd.git] / modules / m_accept.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_accept.c: Allows a user to talk to a +g user.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
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 2 of the License, or
12 * (at your option) 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
24 * $Id: m_accept.c 254 2005-09-21 23:35:12Z nenolod $
25 */
26
27 #include "stdinc.h"
28 #include "client.h"
29 #include "hash.h"
30 #include "ircd.h"
31 #include "numeric.h"
32 #include "s_conf.h"
33 #include "s_serv.h"
34 #include "send.h"
35 #include "msg.h"
36 #include "parse.h"
37 #include "modules.h"
38
39 static int m_accept(struct Client *, struct Client *, int, const char **);
40 static void build_nicklist(struct Client *, char *, char *, const char *);
41
42 static void add_accept(struct Client *, struct Client *);
43 static void list_accepts(struct Client *);
44
45 struct Message accept_msgtab = {
46 "ACCEPT", 0, 0, 0, MFLG_SLOW | MFLG_UNREG,
47 {mg_unreg, {m_accept, 2}, mg_ignore, mg_ignore, mg_ignore, {m_accept, 2}}
48 };
49
50 mapi_clist_av1 accept_clist[] = {
51 &accept_msgtab, NULL
52 };
53 DECLARE_MODULE_AV1(accept, NULL, NULL, accept_clist, NULL, NULL, "$Revision: 254 $");
54
55 /*
56 * m_accept - ACCEPT command handler
57 * parv[0] = sender prefix
58 * parv[1] = servername
59 */
60 static int
61 m_accept(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
62 {
63 char *nick;
64 char *p = NULL;
65 static char addbuf[BUFSIZE];
66 static char delbuf[BUFSIZE];
67 struct Client *target_p;
68 int accept_num;
69
70 if(*parv[1] == '*')
71 {
72 list_accepts(source_p);
73 return 0;
74 }
75
76 build_nicklist(source_p, addbuf, delbuf, parv[1]);
77
78 /* parse the delete list */
79 for (nick = strtoken(&p, delbuf, ","); nick != NULL; nick = strtoken(&p, NULL, ","))
80 {
81 /* shouldnt happen, but lets be paranoid */
82 if((target_p = find_named_person(nick)) == NULL)
83 {
84 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
85 form_str(ERR_NOSUCHNICK), nick);
86 continue;
87 }
88
89 /* user isnt on clients accept list */
90 if(!accept_message(target_p, source_p))
91 {
92 sendto_one(source_p, form_str(ERR_ACCEPTNOT),
93 me.name, source_p->name, target_p->name);
94 continue;
95 }
96
97 rb_dlinkFindDestroy(target_p, &source_p->localClient->allow_list);
98 rb_dlinkFindDestroy(source_p, &target_p->on_allow_list);
99 }
100
101 /* get the number of accepts they have */
102 accept_num = rb_dlink_list_length(&source_p->localClient->allow_list);
103
104 /* parse the add list */
105 for (nick = strtoken(&p, addbuf, ","); nick; nick = strtoken(&p, NULL, ","))
106 {
107 /* shouldnt happen, but lets be paranoid */
108 if((target_p = find_named_person(nick)) == NULL)
109 {
110 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
111 form_str(ERR_NOSUCHNICK), nick);
112 continue;
113 }
114
115 /* user is already on clients accept list */
116 if(accept_message(target_p, source_p))
117 {
118 sendto_one(source_p, form_str(ERR_ACCEPTEXIST),
119 me.name, source_p->name, target_p->name);
120 continue;
121 }
122
123 if(accept_num >= ConfigFileEntry.max_accept)
124 {
125 sendto_one(source_p, form_str(ERR_ACCEPTFULL), me.name, source_p->name);
126 return 0;
127 }
128
129 /* why is this here? */
130 /* del_from accept(target_p, source_p); */
131 add_accept(source_p, target_p);
132 accept_num++;
133 }
134
135 return 0;
136 }
137
138 /*
139 * build_nicklist()
140 *
141 * input - pointer to client
142 * - pointer to addbuffer
143 * - pointer to remove buffer
144 * - pointer to list of nicks
145 * output -
146 * side effects - addbuf/delbuf are modified to give valid nicks
147 */
148 static void
149 build_nicklist(struct Client *source_p, char *addbuf, char *delbuf, const char *nicks)
150 {
151 char *name;
152 char *p;
153 int lenadd;
154 int lendel;
155 int del;
156 struct Client *target_p;
157 char *n = LOCAL_COPY(nicks);
158
159 *addbuf = *delbuf = '\0';
160 del = lenadd = lendel = 0;
161
162 /* build list of clients to add into addbuf, clients to remove in delbuf */
163 for (name = strtoken(&p, n, ","); name; name = strtoken(&p, NULL, ","), del = 0)
164 {
165 if(*name == '-')
166 {
167 del = 1;
168 name++;
169 }
170
171 if((target_p = find_named_person(name)) == NULL)
172 {
173 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
174 form_str(ERR_NOSUCHNICK), name);
175 continue;
176 }
177
178 /* we're deleting a client */
179 if(del)
180 {
181 if(*delbuf)
182 (void) strcat(delbuf, ",");
183
184 (void) strncat(delbuf, name, BUFSIZE - lendel - 1);
185 lendel += strlen(name) + 1;
186 }
187 /* adding a client */
188 else
189 {
190 if(*addbuf)
191 (void) strcat(addbuf, ",");
192
193 (void) strncat(addbuf, name, BUFSIZE - lenadd - 1);
194 lenadd += strlen(name) + 1;
195 }
196 }
197 }
198
199 /*
200 * add_accept()
201 *
202 * input - pointer to clients accept list to add to
203 * - pointer to client to add
204 * output - none
205 * side effects - target is added to clients list
206 */
207 static void
208 add_accept(struct Client *source_p, struct Client *target_p)
209 {
210 rb_dlinkAddAlloc(target_p, &source_p->localClient->allow_list);
211 rb_dlinkAddAlloc(source_p, &target_p->on_allow_list);
212 }
213
214
215 /*
216 * list_accepts()
217 *
218 * input - pointer to client
219 * output - none
220 * side effects - print accept list to client
221 */
222 static void
223 list_accepts(struct Client *source_p)
224 {
225 rb_dlink_node *ptr;
226 struct Client *target_p;
227 char nicks[BUFSIZE];
228 int len = 0;
229 int len2 = 0;
230 int count = 0;
231
232 *nicks = '\0';
233 len2 = strlen(source_p->name) + 10;
234
235 RB_DLINK_FOREACH(ptr, source_p->localClient->allow_list.head)
236 {
237 target_p = ptr->data;
238
239 if(target_p)
240 {
241
242 if((len + strlen(target_p->name) + len2 > BUFSIZE) || count > 14)
243 {
244 sendto_one(source_p, form_str(RPL_ACCEPTLIST),
245 me.name, source_p->name, nicks);
246
247 len = count = 0;
248 *nicks = '\0';
249 }
250
251 len += rb_snprintf(nicks + len, sizeof(nicks) - len, "%s ", target_p->name);
252 count++;
253 }
254 }
255
256 if(*nicks)
257 sendto_one(source_p, form_str(RPL_ACCEPTLIST),
258 me.name, source_p->name, nicks);
259
260 sendto_one(source_p, form_str(RPL_ENDOFACCEPT),
261 me.name, source_p->name);
262
263 }