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_string.h"
49 #include "querycmds.h"
55 #include "s_serv.h" /* max_client_count */
57 #include "sprintf_irc.h"
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 /* Make sure it's a server */
200 /* Make sure it's a server */
201 if (!strchr(to, '*')) {
202 if (0 == (acptr = FindClient(to)))
203 return HUNTED_NOSUCH;
206 acptr = cli_user(acptr)->server;
207 } else if (!(acptr = find_match_server(to))) {
208 send_reply(from, ERR_NOSUCHSERVER, to);
209 return (HUNTED_NOSUCH);
211 } else if (!(acptr = FindNServer(to)))
212 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
215 return (HUNTED_ISME);
217 if (MustBeOper && !IsPrivileged(from)) {
218 send_reply(from, ERR_NOPRIVILEGES);
219 return HUNTED_NOSUCH;
222 assert(!IsServer(from));
224 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
226 sendcmdto_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
227 parv[4], parv[5], parv[6], parv[7], parv[8]);
229 return (HUNTED_PASS);
232 int hunt_server_prio_cmd(struct Client *from, const char *cmd, const char *tok,
233 struct Client *one, int MustBeOper,
234 const char *pattern, int server, int parc,
237 struct Client *acptr;
240 /* Assume it's me, if no server or an unregistered client */
241 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
242 return (HUNTED_ISME);
244 /* Make sure it's a server */
246 /* Make sure it's a server */
247 if (!strchr(to, '*')) {
248 if (0 == (acptr = FindClient(to)))
249 return HUNTED_NOSUCH;
252 acptr = cli_user(acptr)->server;
253 } else if (!(acptr = find_match_server(to))) {
254 send_reply(from, ERR_NOSUCHSERVER, to);
255 return (HUNTED_NOSUCH);
257 } else if (!(acptr = FindNServer(to)))
258 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
261 return (HUNTED_ISME);
263 if (MustBeOper && !IsPrivileged(from)) {
264 send_reply(from, ERR_NOPRIVILEGES);
265 return HUNTED_NOSUCH;
268 assert(!IsServer(from));
270 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
272 sendcmdto_prio_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
273 parv[4], parv[5], parv[6], parv[7], parv[8]);
275 return (HUNTED_PASS);
279 * 'do_nick_name' ensures that the given parameter (nick) is really a proper
280 * string for a nickname (note, the 'nick' may be modified in the process...)
282 * RETURNS the length of the final NICKNAME (0, if nickname is invalid)
284 * Nickname characters are in range 'A'..'}', '_', '-', '0'..'9'
285 * anything outside the above set will terminate nickname.
286 * In addition, the first character cannot be '-' or a Digit.
289 * The '~'-character should be allowed, but a change should be global,
290 * some confusion would result if only few servers allowed it...
292 int do_nick_name(char* nick)
295 char* end = ch + NICKLEN;
298 if (*ch == '-' || IsDigit(*ch)) /* first character in [0..9-] */
301 for ( ; (ch < end) && *ch; ++ch)
302 if (!IsNickChar(*ch))
313 * Copy `source' to `dest', replacing all occurances of '~' and characters that
314 * are not `isIrcUi' by an underscore.
315 * Copies at most USERLEN - 1 characters or up till the first control character.
316 * If `tilde' is true, then a tilde is prepended to `dest'.
317 * Note that `dest' and `source' can point to the same area or to different
318 * non-overlapping areas.
320 static char *clean_user_id(char *dest, char *source, int tilde)
327 ch = *s++; /* Store first character to copy: */
330 *d++ = '~'; /* If `dest' == `source', then this overwrites `ch' */
333 while (ch && !IsCntrl(ch) && rlen--)
335 char nch = *s++; /* Store next character to copy */
336 *d++ = IsUserChar(ch) ? ch : '_'; /* This possibly overwrites it */
349 * This function is called when both NICK and USER messages
350 * have been accepted for the client, in whatever order. Only
351 * after this the USER message is propagated.
353 * NICK's must be propagated at once when received, although
354 * it would be better to delay them too until full info is
355 * available. Doing it is not so simple though, would have
356 * to implement the following:
358 * 1) user telnets in and gives only "NICK foobar" and waits
359 * 2) another user far away logs in normally with the nick
360 * "foobar" (quite legal, as this server didn't propagate it).
361 * 3) now this server gets nick "foobar" from outside, but
362 * has already the same defined locally. Current server
363 * would just issue "KILL foobar" to clean out dups. But,
364 * this is not fair. It should actually request another
365 * nick from local user or kill him/her...
367 int register_user(struct Client *cptr, struct Client *sptr,
368 const char *nick, char *username)
370 struct ConfItem* aconf;
374 char c = 0; /* not alphanum */
375 char d = 'a'; /* not a digit */
383 short digitgroups = 0;
384 struct User* user = cli_user(sptr);
386 char featurebuf[512];
388 user->last = CurrentTime;
389 parv[0] = cli_name(sptr);
390 parv[1] = parv[2] = NULL;
394 static time_t last_too_many1;
395 static time_t last_too_many2;
397 assert(cptr == sptr);
398 switch (conf_check_client(sptr))
402 case ACR_NO_AUTHORIZATION:
403 sendto_opmask_butone(0, SNO_UNAUTH, "Unauthorized connection from %s.",
404 get_client_name(sptr, HIDE_IP));
405 ++ServerStats->is_ref;
406 return exit_client(cptr, sptr, &me,
407 "No Authorization - use another server");
408 case ACR_TOO_MANY_IN_CLASS:
409 if (CurrentTime - last_too_many1 >= (time_t) 60)
411 last_too_many1 = CurrentTime;
412 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections in "
413 "class %i for %s.", get_client_class(sptr),
414 get_client_name(sptr, SHOW_IP));
416 ++ServerStats->is_ref;
417 IPcheck_connect_fail(cli_ip(sptr));
418 return exit_client(cptr, sptr, &me,
419 "Sorry, your connection class is full - try "
420 "again later or try another server");
421 case ACR_TOO_MANY_FROM_IP:
422 if (CurrentTime - last_too_many2 >= (time_t) 60)
424 last_too_many2 = CurrentTime;
425 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections from "
427 get_client_name(sptr, SHOW_IP));
429 ++ServerStats->is_ref;
430 return exit_client(cptr, sptr, &me,
431 "Too many connections from your host");
432 case ACR_ALREADY_AUTHORIZED:
433 /* Can this ever happen? */
435 ++ServerStats->is_ref;
436 IPcheck_connect_fail(cli_ip(sptr));
437 return exit_client(cptr, sptr, &me, "Unknown error -- Try again");
439 ircd_strncpy(user->host, cli_sockhost(sptr), HOSTLEN);
440 aconf = cli_confs(sptr)->value.aconf;
442 clean_user_id(user->username,
443 (cli_flags(sptr) & FLAGS_GOTID) ? cli_username(sptr) : username,
444 (cli_flags(sptr) & FLAGS_DOID) && !(cli_flags(sptr) & FLAGS_GOTID));
446 if ((user->username[0] == '\0')
447 || ((user->username[0] == '~') && (user->username[1] == '\000')))
448 return exit_client(cptr, sptr, &me, "USER: Bogus userid.");
450 if (!EmptyString(aconf->passwd)
451 && !(IsDigit(*aconf->passwd) && !aconf->passwd[1])
452 && strcmp(cli_passwd(sptr), aconf->passwd))
454 ServerStats->is_ref++;
455 IPcheck_connect_fail(cli_ip(sptr));
456 send_reply(sptr, ERR_PASSWDMISMATCH);
457 return exit_client(cptr, sptr, &me, "Bad Password");
459 memset(cli_passwd(sptr), 0, sizeof(cli_passwd(sptr)));
461 * following block for the benefit of time-dependent K:-lines
463 if (find_kill(sptr)) {
464 ServerStats->is_ref++;
465 IPcheck_connect_fail(cli_ip(sptr));
466 return exit_client(cptr, sptr, &me, "K-lined");
469 * Check for mixed case usernames, meaning probably hacked. Jon2 3-94
470 * Summary of rules now implemented in this patch: Ensor 11-94
471 * In a mixed-case name, if first char is upper, one more upper may
472 * appear anywhere. (A mixed-case name *must* have an upper first
473 * char, and may have one other upper.)
474 * A third upper may appear if all 3 appear at the beginning of the
475 * name, separated only by "others" (-/_/.).
476 * A single group of digits is allowed anywhere.
477 * Two groups of digits are allowed if at least one of the groups is
478 * at the beginning or the end.
479 * Only one '-', '_', or '.' is allowed (or two, if not consecutive).
480 * But not as the first or last char.
481 * No other special characters are allowed.
482 * Name must contain at least one letter.
484 tmpstr2 = tmpstr = (username[0] == '~' ? &username[1] : username);
485 while (*tmpstr && !badid)
497 if ((leadcaps || pos == 1) && !lower && !digits)
503 if (pos == 1 || !IsDigit(d))
510 else if (c == '-' || c == '_' || c == '.')
515 else if (d == '-' || d == '_' || d == '.' || other > 2)
524 if (lower && upper && (!leadcaps || leadcaps > 3 ||
525 (upper > 2 && upper > leadcaps)))
527 else if (digitgroups == 2 && !(IsDigit(tmpstr2[0]) || IsDigit(c)))
529 else if ((!lower && !upper) || !IsAlnum(c))
532 if (badid && (!(cli_flags(sptr) & FLAGS_GOTID) ||
533 strcmp(cli_username(sptr), username) != 0))
535 ServerStats->is_ref++;
537 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
538 ":Your username is invalid.");
539 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
540 ":Connect with your real username, in lowercase.");
541 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
542 ":If your mail address were foo@bar.com, your username "
544 return exit_client(cptr, sptr, &me, "USER: Bad username");
546 Count_unknownbecomesclient(sptr, UserStats);
549 ircd_strncpy(user->username, username, USERLEN);
550 Count_newremoteclient(UserStats, user->server);
554 if (IsInvisible(sptr))
555 ++UserStats.inv_clients;
559 if (MyConnect(sptr)) {
560 cli_handler(sptr) = CLIENT_HANDLER;
561 release_dns_reply(sptr);
563 send_reply(sptr, RPL_WELCOME, nick);
565 * This is a duplicate of the NOTICE but see below...
567 send_reply(sptr, RPL_YOURHOST, cli_name(&me), version);
568 send_reply(sptr, RPL_CREATED, creation);
569 send_reply(sptr, RPL_MYINFO, cli_name(&me), version);
570 sprintf_irc(featurebuf,FEATURES,FEATURESVALUES);
571 send_reply(sptr, RPL_ISUPPORT, featurebuf);
572 m_lusers(sptr, sptr, 1, parv);
575 /* nextping = CurrentTime; */
576 if (cli_snomask(sptr) & SNO_NOISY)
577 set_snomask(sptr, cli_snomask(sptr) & SNO_NOISY, SNO_ADD);
578 IPcheck_connect_succeeded(sptr);
581 /* if (IsServer(cptr)) */
583 struct Client *acptr;
585 acptr = user->server;
586 if (cli_from(acptr) != cli_from(sptr))
588 sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])",
589 sptr, cli_name(&me), cli_name(user->server), cli_name(cli_from(acptr)),
590 cli_sockhost(cli_from(acptr)));
591 cli_flags(sptr) |= FLAGS_KILLED;
592 return exit_client(cptr, sptr, &me, "NICK server wrong direction");
595 cli_flags(sptr) |= (cli_flags(acptr) & FLAGS_TS8);
598 * Check to see if this user is being propogated
599 * as part of a net.burst, or is using protocol 9.
600 * FIXME: This can be speeded up - its stupid to check it for
601 * every NICK message in a burst again --Run.
603 for (acptr = user->server; acptr != &me; acptr = cli_serv(acptr)->up) {
604 if (IsBurst(acptr) || Protocol(acptr) < 10)
607 if (!IPcheck_remote_connect(sptr, (acptr != &me))) {
609 * We ran out of bits to count this
611 sendcmdto_one(&me, CMD_KILL, sptr, "%C :%s (Too many connections from your host -- Ghost)",
612 sptr, cli_name(&me));
613 return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled");
616 tmpstr = umode_str(sptr);
617 sendcmdto_serv_butone(user->server, CMD_NICK, cptr,
618 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
619 nick, cli_hopcount(sptr) + 1, cli_lastnick(sptr),
620 user->username, user->host,
621 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
622 inttobase64(ip_base64, ntohl(cli_ip(sptr).s_addr), 6),
623 NumNick(sptr), cli_info(sptr));
625 /* Send umode to client */
628 send_umode(cptr, sptr, 0, ALL_UMODES);
629 if (cli_snomask(sptr) != SNO_DEFAULT && (cli_flags(sptr) & FLAGS_SERVNOTICE))
630 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
637 static const struct UserMode {
642 { FLAGS_LOCOP, 'O' },
643 { FLAGS_INVISIBLE, 'i' },
644 { FLAGS_WALLOP, 'w' },
645 { FLAGS_SERVNOTICE, 's' },
647 { FLAGS_CHSERV, 'k' },
651 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
654 * XXX - find a way to get rid of this
656 static char umodeBuf[BUFSIZE];
658 int set_nick_name(struct Client* cptr, struct Client* sptr,
659 const char* nick, int parc, char* parv[])
661 if (IsServer(sptr)) {
666 * A server introducing a new client, change source
668 struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
669 assert(0 != new_client);
671 cli_hopcount(new_client) = atoi(parv[2]);
672 cli_lastnick(new_client) = atoi(parv[3]);
673 if (Protocol(cptr) > 9 && parc > 7 && *parv[6] == '+') {
674 for (p = parv[6] + 1; *p; p++) {
675 for (i = 0; i < USERMODELIST_SIZE; ++i) {
676 if (userModeList[i].c == *p) {
677 cli_flags(new_client) |= userModeList[i].flag;
683 client_set_privs(new_client); /* set privs on user */
687 strcpy(cli_name(new_client), nick);
688 cli_user(new_client) = make_user(new_client);
689 cli_user(new_client)->server = sptr;
690 SetRemoteNumNick(new_client, parv[parc - 2]);
692 * IP# of remote client
694 cli_ip(new_client).s_addr = htonl(base64toint(parv[parc - 3]));
696 add_client_to_list(new_client);
697 hAddClient(new_client);
699 cli_serv(sptr)->ghost = 0; /* :server NICK means end of net.burst */
700 ircd_strncpy(cli_username(new_client), parv[4], USERLEN);
701 ircd_strncpy(cli_user(new_client)->host, parv[5], HOSTLEN);
702 ircd_strncpy(cli_info(new_client), parv[parc - 1], REALLEN);
704 return register_user(cptr, new_client, cli_name(new_client), parv[4]);
706 else if ((cli_name(sptr))[0]) {
708 * Client changing its nick
710 * If the client belongs to me, then check to see
711 * if client is on any channels where it is currently
712 * banned. If so, do not allow the nick change to occur.
715 const char* channel_name;
716 if ((channel_name = find_no_nickchange_channel(sptr))) {
717 return send_reply(cptr, ERR_BANNICKCHANGE, channel_name);
720 * Refuse nick change if the last nick change was less
721 * then 30 seconds ago. This is intended to get rid of
722 * clone bots doing NICK FLOOD. -SeKs
723 * If someone didn't change their nick for more then 60 seconds
724 * however, allow to do two nick changes immedately after another
725 * before limiting the nick flood. -Run
727 if (CurrentTime < cli_nextnick(cptr)) {
728 cli_nextnick(cptr) += 2;
729 send_reply(cptr, ERR_NICKTOOFAST, parv[1],
730 cli_nextnick(cptr) - CurrentTime);
731 /* Send error message */
732 sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cli_name(cptr));
733 /* bounce NICK to user */
734 return 0; /* ignore nick change! */
737 /* Limit total to 1 change per NICK_DELAY seconds: */
738 cli_nextnick(cptr) += NICK_DELAY;
739 /* However allow _maximal_ 1 extra consecutive nick change: */
740 if (cli_nextnick(cptr) < CurrentTime)
741 cli_nextnick(cptr) = CurrentTime;
745 * Also set 'lastnick' to current time, if changed.
747 if (0 != ircd_strcmp(parv[0], nick))
748 cli_lastnick(sptr) = (sptr == cptr) ? TStime() : atoi(parv[2]);
751 * Client just changing his/her nick. If he/she is
752 * on a channel, send note of change to all clients
753 * on that channel. Propagate notice to other servers.
756 sendcmdto_common_channels(sptr, CMD_NICK, ":%s", nick);
757 add_history(sptr, 1);
758 sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick,
762 sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick);
764 if ((cli_name(sptr))[0])
766 strcpy(cli_name(sptr), nick);
770 /* Local client setting NICK the first time */
772 strcpy(cli_name(sptr), nick);
773 if (!cli_user(sptr)) {
774 cli_user(sptr) = make_user(sptr);
775 cli_user(sptr)->server = &me;
777 SetLocalNumNick(sptr);
781 * If the client hasn't gotten a cookie-ping yet,
782 * choose a cookie and send it. -record!jegelhof@cloud9.net
784 if (!cli_cookie(sptr)) {
786 cli_cookie(sptr) = (ircrandom() & 0x7fffffff);
787 } while (!cli_cookie(sptr));
788 sendrawto_one(cptr, MSG_PING " :%u", cli_cookie(sptr));
790 else if (*(cli_user(sptr))->host && cli_cookie(sptr) == COOKIE_VERIFIED) {
792 * USER and PONG already received, now we have NICK.
793 * register_user may reject the client and call exit_client
794 * for it - must test this and exit m_nick too !
796 cli_lastnick(sptr) = TStime(); /* Always local client */
797 if (register_user(cptr, sptr, nick, cli_user(sptr)->username) == CPTR_KILLED)
804 static unsigned char hash_target(unsigned int target)
806 return (unsigned char) (target >> 16) ^ (target >> 8);
812 * sptr must be a local client!
814 * Cannonifies target for client `sptr'.
816 void add_target(struct Client *sptr, void *target)
818 /* Ok, this shouldn't work esp on alpha
820 unsigned char hash = hash_target((unsigned long) target);
821 unsigned char* targets;
824 assert(cli_local(sptr));
826 targets = cli_targets(sptr);
830 for (i = 0; i < MAXTARGETS; ++i) {
831 if (targets[i] == hash)
837 memmove(&targets[RESERVEDTARGETS + 1],
838 &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
839 targets[RESERVEDTARGETS] = hash;
845 * sptr must be a local client !
847 * Returns 'true' (1) when too many targets are addressed.
848 * Returns 'false' (0) when it's ok to send to this target.
850 int check_target_limit(struct Client *sptr, void *target, const char *name,
853 unsigned char hash = hash_target((unsigned long) target);
855 unsigned char* targets;
858 assert(cli_local(sptr));
859 targets = cli_targets(sptr);
862 * Same target as last time?
864 if (targets[0] == hash)
866 for (i = 1; i < MAXTARGETS; ++i) {
867 if (targets[i] == hash) {
868 memmove(&targets[1], &targets[0], i);
877 if (CurrentTime < cli_nexttarget(sptr)) {
878 if (cli_nexttarget(sptr) - CurrentTime < TARGET_DELAY + 8) {
882 cli_nexttarget(sptr) += 2;
883 send_reply(sptr, ERR_TARGETTOOFAST, name,
884 cli_nexttarget(sptr) - CurrentTime);
889 cli_nexttarget(sptr) += TARGET_DELAY;
890 if (cli_nexttarget(sptr) < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)))
891 cli_nexttarget(sptr) = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1));
894 memmove(&targets[1], &targets[0], MAXTARGETS - 1);
900 * whisper - called from m_cnotice and m_cprivmsg.
902 * parv[0] = sender prefix
905 * parv[3] = Private message text
907 * Added 971023 by Run.
908 * Reason: Allows channel operators to sent an arbitrary number of private
909 * messages to users on their channel, avoiding the max.targets limit.
910 * Building this into m_private would use too much cpu because we'd have
911 * to a cross channel lookup for every private message!
912 * Note that we can't allow non-chan ops to use this command, it would be
913 * abused by mass advertisers.
916 int whisper(struct Client* source, const char* nick, const char* channel,
917 const char* text, int is_notice)
920 struct Channel* chptr;
921 struct Membership* membership;
925 assert(0 != channel);
926 assert(MyUser(source));
928 if (!(dest = FindUser(nick))) {
929 return send_reply(source, ERR_NOSUCHNICK, nick);
931 if (!(chptr = FindChannel(channel))) {
932 return send_reply(source, ERR_NOSUCHCHANNEL, channel);
935 * compare both users channel lists, instead of the channels user list
936 * since the link is the same, this should be a little faster for channels
937 * with a lot of users
939 for (membership = cli_user(source)->channel; membership; membership = membership->next_channel) {
940 if (chptr == membership->channel)
943 if (0 == membership) {
944 return send_reply(source, ERR_NOTONCHANNEL, chptr->chname);
946 if (!IsVoicedOrOpped(membership)) {
947 return send_reply(source, ERR_VOICENEEDED, chptr->chname);
950 * lookup channel in destination
952 assert(0 != cli_user(dest));
953 for (membership = cli_user(dest)->channel; membership; membership = membership->next_channel) {
954 if (chptr == membership->channel)
957 if (0 == membership || IsZombie(membership)) {
958 return send_reply(source, ERR_USERNOTINCHANNEL, cli_name(dest), chptr->chname);
960 if (is_silenced(source, dest))
963 if (cli_user(dest)->away)
964 send_reply(source, RPL_AWAY, cli_name(dest), cli_user(dest)->away);
966 sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text);
968 sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text);
974 * added Sat Jul 25 07:30:42 EST 1992
976 void send_umode_out(struct Client *cptr, struct Client *sptr, int old,
980 struct Client *acptr;
982 send_umode(NULL, sptr, old, SEND_UMODES & ~(prop ? 0 : FLAGS_OPER));
984 for (i = HighestFd; i >= 0; i--) {
985 if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
986 (acptr != cptr) && (acptr != sptr) && *umodeBuf)
987 sendcmdto_one(sptr, CMD_MODE, acptr, "%s :%s", cli_name(sptr), umodeBuf);
989 if (cptr && MyUser(cptr))
990 send_umode(cptr, sptr, old, ALL_UMODES);
995 * send_user_info - send user info userip/userhost
996 * NOTE: formatter must put info into buffer and return a pointer to the end of
997 * the data it put in the buffer.
999 void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
1004 int users_found = 0;
1005 struct Client* acptr;
1012 mb = msgq_make(sptr, rpl_str(rpl), cli_name(&me), cli_name(sptr));
1014 for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
1015 if ((acptr = FindUser(name))) {
1017 msgq_append(0, mb, " ");
1020 if (5 == ++arg_count)
1023 send_buffer(sptr, mb, 0);
1029 * set_user_mode() added 15/10/91 By Darren Reed.
1032 * parv[1] - username to change mode for
1033 * parv[2] - modes to change
1035 int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
1039 struct Client *acptr;
1043 unsigned int tmpmask = 0;
1044 int snomask_given = 0;
1051 return need_more_params(sptr, "MODE");
1053 if (!(acptr = FindUser(parv[1])))
1055 if (MyConnect(sptr))
1056 send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]);
1060 if (IsServer(sptr) || sptr != acptr)
1063 sendcmdto_flag_butone(&me, CMD_WALLOPS, 0, FLAGS_WALLOP,
1064 ":MODE for User %s from %s!%s", parv[1],
1065 cli_name(cptr), cli_name(sptr));
1067 send_reply(sptr, ERR_USERSDONTMATCH);
1075 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1076 if ( (userModeList[i].flag & cli_flags(sptr)))
1077 *m++ = userModeList[i].c;
1080 send_reply(sptr, RPL_UMODEIS, buf);
1081 if ((cli_flags(sptr) & FLAGS_SERVNOTICE) && MyConnect(sptr)
1082 && cli_snomask(sptr) !=
1083 (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT))
1084 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1089 * find flags already set for user
1090 * why not just copy them?
1092 setflags = cli_flags(sptr);
1094 if (MyConnect(sptr))
1095 tmpmask = cli_snomask(sptr);
1098 * parse mode change string(s)
1100 for (p = &parv[2]; *p; p++) { /* p is changed in loop too */
1101 for (m = *p; *m; m++) {
1110 if (*(p + 1) && is_snomask(*(p + 1))) {
1112 tmpmask = umode_make_snomask(tmpmask, *++p, what);
1113 tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1116 tmpmask = (what == MODE_ADD) ?
1117 (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1119 SetServNotice(sptr);
1121 ClearServNotice(sptr);
1124 if (what == MODE_ADD)
1130 if (what == MODE_ADD)
1133 cli_flags(sptr) &= ~(FLAGS_OPER | FLAGS_LOCOP);
1134 if (MyConnect(sptr)) {
1135 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1136 cli_handler(sptr) = CLIENT_HANDLER;
1141 if (what == MODE_ADD)
1144 cli_flags(sptr) &= ~(FLAGS_OPER | FLAGS_LOCOP);
1145 if (MyConnect(sptr)) {
1146 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1147 cli_handler(sptr) = CLIENT_HANDLER;
1152 if (what == MODE_ADD)
1155 ClearInvisible(sptr);
1158 if (what == MODE_ADD)
1164 if (what == MODE_ADD)
1165 SetChannelService(sptr);
1167 ClearChannelService(sptr);
1170 if (what == MODE_ADD)
1181 * Evaluate rules for new user mode
1182 * Stop users making themselves operators too easily:
1184 if (!IsServer(cptr)) {
1185 if (!(setflags & FLAGS_OPER) && IsOper(sptr))
1187 if (!(setflags & FLAGS_LOCOP) && IsLocOp(sptr))
1190 * new umode; servers can set it, local users cannot;
1191 * prevents users from /kick'ing or /mode -o'ing
1193 if (!(setflags & FLAGS_CHSERV))
1194 ClearChannelService(sptr);
1196 * only send wallops to opers
1198 if (feature_bool(FEAT_WALLOPS_OPER_ONLY) && !IsAnOper(sptr) &&
1199 !(setflags & FLAGS_WALLOP))
1201 #ifdef SERVNOTICE_OPER_ONLY
1202 if (MyConnect(sptr) && !IsAnOper(sptr) && !(setflags & FLAGS_SERVNOTICE)) {
1203 ClearServNotice(sptr);
1204 set_snomask(sptr, 0, SNO_SET);
1207 #ifdef DEBUG_OPER_ONLY
1208 if (!IsAnOper(sptr) && !(setflags & FLAGS_DEBUG))
1212 if (MyConnect(sptr)) {
1213 if ((setflags & (FLAGS_OPER | FLAGS_LOCOP)) && !IsAnOper(sptr))
1214 det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPS);
1216 if (SendServNotice(sptr)) {
1217 if (tmpmask != cli_snomask(sptr))
1218 set_snomask(sptr, tmpmask, SNO_SET);
1219 if (cli_snomask(sptr) && snomask_given)
1220 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1224 * Compare new flags with old flags and send string which
1225 * will cause servers to update correctly.
1227 if (!(setflags & FLAGS_OPER) && IsOper(sptr)) { /* user now oper */
1229 client_set_privs(sptr); /* may set propagate privilege */
1231 if (HasPriv(sptr, PRIV_PROPAGATE)) /* remember propagate privilege setting */
1233 if ((setflags & FLAGS_OPER) && !IsOper(sptr)) { /* user no longer oper */
1235 client_set_privs(sptr); /* will clear propagate privilege */
1237 if ((setflags & FLAGS_INVISIBLE) && !IsInvisible(sptr))
1238 --UserStats.inv_clients;
1239 if (!(setflags & FLAGS_INVISIBLE) && IsInvisible(sptr))
1240 ++UserStats.inv_clients;
1241 send_umode_out(cptr, sptr, setflags, prop);
1247 * Build umode string for BURST command
1250 char *umode_str(struct Client *cptr)
1252 char* m = umodeBuf; /* Maximum string size: "owidg\0" */
1256 c_flags = cli_flags(cptr) & SEND_UMODES; /* cleaning up the original code */
1257 if (HasPriv(cptr, PRIV_PROPAGATE))
1258 c_flags |= FLAGS_OPER;
1260 c_flags &= ~FLAGS_OPER;
1262 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1263 if ( (c_flags & userModeList[i].flag))
1264 *m++ = userModeList[i].c;
1268 return umodeBuf; /* Note: static buffer, gets
1269 overwritten by send_umode() */
1273 * Send the MODE string for user (user) to connection cptr
1276 void send_umode(struct Client *cptr, struct Client *sptr, int old, int sendmask)
1281 int what = MODE_NULL;
1284 * Build a string in umodeBuf to represent the change in the user's
1285 * mode between the new (sptr->flag) and 'old'.
1289 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1290 flag = userModeList[i].flag;
1291 if (MyUser(sptr) && !(flag & sendmask))
1293 if ( (flag & old) && !(cli_flags(sptr) & flag))
1295 if (what == MODE_DEL)
1296 *m++ = userModeList[i].c;
1301 *m++ = userModeList[i].c;
1304 else if (!(flag & old) && (cli_flags(sptr) & 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.
1326 int is_snomask(char *word)
1330 for (; *word; word++)
1333 else if (IsAlpha(*word))
1340 * If it begins with a +, count this as an additive mask instead of just
1341 * a replacement. If what == MODE_DEL, "+" has no special effect.
1343 unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
1345 unsigned int sno_what;
1346 unsigned int newmask;
1350 if (what == MODE_ADD)
1355 else if (*arg == '-')
1358 if (what == MODE_ADD)
1364 sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL;
1365 /* pity we don't have strtoul everywhere */
1366 newmask = (unsigned int)atoi(arg);
1367 if (sno_what == SNO_DEL)
1368 newmask = oldmask & ~newmask;
1369 else if (sno_what == SNO_ADD)
1374 static void delfrom_list(struct Client *cptr, struct SLink **list)
1377 struct SLink* prv = NULL;
1379 for (tmp = *list; tmp; tmp = tmp->next) {
1380 if (tmp->value.cptr == cptr) {
1382 prv->next = tmp->next;
1393 * This function sets a Client's server notices mask, according to
1394 * the parameter 'what'. This could be even faster, but the code
1395 * gets mighty hard to read :)
1397 void set_snomask(struct Client *cptr, unsigned int newmask, int what)
1399 unsigned int oldmask, diffmask; /* unsigned please */
1403 oldmask = cli_snomask(cptr);
1405 if (what == SNO_ADD)
1407 else if (what == SNO_DEL)
1408 newmask = oldmask & ~newmask;
1409 else if (what != SNO_SET) /* absolute set, no math needed */
1410 sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what);
1412 newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
1414 diffmask = oldmask ^ newmask;
1416 for (i = 0; diffmask >> i; i++) {
1417 if (((diffmask >> i) & 1))
1419 if (((newmask >> i) & 1))
1422 tmp->next = opsarray[i];
1423 tmp->value.cptr = cptr;
1427 /* not real portable :( */
1428 delfrom_list(cptr, &opsarray[i]);
1431 cli_snomask(cptr) = newmask;
1435 * is_silenced : Does the actual check wether sptr is allowed
1436 * to send a message to acptr.
1437 * Both must be registered persons.
1438 * If sptr is silenced by acptr, his message should not be propagated,
1439 * but more over, if this is detected on a server not local to sptr
1440 * the SILENCE mask is sent upstream.
1442 int is_silenced(struct Client *sptr, struct Client *acptr)
1446 static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
1447 static char senderip[16 + NICKLEN + USERLEN + 5];
1449 if (!cli_user(acptr) || !(lp = cli_user(acptr)->silence) || !(user = cli_user(sptr)))
1451 sprintf_irc(sender, "%s!%s@%s", cli_name(sptr), user->username, user->host);
1452 sprintf_irc(senderip, "%s!%s@%s", cli_name(sptr), user->username,
1453 ircd_ntoa((const char*) &(cli_ip(sptr))));
1454 for (; lp; lp = lp->next)
1456 if ((!(lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, sender)) ||
1457 ((lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, senderip)))
1459 if (!MyConnect(sptr))
1461 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr,
1473 * Removes all silence masks from the list of sptr that fall within `mask'
1474 * Returns -1 if none where found, 0 otherwise.
1476 int del_silence(struct Client *sptr, char *mask)
1482 for (lp = &(cli_user(sptr))->silence; *lp;) {
1483 if (!mmatch(mask, (*lp)->value.cp))
1487 MyFree(tmp->value.cp);
1497 int add_silence(struct Client* sptr, const char* mask)
1499 struct SLink *lp, **lpp;
1500 int cnt = 0, len = strlen(mask);
1503 for (lpp = &(cli_user(sptr))->silence, lp = *lpp; lp;)
1505 if (0 == ircd_strcmp(mask, lp->value.cp))
1507 if (!mmatch(mask, lp->value.cp))
1509 struct SLink *tmp = lp;
1510 *lpp = lp = lp->next;
1511 MyFree(tmp->value.cp);
1517 len += strlen(lp->value.cp);
1518 if ((len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXSILES))) ||
1519 (++cnt >= feature_int(FEAT_MAXSILES)))
1521 send_reply(sptr, ERR_SILELISTFULL, mask);
1524 else if (!mmatch(lp->value.cp, mask))
1531 memset(lp, 0, sizeof(struct SLink));
1532 lp->next = cli_user(sptr)->silence;
1533 lp->value.cp = (char*) MyMalloc(strlen(mask) + 1);
1534 assert(0 != lp->value.cp);
1535 strcpy(lp->value.cp, mask);
1536 if ((ip_start = strrchr(mask, '@')) && check_if_ipmask(ip_start + 1))
1537 lp->flags = CHFL_SILENCE_IPMASK;
1538 cli_user(sptr)->silence = lp;