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