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);
125 /** Find number of User structs allocated and memory used by them.
126 * @param[out] count_out Receives number of User structs allocated.
127 * @param[out] bytes_out Receives number of bytes used by User structs.
129 void user_count_memory(size_t* count_out, size_t* bytes_out)
131 assert(0 != count_out);
132 assert(0 != bytes_out);
133 *count_out = userCount;
134 *bytes_out = userCount * sizeof(struct User);
138 /** Find the next client (starting at \a next) with a name that matches \a ch.
139 * Normal usage loop is:
140 * for (x = client; x = next_client(x,mask); x = x->next)
141 * HandleMatchingClient;
143 * @param[in] next First client to check.
144 * @param[in] ch Name mask to check against.
145 * @return Next matching client found, or NULL if none.
147 struct Client *next_client(struct Client *next, const char* ch)
149 struct Client *tmp = next;
154 next = FindClient(ch);
155 next = next ? next : tmp;
156 if (cli_prev(tmp) == next)
160 for (; next; next = cli_next(next))
161 if (!match(ch, cli_name(next)))
166 /** Find the destination server for a command, and forward it if that is not us.
168 * \a server may be a nickname, server name, server mask (if \a from
169 * is a local user) or server numnick (if \a is a server or remote
172 * @param[in] from Client that sent the command to us.
173 * @param[in] cmd Long-form command text.
174 * @param[in] tok Token-form command text.
175 * @param[in] one Client that originated the command (ignored).
176 * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH.
177 * @param[in] pattern Format string of arguments to command.
178 * @param[in] server Index of target name or mask in \a parv.
179 * @param[in] parc Number of valid elements in \a parv (must be less than 9).
180 * @param[in] parv Array of arguments to command.
181 * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS.
183 int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok,
184 struct Client *one, int MustBeOper, const char *pattern,
185 int server, int parc, char *parv[])
187 struct Client *acptr;
190 /* Assume it's me, if no server or an unregistered client */
191 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
192 return (HUNTED_ISME);
194 if (MustBeOper && !IsPrivileged(from))
196 send_reply(from, ERR_NOPRIVILEGES);
197 return HUNTED_NOSUCH;
200 /* Make sure it's a server */
202 /* Make sure it's a server */
203 if (!strchr(to, '*')) {
204 if (0 == (acptr = FindClient(to))) {
205 send_reply(from, ERR_NOSUCHSERVER, to);
206 return HUNTED_NOSUCH;
210 acptr = cli_user(acptr)->server;
211 } else if (!(acptr = find_match_server(to))) {
212 send_reply(from, ERR_NOSUCHSERVER, to);
213 return (HUNTED_NOSUCH);
215 } else if (!(acptr = FindNServer(to))) {
216 send_reply(from, SND_EXPLICIT | ERR_NOSUCHSERVER, "* :Server has disconnected");
217 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
221 return (HUNTED_ISME);
223 if (MustBeOper && !IsPrivileged(from)) {
224 send_reply(from, ERR_NOPRIVILEGES);
225 return HUNTED_NOSUCH;
228 /* assert(!IsServer(from)); */
230 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
232 sendcmdto_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
233 parv[4], parv[5], parv[6], parv[7], parv[8]);
235 return (HUNTED_PASS);
238 /** Find the destination server for a command, and forward it (as a
239 * high-priority command) if that is not us.
241 * \a server may be a nickname, server name, server mask (if \a from
242 * is a local user) or server numnick (if \a is a server or remote
244 * Unlike hunt_server_cmd(), this appends the message to the
245 * high-priority message queue for the destination server.
247 * @param[in] from Client that sent the command to us.
248 * @param[in] cmd Long-form command text.
249 * @param[in] tok Token-form command text.
250 * @param[in] one Client that originated the command (ignored).
251 * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH.
252 * @param[in] pattern Format string of arguments to command.
253 * @param[in] server Index of target name or mask in \a parv.
254 * @param[in] parc Number of valid elements in \a parv (must be less than 9).
255 * @param[in] parv Array of arguments to command.
256 * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS.
258 int hunt_server_prio_cmd(struct Client *from, const char *cmd, const char *tok,
259 struct Client *one, int MustBeOper,
260 const char *pattern, int server, int parc,
263 struct Client *acptr;
266 /* Assume it's me, if no server or an unregistered client */
267 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
268 return (HUNTED_ISME);
270 /* Make sure it's a server */
272 /* Make sure it's a server */
273 if (!strchr(to, '*')) {
274 if (0 == (acptr = FindClient(to))) {
275 send_reply(from, ERR_NOSUCHSERVER, to);
276 return HUNTED_NOSUCH;
280 acptr = cli_user(acptr)->server;
281 } else if (!(acptr = find_match_server(to))) {
282 send_reply(from, ERR_NOSUCHSERVER, to);
283 return (HUNTED_NOSUCH);
285 } else if (!(acptr = FindNServer(to)))
286 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
289 return (HUNTED_ISME);
291 if (MustBeOper && !IsPrivileged(from)) {
292 send_reply(from, ERR_NOPRIVILEGES);
293 return HUNTED_NOSUCH;
296 /* assert(!IsServer(from)); SETTIME to particular destinations permitted */
298 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
300 sendcmdto_prio_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
301 parv[4], parv[5], parv[6], parv[7], parv[8]);
303 return (HUNTED_PASS);
310 * This function is called when both NICK and USER messages
311 * have been accepted for the client, in whatever order. Only
312 * after this the USER message is propagated.
314 * NICK's must be propagated at once when received, although
315 * it would be better to delay them too until full info is
316 * available. Doing it is not so simple though, would have
317 * to implement the following:
319 * 1) user telnets in and gives only "NICK foobar" and waits
320 * 2) another user far away logs in normally with the nick
321 * "foobar" (quite legal, as this server didn't propagate it).
322 * 3) now this server gets nick "foobar" from outside, but
323 * has already the same defined locally. Current server
324 * would just issue "KILL foobar" to clean out dups. But,
325 * this is not fair. It should actually request another
326 * nick from local user or kill him/her...
328 /** Finish registering a user who has sent both NICK and USER.
329 * For local connections, possibly check IAuth; make sure there is a
330 * matching Client config block; clean the username field; check
331 * K/k-lines; check for "hacked" looking usernames; assign a numnick;
332 * and send greeting (WELCOME, ISUPPORT, MOTD, etc).
333 * For all connections, update the invisible user and operator counts;
334 * run IPcheck against their address; and forward the NICK.
336 * @param[in] cptr Client who introduced the user.
337 * @param[in,out] sptr Client who has been fully introduced.
338 * @return Zero or CPTR_KILLED.
340 int register_user(struct Client *cptr, struct Client *sptr)
344 struct User* user = cli_user(sptr);
347 user->last = CurrentTime;
348 parv[0] = cli_name(sptr);
349 parv[1] = parv[2] = NULL;
353 assert(cptr == sptr);
355 Count_unknownbecomesclient(sptr, UserStats);
358 cli_handler(sptr) = CLIENT_HANDLER;
359 SetLocalNumNick(sptr);
362 feature_str(FEAT_NETWORK),
363 feature_str(FEAT_PROVIDER) ? " via " : "",
364 feature_str(FEAT_PROVIDER) ? feature_str(FEAT_PROVIDER) : "",
367 * This is a duplicate of the NOTICE but see below...
369 send_reply(sptr, RPL_YOURHOST, cli_name(&me), version);
370 send_reply(sptr, RPL_CREATED, creation);
371 send_reply(sptr, RPL_MYINFO, cli_name(&me), version, infousermodes,
372 infochanmodes, infochanmodeswithparams);
373 send_supported(sptr);
374 m_lusers(sptr, sptr, 1, parv);
377 if (cli_snomask(sptr) & SNO_NOISY)
378 set_snomask(sptr, cli_snomask(sptr) & SNO_NOISY, SNO_ADD);
379 if (feature_bool(FEAT_CONNEXIT_NOTICES))
380 sendto_opmask_butone(0, SNO_CONNEXIT,
381 "Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s%s>",
382 cli_name(sptr), user->username, user->host,
383 cli_sock_ip(sptr), get_client_class(sptr),
384 cli_info(sptr), NumNick(cptr) /* two %s's */);
386 IPcheck_connect_succeeded(sptr);
388 * Set user's initial modes
390 tmpstr = (char*)client_get_default_umode(sptr);
391 if (tmpstr) for (; *tmpstr; ++tmpstr) {
394 if (!feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY)) {
396 set_snomask(sptr, SNO_DEFAULT, SNO_SET);
400 if (!feature_bool(FEAT_WALLOPS_OPER_ONLY))
410 if (!feature_bool(FEAT_HIS_DEBUG_OPER_ONLY))
417 struct Client *acptr = user->server;
419 Count_newremoteclient(UserStats, acptr);
421 if (cli_from(acptr) != cli_from(sptr))
423 sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])",
424 sptr, cli_name(&me), cli_name(user->server), cli_name(cli_from(acptr)),
425 cli_sockhost(cli_from(acptr)));
426 SetFlag(sptr, FLAG_KILLED);
427 return exit_client(cptr, sptr, &me, "NICK server wrong direction");
429 else if (HasFlag(acptr, FLAG_TS8))
430 SetFlag(sptr, FLAG_TS8);
433 * Check to see if this user is being propagated
434 * as part of a net.burst, or is using protocol 9.
435 * FIXME: This can be sped up - its stupid to check it for
436 * every NICK message in a burst again --Run.
438 for (; acptr != &me; acptr = cli_serv(acptr)->up)
440 if (IsBurst(acptr) || Protocol(acptr) < 10)
443 if (!IPcheck_remote_connect(sptr, (acptr != &me)))
446 * We ran out of bits to count this
448 sendcmdto_one(&me, CMD_KILL, sptr, "%C :%s (Too many connections from your host -- Ghost)",
449 sptr, cli_name(&me));
450 return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled");
455 if (IsInvisible(sptr))
456 ++UserStats.inv_clients;
459 /* If they get both +x and an account during registration, hide
460 * their hostmask here. Calling hide_hostmask() from IAuth's
461 * account assignment causes a numeric reply during registration.
463 if (HasHiddenHost(sptr))
464 hide_hostmask(sptr, FLAG_HIDDENHOST);
466 tmpstr = umode_str(sptr);
467 /* Send full IP address to IPv6-grokking servers. */
468 sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr,
469 FLAG_IPV6, FLAG_LAST_FLAG,
470 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
471 cli_name(sptr), cli_hopcount(sptr) + 1,
473 user->username, user->realhost,
474 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
475 iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 1),
476 NumNick(sptr), cli_info(sptr));
477 /* Send fake IPv6 addresses to pre-IPv6 servers. */
478 sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr,
479 FLAG_LAST_FLAG, FLAG_IPV6,
480 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
481 cli_name(sptr), cli_hopcount(sptr) + 1,
483 user->username, user->realhost,
484 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
485 iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 0),
486 NumNick(sptr), cli_info(sptr));
488 /* Send user mode to client */
491 static struct Flags flags; /* automatically initialized to zeros */
492 /* To avoid sending +r to the client due to auth-on-connect, set
493 * the "old" FLAG_ACCOUNT bit to match the client's value.
496 FlagSet(&flags, FLAG_ACCOUNT);
498 FlagClr(&flags, FLAG_ACCOUNT);
499 send_umode(cptr, sptr, &flags, ALL_UMODES);
500 if ((cli_snomask(sptr) != SNO_DEFAULT) && HasFlag(sptr, FLAG_SERVNOTICE))
501 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
506 /** List of user mode characters. */
507 static const struct UserMode {
508 unsigned int flag; /**< User mode constant. */
509 char c; /**< Character corresponding to the mode. */
513 { FLAG_INVISIBLE, 'i' },
514 { FLAG_WALLOP, 'w' },
515 { FLAG_SERVNOTICE, 's' },
517 { FLAG_CHSERV, 'k' },
519 { FLAG_ACCOUNT, 'r' },
520 { FLAG_HIDDENHOST, 'x' }
523 /** Length of #userModeList. */
524 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
527 * XXX - find a way to get rid of this
529 /** Nasty global buffer used for communications with umode_str() and others. */
530 static char umodeBuf[BUFSIZE];
532 /** Try to set a user's nickname.
533 * If \a sptr is a server, the client is being introduced for the first time.
534 * @param[in] cptr Client to set nickname.
535 * @param[in] sptr Client sending the NICK.
536 * @param[in] nick New nickname.
537 * @param[in] parc Number of arguments to NICK.
538 * @param[in] parv Argument list to NICK.
539 * @return CPTR_KILLED if \a cptr was killed, else 0.
541 int set_nick_name(struct Client* cptr, struct Client* sptr,
542 const char* nick, int parc, char* parv[])
544 if (IsServer(sptr)) {
546 const char* account = 0;
550 * A server introducing a new client, change source
552 struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
553 assert(0 != new_client);
555 cli_hopcount(new_client) = atoi(parv[2]);
556 cli_lastnick(new_client) = atoi(parv[3]);
557 if (Protocol(cptr) > 9 && parc > 7 && *parv[6] == '+')
559 for (p = parv[6] + 1; *p; p++)
561 for (i = 0; i < USERMODELIST_SIZE; ++i)
563 if (userModeList[i].c == *p)
565 SetFlag(new_client, userModeList[i].flag);
566 if (userModeList[i].flag == FLAG_ACCOUNT)
573 client_set_privs(new_client, NULL); /* set privs on user */
577 strcpy(cli_name(new_client), nick);
578 cli_user(new_client) = make_user(new_client);
579 cli_user(new_client)->server = sptr;
580 SetRemoteNumNick(new_client, parv[parc - 2]);
582 * IP# of remote client
584 base64toip(parv[parc - 3], &cli_ip(new_client));
586 add_client_to_list(new_client);
587 hAddClient(new_client);
589 cli_serv(sptr)->ghost = 0; /* :server NICK means end of net.burst */
590 ircd_strncpy(cli_username(new_client), parv[4], USERLEN);
591 ircd_strncpy(cli_user(new_client)->username, parv[4], USERLEN);
592 ircd_strncpy(cli_user(new_client)->host, parv[5], HOSTLEN);
593 ircd_strncpy(cli_user(new_client)->realhost, parv[5], HOSTLEN);
594 ircd_strncpy(cli_info(new_client), parv[parc - 1], REALLEN);
596 int len = ACCOUNTLEN;
597 if ((p = strchr(account, ':'))) {
598 len = (p++) - account;
599 cli_user(new_client)->acc_create = atoi(p);
600 Debug((DEBUG_DEBUG, "Received timestamped account in user mode; "
601 "account \"%s\", timestamp %Tu", account,
602 cli_user(new_client)->acc_create));
604 ircd_strncpy(cli_user(new_client)->account, account, len);
606 if (HasHiddenHost(new_client))
607 ircd_snprintf(0, cli_user(new_client)->host, HOSTLEN, "%s.%s",
608 cli_user(new_client)->account,
609 feature_str(FEAT_HIDDEN_HOST));
611 return register_user(cptr, new_client);
613 else if ((cli_name(sptr))[0]) {
615 * Client changing its nick
617 * If the client belongs to me, then check to see
618 * if client is on any channels where it is currently
619 * banned. If so, do not allow the nick change to occur.
622 const char* channel_name;
623 struct Membership *member;
624 if ((channel_name = find_no_nickchange_channel(sptr))) {
625 return send_reply(cptr, ERR_BANNICKCHANGE, channel_name);
628 * Refuse nick change if the last nick change was less
629 * then 30 seconds ago. This is intended to get rid of
630 * clone bots doing NICK FLOOD. -SeKs
631 * If someone didn't change their nick for more then 60 seconds
632 * however, allow to do two nick changes immediately after another
633 * before limiting the nick flood. -Run
635 if (CurrentTime < cli_nextnick(cptr))
637 cli_nextnick(cptr) += 2;
638 send_reply(cptr, ERR_NICKTOOFAST, parv[1],
639 cli_nextnick(cptr) - CurrentTime);
640 /* Send error message */
641 sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cli_name(cptr));
642 /* bounce NICK to user */
643 return 0; /* ignore nick change! */
646 /* Limit total to 1 change per NICK_DELAY seconds: */
647 cli_nextnick(cptr) += NICK_DELAY;
648 /* However allow _maximal_ 1 extra consecutive nick change: */
649 if (cli_nextnick(cptr) < CurrentTime)
650 cli_nextnick(cptr) = CurrentTime;
652 /* Invalidate all bans against the user so we check them again */
653 for (member = (cli_user(cptr))->channel; member;
654 member = member->next_channel)
655 ClearBanValid(member);
658 * Also set 'lastnick' to current time, if changed.
660 if (0 != ircd_strcmp(parv[0], nick))
661 cli_lastnick(sptr) = (sptr == cptr) ? TStime() : atoi(parv[2]);
664 * Client just changing his/her nick. If he/she is
665 * on a channel, send note of change to all clients
666 * on that channel. Propagate notice to other servers.
669 sendcmdto_common_channels_butone(sptr, CMD_NICK, NULL, ":%s", nick);
670 add_history(sptr, 1);
671 sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick,
675 sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick);
677 if ((cli_name(sptr))[0])
679 strcpy(cli_name(sptr), nick);
683 /* Local client setting NICK the first time */
684 strcpy(cli_name(sptr), nick);
686 return auth_set_nick(cli_auth(sptr), nick);
691 /** Calculate the hash value for a target.
692 * @param[in] target Pointer to target, cast to unsigned int.
693 * @return Hash value constructed from the pointer.
695 static unsigned char hash_target(unsigned int target)
697 return (unsigned char) (target >> 16) ^ (target >> 8);
700 /** Records \a target as a recent target for \a sptr.
701 * @param[in] sptr User who has sent to a new target.
702 * @param[in] target Target to add.
705 add_target(struct Client *sptr, void *target)
707 /* Ok, this shouldn't work esp on alpha
709 unsigned char hash = hash_target((unsigned long) target);
710 unsigned char* targets;
713 assert(cli_local(sptr));
715 targets = cli_targets(sptr);
720 for (i = 0; i < MAXTARGETS; ++i) {
721 if (targets[i] == hash)
727 memmove(&targets[RESERVEDTARGETS + 1],
728 &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
729 targets[RESERVEDTARGETS] = hash;
732 /** Check whether \a sptr can send to or join \a target yet.
733 * @param[in] sptr User trying to join a channel or send a message.
734 * @param[in] target Target of the join or message.
735 * @param[in] name Name of the target.
736 * @param[in] created If non-zero, trying to join a new channel.
737 * @return Non-zero if too many target changes; zero if okay to send.
739 int check_target_limit(struct Client *sptr, void *target, const char *name,
742 unsigned char hash = hash_target((unsigned long) target);
744 unsigned char* targets;
747 assert(cli_local(sptr));
748 targets = cli_targets(sptr);
750 /* If user is invited to channel, give him/her a free target */
751 if (IsChannelName(name) && IsInvited(sptr, target))
755 * Same target as last time?
757 if (targets[0] == hash)
759 for (i = 1; i < MAXTARGETS; ++i) {
760 if (targets[i] == hash) {
761 memmove(&targets[1], &targets[0], i);
770 if (CurrentTime < cli_nexttarget(sptr)) {
771 if (cli_nexttarget(sptr) - CurrentTime < TARGET_DELAY + 8) {
775 cli_nexttarget(sptr) += 2;
776 send_reply(sptr, ERR_TARGETTOOFAST, name,
777 cli_nexttarget(sptr) - CurrentTime);
782 cli_nexttarget(sptr) += TARGET_DELAY;
783 if (cli_nexttarget(sptr) < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)))
784 cli_nexttarget(sptr) = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1));
787 memmove(&targets[1], &targets[0], MAXTARGETS - 1);
792 /** Allows a channel operator to avoid target change checks when
793 * sending messages to users on their channel.
794 * @param[in] source User sending the message.
795 * @param[in] nick Destination of the message.
796 * @param[in] channel Name of channel being sent to.
797 * @param[in] text Message to send.
798 * @param[in] is_notice If non-zero, use CNOTICE instead of CPRIVMSG.
800 /* Added 971023 by Run. */
801 int whisper(struct Client* source, const char* nick, const char* channel,
802 const char* text, int is_notice)
805 struct Channel* chptr;
806 struct Membership* membership;
810 assert(0 != channel);
811 assert(MyUser(source));
813 if (!(dest = FindUser(nick))) {
814 return send_reply(source, ERR_NOSUCHNICK, nick);
816 if (!(chptr = FindChannel(channel))) {
817 return send_reply(source, ERR_NOSUCHCHANNEL, channel);
820 * compare both users channel lists, instead of the channels user list
821 * since the link is the same, this should be a little faster for channels
822 * with a lot of users
824 for (membership = cli_user(source)->channel; membership; membership = membership->next_channel) {
825 if (chptr == membership->channel)
828 if (0 == membership) {
829 return send_reply(source, ERR_NOTONCHANNEL, chptr->chname);
831 if (!IsVoicedOrOpped(membership)) {
832 return send_reply(source, ERR_VOICENEEDED, chptr->chname);
835 * lookup channel in destination
837 assert(0 != cli_user(dest));
838 for (membership = cli_user(dest)->channel; membership; membership = membership->next_channel) {
839 if (chptr == membership->channel)
842 if (0 == membership || IsZombie(membership)) {
843 return send_reply(source, ERR_USERNOTINCHANNEL, cli_name(dest), chptr->chname);
845 if (is_silenced(source, dest))
848 if (cli_user(dest)->away)
849 send_reply(source, RPL_AWAY, cli_name(dest), cli_user(dest)->away);
851 sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text);
853 sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text);
858 /** Send a user mode change for \a cptr to neighboring servers.
859 * @param[in] cptr User whose mode is changing.
860 * @param[in] sptr Client who sent us the mode change message.
861 * @param[in] old Prior set of user flags.
862 * @param[in] prop If non-zero, also include FLAG_OPER.
864 void send_umode_out(struct Client *cptr, struct Client *sptr,
865 struct Flags *old, int prop)
868 struct Client *acptr;
870 send_umode(NULL, sptr, old, prop ? SEND_UMODES : SEND_UMODES_BUT_OPER);
872 for (i = HighestFd; i >= 0; i--)
874 if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
875 (acptr != cptr) && (acptr != sptr) && *umodeBuf)
876 sendcmdto_one(sptr, CMD_MODE, acptr, "%s :%s", cli_name(sptr), umodeBuf);
878 if (cptr && MyUser(cptr))
879 send_umode(cptr, sptr, old, ALL_UMODES);
883 /** Call \a fmt for each Client named in \a names.
884 * @param[in] sptr Client requesting information.
885 * @param[in] names Space-delimited list of nicknames.
886 * @param[in] rpl Base reply string for messages.
887 * @param[in] fmt Formatting callback function.
889 void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
895 struct Client* acptr;
902 mb = msgq_make(sptr, rpl_str(rpl), cli_name(&me), cli_name(sptr));
904 for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
905 if ((acptr = FindUser(name))) {
907 msgq_append(0, mb, " ");
908 (*fmt)(acptr, sptr, mb);
910 if (5 == ++arg_count)
913 send_buffer(sptr, mb, 0);
917 /** Set \a flag on \a cptr and possibly hide the client's hostmask.
918 * @param[in,out] cptr User who is getting a new flag.
919 * @param[in] flag Some flag that affects host-hiding (FLAG_HIDDENHOST, FLAG_ACCOUNT).
923 hide_hostmask(struct Client *cptr, unsigned int flag)
925 struct Membership *chan;
928 case FLAG_HIDDENHOST:
929 /* Local users cannot set +x unless FEAT_HOST_HIDING is true. */
930 if (MyConnect(cptr) && !feature_bool(FEAT_HOST_HIDING))
934 /* Invalidate all bans against the user so we check them again */
935 for (chan = (cli_user(cptr))->channel; chan;
936 chan = chan->next_channel)
944 if (!HasFlag(cptr, FLAG_HIDDENHOST) || !HasFlag(cptr, FLAG_ACCOUNT))
947 sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Registered");
948 ircd_snprintf(0, cli_user(cptr)->host, HOSTLEN, "%s.%s",
949 cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
951 /* ok, the client is now fully hidden, so let them know -- hikari */
953 send_reply(cptr, RPL_HOSTHIDDEN, cli_user(cptr)->host);
956 * Go through all channels the client was on, rejoin him
957 * and set the modes, if any
959 for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel)
963 /* Send a JOIN unless the user's join has been delayed. */
964 if (!IsDelayedJoin(chan))
965 sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, 0,
966 "%H", chan->channel);
967 if (IsChanOp(chan) && HasVoice(chan))
968 sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0,
969 "%H +ov %C %C", chan->channel, cptr,
971 else if (IsChanOp(chan) || HasVoice(chan))
972 sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0,
973 "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr);
978 /** Set a user's mode. This function checks that \a cptr is trying to
979 * set his own mode, prevents local users from setting inappropriate
980 * modes through this function, and applies any other side effects of
981 * a successful mode change.
983 * @param[in,out] cptr User setting someone's mode.
984 * @param[in] sptr Client who sent the mode change message.
985 * @param[in] parc Number of parameters in \a parv.
986 * @param[in] parv Parameters to MODE.
989 int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
995 struct Flags setflags;
996 unsigned int tmpmask = 0;
997 int snomask_given = 0;
1000 int do_host_hiding = 0;
1008 for (i = 0; i < USERMODELIST_SIZE; i++)
1010 if (HasFlag(sptr, userModeList[i].flag) &&
1011 userModeList[i].flag != FLAG_ACCOUNT)
1012 *m++ = userModeList[i].c;
1015 send_reply(sptr, RPL_UMODEIS, buf);
1016 if (HasFlag(sptr, FLAG_SERVNOTICE) && MyConnect(sptr)
1017 && cli_snomask(sptr) !=
1018 (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT))
1019 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1024 * find flags already set for user
1025 * why not just copy them?
1027 setflags = cli_flags(sptr);
1029 if (MyConnect(sptr))
1030 tmpmask = cli_snomask(sptr);
1033 * parse mode change string(s)
1035 for (p = &parv[2]; *p; p++) { /* p is changed in loop too */
1036 for (m = *p; *m; m++) {
1045 if (*(p + 1) && is_snomask(*(p + 1))) {
1047 tmpmask = umode_make_snomask(tmpmask, *++p, what);
1048 tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1051 tmpmask = (what == MODE_ADD) ?
1052 (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1054 SetServNotice(sptr);
1056 ClearServNotice(sptr);
1059 if (what == MODE_ADD)
1065 if (what == MODE_ADD)
1068 ClrFlag(sptr, FLAG_OPER);
1069 ClrFlag(sptr, FLAG_LOCOP);
1070 if (MyConnect(sptr))
1072 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1073 cli_handler(sptr) = CLIENT_HANDLER;
1078 if (what == MODE_ADD)
1082 ClrFlag(sptr, FLAG_OPER);
1083 ClrFlag(sptr, FLAG_LOCOP);
1084 if (MyConnect(sptr))
1086 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1087 cli_handler(sptr) = CLIENT_HANDLER;
1092 if (what == MODE_ADD)
1095 ClearInvisible(sptr);
1098 if (what == MODE_ADD)
1104 if (what == MODE_ADD)
1105 SetChannelService(sptr);
1107 ClearChannelService(sptr);
1110 if (what == MODE_ADD)
1116 if (what == MODE_ADD)
1120 send_reply(sptr, ERR_UMODEUNKNOWNFLAG, *m);
1126 * Evaluate rules for new user mode
1127 * Stop users making themselves operators too easily:
1129 if (!IsServer(cptr))
1131 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr))
1133 if (!FlagHas(&setflags, FLAG_LOCOP) && IsLocOp(sptr))
1136 * new umode; servers can set it, local users cannot;
1137 * prevents users from /kick'ing or /mode -o'ing
1139 if (!FlagHas(&setflags, FLAG_CHSERV))
1140 ClearChannelService(sptr);
1142 * only send wallops to opers
1144 if (feature_bool(FEAT_WALLOPS_OPER_ONLY) && !IsAnOper(sptr) &&
1145 !FlagHas(&setflags, FLAG_WALLOP))
1147 if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY) && MyConnect(sptr) &&
1148 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_SERVNOTICE))
1150 ClearServNotice(sptr);
1151 set_snomask(sptr, 0, SNO_SET);
1153 if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY) &&
1154 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_DEBUG))
1157 if (MyConnect(sptr))
1159 if ((FlagHas(&setflags, FLAG_OPER) || FlagHas(&setflags, FLAG_LOCOP)) &&
1161 det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPERATOR);
1163 if (SendServNotice(sptr))
1165 if (tmpmask != cli_snomask(sptr))
1166 set_snomask(sptr, tmpmask, SNO_SET);
1167 if (cli_snomask(sptr) && snomask_given)
1168 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1171 set_snomask(sptr, 0, SNO_SET);
1174 * Compare new flags with old flags and send string which
1175 * will cause servers to update correctly.
1177 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr))
1181 client_set_privs(sptr, NULL); /* may set propagate privilege */
1183 /* remember propagate privilege setting */
1184 if (HasPriv(sptr, PRIV_PROPAGATE))
1186 if (FlagHas(&setflags, FLAG_OPER) && !IsOper(sptr))
1188 /* user no longer oper */
1190 client_set_privs(sptr, NULL); /* will clear propagate privilege */
1192 if (FlagHas(&setflags, FLAG_INVISIBLE) && !IsInvisible(sptr))
1193 --UserStats.inv_clients;
1194 if (!FlagHas(&setflags, FLAG_INVISIBLE) && IsInvisible(sptr))
1195 ++UserStats.inv_clients;
1196 if (!FlagHas(&setflags, FLAG_HIDDENHOST) && do_host_hiding)
1197 hide_hostmask(sptr, FLAG_HIDDENHOST);
1198 if (IsRegistered(sptr))
1199 send_umode_out(cptr, sptr, &setflags, prop);
1204 /** Build a mode string to describe modes for \a cptr.
1205 * @param[in] cptr Some user.
1206 * @return Pointer to a static buffer.
1208 char *umode_str(struct Client *cptr)
1210 /* Maximum string size: "owidgrx\0" */
1213 struct Flags c_flags = cli_flags(cptr);
1215 if (!HasPriv(cptr, PRIV_PROPAGATE))
1216 FlagClr(&c_flags, FLAG_OPER);
1218 for (i = 0; i < USERMODELIST_SIZE; ++i)
1220 if (FlagHas(&c_flags, userModeList[i].flag) &&
1221 userModeList[i].flag >= FLAG_GLOBAL_UMODES)
1222 *m++ = userModeList[i].c;
1225 if (IsAccount(cptr))
1227 char* t = cli_user(cptr)->account;
1230 while ((*m++ = *t++))
1233 if (cli_user(cptr)->acc_create) {
1235 Debug((DEBUG_DEBUG, "Sending timestamped account in user mode for "
1236 "account \"%s\"; timestamp %Tu", cli_user(cptr)->account,
1237 cli_user(cptr)->acc_create));
1238 ircd_snprintf(0, t = nbuf, sizeof(nbuf), ":%Tu",
1239 cli_user(cptr)->acc_create);
1240 m--; /* back up over previous nul-termination */
1241 while ((*m++ = *t++))
1248 return umodeBuf; /* Note: static buffer, gets
1249 overwritten by send_umode() */
1252 /** Send a mode change string for \a sptr to \a cptr.
1253 * @param[in] cptr Destination of mode change message.
1254 * @param[in] sptr User whose mode has changed.
1255 * @param[in] old Pre-change set of modes for \a sptr.
1256 * @param[in] sendset One of ALL_UMODES, SEND_UMODES_BUT_OPER,
1257 * SEND_UMODES, to select which changed user modes to send.
1259 void send_umode(struct Client *cptr, struct Client *sptr, struct Flags *old,
1265 int what = MODE_NULL;
1268 * Build a string in umodeBuf to represent the change in the user's
1269 * mode between the new (cli_flags(sptr)) and 'old', but skipping
1270 * the modes indicated by sendset.
1274 for (i = 0; i < USERMODELIST_SIZE; ++i)
1276 flag = userModeList[i].flag;
1277 if (FlagHas(old, flag)
1278 == HasFlag(sptr, flag))
1284 case SEND_UMODES_BUT_OPER:
1285 if (flag == FLAG_OPER)
1287 /* and fall through */
1289 if (flag < FLAG_GLOBAL_UMODES)
1293 if (FlagHas(old, flag))
1295 if (what == MODE_DEL)
1296 *m++ = userModeList[i].c;
1301 *m++ = userModeList[i].c;
1304 else /* !FlagHas(old, flag) */
1306 if (what == MODE_ADD)
1307 *m++ = userModeList[i].c;
1312 *m++ = userModeList[i].c;
1317 if (*umodeBuf && cptr)
1318 sendcmdto_one(sptr, CMD_MODE, cptr, "%s :%s", cli_name(sptr), umodeBuf);
1322 * Check to see if this resembles a sno_mask. It is if 1) there is
1323 * at least one digit and 2) The first digit occurs before the first
1324 * alphabetic character.
1325 * @param[in] word Word to check for sno_mask-ness.
1326 * @return Non-zero if \a word looks like a server notice mask; zero if not.
1328 int is_snomask(char *word)
1332 for (; *word; word++)
1335 else if (IsAlpha(*word))
1341 /** Update snomask \a oldmask according to \a arg and \a what.
1342 * @param[in] oldmask Original user mask.
1343 * @param[in] arg Update string (either a number or '+'/'-' followed by a number).
1344 * @param[in] what MODE_ADD if adding the mask.
1345 * @return New value of service notice mask.
1347 unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
1349 unsigned int sno_what;
1350 unsigned int newmask;
1354 if (what == MODE_ADD)
1359 else if (*arg == '-')
1362 if (what == MODE_ADD)
1368 sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL;
1369 /* pity we don't have strtoul everywhere */
1370 newmask = (unsigned int)atoi(arg);
1371 if (sno_what == SNO_DEL)
1372 newmask = oldmask & ~newmask;
1373 else if (sno_what == SNO_ADD)
1378 /** Remove \a cptr from the singly linked list \a list.
1379 * @param[in] cptr Client to remove from list.
1380 * @param[in,out] list Pointer to head of list containing \a cptr.
1382 static void delfrom_list(struct Client *cptr, struct SLink **list)
1385 struct SLink* prv = NULL;
1387 for (tmp = *list; tmp; tmp = tmp->next) {
1388 if (tmp->value.cptr == cptr) {
1390 prv->next = tmp->next;
1400 /** Set \a cptr's server notice mask, according to \a what.
1401 * @param[in,out] cptr Client whose snomask is updating.
1402 * @param[in] newmask Base value for new snomask.
1403 * @param[in] what One of SNO_ADD, SNO_DEL, SNO_SET, to choose operation.
1405 void set_snomask(struct Client *cptr, unsigned int newmask, int what)
1407 unsigned int oldmask, diffmask; /* unsigned please */
1411 oldmask = cli_snomask(cptr);
1413 if (what == SNO_ADD)
1415 else if (what == SNO_DEL)
1416 newmask = oldmask & ~newmask;
1417 else if (what != SNO_SET) /* absolute set, no math needed */
1418 sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what);
1420 newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
1422 diffmask = oldmask ^ newmask;
1424 for (i = 0; diffmask >> i; i++) {
1425 if (((diffmask >> i) & 1))
1427 if (((newmask >> i) & 1))
1430 tmp->next = opsarray[i];
1431 tmp->value.cptr = cptr;
1435 /* not real portable :( */
1436 delfrom_list(cptr, &opsarray[i]);
1439 cli_snomask(cptr) = newmask;
1442 /** Check whether \a sptr is allowed to send a message to \a acptr.
1443 * If \a sptr is a remote user, it means some server has an outdated
1444 * SILENCE list for \a acptr, so send the missing SILENCE mask(s) back
1445 * in the direction of \a sptr. Skip the check if \a sptr is a server.
1446 * @param[in] sptr Client trying to send a message.
1447 * @param[in] acptr Destination of message.
1448 * @return Non-zero if \a sptr is SILENCEd by \a acptr, zero if not.
1450 int is_silenced(struct Client *sptr, struct Client *acptr)
1454 size_t buf_used, slen;
1457 if (IsServer(sptr) || !(user = cli_user(acptr))
1458 || !(found = find_ban(sptr, user->silence)))
1460 assert(!(found->flags & BAN_EXCEPTION));
1461 if (!MyConnect(sptr)) {
1462 /* Buffer positive silence to send back. */
1463 buf_used = strlen(found->banstr);
1464 memcpy(buf, found->banstr, buf_used);
1465 /* Add exceptions to buffer. */
1466 for (found = user->silence; found; found = found->next) {
1467 if (!(found->flags & BAN_EXCEPTION))
1469 slen = strlen(found->banstr);
1470 if (buf_used + slen + 4 > 400) {
1471 buf[buf_used] = '\0';
1472 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf);
1476 buf[buf_used++] = ',';
1477 buf[buf_used++] = '+';
1478 buf[buf_used++] = '~';
1479 memcpy(buf + buf_used, found->banstr, slen);
1482 /* Flush silence buffer. */
1484 buf[buf_used] = '\0';
1485 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf);
1492 /** Send RPL_ISUPPORT lines to \a cptr.
1493 * @param[in] cptr Client to send ISUPPORT to.
1497 send_supported(struct Client *cptr)
1499 char featurebuf[512];
1501 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES1, FEATURESVALUES1);
1502 send_reply(cptr, RPL_ISUPPORT, featurebuf);
1503 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES2, FEATURESVALUES2);
1504 send_reply(cptr, RPL_ISUPPORT, featurebuf);
1506 return 0; /* convenience return, if it's ever needed */