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_policy.h"
39 #include "ircd_reply.h"
40 #include "ircd_snprintf.h"
41 #include "ircd_string.h"
50 #include "querycmds.h"
56 #include "s_serv.h" /* max_client_count */
60 #include "supported.h"
66 #include "handlers.h" /* m_motd and m_lusers */
76 static int userCount = 0;
79 * 'make_user' add's an User information block to a client
80 * if it was not previously allocated.
82 struct User *make_user(struct Client *cptr)
86 if (!cli_user(cptr)) {
87 cli_user(cptr) = (struct User*) MyMalloc(sizeof(struct User));
88 assert(0 != cli_user(cptr));
90 /* All variables are 0 by default */
91 memset(cli_user(cptr), 0, sizeof(struct User));
95 cli_user(cptr)->refcnt = 1;
97 return cli_user(cptr);
103 * Decrease user reference count by one and release block, if count reaches 0.
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);
127 void user_count_memory(size_t* count_out, size_t* bytes_out)
129 assert(0 != count_out);
130 assert(0 != bytes_out);
131 *count_out = userCount;
132 *bytes_out = userCount * sizeof(struct User);
139 * Local function to find the next matching client. The search
140 * can be continued from the specified client entry. Normal
143 * for (x = client; x = next_client(x,mask); x = x->next)
144 * HandleMatchingClient;
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)))
169 * Do the basic thing in delivering the message (command)
170 * across the relays to the specific server (server) for
173 * Note: The command is a format string and *MUST* be
174 * of prefixed style (e.g. ":%s COMMAND %s ...").
175 * Command can have only max 8 parameters.
177 * server parv[server] is the parameter identifying the
181 * parv[server] is replaced with the pointer to the
182 * real servername from the matched client (I'm lazy
185 * returns: (see #defines)
187 int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok,
188 struct Client *one, int MustBeOper, const char *pattern,
189 int server, int parc, char *parv[])
191 struct Client *acptr;
194 /* Assume it's me, if no server or an unregistered client */
195 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
196 return (HUNTED_ISME);
198 if (MustBeOper && !IsPrivileged(from))
200 send_reply(from, ERR_NOPRIVILEGES);
201 return HUNTED_NOSUCH;
204 /* Make sure it's a server */
206 /* Make sure it's a server */
207 if (!strchr(to, '*')) {
208 if (0 == (acptr = FindClient(to)))
209 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 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 int hunt_server_prio_cmd(struct Client *from, const char *cmd, const char *tok,
239 struct Client *one, int MustBeOper,
240 const char *pattern, int server, int parc,
243 struct Client *acptr;
246 /* Assume it's me, if no server or an unregistered client */
247 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
248 return (HUNTED_ISME);
250 /* Make sure it's a server */
252 /* Make sure it's a server */
253 if (!strchr(to, '*')) {
254 if (0 == (acptr = FindClient(to)))
255 return HUNTED_NOSUCH;
258 acptr = cli_user(acptr)->server;
259 } else if (!(acptr = find_match_server(to))) {
260 send_reply(from, ERR_NOSUCHSERVER, to);
261 return (HUNTED_NOSUCH);
263 } else if (!(acptr = FindNServer(to)))
264 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
267 return (HUNTED_ISME);
269 if (MustBeOper && !IsPrivileged(from)) {
270 send_reply(from, ERR_NOPRIVILEGES);
271 return HUNTED_NOSUCH;
274 assert(!IsServer(from));
276 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
278 sendcmdto_prio_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
279 parv[4], parv[5], parv[6], parv[7], parv[8]);
281 return (HUNTED_PASS);
285 * 'do_nick_name' ensures that the given parameter (nick) is really a proper
286 * string for a nickname (note, the 'nick' may be modified in the process...)
288 * RETURNS the length of the final NICKNAME (0, if nickname is invalid)
290 * Nickname characters are in range 'A'..'}', '_', '-', '0'..'9'
291 * anything outside the above set will terminate nickname.
292 * In addition, the first character cannot be '-' or a Digit.
295 * The '~'-character should be allowed, but a change should be global,
296 * some confusion would result if only few servers allowed it...
298 int do_nick_name(char* nick)
301 char* end = ch + NICKLEN;
304 if (*ch == '-' || IsDigit(*ch)) /* first character in [0..9-] */
307 for ( ; (ch < end) && *ch; ++ch)
308 if (!IsNickChar(*ch))
319 * Copy `source' to `dest', replacing all occurances of '~' and characters that
320 * are not `isIrcUi' by an underscore.
321 * Copies at most USERLEN - 1 characters or up till the first control character.
322 * If `tilde' is true, then a tilde is prepended to `dest'.
323 * Note that `dest' and `source' can point to the same area or to different
324 * non-overlapping areas.
326 static char *clean_user_id(char *dest, char *source, int tilde)
333 ch = *s++; /* Store first character to copy: */
336 *d++ = '~'; /* If `dest' == `source', then this overwrites `ch' */
339 while (ch && !IsCntrl(ch) && rlen--)
341 char nch = *s++; /* Store next character to copy */
342 *d++ = IsUserChar(ch) ? ch : '_'; /* This possibly overwrites it */
355 * This function is called when both NICK and USER messages
356 * have been accepted for the client, in whatever order. Only
357 * after this the USER message is propagated.
359 * NICK's must be propagated at once when received, although
360 * it would be better to delay them too until full info is
361 * available. Doing it is not so simple though, would have
362 * to implement the following:
364 * 1) user telnets in and gives only "NICK foobar" and waits
365 * 2) another user far away logs in normally with the nick
366 * "foobar" (quite legal, as this server didn't propagate it).
367 * 3) now this server gets nick "foobar" from outside, but
368 * has already the same defined locally. Current server
369 * would just issue "KILL foobar" to clean out dups. But,
370 * this is not fair. It should actually request another
371 * nick from local user or kill him/her...
373 int register_user(struct Client *cptr, struct Client *sptr,
374 const char *nick, char *username)
376 struct ConfItem* aconf;
380 char c = 0; /* not alphanum */
381 char d = 'a'; /* not a digit */
389 short digitgroups = 0;
390 struct User* user = cli_user(sptr);
393 user->last = CurrentTime;
394 parv[0] = cli_name(sptr);
395 parv[1] = parv[2] = NULL;
399 static time_t last_too_many1;
400 static time_t last_too_many2;
402 assert(cptr == sptr);
403 switch (conf_check_client(sptr))
407 case ACR_NO_AUTHORIZATION:
408 sendto_opmask_butone(0, SNO_UNAUTH, "Unauthorized connection from %s.",
409 get_client_name(sptr, HIDE_IP));
410 ++ServerStats->is_ref;
411 return exit_client(cptr, sptr, &me,
412 "No Authorization - use another server");
413 case ACR_TOO_MANY_IN_CLASS:
414 if (CurrentTime - last_too_many1 >= (time_t) 60)
416 last_too_many1 = CurrentTime;
417 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections in "
418 "class %i for %s.", get_client_class(sptr),
419 get_client_name(sptr, SHOW_IP));
421 ++ServerStats->is_ref;
422 IPcheck_connect_fail(cli_ip(sptr));
423 return exit_client(cptr, sptr, &me,
424 "Sorry, your connection class is full - try "
425 "again later or try another server");
426 case ACR_TOO_MANY_FROM_IP:
427 if (CurrentTime - last_too_many2 >= (time_t) 60)
429 last_too_many2 = CurrentTime;
430 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections from "
432 get_client_name(sptr, SHOW_IP));
434 ++ServerStats->is_ref;
435 return exit_client(cptr, sptr, &me,
436 "Too many connections from your host");
437 case ACR_ALREADY_AUTHORIZED:
438 /* Can this ever happen? */
440 ++ServerStats->is_ref;
441 IPcheck_connect_fail(cli_ip(sptr));
442 return exit_client(cptr, sptr, &me, "Unknown error -- Try again");
444 ircd_strncpy(user->host, cli_sockhost(sptr), HOSTLEN);
445 ircd_strncpy(user->realhost, cli_sockhost(sptr), HOSTLEN);
446 aconf = cli_confs(sptr)->value.aconf;
448 clean_user_id(user->username,
449 (cli_flags(sptr) & FLAGS_GOTID) ? cli_username(sptr) : username,
450 (cli_flags(sptr) & FLAGS_DOID) && !(cli_flags(sptr) & FLAGS_GOTID));
452 if ((user->username[0] == '\0')
453 || ((user->username[0] == '~') && (user->username[1] == '\000')))
454 return exit_client(cptr, sptr, &me, "USER: Bogus userid.");
456 if (!EmptyString(aconf->passwd)
457 && !(IsDigit(*aconf->passwd) && !aconf->passwd[1])
458 && strcmp(cli_passwd(sptr), aconf->passwd))
460 ServerStats->is_ref++;
461 IPcheck_connect_fail(cli_ip(sptr));
462 send_reply(sptr, ERR_PASSWDMISMATCH);
463 return exit_client(cptr, sptr, &me, "Bad Password");
465 memset(cli_passwd(sptr), 0, sizeof(cli_passwd(sptr)));
467 * following block for the benefit of time-dependent K:-lines
469 if (find_kill(sptr)) {
470 ServerStats->is_ref++;
471 IPcheck_connect_fail(cli_ip(sptr));
472 return exit_client(cptr, sptr, &me, "K-lined");
475 * Check for mixed case usernames, meaning probably hacked. Jon2 3-94
476 * Summary of rules now implemented in this patch: Ensor 11-94
477 * In a mixed-case name, if first char is upper, one more upper may
478 * appear anywhere. (A mixed-case name *must* have an upper first
479 * char, and may have one other upper.)
480 * A third upper may appear if all 3 appear at the beginning of the
481 * name, separated only by "others" (-/_/.).
482 * A single group of digits is allowed anywhere.
483 * Two groups of digits are allowed if at least one of the groups is
484 * at the beginning or the end.
485 * Only one '-', '_', or '.' is allowed (or two, if not consecutive).
486 * But not as the first or last char.
487 * No other special characters are allowed.
488 * Name must contain at least one letter.
490 tmpstr2 = tmpstr = (username[0] == '~' ? &username[1] : username);
491 while (*tmpstr && !badid)
503 if ((leadcaps || pos == 1) && !lower && !digits)
509 if (pos == 1 || !IsDigit(d))
516 else if (c == '-' || c == '_' || c == '.')
521 else if (d == '-' || d == '_' || d == '.' || other > 2)
530 if (lower && upper && (!leadcaps || leadcaps > 3 ||
531 (upper > 2 && upper > leadcaps)))
533 else if (digitgroups == 2 && !(IsDigit(tmpstr2[0]) || IsDigit(c)))
535 else if ((!lower && !upper) || !IsAlnum(c))
538 if (badid && (!(cli_flags(sptr) & FLAGS_GOTID) ||
539 strcmp(cli_username(sptr), username) != 0))
541 ServerStats->is_ref++;
543 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
544 ":Your username is invalid.");
545 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
546 ":Connect with your real username, in lowercase.");
547 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
548 ":If your mail address were foo@bar.com, your username "
550 return exit_client(cptr, sptr, &me, "USER: Bad username");
552 Count_unknownbecomesclient(sptr, UserStats);
555 ircd_strncpy(user->username, username, USERLEN);
556 Count_newremoteclient(UserStats, user->server);
560 if (IsInvisible(sptr))
561 ++UserStats.inv_clients;
565 if (MyConnect(sptr)) {
566 cli_handler(sptr) = CLIENT_HANDLER;
567 release_dns_reply(sptr);
569 send_reply(sptr, RPL_WELCOME, nick);
571 * This is a duplicate of the NOTICE but see below...
573 send_reply(sptr, RPL_YOURHOST, cli_name(&me), version);
574 send_reply(sptr, RPL_CREATED, creation);
575 send_reply(sptr, RPL_MYINFO, cli_name(&me), version);
576 send_supported(sptr);
577 m_lusers(sptr, sptr, 1, parv);
580 /* nextping = CurrentTime; */
581 if (cli_snomask(sptr) & SNO_NOISY)
582 set_snomask(sptr, cli_snomask(sptr) & SNO_NOISY, SNO_ADD);
583 IPcheck_connect_succeeded(sptr);
586 /* if (IsServer(cptr)) */
588 struct Client *acptr;
590 acptr = user->server;
591 if (cli_from(acptr) != cli_from(sptr))
593 sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])",
594 sptr, cli_name(&me), cli_name(user->server), cli_name(cli_from(acptr)),
595 cli_sockhost(cli_from(acptr)));
596 cli_flags(sptr) |= FLAGS_KILLED;
597 return exit_client(cptr, sptr, &me, "NICK server wrong direction");
600 cli_flags(sptr) |= (cli_flags(acptr) & FLAGS_TS8);
603 * Check to see if this user is being propogated
604 * as part of a net.burst, or is using protocol 9.
605 * FIXME: This can be speeded up - its stupid to check it for
606 * every NICK message in a burst again --Run.
608 for (acptr = user->server; acptr != &me; acptr = cli_serv(acptr)->up) {
609 if (IsBurst(acptr) || Protocol(acptr) < 10)
612 if (!IPcheck_remote_connect(sptr, (acptr != &me))) {
614 * We ran out of bits to count this
616 sendcmdto_one(&me, CMD_KILL, sptr, "%C :%s (Too many connections from your host -- Ghost)",
617 sptr, cli_name(&me));
618 return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled");
621 tmpstr = umode_str(sptr);
622 sendcmdto_serv_butone(user->server, CMD_NICK, cptr,
623 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
624 nick, cli_hopcount(sptr) + 1, cli_lastnick(sptr),
625 user->username, user->realhost,
626 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
627 inttobase64(ip_base64, ntohl(cli_ip(sptr).s_addr), 6),
628 NumNick(sptr), cli_info(sptr));
630 /* Send umode to client */
633 send_umode(cptr, sptr, 0, ALL_UMODES);
634 if (cli_snomask(sptr) != SNO_DEFAULT && (cli_flags(sptr) & FLAGS_SERVNOTICE))
635 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
642 static const struct UserMode {
647 { FLAGS_LOCOP, 'O' },
648 { FLAGS_INVISIBLE, 'i' },
649 { FLAGS_WALLOP, 'w' },
650 { FLAGS_SERVNOTICE, 's' },
652 { FLAGS_CHSERV, 'k' },
653 { FLAGS_DEBUG, 'g' },
654 { FLAGS_ACCOUNT, 'r' },
655 { FLAGS_HIDDENHOST, 'x' }
658 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
661 * XXX - find a way to get rid of this
663 static char umodeBuf[BUFSIZE];
665 int set_nick_name(struct Client* cptr, struct Client* sptr,
666 const char* nick, int parc, char* parv[])
668 if (IsServer(sptr)) {
670 const char* account = 0;
674 * A server introducing a new client, change source
676 struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
677 assert(0 != new_client);
679 cli_hopcount(new_client) = atoi(parv[2]);
680 cli_lastnick(new_client) = atoi(parv[3]);
681 if (Protocol(cptr) > 9 && parc > 7 && *parv[6] == '+') {
682 for (p = parv[6] + 1; *p; p++) {
683 for (i = 0; i < USERMODELIST_SIZE; ++i) {
684 if (userModeList[i].c == *p) {
685 cli_flags(new_client) |= userModeList[i].flag;
686 if (userModeList[i].flag & FLAGS_ACCOUNT)
693 client_set_privs(new_client); /* set privs on user */
697 strcpy(cli_name(new_client), nick);
698 cli_user(new_client) = make_user(new_client);
699 cli_user(new_client)->server = sptr;
700 SetRemoteNumNick(new_client, parv[parc - 2]);
702 * IP# of remote client
704 cli_ip(new_client).s_addr = htonl(base64toint(parv[parc - 3]));
706 add_client_to_list(new_client);
707 hAddClient(new_client);
709 cli_serv(sptr)->ghost = 0; /* :server NICK means end of net.burst */
710 ircd_strncpy(cli_username(new_client), parv[4], USERLEN);
711 ircd_strncpy(cli_user(new_client)->host, parv[5], HOSTLEN);
712 ircd_strncpy(cli_user(new_client)->realhost, parv[5], HOSTLEN);
713 ircd_strncpy(cli_info(new_client), parv[parc - 1], REALLEN);
715 ircd_strncpy(cli_user(new_client)->account, account, ACCOUNTLEN);
716 if (HasHiddenHost(new_client))
717 ircd_snprintf(0, cli_user(new_client)->host, HOSTLEN, "%s.%s",
718 account, feature_str(FEAT_HIDDEN_HOST));
720 return register_user(cptr, new_client, cli_name(new_client), parv[4]);
722 else if ((cli_name(sptr))[0]) {
724 * Client changing its nick
726 * If the client belongs to me, then check to see
727 * if client is on any channels where it is currently
728 * banned. If so, do not allow the nick change to occur.
731 const char* channel_name;
732 struct Membership *member;
733 if ((channel_name = find_no_nickchange_channel(sptr))) {
734 return send_reply(cptr, ERR_BANNICKCHANGE, channel_name);
737 * Refuse nick change if the last nick change was less
738 * then 30 seconds ago. This is intended to get rid of
739 * clone bots doing NICK FLOOD. -SeKs
740 * If someone didn't change their nick for more then 60 seconds
741 * however, allow to do two nick changes immediately after another
742 * before limiting the nick flood. -Run
744 if (CurrentTime < cli_nextnick(cptr)) {
745 cli_nextnick(cptr) += 2;
746 send_reply(cptr, ERR_NICKTOOFAST, parv[1],
747 cli_nextnick(cptr) - CurrentTime);
748 /* Send error message */
749 sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cli_name(cptr));
750 /* bounce NICK to user */
751 return 0; /* ignore nick change! */
754 /* Limit total to 1 change per NICK_DELAY seconds: */
755 cli_nextnick(cptr) += NICK_DELAY;
756 /* However allow _maximal_ 1 extra consecutive nick change: */
757 if (cli_nextnick(cptr) < CurrentTime)
758 cli_nextnick(cptr) = CurrentTime;
760 /* Invalidate all bans against the user so we check them again */
761 for (member = (cli_user(cptr))->channel; member;
762 member = member->next_channel)
763 ClearBanValid(member);
766 * Also set 'lastnick' to current time, if changed.
768 if (0 != ircd_strcmp(parv[0], nick))
769 cli_lastnick(sptr) = (sptr == cptr) ? TStime() : atoi(parv[2]);
772 * Client just changing his/her nick. If he/she is
773 * on a channel, send note of change to all clients
774 * on that channel. Propagate notice to other servers.
777 sendcmdto_common_channels_butone(sptr, CMD_NICK, NULL, ":%s", nick);
778 add_history(sptr, 1);
779 sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick,
783 sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick);
785 if ((cli_name(sptr))[0])
787 strcpy(cli_name(sptr), nick);
791 /* Local client setting NICK the first time */
793 strcpy(cli_name(sptr), nick);
794 if (!cli_user(sptr)) {
795 cli_user(sptr) = make_user(sptr);
796 cli_user(sptr)->server = &me;
798 SetLocalNumNick(sptr);
802 * If the client hasn't gotten a cookie-ping yet,
803 * choose a cookie and send it. -record!jegelhof@cloud9.net
805 if (!cli_cookie(sptr)) {
807 cli_cookie(sptr) = (ircrandom() & 0x7fffffff);
808 } while (!cli_cookie(sptr));
809 sendrawto_one(cptr, MSG_PING " :%u", cli_cookie(sptr));
811 else if (*(cli_user(sptr))->host && cli_cookie(sptr) == COOKIE_VERIFIED) {
813 * USER and PONG already received, now we have NICK.
814 * register_user may reject the client and call exit_client
815 * for it - must test this and exit m_nick too !
817 cli_lastnick(sptr) = TStime(); /* Always local client */
818 if (register_user(cptr, sptr, nick, cli_user(sptr)->username) == CPTR_KILLED)
825 static unsigned char hash_target(unsigned int target)
827 return (unsigned char) (target >> 16) ^ (target >> 8);
833 * sptr must be a local client!
835 * Cannonifies target for client `sptr'.
837 void add_target(struct Client *sptr, void *target)
839 /* Ok, this shouldn't work esp on alpha
841 unsigned char hash = hash_target((unsigned long) target);
842 unsigned char* targets;
845 assert(cli_local(sptr));
847 targets = cli_targets(sptr);
851 for (i = 0; i < MAXTARGETS; ++i) {
852 if (targets[i] == hash)
858 memmove(&targets[RESERVEDTARGETS + 1],
859 &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
860 targets[RESERVEDTARGETS] = hash;
866 * sptr must be a local client !
868 * Returns 'true' (1) when too many targets are addressed.
869 * Returns 'false' (0) when it's ok to send to this target.
871 int check_target_limit(struct Client *sptr, void *target, const char *name,
874 unsigned char hash = hash_target((unsigned long) target);
876 unsigned char* targets;
879 assert(cli_local(sptr));
880 targets = cli_targets(sptr);
883 * Same target as last time?
885 if (targets[0] == hash)
887 for (i = 1; i < MAXTARGETS; ++i) {
888 if (targets[i] == hash) {
889 memmove(&targets[1], &targets[0], i);
898 if (CurrentTime < cli_nexttarget(sptr)) {
899 if (cli_nexttarget(sptr) - CurrentTime < TARGET_DELAY + 8) {
903 cli_nexttarget(sptr) += 2;
904 send_reply(sptr, ERR_TARGETTOOFAST, name,
905 cli_nexttarget(sptr) - CurrentTime);
910 cli_nexttarget(sptr) += TARGET_DELAY;
911 if (cli_nexttarget(sptr) < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)))
912 cli_nexttarget(sptr) = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1));
915 memmove(&targets[1], &targets[0], MAXTARGETS - 1);
921 * whisper - called from m_cnotice and m_cprivmsg.
923 * parv[0] = sender prefix
926 * parv[3] = Private message text
928 * Added 971023 by Run.
929 * Reason: Allows channel operators to sent an arbitrary number of private
930 * messages to users on their channel, avoiding the max.targets limit.
931 * Building this into m_private would use too much cpu because we'd have
932 * to a cross channel lookup for every private message!
933 * Note that we can't allow non-chan ops to use this command, it would be
934 * abused by mass advertisers.
937 int whisper(struct Client* source, const char* nick, const char* channel,
938 const char* text, int is_notice)
941 struct Channel* chptr;
942 struct Membership* membership;
946 assert(0 != channel);
947 assert(MyUser(source));
949 if (!(dest = FindUser(nick))) {
950 return send_reply(source, ERR_NOSUCHNICK, nick);
952 if (!(chptr = FindChannel(channel))) {
953 return send_reply(source, ERR_NOSUCHCHANNEL, channel);
956 * compare both users channel lists, instead of the channels user list
957 * since the link is the same, this should be a little faster for channels
958 * with a lot of users
960 for (membership = cli_user(source)->channel; membership; membership = membership->next_channel) {
961 if (chptr == membership->channel)
964 if (0 == membership) {
965 return send_reply(source, ERR_NOTONCHANNEL, chptr->chname);
967 if (!IsVoicedOrOpped(membership)) {
968 return send_reply(source, ERR_VOICENEEDED, chptr->chname);
971 * lookup channel in destination
973 assert(0 != cli_user(dest));
974 for (membership = cli_user(dest)->channel; membership; membership = membership->next_channel) {
975 if (chptr == membership->channel)
978 if (0 == membership || IsZombie(membership)) {
979 return send_reply(source, ERR_USERNOTINCHANNEL, cli_name(dest), chptr->chname);
981 if (is_silenced(source, dest))
984 if (cli_user(dest)->away)
985 send_reply(source, RPL_AWAY, cli_name(dest), cli_user(dest)->away);
987 sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text);
989 sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text);
995 * added Sat Jul 25 07:30:42 EST 1992
997 void send_umode_out(struct Client *cptr, struct Client *sptr, int old,
1001 struct Client *acptr;
1003 send_umode(NULL, sptr, old, SEND_UMODES & ~(prop ? 0 : FLAGS_OPER));
1005 for (i = HighestFd; i >= 0; i--) {
1006 if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
1007 (acptr != cptr) && (acptr != sptr) && *umodeBuf)
1008 sendcmdto_one(sptr, CMD_MODE, acptr, "%s :%s", cli_name(sptr), umodeBuf);
1010 if (cptr && MyUser(cptr))
1011 send_umode(cptr, sptr, old, ALL_UMODES);
1016 * send_user_info - send user info userip/userhost
1017 * NOTE: formatter must put info into buffer and return a pointer to the end of
1018 * the data it put in the buffer.
1020 void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
1025 int users_found = 0;
1026 struct Client* acptr;
1033 mb = msgq_make(sptr, rpl_str(rpl), cli_name(&me), cli_name(sptr));
1035 for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
1036 if ((acptr = FindUser(name))) {
1038 msgq_append(0, mb, " ");
1039 (*fmt)(acptr, sptr, mb);
1041 if (5 == ++arg_count)
1044 send_buffer(sptr, mb, 0);
1051 * If, after setting the flags, the user has both HiddenHost and Account
1052 * set, its hostmask is changed.
1054 #define FLAGS_HOST_HIDDEN (FLAGS_ACCOUNT|FLAGS_HIDDENHOST)
1055 int hide_hostmask(struct Client *cptr, unsigned int flags)
1057 struct Membership *chan;
1060 if (MyConnect(cptr) && !feature_bool(FEAT_HOST_HIDING))
1061 flags &= ~FLAGS_HIDDENHOST;
1063 newflags = cli_flags(cptr) | flags;
1064 if ((newflags & FLAGS_HOST_HIDDEN) != FLAGS_HOST_HIDDEN) {
1065 /* The user doesn't have both flags, don't change the hostmask */
1066 cli_flags(cptr) |= flags;
1070 sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Registered");
1071 ircd_snprintf(0, cli_user(cptr)->host, HOSTLEN, "%s.%s",
1072 cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
1073 cli_flags(cptr) |= flags;
1076 * Go through all channels the client was on, rejoin him
1077 * and set the modes, if any
1079 for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel) {
1080 sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr,
1081 "%H", chan->channel);
1082 if (IsChanOp(chan) && HasVoice(chan)) {
1083 sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr,
1084 "%H +ov %C %C", chan->channel, cptr, cptr);
1085 } else if (IsChanOp(chan) || HasVoice(chan)) {
1086 sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr,
1087 "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr);
1095 * set_user_mode() added 15/10/91 By Darren Reed.
1098 * parv[1] - username to change mode for
1099 * parv[2] - modes to change
1101 int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
1105 struct Client *acptr;
1109 unsigned int tmpmask = 0;
1110 int snomask_given = 0;
1113 int do_host_hiding = 0;
1118 return need_more_params(sptr, "MODE");
1120 if (!(acptr = FindUser(parv[1])))
1122 if (MyConnect(sptr))
1123 send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]);
1127 if (IsServer(sptr) || sptr != acptr)
1130 sendwallto_group_butone(&me, WALL_WALLOPS, 0,
1131 "MODE for User %s from %s!%s", parv[1],
1132 cli_name(cptr), cli_name(sptr));
1134 send_reply(sptr, ERR_USERSDONTMATCH);
1142 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1143 if ((userModeList[i].flag & cli_flags(sptr)) &&
1144 !(userModeList[i].flag & FLAGS_ACCOUNT))
1145 *m++ = userModeList[i].c;
1148 send_reply(sptr, RPL_UMODEIS, buf);
1149 if ((cli_flags(sptr) & FLAGS_SERVNOTICE) && MyConnect(sptr)
1150 && cli_snomask(sptr) !=
1151 (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT))
1152 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1157 * find flags already set for user
1158 * why not just copy them?
1160 setflags = cli_flags(sptr);
1162 if (MyConnect(sptr))
1163 tmpmask = cli_snomask(sptr);
1166 * parse mode change string(s)
1168 for (p = &parv[2]; *p; p++) { /* p is changed in loop too */
1169 for (m = *p; *m; m++) {
1178 if (*(p + 1) && is_snomask(*(p + 1))) {
1180 tmpmask = umode_make_snomask(tmpmask, *++p, what);
1181 tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1184 tmpmask = (what == MODE_ADD) ?
1185 (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1187 SetServNotice(sptr);
1189 ClearServNotice(sptr);
1192 if (what == MODE_ADD)
1198 if (what == MODE_ADD)
1201 cli_flags(sptr) &= ~(FLAGS_OPER | FLAGS_LOCOP);
1202 if (MyConnect(sptr)) {
1203 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1204 cli_handler(sptr) = CLIENT_HANDLER;
1209 if (what == MODE_ADD)
1212 cli_flags(sptr) &= ~(FLAGS_OPER | FLAGS_LOCOP);
1213 if (MyConnect(sptr)) {
1214 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1215 cli_handler(sptr) = CLIENT_HANDLER;
1220 if (what == MODE_ADD)
1223 ClearInvisible(sptr);
1226 if (what == MODE_ADD)
1232 if (what == MODE_ADD)
1233 SetChannelService(sptr);
1235 ClearChannelService(sptr);
1238 if (what == MODE_ADD)
1244 if (what == MODE_ADD)
1253 * Evaluate rules for new user mode
1254 * Stop users making themselves operators too easily:
1256 if (!IsServer(cptr)) {
1257 if (!(setflags & FLAGS_OPER) && IsOper(sptr))
1259 if (!(setflags & FLAGS_LOCOP) && IsLocOp(sptr))
1262 * new umode; servers can set it, local users cannot;
1263 * prevents users from /kick'ing or /mode -o'ing
1265 if (!(setflags & FLAGS_CHSERV))
1266 ClearChannelService(sptr);
1268 * only send wallops to opers
1270 if (feature_bool(FEAT_WALLOPS_OPER_ONLY) && !IsAnOper(sptr) &&
1271 !(setflags & FLAGS_WALLOP))
1273 #ifdef SERVNOTICE_OPER_ONLY
1274 if (MyConnect(sptr) && !IsAnOper(sptr) && !(setflags & FLAGS_SERVNOTICE)) {
1275 ClearServNotice(sptr);
1276 set_snomask(sptr, 0, SNO_SET);
1279 #ifdef DEBUG_OPER_ONLY
1280 if (!IsAnOper(sptr) && !(setflags & FLAGS_DEBUG))
1284 if (MyConnect(sptr)) {
1285 if ((setflags & (FLAGS_OPER | FLAGS_LOCOP)) && !IsAnOper(sptr))
1286 det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPS);
1288 if (SendServNotice(sptr)) {
1289 if (tmpmask != cli_snomask(sptr))
1290 set_snomask(sptr, tmpmask, SNO_SET);
1291 if (cli_snomask(sptr) && snomask_given)
1292 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1294 set_snomask(sptr, 0, SNO_SET);
1297 * Compare new flags with old flags and send string which
1298 * will cause servers to update correctly.
1300 if (!(setflags & FLAGS_OPER) && IsOper(sptr)) { /* user now oper */
1302 client_set_privs(sptr); /* may set propagate privilege */
1304 if (HasPriv(sptr, PRIV_PROPAGATE)) /* remember propagate privilege setting */
1306 if ((setflags & FLAGS_OPER) && !IsOper(sptr)) { /* user no longer oper */
1308 client_set_privs(sptr); /* will clear propagate privilege */
1310 if ((setflags & FLAGS_INVISIBLE) && !IsInvisible(sptr))
1311 --UserStats.inv_clients;
1312 if (!(setflags & FLAGS_INVISIBLE) && IsInvisible(sptr))
1313 ++UserStats.inv_clients;
1314 if (!(setflags & FLAGS_HIDDENHOST) && do_host_hiding)
1315 hide_hostmask(sptr, FLAGS_HIDDENHOST);
1316 send_umode_out(cptr, sptr, setflags, prop);
1322 * Build umode string for BURST command
1325 char *umode_str(struct Client *cptr)
1327 char* m = umodeBuf; /* Maximum string size: "owidg\0" */
1331 c_flags = cli_flags(cptr) & SEND_UMODES; /* cleaning up the original code */
1332 if (HasPriv(cptr, PRIV_PROPAGATE))
1333 c_flags |= FLAGS_OPER;
1335 c_flags &= ~FLAGS_OPER;
1337 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1338 if ( (c_flags & userModeList[i].flag))
1339 *m++ = userModeList[i].c;
1342 if (IsAccount(cptr)) {
1343 char* t = cli_user(cptr)->account;
1346 while ((*m++ = *t++))
1352 return umodeBuf; /* Note: static buffer, gets
1353 overwritten by send_umode() */
1357 * Send the MODE string for user (user) to connection cptr
1360 void send_umode(struct Client *cptr, struct Client *sptr, int old, int sendmask)
1365 int what = MODE_NULL;
1368 * Build a string in umodeBuf to represent the change in the user's
1369 * mode between the new (sptr->flag) and 'old'.
1373 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1374 flag = userModeList[i].flag;
1375 if (MyUser(sptr) && !(flag & sendmask))
1377 if ( (flag & old) && !(cli_flags(sptr) & flag))
1379 if (what == MODE_DEL)
1380 *m++ = userModeList[i].c;
1385 *m++ = userModeList[i].c;
1388 else if (!(flag & old) && (cli_flags(sptr) & flag))
1390 if (what == MODE_ADD)
1391 *m++ = userModeList[i].c;
1396 *m++ = userModeList[i].c;
1401 if (*umodeBuf && cptr)
1402 sendcmdto_one(sptr, CMD_MODE, cptr, "%s :%s", cli_name(sptr), umodeBuf);
1406 * Check to see if this resembles a sno_mask. It is if 1) there is
1407 * at least one digit and 2) The first digit occurs before the first
1408 * alphabetic character.
1410 int is_snomask(char *word)
1414 for (; *word; word++)
1417 else if (IsAlpha(*word))
1424 * If it begins with a +, count this as an additive mask instead of just
1425 * a replacement. If what == MODE_DEL, "+" has no special effect.
1427 unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
1429 unsigned int sno_what;
1430 unsigned int newmask;
1434 if (what == MODE_ADD)
1439 else if (*arg == '-')
1442 if (what == MODE_ADD)
1448 sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL;
1449 /* pity we don't have strtoul everywhere */
1450 newmask = (unsigned int)atoi(arg);
1451 if (sno_what == SNO_DEL)
1452 newmask = oldmask & ~newmask;
1453 else if (sno_what == SNO_ADD)
1458 static void delfrom_list(struct Client *cptr, struct SLink **list)
1461 struct SLink* prv = NULL;
1463 for (tmp = *list; tmp; tmp = tmp->next) {
1464 if (tmp->value.cptr == cptr) {
1466 prv->next = tmp->next;
1477 * This function sets a Client's server notices mask, according to
1478 * the parameter 'what'. This could be even faster, but the code
1479 * gets mighty hard to read :)
1481 void set_snomask(struct Client *cptr, unsigned int newmask, int what)
1483 unsigned int oldmask, diffmask; /* unsigned please */
1487 oldmask = cli_snomask(cptr);
1489 if (what == SNO_ADD)
1491 else if (what == SNO_DEL)
1492 newmask = oldmask & ~newmask;
1493 else if (what != SNO_SET) /* absolute set, no math needed */
1494 sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what);
1496 newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
1498 diffmask = oldmask ^ newmask;
1500 for (i = 0; diffmask >> i; i++) {
1501 if (((diffmask >> i) & 1))
1503 if (((newmask >> i) & 1))
1506 tmp->next = opsarray[i];
1507 tmp->value.cptr = cptr;
1511 /* not real portable :( */
1512 delfrom_list(cptr, &opsarray[i]);
1515 cli_snomask(cptr) = newmask;
1519 * is_silenced : Does the actual check wether sptr is allowed
1520 * to send a message to acptr.
1521 * Both must be registered persons.
1522 * If sptr is silenced by acptr, his message should not be propagated,
1523 * but more over, if this is detected on a server not local to sptr
1524 * the SILENCE mask is sent upstream.
1526 int is_silenced(struct Client *sptr, struct Client *acptr)
1530 static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
1531 static char senderip[16 + NICKLEN + USERLEN + 5];
1532 static char senderh[HOSTLEN + ACCOUNTLEN + USERLEN + 6];
1534 if (!cli_user(acptr) || !(lp = cli_user(acptr)->silence) || !(user = cli_user(sptr)))
1536 ircd_snprintf(0, sender, sizeof(sender), "%s!%s@%s", cli_name(sptr),
1537 user->username, user->host);
1538 ircd_snprintf(0, senderip, sizeof(senderip), "%s!%s@%s", cli_name(sptr),
1539 user->username, ircd_ntoa((const char*) &(cli_ip(sptr))));
1540 if (HasHiddenHost(sptr))
1541 ircd_snprintf(0, senderh, sizeof(senderh), "%s!%s@%s", cli_name(sptr),
1542 user->username, user->realhost);
1543 for (; lp; lp = lp->next)
1545 if ((!(lp->flags & CHFL_SILENCE_IPMASK) && (!match(lp->value.cp, sender) ||
1546 (HasHiddenHost(sptr) && !match(lp->value.cp, senderh)))) ||
1547 ((lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, senderip)))
1549 if (!MyConnect(sptr))
1551 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr,
1563 * Removes all silence masks from the list of sptr that fall within `mask'
1564 * Returns -1 if none where found, 0 otherwise.
1566 int del_silence(struct Client *sptr, char *mask)
1572 for (lp = &(cli_user(sptr))->silence; *lp;) {
1573 if (!mmatch(mask, (*lp)->value.cp))
1577 MyFree(tmp->value.cp);
1587 int add_silence(struct Client* sptr, const char* mask)
1589 struct SLink *lp, **lpp;
1590 int cnt = 0, len = strlen(mask);
1593 for (lpp = &(cli_user(sptr))->silence, lp = *lpp; lp;)
1595 if (0 == ircd_strcmp(mask, lp->value.cp))
1597 if (!mmatch(mask, lp->value.cp))
1599 struct SLink *tmp = lp;
1600 *lpp = lp = lp->next;
1601 MyFree(tmp->value.cp);
1607 len += strlen(lp->value.cp);
1608 if ((len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXSILES))) ||
1609 (++cnt >= feature_int(FEAT_MAXSILES)))
1611 send_reply(sptr, ERR_SILELISTFULL, mask);
1614 else if (!mmatch(lp->value.cp, mask))
1621 memset(lp, 0, sizeof(struct SLink));
1622 lp->next = cli_user(sptr)->silence;
1623 lp->value.cp = (char*) MyMalloc(strlen(mask) + 1);
1624 assert(0 != lp->value.cp);
1625 strcpy(lp->value.cp, mask);
1626 if ((ip_start = strrchr(mask, '@')) && check_if_ipmask(ip_start + 1))
1627 lp->flags = CHFL_SILENCE_IPMASK;
1628 cli_user(sptr)->silence = lp;
1633 send_supported(struct Client *cptr)
1635 char featurebuf[512];
1637 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES1, FEATURESVALUES1);
1638 send_reply(cptr, RPL_ISUPPORT, featurebuf);
1639 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES2, FEATURESVALUES2);
1640 send_reply(cptr, RPL_ISUPPORT, featurebuf);
1642 return 0; /* convenience return, if it's ever needed */