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_chattr.h"
38 #include "ircd_features.h"
40 #include "ircd_reply.h"
41 #include "ircd_snprintf.h"
42 #include "ircd_string.h"
51 #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));
95 cli_user(cptr)->refcnt = 1;
97 return cli_user(cptr);
100 /** Dereference \a user.
101 * User structures are reference-counted; if the refcount of \a user
102 * becomes zero, free it.
103 * @param[in] user User to dereference.
105 void free_user(struct User* user)
108 assert(0 < user->refcnt);
110 if (--user->refcnt == 0) {
116 assert(0 == user->joined);
117 assert(0 == user->invited);
118 assert(0 == user->channel);
126 /** Find number of User structs allocated and memory used by them.
127 * @param[out] count_out Receives number of User structs allocated.
128 * @param[out] bytes_out Receives number of bytes used by User structs.
130 void user_count_memory(size_t* count_out, size_t* bytes_out)
132 assert(0 != count_out);
133 assert(0 != bytes_out);
134 *count_out = userCount;
135 *bytes_out = userCount * sizeof(struct User);
139 /** Find the next client (starting at \a next) with a name that matches \a ch.
140 * Normal usage loop is:
141 * for (x = client; x = next_client(x,mask); x = x->next)
142 * HandleMatchingClient;
144 * @param[in] next First client to check.
145 * @param[in] ch Name mask to check against.
146 * @return Next matching client found, or NULL if none.
148 struct Client *next_client(struct Client *next, const char* ch)
150 struct Client *tmp = next;
155 next = FindClient(ch);
156 next = next ? next : tmp;
157 if (cli_prev(tmp) == next)
161 for (; next; next = cli_next(next))
162 if (!match(ch, cli_name(next)))
167 /** Find the destination server for a command, and forward it if that is not us.
169 * \a server may be a nickname, server name, server mask (if \a from
170 * is a local user) or server numnick (if \a is a server or remote
173 * @param[in] from Client that sent the command to us.
174 * @param[in] cmd Long-form command text.
175 * @param[in] tok Token-form command text.
176 * @param[in] one Client that originated the command (ignored).
177 * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH.
178 * @param[in] pattern Format string of arguments to command.
179 * @param[in] server Index of target name or mask in \a parv.
180 * @param[in] parc Number of valid elements in \a parv (must be less than 9).
181 * @param[in] parv Array of arguments to command.
182 * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS.
184 int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok,
185 struct Client *one, int MustBeOper, const char *pattern,
186 int server, int parc, char *parv[])
188 struct Client *acptr;
191 /* Assume it's me, if no server or an unregistered client */
192 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
193 return (HUNTED_ISME);
195 if (MustBeOper && !IsPrivileged(from))
197 send_reply(from, ERR_NOPRIVILEGES);
198 return HUNTED_NOSUCH;
201 /* Make sure it's a server */
203 /* Make sure it's a server */
204 if (!strchr(to, '*')) {
205 if (0 == (acptr = FindClient(to))) {
206 send_reply(from, ERR_NOSUCHSERVER, to);
207 return HUNTED_NOSUCH;
211 acptr = cli_user(acptr)->server;
212 } else if (!(acptr = find_match_server(to))) {
213 send_reply(from, ERR_NOSUCHSERVER, to);
214 return (HUNTED_NOSUCH);
216 } else if (!(acptr = FindNServer(to))) {
217 send_reply(from, SND_EXPLICIT | ERR_NOSUCHSERVER, "* :Server has disconnected");
218 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
222 return (HUNTED_ISME);
224 if (MustBeOper && !IsPrivileged(from)) {
225 send_reply(from, ERR_NOPRIVILEGES);
226 return HUNTED_NOSUCH;
229 /* assert(!IsServer(from)); */
231 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
233 sendcmdto_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
234 parv[4], parv[5], parv[6], parv[7], parv[8]);
236 return (HUNTED_PASS);
239 /** Find the destination server for a command, and forward it (as a
240 * high-priority command) if that is not us.
242 * \a server may be a nickname, server name, server mask (if \a from
243 * is a local user) or server numnick (if \a is a server or remote
245 * Unlike hunt_server_cmd(), this appends the message to the
246 * high-priority message queue for the destination server.
248 * @param[in] from Client that sent the command to us.
249 * @param[in] cmd Long-form command text.
250 * @param[in] tok Token-form command text.
251 * @param[in] one Client that originated the command (ignored).
252 * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH.
253 * @param[in] pattern Format string of arguments to command.
254 * @param[in] server Index of target name or mask in \a parv.
255 * @param[in] parc Number of valid elements in \a parv (must be less than 9).
256 * @param[in] parv Array of arguments to command.
257 * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS.
259 int hunt_server_prio_cmd(struct Client *from, const char *cmd, const char *tok,
260 struct Client *one, int MustBeOper,
261 const char *pattern, int server, int parc,
264 struct Client *acptr;
267 /* Assume it's me, if no server or an unregistered client */
268 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
269 return (HUNTED_ISME);
271 /* Make sure it's a server */
273 /* Make sure it's a server */
274 if (!strchr(to, '*')) {
275 if (0 == (acptr = FindClient(to))) {
276 send_reply(from, ERR_NOSUCHSERVER, to);
277 return HUNTED_NOSUCH;
281 acptr = cli_user(acptr)->server;
282 } else if (!(acptr = find_match_server(to))) {
283 send_reply(from, ERR_NOSUCHSERVER, to);
284 return (HUNTED_NOSUCH);
286 } else if (!(acptr = FindNServer(to)))
287 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
290 return (HUNTED_ISME);
292 if (MustBeOper && !IsPrivileged(from)) {
293 send_reply(from, ERR_NOPRIVILEGES);
294 return HUNTED_NOSUCH;
297 /* assert(!IsServer(from)); SETTIME to particular destinations permitted */
299 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
301 sendcmdto_prio_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
302 parv[4], parv[5], parv[6], parv[7], parv[8]);
304 return (HUNTED_PASS);
311 * This function is called when both NICK and USER messages
312 * have been accepted for the client, in whatever order. Only
313 * after this the USER message is propagated.
315 * NICK's must be propagated at once when received, although
316 * it would be better to delay them too until full info is
317 * available. Doing it is not so simple though, would have
318 * to implement the following:
320 * 1) user telnets in and gives only "NICK foobar" and waits
321 * 2) another user far away logs in normally with the nick
322 * "foobar" (quite legal, as this server didn't propagate it).
323 * 3) now this server gets nick "foobar" from outside, but
324 * has already the same defined locally. Current server
325 * would just issue "KILL foobar" to clean out dups. But,
326 * this is not fair. It should actually request another
327 * nick from local user or kill him/her...
329 /** Finish registering a user who has sent both NICK and USER.
330 * For local connections, possibly check IAuth; make sure there is a
331 * matching Client config block; clean the username field; check
332 * K/k-lines; check for "hacked" looking usernames; assign a numnick;
333 * and send greeting (WELCOME, ISUPPORT, MOTD, etc).
334 * For all connections, update the invisible user and operator counts;
335 * run IPcheck against their address; and forward the NICK.
337 * @param[in] cptr Client who introduced the user.
338 * @param[in,out] sptr Client who has been fully introduced.
339 * @return Zero or CPTR_KILLED.
341 int register_user(struct Client *cptr, struct Client *sptr)
345 struct User* user = cli_user(sptr);
348 user->last = CurrentTime;
349 parv[0] = cli_name(sptr);
350 parv[1] = parv[2] = NULL;
354 assert(cptr == sptr);
356 Count_unknownbecomesclient(sptr, UserStats);
359 cli_handler(sptr) = CLIENT_HANDLER;
360 SetLocalNumNick(sptr);
363 feature_str(FEAT_NETWORK),
364 feature_str(FEAT_PROVIDER) ? " via " : "",
365 feature_str(FEAT_PROVIDER) ? feature_str(FEAT_PROVIDER) : "",
368 * This is a duplicate of the NOTICE but see below...
370 send_reply(sptr, RPL_YOURHOST, cli_name(&me), version);
371 send_reply(sptr, RPL_CREATED, creation);
372 send_reply(sptr, RPL_MYINFO, cli_name(&me), version, infousermodes,
373 infochanmodes, infochanmodeswithparams);
374 send_supported(sptr);
375 m_lusers(sptr, sptr, 1, parv);
378 if (cli_snomask(sptr) & SNO_NOISY)
379 set_snomask(sptr, cli_snomask(sptr) & SNO_NOISY, SNO_ADD);
380 if (feature_bool(FEAT_CONNEXIT_NOTICES))
381 sendto_opmask_butone(0, SNO_CONNEXIT,
382 "Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s%s>",
383 cli_name(sptr), user->username, user->host,
384 cli_sock_ip(sptr), get_client_class(sptr),
385 cli_info(sptr), NumNick(cptr) /* two %s's */);
387 IPcheck_connect_succeeded(sptr);
389 * Set user's initial modes
391 tmpstr = (char*)client_get_default_umode(sptr);
393 char *umodev[] = { NULL, NULL, NULL, NULL };
395 set_user_mode(cptr, sptr, 3, umodev, ALLOWMODES_ANY);
399 struct Client *acptr = user->server;
401 if (cli_from(acptr) != cli_from(sptr))
403 sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])",
404 sptr, cli_name(&me), cli_name(user->server), cli_name(cli_from(acptr)),
405 cli_sockhost(cli_from(acptr)));
406 SetFlag(sptr, FLAG_KILLED);
407 return exit_client(cptr, sptr, &me, "NICK server wrong direction");
409 else if (HasFlag(acptr, FLAG_TS8))
410 SetFlag(sptr, FLAG_TS8);
413 * Check to see if this user is being propagated
414 * as part of a net.burst, or is using protocol 9.
415 * FIXME: This can be sped up - its stupid to check it for
416 * every NICK message in a burst again --Run.
418 for (; acptr != &me; acptr = cli_serv(acptr)->up)
420 if (IsBurst(acptr) || Protocol(acptr) < 10)
423 if (!IPcheck_remote_connect(sptr, (acptr != &me)))
426 * We ran out of bits to count this
428 sendcmdto_one(&me, CMD_KILL, sptr, "%C :%s (Too many connections from your host -- Ghost)",
429 sptr, cli_name(&me));
430 return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled");
435 /* If they get both +x and an account during registration, hide
436 * their hostmask here. Calling hide_hostmask() from IAuth's
437 * account assignment causes a numeric reply during registration.
439 if (HasHiddenHost(sptr))
440 hide_hostmask(sptr, FLAG_HIDDENHOST);
442 tmpstr = umode_str(sptr);
443 /* Send full IP address to IPv6-grokking servers. */
444 sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr,
445 FLAG_IPV6, FLAG_LAST_FLAG,
446 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
447 cli_name(sptr), cli_hopcount(sptr) + 1,
449 user->username, user->realhost,
450 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
451 iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 1),
452 NumNick(sptr), cli_info(sptr));
453 /* Send fake IPv6 addresses to pre-IPv6 servers. */
454 sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr,
455 FLAG_LAST_FLAG, FLAG_IPV6,
456 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
457 cli_name(sptr), cli_hopcount(sptr) + 1,
459 user->username, user->realhost,
460 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
461 iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 0),
462 NumNick(sptr), cli_info(sptr));
464 /* Send user mode to client */
467 static struct Flags flags; /* automatically initialized to zeros */
468 /* To avoid sending +r to the client due to auth-on-connect, set
469 * the "old" FLAG_ACCOUNT bit to match the client's value.
472 FlagSet(&flags, FLAG_ACCOUNT);
474 FlagClr(&flags, FLAG_ACCOUNT);
475 client_set_privs(sptr, NULL);
476 send_umode(cptr, sptr, &flags, ALL_UMODES);
477 if ((cli_snomask(sptr) != SNO_DEFAULT) && HasFlag(sptr, FLAG_SERVNOTICE))
478 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
483 /** List of user mode characters. */
484 static const struct UserMode {
485 unsigned int flag; /**< User mode constant. */
486 char c; /**< Character corresponding to the mode. */
490 { FLAG_INVISIBLE, 'i' },
491 { FLAG_WALLOP, 'w' },
492 { FLAG_SERVNOTICE, 's' },
494 { FLAG_CHSERV, 'k' },
496 { FLAG_ACCOUNT, 'r' },
497 { FLAG_HIDDENHOST, 'x' }
500 /** Length of #userModeList. */
501 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
504 * XXX - find a way to get rid of this
506 /** Nasty global buffer used for communications with umode_str() and others. */
507 static char umodeBuf[BUFSIZE];
509 /** Try to set a user's nickname.
510 * If \a sptr is a server, the client is being introduced for the first time.
511 * @param[in] cptr Client to set nickname.
512 * @param[in] sptr Client sending the NICK.
513 * @param[in] nick New nickname.
514 * @param[in] parc Number of arguments to NICK.
515 * @param[in] parv Argument list to NICK.
516 * @return CPTR_KILLED if \a cptr was killed, else 0.
518 int set_nick_name(struct Client* cptr, struct Client* sptr,
519 const char* nick, int parc, char* parv[])
521 if (IsServer(sptr)) {
524 * A server introducing a new client, change source
526 struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
527 assert(0 != new_client);
529 cli_hopcount(new_client) = atoi(parv[2]);
530 cli_lastnick(new_client) = atoi(parv[3]);
535 strcpy(cli_name(new_client), nick);
536 cli_user(new_client) = make_user(new_client);
537 cli_user(new_client)->server = sptr;
538 SetRemoteNumNick(new_client, parv[parc - 2]);
540 * IP# of remote client
542 base64toip(parv[parc - 3], &cli_ip(new_client));
544 add_client_to_list(new_client);
545 hAddClient(new_client);
547 cli_serv(sptr)->ghost = 0; /* :server NICK means end of net.burst */
548 ircd_strncpy(cli_username(new_client), parv[4], USERLEN);
549 ircd_strncpy(cli_user(new_client)->username, parv[4], USERLEN);
550 ircd_strncpy(cli_user(new_client)->host, parv[5], HOSTLEN);
551 ircd_strncpy(cli_user(new_client)->realhost, parv[5], HOSTLEN);
552 ircd_strncpy(cli_info(new_client), parv[parc - 1], REALLEN);
554 Count_newremoteclient(UserStats, sptr);
556 if (parc > 7 && *parv[6] == '+') {
557 /* (parc-4) -3 for the ip, numeric nick, realname */
558 set_user_mode(cptr, new_client, parc-7, parv+4, ALLOWMODES_ANY);
561 return register_user(cptr, new_client);
563 else if ((cli_name(sptr))[0]) {
565 * Client changing its nick
567 * If the client belongs to me, then check to see
568 * if client is on any channels where it is currently
569 * banned. If so, do not allow the nick change to occur.
572 const char* channel_name;
573 struct Membership *member;
574 if ((channel_name = find_no_nickchange_channel(sptr))) {
575 return send_reply(cptr, ERR_BANNICKCHANGE, channel_name);
578 * Refuse nick change if the last nick change was less
579 * then 30 seconds ago. This is intended to get rid of
580 * clone bots doing NICK FLOOD. -SeKs
581 * If someone didn't change their nick for more then 60 seconds
582 * however, allow to do two nick changes immediately after another
583 * before limiting the nick flood. -Run
585 if (CurrentTime < cli_nextnick(cptr))
587 cli_nextnick(cptr) += 2;
588 send_reply(cptr, ERR_NICKTOOFAST, parv[1],
589 cli_nextnick(cptr) - CurrentTime);
590 /* Send error message */
591 sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cli_name(cptr));
592 /* bounce NICK to user */
593 return 0; /* ignore nick change! */
596 /* Limit total to 1 change per NICK_DELAY seconds: */
597 cli_nextnick(cptr) += NICK_DELAY;
598 /* However allow _maximal_ 1 extra consecutive nick change: */
599 if (cli_nextnick(cptr) < CurrentTime)
600 cli_nextnick(cptr) = CurrentTime;
602 /* Invalidate all bans against the user so we check them again */
603 for (member = (cli_user(cptr))->channel; member;
604 member = member->next_channel)
605 ClearBanValid(member);
608 * Also set 'lastnick' to current time, if changed.
610 if (0 != ircd_strcmp(parv[0], nick))
611 cli_lastnick(sptr) = (sptr == cptr) ? TStime() : atoi(parv[2]);
614 * Client just changing his/her nick. If he/she is
615 * on a channel, send note of change to all clients
616 * on that channel. Propagate notice to other servers.
619 sendcmdto_common_channels_butone(sptr, CMD_NICK, NULL, ":%s", nick);
620 add_history(sptr, 1);
621 sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick,
625 sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick);
627 if ((cli_name(sptr))[0])
629 strcpy(cli_name(sptr), nick);
633 /* Local client setting NICK the first time */
634 strcpy(cli_name(sptr), nick);
636 return auth_set_nick(cli_auth(sptr), nick);
641 /** Calculate the hash value for a target.
642 * @param[in] target Pointer to target, cast to unsigned int.
643 * @return Hash value constructed from the pointer.
645 static unsigned char hash_target(unsigned int target)
647 return (unsigned char) (target >> 16) ^ (target >> 8);
650 /** Records \a target as a recent target for \a sptr.
651 * @param[in] sptr User who has sent to a new target.
652 * @param[in] target Target to add.
655 add_target(struct Client *sptr, void *target)
657 /* Ok, this shouldn't work esp on alpha
659 unsigned char hash = hash_target((unsigned long) target);
660 unsigned char* targets;
663 assert(cli_local(sptr));
665 targets = cli_targets(sptr);
670 for (i = 0; i < MAXTARGETS; ++i) {
671 if (targets[i] == hash)
677 memmove(&targets[RESERVEDTARGETS + 1],
678 &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
679 targets[RESERVEDTARGETS] = hash;
682 /** Check whether \a sptr can send to or join \a target yet.
683 * @param[in] sptr User trying to join a channel or send a message.
684 * @param[in] target Target of the join or message.
685 * @param[in] name Name of the target.
686 * @param[in] created If non-zero, trying to join a new channel.
687 * @return Non-zero if too many target changes; zero if okay to send.
689 int check_target_limit(struct Client *sptr, void *target, const char *name,
692 unsigned char hash = hash_target((unsigned long) target);
694 unsigned char* targets;
697 assert(cli_local(sptr));
698 targets = cli_targets(sptr);
701 * Same target as last time?
703 if (targets[0] == hash)
705 for (i = 1; i < MAXTARGETS; ++i) {
706 if (targets[i] == hash) {
707 memmove(&targets[1], &targets[0], i);
716 if (CurrentTime < cli_nexttarget(sptr)) {
717 /* If user is invited to channel, give him/her a free target */
718 if (IsChannelName(name) && IsInvited(sptr, target))
721 if (cli_nexttarget(sptr) - CurrentTime < TARGET_DELAY + 8) {
725 cli_nexttarget(sptr) += 2;
726 send_reply(sptr, ERR_TARGETTOOFAST, name,
727 cli_nexttarget(sptr) - CurrentTime);
732 cli_nexttarget(sptr) += TARGET_DELAY;
733 if (cli_nexttarget(sptr) < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)))
734 cli_nexttarget(sptr) = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1));
737 memmove(&targets[1], &targets[0], MAXTARGETS - 1);
742 /** Allows a channel operator to avoid target change checks when
743 * sending messages to users on their channel.
744 * @param[in] source User sending the message.
745 * @param[in] nick Destination of the message.
746 * @param[in] channel Name of channel being sent to.
747 * @param[in] text Message to send.
748 * @param[in] is_notice If non-zero, use CNOTICE instead of CPRIVMSG.
750 /* Added 971023 by Run. */
751 int whisper(struct Client* source, const char* nick, const char* channel,
752 const char* text, int is_notice)
755 struct Channel* chptr;
756 struct Membership* membership;
760 assert(0 != channel);
761 assert(MyUser(source));
763 if (!(dest = FindUser(nick))) {
764 return send_reply(source, ERR_NOSUCHNICK, nick);
766 if (!(chptr = FindChannel(channel))) {
767 return send_reply(source, ERR_NOSUCHCHANNEL, channel);
770 * compare both users channel lists, instead of the channels user list
771 * since the link is the same, this should be a little faster for channels
772 * with a lot of users
774 for (membership = cli_user(source)->channel; membership; membership = membership->next_channel) {
775 if (chptr == membership->channel)
778 if (0 == membership) {
779 return send_reply(source, ERR_NOTONCHANNEL, chptr->chname);
781 if (!IsVoicedOrOpped(membership)) {
782 return send_reply(source, ERR_VOICENEEDED, chptr->chname);
785 * lookup channel in destination
787 assert(0 != cli_user(dest));
788 for (membership = cli_user(dest)->channel; membership; membership = membership->next_channel) {
789 if (chptr == membership->channel)
792 if (0 == membership || IsZombie(membership)) {
793 return send_reply(source, ERR_USERNOTINCHANNEL, cli_name(dest), chptr->chname);
795 if (is_silenced(source, dest))
799 sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text);
802 if (cli_user(dest)->away)
803 send_reply(source, RPL_AWAY, cli_name(dest), cli_user(dest)->away);
804 sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text);
810 /** Send a user mode change for \a cptr to neighboring servers.
811 * @param[in] cptr User whose mode is changing.
812 * @param[in] sptr Client who sent us the mode change message.
813 * @param[in] old Prior set of user flags.
814 * @param[in] prop If non-zero, also include FLAG_OPER.
816 void send_umode_out(struct Client *cptr, struct Client *sptr,
817 struct Flags *old, int prop)
820 struct Client *acptr;
822 send_umode(NULL, sptr, old, prop ? SEND_UMODES : SEND_UMODES_BUT_OPER);
824 for (i = HighestFd; i >= 0; i--)
826 if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
827 (acptr != cptr) && (acptr != sptr) && *umodeBuf)
828 sendcmdto_one(sptr, CMD_MODE, acptr, "%s :%s", cli_name(sptr), umodeBuf);
830 if (cptr && MyUser(cptr))
831 send_umode(cptr, sptr, old, ALL_UMODES);
835 /** Call \a fmt for each Client named in \a names.
836 * @param[in] sptr Client requesting information.
837 * @param[in] names Space-delimited list of nicknames.
838 * @param[in] rpl Base reply string for messages.
839 * @param[in] fmt Formatting callback function.
841 void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
847 struct Client* acptr;
854 mb = msgq_make(sptr, rpl_str(rpl), cli_name(&me), cli_name(sptr));
856 for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
857 if ((acptr = FindUser(name))) {
859 msgq_append(0, mb, " ");
860 (*fmt)(acptr, sptr, mb);
862 if (5 == ++arg_count)
865 send_buffer(sptr, mb, 0);
869 /** Set \a flag on \a cptr and possibly hide the client's hostmask.
870 * @param[in,out] cptr User who is getting a new flag.
871 * @param[in] flag Some flag that affects host-hiding (FLAG_HIDDENHOST, FLAG_ACCOUNT).
875 hide_hostmask(struct Client *cptr, unsigned int flag)
877 struct Membership *chan;
880 case FLAG_HIDDENHOST:
881 /* Local users cannot set +x unless FEAT_HOST_HIDING is true. */
882 if (MyConnect(cptr) && !feature_bool(FEAT_HOST_HIDING))
886 /* Invalidate all bans against the user so we check them again */
887 for (chan = (cli_user(cptr))->channel; chan;
888 chan = chan->next_channel)
896 if (!HasFlag(cptr, FLAG_HIDDENHOST) || !HasFlag(cptr, FLAG_ACCOUNT))
899 sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Registered");
900 ircd_snprintf(0, cli_user(cptr)->host, HOSTLEN, "%s.%s",
901 cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
903 /* ok, the client is now fully hidden, so let them know -- hikari */
905 send_reply(cptr, RPL_HOSTHIDDEN, cli_user(cptr)->host);
908 * Go through all channels the client was on, rejoin him
909 * and set the modes, if any
911 for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel)
915 /* Send a JOIN unless the user's join has been delayed. */
916 if (!IsDelayedJoin(chan))
917 sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, 0,
918 "%H", chan->channel);
919 if (IsChanOp(chan) && HasVoice(chan))
920 sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0,
921 "%H +ov %C %C", chan->channel, cptr,
923 else if (IsChanOp(chan) || HasVoice(chan))
924 sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0,
925 "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr);
930 /** Set a user's mode. This function checks that \a cptr is trying to
931 * set his own mode, prevents local users from setting inappropriate
932 * modes through this function, and applies any other side effects of
933 * a successful mode change.
935 * @param[in,out] cptr User setting someone's mode.
936 * @param[in] sptr Client who sent the mode change message.
937 * @param[in] parc Number of parameters in \a parv.
938 * @param[in] parv Parameters to MODE.
939 * @param[in] allow_modes ALLOWMODES_ANY for any mode, ALLOWMODES_DEFAULT for
940 * only permitting legitimate default user modes.
943 int set_user_mode(struct Client *cptr, struct Client *sptr, int parc,
944 char *parv[], int allow_modes)
950 struct Flags setflags;
951 unsigned int tmpmask = 0;
952 int snomask_given = 0;
955 int do_host_hiding = 0;
956 char* account = NULL;
964 for (i = 0; i < USERMODELIST_SIZE; i++)
966 if (HasFlag(sptr, userModeList[i].flag) &&
967 userModeList[i].flag != FLAG_ACCOUNT)
968 *m++ = userModeList[i].c;
971 send_reply(sptr, RPL_UMODEIS, buf);
972 if (HasFlag(sptr, FLAG_SERVNOTICE) && MyConnect(sptr)
973 && cli_snomask(sptr) !=
974 (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT))
975 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
980 * find flags already set for user
981 * why not just copy them?
983 setflags = cli_flags(sptr);
986 tmpmask = cli_snomask(sptr);
989 * parse mode change string(s)
991 for (p = &parv[2]; *p && p<&parv[parc]; p++) { /* p is changed in loop too */
992 for (m = *p; *m; m++) {
1001 if (*(p + 1) && is_snomask(*(p + 1))) {
1003 tmpmask = umode_make_snomask(tmpmask, *++p, what);
1004 tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1007 tmpmask = (what == MODE_ADD) ?
1008 (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1010 SetServNotice(sptr);
1012 ClearServNotice(sptr);
1015 if (what == MODE_ADD)
1021 if (what == MODE_ADD)
1024 ClrFlag(sptr, FLAG_OPER);
1025 ClrFlag(sptr, FLAG_LOCOP);
1026 if (MyConnect(sptr))
1028 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1029 cli_handler(sptr) = CLIENT_HANDLER;
1034 if (what == MODE_ADD)
1038 ClrFlag(sptr, FLAG_OPER);
1039 ClrFlag(sptr, FLAG_LOCOP);
1040 if (MyConnect(sptr))
1042 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1043 cli_handler(sptr) = CLIENT_HANDLER;
1048 if (what == MODE_ADD)
1051 ClearInvisible(sptr);
1054 if (what == MODE_ADD)
1060 if (what == MODE_ADD)
1061 SetChannelService(sptr);
1063 ClearChannelService(sptr);
1066 if (what == MODE_ADD)
1072 if (what == MODE_ADD)
1076 if (*(p + 1) && (what == MODE_ADD)) {
1080 /* There is no -r */
1083 send_reply(sptr, ERR_UMODEUNKNOWNFLAG, *m);
1089 * Evaluate rules for new user mode
1090 * Stop users making themselves operators too easily:
1092 if (!IsServer(cptr))
1094 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr))
1096 if (!FlagHas(&setflags, FLAG_LOCOP) && IsLocOp(sptr))
1098 if (!FlagHas(&setflags, FLAG_ACCOUNT) && IsAccount(sptr))
1099 ClrFlag(sptr, FLAG_ACCOUNT);
1101 * new umode; servers can set it, local users cannot;
1102 * prevents users from /kick'ing or /mode -o'ing
1104 if (!FlagHas(&setflags, FLAG_CHSERV))
1105 ClearChannelService(sptr);
1107 * only send wallops to opers
1109 if (feature_bool(FEAT_WALLOPS_OPER_ONLY) && !IsAnOper(sptr) &&
1110 !FlagHas(&setflags, FLAG_WALLOP))
1112 if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY) && MyConnect(sptr) &&
1113 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_SERVNOTICE))
1115 ClearServNotice(sptr);
1116 set_snomask(sptr, 0, SNO_SET);
1118 if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY) &&
1119 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_DEBUG))
1122 if (MyConnect(sptr))
1124 if ((FlagHas(&setflags, FLAG_OPER) || FlagHas(&setflags, FLAG_LOCOP)) &&
1126 det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPERATOR);
1128 if (SendServNotice(sptr))
1130 if (tmpmask != cli_snomask(sptr))
1131 set_snomask(sptr, tmpmask, SNO_SET);
1132 if (cli_snomask(sptr) && snomask_given)
1133 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1136 set_snomask(sptr, 0, SNO_SET);
1139 * Compare new flags with old flags and send string which
1140 * will cause servers to update correctly.
1142 if (!FlagHas(&setflags, FLAG_ACCOUNT) && IsAccount(sptr)) {
1143 int len = ACCOUNTLEN;
1145 if ((ts = strchr(account, ':'))) {
1146 len = (ts++) - account;
1147 cli_user(sptr)->acc_create = atoi(ts);
1148 Debug((DEBUG_DEBUG, "Received timestamped account in user mode; "
1149 "account \"%s\", timestamp %Tu", account,
1150 cli_user(sptr)->acc_create));
1152 ircd_strncpy(cli_user(sptr)->account, account, len);
1154 if (!FlagHas(&setflags, FLAG_HIDDENHOST) && do_host_hiding && allow_modes != ALLOWMODES_DEFAULT)
1155 hide_hostmask(sptr, FLAG_HIDDENHOST);
1157 if (IsRegistered(sptr)) {
1158 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr)) {
1161 client_set_privs(sptr, NULL); /* may set propagate privilege */
1163 /* remember propagate privilege setting */
1164 if (HasPriv(sptr, PRIV_PROPAGATE)) {
1167 if (FlagHas(&setflags, FLAG_OPER) && !IsOper(sptr)) {
1168 /* user no longer oper */
1169 assert(UserStats.opers > 0);
1171 client_set_privs(sptr, NULL); /* will clear propagate privilege */
1173 if (FlagHas(&setflags, FLAG_INVISIBLE) && !IsInvisible(sptr)) {
1174 assert(UserStats.inv_clients > 0);
1175 --UserStats.inv_clients;
1177 if (!FlagHas(&setflags, FLAG_INVISIBLE) && IsInvisible(sptr)) {
1178 ++UserStats.inv_clients;
1180 assert(UserStats.opers <= UserStats.clients + UserStats.unknowns);
1181 assert(UserStats.inv_clients <= UserStats.clients + UserStats.unknowns);
1182 send_umode_out(cptr, sptr, &setflags, prop);
1188 /** Build a mode string to describe modes for \a cptr.
1189 * @param[in] cptr Some user.
1190 * @return Pointer to a static buffer.
1192 char *umode_str(struct Client *cptr)
1194 /* Maximum string size: "owidgrx\0" */
1197 struct Flags c_flags = cli_flags(cptr);
1199 if (!HasPriv(cptr, PRIV_PROPAGATE))
1200 FlagClr(&c_flags, FLAG_OPER);
1202 for (i = 0; i < USERMODELIST_SIZE; ++i)
1204 if (FlagHas(&c_flags, userModeList[i].flag) &&
1205 userModeList[i].flag >= FLAG_GLOBAL_UMODES)
1206 *m++ = userModeList[i].c;
1209 if (IsAccount(cptr))
1211 char* t = cli_user(cptr)->account;
1214 while ((*m++ = *t++))
1217 if (cli_user(cptr)->acc_create) {
1219 Debug((DEBUG_DEBUG, "Sending timestamped account in user mode for "
1220 "account \"%s\"; timestamp %Tu", cli_user(cptr)->account,
1221 cli_user(cptr)->acc_create));
1222 ircd_snprintf(0, t = nbuf, sizeof(nbuf), ":%Tu",
1223 cli_user(cptr)->acc_create);
1224 m--; /* back up over previous nul-termination */
1225 while ((*m++ = *t++))
1232 return umodeBuf; /* Note: static buffer, gets
1233 overwritten by send_umode() */
1236 /** Send a mode change string for \a sptr to \a cptr.
1237 * @param[in] cptr Destination of mode change message.
1238 * @param[in] sptr User whose mode has changed.
1239 * @param[in] old Pre-change set of modes for \a sptr.
1240 * @param[in] sendset One of ALL_UMODES, SEND_UMODES_BUT_OPER,
1241 * SEND_UMODES, to select which changed user modes to send.
1243 void send_umode(struct Client *cptr, struct Client *sptr, struct Flags *old,
1249 int what = MODE_NULL;
1252 * Build a string in umodeBuf to represent the change in the user's
1253 * mode between the new (cli_flags(sptr)) and 'old', but skipping
1254 * the modes indicated by sendset.
1258 for (i = 0; i < USERMODELIST_SIZE; ++i)
1260 flag = userModeList[i].flag;
1261 if (FlagHas(old, flag)
1262 == HasFlag(sptr, flag))
1268 case SEND_UMODES_BUT_OPER:
1269 if (flag == FLAG_OPER)
1271 /* and fall through */
1273 if (flag < FLAG_GLOBAL_UMODES)
1277 if (FlagHas(old, flag))
1279 if (what == MODE_DEL)
1280 *m++ = userModeList[i].c;
1285 *m++ = userModeList[i].c;
1288 else /* !FlagHas(old, flag) */
1290 if (what == MODE_ADD)
1291 *m++ = userModeList[i].c;
1296 *m++ = userModeList[i].c;
1301 if (*umodeBuf && cptr)
1302 sendcmdto_one(sptr, CMD_MODE, cptr, "%s :%s", cli_name(sptr), umodeBuf);
1306 * Check to see if this resembles a sno_mask. It is if 1) there is
1307 * at least one digit and 2) The first digit occurs before the first
1308 * alphabetic character.
1309 * @param[in] word Word to check for sno_mask-ness.
1310 * @return Non-zero if \a word looks like a server notice mask; zero if not.
1312 int is_snomask(char *word)
1316 for (; *word; word++)
1319 else if (IsAlpha(*word))
1325 /** Update snomask \a oldmask according to \a arg and \a what.
1326 * @param[in] oldmask Original user mask.
1327 * @param[in] arg Update string (either a number or '+'/'-' followed by a number).
1328 * @param[in] what MODE_ADD if adding the mask.
1329 * @return New value of service notice mask.
1331 unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
1333 unsigned int sno_what;
1334 unsigned int newmask;
1338 if (what == MODE_ADD)
1343 else if (*arg == '-')
1346 if (what == MODE_ADD)
1352 sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL;
1353 /* pity we don't have strtoul everywhere */
1354 newmask = (unsigned int)atoi(arg);
1355 if (sno_what == SNO_DEL)
1356 newmask = oldmask & ~newmask;
1357 else if (sno_what == SNO_ADD)
1362 /** Remove \a cptr from the singly linked list \a list.
1363 * @param[in] cptr Client to remove from list.
1364 * @param[in,out] list Pointer to head of list containing \a cptr.
1366 static void delfrom_list(struct Client *cptr, struct SLink **list)
1369 struct SLink* prv = NULL;
1371 for (tmp = *list; tmp; tmp = tmp->next) {
1372 if (tmp->value.cptr == cptr) {
1374 prv->next = tmp->next;
1384 /** Set \a cptr's server notice mask, according to \a what.
1385 * @param[in,out] cptr Client whose snomask is updating.
1386 * @param[in] newmask Base value for new snomask.
1387 * @param[in] what One of SNO_ADD, SNO_DEL, SNO_SET, to choose operation.
1389 void set_snomask(struct Client *cptr, unsigned int newmask, int what)
1391 unsigned int oldmask, diffmask; /* unsigned please */
1395 oldmask = cli_snomask(cptr);
1397 if (what == SNO_ADD)
1399 else if (what == SNO_DEL)
1400 newmask = oldmask & ~newmask;
1401 else if (what != SNO_SET) /* absolute set, no math needed */
1402 sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what);
1404 newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
1406 diffmask = oldmask ^ newmask;
1408 for (i = 0; diffmask >> i; i++) {
1409 if (((diffmask >> i) & 1))
1411 if (((newmask >> i) & 1))
1414 tmp->next = opsarray[i];
1415 tmp->value.cptr = cptr;
1419 /* not real portable :( */
1420 delfrom_list(cptr, &opsarray[i]);
1423 cli_snomask(cptr) = newmask;
1426 /** Check whether \a sptr is allowed to send a message to \a acptr.
1427 * If \a sptr is a remote user, it means some server has an outdated
1428 * SILENCE list for \a acptr, so send the missing SILENCE mask(s) back
1429 * in the direction of \a sptr. Skip the check if \a sptr is a server.
1430 * @param[in] sptr Client trying to send a message.
1431 * @param[in] acptr Destination of message.
1432 * @return Non-zero if \a sptr is SILENCEd by \a acptr, zero if not.
1434 int is_silenced(struct Client *sptr, struct Client *acptr)
1438 size_t buf_used, slen;
1441 if (IsServer(sptr) || !(user = cli_user(acptr))
1442 || !(found = find_ban(sptr, user->silence)))
1444 assert(!(found->flags & BAN_EXCEPTION));
1445 if (!MyConnect(sptr)) {
1446 /* Buffer positive silence to send back. */
1447 buf_used = strlen(found->banstr);
1448 memcpy(buf, found->banstr, buf_used);
1449 /* Add exceptions to buffer. */
1450 for (found = user->silence; found; found = found->next) {
1451 if (!(found->flags & BAN_EXCEPTION))
1453 slen = strlen(found->banstr);
1454 if (buf_used + slen + 4 > 400) {
1455 buf[buf_used] = '\0';
1456 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf);
1460 buf[buf_used++] = ',';
1461 buf[buf_used++] = '+';
1462 buf[buf_used++] = '~';
1463 memcpy(buf + buf_used, found->banstr, slen);
1466 /* Flush silence buffer. */
1468 buf[buf_used] = '\0';
1469 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf);
1476 /** Send RPL_ISUPPORT lines to \a cptr.
1477 * @param[in] cptr Client to send ISUPPORT to.
1481 send_supported(struct Client *cptr)
1483 char featurebuf[512];
1485 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES1, FEATURESVALUES1);
1486 send_reply(cptr, RPL_ISUPPORT, featurebuf);
1487 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES2, FEATURESVALUES2);
1488 send_reply(cptr, RPL_ISUPPORT, featurebuf);
1490 return 0; /* convenience return, if it's ever needed */
1493 /* vim: shiftwidth=2