1 |
adx |
30 |
/* |
2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
adx |
30 |
* |
4 |
michael |
2916 |
* Copyright (c) 1997-2014 ircd-hybrid development team |
5 |
adx |
30 |
* |
6 |
|
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
|
|
* USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
/*! \file channel.c |
23 |
|
|
* \brief Responsible for managing channels, members, bans and topics |
24 |
knight |
31 |
* \version $Id$ |
25 |
adx |
30 |
*/ |
26 |
|
|
|
27 |
|
|
#include "stdinc.h" |
28 |
michael |
1011 |
#include "list.h" |
29 |
adx |
30 |
#include "channel.h" |
30 |
|
|
#include "channel_mode.h" |
31 |
|
|
#include "client.h" |
32 |
|
|
#include "hash.h" |
33 |
michael |
1632 |
#include "conf.h" |
34 |
michael |
371 |
#include "hostmask.h" |
35 |
adx |
30 |
#include "irc_string.h" |
36 |
|
|
#include "ircd.h" |
37 |
|
|
#include "numeric.h" |
38 |
michael |
2916 |
#include "s_serv.h" |
39 |
adx |
30 |
#include "send.h" |
40 |
|
|
#include "event.h" |
41 |
|
|
#include "memory.h" |
42 |
michael |
1654 |
#include "mempool.h" |
43 |
michael |
1751 |
#include "s_misc.h" |
44 |
michael |
1826 |
#include "resv.h" |
45 |
adx |
30 |
|
46 |
|
|
dlink_list global_channel_list = { NULL, NULL, 0 }; |
47 |
michael |
1654 |
mp_pool_t *ban_pool; /*! \todo ban_pool shouldn't be a global var */ |
48 |
adx |
30 |
|
49 |
michael |
1654 |
static mp_pool_t *member_pool = NULL; |
50 |
|
|
static mp_pool_t *channel_pool = NULL; |
51 |
adx |
30 |
|
52 |
|
|
static char buf[IRCD_BUFSIZE]; |
53 |
|
|
static char modebuf[MODEBUFLEN]; |
54 |
|
|
static char parabuf[MODEBUFLEN]; |
55 |
|
|
|
56 |
|
|
|
57 |
|
|
/*! \brief Initializes the channel blockheap, adds known channel CAPAB |
58 |
|
|
*/ |
59 |
|
|
void |
60 |
michael |
1798 |
channel_init(void) |
61 |
adx |
30 |
{ |
62 |
|
|
add_capability("EX", CAP_EX, 1); |
63 |
|
|
add_capability("IE", CAP_IE, 1); |
64 |
|
|
|
65 |
michael |
1654 |
channel_pool = mp_pool_new(sizeof(struct Channel), MP_CHUNK_SIZE_CHANNEL); |
66 |
|
|
ban_pool = mp_pool_new(sizeof(struct Ban), MP_CHUNK_SIZE_BAN); |
67 |
|
|
member_pool = mp_pool_new(sizeof(struct Membership), MP_CHUNK_SIZE_MEMBER); |
68 |
adx |
30 |
} |
69 |
|
|
|
70 |
|
|
/*! \brief adds a user to a channel by adding another link to the |
71 |
|
|
* channels member chain. |
72 |
|
|
* \param chptr pointer to channel to add client to |
73 |
|
|
* \param who pointer to client (who) to add |
74 |
|
|
* \param flags flags for chanops etc |
75 |
|
|
* \param flood_ctrl whether to count this join in flood calculations |
76 |
|
|
*/ |
77 |
|
|
void |
78 |
|
|
add_user_to_channel(struct Channel *chptr, struct Client *who, |
79 |
|
|
unsigned int flags, int flood_ctrl) |
80 |
|
|
{ |
81 |
|
|
struct Membership *ms = NULL; |
82 |
|
|
|
83 |
|
|
if (GlobalSetOptions.joinfloodtime > 0) |
84 |
|
|
{ |
85 |
|
|
if (flood_ctrl) |
86 |
|
|
chptr->number_joined++; |
87 |
|
|
|
88 |
|
|
chptr->number_joined -= (CurrentTime - chptr->last_join_time) * |
89 |
|
|
(((float)GlobalSetOptions.joinfloodcount) / |
90 |
|
|
(float)GlobalSetOptions.joinfloodtime); |
91 |
|
|
|
92 |
|
|
if (chptr->number_joined <= 0) |
93 |
|
|
{ |
94 |
|
|
chptr->number_joined = 0; |
95 |
|
|
ClearJoinFloodNoticed(chptr); |
96 |
|
|
} |
97 |
|
|
else if (chptr->number_joined >= GlobalSetOptions.joinfloodcount) |
98 |
|
|
{ |
99 |
|
|
chptr->number_joined = GlobalSetOptions.joinfloodcount; |
100 |
|
|
|
101 |
|
|
if (!IsSetJoinFloodNoticed(chptr)) |
102 |
|
|
{ |
103 |
|
|
SetJoinFloodNoticed(chptr); |
104 |
michael |
1618 |
sendto_realops_flags(UMODE_BOTS, L_ALL, SEND_NOTICE, |
105 |
adx |
30 |
"Possible Join Flooder %s on %s target: %s", |
106 |
|
|
get_client_name(who, HIDE_IP), |
107 |
|
|
who->servptr->name, chptr->chname); |
108 |
|
|
} |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
chptr->last_join_time = CurrentTime; |
112 |
|
|
} |
113 |
|
|
|
114 |
michael |
1654 |
ms = mp_pool_get(member_pool); |
115 |
|
|
memset(ms, 0, sizeof(*ms)); |
116 |
|
|
|
117 |
adx |
30 |
ms->client_p = who; |
118 |
|
|
ms->chptr = chptr; |
119 |
|
|
ms->flags = flags; |
120 |
|
|
|
121 |
|
|
dlinkAdd(ms, &ms->channode, &chptr->members); |
122 |
|
|
dlinkAdd(ms, &ms->usernode, &who->channel); |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
/*! \brief deletes an user from a channel by removing a link in the |
126 |
|
|
* channels member chain. |
127 |
|
|
* \param member pointer to Membership struct |
128 |
|
|
*/ |
129 |
|
|
void |
130 |
|
|
remove_user_from_channel(struct Membership *member) |
131 |
|
|
{ |
132 |
|
|
struct Client *client_p = member->client_p; |
133 |
|
|
struct Channel *chptr = member->chptr; |
134 |
|
|
|
135 |
|
|
dlinkDelete(&member->channode, &chptr->members); |
136 |
|
|
dlinkDelete(&member->usernode, &client_p->channel); |
137 |
|
|
|
138 |
michael |
1654 |
mp_pool_release(member); |
139 |
adx |
30 |
|
140 |
michael |
1011 |
if (chptr->members.head == NULL) |
141 |
adx |
30 |
destroy_channel(chptr); |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
/* send_members() |
145 |
|
|
* |
146 |
|
|
* inputs - |
147 |
|
|
* output - NONE |
148 |
|
|
* side effects - |
149 |
|
|
*/ |
150 |
|
|
static void |
151 |
|
|
send_members(struct Client *client_p, struct Channel *chptr, |
152 |
|
|
char *lmodebuf, char *lparabuf) |
153 |
|
|
{ |
154 |
michael |
1847 |
const dlink_node *ptr = NULL; |
155 |
adx |
30 |
int tlen; /* length of text to append */ |
156 |
|
|
char *t, *start; /* temp char pointer */ |
157 |
|
|
|
158 |
michael |
1847 |
start = t = buf + snprintf(buf, sizeof(buf), ":%s SJOIN %lu %s %s %s:", |
159 |
|
|
ID_or_name(&me, client_p), |
160 |
|
|
(unsigned long)chptr->channelts, |
161 |
|
|
chptr->chname, lmodebuf, lparabuf); |
162 |
adx |
30 |
|
163 |
|
|
DLINK_FOREACH(ptr, chptr->members.head) |
164 |
|
|
{ |
165 |
michael |
1847 |
const struct Membership *ms = ptr->data; |
166 |
adx |
30 |
|
167 |
|
|
tlen = strlen(IsCapable(client_p, CAP_TS6) ? |
168 |
|
|
ID(ms->client_p) : ms->client_p->name) + 1; /* nick + space */ |
169 |
|
|
|
170 |
|
|
if (ms->flags & CHFL_CHANOP) |
171 |
|
|
tlen++; |
172 |
|
|
#ifdef HALFOPS |
173 |
adx |
356 |
else if (ms->flags & CHFL_HALFOP) |
174 |
adx |
30 |
tlen++; |
175 |
|
|
#endif |
176 |
|
|
if (ms->flags & CHFL_VOICE) |
177 |
|
|
tlen++; |
178 |
|
|
|
179 |
|
|
/* space will be converted into CR, but we also need space for LF.. |
180 |
|
|
* That's why we use '- 1' here |
181 |
|
|
* -adx */ |
182 |
michael |
1330 |
if (t + tlen - buf > IRCD_BUFSIZE - 1) |
183 |
adx |
30 |
{ |
184 |
|
|
*(t - 1) = '\0'; /* kill the space and terminate the string */ |
185 |
|
|
sendto_one(client_p, "%s", buf); |
186 |
|
|
t = start; |
187 |
|
|
} |
188 |
|
|
|
189 |
adx |
356 |
if ((ms->flags & (CHFL_CHANOP | CHFL_HALFOP))) |
190 |
|
|
*t++ = (!(ms->flags & CHFL_CHANOP) && IsCapable(client_p, CAP_HOPS)) ? |
191 |
|
|
'%' : '@'; |
192 |
|
|
if ((ms->flags & CHFL_VOICE)) |
193 |
|
|
*t++ = '+'; |
194 |
adx |
30 |
|
195 |
|
|
if (IsCapable(client_p, CAP_TS6)) |
196 |
|
|
strcpy(t, ID(ms->client_p)); |
197 |
|
|
else |
198 |
|
|
strcpy(t, ms->client_p->name); |
199 |
|
|
t += strlen(t); |
200 |
|
|
*t++ = ' '; |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
/* should always be non-NULL unless we have a kind of persistent channels */ |
204 |
|
|
if (chptr->members.head != NULL) |
205 |
|
|
t--; /* take the space out */ |
206 |
|
|
*t = '\0'; |
207 |
|
|
sendto_one(client_p, "%s", buf); |
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
/*! \brief sends +b/+e/+I |
211 |
|
|
* \param client_p client pointer to server |
212 |
|
|
* \param chptr pointer to channel |
213 |
|
|
* \param top pointer to top of mode link list to send |
214 |
|
|
* \param flag char flag flagging type of mode. Currently this can be 'b', e' or 'I' |
215 |
|
|
*/ |
216 |
|
|
static void |
217 |
|
|
send_mode_list(struct Client *client_p, struct Channel *chptr, |
218 |
michael |
1847 |
const dlink_list *top, char flag) |
219 |
adx |
30 |
{ |
220 |
|
|
int ts5 = !IsCapable(client_p, CAP_TS6); |
221 |
michael |
1847 |
const dlink_node *lp = NULL; |
222 |
adx |
30 |
char pbuf[IRCD_BUFSIZE]; |
223 |
|
|
int tlen, mlen, cur_len, count = 0; |
224 |
|
|
char *mp = NULL, *pp = pbuf; |
225 |
|
|
|
226 |
|
|
if (top == NULL || top->length == 0) |
227 |
|
|
return; |
228 |
|
|
|
229 |
|
|
if (ts5) |
230 |
michael |
1847 |
mlen = snprintf(buf, sizeof(buf), ":%s MODE %s +", me.name, chptr->chname); |
231 |
adx |
30 |
else |
232 |
michael |
1847 |
mlen = snprintf(buf, sizeof(buf), ":%s BMASK %lu %s %c :", me.id, |
233 |
michael |
1793 |
(unsigned long)chptr->channelts, chptr->chname, flag); |
234 |
adx |
30 |
|
235 |
|
|
/* MODE needs additional one byte for space between buf and pbuf */ |
236 |
|
|
cur_len = mlen + ts5; |
237 |
|
|
mp = buf + mlen; |
238 |
|
|
|
239 |
|
|
DLINK_FOREACH(lp, top->head) |
240 |
|
|
{ |
241 |
michael |
1847 |
const struct Ban *banptr = lp->data; |
242 |
adx |
30 |
|
243 |
|
|
/* must add another b/e/I letter if we use MODE */ |
244 |
|
|
tlen = banptr->len + 3 + ts5; |
245 |
|
|
|
246 |
|
|
/* |
247 |
|
|
* send buffer and start over if we cannot fit another ban, |
248 |
|
|
* or if the target is non-ts6 and we have too many modes in |
249 |
|
|
* in this line. |
250 |
|
|
*/ |
251 |
|
|
if (cur_len + (tlen - 1) > IRCD_BUFSIZE - 2 || |
252 |
|
|
(!IsCapable(client_p, CAP_TS6) && |
253 |
|
|
(count >= MAXMODEPARAMS || pp - pbuf >= MODEBUFLEN))) |
254 |
|
|
{ |
255 |
|
|
*(pp - 1) = '\0'; /* get rid of trailing space on buffer */ |
256 |
|
|
sendto_one(client_p, "%s%s%s", buf, ts5 ? " " : "", pbuf); |
257 |
|
|
|
258 |
|
|
cur_len = mlen + ts5; |
259 |
|
|
mp = buf + mlen; |
260 |
|
|
pp = pbuf; |
261 |
|
|
count = 0; |
262 |
|
|
} |
263 |
|
|
|
264 |
|
|
count++; |
265 |
|
|
if (ts5) |
266 |
|
|
{ |
267 |
|
|
*mp++ = flag; |
268 |
|
|
*mp = '\0'; |
269 |
|
|
} |
270 |
|
|
|
271 |
michael |
2296 |
pp += sprintf(pp, "%s!%s@%s ", banptr->name, banptr->user, |
272 |
michael |
1793 |
banptr->host); |
273 |
adx |
30 |
cur_len += tlen; |
274 |
|
|
} |
275 |
|
|
|
276 |
|
|
*(pp - 1) = '\0'; /* get rid of trailing space on buffer */ |
277 |
|
|
sendto_one(client_p, "%s%s%s", buf, ts5 ? " " : "", pbuf); |
278 |
|
|
} |
279 |
|
|
|
280 |
|
|
/*! \brief send "client_p" a full list of the modes for channel chptr |
281 |
|
|
* \param client_p pointer to client client_p |
282 |
|
|
* \param chptr pointer to channel pointer |
283 |
|
|
*/ |
284 |
|
|
void |
285 |
|
|
send_channel_modes(struct Client *client_p, struct Channel *chptr) |
286 |
|
|
{ |
287 |
|
|
*modebuf = *parabuf = '\0'; |
288 |
|
|
channel_modes(chptr, client_p, modebuf, parabuf); |
289 |
|
|
send_members(client_p, chptr, modebuf, parabuf); |
290 |
|
|
|
291 |
|
|
send_mode_list(client_p, chptr, &chptr->banlist, 'b'); |
292 |
michael |
1661 |
send_mode_list(client_p, chptr, &chptr->exceptlist, 'e'); |
293 |
|
|
send_mode_list(client_p, chptr, &chptr->invexlist, 'I'); |
294 |
adx |
30 |
} |
295 |
|
|
|
296 |
|
|
/*! \brief check channel name for invalid characters |
297 |
|
|
* \param name pointer to channel name string |
298 |
michael |
632 |
* \param local indicates whether it's a local or remote creation |
299 |
|
|
* \return 0 if invalid, 1 otherwise |
300 |
adx |
30 |
*/ |
301 |
|
|
int |
302 |
michael |
1847 |
check_channel_name(const char *name, const int local) |
303 |
adx |
30 |
{ |
304 |
michael |
632 |
const char *p = name; |
305 |
michael |
1455 |
const int max_length = local ? LOCAL_CHANNELLEN : CHANNELLEN; |
306 |
adx |
30 |
assert(name != NULL); |
307 |
|
|
|
308 |
michael |
632 |
if (!IsChanPrefix(*p)) |
309 |
|
|
return 0; |
310 |
adx |
30 |
|
311 |
db |
633 |
if (!local || !ConfigChannel.disable_fake_channels) |
312 |
michael |
632 |
{ |
313 |
|
|
while (*++p) |
314 |
db |
634 |
if (!IsChanChar(*p)) |
315 |
michael |
632 |
return 0; |
316 |
|
|
} |
317 |
|
|
else |
318 |
|
|
{ |
319 |
|
|
while (*++p) |
320 |
db |
634 |
if (!IsVisibleChanChar(*p)) |
321 |
michael |
632 |
return 0; |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
return p - name <= max_length; |
325 |
adx |
30 |
} |
326 |
|
|
|
327 |
|
|
void |
328 |
|
|
remove_ban(struct Ban *bptr, dlink_list *list) |
329 |
|
|
{ |
330 |
|
|
dlinkDelete(&bptr->node, list); |
331 |
|
|
|
332 |
|
|
MyFree(bptr->name); |
333 |
michael |
2296 |
MyFree(bptr->user); |
334 |
adx |
30 |
MyFree(bptr->host); |
335 |
|
|
MyFree(bptr->who); |
336 |
|
|
|
337 |
michael |
1654 |
mp_pool_release(bptr); |
338 |
adx |
30 |
} |
339 |
|
|
|
340 |
|
|
/* free_channel_list() |
341 |
|
|
* |
342 |
|
|
* inputs - pointer to dlink_list |
343 |
|
|
* output - NONE |
344 |
|
|
* side effects - |
345 |
|
|
*/ |
346 |
|
|
void |
347 |
|
|
free_channel_list(dlink_list *list) |
348 |
|
|
{ |
349 |
|
|
dlink_node *ptr = NULL, *next_ptr = NULL; |
350 |
|
|
|
351 |
|
|
DLINK_FOREACH_SAFE(ptr, next_ptr, list->head) |
352 |
|
|
remove_ban(ptr->data, list); |
353 |
|
|
|
354 |
|
|
assert(list->tail == NULL && list->head == NULL); |
355 |
|
|
} |
356 |
|
|
|
357 |
|
|
/*! \brief Get Channel block for chname (and allocate a new channel |
358 |
|
|
* block, if it didn't exist before) |
359 |
michael |
632 |
* \param chname channel name |
360 |
|
|
* \return channel block |
361 |
adx |
30 |
*/ |
362 |
|
|
struct Channel * |
363 |
michael |
632 |
make_channel(const char *chname) |
364 |
adx |
30 |
{ |
365 |
|
|
struct Channel *chptr = NULL; |
366 |
|
|
|
367 |
michael |
632 |
assert(!EmptyString(chname)); |
368 |
adx |
30 |
|
369 |
michael |
1654 |
chptr = mp_pool_get(channel_pool); |
370 |
adx |
30 |
|
371 |
michael |
1654 |
memset(chptr, 0, sizeof(*chptr)); |
372 |
|
|
|
373 |
adx |
30 |
/* doesn't hurt to set it here */ |
374 |
michael |
632 |
chptr->channelts = CurrentTime; |
375 |
|
|
chptr->last_join_time = CurrentTime; |
376 |
adx |
30 |
|
377 |
|
|
strlcpy(chptr->chname, chname, sizeof(chptr->chname)); |
378 |
|
|
dlinkAdd(chptr, &chptr->node, &global_channel_list); |
379 |
|
|
|
380 |
|
|
hash_add_channel(chptr); |
381 |
|
|
|
382 |
|
|
return chptr; |
383 |
|
|
} |
384 |
|
|
|
385 |
|
|
/*! \brief walk through this channel, and destroy it. |
386 |
|
|
* \param chptr channel pointer |
387 |
|
|
*/ |
388 |
|
|
void |
389 |
|
|
destroy_channel(struct Channel *chptr) |
390 |
|
|
{ |
391 |
|
|
dlink_node *ptr = NULL, *ptr_next = NULL; |
392 |
|
|
|
393 |
|
|
DLINK_FOREACH_SAFE(ptr, ptr_next, chptr->invites.head) |
394 |
|
|
del_invite(chptr, ptr->data); |
395 |
|
|
|
396 |
|
|
/* free ban/exception/invex lists */ |
397 |
|
|
free_channel_list(&chptr->banlist); |
398 |
|
|
free_channel_list(&chptr->exceptlist); |
399 |
|
|
free_channel_list(&chptr->invexlist); |
400 |
|
|
|
401 |
|
|
dlinkDelete(&chptr->node, &global_channel_list); |
402 |
|
|
hash_del_channel(chptr); |
403 |
|
|
|
404 |
michael |
1654 |
mp_pool_release(chptr); |
405 |
adx |
30 |
} |
406 |
|
|
|
407 |
|
|
/*! |
408 |
|
|
* \param chptr pointer to channel |
409 |
|
|
* \return string pointer "=" if public, "@" if secret else "*" |
410 |
|
|
*/ |
411 |
|
|
static const char * |
412 |
michael |
1013 |
channel_pub_or_secret(const struct Channel *chptr) |
413 |
adx |
30 |
{ |
414 |
|
|
if (SecretChannel(chptr)) |
415 |
|
|
return "@"; |
416 |
|
|
if (PrivateChannel(chptr)) |
417 |
|
|
return "*"; |
418 |
|
|
return "="; |
419 |
|
|
} |
420 |
|
|
|
421 |
|
|
/*! \brief lists all names on given channel |
422 |
|
|
* \param source_p pointer to client struct requesting names |
423 |
|
|
* \param chptr pointer to channel block |
424 |
|
|
* \param show_eon show ENDOFNAMES numeric or not |
425 |
|
|
* (don't want it with /names with no params) |
426 |
|
|
*/ |
427 |
|
|
void |
428 |
|
|
channel_member_names(struct Client *source_p, struct Channel *chptr, |
429 |
|
|
int show_eon) |
430 |
|
|
{ |
431 |
michael |
1847 |
const dlink_node *ptr = NULL; |
432 |
adx |
30 |
char lbuf[IRCD_BUFSIZE + 1]; |
433 |
|
|
char *t = NULL, *start = NULL; |
434 |
|
|
int tlen = 0; |
435 |
|
|
int is_member = IsMember(source_p, chptr); |
436 |
michael |
1146 |
int multi_prefix = HasCap(source_p, CAP_MULTI_PREFIX) != 0; |
437 |
michael |
2910 |
int uhnames = HasCap(source_p, CAP_UHNAMES) != 0; |
438 |
adx |
30 |
|
439 |
|
|
if (PubChannel(chptr) || is_member) |
440 |
|
|
{ |
441 |
michael |
1847 |
t = lbuf + snprintf(lbuf, sizeof(lbuf), form_str(RPL_NAMREPLY), |
442 |
|
|
me.name, source_p->name, |
443 |
|
|
channel_pub_or_secret(chptr), chptr->chname); |
444 |
adx |
30 |
start = t; |
445 |
|
|
|
446 |
|
|
DLINK_FOREACH(ptr, chptr->members.head) |
447 |
|
|
{ |
448 |
michael |
1847 |
const struct Membership *ms = ptr->data; |
449 |
adx |
30 |
|
450 |
michael |
1847 |
if (HasUMode(ms->client_p, UMODE_INVISIBLE) && !is_member) |
451 |
adx |
30 |
continue; |
452 |
|
|
|
453 |
michael |
2910 |
if (!uhnames) |
454 |
|
|
tlen = strlen(ms->client_p->name) + 1; /* nick + space */ |
455 |
|
|
else |
456 |
|
|
tlen = strlen(ms->client_p->name) + strlen(ms->client_p->username) + |
457 |
|
|
strlen(ms->client_p->host) + 3; |
458 |
adx |
30 |
|
459 |
michael |
506 |
if (!multi_prefix) |
460 |
|
|
{ |
461 |
|
|
if (ms->flags & (CHFL_CHANOP | CHFL_HALFOP | CHFL_VOICE)) |
462 |
|
|
++tlen; |
463 |
|
|
} |
464 |
|
|
else |
465 |
|
|
{ |
466 |
|
|
if (ms->flags & CHFL_CHANOP) |
467 |
|
|
++tlen; |
468 |
|
|
if (ms->flags & CHFL_HALFOP) |
469 |
|
|
++tlen; |
470 |
|
|
if (ms->flags & CHFL_VOICE) |
471 |
|
|
++tlen; |
472 |
|
|
} |
473 |
|
|
|
474 |
adx |
675 |
if (t + tlen - lbuf > IRCD_BUFSIZE - 2) |
475 |
adx |
30 |
{ |
476 |
|
|
*(t - 1) = '\0'; |
477 |
|
|
sendto_one(source_p, "%s", lbuf); |
478 |
|
|
t = start; |
479 |
|
|
} |
480 |
|
|
|
481 |
michael |
2910 |
if (!uhnames) |
482 |
|
|
t += sprintf(t, "%s%s ", get_member_status(ms, multi_prefix), |
483 |
|
|
ms->client_p->name); |
484 |
|
|
else |
485 |
|
|
t += sprintf(t, "%s%s!%s@%s ", get_member_status(ms, multi_prefix), |
486 |
|
|
ms->client_p->name, ms->client_p->username, |
487 |
|
|
ms->client_p->host); |
488 |
adx |
30 |
} |
489 |
|
|
|
490 |
|
|
if (tlen != 0) |
491 |
|
|
{ |
492 |
|
|
*(t - 1) = '\0'; |
493 |
|
|
sendto_one(source_p, "%s", lbuf); |
494 |
|
|
} |
495 |
|
|
} |
496 |
|
|
|
497 |
|
|
if (show_eon) |
498 |
michael |
2910 |
sendto_one(source_p, form_str(RPL_ENDOFNAMES), me.name, |
499 |
|
|
source_p->name, chptr->chname); |
500 |
adx |
30 |
} |
501 |
|
|
|
502 |
|
|
/*! \brief adds client to invite list |
503 |
|
|
* \param chptr pointer to channel block |
504 |
|
|
* \param who pointer to client to add invite to |
505 |
|
|
*/ |
506 |
|
|
void |
507 |
|
|
add_invite(struct Channel *chptr, struct Client *who) |
508 |
|
|
{ |
509 |
|
|
del_invite(chptr, who); |
510 |
|
|
|
511 |
|
|
/* |
512 |
|
|
* delete last link in chain if the list is max length |
513 |
|
|
*/ |
514 |
michael |
317 |
if (dlink_list_length(&who->localClient->invited) >= |
515 |
adx |
30 |
ConfigChannel.max_chans_per_user) |
516 |
michael |
317 |
del_invite(who->localClient->invited.tail->data, who); |
517 |
adx |
30 |
|
518 |
|
|
/* add client to channel invite list */ |
519 |
|
|
dlinkAdd(who, make_dlink_node(), &chptr->invites); |
520 |
|
|
|
521 |
|
|
/* add channel to the end of the client invite list */ |
522 |
michael |
317 |
dlinkAdd(chptr, make_dlink_node(), &who->localClient->invited); |
523 |
adx |
30 |
} |
524 |
|
|
|
525 |
|
|
/*! \brief Delete Invite block from channel invite list |
526 |
|
|
* and client invite list |
527 |
|
|
* \param chptr pointer to Channel struct |
528 |
|
|
* \param who pointer to client to remove invites from |
529 |
|
|
*/ |
530 |
|
|
void |
531 |
|
|
del_invite(struct Channel *chptr, struct Client *who) |
532 |
|
|
{ |
533 |
|
|
dlink_node *ptr = NULL; |
534 |
|
|
|
535 |
michael |
317 |
if ((ptr = dlinkFindDelete(&who->localClient->invited, chptr))) |
536 |
adx |
30 |
free_dlink_node(ptr); |
537 |
|
|
|
538 |
|
|
if ((ptr = dlinkFindDelete(&chptr->invites, who))) |
539 |
|
|
free_dlink_node(ptr); |
540 |
|
|
} |
541 |
|
|
|
542 |
|
|
/* get_member_status() |
543 |
|
|
* |
544 |
|
|
* inputs - pointer to struct Membership |
545 |
|
|
* - YES if we can combine different flags |
546 |
|
|
* output - string either @, +, % or "" depending on whether |
547 |
|
|
* chanop, voiced or user |
548 |
|
|
* side effects - |
549 |
|
|
* |
550 |
|
|
* NOTE: Returned string is usually a static buffer |
551 |
|
|
* (like in get_client_name) |
552 |
|
|
*/ |
553 |
|
|
const char * |
554 |
michael |
2133 |
get_member_status(const struct Membership *ms, const int combine) |
555 |
adx |
30 |
{ |
556 |
|
|
static char buffer[4]; |
557 |
michael |
1902 |
char *p = buffer; |
558 |
adx |
30 |
|
559 |
|
|
if (ms->flags & CHFL_CHANOP) |
560 |
|
|
{ |
561 |
|
|
if (!combine) |
562 |
|
|
return "@"; |
563 |
|
|
*p++ = '@'; |
564 |
|
|
} |
565 |
|
|
|
566 |
|
|
#ifdef HALFOPS |
567 |
|
|
if (ms->flags & CHFL_HALFOP) |
568 |
|
|
{ |
569 |
|
|
if (!combine) |
570 |
|
|
return "%"; |
571 |
|
|
*p++ = '%'; |
572 |
|
|
} |
573 |
|
|
#endif |
574 |
|
|
|
575 |
|
|
if (ms->flags & CHFL_VOICE) |
576 |
|
|
*p++ = '+'; |
577 |
|
|
*p = '\0'; |
578 |
|
|
|
579 |
|
|
return buffer; |
580 |
|
|
} |
581 |
|
|
|
582 |
|
|
/*! |
583 |
|
|
* \param who pointer to Client to check |
584 |
|
|
* \param list pointer to ban list to search |
585 |
|
|
* \return 1 if ban found for given n!u\@h mask, 0 otherwise |
586 |
|
|
* |
587 |
|
|
*/ |
588 |
|
|
static int |
589 |
|
|
find_bmask(const struct Client *who, const dlink_list *const list) |
590 |
|
|
{ |
591 |
|
|
const dlink_node *ptr = NULL; |
592 |
|
|
|
593 |
|
|
DLINK_FOREACH(ptr, list->head) |
594 |
|
|
{ |
595 |
michael |
1455 |
const struct Ban *bp = ptr->data; |
596 |
adx |
30 |
|
597 |
michael |
2296 |
if (!match(bp->name, who->name) && !match(bp->user, who->username)) |
598 |
michael |
371 |
{ |
599 |
|
|
switch (bp->type) |
600 |
|
|
{ |
601 |
|
|
case HM_HOST: |
602 |
michael |
1652 |
if (!match(bp->host, who->host) || !match(bp->host, who->sockhost)) |
603 |
michael |
371 |
return 1; |
604 |
|
|
break; |
605 |
|
|
case HM_IPV4: |
606 |
|
|
if (who->localClient->aftype == AF_INET) |
607 |
|
|
if (match_ipv4(&who->localClient->ip, &bp->addr, bp->bits)) |
608 |
|
|
return 1; |
609 |
|
|
break; |
610 |
|
|
#ifdef IPV6 |
611 |
|
|
case HM_IPV6: |
612 |
|
|
if (who->localClient->aftype == AF_INET6) |
613 |
|
|
if (match_ipv6(&who->localClient->ip, &bp->addr, bp->bits)) |
614 |
|
|
return 1; |
615 |
|
|
break; |
616 |
|
|
#endif |
617 |
|
|
default: |
618 |
|
|
assert(0); |
619 |
|
|
} |
620 |
|
|
} |
621 |
adx |
30 |
} |
622 |
|
|
|
623 |
|
|
return 0; |
624 |
|
|
} |
625 |
|
|
|
626 |
|
|
/*! |
627 |
|
|
* \param chptr pointer to channel block |
628 |
|
|
* \param who pointer to client to check access fo |
629 |
|
|
* \return 0 if not banned, 1 otherwise |
630 |
|
|
*/ |
631 |
|
|
int |
632 |
michael |
1013 |
is_banned(const struct Channel *chptr, const struct Client *who) |
633 |
adx |
30 |
{ |
634 |
michael |
632 |
if (find_bmask(who, &chptr->banlist)) |
635 |
michael |
1495 |
if (!find_bmask(who, &chptr->exceptlist)) |
636 |
michael |
632 |
return 1; |
637 |
adx |
30 |
|
638 |
michael |
632 |
return 0; |
639 |
adx |
30 |
} |
640 |
|
|
|
641 |
|
|
/*! |
642 |
|
|
* \param source_p pointer to client attempting to join |
643 |
michael |
2345 |
* \param chptr pointer to channel |
644 |
adx |
30 |
* \param key key sent by client attempting to join if present |
645 |
|
|
* \return ERR_BANNEDFROMCHAN, ERR_INVITEONLYCHAN, ERR_CHANNELISFULL |
646 |
|
|
* or 0 if allowed to join. |
647 |
|
|
*/ |
648 |
michael |
1834 |
int |
649 |
adx |
30 |
can_join(struct Client *source_p, struct Channel *chptr, const char *key) |
650 |
|
|
{ |
651 |
michael |
2246 |
if ((chptr->mode.mode & MODE_SSLONLY) && !HasUMode(source_p, UMODE_SSL)) |
652 |
michael |
1150 |
return ERR_SSLONLYCHAN; |
653 |
|
|
|
654 |
michael |
1173 |
if ((chptr->mode.mode & MODE_REGONLY) && !HasUMode(source_p, UMODE_REGISTERED)) |
655 |
|
|
return ERR_NEEDREGGEDNICK; |
656 |
|
|
|
657 |
michael |
1219 |
if ((chptr->mode.mode & MODE_OPERONLY) && !HasUMode(source_p, UMODE_OPER)) |
658 |
michael |
1150 |
return ERR_OPERONLYCHAN; |
659 |
|
|
|
660 |
adx |
30 |
if (chptr->mode.mode & MODE_INVITEONLY) |
661 |
michael |
317 |
if (!dlinkFind(&source_p->localClient->invited, chptr)) |
662 |
michael |
1495 |
if (!find_bmask(source_p, &chptr->invexlist)) |
663 |
adx |
30 |
return ERR_INVITEONLYCHAN; |
664 |
|
|
|
665 |
michael |
1430 |
if (chptr->mode.key[0] && (!key || strcmp(chptr->mode.key, key))) |
666 |
adx |
30 |
return ERR_BADCHANNELKEY; |
667 |
|
|
|
668 |
|
|
if (chptr->mode.limit && dlink_list_length(&chptr->members) >= |
669 |
|
|
chptr->mode.limit) |
670 |
|
|
return ERR_CHANNELISFULL; |
671 |
|
|
|
672 |
michael |
2208 |
if (is_banned(chptr, source_p)) |
673 |
|
|
return ERR_BANNEDFROMCHAN; |
674 |
|
|
|
675 |
michael |
1834 |
return 0; |
676 |
adx |
30 |
} |
677 |
|
|
|
678 |
|
|
int |
679 |
michael |
1847 |
has_member_flags(const struct Membership *ms, const unsigned int flags) |
680 |
adx |
30 |
{ |
681 |
|
|
if (ms != NULL) |
682 |
|
|
return ms->flags & flags; |
683 |
|
|
return 0; |
684 |
|
|
} |
685 |
|
|
|
686 |
|
|
struct Membership * |
687 |
|
|
find_channel_link(struct Client *client_p, struct Channel *chptr) |
688 |
|
|
{ |
689 |
|
|
dlink_node *ptr = NULL; |
690 |
|
|
|
691 |
|
|
if (!IsClient(client_p)) |
692 |
|
|
return NULL; |
693 |
|
|
|
694 |
michael |
2567 |
if (dlink_list_length(&chptr->members) < dlink_list_length(&client_p->channel)) |
695 |
|
|
{ |
696 |
|
|
DLINK_FOREACH(ptr, chptr->members.head) |
697 |
|
|
if (((struct Membership *)ptr->data)->client_p == client_p) |
698 |
|
|
return ptr->data; |
699 |
|
|
} |
700 |
|
|
else |
701 |
|
|
{ |
702 |
|
|
DLINK_FOREACH(ptr, client_p->channel.head) |
703 |
|
|
if (((struct Membership *)ptr->data)->chptr == chptr) |
704 |
|
|
return ptr->data; |
705 |
|
|
} |
706 |
adx |
30 |
|
707 |
|
|
return NULL; |
708 |
|
|
} |
709 |
|
|
|
710 |
michael |
1937 |
/* |
711 |
|
|
* Basically the same functionality as in bahamut |
712 |
|
|
*/ |
713 |
|
|
static int |
714 |
|
|
msg_has_ctrls(const char *message) |
715 |
|
|
{ |
716 |
|
|
const unsigned char *p = (const unsigned char *)message; |
717 |
|
|
|
718 |
|
|
for (; *p; ++p) |
719 |
|
|
{ |
720 |
|
|
if (*p > 31 || *p == 1) |
721 |
|
|
continue; |
722 |
|
|
|
723 |
|
|
if (*p == 27) |
724 |
|
|
{ |
725 |
|
|
if (*(p + 1) == '$' || |
726 |
|
|
*(p + 1) == '(') |
727 |
|
|
{ |
728 |
|
|
++p; |
729 |
|
|
continue; |
730 |
|
|
} |
731 |
|
|
} |
732 |
|
|
|
733 |
|
|
return 1; |
734 |
|
|
} |
735 |
|
|
|
736 |
|
|
return 0; |
737 |
|
|
} |
738 |
|
|
|
739 |
adx |
30 |
/*! |
740 |
|
|
* \param chptr pointer to Channel struct |
741 |
|
|
* \param source_p pointer to Client struct |
742 |
michael |
454 |
* \param ms pointer to Membership struct (can be NULL) |
743 |
adx |
30 |
* \return CAN_SEND_OPV if op or voiced on channel\n |
744 |
|
|
* CAN_SEND_NONOP if can send to channel but is not an op\n |
745 |
michael |
1173 |
* ERR_CANNOTSENDTOCHAN or ERR_NEEDREGGEDNICK if they cannot send to channel\n |
746 |
adx |
30 |
*/ |
747 |
|
|
int |
748 |
michael |
1937 |
can_send(struct Channel *chptr, struct Client *source_p, |
749 |
|
|
struct Membership *ms, const char *message) |
750 |
adx |
30 |
{ |
751 |
michael |
1858 |
struct MaskItem *conf = NULL; |
752 |
|
|
|
753 |
michael |
1219 |
if (IsServer(source_p) || HasFlag(source_p, FLAGS_SERVICE)) |
754 |
adx |
30 |
return CAN_SEND_OPV; |
755 |
|
|
|
756 |
michael |
565 |
if (MyClient(source_p) && !IsExemptResv(source_p)) |
757 |
michael |
1219 |
if (!(HasUMode(source_p, UMODE_OPER) && ConfigFileEntry.oper_pass_resv)) |
758 |
michael |
1858 |
if ((conf = match_find_resv(chptr->chname)) && !resv_find_exempt(source_p, conf)) |
759 |
michael |
1834 |
return ERR_CANNOTSENDTOCHAN; |
760 |
adx |
30 |
|
761 |
michael |
1944 |
if ((chptr->mode.mode & MODE_NOCTRL) && msg_has_ctrls(message)) |
762 |
|
|
return ERR_NOCTRLSONCHAN; |
763 |
|
|
if (ms || (ms = find_channel_link(source_p, chptr))) |
764 |
adx |
30 |
if (ms->flags & (CHFL_CHANOP|CHFL_HALFOP|CHFL_VOICE)) |
765 |
|
|
return CAN_SEND_OPV; |
766 |
michael |
2441 |
if (!ms && (chptr->mode.mode & MODE_NOPRIVMSGS)) |
767 |
|
|
return ERR_CANNOTSENDTOCHAN; |
768 |
michael |
1944 |
if (chptr->mode.mode & MODE_MODERATED) |
769 |
|
|
return ERR_CANNOTSENDTOCHAN; |
770 |
michael |
1954 |
if ((chptr->mode.mode & MODE_MODREG) && !HasUMode(source_p, UMODE_REGISTERED)) |
771 |
michael |
1944 |
return ERR_NEEDREGGEDNICK; |
772 |
adx |
30 |
|
773 |
michael |
1951 |
/* cache can send if banned */ |
774 |
michael |
1944 |
if (MyClient(source_p)) |
775 |
|
|
{ |
776 |
|
|
if (ms) |
777 |
adx |
30 |
{ |
778 |
|
|
if (ms->flags & CHFL_BAN_SILENCED) |
779 |
michael |
1834 |
return ERR_CANNOTSENDTOCHAN; |
780 |
adx |
30 |
|
781 |
|
|
if (!(ms->flags & CHFL_BAN_CHECKED)) |
782 |
|
|
{ |
783 |
|
|
if (is_banned(chptr, source_p)) |
784 |
|
|
{ |
785 |
|
|
ms->flags |= (CHFL_BAN_CHECKED|CHFL_BAN_SILENCED); |
786 |
michael |
1834 |
return ERR_CANNOTSENDTOCHAN; |
787 |
adx |
30 |
} |
788 |
|
|
|
789 |
|
|
ms->flags |= CHFL_BAN_CHECKED; |
790 |
|
|
} |
791 |
|
|
} |
792 |
michael |
1944 |
else if (is_banned(chptr, source_p)) |
793 |
michael |
1941 |
return ERR_CANNOTSENDTOCHAN; |
794 |
|
|
} |
795 |
adx |
30 |
|
796 |
|
|
return CAN_SEND_NONOP; |
797 |
|
|
} |
798 |
|
|
|
799 |
|
|
/*! \brief Updates the client's oper_warn_count_down, warns the |
800 |
|
|
* IRC operators if necessary, and updates |
801 |
|
|
* join_leave_countdown as needed. |
802 |
|
|
* \param source_p pointer to struct Client to check |
803 |
|
|
* \param name channel name or NULL if this is a part. |
804 |
|
|
*/ |
805 |
|
|
void |
806 |
|
|
check_spambot_warning(struct Client *source_p, const char *name) |
807 |
|
|
{ |
808 |
|
|
int t_delta = 0; |
809 |
|
|
int decrement_count = 0; |
810 |
|
|
|
811 |
|
|
if ((GlobalSetOptions.spam_num && |
812 |
|
|
(source_p->localClient->join_leave_count >= |
813 |
|
|
GlobalSetOptions.spam_num))) |
814 |
|
|
{ |
815 |
|
|
if (source_p->localClient->oper_warn_count_down > 0) |
816 |
|
|
source_p->localClient->oper_warn_count_down--; |
817 |
|
|
else |
818 |
|
|
source_p->localClient->oper_warn_count_down = 0; |
819 |
|
|
|
820 |
|
|
if (source_p->localClient->oper_warn_count_down == 0) |
821 |
|
|
{ |
822 |
|
|
/* Its already known as a possible spambot */ |
823 |
|
|
if (name != NULL) |
824 |
michael |
1618 |
sendto_realops_flags(UMODE_BOTS, L_ALL, SEND_NOTICE, |
825 |
adx |
30 |
"User %s (%s@%s) trying to join %s is a possible spambot", |
826 |
|
|
source_p->name, source_p->username, |
827 |
|
|
source_p->host, name); |
828 |
|
|
else |
829 |
michael |
1618 |
sendto_realops_flags(UMODE_BOTS, L_ALL, SEND_NOTICE, |
830 |
adx |
30 |
"User %s (%s@%s) is a possible spambot", |
831 |
|
|
source_p->name, source_p->username, |
832 |
|
|
source_p->host); |
833 |
|
|
source_p->localClient->oper_warn_count_down = OPER_SPAM_COUNTDOWN; |
834 |
|
|
} |
835 |
|
|
} |
836 |
|
|
else |
837 |
|
|
{ |
838 |
|
|
if ((t_delta = (CurrentTime - source_p->localClient->last_leave_time)) > |
839 |
|
|
JOIN_LEAVE_COUNT_EXPIRE_TIME) |
840 |
|
|
{ |
841 |
|
|
decrement_count = (t_delta / JOIN_LEAVE_COUNT_EXPIRE_TIME); |
842 |
|
|
if (decrement_count > source_p->localClient->join_leave_count) |
843 |
|
|
source_p->localClient->join_leave_count = 0; |
844 |
|
|
else |
845 |
|
|
source_p->localClient->join_leave_count -= decrement_count; |
846 |
|
|
} |
847 |
|
|
else |
848 |
|
|
{ |
849 |
|
|
if ((CurrentTime - (source_p->localClient->last_join_time)) < |
850 |
|
|
GlobalSetOptions.spam_time) |
851 |
|
|
{ |
852 |
|
|
/* oh, its a possible spambot */ |
853 |
|
|
source_p->localClient->join_leave_count++; |
854 |
|
|
} |
855 |
|
|
} |
856 |
|
|
|
857 |
|
|
if (name != NULL) |
858 |
|
|
source_p->localClient->last_join_time = CurrentTime; |
859 |
|
|
else |
860 |
|
|
source_p->localClient->last_leave_time = CurrentTime; |
861 |
|
|
} |
862 |
|
|
} |
863 |
|
|
|
864 |
|
|
/*! \brief compares usercount and servercount against their split |
865 |
|
|
* values and adjusts splitmode accordingly |
866 |
|
|
* \param unused Unused address pointer |
867 |
|
|
*/ |
868 |
|
|
void |
869 |
|
|
check_splitmode(void *unused) |
870 |
|
|
{ |
871 |
|
|
if (splitchecking && (ConfigChannel.no_join_on_split || |
872 |
|
|
ConfigChannel.no_create_on_split)) |
873 |
|
|
{ |
874 |
|
|
const unsigned int server = dlink_list_length(&global_serv_list); |
875 |
|
|
|
876 |
|
|
if (!splitmode && ((server < split_servers) || (Count.total < split_users))) |
877 |
|
|
{ |
878 |
|
|
splitmode = 1; |
879 |
|
|
|
880 |
michael |
1618 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
881 |
adx |
30 |
"Network split, activating splitmode"); |
882 |
|
|
eventAddIsh("check_splitmode", check_splitmode, NULL, 10); |
883 |
|
|
} |
884 |
|
|
else if (splitmode && (server > split_servers) && (Count.total > split_users)) |
885 |
|
|
{ |
886 |
|
|
splitmode = 0; |
887 |
|
|
|
888 |
michael |
1618 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
889 |
adx |
30 |
"Network rejoined, deactivating splitmode"); |
890 |
|
|
eventDelete(check_splitmode, NULL); |
891 |
|
|
} |
892 |
|
|
} |
893 |
|
|
} |
894 |
|
|
|
895 |
|
|
/*! \brief Sets the channel topic for chptr |
896 |
|
|
* \param chptr Pointer to struct Channel |
897 |
|
|
* \param topic The topic string |
898 |
|
|
* \param topic_info n!u\@h formatted string of the topic setter |
899 |
|
|
* \param topicts timestamp on the topic |
900 |
|
|
*/ |
901 |
|
|
void |
902 |
|
|
set_channel_topic(struct Channel *chptr, const char *topic, |
903 |
michael |
1751 |
const char *topic_info, time_t topicts, int local) |
904 |
adx |
30 |
{ |
905 |
michael |
1751 |
if (local) |
906 |
|
|
strlcpy(chptr->topic, topic, IRCD_MIN(sizeof(chptr->topic), ServerInfo.max_topic_length + 1)); |
907 |
|
|
else |
908 |
|
|
strlcpy(chptr->topic, topic, sizeof(chptr->topic)); |
909 |
|
|
|
910 |
michael |
1203 |
strlcpy(chptr->topic_info, topic_info, sizeof(chptr->topic_info)); |
911 |
michael |
2345 |
chptr->topic_time = topicts; |
912 |
adx |
30 |
} |