2 * IRC - Internet Relay Chat, ircd/s_user.c (formerly ircd/s_msg.c)
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
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)
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.
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.
24 * @brief Miscellaneous user-related helper functions.
36 #include "ircd_alloc.h"
37 #include "ircd_auth.h"
38 #include "ircd_chattr.h"
39 #include "ircd_features.h"
41 #include "ircd_reply.h"
42 #include "ircd_snprintf.h"
43 #include "ircd_string.h"
52 #include "querycmds.h"
58 #include "s_serv.h" /* max_client_count */
62 #include "supported.h"
68 #include "handlers.h" /* m_motd and m_lusers */
77 /** Count of allocated User structures. */
78 static int userCount = 0;
80 /** Makes sure that \a cptr has a User information block.
81 * If cli_user(cptr) != NULL, does nothing.
82 * @param[in] cptr Client to attach User struct to.
83 * @return User struct associated with \a cptr.
85 struct User *make_user(struct Client *cptr)
89 if (!cli_user(cptr)) {
90 cli_user(cptr) = (struct User*) MyMalloc(sizeof(struct User));
91 assert(0 != cli_user(cptr));
93 /* All variables are 0 by default */
94 memset(cli_user(cptr), 0, sizeof(struct User));
98 cli_user(cptr)->refcnt = 1;
100 return cli_user(cptr);
103 /** Dereference \a user.
104 * User structures are reference-counted; if the refcount of \a user
105 * becomes zero, free it.
106 * @param[in] user User to dereference.
108 void free_user(struct User* user)
111 assert(0 < user->refcnt);
113 if (--user->refcnt == 0) {
119 assert(0 == user->joined);
120 assert(0 == user->invited);
121 assert(0 == user->channel);
130 /** Find number of User structs allocated and memory used by them.
131 * @param[out] count_out Receives number of User structs allocated.
132 * @param[out] bytes_out Receives number of bytes used by User structs.
134 void user_count_memory(size_t* count_out, size_t* bytes_out)
136 assert(0 != count_out);
137 assert(0 != bytes_out);
138 *count_out = userCount;
139 *bytes_out = userCount * sizeof(struct User);
143 /** Find the next client (starting at \a next) with a name that matches \a ch.
144 * Normal usage loop is:
145 * for (x = client; x = next_client(x,mask); x = x->next)
146 * HandleMatchingClient;
148 * @param[in] next First client to check.
149 * @param[in] ch Name mask to check against.
150 * @return Next matching client found, or NULL if none.
152 struct Client *next_client(struct Client *next, const char* ch)
154 struct Client *tmp = next;
159 next = FindClient(ch);
160 next = next ? next : tmp;
161 if (cli_prev(tmp) == next)
165 for (; next; next = cli_next(next))
166 if (!match(ch, cli_name(next)))
171 /** Find the destination server for a command, and forward it if that is not us.
173 * \a server may be a nickname, server name, server mask (if \a from
174 * is a local user) or server numnick (if \a is a server or remote
177 * @param[in] from Client that sent the command to us.
178 * @param[in] cmd Long-form command text.
179 * @param[in] tok Token-form command text.
180 * @param[in] one Client that originated the command (ignored).
181 * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH.
182 * @param[in] pattern Format string of arguments to command.
183 * @param[in] server Index of target name or mask in \a parv.
184 * @param[in] parc Number of valid elements in \a parv (must be less than 9).
185 * @param[in] parv Array of arguments to command.
186 * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS.
188 int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok,
189 struct Client *one, int MustBeOper, const char *pattern,
190 int server, int parc, char *parv[])
192 struct Client *acptr;
195 /* Assume it's me, if no server or an unregistered client */
196 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
197 return (HUNTED_ISME);
199 if (MustBeOper && !IsPrivileged(from))
201 send_reply(from, ERR_NOPRIVILEGES);
202 return HUNTED_NOSUCH;
205 /* Make sure it's a server */
207 /* Make sure it's a server */
208 if (!strchr(to, '*')) {
209 if (0 == (acptr = FindClient(to))) {
210 send_reply(from, ERR_NOSUCHSERVER, to);
211 return HUNTED_NOSUCH;
215 acptr = cli_user(acptr)->server;
216 } else if (!(acptr = find_match_server(to))) {
217 send_reply(from, ERR_NOSUCHSERVER, to);
218 return (HUNTED_NOSUCH);
220 } else if (!(acptr = FindNServer(to))) {
221 send_reply(from, SND_EXPLICIT | ERR_NOSUCHSERVER, "* :Server has disconnected");
222 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
226 return (HUNTED_ISME);
228 if (MustBeOper && !IsPrivileged(from)) {
229 send_reply(from, ERR_NOPRIVILEGES);
230 return HUNTED_NOSUCH;
233 /* assert(!IsServer(from)); */
235 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
237 sendcmdto_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
238 parv[4], parv[5], parv[6], parv[7], parv[8]);
240 return (HUNTED_PASS);
243 /** Find the destination server for a command, and forward it (as a
244 * high-priority command) if that is not us.
246 * \a server may be a nickname, server name, server mask (if \a from
247 * is a local user) or server numnick (if \a is a server or remote
249 * Unlike hunt_server_cmd(), this appends the message to the
250 * high-priority message queue for the destination server.
252 * @param[in] from Client that sent the command to us.
253 * @param[in] cmd Long-form command text.
254 * @param[in] tok Token-form command text.
255 * @param[in] one Client that originated the command (ignored).
256 * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH.
257 * @param[in] pattern Format string of arguments to command.
258 * @param[in] server Index of target name or mask in \a parv.
259 * @param[in] parc Number of valid elements in \a parv (must be less than 9).
260 * @param[in] parv Array of arguments to command.
261 * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS.
263 int hunt_server_prio_cmd(struct Client *from, const char *cmd, const char *tok,
264 struct Client *one, int MustBeOper,
265 const char *pattern, int server, int parc,
268 struct Client *acptr;
271 /* Assume it's me, if no server or an unregistered client */
272 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
273 return (HUNTED_ISME);
275 /* Make sure it's a server */
277 /* Make sure it's a server */
278 if (!strchr(to, '*')) {
279 if (0 == (acptr = FindClient(to))) {
280 send_reply(from, ERR_NOSUCHSERVER, to);
281 return HUNTED_NOSUCH;
285 acptr = cli_user(acptr)->server;
286 } else if (!(acptr = find_match_server(to))) {
287 send_reply(from, ERR_NOSUCHSERVER, to);
288 return (HUNTED_NOSUCH);
290 } else if (!(acptr = FindNServer(to)))
291 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
294 return (HUNTED_ISME);
296 if (MustBeOper && !IsPrivileged(from)) {
297 send_reply(from, ERR_NOPRIVILEGES);
298 return HUNTED_NOSUCH;
301 /* assert(!IsServer(from)); SETTIME to particular destinations permitted */
303 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
305 sendcmdto_prio_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
306 parv[4], parv[5], parv[6], parv[7], parv[8]);
308 return (HUNTED_PASS);
312 /** Copy a cleaned-up version of a username.
313 * Replace all instances of '~' and "invalid" username characters
314 * (!isIrcUi()) with underscores, truncating at USERLEN or the first
315 * control character. \a dest and \a source may be the same buffer.
316 * @param[out] dest Destination buffer.
317 * @param[in] source Source buffer.
318 * @param[in] tilde If non-zero, prepend a '~' to \a dest.
320 static char *clean_user_id(char *dest, char *source, int tilde)
327 ch = *s++; /* Store first character to copy: */
330 *d++ = '~'; /* If `dest' == `source', then this overwrites `ch' */
333 while (ch && !IsCntrl(ch) && rlen--)
335 char nch = *s++; /* Store next character to copy */
336 *d++ = IsUserChar(ch) ? ch : '_'; /* This possibly overwrites it */
349 * This function is called when both NICK and USER messages
350 * have been accepted for the client, in whatever order. Only
351 * after this the USER message is propagated.
353 * NICK's must be propagated at once when received, although
354 * it would be better to delay them too until full info is
355 * available. Doing it is not so simple though, would have
356 * to implement the following:
358 * 1) user telnets in and gives only "NICK foobar" and waits
359 * 2) another user far away logs in normally with the nick
360 * "foobar" (quite legal, as this server didn't propagate it).
361 * 3) now this server gets nick "foobar" from outside, but
362 * has already the same defined locally. Current server
363 * would just issue "KILL foobar" to clean out dups. But,
364 * this is not fair. It should actually request another
365 * nick from local user or kill him/her...
367 /** Finish registering a user who has sent both NICK and USER.
368 * For local connections, possibly check IAuth; make sure there is a
369 * matching Client config block; clean the username field; check
370 * K/k-lines; check for "hacked" looking usernames; assign a numnick;
371 * and send greeting (WELCOME, ISUPPORT, MOTD, etc).
372 * For all connections, update the invisible user and operator counts;
373 * run IPcheck against their address; and forward the NICK.
375 * @param[in] cptr Client who introduced the user.
376 * @param[in,out] sptr Client who has been fully introduced.
377 * @param[in] nick Client's new nickname.
378 * @param[in] username Client's username.
379 * @return Zero or CPTR_KILLED.
381 int register_user(struct Client *cptr, struct Client *sptr,
382 const char *nick, char *username)
384 struct ConfItem* aconf;
388 char c = 0; /* not alphanum */
389 char d = 'a'; /* not a digit */
397 short digitgroups = 0;
398 struct User* user = cli_user(sptr);
402 user->last = CurrentTime;
403 parv[0] = cli_name(sptr);
404 parv[1] = parv[2] = NULL;
408 static time_t last_too_many1;
409 static time_t last_too_many2;
411 assert(cptr == sptr);
412 if (!IsIAuthed(sptr)) {
414 return iauth_start_client(iauth_active, sptr);
418 switch (conf_check_client(sptr))
422 case ACR_NO_AUTHORIZATION:
423 sendto_opmask_butone(0, SNO_UNAUTH, "Unauthorized connection from %s.",
424 get_client_name(sptr, HIDE_IP));
425 ++ServerStats->is_ref;
426 return exit_client(cptr, sptr, &me,
427 "No Authorization - use another server");
428 case ACR_TOO_MANY_IN_CLASS:
429 if (CurrentTime - last_too_many1 >= (time_t) 60)
431 last_too_many1 = CurrentTime;
432 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections in "
433 "class %i for %s.", get_client_class(sptr),
434 get_client_name(sptr, SHOW_IP));
436 ++ServerStats->is_ref;
437 IPcheck_connect_fail(&cli_ip(sptr));
438 return exit_client(cptr, sptr, &me,
439 "Sorry, your connection class is full - try "
440 "again later or try another server");
441 case ACR_TOO_MANY_FROM_IP:
442 if (CurrentTime - last_too_many2 >= (time_t) 60)
444 last_too_many2 = CurrentTime;
445 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections from "
447 get_client_name(sptr, SHOW_IP));
449 ++ServerStats->is_ref;
450 return exit_client(cptr, sptr, &me,
451 "Too many connections from your host");
452 case ACR_ALREADY_AUTHORIZED:
453 /* Can this ever happen? */
455 ++ServerStats->is_ref;
456 IPcheck_connect_fail(&cli_ip(sptr));
457 return exit_client(cptr, sptr, &me, "Unknown error -- Try again");
459 ircd_strncpy(user->host, cli_sockhost(sptr), HOSTLEN);
460 ircd_strncpy(user->realhost, cli_sockhost(sptr), HOSTLEN);
461 aconf = cli_confs(sptr)->value.aconf;
463 clean_user_id(user->username,
464 HasFlag(sptr, FLAG_GOTID) ? cli_username(sptr) : username,
465 HasFlag(sptr, FLAG_DOID) && !HasFlag(sptr, FLAG_GOTID));
467 if ((user->username[0] == '\0')
468 || ((user->username[0] == '~') && (user->username[1] == '\000')))
469 return exit_client(cptr, sptr, &me, "USER: Bogus userid.");
471 if (!EmptyString(aconf->passwd)
472 && !(IsDigit(*aconf->passwd) && !aconf->passwd[1])
473 && strcmp(cli_passwd(sptr), aconf->passwd))
475 ServerStats->is_ref++;
476 send_reply(sptr, ERR_PASSWDMISMATCH);
477 return exit_client(cptr, sptr, &me, "Bad Password");
479 memset(cli_passwd(sptr), 0, sizeof(cli_passwd(sptr)));
481 * following block for the benefit of time-dependent K:-lines
483 killreason = find_kill(sptr);
485 ServerStats->is_ref++;
486 return exit_client(cptr, sptr, &me,
487 (killreason == -1 ? "K-lined" : "G-lined"));
490 * Check for mixed case usernames, meaning probably hacked. Jon2 3-94
491 * Summary of rules now implemented in this patch: Ensor 11-94
492 * In a mixed-case name, if first char is upper, one more upper may
493 * appear anywhere. (A mixed-case name *must* have an upper first
494 * char, and may have one other upper.)
495 * A third upper may appear if all 3 appear at the beginning of the
496 * name, separated only by "others" (-/_/.).
497 * A single group of digits is allowed anywhere.
498 * Two groups of digits are allowed if at least one of the groups is
499 * at the beginning or the end.
500 * Only one '-', '_', or '.' is allowed (or two, if not consecutive).
501 * But not as the first or last char.
502 * No other special characters are allowed.
503 * Name must contain at least one letter.
505 tmpstr2 = tmpstr = (username[0] == '~' ? &username[1] : username);
506 while (*tmpstr && !badid)
518 if ((leadcaps || pos == 1) && !lower && !digits)
524 if (pos == 1 || !IsDigit(d))
531 else if (c == '-' || c == '_' || c == '.')
536 else if (d == '-' || d == '_' || d == '.' || other > 2)
545 if (lower && upper && (!leadcaps || leadcaps > 3 ||
546 (upper > 2 && upper > leadcaps)))
548 else if (digitgroups == 2 && !(IsDigit(tmpstr2[0]) || IsDigit(c)))
550 else if ((!lower && !upper) || !IsAlnum(c))
553 if (badid && (!HasFlag(sptr, FLAG_GOTID) ||
554 strcmp(cli_username(sptr), username) != 0))
556 ServerStats->is_ref++;
558 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
559 ":Your username is invalid.");
560 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
561 ":Connect with your real username, in lowercase.");
562 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
563 ":If your mail address were foo@bar.com, your username "
565 return exit_client(cptr, sptr, &me, "USER: Bad username");
567 Count_unknownbecomesclient(sptr, UserStats);
570 ircd_strncpy(user->username, username, USERLEN);
571 Count_newremoteclient(UserStats, user->server);
575 if (IsInvisible(sptr))
576 ++UserStats.inv_clients;
580 if (MyConnect(sptr)) {
581 cli_handler(sptr) = CLIENT_HANDLER;
582 release_dns_reply(sptr);
583 SetLocalNumNick(sptr);
586 feature_str(FEAT_NETWORK),
587 feature_str(FEAT_PROVIDER) ? " via " : "",
588 feature_str(FEAT_PROVIDER) ? feature_str(FEAT_PROVIDER) : "",
591 * This is a duplicate of the NOTICE but see below...
593 send_reply(sptr, RPL_YOURHOST, cli_name(&me), version);
594 send_reply(sptr, RPL_CREATED, creation);
595 send_reply(sptr, RPL_MYINFO, cli_name(&me), infousermodes, infochanmodes,
596 infochanmodeswithparams, version);
597 send_supported(sptr);
598 m_lusers(sptr, sptr, 1, parv);
601 /* nextping = CurrentTime; */
602 if (cli_snomask(sptr) & SNO_NOISY)
603 set_snomask(sptr, cli_snomask(sptr) & SNO_NOISY, SNO_ADD);
604 if (feature_bool(FEAT_CONNEXIT_NOTICES))
605 sendto_opmask_butone(0, SNO_CONNEXIT,
606 "Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s%s>",
607 cli_name(sptr), user->username, user->host,
608 cli_sock_ip(sptr), get_client_class(sptr),
609 cli_info(sptr), NumNick(cptr) /* two %s's */);
611 IPcheck_connect_succeeded(sptr);
613 * Set user's initial modes
615 parv[0] = (char*)nick;
616 parv[1] = (char*)nick;
617 parv[2] = (char*)client_get_default_umode(sptr);
618 parv[3] = NULL; /* needed in case of +s */
619 set_user_mode(sptr, sptr, 3, parv);
620 ClearHiddenHost(sptr); /* just in case somebody stuck +x in there */
623 /* if (IsServer(cptr)) */
625 struct Client *acptr;
627 acptr = user->server;
628 if (cli_from(acptr) != cli_from(sptr))
630 sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])",
631 sptr, cli_name(&me), cli_name(user->server), cli_name(cli_from(acptr)),
632 cli_sockhost(cli_from(acptr)));
633 SetFlag(sptr, FLAG_KILLED);
634 return exit_client(cptr, sptr, &me, "NICK server wrong direction");
636 else if (HasFlag(acptr, FLAG_TS8))
637 SetFlag(sptr, FLAG_TS8);
640 * Check to see if this user is being propogated
641 * as part of a net.burst, or is using protocol 9.
642 * FIXME: This can be speeded up - its stupid to check it for
643 * every NICK message in a burst again --Run.
645 for (acptr = user->server; acptr != &me; acptr = cli_serv(acptr)->up)
647 if (IsBurst(acptr) || Protocol(acptr) < 10)
650 if (!IPcheck_remote_connect(sptr, (acptr != &me)))
653 * We ran out of bits to count this
655 sendcmdto_one(&me, CMD_KILL, sptr, "%C :%s (Too many connections from your host -- Ghost)",
656 sptr, cli_name(&me));
657 return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled");
660 tmpstr = umode_str(sptr);
661 sendcmdto_serv_butone(user->server, CMD_NICK, cptr,
662 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
663 nick, cli_hopcount(sptr) + 1, cli_lastnick(sptr),
664 user->username, user->realhost,
665 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
666 iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64)),
667 NumNick(sptr), cli_info(sptr));
669 /* Send server notice mask to client */
670 if (MyUser(sptr) && (cli_snomask(sptr) != SNO_DEFAULT) && HasFlag(sptr, FLAG_SERVNOTICE))
671 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
676 /** List of user mode characters. */
677 static const struct UserMode {
678 unsigned int flag; /**< User mode constant. */
679 char c; /**< Character corresponding to the mode. */
683 { FLAG_INVISIBLE, 'i' },
684 { FLAG_WALLOP, 'w' },
685 { FLAG_SERVNOTICE, 's' },
687 { FLAG_CHSERV, 'k' },
689 { FLAG_ACCOUNT, 'r' },
690 { FLAG_HIDDENHOST, 'x' }
693 /** Length of #userModeList. */
694 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
697 * XXX - find a way to get rid of this
699 /** Nasty global buffer used for communications with umode_str() and others. */
700 static char umodeBuf[BUFSIZE];
702 /** Try to set a user's nickname.
703 * If \a sptr is a server, the client is being introduced for the first time.
704 * @param[in] cptr Client to set nickname.
705 * @param[in] sptr Client sending the NICK.
706 * @param[in] nick New nickname.
707 * @param[in] parc Number of arguments to NICK.
708 * @param[in] parv Argument list to NICK.
709 * @return CPTR_KILLED if \a cptr was killed, else 0.
711 int set_nick_name(struct Client* cptr, struct Client* sptr,
712 const char* nick, int parc, char* parv[])
714 if (IsServer(sptr)) {
716 const char* account = 0;
720 * A server introducing a new client, change source
722 struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
723 assert(0 != new_client);
725 cli_hopcount(new_client) = atoi(parv[2]);
726 cli_lastnick(new_client) = atoi(parv[3]);
727 if (Protocol(cptr) > 9 && parc > 7 && *parv[6] == '+')
729 for (p = parv[6] + 1; *p; p++)
731 for (i = 0; i < USERMODELIST_SIZE; ++i)
733 if (userModeList[i].c == *p)
735 SetFlag(new_client, userModeList[i].flag);
736 if (userModeList[i].flag == FLAG_ACCOUNT)
743 client_set_privs(new_client, NULL); /* set privs on user */
747 strcpy(cli_name(new_client), nick);
748 cli_user(new_client) = make_user(new_client);
749 cli_user(new_client)->server = sptr;
750 SetRemoteNumNick(new_client, parv[parc - 2]);
752 * IP# of remote client
754 base64toip(parv[parc - 3], &cli_ip(new_client));
756 add_client_to_list(new_client);
757 hAddClient(new_client);
759 cli_serv(sptr)->ghost = 0; /* :server NICK means end of net.burst */
760 ircd_strncpy(cli_username(new_client), parv[4], USERLEN);
761 ircd_strncpy(cli_user(new_client)->host, parv[5], HOSTLEN);
762 ircd_strncpy(cli_user(new_client)->realhost, parv[5], HOSTLEN);
763 ircd_strncpy(cli_info(new_client), parv[parc - 1], REALLEN);
765 int len = ACCOUNTLEN;
766 if ((p = strchr(account, ':'))) {
767 len = (p++) - account;
768 cli_user(new_client)->acc_create = atoi(p);
769 Debug((DEBUG_DEBUG, "Received timestamped account in user mode; "
770 "account \"%s\", timestamp %Tu", account,
771 cli_user(new_client)->acc_create));
773 ircd_strncpy(cli_user(new_client)->account, account, len);
775 if (HasHiddenHost(new_client))
776 ircd_snprintf(0, cli_user(new_client)->host, HOSTLEN, "%s.%s",
777 account, feature_str(FEAT_HIDDEN_HOST));
779 return register_user(cptr, new_client, cli_name(new_client), parv[4]);
781 else if ((cli_name(sptr))[0]) {
783 * Client changing its nick
785 * If the client belongs to me, then check to see
786 * if client is on any channels where it is currently
787 * banned. If so, do not allow the nick change to occur.
790 const char* channel_name;
791 struct Membership *member;
792 if ((channel_name = find_no_nickchange_channel(sptr))) {
793 return send_reply(cptr, ERR_BANNICKCHANGE, channel_name);
796 * Refuse nick change if the last nick change was less
797 * then 30 seconds ago. This is intended to get rid of
798 * clone bots doing NICK FLOOD. -SeKs
799 * If someone didn't change their nick for more then 60 seconds
800 * however, allow to do two nick changes immediately after another
801 * before limiting the nick flood. -Run
803 if (CurrentTime < cli_nextnick(cptr))
805 cli_nextnick(cptr) += 2;
806 send_reply(cptr, ERR_NICKTOOFAST, parv[1],
807 cli_nextnick(cptr) - CurrentTime);
808 /* Send error message */
809 sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cli_name(cptr));
810 /* bounce NICK to user */
811 return 0; /* ignore nick change! */
814 /* Limit total to 1 change per NICK_DELAY seconds: */
815 cli_nextnick(cptr) += NICK_DELAY;
816 /* However allow _maximal_ 1 extra consecutive nick change: */
817 if (cli_nextnick(cptr) < CurrentTime)
818 cli_nextnick(cptr) = CurrentTime;
820 /* Invalidate all bans against the user so we check them again */
821 for (member = (cli_user(cptr))->channel; member;
822 member = member->next_channel)
823 ClearBanValid(member);
826 * Also set 'lastnick' to current time, if changed.
828 if (0 != ircd_strcmp(parv[0], nick))
829 cli_lastnick(sptr) = (sptr == cptr) ? TStime() : atoi(parv[2]);
832 * Client just changing his/her nick. If he/she is
833 * on a channel, send note of change to all clients
834 * on that channel. Propagate notice to other servers.
837 sendcmdto_common_channels_butone(sptr, CMD_NICK, NULL, ":%s", nick);
838 add_history(sptr, 1);
839 sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick,
843 sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick);
845 if ((cli_name(sptr))[0])
847 strcpy(cli_name(sptr), nick);
851 /* Local client setting NICK the first time */
853 strcpy(cli_name(sptr), nick);
854 if (!cli_user(sptr)) {
855 cli_user(sptr) = make_user(sptr);
856 cli_user(sptr)->server = &me;
861 * If the client hasn't gotten a cookie-ping yet,
862 * choose a cookie and send it. -record!jegelhof@cloud9.net
864 if (!cli_cookie(sptr)) {
866 cli_cookie(sptr) = (ircrandom() & 0x7fffffff);
867 } while (!cli_cookie(sptr));
868 sendrawto_one(cptr, MSG_PING " :%u", cli_cookie(sptr));
870 else if (*(cli_user(sptr))->host && cli_cookie(sptr) == COOKIE_VERIFIED) {
872 * USER and PONG already received, now we have NICK.
873 * register_user may reject the client and call exit_client
874 * for it - must test this and exit m_nick too !
876 cli_lastnick(sptr) = TStime(); /* Always local client */
877 if (register_user(cptr, sptr, nick, cli_user(sptr)->username) == CPTR_KILLED)
884 /** Calculate the hash value for a target.
885 * @param[in] target Pointer to target, cast to unsigned int.
886 * @return Hash value constructed from the pointer.
888 static unsigned char hash_target(unsigned int target)
890 return (unsigned char) (target >> 16) ^ (target >> 8);
893 /** Records \a target as a recent target for \a sptr.
894 * @param[in] sptr User who has sent to a new target.
895 * @param[in] target Target to add.
898 add_target(struct Client *sptr, void *target)
900 /* Ok, this shouldn't work esp on alpha
902 unsigned char hash = hash_target((unsigned long) target);
903 unsigned char* targets;
906 assert(cli_local(sptr));
908 targets = cli_targets(sptr);
913 for (i = 0; i < MAXTARGETS; ++i) {
914 if (targets[i] == hash)
920 memmove(&targets[RESERVEDTARGETS + 1],
921 &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
922 targets[RESERVEDTARGETS] = hash;
925 /** Check whether \a sptr can send to or join \a target yet.
926 * @param[in] sptr User trying to join a channel or send a message.
927 * @param[in] target Target of the join or message.
928 * @param[in] name Name of the target.
929 * @param[in] created If non-zero, trying to join a new channel.
930 * @return Non-zero if too many target changes; zero if okay to send.
932 int check_target_limit(struct Client *sptr, void *target, const char *name,
935 unsigned char hash = hash_target((unsigned long) target);
937 unsigned char* targets;
940 assert(cli_local(sptr));
941 targets = cli_targets(sptr);
943 /* If user is invited to channel, give him/her a free target */
944 if (IsChannelName(name) && IsInvited(sptr, target))
948 * Same target as last time?
950 if (targets[0] == hash)
952 for (i = 1; i < MAXTARGETS; ++i) {
953 if (targets[i] == hash) {
954 memmove(&targets[1], &targets[0], i);
963 if (CurrentTime < cli_nexttarget(sptr)) {
964 if (cli_nexttarget(sptr) - CurrentTime < TARGET_DELAY + 8) {
968 cli_nexttarget(sptr) += 2;
969 send_reply(sptr, ERR_TARGETTOOFAST, name,
970 cli_nexttarget(sptr) - CurrentTime);
975 cli_nexttarget(sptr) += TARGET_DELAY;
976 if (cli_nexttarget(sptr) < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)))
977 cli_nexttarget(sptr) = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1));
980 memmove(&targets[1], &targets[0], MAXTARGETS - 1);
985 /** Allows a channel operator to avoid target change checks when
986 * sending messages to users on their channel.
987 * @param[in] source User sending the message.
988 * @param[in] nick Destination of the message.
989 * @param[in] channel Name of channel being sent to.
990 * @param[in] text Message to send.
991 * @param[in] is_notice If non-zero, use CNOTICE instead of CPRIVMSG.
993 /* Added 971023 by Run. */
994 int whisper(struct Client* source, const char* nick, const char* channel,
995 const char* text, int is_notice)
998 struct Channel* chptr;
999 struct Membership* membership;
1001 assert(0 != source);
1003 assert(0 != channel);
1004 assert(MyUser(source));
1006 if (!(dest = FindUser(nick))) {
1007 return send_reply(source, ERR_NOSUCHNICK, nick);
1009 if (!(chptr = FindChannel(channel))) {
1010 return send_reply(source, ERR_NOSUCHCHANNEL, channel);
1013 * compare both users channel lists, instead of the channels user list
1014 * since the link is the same, this should be a little faster for channels
1015 * with a lot of users
1017 for (membership = cli_user(source)->channel; membership; membership = membership->next_channel) {
1018 if (chptr == membership->channel)
1021 if (0 == membership) {
1022 return send_reply(source, ERR_NOTONCHANNEL, chptr->chname);
1024 if (!IsVoicedOrOpped(membership)) {
1025 return send_reply(source, ERR_VOICENEEDED, chptr->chname);
1028 * lookup channel in destination
1030 assert(0 != cli_user(dest));
1031 for (membership = cli_user(dest)->channel; membership; membership = membership->next_channel) {
1032 if (chptr == membership->channel)
1035 if (0 == membership || IsZombie(membership)) {
1036 return send_reply(source, ERR_USERNOTINCHANNEL, cli_name(dest), chptr->chname);
1038 if (is_silenced(source, dest))
1041 if (cli_user(dest)->away)
1042 send_reply(source, RPL_AWAY, cli_name(dest), cli_user(dest)->away);
1044 sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text);
1046 sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text);
1051 /** Send a user mode change for \a cptr to neighboring servers.
1052 * @param[in] cptr User whose mode is changing.
1053 * @param[in] sptr Client who sent us the mode change message.
1054 * @param[in] old Prior set of user flags.
1055 * @param[in] prop If non-zero, also include FLAG_OPER.
1057 void send_umode_out(struct Client *cptr, struct Client *sptr,
1058 struct Flags *old, int prop)
1061 struct Client *acptr;
1063 send_umode(NULL, sptr, old, prop ? SEND_UMODES : SEND_UMODES_BUT_OPER);
1065 for (i = HighestFd; i >= 0; i--)
1067 if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
1068 (acptr != cptr) && (acptr != sptr) && *umodeBuf)
1069 sendcmdto_one(sptr, CMD_MODE, acptr, "%s :%s", cli_name(sptr), umodeBuf);
1071 if (cptr && MyUser(cptr))
1072 send_umode(cptr, sptr, old, ALL_UMODES);
1076 /** Call \a fmt for each Client named in \a names.
1077 * @param[in] sptr Client requesting information.
1078 * @param[in] names Space-delimited list of nicknames.
1079 * @param[in] rpl Base reply string for messages.
1080 * @param[in] fmt Formatting callback function.
1082 void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
1087 int users_found = 0;
1088 struct Client* acptr;
1095 mb = msgq_make(sptr, rpl_str(rpl), cli_name(&me), cli_name(sptr));
1097 for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
1098 if ((acptr = FindUser(name))) {
1100 msgq_append(0, mb, " ");
1101 (*fmt)(acptr, sptr, mb);
1103 if (5 == ++arg_count)
1106 send_buffer(sptr, mb, 0);
1110 /** Set \a flag on \a cptr and possibly hide the client's hostmask.
1111 * @param[in,out] cptr User who is getting a new flag.
1112 * @param[in] flag Some flag that affects host-hiding (FLAG_HIDDENHOST, FLAG_ACCOUNT).
1116 hide_hostmask(struct Client *cptr, unsigned int flag)
1118 struct Membership *chan;
1121 case FLAG_HIDDENHOST:
1122 /* Local users cannot set +x unless FEAT_HOST_HIDING is true. */
1123 if (MyConnect(cptr) && !feature_bool(FEAT_HOST_HIDING))
1127 /* Invalidate all bans against the user so we check them again */
1128 for (chan = (cli_user(cptr))->channel; chan;
1129 chan = chan->next_channel)
1130 ClearBanValid(chan);
1135 SetFlag(cptr, flag);
1136 if (!HasFlag(cptr, FLAG_HIDDENHOST) || !HasFlag(cptr, FLAG_ACCOUNT))
1139 sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Registered");
1140 ircd_snprintf(0, cli_user(cptr)->host, HOSTLEN, "%s.%s",
1141 cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
1143 /* ok, the client is now fully hidden, so let them know -- hikari */
1144 if (MyConnect(cptr))
1145 send_reply(cptr, RPL_HOSTHIDDEN, cli_user(cptr)->host);
1148 * Go through all channels the client was on, rejoin him
1149 * and set the modes, if any
1151 for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel)
1155 /* For a user with no modes in a join-delayed channel, do not show
1157 if (!IsChanOp(chan) && !HasVoice(chan)
1158 && (chan->channel->mode.mode & MODE_DELJOINS))
1159 SetDelayedJoin(chan);
1161 sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, 0,
1162 "%H", chan->channel);
1163 if (IsChanOp(chan) && HasVoice(chan))
1164 sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr, 0,
1165 "%H +ov %C %C", chan->channel, cptr,
1167 else if (IsChanOp(chan) || HasVoice(chan))
1168 sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr, 0,
1169 "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr);
1174 /** Set a user's mode. This function checks that \a cptr is trying to
1175 * set his own mode, prevents local users from setting inappropriate
1176 * modes through this function, and applies any other side effects of
1177 * a successful mode change.
1179 * @param[in,out] cptr User setting someone's mode.
1180 * @param[in] sptr Client who sent the mode change message.
1181 * @param[in] parc Number of parameters in \a parv.
1182 * @param[in] parv Parameters to MODE.
1185 int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
1189 struct Client *acptr;
1192 struct Flags setflags;
1193 unsigned int tmpmask = 0;
1194 int snomask_given = 0;
1197 int do_host_hiding = 0;
1202 return need_more_params(sptr, "MODE");
1204 if (!(acptr = FindUser(parv[1])))
1206 if (MyConnect(sptr))
1207 send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]);
1211 if (IsServer(sptr) || sptr != acptr)
1214 sendwallto_group_butone(&me, WALL_WALLOPS, 0,
1215 "MODE for User %s from %s!%s", parv[1],
1216 cli_name(cptr), cli_name(sptr));
1218 send_reply(sptr, ERR_USERSDONTMATCH);
1226 for (i = 0; i < USERMODELIST_SIZE; i++)
1228 if (HasFlag(sptr, userModeList[i].flag) &&
1229 userModeList[i].flag != FLAG_ACCOUNT)
1230 *m++ = userModeList[i].c;
1233 send_reply(sptr, RPL_UMODEIS, buf);
1234 if (HasFlag(sptr, FLAG_SERVNOTICE) && MyConnect(sptr)
1235 && cli_snomask(sptr) !=
1236 (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT))
1237 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1242 * find flags already set for user
1243 * why not just copy them?
1245 setflags = cli_flags(sptr);
1247 if (MyConnect(sptr))
1248 tmpmask = cli_snomask(sptr);
1251 * parse mode change string(s)
1253 for (p = &parv[2]; *p; p++) { /* p is changed in loop too */
1254 for (m = *p; *m; m++) {
1263 if (*(p + 1) && is_snomask(*(p + 1))) {
1265 tmpmask = umode_make_snomask(tmpmask, *++p, what);
1266 tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1269 tmpmask = (what == MODE_ADD) ?
1270 (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1272 SetServNotice(sptr);
1274 ClearServNotice(sptr);
1277 if (what == MODE_ADD)
1283 if (what == MODE_ADD)
1286 ClrFlag(sptr, FLAG_OPER);
1287 ClrFlag(sptr, FLAG_LOCOP);
1288 if (MyConnect(sptr))
1290 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1291 cli_handler(sptr) = CLIENT_HANDLER;
1296 if (what == MODE_ADD)
1300 ClrFlag(sptr, FLAG_OPER);
1301 ClrFlag(sptr, FLAG_LOCOP);
1302 if (MyConnect(sptr))
1304 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1305 cli_handler(sptr) = CLIENT_HANDLER;
1310 if (what == MODE_ADD)
1313 ClearInvisible(sptr);
1316 if (what == MODE_ADD)
1322 if (what == MODE_ADD)
1323 SetChannelService(sptr);
1325 ClearChannelService(sptr);
1328 if (what == MODE_ADD)
1334 if (what == MODE_ADD)
1338 send_reply(sptr, ERR_UMODEUNKNOWNFLAG, *m);
1344 * Evaluate rules for new user mode
1345 * Stop users making themselves operators too easily:
1347 if (!IsServer(cptr))
1349 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr))
1351 if (!FlagHas(&setflags, FLAG_LOCOP) && IsLocOp(sptr))
1354 * new umode; servers can set it, local users cannot;
1355 * prevents users from /kick'ing or /mode -o'ing
1357 if (!FlagHas(&setflags, FLAG_CHSERV))
1358 ClearChannelService(sptr);
1360 * only send wallops to opers
1362 if (feature_bool(FEAT_WALLOPS_OPER_ONLY) && !IsAnOper(sptr) &&
1363 !FlagHas(&setflags, FLAG_WALLOP))
1365 if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY) && MyConnect(sptr) &&
1366 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_SERVNOTICE))
1368 ClearServNotice(sptr);
1369 set_snomask(sptr, 0, SNO_SET);
1371 if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY) &&
1372 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_DEBUG))
1375 if (MyConnect(sptr))
1377 if ((FlagHas(&setflags, FLAG_OPER) || FlagHas(&setflags, FLAG_LOCOP)) &&
1379 det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPERATOR);
1381 if (SendServNotice(sptr))
1383 if (tmpmask != cli_snomask(sptr))
1384 set_snomask(sptr, tmpmask, SNO_SET);
1385 if (cli_snomask(sptr) && snomask_given)
1386 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1389 set_snomask(sptr, 0, SNO_SET);
1392 * Compare new flags with old flags and send string which
1393 * will cause servers to update correctly.
1395 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr))
1399 client_set_privs(sptr, NULL); /* may set propagate privilege */
1401 /* remember propagate privilege setting */
1402 if (HasPriv(sptr, PRIV_PROPAGATE))
1404 if (FlagHas(&setflags, FLAG_OPER) && !IsOper(sptr))
1406 /* user no longer oper */
1408 client_set_privs(sptr, NULL); /* will clear propagate privilege */
1410 if (FlagHas(&setflags, FLAG_INVISIBLE) && !IsInvisible(sptr))
1411 --UserStats.inv_clients;
1412 if (!FlagHas(&setflags, FLAG_INVISIBLE) && IsInvisible(sptr))
1413 ++UserStats.inv_clients;
1414 if (!FlagHas(&setflags, FLAG_HIDDENHOST) && do_host_hiding)
1415 hide_hostmask(sptr, FLAG_HIDDENHOST);
1416 send_umode_out(cptr, sptr, &setflags, prop);
1421 /** Build a mode string to describe modes for \a cptr.
1422 * @param[in] cptr Some user.
1423 * @return Pointer to a static buffer.
1425 char *umode_str(struct Client *cptr)
1427 /* Maximum string size: "owidgrx\0" */
1430 struct Flags c_flags = cli_flags(cptr);
1432 if (HasPriv(cptr, PRIV_PROPAGATE))
1433 FlagSet(&c_flags, FLAG_OPER);
1435 FlagClr(&c_flags, FLAG_OPER);
1437 for (i = 0; i < USERMODELIST_SIZE; ++i)
1439 if (FlagHas(&c_flags, userModeList[i].flag) &&
1440 userModeList[i].flag >= FLAG_GLOBAL_UMODES)
1441 *m++ = userModeList[i].c;
1444 if (IsAccount(cptr))
1446 char* t = cli_user(cptr)->account;
1449 while ((*m++ = *t++))
1452 if (cli_user(cptr)->acc_create) {
1454 Debug((DEBUG_DEBUG, "Sending timestamped account in user mode for "
1455 "account \"%s\"; timestamp %Tu", cli_user(cptr)->account,
1456 cli_user(cptr)->acc_create));
1457 ircd_snprintf(0, t = nbuf, sizeof(nbuf), ":%Tu",
1458 cli_user(cptr)->acc_create);
1459 m--; /* back up over previous nul-termination */
1460 while ((*m++ = *t++))
1467 return umodeBuf; /* Note: static buffer, gets
1468 overwritten by send_umode() */
1471 /** Send a mode change string for \a sptr to \a cptr.
1472 * @param[in] cptr Destination of mode change message.
1473 * @param[in] sptr User whose mode has changed.
1474 * @param[in] old Pre-change set of modes for \a sptr.
1475 * @param[in] sendset One of ALL_UMODES, SEND_UMODES_BUT_OPER,
1476 * SEND_UMODES, to select which changed user modes to send.
1478 void send_umode(struct Client *cptr, struct Client *sptr, struct Flags *old,
1484 int what = MODE_NULL;
1487 * Build a string in umodeBuf to represent the change in the user's
1488 * mode between the new (cli_flags(sptr)) and 'old', but skipping
1489 * the modes indicated by sendset.
1493 for (i = 0; i < USERMODELIST_SIZE; ++i)
1495 flag = userModeList[i].flag;
1496 if (FlagHas(old, flag)
1497 == HasFlag(sptr, flag))
1503 case SEND_UMODES_BUT_OPER:
1504 if (flag == FLAG_OPER)
1506 /* and fall through */
1508 if (flag < FLAG_GLOBAL_UMODES)
1512 if (FlagHas(old, flag))
1514 if (what == MODE_DEL)
1515 *m++ = userModeList[i].c;
1520 *m++ = userModeList[i].c;
1523 else /* !FlagHas(old, flag) */
1525 if (what == MODE_ADD)
1526 *m++ = userModeList[i].c;
1531 *m++ = userModeList[i].c;
1536 if (*umodeBuf && cptr)
1537 sendcmdto_one(sptr, CMD_MODE, cptr, "%s :%s", cli_name(sptr), umodeBuf);
1541 * Check to see if this resembles a sno_mask. It is if 1) there is
1542 * at least one digit and 2) The first digit occurs before the first
1543 * alphabetic character.
1544 * @param[in] word Word to check for sno_mask-ness.
1545 * @return Non-zero if \a word looks like a server notice mask; zero if not.
1547 int is_snomask(char *word)
1551 for (; *word; word++)
1554 else if (IsAlpha(*word))
1560 /** Update snomask \a oldmask according to \a arg and \a what.
1561 * @param[in] oldmask Original user mask.
1562 * @param[in] arg Update string (either a number or '+'/'-' followed by a number).
1563 * @param[in] what MODE_ADD if adding the mask.
1564 * @return New value of service notice mask.
1566 unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
1568 unsigned int sno_what;
1569 unsigned int newmask;
1573 if (what == MODE_ADD)
1578 else if (*arg == '-')
1581 if (what == MODE_ADD)
1587 sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL;
1588 /* pity we don't have strtoul everywhere */
1589 newmask = (unsigned int)atoi(arg);
1590 if (sno_what == SNO_DEL)
1591 newmask = oldmask & ~newmask;
1592 else if (sno_what == SNO_ADD)
1597 /** Remove \a cptr from the singly linked list \a list.
1598 * @param[in] cptr Client to remove from list.
1599 * @param[in,out] list Pointer to head of list containing \a cptr.
1601 static void delfrom_list(struct Client *cptr, struct SLink **list)
1604 struct SLink* prv = NULL;
1606 for (tmp = *list; tmp; tmp = tmp->next) {
1607 if (tmp->value.cptr == cptr) {
1609 prv->next = tmp->next;
1619 /** Set \a cptr's server notice mask, according to \a what.
1620 * @param[in,out] cptr Client whose snomask is updating.
1621 * @param[in] newmask Base value for new snomask.
1622 * @param[in] what One of SNO_ADD, SNO_DEL, SNO_SET, to choose operation.
1624 void set_snomask(struct Client *cptr, unsigned int newmask, int what)
1626 unsigned int oldmask, diffmask; /* unsigned please */
1630 oldmask = cli_snomask(cptr);
1632 if (what == SNO_ADD)
1634 else if (what == SNO_DEL)
1635 newmask = oldmask & ~newmask;
1636 else if (what != SNO_SET) /* absolute set, no math needed */
1637 sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what);
1639 newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
1641 diffmask = oldmask ^ newmask;
1643 for (i = 0; diffmask >> i; i++) {
1644 if (((diffmask >> i) & 1))
1646 if (((newmask >> i) & 1))
1649 tmp->next = opsarray[i];
1650 tmp->value.cptr = cptr;
1654 /* not real portable :( */
1655 delfrom_list(cptr, &opsarray[i]);
1658 cli_snomask(cptr) = newmask;
1661 /** Check whether \a sptr is allowed to send a message to \a acptr.
1662 * If \a sptr is a remote user, it means some server has an outdated
1663 * SILENCE list for \a acptr, so send the missing SILENCE back in the
1664 * direction of \a sptr.
1665 * @param[in] sptr Client trying to send a message.
1666 * @param[in] acptr Destination of message.
1667 * @return Non-zero if \a sptr is SILENCEd by \a acptr, zero if not.
1669 int is_silenced(struct Client *sptr, struct Client *acptr)
1673 static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
1674 static char senderip[16 + NICKLEN + USERLEN + 5];
1675 static char senderh[HOSTLEN + ACCOUNTLEN + USERLEN + 6];
1677 if (!cli_user(acptr) || !(lp = cli_user(acptr)->silence) || !(user = cli_user(sptr)))
1679 ircd_snprintf(0, sender, sizeof(sender), "%s!%s@%s", cli_name(sptr),
1680 user->username, user->host);
1681 ircd_snprintf(0, senderip, sizeof(senderip), "%s!%s@%s", cli_name(sptr),
1682 user->username, ircd_ntoa(&cli_ip(sptr)));
1683 if (HasHiddenHost(sptr))
1684 ircd_snprintf(0, senderh, sizeof(senderh), "%s!%s@%s", cli_name(sptr),
1685 user->username, user->realhost);
1686 for (; lp; lp = lp->next)
1688 if ((!(lp->flags & CHFL_SILENCE_IPMASK) && (!match(lp->value.cp, sender) ||
1689 (HasHiddenHost(sptr) && !match(lp->value.cp, senderh)))) ||
1690 ((lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, senderip)))
1692 if (!MyConnect(sptr))
1694 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr,
1703 /** Remove all silence masks from \a sptr that match \a mask.
1704 * @param[in,out] sptr Client to update.
1705 * @param[in] mask Silence mask to remove.
1706 * @return Zero if any silence masks were removed; non-zero if all were kept.
1708 int del_silence(struct Client *sptr, char *mask)
1714 for (lp = &(cli_user(sptr))->silence; *lp;) {
1715 if (!mmatch(mask, (*lp)->value.cp))
1719 MyFree(tmp->value.cp);
1729 /** Add \a mask to the silence masks for \a sptr.
1730 * Removes any silence masks that are subsets of \a mask.
1731 * @param[in,out] sptr Client adding silence mask.
1732 * @param[in] mask Silence mask to add.
1733 * @return Zero on success; non-zero on any failure.
1735 int add_silence(struct Client* sptr, const char* mask)
1737 struct SLink *lp, **lpp;
1738 int cnt = 0, len = strlen(mask);
1741 for (lpp = &(cli_user(sptr))->silence, lp = *lpp; lp;)
1743 if (0 == ircd_strcmp(mask, lp->value.cp))
1745 if (!mmatch(mask, lp->value.cp))
1747 struct SLink *tmp = lp;
1748 *lpp = lp = lp->next;
1749 MyFree(tmp->value.cp);
1755 len += strlen(lp->value.cp);
1756 if ((len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXSILES))) ||
1757 (++cnt >= feature_int(FEAT_MAXSILES)))
1759 send_reply(sptr, ERR_SILELISTFULL, mask);
1762 else if (!mmatch(lp->value.cp, mask))
1769 memset(lp, 0, sizeof(struct SLink));
1770 lp->next = cli_user(sptr)->silence;
1771 lp->value.cp = (char*) MyMalloc(strlen(mask) + 1);
1772 assert(0 != lp->value.cp);
1773 strcpy(lp->value.cp, mask);
1774 if ((ip_start = strrchr(mask, '@')) && check_if_ipmask(ip_start + 1))
1775 lp->flags = CHFL_SILENCE_IPMASK;
1776 cli_user(sptr)->silence = lp;
1780 /** Send RPL_ISUPPORT lines to \a cptr.
1781 * @param[in] cptr Client to send ISUPPORT to.
1785 send_supported(struct Client *cptr)
1787 char featurebuf[512];
1789 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES1, FEATURESVALUES1);
1790 send_reply(cptr, RPL_ISUPPORT, featurebuf);
1791 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES2, FEATURESVALUES2);
1792 send_reply(cptr, RPL_ISUPPORT, featurebuf);
1794 return 0; /* convenience return, if it's ever needed */