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 */
62 #include "supported.h"
68 #include "handlers.h" /* m_motd and m_lusers */
70 /* #include <assert.h> -- Now using assert in ircd_log.h */
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));
96 cli_user(cptr)->refcnt = 1;
98 return cli_user(cptr);
101 /** Dereference \a user.
102 * User structures are reference-counted; if the refcount of \a user
103 * becomes zero, free it.
104 * @param[in] user User to dereference.
106 void free_user(struct User* user)
109 assert(0 < user->refcnt);
111 if (--user->refcnt == 0) {
117 assert(0 == user->joined);
118 assert(0 == user->invited);
119 assert(0 == user->channel);
127 /** Find number of User structs allocated and memory used by them.
128 * @param[out] count_out Receives number of User structs allocated.
129 * @param[out] bytes_out Receives number of bytes used by User structs.
131 void user_count_memory(size_t* count_out, size_t* bytes_out)
133 assert(0 != count_out);
134 assert(0 != bytes_out);
135 *count_out = userCount;
136 *bytes_out = userCount * sizeof(struct User);
140 /** Find the next client (starting at \a next) with a name that matches \a ch.
141 * Normal usage loop is:
142 * for (x = client; x = next_client(x,mask); x = x->next)
143 * HandleMatchingClient;
145 * @param[in] next First client to check.
146 * @param[in] ch Name mask to check against.
147 * @return Next matching client found, or NULL if none.
149 struct Client *next_client(struct Client *next, const char* ch)
151 struct Client *tmp = next;
156 next = FindClient(ch);
157 next = next ? next : tmp;
158 if (cli_prev(tmp) == next)
162 for (; next; next = cli_next(next))
163 if (!match(ch, cli_name(next)))
168 /** Find the destination server for a command, and forward it if that is not us.
170 * \a server may be a nickname, server name, server mask (if \a from
171 * is a local user) or server numnick (if \a is a server or remote
174 * @param[in] from Client that sent the command to us.
175 * @param[in] cmd Long-form command text.
176 * @param[in] tok Token-form command text.
177 * @param[in] one Client that originated the command (ignored).
178 * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH.
179 * @param[in] pattern Format string of arguments to command.
180 * @param[in] server Index of target name or mask in \a parv.
181 * @param[in] parc Number of valid elements in \a parv (must be less than 9).
182 * @param[in] parv Array of arguments to command.
183 * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS.
185 int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok,
186 struct Client *one, int MustBeOper, const char *pattern,
187 int server, int parc, char *parv[])
189 struct Client *acptr;
192 /* Assume it's me, if no server or an unregistered client */
193 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
194 return (HUNTED_ISME);
196 if (MustBeOper && !IsPrivileged(from))
198 send_reply(from, ERR_NOPRIVILEGES);
199 return HUNTED_NOSUCH;
202 /* Make sure it's a server */
204 /* Make sure it's a server */
205 if (!strchr(to, '*')) {
206 if (0 == (acptr = FindClient(to))) {
207 send_reply(from, ERR_NOSUCHSERVER, to);
208 return HUNTED_NOSUCH;
212 acptr = cli_user(acptr)->server;
213 } else if (!(acptr = find_match_server(to))) {
214 send_reply(from, ERR_NOSUCHSERVER, to);
215 return (HUNTED_NOSUCH);
217 } else if (!(acptr = FindNServer(to))) {
218 send_reply(from, SND_EXPLICIT | ERR_NOSUCHSERVER, "* :Server has disconnected");
219 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
223 return (HUNTED_ISME);
225 if (MustBeOper && !IsPrivileged(from)) {
226 send_reply(from, ERR_NOPRIVILEGES);
227 return HUNTED_NOSUCH;
230 /* assert(!IsServer(from)); */
232 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
234 sendcmdto_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
235 parv[4], parv[5], parv[6], parv[7], parv[8]);
237 return (HUNTED_PASS);
240 /** Find the destination server for a command, and forward it (as a
241 * high-priority command) if that is not us.
243 * \a server may be a nickname, server name, server mask (if \a from
244 * is a local user) or server numnick (if \a is a server or remote
246 * Unlike hunt_server_cmd(), this appends the message to the
247 * high-priority message queue for the destination server.
249 * @param[in] from Client that sent the command to us.
250 * @param[in] cmd Long-form command text.
251 * @param[in] tok Token-form command text.
252 * @param[in] one Client that originated the command (ignored).
253 * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH.
254 * @param[in] pattern Format string of arguments to command.
255 * @param[in] server Index of target name or mask in \a parv.
256 * @param[in] parc Number of valid elements in \a parv (must be less than 9).
257 * @param[in] parv Array of arguments to command.
258 * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS.
260 int hunt_server_prio_cmd(struct Client *from, const char *cmd, const char *tok,
261 struct Client *one, int MustBeOper,
262 const char *pattern, int server, int parc,
265 struct Client *acptr;
268 /* Assume it's me, if no server or an unregistered client */
269 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
270 return (HUNTED_ISME);
272 /* Make sure it's a server */
274 /* Make sure it's a server */
275 if (!strchr(to, '*')) {
276 if (0 == (acptr = FindClient(to))) {
277 send_reply(from, ERR_NOSUCHSERVER, to);
278 return HUNTED_NOSUCH;
282 acptr = cli_user(acptr)->server;
283 } else if (!(acptr = find_match_server(to))) {
284 send_reply(from, ERR_NOSUCHSERVER, to);
285 return (HUNTED_NOSUCH);
287 } else if (!(acptr = FindNServer(to)))
288 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
291 return (HUNTED_ISME);
293 if (MustBeOper && !IsPrivileged(from)) {
294 send_reply(from, ERR_NOPRIVILEGES);
295 return HUNTED_NOSUCH;
298 /* assert(!IsServer(from)); SETTIME to particular destinations permitted */
300 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
302 sendcmdto_prio_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
303 parv[4], parv[5], parv[6], parv[7], parv[8]);
305 return (HUNTED_PASS);
312 * This function is called when both NICK and USER messages
313 * have been accepted for the client, in whatever order. Only
314 * after this the USER message is propagated.
316 * NICK's must be propagated at once when received, although
317 * it would be better to delay them too until full info is
318 * available. Doing it is not so simple though, would have
319 * to implement the following:
321 * 1) user telnets in and gives only "NICK foobar" and waits
322 * 2) another user far away logs in normally with the nick
323 * "foobar" (quite legal, as this server didn't propagate it).
324 * 3) now this server gets nick "foobar" from outside, but
325 * has already the same defined locally. Current server
326 * would just issue "KILL foobar" to clean out dups. But,
327 * this is not fair. It should actually request another
328 * nick from local user or kill him/her...
330 /** Finish registering a user who has sent both NICK and USER.
331 * For local connections, possibly check IAuth; make sure there is a
332 * matching Client config block; clean the username field; check
333 * K/k-lines; check for "hacked" looking usernames; assign a numnick;
334 * and send greeting (WELCOME, ISUPPORT, MOTD, etc).
335 * For all connections, update the invisible user and operator counts;
336 * run IPcheck against their address; and forward the NICK.
338 * @param[in] cptr Client who introduced the user.
339 * @param[in,out] sptr Client who has been fully introduced.
340 * @return Zero or CPTR_KILLED.
342 int register_user(struct Client *cptr, struct Client *sptr)
346 struct User* user = cli_user(sptr);
349 user->last = CurrentTime;
350 parv[0] = cli_name(sptr);
351 parv[1] = parv[2] = NULL;
355 assert(cptr == sptr);
357 Count_unknownbecomesclient(sptr, UserStats);
360 * Set user's initial modes
362 tmpstr = (char*)client_get_default_umode(sptr);
364 char *umodev[] = { NULL, NULL, NULL, NULL };
366 set_user_mode(cptr, sptr, 3, umodev, ALLOWMODES_ANY);
370 cli_handler(sptr) = CLIENT_HANDLER;
371 SetLocalNumNick(sptr);
374 feature_str(FEAT_NETWORK),
375 feature_str(FEAT_PROVIDER) ? " via " : "",
376 feature_str(FEAT_PROVIDER) ? feature_str(FEAT_PROVIDER) : "",
379 * This is a duplicate of the NOTICE but see below...
381 send_reply(sptr, RPL_YOURHOST, cli_name(&me), version);
382 send_reply(sptr, RPL_CREATED, creation);
383 send_reply(sptr, RPL_MYINFO, cli_name(&me), version, infousermodes,
384 infochanmodes, infochanmodeswithparams);
385 send_supported(sptr);
386 m_lusers(sptr, sptr, 1, parv);
389 if (cli_snomask(sptr) & SNO_NOISY)
390 set_snomask(sptr, cli_snomask(sptr) & SNO_NOISY, SNO_ADD);
391 if (feature_bool(FEAT_CONNEXIT_NOTICES))
392 sendto_opmask_butone(0, SNO_CONNEXIT,
393 "Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s%s>",
394 cli_name(sptr), user->username, user->host,
395 cli_sock_ip(sptr), get_client_class(sptr),
396 cli_info(sptr), NumNick(cptr) /* two %s's */);
398 IPcheck_connect_succeeded(sptr);
400 if(cli_connect(sptr)->con_ssl) {
405 struct Client *acptr = user->server;
407 if (cli_from(acptr) != cli_from(sptr))
409 sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])",
410 sptr, cli_name(&me), cli_name(user->server), cli_name(cli_from(acptr)),
411 cli_sockhost(cli_from(acptr)));
412 SetFlag(sptr, FLAG_KILLED);
413 return exit_client(cptr, sptr, &me, "NICK server wrong direction");
415 else if (HasFlag(acptr, FLAG_TS8))
416 SetFlag(sptr, FLAG_TS8);
419 * Check to see if this user is being propagated
420 * as part of a net.burst, or is using protocol 9.
421 * FIXME: This can be sped up - its stupid to check it for
422 * every NICK message in a burst again --Run.
424 for (; acptr != &me; acptr = cli_serv(acptr)->up)
426 if (IsBurst(acptr) || Protocol(acptr) < 10)
429 if (!IPcheck_remote_connect(sptr, (acptr != &me)))
432 * We ran out of bits to count this
434 sendcmdto_one(&me, CMD_KILL, sptr, "%C :%s (Too many connections from your host -- Ghost)",
435 sptr, cli_name(&me));
436 return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled");
441 /* If they get both +x and an account during registration, hide
442 * their hostmask here. Calling hide_hostmask() from IAuth's
443 * account assignment causes a numeric reply during registration.
445 if (HasHiddenHost(sptr))
446 hide_hostmask(sptr, FLAG_HIDDENHOST);
447 if (IsInvisible(sptr))
448 ++UserStats.inv_clients;
452 tmpstr = umode_str(sptr);
453 /* Send full IP address to IPv6-grokking servers. */
454 sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr,
455 FLAG_IPV6, FLAG_LAST_FLAG,
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), 1),
462 NumNick(sptr), cli_info(sptr));
463 /* Send fake IPv6 addresses to pre-IPv6 servers. */
464 sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr,
465 FLAG_LAST_FLAG, FLAG_IPV6,
466 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
467 cli_name(sptr), cli_hopcount(sptr) + 1,
469 user->username, user->realhost,
470 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
471 iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 0),
472 NumNick(sptr), cli_info(sptr));
474 /* Send user mode to client */
477 static struct Flags flags; /* automatically initialized to zeros */
478 /* To avoid sending +r to the client due to auth-on-connect, set
479 * the "old" FLAG_ACCOUNT bit to match the client's value.
482 FlagSet(&flags, FLAG_ACCOUNT);
484 FlagClr(&flags, FLAG_ACCOUNT);
485 client_set_privs(sptr, NULL);
486 send_umode(cptr, sptr, &flags, ALL_UMODES);
487 if ((cli_snomask(sptr) != SNO_DEFAULT) && HasFlag(sptr, FLAG_SERVNOTICE))
488 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
493 /** List of user mode characters. */
494 static const struct UserMode {
495 unsigned int flag; /**< User mode constant. */
496 char c; /**< Character corresponding to the mode. */
500 { FLAG_INVISIBLE, 'i' },
501 { FLAG_WALLOP, 'w' },
502 { FLAG_SERVNOTICE, 's' },
504 { FLAG_CHSERV, 'k' },
506 { FLAG_ACCOUNT, 'r' },
507 { FLAG_HIDDENHOST, 'x' },
508 { FLAG_SSLCONN, 'S' }
511 /** Length of #userModeList. */
512 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
515 * XXX - find a way to get rid of this
517 /** Nasty global buffer used for communications with umode_str() and others. */
518 static char umodeBuf[BUFSIZE];
520 /** Try to set a user's nickname.
521 * If \a sptr is a server, the client is being introduced for the first time.
522 * @param[in] cptr Client to set nickname.
523 * @param[in] sptr Client sending the NICK.
524 * @param[in] nick New nickname.
525 * @param[in] parc Number of arguments to NICK.
526 * @param[in] parv Argument list to NICK.
527 * @return CPTR_KILLED if \a cptr was killed, else 0.
529 int set_nick_name(struct Client* cptr, struct Client* sptr,
530 const char* nick, int parc, char* parv[])
532 if (IsServer(sptr)) {
535 * A server introducing a new client, change source
537 struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
538 assert(0 != new_client);
540 cli_hopcount(new_client) = atoi(parv[2]);
541 cli_lastnick(new_client) = atoi(parv[3]);
546 strcpy(cli_name(new_client), nick);
547 cli_user(new_client) = make_user(new_client);
548 cli_user(new_client)->server = sptr;
549 SetRemoteNumNick(new_client, parv[parc - 2]);
551 * IP# of remote client
553 base64toip(parv[parc - 3], &cli_ip(new_client));
555 add_client_to_list(new_client);
556 hAddClient(new_client);
558 cli_serv(sptr)->ghost = 0; /* :server NICK means end of net.burst */
559 ircd_strncpy(cli_username(new_client), parv[4], USERLEN);
560 ircd_strncpy(cli_user(new_client)->username, parv[4], USERLEN);
561 ircd_strncpy(cli_user(new_client)->host, parv[5], HOSTLEN);
562 ircd_strncpy(cli_user(new_client)->realhost, parv[5], HOSTLEN);
563 ircd_strncpy(cli_info(new_client), parv[parc - 1], REALLEN);
565 Count_newremoteclient(UserStats, sptr);
567 if (parc > 7 && *parv[6] == '+') {
568 /* (parc-4) -3 for the ip, numeric nick, realname */
569 set_user_mode(cptr, new_client, parc-7, parv+4, ALLOWMODES_ANY);
572 return register_user(cptr, new_client);
574 else if ((cli_name(sptr))[0]) {
576 * Client changing its nick
578 * If the client belongs to me, then check to see
579 * if client is on any channels where it is currently
580 * banned. If so, do not allow the nick change to occur.
583 const char* channel_name;
584 struct Membership *member;
585 if ((channel_name = find_no_nickchange_channel(sptr))) {
586 return send_reply(cptr, ERR_BANNICKCHANGE, channel_name);
589 * Refuse nick change if the last nick change was less
590 * then 30 seconds ago. This is intended to get rid of
591 * clone bots doing NICK FLOOD. -SeKs
592 * If someone didn't change their nick for more then 60 seconds
593 * however, allow to do two nick changes immediately after another
594 * before limiting the nick flood. -Run
596 if (CurrentTime < cli_nextnick(cptr))
598 cli_nextnick(cptr) += 2;
599 send_reply(cptr, ERR_NICKTOOFAST, parv[1],
600 cli_nextnick(cptr) - CurrentTime);
601 /* Send error message */
602 sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cli_name(cptr));
603 /* bounce NICK to user */
604 return 0; /* ignore nick change! */
607 /* Limit total to 1 change per NICK_DELAY seconds: */
608 cli_nextnick(cptr) += NICK_DELAY;
609 /* However allow _maximal_ 1 extra consecutive nick change: */
610 if (cli_nextnick(cptr) < CurrentTime)
611 cli_nextnick(cptr) = CurrentTime;
613 /* Invalidate all bans against the user so we check them again */
614 for (member = (cli_user(cptr))->channel; member;
615 member = member->next_channel)
616 ClearBanValid(member);
619 * Also set 'lastnick' to current time, if changed.
621 if (0 != ircd_strcmp(parv[0], nick))
622 cli_lastnick(sptr) = (sptr == cptr) ? TStime() : atoi(parv[2]);
625 * Client just changing his/her nick. If he/she is
626 * on a channel, send note of change to all clients
627 * on that channel. Propagate notice to other servers.
630 sendcmdto_common_channels_butone(sptr, CMD_NICK, NULL, ":%s", nick);
631 add_history(sptr, 1);
632 sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick,
636 sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick);
638 if ((cli_name(sptr))[0])
640 strcpy(cli_name(sptr), nick);
644 /* Local client setting NICK the first time */
645 strcpy(cli_name(sptr), nick);
647 return auth_set_nick(cli_auth(sptr), nick);
652 /** Calculate the hash value for a target.
653 * @param[in] target Pointer to target, cast to unsigned int.
654 * @return Hash value constructed from the pointer.
656 static unsigned char hash_target(unsigned int target)
658 return (unsigned char) (target >> 16) ^ (target >> 8);
661 /** Records \a target as a recent target for \a sptr.
662 * @param[in] sptr User who has sent to a new target.
663 * @param[in] target Target to add.
666 add_target(struct Client *sptr, void *target)
668 /* Ok, this shouldn't work esp on alpha
670 unsigned char hash = hash_target((unsigned long) target);
671 unsigned char* targets;
674 assert(cli_local(sptr));
676 targets = cli_targets(sptr);
681 for (i = 0; i < MAXTARGETS; ++i) {
682 if (targets[i] == hash)
688 memmove(&targets[RESERVEDTARGETS + 1],
689 &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
690 targets[RESERVEDTARGETS] = hash;
693 /** Check whether \a sptr can send to or join \a target yet.
694 * @param[in] sptr User trying to join a channel or send a message.
695 * @param[in] target Target of the join or message.
696 * @param[in] name Name of the target.
697 * @param[in] created If non-zero, trying to join a new channel.
698 * @return Non-zero if too many target changes; zero if okay to send.
700 int check_target_limit(struct Client *sptr, void *target, const char *name,
703 unsigned char hash = hash_target((unsigned long) target);
705 unsigned char* targets;
708 assert(cli_local(sptr));
709 targets = cli_targets(sptr);
712 * Same target as last time?
714 if (targets[0] == hash)
716 for (i = 1; i < MAXTARGETS; ++i) {
717 if (targets[i] == hash) {
718 memmove(&targets[1], &targets[0], i);
727 if (CurrentTime < cli_nexttarget(sptr)) {
728 /* If user is invited to channel, give him/her a free target */
729 if (IsChannelName(name) && IsInvited(sptr, target))
732 if (cli_nexttarget(sptr) - CurrentTime < TARGET_DELAY + 8) {
736 cli_nexttarget(sptr) += 2;
737 send_reply(sptr, ERR_TARGETTOOFAST, name,
738 cli_nexttarget(sptr) - CurrentTime);
743 cli_nexttarget(sptr) += TARGET_DELAY;
744 if (cli_nexttarget(sptr) < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)))
745 cli_nexttarget(sptr) = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1));
748 memmove(&targets[1], &targets[0], MAXTARGETS - 1);
753 /** Allows a channel operator to avoid target change checks when
754 * sending messages to users on their channel.
755 * @param[in] source User sending the message.
756 * @param[in] nick Destination of the message.
757 * @param[in] channel Name of channel being sent to.
758 * @param[in] text Message to send.
759 * @param[in] is_notice If non-zero, use CNOTICE instead of CPRIVMSG.
761 /* Added 971023 by Run. */
762 int whisper(struct Client* source, const char* nick, const char* channel,
763 const char* text, int is_notice)
766 struct Channel* chptr;
767 struct Membership* membership;
771 assert(0 != channel);
772 assert(MyUser(source));
774 if (!(dest = FindUser(nick))) {
775 return send_reply(source, ERR_NOSUCHNICK, nick);
777 if (!(chptr = FindChannel(channel))) {
778 return send_reply(source, ERR_NOSUCHCHANNEL, channel);
781 * compare both users channel lists, instead of the channels user list
782 * since the link is the same, this should be a little faster for channels
783 * with a lot of users
785 for (membership = cli_user(source)->channel; membership; membership = membership->next_channel) {
786 if (chptr == membership->channel)
789 if (0 == membership) {
790 return send_reply(source, ERR_NOTONCHANNEL, chptr->chname);
792 if (!IsVoicedOrOpped(membership)) {
793 return send_reply(source, ERR_VOICENEEDED, chptr->chname);
796 * lookup channel in destination
798 assert(0 != cli_user(dest));
799 for (membership = cli_user(dest)->channel; membership; membership = membership->next_channel) {
800 if (chptr == membership->channel)
803 if (0 == membership || IsZombie(membership)) {
804 return send_reply(source, ERR_USERNOTINCHANNEL, cli_name(dest), chptr->chname);
806 if (is_silenced(source, dest))
810 sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text);
813 if (cli_user(dest)->away)
814 send_reply(source, RPL_AWAY, cli_name(dest), cli_user(dest)->away);
815 sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text);
821 /** Send a user mode change for \a cptr to neighboring servers.
822 * @param[in] cptr User whose mode is changing.
823 * @param[in] sptr Client who sent us the mode change message.
824 * @param[in] old Prior set of user flags.
825 * @param[in] prop If non-zero, also include FLAG_OPER.
827 void send_umode_out(struct Client *cptr, struct Client *sptr,
828 struct Flags *old, int prop)
831 struct Client *acptr;
833 send_umode(NULL, sptr, old, prop ? SEND_UMODES : SEND_UMODES_BUT_OPER);
835 for (i = HighestFd; i >= 0; i--)
837 if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
838 (acptr != cptr) && (acptr != sptr) && *umodeBuf)
839 sendcmdto_one(sptr, CMD_MODE, acptr, "%s :%s", cli_name(sptr), umodeBuf);
841 if (cptr && MyUser(cptr))
842 send_umode(cptr, sptr, old, ALL_UMODES);
846 /** Call \a fmt for each Client named in \a names.
847 * @param[in] sptr Client requesting information.
848 * @param[in] names Space-delimited list of nicknames.
849 * @param[in] rpl Base reply string for messages.
850 * @param[in] fmt Formatting callback function.
852 void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
858 struct Client* acptr;
865 mb = msgq_make(sptr, rpl_str(rpl), cli_name(&me), cli_name(sptr));
867 for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
868 if ((acptr = FindUser(name))) {
870 msgq_append(0, mb, " ");
871 (*fmt)(acptr, sptr, mb);
873 if (5 == ++arg_count)
876 send_buffer(sptr, mb, 0);
880 /** Set \a flag on \a cptr and possibly hide the client's hostmask.
881 * @param[in,out] cptr User who is getting a new flag.
882 * @param[in] flag Some flag that affects host-hiding (FLAG_HIDDENHOST, FLAG_ACCOUNT).
886 hide_hostmask(struct Client *cptr, unsigned int flag)
888 struct Membership *chan;
891 case FLAG_HIDDENHOST:
892 /* Local users cannot set +x unless FEAT_HOST_HIDING is true. */
893 if (MyConnect(cptr) && !feature_bool(FEAT_HOST_HIDING))
897 /* Invalidate all bans against the user so we check them again */
898 for (chan = (cli_user(cptr))->channel; chan;
899 chan = chan->next_channel)
907 if (!HasFlag(cptr, FLAG_HIDDENHOST) || !HasFlag(cptr, FLAG_ACCOUNT))
910 sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Registered");
911 ircd_snprintf(0, cli_user(cptr)->host, HOSTLEN, "%s.%s",
912 cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
914 /* ok, the client is now fully hidden, so let them know -- hikari */
916 send_reply(cptr, RPL_HOSTHIDDEN, cli_user(cptr)->host);
919 * Go through all channels the client was on, rejoin him
920 * and set the modes, if any
922 for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel)
926 /* Send a JOIN unless the user's join has been delayed. */
927 if (!IsDelayedJoin(chan))
928 sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, 0,
929 "%H", chan->channel);
930 if (IsChanOp(chan) && HasVoice(chan))
931 sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0,
932 "%H +ov %C %C", chan->channel, cptr,
934 else if (IsChanOp(chan) || HasVoice(chan))
935 sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0,
936 "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr);
941 /** Set a user's mode. This function checks that \a cptr is trying to
942 * set his own mode, prevents local users from setting inappropriate
943 * modes through this function, and applies any other side effects of
944 * a successful mode change.
946 * @param[in,out] cptr User setting someone's mode.
947 * @param[in] sptr Client who sent the mode change message.
948 * @param[in] parc Number of parameters in \a parv.
949 * @param[in] parv Parameters to MODE.
950 * @param[in] allow_modes ALLOWMODES_ANY for any mode, ALLOWMODES_DEFAULT for
951 * only permitting legitimate default user modes.
954 int set_user_mode(struct Client *cptr, struct Client *sptr, int parc,
955 char *parv[], int allow_modes)
961 struct Flags setflags;
962 unsigned int tmpmask = 0;
963 int snomask_given = 0;
966 int do_host_hiding = 0;
967 char* account = NULL;
975 for (i = 0; i < USERMODELIST_SIZE; i++)
977 if (HasFlag(sptr, userModeList[i].flag) &&
978 userModeList[i].flag != FLAG_ACCOUNT)
979 *m++ = userModeList[i].c;
982 send_reply(sptr, RPL_UMODEIS, buf);
983 if (HasFlag(sptr, FLAG_SERVNOTICE) && MyConnect(sptr)
984 && cli_snomask(sptr) !=
985 (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT))
986 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
991 * find flags already set for user
992 * why not just copy them?
994 setflags = cli_flags(sptr);
997 tmpmask = cli_snomask(sptr);
1000 * parse mode change string(s)
1002 for (p = &parv[2]; *p && p<&parv[parc]; p++) { /* p is changed in loop too */
1003 for (m = *p; *m; m++) {
1012 if (*(p + 1) && is_snomask(*(p + 1))) {
1014 tmpmask = umode_make_snomask(tmpmask, *++p, what);
1015 tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1018 tmpmask = (what == MODE_ADD) ?
1019 (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1021 SetServNotice(sptr);
1023 ClearServNotice(sptr);
1026 if (what == MODE_ADD)
1032 if (what == MODE_ADD)
1035 ClrFlag(sptr, FLAG_OPER);
1036 ClrFlag(sptr, FLAG_LOCOP);
1037 if (MyConnect(sptr))
1039 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1040 cli_handler(sptr) = CLIENT_HANDLER;
1045 if (what == MODE_ADD)
1049 ClrFlag(sptr, FLAG_OPER);
1050 ClrFlag(sptr, FLAG_LOCOP);
1051 if (MyConnect(sptr))
1053 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1054 cli_handler(sptr) = CLIENT_HANDLER;
1059 if (what == MODE_ADD)
1062 ClearInvisible(sptr);
1065 if (what == MODE_ADD)
1071 if (what == MODE_ADD)
1072 SetChannelService(sptr);
1074 ClearChannelService(sptr);
1077 if (what == MODE_ADD)
1083 if (what == MODE_ADD)
1086 if (what == MODE_ADD)
1093 if (*(p + 1) && (what == MODE_ADD)) {
1097 /* There is no -r */
1100 send_reply(sptr, ERR_UMODEUNKNOWNFLAG, *m);
1106 * Evaluate rules for new user mode
1107 * Stop users making themselves operators too easily:
1109 if (!IsServer(cptr))
1111 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr))
1113 if (!FlagHas(&setflags, FLAG_LOCOP) && IsLocOp(sptr))
1115 if (!FlagHas(&setflags, FLAG_ACCOUNT) && IsAccount(sptr))
1116 ClrFlag(sptr, FLAG_ACCOUNT);
1117 if (!FlagHas(&setflags, FLAG_SSLCONN) && IsSSLConn(sptr))
1118 ClrFlag(sptr, FLAG_SSLCONN);
1119 else if (FlagHas(&setflags, FLAG_SSLCONN) && !IsSSLConn(sptr))
1120 SetFlag(sptr, FLAG_SSLCONN);
1122 * new umode; servers can set it, local users cannot;
1123 * prevents users from /kick'ing or /mode -o'ing
1125 if (!FlagHas(&setflags, FLAG_CHSERV))
1126 ClearChannelService(sptr);
1128 * only send wallops to opers
1130 if (feature_bool(FEAT_WALLOPS_OPER_ONLY) && !IsAnOper(sptr) &&
1131 !FlagHas(&setflags, FLAG_WALLOP))
1133 if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY) && MyConnect(sptr) &&
1134 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_SERVNOTICE))
1136 ClearServNotice(sptr);
1137 set_snomask(sptr, 0, SNO_SET);
1139 if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY) &&
1140 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_DEBUG))
1143 if (MyConnect(sptr))
1145 if ((FlagHas(&setflags, FLAG_OPER) || FlagHas(&setflags, FLAG_LOCOP)) &&
1148 det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPERATOR);
1149 client_set_privs(sptr, NULL);
1152 if (SendServNotice(sptr))
1154 if (tmpmask != cli_snomask(sptr))
1155 set_snomask(sptr, tmpmask, SNO_SET);
1156 if (cli_snomask(sptr) && snomask_given)
1157 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1160 set_snomask(sptr, 0, SNO_SET);
1163 * Compare new flags with old flags and send string which
1164 * will cause servers to update correctly.
1166 if (!FlagHas(&setflags, FLAG_ACCOUNT) && IsAccount(sptr)) {
1167 int len = ACCOUNTLEN;
1169 if ((ts = strchr(account, ':'))) {
1170 len = (ts++) - account;
1171 cli_user(sptr)->acc_create = atoi(ts);
1172 Debug((DEBUG_DEBUG, "Received timestamped account in user mode; "
1173 "account \"%s\", timestamp %Tu", account,
1174 cli_user(sptr)->acc_create));
1176 ircd_strncpy(cli_user(sptr)->account, account, len);
1178 if (!FlagHas(&setflags, FLAG_HIDDENHOST) && do_host_hiding && allow_modes != ALLOWMODES_DEFAULT)
1179 hide_hostmask(sptr, FLAG_HIDDENHOST);
1181 if (IsRegistered(sptr)) {
1182 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr)) {
1185 client_set_privs(sptr, NULL); /* may set propagate privilege */
1187 /* remember propagate privilege setting */
1188 if (HasPriv(sptr, PRIV_PROPAGATE)) {
1191 if (FlagHas(&setflags, FLAG_OPER) && !IsOper(sptr)) {
1192 /* user no longer oper */
1193 assert(UserStats.opers > 0);
1195 client_set_privs(sptr, NULL); /* will clear propagate privilege */
1197 if (FlagHas(&setflags, FLAG_INVISIBLE) && !IsInvisible(sptr)) {
1198 assert(UserStats.inv_clients > 0);
1199 --UserStats.inv_clients;
1201 if (!FlagHas(&setflags, FLAG_INVISIBLE) && IsInvisible(sptr)) {
1202 ++UserStats.inv_clients;
1204 assert(UserStats.opers <= UserStats.clients + UserStats.unknowns);
1205 assert(UserStats.inv_clients <= UserStats.clients + UserStats.unknowns);
1206 send_umode_out(cptr, sptr, &setflags, prop);
1212 /** Build a mode string to describe modes for \a cptr.
1213 * @param[in] cptr Some user.
1214 * @return Pointer to a static buffer.
1216 char *umode_str(struct Client *cptr)
1218 /* Maximum string size: "owidgrx\0" */
1221 struct Flags c_flags = cli_flags(cptr);
1223 if (!HasPriv(cptr, PRIV_PROPAGATE))
1224 FlagClr(&c_flags, FLAG_OPER);
1226 for (i = 0; i < USERMODELIST_SIZE; ++i)
1228 if (FlagHas(&c_flags, userModeList[i].flag) &&
1229 userModeList[i].flag >= FLAG_GLOBAL_UMODES)
1230 *m++ = userModeList[i].c;
1233 if (IsAccount(cptr))
1235 char* t = cli_user(cptr)->account;
1238 while ((*m++ = *t++))
1241 if (cli_user(cptr)->acc_create) {
1243 Debug((DEBUG_DEBUG, "Sending timestamped account in user mode for "
1244 "account \"%s\"; timestamp %Tu", cli_user(cptr)->account,
1245 cli_user(cptr)->acc_create));
1246 ircd_snprintf(0, t = nbuf, sizeof(nbuf), ":%Tu",
1247 cli_user(cptr)->acc_create);
1248 m--; /* back up over previous nul-termination */
1249 while ((*m++ = *t++))
1256 return umodeBuf; /* Note: static buffer, gets
1257 overwritten by send_umode() */
1260 /** Send a mode change string for \a sptr to \a cptr.
1261 * @param[in] cptr Destination of mode change message.
1262 * @param[in] sptr User whose mode has changed.
1263 * @param[in] old Pre-change set of modes for \a sptr.
1264 * @param[in] sendset One of ALL_UMODES, SEND_UMODES_BUT_OPER,
1265 * SEND_UMODES, to select which changed user modes to send.
1267 void send_umode(struct Client *cptr, struct Client *sptr, struct Flags *old,
1273 int what = MODE_NULL;
1276 * Build a string in umodeBuf to represent the change in the user's
1277 * mode between the new (cli_flags(sptr)) and 'old', but skipping
1278 * the modes indicated by sendset.
1282 for (i = 0; i < USERMODELIST_SIZE; ++i)
1284 flag = userModeList[i].flag;
1285 if (FlagHas(old, flag)
1286 == HasFlag(sptr, flag))
1292 case SEND_UMODES_BUT_OPER:
1293 if (flag == FLAG_OPER)
1295 /* and fall through */
1297 if (flag < FLAG_GLOBAL_UMODES)
1301 if (FlagHas(old, flag))
1303 if (what == MODE_DEL)
1304 *m++ = userModeList[i].c;
1309 *m++ = userModeList[i].c;
1312 else /* !FlagHas(old, flag) */
1314 if (what == MODE_ADD)
1315 *m++ = userModeList[i].c;
1320 *m++ = userModeList[i].c;
1325 if (*umodeBuf && cptr)
1326 sendcmdto_one(sptr, CMD_MODE, cptr, "%s :%s", cli_name(sptr), umodeBuf);
1330 * Check to see if this resembles a sno_mask. It is if 1) there is
1331 * at least one digit and 2) The first digit occurs before the first
1332 * alphabetic character.
1333 * @param[in] word Word to check for sno_mask-ness.
1334 * @return Non-zero if \a word looks like a server notice mask; zero if not.
1336 int is_snomask(char *word)
1340 for (; *word; word++)
1343 else if (IsAlpha(*word))
1349 /** Update snomask \a oldmask according to \a arg and \a what.
1350 * @param[in] oldmask Original user mask.
1351 * @param[in] arg Update string (either a number or '+'/'-' followed by a number).
1352 * @param[in] what MODE_ADD if adding the mask.
1353 * @return New value of service notice mask.
1355 unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
1357 unsigned int sno_what;
1358 unsigned int newmask;
1362 if (what == MODE_ADD)
1367 else if (*arg == '-')
1370 if (what == MODE_ADD)
1376 sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL;
1377 /* pity we don't have strtoul everywhere */
1378 newmask = (unsigned int)atoi(arg);
1379 if (sno_what == SNO_DEL)
1380 newmask = oldmask & ~newmask;
1381 else if (sno_what == SNO_ADD)
1386 /** Remove \a cptr from the singly linked list \a list.
1387 * @param[in] cptr Client to remove from list.
1388 * @param[in,out] list Pointer to head of list containing \a cptr.
1390 static void delfrom_list(struct Client *cptr, struct SLink **list)
1393 struct SLink* prv = NULL;
1395 for (tmp = *list; tmp; tmp = tmp->next) {
1396 if (tmp->value.cptr == cptr) {
1398 prv->next = tmp->next;
1408 /** Set \a cptr's server notice mask, according to \a what.
1409 * @param[in,out] cptr Client whose snomask is updating.
1410 * @param[in] newmask Base value for new snomask.
1411 * @param[in] what One of SNO_ADD, SNO_DEL, SNO_SET, to choose operation.
1413 void set_snomask(struct Client *cptr, unsigned int newmask, int what)
1415 unsigned int oldmask, diffmask; /* unsigned please */
1419 oldmask = cli_snomask(cptr);
1421 if (what == SNO_ADD)
1423 else if (what == SNO_DEL)
1424 newmask = oldmask & ~newmask;
1425 else if (what != SNO_SET) /* absolute set, no math needed */
1426 sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what);
1428 newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
1430 diffmask = oldmask ^ newmask;
1432 for (i = 0; diffmask >> i; i++) {
1433 if (((diffmask >> i) & 1))
1435 if (((newmask >> i) & 1))
1438 tmp->next = opsarray[i];
1439 tmp->value.cptr = cptr;
1443 /* not real portable :( */
1444 delfrom_list(cptr, &opsarray[i]);
1447 cli_snomask(cptr) = newmask;
1450 /** Check whether \a sptr is allowed to send a message to \a acptr.
1451 * If \a sptr is a remote user, it means some server has an outdated
1452 * SILENCE list for \a acptr, so send the missing SILENCE mask(s) back
1453 * in the direction of \a sptr. Skip the check if \a sptr is a server.
1454 * @param[in] sptr Client trying to send a message.
1455 * @param[in] acptr Destination of message.
1456 * @return Non-zero if \a sptr is SILENCEd by \a acptr, zero if not.
1458 int is_silenced(struct Client *sptr, struct Client *acptr)
1462 size_t buf_used, slen;
1465 if (IsServer(sptr) || !(user = cli_user(acptr))
1466 || !(found = find_ban(sptr, user->silence)))
1468 assert(!(found->flags & BAN_EXCEPTION));
1469 if (!MyConnect(sptr)) {
1470 /* Buffer positive silence to send back. */
1471 buf_used = strlen(found->banstr);
1472 memcpy(buf, found->banstr, buf_used);
1473 /* Add exceptions to buffer. */
1474 for (found = user->silence; found; found = found->next) {
1475 if (!(found->flags & BAN_EXCEPTION))
1477 slen = strlen(found->banstr);
1478 if (buf_used + slen + 4 > 400) {
1479 buf[buf_used] = '\0';
1480 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf);
1484 buf[buf_used++] = ',';
1485 buf[buf_used++] = '+';
1486 buf[buf_used++] = '~';
1487 memcpy(buf + buf_used, found->banstr, slen);
1490 /* Flush silence buffer. */
1492 buf[buf_used] = '\0';
1493 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf);
1500 /** Send RPL_ISUPPORT lines to \a cptr.
1501 * @param[in] cptr Client to send ISUPPORT to.
1505 send_supported(struct Client *cptr)
1507 char featurebuf[512];
1509 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES1, FEATURESVALUES1);
1510 send_reply(cptr, RPL_ISUPPORT, featurebuf);
1511 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES2, FEATURESVALUES2);
1512 send_reply(cptr, RPL_ISUPPORT, featurebuf);
1514 return 0; /* convenience return, if it's ever needed */
1517 /* vim: shiftwidth=2