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 */
61 #include "supported.h"
67 #include "handlers.h" /* m_motd and m_lusers */
69 /* #include <assert.h> -- Now using assert in ircd_log.h */
76 /** Count of allocated User structures. */
77 static int userCount = 0;
79 /** Makes sure that \a cptr has a User information block.
80 * If cli_user(cptr) != NULL, does nothing.
81 * @param[in] cptr Client to attach User struct to.
82 * @return User struct associated with \a cptr.
84 struct User *make_user(struct Client *cptr)
88 if (!cli_user(cptr)) {
89 cli_user(cptr) = (struct User*) MyMalloc(sizeof(struct User));
90 assert(0 != cli_user(cptr));
92 /* All variables are 0 by default */
93 memset(cli_user(cptr), 0, sizeof(struct User));
97 cli_user(cptr)->refcnt = 1;
99 return cli_user(cptr);
102 /** Dereference \a user.
103 * User structures are reference-counted; if the refcount of \a user
104 * becomes zero, free it.
105 * @param[in] user User to dereference.
107 void free_user(struct User* user)
110 assert(0 < user->refcnt);
112 if (--user->refcnt == 0) {
118 assert(0 == user->joined);
119 assert(0 == user->invited);
120 assert(0 == user->channel);
129 /** Find number of User structs allocated and memory used by them.
130 * @param[out] count_out Receives number of User structs allocated.
131 * @param[out] bytes_out Receives number of bytes used by User structs.
133 void user_count_memory(size_t* count_out, size_t* bytes_out)
135 assert(0 != count_out);
136 assert(0 != bytes_out);
137 *count_out = userCount;
138 *bytes_out = userCount * sizeof(struct User);
142 /** Find the next client (starting at \a next) with a name that matches \a ch.
143 * Normal usage loop is:
144 * for (x = client; x = next_client(x,mask); x = x->next)
145 * HandleMatchingClient;
147 * @param[in] next First client to check.
148 * @param[in] ch Name mask to check against.
149 * @return Next matching client found, or NULL if none.
151 struct Client *next_client(struct Client *next, const char* ch)
153 struct Client *tmp = next;
158 next = FindClient(ch);
159 next = next ? next : tmp;
160 if (cli_prev(tmp) == next)
164 for (; next; next = cli_next(next))
165 if (!match(ch, cli_name(next)))
170 /** Find the destination server for a command, and forward it if that is not us.
172 * \a server may be a nickname, server name, server mask (if \a from
173 * is a local user) or server numnick (if \a is a server or remote
176 * @param[in] from Client that sent the command to us.
177 * @param[in] cmd Long-form command text.
178 * @param[in] tok Token-form command text.
179 * @param[in] one Client that originated the command (ignored).
180 * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH.
181 * @param[in] pattern Format string of arguments to command.
182 * @param[in] server Index of target name or mask in \a parv.
183 * @param[in] parc Number of valid elements in \a parv (must be less than 9).
184 * @param[in] parv Array of arguments to command.
185 * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS.
187 int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok,
188 struct Client *one, int MustBeOper, const char *pattern,
189 int server, int parc, char *parv[])
191 struct Client *acptr;
194 /* Assume it's me, if no server or an unregistered client */
195 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
196 return (HUNTED_ISME);
198 if (MustBeOper && !IsPrivileged(from))
200 send_reply(from, ERR_NOPRIVILEGES);
201 return HUNTED_NOSUCH;
204 /* Make sure it's a server */
206 /* Make sure it's a server */
207 if (!strchr(to, '*')) {
208 if (0 == (acptr = FindClient(to))) {
209 send_reply(from, ERR_NOSUCHSERVER, to);
210 return HUNTED_NOSUCH;
214 acptr = cli_user(acptr)->server;
215 } else if (!(acptr = find_match_server(to))) {
216 send_reply(from, ERR_NOSUCHSERVER, to);
217 return (HUNTED_NOSUCH);
219 } else if (!(acptr = FindNServer(to))) {
220 send_reply(from, SND_EXPLICIT | ERR_NOSUCHSERVER, "* :Server has disconnected");
221 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
225 return (HUNTED_ISME);
227 if (MustBeOper && !IsPrivileged(from)) {
228 send_reply(from, ERR_NOPRIVILEGES);
229 return HUNTED_NOSUCH;
232 /* assert(!IsServer(from)); */
234 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
236 sendcmdto_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
237 parv[4], parv[5], parv[6], parv[7], parv[8]);
239 return (HUNTED_PASS);
242 /** Find the destination server for a command, and forward it (as a
243 * high-priority command) if that is not us.
245 * \a server may be a nickname, server name, server mask (if \a from
246 * is a local user) or server numnick (if \a is a server or remote
248 * Unlike hunt_server_cmd(), this appends the message to the
249 * high-priority message queue for the destination server.
251 * @param[in] from Client that sent the command to us.
252 * @param[in] cmd Long-form command text.
253 * @param[in] tok Token-form command text.
254 * @param[in] one Client that originated the command (ignored).
255 * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH.
256 * @param[in] pattern Format string of arguments to command.
257 * @param[in] server Index of target name or mask in \a parv.
258 * @param[in] parc Number of valid elements in \a parv (must be less than 9).
259 * @param[in] parv Array of arguments to command.
260 * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS.
262 int hunt_server_prio_cmd(struct Client *from, const char *cmd, const char *tok,
263 struct Client *one, int MustBeOper,
264 const char *pattern, int server, int parc,
267 struct Client *acptr;
270 /* Assume it's me, if no server or an unregistered client */
271 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
272 return (HUNTED_ISME);
274 /* Make sure it's a server */
276 /* Make sure it's a server */
277 if (!strchr(to, '*')) {
278 if (0 == (acptr = FindClient(to))) {
279 send_reply(from, ERR_NOSUCHSERVER, to);
280 return HUNTED_NOSUCH;
284 acptr = cli_user(acptr)->server;
285 } else if (!(acptr = find_match_server(to))) {
286 send_reply(from, ERR_NOSUCHSERVER, to);
287 return (HUNTED_NOSUCH);
289 } else if (!(acptr = FindNServer(to)))
290 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
293 return (HUNTED_ISME);
295 if (MustBeOper && !IsPrivileged(from)) {
296 send_reply(from, ERR_NOPRIVILEGES);
297 return HUNTED_NOSUCH;
300 /* assert(!IsServer(from)); SETTIME to particular destinations permitted */
302 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
304 sendcmdto_prio_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
305 parv[4], parv[5], parv[6], parv[7], parv[8]);
307 return (HUNTED_PASS);
311 /** Copy a cleaned-up version of a username.
312 * Replace all instances of '~' and "invalid" username characters
313 * (!isIrcUi()) with underscores, truncating at USERLEN or the first
314 * control character. \a dest and \a source may be the same buffer.
315 * @param[out] dest Destination buffer.
316 * @param[in] source Source buffer.
317 * @param[in] tilde If non-zero, prepend a '~' to \a dest.
319 static char *clean_user_id(char *dest, char *source, int tilde)
326 ch = *s++; /* Store first character to copy: */
329 *d++ = '~'; /* If `dest' == `source', then this overwrites `ch' */
332 while (ch && !IsCntrl(ch) && rlen--)
334 char nch = *s++; /* Store next character to copy */
335 *d++ = IsUserChar(ch) ? ch : '_'; /* This possibly overwrites it */
348 * This function is called when both NICK and USER messages
349 * have been accepted for the client, in whatever order. Only
350 * after this the USER message is propagated.
352 * NICK's must be propagated at once when received, although
353 * it would be better to delay them too until full info is
354 * available. Doing it is not so simple though, would have
355 * to implement the following:
357 * 1) user telnets in and gives only "NICK foobar" and waits
358 * 2) another user far away logs in normally with the nick
359 * "foobar" (quite legal, as this server didn't propagate it).
360 * 3) now this server gets nick "foobar" from outside, but
361 * has already the same defined locally. Current server
362 * would just issue "KILL foobar" to clean out dups. But,
363 * this is not fair. It should actually request another
364 * nick from local user or kill him/her...
366 /** Finish registering a user who has sent both NICK and USER.
367 * For local connections, possibly check IAuth; make sure there is a
368 * matching Client config block; clean the username field; check
369 * K/k-lines; check for "hacked" looking usernames; assign a numnick;
370 * and send greeting (WELCOME, ISUPPORT, MOTD, etc).
371 * For all connections, update the invisible user and operator counts;
372 * run IPcheck against their address; and forward the NICK.
374 * @param[in] cptr Client who introduced the user.
375 * @param[in,out] sptr Client who has been fully introduced.
376 * @param[in] nick Client's new nickname.
377 * @param[in] username Client's username.
378 * @return Zero or CPTR_KILLED.
380 int register_user(struct Client *cptr, struct Client *sptr,
381 const char *nick, char *username)
383 struct ConfItem* aconf;
387 char c = 0; /* not alphanum */
388 char d = 'a'; /* not a digit */
396 short digitgroups = 0;
397 struct User* user = cli_user(sptr);
401 user->last = CurrentTime;
402 parv[0] = cli_name(sptr);
403 parv[1] = parv[2] = NULL;
407 static time_t last_too_many1;
408 static time_t last_too_many2;
410 assert(cptr == sptr);
411 assert(cli_unreg(sptr) == 0);
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 %s for %s.", get_client_class(sptr),
434 get_client_name(sptr, SHOW_IP));
436 ++ServerStats->is_ref;
437 IPcheck_connect_fail(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(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 && strcmp(cli_passwd(sptr), aconf->passwd))
474 ServerStats->is_ref++;
475 send_reply(sptr, ERR_PASSWDMISMATCH);
476 return exit_client(cptr, sptr, &me, "Bad Password");
478 memset(cli_passwd(sptr), 0, sizeof(cli_passwd(sptr)));
480 * following block for the benefit of time-dependent K:-lines
482 killreason = find_kill(sptr);
484 ServerStats->is_ref++;
485 return exit_client(cptr, sptr, &me,
486 (killreason == -1 ? "K-lined" : "G-lined"));
489 * Check for mixed case usernames, meaning probably hacked. Jon2 3-94
490 * Summary of rules now implemented in this patch: Ensor 11-94
491 * In a mixed-case name, if first char is upper, one more upper may
492 * appear anywhere. (A mixed-case name *must* have an upper first
493 * char, and may have one other upper.)
494 * A third upper may appear if all 3 appear at the beginning of the
495 * name, separated only by "others" (-/_/.).
496 * A single group of digits is allowed anywhere.
497 * Two groups of digits are allowed if at least one of the groups is
498 * at the beginning or the end.
499 * Only one '-', '_', or '.' is allowed (or two, if not consecutive).
500 * But not as the first or last char.
501 * No other special characters are allowed.
502 * Name must contain at least one letter.
504 tmpstr2 = tmpstr = (username[0] == '~' ? &username[1] : username);
505 while (*tmpstr && !badid)
517 if ((leadcaps || pos == 1) && !lower && !digits)
523 if (pos == 1 || !IsDigit(d))
530 else if (c == '-' || c == '_' || c == '.')
535 else if (d == '-' || d == '_' || d == '.' || other > 2)
544 if (lower && upper && (!leadcaps || leadcaps > 3 ||
545 (upper > 2 && upper > leadcaps)))
547 else if (digitgroups == 2 && !(IsDigit(tmpstr2[0]) || IsDigit(c)))
549 else if ((!lower && !upper) || !IsAlnum(c))
552 if (badid && (!HasFlag(sptr, FLAG_GOTID) ||
553 strcmp(cli_username(sptr), username) != 0))
555 ServerStats->is_ref++;
557 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
558 ":Your username is invalid.");
559 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
560 ":Connect with your real username, in lowercase.");
561 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
562 ":If your mail address were foo@bar.com, your username "
564 return exit_client(cptr, sptr, &me, "USER: Bad username");
566 Count_unknownbecomesclient(sptr, UserStats);
569 cli_handler(sptr) = CLIENT_HANDLER;
570 release_dns_reply(sptr);
571 SetLocalNumNick(sptr);
574 feature_str(FEAT_NETWORK),
575 feature_str(FEAT_PROVIDER) ? " via " : "",
576 feature_str(FEAT_PROVIDER) ? feature_str(FEAT_PROVIDER) : "",
579 * This is a duplicate of the NOTICE but see below...
581 send_reply(sptr, RPL_YOURHOST, cli_name(&me), version);
582 send_reply(sptr, RPL_CREATED, creation);
583 send_reply(sptr, RPL_MYINFO, cli_name(&me), version, infousermodes,
584 infochanmodes, infochanmodeswithparams);
585 send_supported(sptr);
586 m_lusers(sptr, sptr, 1, parv);
589 if (cli_snomask(sptr) & SNO_NOISY)
590 set_snomask(sptr, cli_snomask(sptr) & SNO_NOISY, SNO_ADD);
591 if (feature_bool(FEAT_CONNEXIT_NOTICES))
592 sendto_opmask_butone(0, SNO_CONNEXIT,
593 "Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s%s>",
594 cli_name(sptr), user->username, user->host,
595 cli_sock_ip(sptr), get_client_class(sptr),
596 cli_info(sptr), NumNick(cptr) /* two %s's */);
598 IPcheck_connect_succeeded(sptr);
600 * Set user's initial modes
602 tmpstr = (char*)client_get_default_umode(sptr);
603 if (tmpstr) for (; *tmpstr; ++tmpstr) {
606 if (!feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY)) {
608 set_snomask(sptr, SNO_DEFAULT, SNO_SET);
612 if (!feature_bool(FEAT_WALLOPS_OPER_ONLY))
622 if (!feature_bool(FEAT_HIS_DEBUG_OPER_ONLY))
629 struct Client *acptr;
631 ircd_strncpy(user->username, username, USERLEN);
632 Count_newremoteclient(UserStats, user->server);
634 acptr = user->server;
635 if (cli_from(acptr) != cli_from(sptr))
637 sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])",
638 sptr, cli_name(&me), cli_name(user->server), cli_name(cli_from(acptr)),
639 cli_sockhost(cli_from(acptr)));
640 SetFlag(sptr, FLAG_KILLED);
641 return exit_client(cptr, sptr, &me, "NICK server wrong direction");
643 else if (HasFlag(acptr, FLAG_TS8))
644 SetFlag(sptr, FLAG_TS8);
647 * Check to see if this user is being propagated
648 * as part of a net.burst, or is using protocol 9.
649 * FIXME: This can be sped up - its stupid to check it for
650 * every NICK message in a burst again --Run.
652 for (; acptr != &me; acptr = cli_serv(acptr)->up)
654 if (IsBurst(acptr) || Protocol(acptr) < 10)
657 if (!IPcheck_remote_connect(sptr, (acptr != &me)))
660 * We ran out of bits to count this
662 sendcmdto_one(&me, CMD_KILL, sptr, "%C :%s (Too many connections from your host -- Ghost)",
663 sptr, cli_name(&me));
664 return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled");
669 if (IsInvisible(sptr))
670 ++UserStats.inv_clients;
674 tmpstr = umode_str(sptr);
675 /* Send full IP address to IPv6-grokking servers. */
676 sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr,
677 FLAG_IPV6, FLAG_LAST_FLAG,
678 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
679 nick, cli_hopcount(sptr) + 1, cli_lastnick(sptr),
680 user->username, user->realhost,
681 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
682 iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 1),
683 NumNick(sptr), cli_info(sptr));
684 /* Send fake IPv6 addresses to pre-IPv6 servers. */
685 sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr,
686 FLAG_LAST_FLAG, FLAG_IPV6,
687 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
688 nick, cli_hopcount(sptr) + 1, cli_lastnick(sptr),
689 user->username, user->realhost,
690 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
691 iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 0),
692 NumNick(sptr), cli_info(sptr));
694 /* Send user mode to client */
697 static struct Flags flags; /* automatically initialized to zeros */
698 send_umode(cptr, sptr, &flags, ALL_UMODES);
699 if ((cli_snomask(sptr) != SNO_DEFAULT) && HasFlag(sptr, FLAG_SERVNOTICE))
700 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
705 /** List of user mode characters. */
706 static const struct UserMode {
707 unsigned int flag; /**< User mode constant. */
708 char c; /**< Character corresponding to the mode. */
712 { FLAG_INVISIBLE, 'i' },
713 { FLAG_WALLOP, 'w' },
714 { FLAG_SERVNOTICE, 's' },
716 { FLAG_CHSERV, 'k' },
718 { FLAG_ACCOUNT, 'r' },
719 { FLAG_HIDDENHOST, 'x' }
722 /** Length of #userModeList. */
723 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
726 * XXX - find a way to get rid of this
728 /** Nasty global buffer used for communications with umode_str() and others. */
729 static char umodeBuf[BUFSIZE];
731 /** Try to set a user's nickname.
732 * If \a sptr is a server, the client is being introduced for the first time.
733 * @param[in] cptr Client to set nickname.
734 * @param[in] sptr Client sending the NICK.
735 * @param[in] nick New nickname.
736 * @param[in] parc Number of arguments to NICK.
737 * @param[in] parv Argument list to NICK.
738 * @return CPTR_KILLED if \a cptr was killed, else 0.
740 int set_nick_name(struct Client* cptr, struct Client* sptr,
741 const char* nick, int parc, char* parv[])
743 if (IsServer(sptr)) {
745 const char* account = 0;
749 * A server introducing a new client, change source
751 struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
752 assert(0 != new_client);
754 cli_hopcount(new_client) = atoi(parv[2]);
755 cli_lastnick(new_client) = atoi(parv[3]);
756 if (Protocol(cptr) > 9 && parc > 7 && *parv[6] == '+')
758 for (p = parv[6] + 1; *p; p++)
760 for (i = 0; i < USERMODELIST_SIZE; ++i)
762 if (userModeList[i].c == *p)
764 SetFlag(new_client, userModeList[i].flag);
765 if (userModeList[i].flag == FLAG_ACCOUNT)
772 client_set_privs(new_client, NULL); /* set privs on user */
776 strcpy(cli_name(new_client), nick);
777 cli_user(new_client) = make_user(new_client);
778 cli_user(new_client)->server = sptr;
779 SetRemoteNumNick(new_client, parv[parc - 2]);
781 * IP# of remote client
783 base64toip(parv[parc - 3], &cli_ip(new_client));
785 add_client_to_list(new_client);
786 hAddClient(new_client);
788 cli_serv(sptr)->ghost = 0; /* :server NICK means end of net.burst */
789 ircd_strncpy(cli_username(new_client), parv[4], USERLEN);
790 ircd_strncpy(cli_user(new_client)->host, parv[5], HOSTLEN);
791 ircd_strncpy(cli_user(new_client)->realhost, parv[5], HOSTLEN);
792 ircd_strncpy(cli_info(new_client), parv[parc - 1], REALLEN);
794 int len = ACCOUNTLEN;
795 if ((p = strchr(account, ':'))) {
796 len = (p++) - account;
797 cli_user(new_client)->acc_create = atoi(p);
798 Debug((DEBUG_DEBUG, "Received timestamped account in user mode; "
799 "account \"%s\", timestamp %Tu", account,
800 cli_user(new_client)->acc_create));
802 ircd_strncpy(cli_user(new_client)->account, account, len);
804 if (HasHiddenHost(new_client))
805 ircd_snprintf(0, cli_user(new_client)->host, HOSTLEN, "%s.%s",
806 account, feature_str(FEAT_HIDDEN_HOST));
808 return register_user(cptr, new_client, cli_name(new_client), parv[4]);
810 else if ((cli_name(sptr))[0]) {
812 * Client changing its nick
814 * If the client belongs to me, then check to see
815 * if client is on any channels where it is currently
816 * banned. If so, do not allow the nick change to occur.
819 const char* channel_name;
820 struct Membership *member;
821 if ((channel_name = find_no_nickchange_channel(sptr))) {
822 return send_reply(cptr, ERR_BANNICKCHANGE, channel_name);
825 * Refuse nick change if the last nick change was less
826 * then 30 seconds ago. This is intended to get rid of
827 * clone bots doing NICK FLOOD. -SeKs
828 * If someone didn't change their nick for more then 60 seconds
829 * however, allow to do two nick changes immediately after another
830 * before limiting the nick flood. -Run
832 if (CurrentTime < cli_nextnick(cptr))
834 cli_nextnick(cptr) += 2;
835 send_reply(cptr, ERR_NICKTOOFAST, parv[1],
836 cli_nextnick(cptr) - CurrentTime);
837 /* Send error message */
838 sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cli_name(cptr));
839 /* bounce NICK to user */
840 return 0; /* ignore nick change! */
843 /* Limit total to 1 change per NICK_DELAY seconds: */
844 cli_nextnick(cptr) += NICK_DELAY;
845 /* However allow _maximal_ 1 extra consecutive nick change: */
846 if (cli_nextnick(cptr) < CurrentTime)
847 cli_nextnick(cptr) = CurrentTime;
849 /* Invalidate all bans against the user so we check them again */
850 for (member = (cli_user(cptr))->channel; member;
851 member = member->next_channel)
852 ClearBanValid(member);
855 * Also set 'lastnick' to current time, if changed.
857 if (0 != ircd_strcmp(parv[0], nick))
858 cli_lastnick(sptr) = (sptr == cptr) ? TStime() : atoi(parv[2]);
861 * Client just changing his/her nick. If he/she is
862 * on a channel, send note of change to all clients
863 * on that channel. Propagate notice to other servers.
866 sendcmdto_common_channels_butone(sptr, CMD_NICK, NULL, ":%s", nick);
867 add_history(sptr, 1);
868 sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick,
872 sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick);
874 if ((cli_name(sptr))[0])
876 strcpy(cli_name(sptr), nick);
880 /* Local client setting NICK the first time */
882 strcpy(cli_name(sptr), nick);
883 if (!cli_user(sptr)) {
884 cli_user(sptr) = make_user(sptr);
885 cli_user(sptr)->server = &me;
889 cli_unreg(sptr) &= ~CLIREG_NICK; /* nickname now set */
892 * If the client hasn't gotten a cookie-ping yet,
893 * choose a cookie and send it. -record!jegelhof@cloud9.net
895 if (!cli_cookie(sptr)) {
897 cli_cookie(sptr) = (ircrandom() & 0x7fffffff);
898 } while (!cli_cookie(sptr));
899 sendrawto_one(cptr, MSG_PING " :%u", cli_cookie(sptr));
901 else if (!cli_unreg(sptr)) {
903 * USER and PONG already received, now we have NICK.
904 * register_user may reject the client and call exit_client
905 * for it - must test this and exit m_nick too !
907 cli_lastnick(sptr) = TStime(); /* Always local client */
908 if (register_user(cptr, sptr, nick, cli_user(sptr)->username) == CPTR_KILLED)
915 /** Calculate the hash value for a target.
916 * @param[in] target Pointer to target, cast to unsigned int.
917 * @return Hash value constructed from the pointer.
919 static unsigned char hash_target(unsigned int target)
921 return (unsigned char) (target >> 16) ^ (target >> 8);
924 /** Records \a target as a recent target for \a sptr.
925 * @param[in] sptr User who has sent to a new target.
926 * @param[in] target Target to add.
929 add_target(struct Client *sptr, void *target)
931 /* Ok, this shouldn't work esp on alpha
933 unsigned char hash = hash_target((unsigned long) target);
934 unsigned char* targets;
937 assert(cli_local(sptr));
939 targets = cli_targets(sptr);
944 for (i = 0; i < MAXTARGETS; ++i) {
945 if (targets[i] == hash)
951 memmove(&targets[RESERVEDTARGETS + 1],
952 &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
953 targets[RESERVEDTARGETS] = hash;
956 /** Check whether \a sptr can send to or join \a target yet.
957 * @param[in] sptr User trying to join a channel or send a message.
958 * @param[in] target Target of the join or message.
959 * @param[in] name Name of the target.
960 * @param[in] created If non-zero, trying to join a new channel.
961 * @return Non-zero if too many target changes; zero if okay to send.
963 int check_target_limit(struct Client *sptr, void *target, const char *name,
966 unsigned char hash = hash_target((unsigned long) target);
968 unsigned char* targets;
971 assert(cli_local(sptr));
972 targets = cli_targets(sptr);
974 /* If user is invited to channel, give him/her a free target */
975 if (IsChannelName(name) && IsInvited(sptr, target))
979 * Same target as last time?
981 if (targets[0] == hash)
983 for (i = 1; i < MAXTARGETS; ++i) {
984 if (targets[i] == hash) {
985 memmove(&targets[1], &targets[0], i);
994 if (CurrentTime < cli_nexttarget(sptr)) {
995 if (cli_nexttarget(sptr) - CurrentTime < TARGET_DELAY + 8) {
999 cli_nexttarget(sptr) += 2;
1000 send_reply(sptr, ERR_TARGETTOOFAST, name,
1001 cli_nexttarget(sptr) - CurrentTime);
1006 cli_nexttarget(sptr) += TARGET_DELAY;
1007 if (cli_nexttarget(sptr) < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)))
1008 cli_nexttarget(sptr) = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1));
1011 memmove(&targets[1], &targets[0], MAXTARGETS - 1);
1016 /** Allows a channel operator to avoid target change checks when
1017 * sending messages to users on their channel.
1018 * @param[in] source User sending the message.
1019 * @param[in] nick Destination of the message.
1020 * @param[in] channel Name of channel being sent to.
1021 * @param[in] text Message to send.
1022 * @param[in] is_notice If non-zero, use CNOTICE instead of CPRIVMSG.
1024 /* Added 971023 by Run. */
1025 int whisper(struct Client* source, const char* nick, const char* channel,
1026 const char* text, int is_notice)
1028 struct Client* dest;
1029 struct Channel* chptr;
1030 struct Membership* membership;
1032 assert(0 != source);
1034 assert(0 != channel);
1035 assert(MyUser(source));
1037 if (!(dest = FindUser(nick))) {
1038 return send_reply(source, ERR_NOSUCHNICK, nick);
1040 if (!(chptr = FindChannel(channel))) {
1041 return send_reply(source, ERR_NOSUCHCHANNEL, channel);
1044 * compare both users channel lists, instead of the channels user list
1045 * since the link is the same, this should be a little faster for channels
1046 * with a lot of users
1048 for (membership = cli_user(source)->channel; membership; membership = membership->next_channel) {
1049 if (chptr == membership->channel)
1052 if (0 == membership) {
1053 return send_reply(source, ERR_NOTONCHANNEL, chptr->chname);
1055 if (!IsVoicedOrOpped(membership)) {
1056 return send_reply(source, ERR_VOICENEEDED, chptr->chname);
1059 * lookup channel in destination
1061 assert(0 != cli_user(dest));
1062 for (membership = cli_user(dest)->channel; membership; membership = membership->next_channel) {
1063 if (chptr == membership->channel)
1066 if (0 == membership || IsZombie(membership)) {
1067 return send_reply(source, ERR_USERNOTINCHANNEL, cli_name(dest), chptr->chname);
1069 if (is_silenced(source, dest))
1072 if (cli_user(dest)->away)
1073 send_reply(source, RPL_AWAY, cli_name(dest), cli_user(dest)->away);
1075 sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text);
1077 sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text);
1082 /** Send a user mode change for \a cptr to neighboring servers.
1083 * @param[in] cptr User whose mode is changing.
1084 * @param[in] sptr Client who sent us the mode change message.
1085 * @param[in] old Prior set of user flags.
1086 * @param[in] prop If non-zero, also include FLAG_OPER.
1088 void send_umode_out(struct Client *cptr, struct Client *sptr,
1089 struct Flags *old, int prop)
1092 struct Client *acptr;
1094 send_umode(NULL, sptr, old, prop ? SEND_UMODES : SEND_UMODES_BUT_OPER);
1096 for (i = HighestFd; i >= 0; i--)
1098 if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
1099 (acptr != cptr) && (acptr != sptr) && *umodeBuf)
1100 sendcmdto_one(sptr, CMD_MODE, acptr, "%s :%s", cli_name(sptr), umodeBuf);
1102 if (cptr && MyUser(cptr))
1103 send_umode(cptr, sptr, old, ALL_UMODES);
1107 /** Call \a fmt for each Client named in \a names.
1108 * @param[in] sptr Client requesting information.
1109 * @param[in] names Space-delimited list of nicknames.
1110 * @param[in] rpl Base reply string for messages.
1111 * @param[in] fmt Formatting callback function.
1113 void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
1118 int users_found = 0;
1119 struct Client* acptr;
1126 mb = msgq_make(sptr, rpl_str(rpl), cli_name(&me), cli_name(sptr));
1128 for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
1129 if ((acptr = FindUser(name))) {
1131 msgq_append(0, mb, " ");
1132 (*fmt)(acptr, sptr, mb);
1134 if (5 == ++arg_count)
1137 send_buffer(sptr, mb, 0);
1141 /** Set \a flag on \a cptr and possibly hide the client's hostmask.
1142 * @param[in,out] cptr User who is getting a new flag.
1143 * @param[in] flag Some flag that affects host-hiding (FLAG_HIDDENHOST, FLAG_ACCOUNT).
1147 hide_hostmask(struct Client *cptr, unsigned int flag)
1149 struct Membership *chan;
1152 case FLAG_HIDDENHOST:
1153 /* Local users cannot set +x unless FEAT_HOST_HIDING is true. */
1154 if (MyConnect(cptr) && !feature_bool(FEAT_HOST_HIDING))
1158 /* Invalidate all bans against the user so we check them again */
1159 for (chan = (cli_user(cptr))->channel; chan;
1160 chan = chan->next_channel)
1161 ClearBanValid(chan);
1167 SetFlag(cptr, flag);
1168 if (!HasFlag(cptr, FLAG_HIDDENHOST) || !HasFlag(cptr, FLAG_ACCOUNT))
1171 sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Registered");
1172 ircd_snprintf(0, cli_user(cptr)->host, HOSTLEN, "%s.%s",
1173 cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
1175 /* ok, the client is now fully hidden, so let them know -- hikari */
1176 if (MyConnect(cptr))
1177 send_reply(cptr, RPL_HOSTHIDDEN, cli_user(cptr)->host);
1180 * Go through all channels the client was on, rejoin him
1181 * and set the modes, if any
1183 for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel)
1187 /* Send a JOIN unless the user's join has been delayed. */
1188 if (!IsDelayedJoin(chan))
1189 sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, 0,
1190 "%H", chan->channel);
1191 if (IsChanOp(chan) && HasVoice(chan))
1192 sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr, 0,
1193 "%H +ov %C %C", chan->channel, cptr,
1195 else if (IsChanOp(chan) || HasVoice(chan))
1196 sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr, 0,
1197 "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr);
1202 /** Set a user's mode. This function checks that \a cptr is trying to
1203 * set his own mode, prevents local users from setting inappropriate
1204 * modes through this function, and applies any other side effects of
1205 * a successful mode change.
1207 * @param[in,out] cptr User setting someone's mode.
1208 * @param[in] sptr Client who sent the mode change message.
1209 * @param[in] parc Number of parameters in \a parv.
1210 * @param[in] parv Parameters to MODE.
1213 int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
1217 struct Client *acptr;
1220 struct Flags setflags;
1221 unsigned int tmpmask = 0;
1222 int snomask_given = 0;
1225 int do_host_hiding = 0;
1230 return need_more_params(sptr, "MODE");
1232 if (!(acptr = FindUser(parv[1])))
1234 if (MyConnect(sptr))
1235 send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]);
1239 if (IsServer(sptr) || sptr != acptr)
1242 sendwallto_group_butone(&me, WALL_WALLOPS, 0,
1243 "MODE for User %s from %s!%s", parv[1],
1244 cli_name(cptr), cli_name(sptr));
1246 send_reply(sptr, ERR_USERSDONTMATCH);
1254 for (i = 0; i < USERMODELIST_SIZE; i++)
1256 if (HasFlag(sptr, userModeList[i].flag) &&
1257 userModeList[i].flag != FLAG_ACCOUNT)
1258 *m++ = userModeList[i].c;
1261 send_reply(sptr, RPL_UMODEIS, buf);
1262 if (HasFlag(sptr, FLAG_SERVNOTICE) && MyConnect(sptr)
1263 && cli_snomask(sptr) !=
1264 (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT))
1265 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1270 * find flags already set for user
1271 * why not just copy them?
1273 setflags = cli_flags(sptr);
1275 if (MyConnect(sptr))
1276 tmpmask = cli_snomask(sptr);
1279 * parse mode change string(s)
1281 for (p = &parv[2]; *p; p++) { /* p is changed in loop too */
1282 for (m = *p; *m; m++) {
1291 if (*(p + 1) && is_snomask(*(p + 1))) {
1293 tmpmask = umode_make_snomask(tmpmask, *++p, what);
1294 tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1297 tmpmask = (what == MODE_ADD) ?
1298 (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1300 SetServNotice(sptr);
1302 ClearServNotice(sptr);
1305 if (what == MODE_ADD)
1311 if (what == MODE_ADD)
1314 ClrFlag(sptr, FLAG_OPER);
1315 ClrFlag(sptr, FLAG_LOCOP);
1316 if (MyConnect(sptr))
1318 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1319 cli_handler(sptr) = CLIENT_HANDLER;
1324 if (what == MODE_ADD)
1328 ClrFlag(sptr, FLAG_OPER);
1329 ClrFlag(sptr, FLAG_LOCOP);
1330 if (MyConnect(sptr))
1332 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1333 cli_handler(sptr) = CLIENT_HANDLER;
1338 if (what == MODE_ADD)
1341 ClearInvisible(sptr);
1344 if (what == MODE_ADD)
1350 if (what == MODE_ADD)
1351 SetChannelService(sptr);
1353 ClearChannelService(sptr);
1356 if (what == MODE_ADD)
1362 if (what == MODE_ADD)
1366 send_reply(sptr, ERR_UMODEUNKNOWNFLAG, *m);
1372 * Evaluate rules for new user mode
1373 * Stop users making themselves operators too easily:
1375 if (!IsServer(cptr))
1377 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr))
1379 if (!FlagHas(&setflags, FLAG_LOCOP) && IsLocOp(sptr))
1382 * new umode; servers can set it, local users cannot;
1383 * prevents users from /kick'ing or /mode -o'ing
1385 if (!FlagHas(&setflags, FLAG_CHSERV))
1386 ClearChannelService(sptr);
1388 * only send wallops to opers
1390 if (feature_bool(FEAT_WALLOPS_OPER_ONLY) && !IsAnOper(sptr) &&
1391 !FlagHas(&setflags, FLAG_WALLOP))
1393 if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY) && MyConnect(sptr) &&
1394 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_SERVNOTICE))
1396 ClearServNotice(sptr);
1397 set_snomask(sptr, 0, SNO_SET);
1399 if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY) &&
1400 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_DEBUG))
1403 if (MyConnect(sptr))
1405 if ((FlagHas(&setflags, FLAG_OPER) || FlagHas(&setflags, FLAG_LOCOP)) &&
1407 det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPERATOR);
1409 if (SendServNotice(sptr))
1411 if (tmpmask != cli_snomask(sptr))
1412 set_snomask(sptr, tmpmask, SNO_SET);
1413 if (cli_snomask(sptr) && snomask_given)
1414 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1417 set_snomask(sptr, 0, SNO_SET);
1420 * Compare new flags with old flags and send string which
1421 * will cause servers to update correctly.
1423 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr))
1427 client_set_privs(sptr, NULL); /* may set propagate privilege */
1429 /* remember propagate privilege setting */
1430 if (HasPriv(sptr, PRIV_PROPAGATE))
1432 if (FlagHas(&setflags, FLAG_OPER) && !IsOper(sptr))
1434 /* user no longer oper */
1436 client_set_privs(sptr, NULL); /* will clear propagate privilege */
1438 if (FlagHas(&setflags, FLAG_INVISIBLE) && !IsInvisible(sptr))
1439 --UserStats.inv_clients;
1440 if (!FlagHas(&setflags, FLAG_INVISIBLE) && IsInvisible(sptr))
1441 ++UserStats.inv_clients;
1442 if (!FlagHas(&setflags, FLAG_HIDDENHOST) && do_host_hiding)
1443 hide_hostmask(sptr, FLAG_HIDDENHOST);
1444 send_umode_out(cptr, sptr, &setflags, prop);
1449 /** Build a mode string to describe modes for \a cptr.
1450 * @param[in] cptr Some user.
1451 * @return Pointer to a static buffer.
1453 char *umode_str(struct Client *cptr)
1455 /* Maximum string size: "owidgrx\0" */
1458 struct Flags c_flags = cli_flags(cptr);
1460 if (!HasPriv(cptr, PRIV_PROPAGATE))
1461 FlagClr(&c_flags, FLAG_OPER);
1463 for (i = 0; i < USERMODELIST_SIZE; ++i)
1465 if (FlagHas(&c_flags, userModeList[i].flag) &&
1466 userModeList[i].flag >= FLAG_GLOBAL_UMODES)
1467 *m++ = userModeList[i].c;
1470 if (IsAccount(cptr))
1472 char* t = cli_user(cptr)->account;
1475 while ((*m++ = *t++))
1478 if (cli_user(cptr)->acc_create) {
1480 Debug((DEBUG_DEBUG, "Sending timestamped account in user mode for "
1481 "account \"%s\"; timestamp %Tu", cli_user(cptr)->account,
1482 cli_user(cptr)->acc_create));
1483 ircd_snprintf(0, t = nbuf, sizeof(nbuf), ":%Tu",
1484 cli_user(cptr)->acc_create);
1485 m--; /* back up over previous nul-termination */
1486 while ((*m++ = *t++))
1493 return umodeBuf; /* Note: static buffer, gets
1494 overwritten by send_umode() */
1497 /** Send a mode change string for \a sptr to \a cptr.
1498 * @param[in] cptr Destination of mode change message.
1499 * @param[in] sptr User whose mode has changed.
1500 * @param[in] old Pre-change set of modes for \a sptr.
1501 * @param[in] sendset One of ALL_UMODES, SEND_UMODES_BUT_OPER,
1502 * SEND_UMODES, to select which changed user modes to send.
1504 void send_umode(struct Client *cptr, struct Client *sptr, struct Flags *old,
1510 int what = MODE_NULL;
1513 * Build a string in umodeBuf to represent the change in the user's
1514 * mode between the new (cli_flags(sptr)) and 'old', but skipping
1515 * the modes indicated by sendset.
1519 for (i = 0; i < USERMODELIST_SIZE; ++i)
1521 flag = userModeList[i].flag;
1522 if (FlagHas(old, flag)
1523 == HasFlag(sptr, flag))
1529 case SEND_UMODES_BUT_OPER:
1530 if (flag == FLAG_OPER)
1532 /* and fall through */
1534 if (flag < FLAG_GLOBAL_UMODES)
1538 if (FlagHas(old, flag))
1540 if (what == MODE_DEL)
1541 *m++ = userModeList[i].c;
1546 *m++ = userModeList[i].c;
1549 else /* !FlagHas(old, flag) */
1551 if (what == MODE_ADD)
1552 *m++ = userModeList[i].c;
1557 *m++ = userModeList[i].c;
1562 if (*umodeBuf && cptr)
1563 sendcmdto_one(sptr, CMD_MODE, cptr, "%s :%s", cli_name(sptr), umodeBuf);
1567 * Check to see if this resembles a sno_mask. It is if 1) there is
1568 * at least one digit and 2) The first digit occurs before the first
1569 * alphabetic character.
1570 * @param[in] word Word to check for sno_mask-ness.
1571 * @return Non-zero if \a word looks like a server notice mask; zero if not.
1573 int is_snomask(char *word)
1577 for (; *word; word++)
1580 else if (IsAlpha(*word))
1586 /** Update snomask \a oldmask according to \a arg and \a what.
1587 * @param[in] oldmask Original user mask.
1588 * @param[in] arg Update string (either a number or '+'/'-' followed by a number).
1589 * @param[in] what MODE_ADD if adding the mask.
1590 * @return New value of service notice mask.
1592 unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
1594 unsigned int sno_what;
1595 unsigned int newmask;
1599 if (what == MODE_ADD)
1604 else if (*arg == '-')
1607 if (what == MODE_ADD)
1613 sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL;
1614 /* pity we don't have strtoul everywhere */
1615 newmask = (unsigned int)atoi(arg);
1616 if (sno_what == SNO_DEL)
1617 newmask = oldmask & ~newmask;
1618 else if (sno_what == SNO_ADD)
1623 /** Remove \a cptr from the singly linked list \a list.
1624 * @param[in] cptr Client to remove from list.
1625 * @param[in,out] list Pointer to head of list containing \a cptr.
1627 static void delfrom_list(struct Client *cptr, struct SLink **list)
1630 struct SLink* prv = NULL;
1632 for (tmp = *list; tmp; tmp = tmp->next) {
1633 if (tmp->value.cptr == cptr) {
1635 prv->next = tmp->next;
1645 /** Set \a cptr's server notice mask, according to \a what.
1646 * @param[in,out] cptr Client whose snomask is updating.
1647 * @param[in] newmask Base value for new snomask.
1648 * @param[in] what One of SNO_ADD, SNO_DEL, SNO_SET, to choose operation.
1650 void set_snomask(struct Client *cptr, unsigned int newmask, int what)
1652 unsigned int oldmask, diffmask; /* unsigned please */
1656 oldmask = cli_snomask(cptr);
1658 if (what == SNO_ADD)
1660 else if (what == SNO_DEL)
1661 newmask = oldmask & ~newmask;
1662 else if (what != SNO_SET) /* absolute set, no math needed */
1663 sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what);
1665 newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
1667 diffmask = oldmask ^ newmask;
1669 for (i = 0; diffmask >> i; i++) {
1670 if (((diffmask >> i) & 1))
1672 if (((newmask >> i) & 1))
1675 tmp->next = opsarray[i];
1676 tmp->value.cptr = cptr;
1680 /* not real portable :( */
1681 delfrom_list(cptr, &opsarray[i]);
1684 cli_snomask(cptr) = newmask;
1687 /** Check whether \a sptr is allowed to send a message to \a acptr.
1688 * If \a sptr is a remote user, it means some server has an outdated
1689 * SILENCE list for \a acptr, so send the missing SILENCE mask(s) back
1690 * in the direction of \a sptr. Skip the check if \a sptr is a server.
1691 * @param[in] sptr Client trying to send a message.
1692 * @param[in] acptr Destination of message.
1693 * @return Non-zero if \a sptr is SILENCEd by \a acptr, zero if not.
1695 int is_silenced(struct Client *sptr, struct Client *acptr)
1699 size_t buf_used, slen;
1702 if (IsServer(sptr) || !(user = cli_user(acptr))
1703 || !(found = find_ban(sptr, user->silence)))
1705 assert(!(found->flags & BAN_EXCEPTION));
1706 if (!MyConnect(sptr)) {
1707 /* Buffer positive silence to send back. */
1708 buf_used = strlen(found->banstr);
1709 memcpy(buf, found->banstr, buf_used);
1710 /* Add exceptions to buffer. */
1711 for (found = user->silence; found; found = found->next) {
1712 if (!(found->flags & BAN_EXCEPTION))
1714 slen = strlen(found->banstr);
1715 if (buf_used + slen + 4 > 400) {
1716 buf[buf_used] = '\0';
1717 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf);
1721 buf[buf_used++] = ',';
1722 buf[buf_used++] = '+';
1723 buf[buf_used++] = '~';
1724 memcpy(buf + buf_used, found->banstr, slen);
1727 /* Flush silence buffer. */
1729 buf[buf_used] = '\0';
1730 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf);
1737 /** Send RPL_ISUPPORT lines to \a cptr.
1738 * @param[in] cptr Client to send ISUPPORT to.
1742 send_supported(struct Client *cptr)
1744 char featurebuf[512];
1746 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES1, FEATURESVALUES1);
1747 send_reply(cptr, RPL_ISUPPORT, featurebuf);
1748 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES2, FEATURESVALUES2);
1749 send_reply(cptr, RPL_ISUPPORT, featurebuf);
1751 return 0; /* convenience return, if it's ever needed */