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.
34 #include "ircd_alloc.h"
35 #include "ircd_chattr.h"
36 #include "ircd_features.h"
38 #include "ircd_reply.h"
39 #include "ircd_snprintf.h"
40 #include "ircd_string.h"
49 #include "querycmds.h"
55 #include "s_serv.h" /* max_client_count */
59 #include "supported.h"
65 #include "handlers.h" /* m_motd and m_lusers */
75 static int userCount = 0;
78 * 'make_user' add's an User information block to a client
79 * if it was not previously allocated.
81 struct User *make_user(struct Client *cptr)
85 if (!cli_user(cptr)) {
86 cli_user(cptr) = (struct User*) MyMalloc(sizeof(struct User));
87 assert(0 != cli_user(cptr));
89 /* All variables are 0 by default */
90 memset(cli_user(cptr), 0, sizeof(struct User));
94 cli_user(cptr)->refcnt = 1;
96 return cli_user(cptr);
102 * Decrease user reference count by one and release block, if count reaches 0.
104 void free_user(struct User* user)
107 assert(0 < user->refcnt);
109 if (--user->refcnt == 0) {
115 assert(0 == user->joined);
116 assert(0 == user->invited);
117 assert(0 == user->channel);
126 void user_count_memory(size_t* count_out, size_t* bytes_out)
128 assert(0 != count_out);
129 assert(0 != bytes_out);
130 *count_out = userCount;
131 *bytes_out = userCount * sizeof(struct User);
138 * Local function to find the next matching client. The search
139 * can be continued from the specified client entry. Normal
142 * for (x = client; x = next_client(x,mask); x = x->next)
143 * HandleMatchingClient;
146 struct Client *next_client(struct Client *next, const char* ch)
148 struct Client *tmp = next;
153 next = FindClient(ch);
154 next = next ? next : tmp;
155 if (cli_prev(tmp) == next)
159 for (; next; next = cli_next(next))
160 if (!match(ch, cli_name(next)))
168 * Do the basic thing in delivering the message (command)
169 * across the relays to the specific server (server) for
172 * Note: The command is a format string and *MUST* be
173 * of prefixed style (e.g. ":%s COMMAND %s ...").
174 * Command can have only max 8 parameters.
176 * server parv[server] is the parameter identifying the
177 * target server. It can be a nickname, servername,
178 * or server mask (from a local user) or a server
179 * numeric (from a remote server).
182 * parv[server] is replaced with the pointer to the
183 * real servername from the matched client (I'm lazy
186 * returns: (see #defines)
188 int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok,
189 struct Client *one, int MustBeOper, const char *pattern,
190 int server, int parc, char *parv[])
192 struct Client *acptr;
195 /* Assume it's me, if no server or an unregistered client */
196 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
197 return (HUNTED_ISME);
199 if (MustBeOper && !IsPrivileged(from))
201 send_reply(from, ERR_NOPRIVILEGES);
202 return HUNTED_NOSUCH;
205 /* Make sure it's a server */
207 /* Make sure it's a server */
208 if (!strchr(to, '*')) {
209 if (0 == (acptr = FindClient(to))) {
210 send_reply(from, ERR_NOSUCHSERVER, to);
211 return HUNTED_NOSUCH;
215 acptr = cli_user(acptr)->server;
216 } else if (!(acptr = find_match_server(to))) {
217 send_reply(from, ERR_NOSUCHSERVER, to);
218 return (HUNTED_NOSUCH);
220 } else if (!(acptr = FindNServer(to)))
221 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
224 return (HUNTED_ISME);
226 if (MustBeOper && !IsPrivileged(from)) {
227 send_reply(from, ERR_NOPRIVILEGES);
228 return HUNTED_NOSUCH;
231 /* assert(!IsServer(from)); */
233 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
235 sendcmdto_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
236 parv[4], parv[5], parv[6], parv[7], parv[8]);
238 return (HUNTED_PASS);
241 int hunt_server_prio_cmd(struct Client *from, const char *cmd, const char *tok,
242 struct Client *one, int MustBeOper,
243 const char *pattern, int server, int parc,
246 struct Client *acptr;
249 /* Assume it's me, if no server or an unregistered client */
250 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
251 return (HUNTED_ISME);
253 /* Make sure it's a server */
255 /* Make sure it's a server */
256 if (!strchr(to, '*')) {
257 if (0 == (acptr = FindClient(to))) {
258 send_reply(from, ERR_NOSUCHSERVER, to);
259 return HUNTED_NOSUCH;
263 acptr = cli_user(acptr)->server;
264 } else if (!(acptr = find_match_server(to))) {
265 send_reply(from, ERR_NOSUCHSERVER, to);
266 return (HUNTED_NOSUCH);
268 } else if (!(acptr = FindNServer(to)))
269 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
272 return (HUNTED_ISME);
274 if (MustBeOper && !IsPrivileged(from)) {
275 send_reply(from, ERR_NOPRIVILEGES);
276 return HUNTED_NOSUCH;
279 /* assert(!IsServer(from)); SETTIME to particular destinations permitted */
281 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
283 sendcmdto_prio_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
284 parv[4], parv[5], parv[6], parv[7], parv[8]);
286 return (HUNTED_PASS);
293 * Copy `source' to `dest', replacing all occurances of '~' and characters that
294 * are not `isIrcUi' by an underscore.
295 * Copies at most USERLEN - 1 characters or up till the first control character.
296 * If `tilde' is true, then a tilde is prepended to `dest'.
297 * Note that `dest' and `source' can point to the same area or to different
298 * non-overlapping areas.
300 static char *clean_user_id(char *dest, char *source, int tilde)
307 ch = *s++; /* Store first character to copy: */
310 *d++ = '~'; /* If `dest' == `source', then this overwrites `ch' */
313 while (ch && !IsCntrl(ch) && rlen--)
315 char nch = *s++; /* Store next character to copy */
316 *d++ = IsUserChar(ch) ? ch : '_'; /* This possibly overwrites it */
329 * This function is called when both NICK and USER messages
330 * have been accepted for the client, in whatever order. Only
331 * after this the USER message is propagated.
333 * NICK's must be propagated at once when received, although
334 * it would be better to delay them too until full info is
335 * available. Doing it is not so simple though, would have
336 * to implement the following:
338 * 1) user telnets in and gives only "NICK foobar" and waits
339 * 2) another user far away logs in normally with the nick
340 * "foobar" (quite legal, as this server didn't propagate it).
341 * 3) now this server gets nick "foobar" from outside, but
342 * has already the same defined locally. Current server
343 * would just issue "KILL foobar" to clean out dups. But,
344 * this is not fair. It should actually request another
345 * nick from local user or kill him/her...
347 int register_user(struct Client *cptr, struct Client *sptr,
348 const char *nick, char *username)
350 struct ConfItem* aconf;
354 char c = 0; /* not alphanum */
355 char d = 'a'; /* not a digit */
363 short digitgroups = 0;
364 struct User* user = cli_user(sptr);
368 user->last = CurrentTime;
369 parv[0] = cli_name(sptr);
370 parv[1] = parv[2] = NULL;
374 static time_t last_too_many1;
375 static time_t last_too_many2;
377 assert(cptr == sptr);
378 switch (conf_check_client(sptr))
382 case ACR_NO_AUTHORIZATION:
383 sendto_opmask_butone(0, SNO_UNAUTH, "Unauthorized connection from %s.",
384 get_client_name(sptr, HIDE_IP));
385 ++ServerStats->is_ref;
386 return exit_client(cptr, sptr, &me,
387 "No Authorization - use another server");
388 case ACR_TOO_MANY_IN_CLASS:
389 if (CurrentTime - last_too_many1 >= (time_t) 60)
391 last_too_many1 = CurrentTime;
392 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections in "
393 "class %i for %s.", get_client_class(sptr),
394 get_client_name(sptr, SHOW_IP));
396 ++ServerStats->is_ref;
397 IPcheck_connect_fail(cli_ip(sptr));
398 return exit_client(cptr, sptr, &me,
399 "Sorry, your connection class is full - try "
400 "again later or try another server");
401 case ACR_TOO_MANY_FROM_IP:
402 if (CurrentTime - last_too_many2 >= (time_t) 60)
404 last_too_many2 = CurrentTime;
405 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections from "
407 get_client_name(sptr, SHOW_IP));
409 ++ServerStats->is_ref;
410 return exit_client(cptr, sptr, &me,
411 "Too many connections from your host");
412 case ACR_ALREADY_AUTHORIZED:
413 /* Can this ever happen? */
415 ++ServerStats->is_ref;
416 IPcheck_connect_fail(cli_ip(sptr));
417 return exit_client(cptr, sptr, &me, "Unknown error -- Try again");
419 ircd_strncpy(user->host, cli_sockhost(sptr), HOSTLEN);
420 ircd_strncpy(user->realhost, cli_sockhost(sptr), HOSTLEN);
421 aconf = cli_confs(sptr)->value.aconf;
423 clean_user_id(user->username,
424 HasFlag(sptr, FLAG_GOTID) ? cli_username(sptr) : username,
425 HasFlag(sptr, FLAG_DOID) && !HasFlag(sptr, FLAG_GOTID));
427 if ((user->username[0] == '\0')
428 || ((user->username[0] == '~') && (user->username[1] == '\000')))
429 return exit_client(cptr, sptr, &me, "USER: Bogus userid.");
431 if (!EmptyString(aconf->passwd)
432 && !(IsDigit(*aconf->passwd) && !aconf->passwd[1])
433 && strcmp(cli_passwd(sptr), aconf->passwd))
435 ServerStats->is_ref++;
436 send_reply(sptr, ERR_PASSWDMISMATCH);
437 return exit_client(cptr, sptr, &me, "Bad Password");
439 memset(cli_passwd(sptr), 0, sizeof(cli_passwd(sptr)));
441 * following block for the benefit of time-dependent K:-lines
443 killreason = find_kill(sptr);
445 ServerStats->is_ref++;
446 return exit_client(cptr, sptr, &me,
447 (killreason == -1 ? "K-lined" : "G-lined"));
450 * Check for mixed case usernames, meaning probably hacked. Jon2 3-94
451 * Summary of rules now implemented in this patch: Ensor 11-94
452 * In a mixed-case name, if first char is upper, one more upper may
453 * appear anywhere. (A mixed-case name *must* have an upper first
454 * char, and may have one other upper.)
455 * A third upper may appear if all 3 appear at the beginning of the
456 * name, separated only by "others" (-/_/.).
457 * A single group of digits is allowed anywhere.
458 * Two groups of digits are allowed if at least one of the groups is
459 * at the beginning or the end.
460 * Only one '-', '_', or '.' is allowed (or two, if not consecutive).
461 * But not as the first or last char.
462 * No other special characters are allowed.
463 * Name must contain at least one letter.
465 tmpstr2 = tmpstr = (username[0] == '~' ? &username[1] : username);
466 while (*tmpstr && !badid)
478 if ((leadcaps || pos == 1) && !lower && !digits)
484 if (pos == 1 || !IsDigit(d))
491 else if (c == '-' || c == '_' || c == '.')
496 else if (d == '-' || d == '_' || d == '.' || other > 2)
505 if (lower && upper && (!leadcaps || leadcaps > 3 ||
506 (upper > 2 && upper > leadcaps)))
508 else if (digitgroups == 2 && !(IsDigit(tmpstr2[0]) || IsDigit(c)))
510 else if ((!lower && !upper) || !IsAlnum(c))
513 if (badid && (!HasFlag(sptr, FLAG_GOTID) ||
514 strcmp(cli_username(sptr), username) != 0))
516 ServerStats->is_ref++;
518 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
519 ":Your username is invalid.");
520 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
521 ":Connect with your real username, in lowercase.");
522 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
523 ":If your mail address were foo@bar.com, your username "
525 return exit_client(cptr, sptr, &me, "USER: Bad username");
527 Count_unknownbecomesclient(sptr, UserStats);
530 ircd_strncpy(user->username, username, USERLEN);
531 Count_newremoteclient(UserStats, user->server);
535 if (IsInvisible(sptr))
536 ++UserStats.inv_clients;
540 if (MyConnect(sptr)) {
541 cli_handler(sptr) = CLIENT_HANDLER;
542 release_dns_reply(sptr);
546 feature_str(FEAT_NETWORK),
547 feature_str(FEAT_PROVIDER) ? " via " : "",
548 feature_str(FEAT_PROVIDER) ? feature_str(FEAT_PROVIDER) : "",
551 * This is a duplicate of the NOTICE but see below...
553 send_reply(sptr, RPL_YOURHOST, cli_name(&me), version);
554 send_reply(sptr, RPL_CREATED, creation);
555 send_reply(sptr, RPL_MYINFO, cli_name(&me), infousermodes, infochanmodes,
556 infochanmodeswithparams, version);
557 send_supported(sptr);
558 m_lusers(sptr, sptr, 1, parv);
561 /* nextping = CurrentTime; */
562 if (cli_snomask(sptr) & SNO_NOISY)
563 set_snomask(sptr, cli_snomask(sptr) & SNO_NOISY, SNO_ADD);
564 if (feature_bool(FEAT_CONNEXIT_NOTICES))
565 sendto_opmask_butone(0, SNO_CONNEXIT,
566 "Client connecting: %s (%s@%s) [%s] {%d} [%s] <%s%s>",
567 cli_name(sptr), user->username, user->host,
568 cli_sock_ip(sptr), get_client_class(sptr),
569 cli_info(sptr), NumNick(cptr) /* two %s's */);
571 IPcheck_connect_succeeded(sptr);
573 * Set user's initial modes
575 parv[0] = (char*)nick;
576 parv[1] = (char*)nick;
577 parv[2] = (char*)client_get_default_umode(sptr);
578 parv[3] = NULL; /* needed in case of +s */
579 set_user_mode(sptr, sptr, 3, parv);
580 ClearHiddenHost(sptr); /* just in case somebody stuck +x in there */
583 /* if (IsServer(cptr)) */
585 struct Client *acptr;
587 acptr = user->server;
588 if (cli_from(acptr) != cli_from(sptr))
590 sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])",
591 sptr, cli_name(&me), cli_name(user->server), cli_name(cli_from(acptr)),
592 cli_sockhost(cli_from(acptr)));
593 SetFlag(sptr, FLAG_KILLED);
594 return exit_client(cptr, sptr, &me, "NICK server wrong direction");
596 else if (HasFlag(acptr, FLAG_TS8))
597 SetFlag(sptr, FLAG_TS8);
600 * Check to see if this user is being propogated
601 * as part of a net.burst, or is using protocol 9.
602 * FIXME: This can be speeded up - its stupid to check it for
603 * every NICK message in a burst again --Run.
605 for (acptr = user->server; acptr != &me; acptr = cli_serv(acptr)->up)
607 if (IsBurst(acptr) || Protocol(acptr) < 10)
610 if (!IPcheck_remote_connect(sptr, (acptr != &me)))
613 * We ran out of bits to count this
615 sendcmdto_one(&me, CMD_KILL, sptr, "%C :%s (Too many connections from your host -- Ghost)",
616 sptr, cli_name(&me));
617 return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled");
620 tmpstr = umode_str(sptr);
621 sendcmdto_serv_butone(user->server, CMD_NICK, cptr,
622 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
623 nick, cli_hopcount(sptr) + 1, cli_lastnick(sptr),
624 user->username, user->realhost,
625 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
626 inttobase64(ip_base64, ntohl(cli_ip(sptr).s_addr), 6),
627 NumNick(sptr), cli_info(sptr));
629 /* Send server notice mask to client */
630 if (MyUser(sptr) && (cli_snomask(sptr) != SNO_DEFAULT) && HasFlag(sptr, FLAG_SERVNOTICE))
631 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
637 static const struct UserMode {
643 { FLAG_INVISIBLE, 'i' },
644 { FLAG_WALLOP, 'w' },
645 { FLAG_SERVNOTICE, 's' },
647 { FLAG_CHSERV, 'k' },
649 { FLAG_ACCOUNT, 'r' },
650 { FLAG_HIDDENHOST, 'x' }
653 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
656 * XXX - find a way to get rid of this
658 static char umodeBuf[BUFSIZE];
660 int set_nick_name(struct Client* cptr, struct Client* sptr,
661 const char* nick, int parc, char* parv[])
663 if (IsServer(sptr)) {
665 const char* account = 0;
669 * A server introducing a new client, change source
671 struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
672 assert(0 != new_client);
674 cli_hopcount(new_client) = atoi(parv[2]);
675 cli_lastnick(new_client) = atoi(parv[3]);
676 if (Protocol(cptr) > 9 && parc > 7 && *parv[6] == '+')
678 for (p = parv[6] + 1; *p; p++)
680 for (i = 0; i < USERMODELIST_SIZE; ++i)
682 if (userModeList[i].c == *p)
684 SetFlag(new_client, userModeList[i].flag);
685 if (userModeList[i].flag == FLAG_ACCOUNT)
692 client_set_privs(new_client, NULL); /* set privs on user */
696 strcpy(cli_name(new_client), nick);
697 cli_user(new_client) = make_user(new_client);
698 cli_user(new_client)->server = sptr;
699 SetRemoteNumNick(new_client, parv[parc - 2]);
701 * IP# of remote client
703 cli_ip(new_client).s_addr = htonl(base64toint(parv[parc - 3]));
705 add_client_to_list(new_client);
706 hAddClient(new_client);
708 cli_serv(sptr)->ghost = 0; /* :server NICK means end of net.burst */
709 ircd_strncpy(cli_username(new_client), parv[4], USERLEN);
710 ircd_strncpy(cli_user(new_client)->host, parv[5], HOSTLEN);
711 ircd_strncpy(cli_user(new_client)->realhost, parv[5], HOSTLEN);
712 ircd_strncpy(cli_info(new_client), parv[parc - 1], REALLEN);
714 int len = ACCOUNTLEN;
715 if ((p = strchr(account, ':'))) {
716 len = (p++) - account;
717 cli_user(new_client)->acc_create = atoi(p);
718 Debug((DEBUG_DEBUG, "Received timestamped account in user mode; "
719 "account \"%s\", timestamp %Tu", account,
720 cli_user(new_client)->acc_create));
722 ircd_strncpy(cli_user(new_client)->account, account, len);
724 if (HasHiddenHost(new_client))
725 ircd_snprintf(0, cli_user(new_client)->host, HOSTLEN, "%s.%s",
726 account, feature_str(FEAT_HIDDEN_HOST));
728 return register_user(cptr, new_client, cli_name(new_client), parv[4]);
730 else if ((cli_name(sptr))[0]) {
732 * Client changing its nick
734 * If the client belongs to me, then check to see
735 * if client is on any channels where it is currently
736 * banned. If so, do not allow the nick change to occur.
739 const char* channel_name;
740 struct Membership *member;
741 if ((channel_name = find_no_nickchange_channel(sptr))) {
742 return send_reply(cptr, ERR_BANNICKCHANGE, channel_name);
745 * Refuse nick change if the last nick change was less
746 * then 30 seconds ago. This is intended to get rid of
747 * clone bots doing NICK FLOOD. -SeKs
748 * If someone didn't change their nick for more then 60 seconds
749 * however, allow to do two nick changes immediately after another
750 * before limiting the nick flood. -Run
752 if (CurrentTime < cli_nextnick(cptr))
754 cli_nextnick(cptr) += 2;
755 send_reply(cptr, ERR_NICKTOOFAST, parv[1],
756 cli_nextnick(cptr) - CurrentTime);
757 /* Send error message */
758 sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cli_name(cptr));
759 /* bounce NICK to user */
760 return 0; /* ignore nick change! */
763 /* Limit total to 1 change per NICK_DELAY seconds: */
764 cli_nextnick(cptr) += NICK_DELAY;
765 /* However allow _maximal_ 1 extra consecutive nick change: */
766 if (cli_nextnick(cptr) < CurrentTime)
767 cli_nextnick(cptr) = CurrentTime;
769 /* Invalidate all bans against the user so we check them again */
770 for (member = (cli_user(cptr))->channel; member;
771 member = member->next_channel)
772 ClearBanValid(member);
775 * Also set 'lastnick' to current time, if changed.
777 if (0 != ircd_strcmp(parv[0], nick))
778 cli_lastnick(sptr) = (sptr == cptr) ? TStime() : atoi(parv[2]);
781 * Client just changing his/her nick. If he/she is
782 * on a channel, send note of change to all clients
783 * on that channel. Propagate notice to other servers.
786 sendcmdto_common_channels_butone(sptr, CMD_NICK, NULL, ":%s", nick);
787 add_history(sptr, 1);
788 sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick,
792 sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick);
794 if ((cli_name(sptr))[0])
796 strcpy(cli_name(sptr), nick);
800 /* Local client setting NICK the first time */
802 strcpy(cli_name(sptr), nick);
803 if (!cli_user(sptr)) {
804 cli_user(sptr) = make_user(sptr);
805 cli_user(sptr)->server = &me;
807 SetLocalNumNick(sptr);
811 * If the client hasn't gotten a cookie-ping yet,
812 * choose a cookie and send it. -record!jegelhof@cloud9.net
814 if (!cli_cookie(sptr)) {
816 cli_cookie(sptr) = (ircrandom() & 0x7fffffff);
817 } while (!cli_cookie(sptr));
818 sendrawto_one(cptr, MSG_PING " :%u", cli_cookie(sptr));
820 else if (*(cli_user(sptr))->host && cli_cookie(sptr) == COOKIE_VERIFIED) {
822 * USER and PONG already received, now we have NICK.
823 * register_user may reject the client and call exit_client
824 * for it - must test this and exit m_nick too !
826 cli_lastnick(sptr) = TStime(); /* Always local client */
827 if (register_user(cptr, sptr, nick, cli_user(sptr)->username) == CPTR_KILLED)
834 static unsigned char hash_target(unsigned int target)
836 return (unsigned char) (target >> 16) ^ (target >> 8);
842 * sptr must be a local client!
844 * Cannonifies target for client `sptr'.
847 add_target(struct Client *sptr, void *target)
849 /* Ok, this shouldn't work esp on alpha
851 unsigned char hash = hash_target((unsigned long) target);
852 unsigned char* targets;
855 assert(cli_local(sptr));
857 targets = cli_targets(sptr);
862 for (i = 0; i < MAXTARGETS; ++i) {
863 if (targets[i] == hash)
869 memmove(&targets[RESERVEDTARGETS + 1],
870 &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
871 targets[RESERVEDTARGETS] = hash;
877 * sptr must be a local client !
879 * Returns 'true' (1) when too many targets are addressed.
880 * Returns 'false' (0) when it's ok to send to this target.
882 int check_target_limit(struct Client *sptr, void *target, const char *name,
885 unsigned char hash = hash_target((unsigned long) target);
887 unsigned char* targets;
890 assert(cli_local(sptr));
891 targets = cli_targets(sptr);
893 /* If user is invited to channel, give him/her a free target */
894 if (IsChannelName(name) && IsInvited(sptr, target))
898 * Same target as last time?
900 if (targets[0] == hash)
902 for (i = 1; i < MAXTARGETS; ++i) {
903 if (targets[i] == hash) {
904 memmove(&targets[1], &targets[0], i);
913 if (CurrentTime < cli_nexttarget(sptr)) {
914 if (cli_nexttarget(sptr) - CurrentTime < TARGET_DELAY + 8) {
918 cli_nexttarget(sptr) += 2;
919 send_reply(sptr, ERR_TARGETTOOFAST, name,
920 cli_nexttarget(sptr) - CurrentTime);
925 cli_nexttarget(sptr) += TARGET_DELAY;
926 if (cli_nexttarget(sptr) < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)))
927 cli_nexttarget(sptr) = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1));
930 memmove(&targets[1], &targets[0], MAXTARGETS - 1);
936 * whisper - called from m_cnotice and m_cprivmsg.
938 * parv[0] = sender prefix
941 * parv[3] = Private message text
943 * Added 971023 by Run.
944 * Reason: Allows channel operators to sent an arbitrary number of private
945 * messages to users on their channel, avoiding the max.targets limit.
946 * Building this into m_private would use too much cpu because we'd have
947 * to a cross channel lookup for every private message!
948 * Note that we can't allow non-chan ops to use this command, it would be
949 * abused by mass advertisers.
952 int whisper(struct Client* source, const char* nick, const char* channel,
953 const char* text, int is_notice)
956 struct Channel* chptr;
957 struct Membership* membership;
961 assert(0 != channel);
962 assert(MyUser(source));
964 if (!(dest = FindUser(nick))) {
965 return send_reply(source, ERR_NOSUCHNICK, nick);
967 if (!(chptr = FindChannel(channel))) {
968 return send_reply(source, ERR_NOSUCHCHANNEL, channel);
971 * compare both users channel lists, instead of the channels user list
972 * since the link is the same, this should be a little faster for channels
973 * with a lot of users
975 for (membership = cli_user(source)->channel; membership; membership = membership->next_channel) {
976 if (chptr == membership->channel)
979 if (0 == membership) {
980 return send_reply(source, ERR_NOTONCHANNEL, chptr->chname);
982 if (!IsVoicedOrOpped(membership)) {
983 return send_reply(source, ERR_VOICENEEDED, chptr->chname);
986 * lookup channel in destination
988 assert(0 != cli_user(dest));
989 for (membership = cli_user(dest)->channel; membership; membership = membership->next_channel) {
990 if (chptr == membership->channel)
993 if (0 == membership || IsZombie(membership)) {
994 return send_reply(source, ERR_USERNOTINCHANNEL, cli_name(dest), chptr->chname);
996 if (is_silenced(source, dest))
999 if (cli_user(dest)->away)
1000 send_reply(source, RPL_AWAY, cli_name(dest), cli_user(dest)->away);
1002 sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text);
1004 sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text);
1010 * added Sat Jul 25 07:30:42 EST 1992
1012 void send_umode_out(struct Client *cptr, struct Client *sptr,
1013 struct Flags *old, int prop)
1016 struct Client *acptr;
1018 send_umode(NULL, sptr, old, prop ? SEND_UMODES : SEND_UMODES_BUT_OPER);
1020 for (i = HighestFd; i >= 0; i--)
1022 if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
1023 (acptr != cptr) && (acptr != sptr) && *umodeBuf)
1024 sendcmdto_one(sptr, CMD_MODE, acptr, "%s :%s", cli_name(sptr), umodeBuf);
1026 if (cptr && MyUser(cptr))
1027 send_umode(cptr, sptr, old, ALL_UMODES);
1032 * send_user_info - send user info userip/userhost
1033 * NOTE: formatter must put info into buffer and return a pointer to the end of
1034 * the data it put in the buffer.
1036 void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
1041 int users_found = 0;
1042 struct Client* acptr;
1049 mb = msgq_make(sptr, rpl_str(rpl), cli_name(&me), cli_name(sptr));
1051 for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
1052 if ((acptr = FindUser(name))) {
1054 msgq_append(0, mb, " ");
1055 (*fmt)(acptr, sptr, mb);
1057 if (5 == ++arg_count)
1060 send_buffer(sptr, mb, 0);
1067 * If, after setting the flags, the user has both HiddenHost and Account
1068 * set, its hostmask is changed.
1071 hide_hostmask(struct Client *cptr, unsigned int flag)
1073 struct Membership *chan;
1075 if (MyConnect(cptr) && !feature_bool(FEAT_HOST_HIDING) &&
1076 flag == FLAG_HIDDENHOST)
1079 /* Invalidate all bans against the user so we check them again */
1080 for (chan = (cli_user(cptr))->channel; chan;
1081 chan = chan->next_channel)
1082 ClearBanValid(chan);
1084 SetFlag(cptr, flag);
1085 if (!HasFlag(cptr, FLAG_HIDDENHOST) || !HasFlag(cptr, FLAG_ACCOUNT))
1088 sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Registered");
1089 ircd_snprintf(0, cli_user(cptr)->host, HOSTLEN, "%s.%s",
1090 cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
1092 /* ok, the client is now fully hidden, so let them know -- hikari */
1093 if (MyConnect(cptr))
1094 send_reply(cptr, RPL_HOSTHIDDEN, cli_user(cptr)->host);
1097 * Go through all channels the client was on, rejoin him
1098 * and set the modes, if any
1100 for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel)
1104 /* For a user with no modes in a join-delayed channel, do not show
1106 if (!IsChanOp(chan) && !HasVoice(chan)
1107 && (chan->channel->mode.mode & MODE_DELJOINS))
1108 SetDelayedJoin(chan);
1110 sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, 0,
1111 "%H", chan->channel);
1112 if (IsChanOp(chan) && HasVoice(chan))
1113 sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr, 0,
1114 "%H +ov %C %C", chan->channel, cptr,
1116 else if (IsChanOp(chan) || HasVoice(chan))
1117 sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr, 0,
1118 "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr);
1124 * set_user_mode() added 15/10/91 By Darren Reed.
1127 * parv[1] - username to change mode for
1128 * parv[2] - modes to change
1130 int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
1134 struct Client *acptr;
1137 struct Flags setflags;
1138 unsigned int tmpmask = 0;
1139 int snomask_given = 0;
1142 int do_host_hiding = 0;
1147 return need_more_params(sptr, "MODE");
1149 if (!(acptr = FindUser(parv[1])))
1151 if (MyConnect(sptr))
1152 send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]);
1156 if (IsServer(sptr) || sptr != acptr)
1159 sendwallto_group_butone(&me, WALL_WALLOPS, 0,
1160 "MODE for User %s from %s!%s", parv[1],
1161 cli_name(cptr), cli_name(sptr));
1163 send_reply(sptr, ERR_USERSDONTMATCH);
1171 for (i = 0; i < USERMODELIST_SIZE; i++)
1173 if (HasFlag(sptr, userModeList[i].flag) &&
1174 userModeList[i].flag != FLAG_ACCOUNT)
1175 *m++ = userModeList[i].c;
1178 send_reply(sptr, RPL_UMODEIS, buf);
1179 if (HasFlag(sptr, FLAG_SERVNOTICE) && MyConnect(sptr)
1180 && cli_snomask(sptr) !=
1181 (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT))
1182 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1187 * find flags already set for user
1188 * why not just copy them?
1190 setflags = cli_flags(sptr);
1192 if (MyConnect(sptr))
1193 tmpmask = cli_snomask(sptr);
1196 * parse mode change string(s)
1198 for (p = &parv[2]; *p; p++) { /* p is changed in loop too */
1199 for (m = *p; *m; m++) {
1208 if (*(p + 1) && is_snomask(*(p + 1))) {
1210 tmpmask = umode_make_snomask(tmpmask, *++p, what);
1211 tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1214 tmpmask = (what == MODE_ADD) ?
1215 (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1217 SetServNotice(sptr);
1219 ClearServNotice(sptr);
1222 if (what == MODE_ADD)
1228 if (what == MODE_ADD)
1231 ClrFlag(sptr, FLAG_OPER);
1232 ClrFlag(sptr, FLAG_LOCOP);
1233 if (MyConnect(sptr))
1235 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1236 cli_handler(sptr) = CLIENT_HANDLER;
1241 if (what == MODE_ADD)
1245 ClrFlag(sptr, FLAG_OPER);
1246 ClrFlag(sptr, FLAG_LOCOP);
1247 if (MyConnect(sptr))
1249 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1250 cli_handler(sptr) = CLIENT_HANDLER;
1255 if (what == MODE_ADD)
1258 ClearInvisible(sptr);
1261 if (what == MODE_ADD)
1267 if (what == MODE_ADD)
1268 SetChannelService(sptr);
1270 ClearChannelService(sptr);
1273 if (what == MODE_ADD)
1279 if (what == MODE_ADD)
1283 send_reply(sptr, ERR_UMODEUNKNOWNFLAG, *m);
1289 * Evaluate rules for new user mode
1290 * Stop users making themselves operators too easily:
1292 if (!IsServer(cptr))
1294 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr))
1296 if (!FlagHas(&setflags, FLAG_LOCOP) && IsLocOp(sptr))
1299 * new umode; servers can set it, local users cannot;
1300 * prevents users from /kick'ing or /mode -o'ing
1302 if (!FlagHas(&setflags, FLAG_CHSERV))
1303 ClearChannelService(sptr);
1305 * only send wallops to opers
1307 if (feature_bool(FEAT_WALLOPS_OPER_ONLY) && !IsAnOper(sptr) &&
1308 !FlagHas(&setflags, FLAG_WALLOP))
1310 if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY) && MyConnect(sptr) &&
1311 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_SERVNOTICE))
1313 ClearServNotice(sptr);
1314 set_snomask(sptr, 0, SNO_SET);
1316 if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY) &&
1317 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_DEBUG))
1320 if (MyConnect(sptr))
1322 if ((FlagHas(&setflags, FLAG_OPER) || FlagHas(&setflags, FLAG_LOCOP)) &&
1324 det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPERATOR);
1326 if (SendServNotice(sptr))
1328 if (tmpmask != cli_snomask(sptr))
1329 set_snomask(sptr, tmpmask, SNO_SET);
1330 if (cli_snomask(sptr) && snomask_given)
1331 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1334 set_snomask(sptr, 0, SNO_SET);
1337 * Compare new flags with old flags and send string which
1338 * will cause servers to update correctly.
1340 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr))
1344 client_set_privs(sptr, NULL); /* may set propagate privilege */
1346 /* remember propagate privilege setting */
1347 if (HasPriv(sptr, PRIV_PROPAGATE))
1349 if (FlagHas(&setflags, FLAG_OPER) && !IsOper(sptr))
1351 /* user no longer oper */
1353 client_set_privs(sptr, NULL); /* will clear propagate privilege */
1355 if (FlagHas(&setflags, FLAG_INVISIBLE) && !IsInvisible(sptr))
1356 --UserStats.inv_clients;
1357 if (!FlagHas(&setflags, FLAG_INVISIBLE) && IsInvisible(sptr))
1358 ++UserStats.inv_clients;
1359 if (!FlagHas(&setflags, FLAG_HIDDENHOST) && do_host_hiding)
1360 hide_hostmask(sptr, FLAG_HIDDENHOST);
1361 send_umode_out(cptr, sptr, &setflags, prop);
1367 * Build umode string for BURST command
1370 char *umode_str(struct Client *cptr)
1372 /* Maximum string size: "owidgrx\0" */
1375 struct Flags c_flags = cli_flags(cptr);
1377 if (HasPriv(cptr, PRIV_PROPAGATE))
1378 FlagSet(&c_flags, FLAG_OPER);
1380 FlagClr(&c_flags, FLAG_OPER);
1382 for (i = 0; i < USERMODELIST_SIZE; ++i)
1384 if (FlagHas(&c_flags, userModeList[i].flag) &&
1385 userModeList[i].flag >= FLAG_GLOBAL_UMODES)
1386 *m++ = userModeList[i].c;
1389 if (IsAccount(cptr))
1391 char* t = cli_user(cptr)->account;
1394 while ((*m++ = *t++))
1397 if (cli_user(cptr)->acc_create) {
1399 Debug((DEBUG_DEBUG, "Sending timestamped account in user mode for "
1400 "account \"%s\"; timestamp %Tu", cli_user(cptr)->account,
1401 cli_user(cptr)->acc_create));
1402 ircd_snprintf(0, t = nbuf, sizeof(nbuf), ":%Tu",
1403 cli_user(cptr)->acc_create);
1404 m--; /* back up over previous nul-termination */
1405 while ((*m++ = *t++))
1412 return umodeBuf; /* Note: static buffer, gets
1413 overwritten by send_umode() */
1417 * Send the MODE string for user (user) to connection cptr
1420 void send_umode(struct Client *cptr, struct Client *sptr, struct Flags *old,
1426 int what = MODE_NULL;
1429 * Build a string in umodeBuf to represent the change in the user's
1430 * mode between the new (cli_flags(sptr)) and 'old', but skipping
1431 * the modes indicated by sendset.
1435 for (i = 0; i < USERMODELIST_SIZE; ++i)
1437 flag = userModeList[i].flag;
1438 if (FlagHas(old, flag)
1439 == HasFlag(sptr, flag))
1445 case SEND_UMODES_BUT_OPER:
1446 if (flag == FLAG_OPER)
1448 /* and fall through */
1450 if (flag < FLAG_GLOBAL_UMODES)
1454 if (FlagHas(old, flag))
1456 if (what == MODE_DEL)
1457 *m++ = userModeList[i].c;
1462 *m++ = userModeList[i].c;
1465 else /* !FlagHas(old, flag) */
1467 if (what == MODE_ADD)
1468 *m++ = userModeList[i].c;
1473 *m++ = userModeList[i].c;
1478 if (*umodeBuf && cptr)
1479 sendcmdto_one(sptr, CMD_MODE, cptr, "%s :%s", cli_name(sptr), umodeBuf);
1483 * Check to see if this resembles a sno_mask. It is if 1) there is
1484 * at least one digit and 2) The first digit occurs before the first
1485 * alphabetic character.
1487 int is_snomask(char *word)
1491 for (; *word; word++)
1494 else if (IsAlpha(*word))
1501 * If it begins with a +, count this as an additive mask instead of just
1502 * a replacement. If what == MODE_DEL, "+" has no special effect.
1504 unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
1506 unsigned int sno_what;
1507 unsigned int newmask;
1511 if (what == MODE_ADD)
1516 else if (*arg == '-')
1519 if (what == MODE_ADD)
1525 sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL;
1526 /* pity we don't have strtoul everywhere */
1527 newmask = (unsigned int)atoi(arg);
1528 if (sno_what == SNO_DEL)
1529 newmask = oldmask & ~newmask;
1530 else if (sno_what == SNO_ADD)
1535 static void delfrom_list(struct Client *cptr, struct SLink **list)
1538 struct SLink* prv = NULL;
1540 for (tmp = *list; tmp; tmp = tmp->next) {
1541 if (tmp->value.cptr == cptr) {
1543 prv->next = tmp->next;
1554 * This function sets a Client's server notices mask, according to
1555 * the parameter 'what'. This could be even faster, but the code
1556 * gets mighty hard to read :)
1558 void set_snomask(struct Client *cptr, unsigned int newmask, int what)
1560 unsigned int oldmask, diffmask; /* unsigned please */
1564 oldmask = cli_snomask(cptr);
1566 if (what == SNO_ADD)
1568 else if (what == SNO_DEL)
1569 newmask = oldmask & ~newmask;
1570 else if (what != SNO_SET) /* absolute set, no math needed */
1571 sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what);
1573 newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
1575 diffmask = oldmask ^ newmask;
1577 for (i = 0; diffmask >> i; i++) {
1578 if (((diffmask >> i) & 1))
1580 if (((newmask >> i) & 1))
1583 tmp->next = opsarray[i];
1584 tmp->value.cptr = cptr;
1588 /* not real portable :( */
1589 delfrom_list(cptr, &opsarray[i]);
1592 cli_snomask(cptr) = newmask;
1596 * is_silenced : Does the actual check wether sptr is allowed
1597 * to send a message to acptr.
1598 * Both must be registered persons.
1599 * If sptr is silenced by acptr, his message should not be propagated,
1600 * but more over, if this is detected on a server not local to sptr
1601 * the SILENCE mask is sent upstream.
1603 int is_silenced(struct Client *sptr, struct Client *acptr)
1607 static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
1608 static char senderip[16 + NICKLEN + USERLEN + 5];
1609 static char senderh[HOSTLEN + ACCOUNTLEN + USERLEN + 6];
1611 if (!cli_user(acptr) || !(lp = cli_user(acptr)->silence) || !(user = cli_user(sptr)))
1613 ircd_snprintf(0, sender, sizeof(sender), "%s!%s@%s", cli_name(sptr),
1614 user->username, user->host);
1615 ircd_snprintf(0, senderip, sizeof(senderip), "%s!%s@%s", cli_name(sptr),
1616 user->username, ircd_ntoa((const char*) &(cli_ip(sptr))));
1617 if (HasHiddenHost(sptr))
1618 ircd_snprintf(0, senderh, sizeof(senderh), "%s!%s@%s", cli_name(sptr),
1619 user->username, user->realhost);
1620 for (; lp; lp = lp->next)
1622 if ((!(lp->flags & CHFL_SILENCE_IPMASK) && (!match(lp->value.cp, sender) ||
1623 (HasHiddenHost(sptr) && !match(lp->value.cp, senderh)))) ||
1624 ((lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, senderip)))
1626 if (!MyConnect(sptr))
1628 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr,
1640 * Removes all silence masks from the list of sptr that fall within `mask'
1641 * Returns -1 if none where found, 0 otherwise.
1643 int del_silence(struct Client *sptr, char *mask)
1649 for (lp = &(cli_user(sptr))->silence; *lp;) {
1650 if (!mmatch(mask, (*lp)->value.cp))
1654 MyFree(tmp->value.cp);
1664 int add_silence(struct Client* sptr, const char* mask)
1666 struct SLink *lp, **lpp;
1667 int cnt = 0, len = strlen(mask);
1670 for (lpp = &(cli_user(sptr))->silence, lp = *lpp; lp;)
1672 if (0 == ircd_strcmp(mask, lp->value.cp))
1674 if (!mmatch(mask, lp->value.cp))
1676 struct SLink *tmp = lp;
1677 *lpp = lp = lp->next;
1678 MyFree(tmp->value.cp);
1684 len += strlen(lp->value.cp);
1685 if ((len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXSILES))) ||
1686 (++cnt >= feature_int(FEAT_MAXSILES)))
1688 send_reply(sptr, ERR_SILELISTFULL, mask);
1691 else if (!mmatch(lp->value.cp, mask))
1698 memset(lp, 0, sizeof(struct SLink));
1699 lp->next = cli_user(sptr)->silence;
1700 lp->value.cp = (char*) MyMalloc(strlen(mask) + 1);
1701 assert(0 != lp->value.cp);
1702 strcpy(lp->value.cp, mask);
1703 if ((ip_start = strrchr(mask, '@')) && check_if_ipmask(ip_start + 1))
1704 lp->flags = CHFL_SILENCE_IPMASK;
1705 cli_user(sptr)->silence = lp;
1710 send_supported(struct Client *cptr)
1712 char featurebuf[512];
1714 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES1, FEATURESVALUES1);
1715 send_reply(cptr, RPL_ISUPPORT, featurebuf);
1716 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES2, FEATURESVALUES2);
1717 send_reply(cptr, RPL_ISUPPORT, featurebuf);
1719 return 0; /* convenience return, if it's ever needed */