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.
33 #include "ircd_alloc.h"
34 #include "ircd_chattr.h"
36 #include "ircd_reply.h"
37 #include "ircd_string.h"
46 #include "querycmds.h"
52 #include "s_serv.h" /* max_client_count */
54 #include "sprintf_irc.h"
57 #include "supported.h"
63 #include "handlers.h" /* m_motd and m_lusers */
73 static int userCount = 0;
76 * 'make_user' add's an User information block to a client
77 * if it was not previously allocated.
79 struct User *make_user(struct Client *cptr)
84 cptr->user = (struct User*) MyMalloc(sizeof(struct User));
85 assert(0 != cptr->user);
87 /* All variables are 0 by default */
88 memset(cptr->user, 0, sizeof(struct User));
92 cptr->user->refcnt = 1;
100 * Decrease user reference count by one and release block, if count reaches 0.
102 void free_user(struct User* user)
105 assert(0 < user->refcnt);
107 if (--user->refcnt == 0) {
113 assert(0 == user->joined);
114 assert(0 == user->invited);
115 assert(0 == user->channel);
124 void user_count_memory(size_t* count_out, size_t* bytes_out)
126 assert(0 != count_out);
127 assert(0 != bytes_out);
128 *count_out = userCount;
129 *bytes_out = userCount * sizeof(struct User);
133 * user_set_away - set user away state
134 * returns 1 if client is away or changed away message, 0 if
135 * client is removing away status.
136 * NOTE: this function may modify user and message, so they
139 int user_set_away(struct User* user, char* message)
146 if (EmptyString(message)) {
148 * Marking as not away
159 unsigned int len = strlen(message);
161 if (len > TOPICLEN) {
162 message[TOPICLEN] = '\0';
166 away = (char*) MyRealloc(away, len + 1);
168 away = (char*) MyMalloc(len + 1);
172 strcpy(away, message);
174 return (user->away != 0);
180 * Local function to find the next matching client. The search
181 * can be continued from the specified client entry. Normal
184 * for (x = client; x = next_client(x,mask); x = x->next)
185 * HandleMatchingClient;
188 struct Client *next_client(struct Client *next, const char* ch)
190 struct Client *tmp = next;
195 next = FindClient(ch);
196 next = next ? next : tmp;
197 if (tmp->prev == next)
201 for (; next; next = next->next)
202 if (!match(ch, next->name))
210 * Do the basic thing in delivering the message (command)
211 * across the relays to the specific server (server) for
214 * Note: The command is a format string and *MUST* be
215 * of prefixed style (e.g. ":%s COMMAND %s ...").
216 * Command can have only max 8 parameters.
218 * server parv[server] is the parameter identifying the
222 * parv[server] is replaced with the pointer to the
223 * real servername from the matched client (I'm lazy
226 * returns: (see #defines)
228 int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok,
229 struct Client *one, int MustBeOper, const char *pattern,
230 int server, int parc, char *parv[])
232 struct Client *acptr;
235 /* Assume it's me, if no server or an unregistered client */
236 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
237 return (HUNTED_ISME);
239 /* Make sure it's a server */
241 /* Make sure it's a server */
242 if (!strchr(to, '*')) {
243 if (0 == (acptr = FindClient(to)))
244 return HUNTED_NOSUCH;
247 acptr = acptr->user->server;
248 } else if (!(acptr = find_match_server(to))) {
249 send_reply(from, ERR_NOSUCHSERVER, to);
250 return (HUNTED_NOSUCH);
252 } else if (!(acptr = FindNServer(to)))
253 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
256 return (HUNTED_ISME);
258 if (MustBeOper && !IsPrivileged(from)) {
259 send_reply(from, ERR_NOPRIVILEGES);
260 return HUNTED_NOSUCH;
263 assert(!IsServer(from));
265 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
267 sendcmdto_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
268 parv[4], parv[5], parv[6], parv[7], parv[8]);
270 return (HUNTED_PASS);
274 * 'do_nick_name' ensures that the given parameter (nick) is really a proper
275 * string for a nickname (note, the 'nick' may be modified in the process...)
277 * RETURNS the length of the final NICKNAME (0, if nickname is invalid)
279 * Nickname characters are in range 'A'..'}', '_', '-', '0'..'9'
280 * anything outside the above set will terminate nickname.
281 * In addition, the first character cannot be '-' or a Digit.
284 * The '~'-character should be allowed, but a change should be global,
285 * some confusion would result if only few servers allowed it...
287 int do_nick_name(char* nick)
290 char* end = ch + NICKLEN;
293 if (*ch == '-' || IsDigit(*ch)) /* first character in [0..9-] */
296 for ( ; (ch < end) && *ch; ++ch)
297 if (!IsNickChar(*ch))
308 * Copy `source' to `dest', replacing all occurances of '~' and characters that
309 * are not `isIrcUi' by an underscore.
310 * Copies at most USERLEN - 1 characters or up till the first control character.
311 * If `tilde' is true, then a tilde is prepended to `dest'.
312 * Note that `dest' and `source' can point to the same area or to different
313 * non-overlapping areas.
315 static char *clean_user_id(char *dest, char *source, int tilde)
322 ch = *s++; /* Store first character to copy: */
325 *d++ = '~'; /* If `dest' == `source', then this overwrites `ch' */
328 while (ch && !IsCntrl(ch) && rlen--)
330 char nch = *s++; /* Store next character to copy */
331 *d++ = IsUserChar(ch) ? ch : '_'; /* This possibly overwrites it */
344 * This function is called when both NICK and USER messages
345 * have been accepted for the client, in whatever order. Only
346 * after this the USER message is propagated.
348 * NICK's must be propagated at once when received, although
349 * it would be better to delay them too until full info is
350 * available. Doing it is not so simple though, would have
351 * to implement the following:
353 * 1) user telnets in and gives only "NICK foobar" and waits
354 * 2) another user far away logs in normally with the nick
355 * "foobar" (quite legal, as this server didn't propagate it).
356 * 3) now this server gets nick "foobar" from outside, but
357 * has already the same defined locally. Current server
358 * would just issue "KILL foobar" to clean out dups. But,
359 * this is not fair. It should actually request another
360 * nick from local user or kill him/her...
362 int register_user(struct Client *cptr, struct Client *sptr,
363 const char *nick, char *username, struct Gline *agline)
365 struct ConfItem* aconf;
369 char c = 0; /* not alphanum */
370 char d = 'a'; /* not a digit */
378 short digitgroups = 0;
379 struct User* user = sptr->user;
381 char featurebuf[512];
383 user->last = CurrentTime;
384 parv[0] = sptr->name;
385 parv[1] = parv[2] = NULL;
389 static time_t last_too_many1;
390 static time_t last_too_many2;
392 assert(cptr == sptr);
393 switch (conf_check_client(sptr))
397 case ACR_NO_AUTHORIZATION:
398 sendto_opmask_butone(0, SNO_UNAUTH, "Unauthorized connection from %s.",
399 get_client_name(sptr, HIDE_IP));
400 ++ServerStats->is_ref;
401 return exit_client(cptr, sptr, &me,
402 "No Authorization - use another server");
403 case ACR_TOO_MANY_IN_CLASS:
404 if (CurrentTime - last_too_many1 >= (time_t) 60)
406 last_too_many1 = CurrentTime;
407 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections in "
409 get_client_name(sptr, HIDE_IP));
411 ++ServerStats->is_ref;
412 IPcheck_connect_fail(sptr->ip);
413 return exit_client(cptr, sptr, &me,
414 "Sorry, your connection class is full - try "
415 "again later or try another server");
416 case ACR_TOO_MANY_FROM_IP:
417 if (CurrentTime - last_too_many2 >= (time_t) 60)
419 last_too_many2 = CurrentTime;
420 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections from "
422 get_client_name(sptr, HIDE_IP));
424 ++ServerStats->is_ref;
425 return exit_client(cptr, sptr, &me,
426 "Too many connections from your host");
427 case ACR_ALREADY_AUTHORIZED:
428 /* Can this ever happen? */
430 ++ServerStats->is_ref;
431 IPcheck_connect_fail(sptr->ip);
432 return exit_client(cptr, sptr, &me, "Unknown error -- Try again");
434 ircd_strncpy(user->host, sptr->sockhost, HOSTLEN);
435 aconf = sptr->confs->value.aconf;
437 clean_user_id(user->username,
438 (sptr->flags & FLAGS_GOTID) ? sptr->username : username,
439 (sptr->flags & FLAGS_DOID) && !(sptr->flags & FLAGS_GOTID));
441 if ((user->username[0] == '\0')
442 || ((user->username[0] == '~') && (user->username[1] == '\000')))
443 return exit_client(cptr, sptr, &me, "USER: Bogus userid.");
445 if (!EmptyString(aconf->passwd)
446 && !(IsDigit(*aconf->passwd) && !aconf->passwd[1])
447 && strcmp(sptr->passwd, aconf->passwd))
449 ServerStats->is_ref++;
450 IPcheck_connect_fail(sptr->ip);
451 send_reply(sptr, ERR_PASSWDMISMATCH);
452 return exit_client(cptr, sptr, &me, "Bad Password");
454 memset(sptr->passwd, 0, sizeof(sptr->passwd));
456 * following block for the benefit of time-dependent K:-lines
458 if (find_kill(sptr)) {
459 ServerStats->is_ref++;
460 IPcheck_connect_fail(sptr->ip);
461 return exit_client(cptr, sptr, &me, "K-lined");
464 * Check for mixed case usernames, meaning probably hacked. Jon2 3-94
465 * Summary of rules now implemented in this patch: Ensor 11-94
466 * In a mixed-case name, if first char is upper, one more upper may
467 * appear anywhere. (A mixed-case name *must* have an upper first
468 * char, and may have one other upper.)
469 * A third upper may appear if all 3 appear at the beginning of the
470 * name, separated only by "others" (-/_/.).
471 * A single group of digits is allowed anywhere.
472 * Two groups of digits are allowed if at least one of the groups is
473 * at the beginning or the end.
474 * Only one '-', '_', or '.' is allowed (or two, if not consecutive).
475 * But not as the first or last char.
476 * No other special characters are allowed.
477 * Name must contain at least one letter.
479 tmpstr2 = tmpstr = (username[0] == '~' ? &username[1] : username);
480 while (*tmpstr && !badid)
492 if ((leadcaps || pos == 1) && !lower && !digits)
498 if (pos == 1 || !IsDigit(d))
505 else if (c == '-' || c == '_' || c == '.')
510 else if (d == '-' || d == '_' || d == '.' || other > 2)
519 if (lower && upper && (!leadcaps || leadcaps > 3 ||
520 (upper > 2 && upper > leadcaps)))
522 else if (digitgroups == 2 && !(IsDigit(tmpstr2[0]) || IsDigit(c)))
524 else if ((!lower && !upper) || !IsAlnum(c))
527 if (badid && (!(sptr->flags & FLAGS_GOTID) ||
528 strcmp(sptr->username, username) != 0))
530 ServerStats->is_ref++;
532 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
533 ":Your username is invalid.");
534 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
535 ":Connect with your real username, in lowercase.");
536 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
537 ":If your mail address were foo@bar.com, your username "
539 return exit_client(cptr, sptr, &me, "USER: Bad username");
541 Count_unknownbecomesclient(sptr, UserStats);
544 ircd_strncpy(user->username, username, USERLEN);
545 Count_newremoteclient(UserStats, user->server);
549 /* a gline wasn't passed in, so find a matching global one that isn't
550 * a Uworld-set one, and propagate it if there is such an animal.
553 (agline = gline_lookup(sptr, GLINE_GLOBAL | GLINE_LASTMOD)) &&
554 !IsBurstOrBurstAck(cptr))
555 gline_resend(cptr, agline);
557 if (IsInvisible(sptr))
558 ++UserStats.inv_clients;
562 if (MyConnect(sptr)) {
563 sptr->handler = CLIENT_HANDLER;
564 release_dns_reply(sptr);
566 send_reply(sptr, RPL_WELCOME, nick);
568 * This is a duplicate of the NOTICE but see below...
570 send_reply(sptr, RPL_YOURHOST, me.name, version);
571 send_reply(sptr, RPL_CREATED, creation);
572 send_reply(sptr, RPL_MYINFO, me.name, version);
573 sprintf_irc(featurebuf,FEATURES,FEATURESVALUES);
574 send_reply(sptr, RPL_ISUPPORT, featurebuf);
575 m_lusers(sptr, sptr, 1, parv);
578 nextping = CurrentTime;
579 if (sptr->snomask & SNO_NOISY)
580 set_snomask(sptr, sptr->snomask & SNO_NOISY, SNO_ADD);
581 IPcheck_connect_succeeded(sptr);
584 /* if (IsServer(cptr)) */
586 struct Client *acptr;
588 acptr = user->server;
589 if (acptr->from != sptr->from)
591 sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])",
592 sptr, me.name, user->server->name, acptr->from->name,
593 acptr->from->sockhost);
594 sptr->flags |= FLAGS_KILLED;
595 return exit_client(cptr, sptr, &me, "NICK server wrong direction");
598 sptr->flags |= (acptr->flags & FLAGS_TS8);
601 * Check to see if this user is being propogated
602 * as part of a net.burst, or is using protocol 9.
603 * FIXME: This can be speeded up - its stupid to check it for
604 * every NICK message in a burst again --Run.
606 for (acptr = user->server; acptr != &me; acptr = acptr->serv->up) {
607 if (IsBurst(acptr) || Protocol(acptr) < 10)
610 if (!IPcheck_remote_connect(sptr, (acptr != &me))) {
612 * We ran out of bits to count this
614 sendcmdto_one(&me, CMD_KILL, sptr, "%C :%s (Too many connections from your host -- Ghost)",
616 return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled");
619 tmpstr = umode_str(sptr);
621 sendcmdto_serv_butone(user->server, CMD_NICK, cptr,
622 "%s %d %Tu %s %s %s%s%s%%%Tu:%s@%s %s %s%s :%s",
623 nick, sptr->hopcount + 1, sptr->lastnick,
624 user->username, user->host,
625 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
626 GlineLastMod(agline), GlineUser(agline),
628 inttobase64(ip_base64, ntohl(sptr->ip.s_addr), 6),
629 NumNick(sptr), sptr->info);
631 sendcmdto_serv_butone(user->server, CMD_NICK, cptr,
632 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
633 nick, sptr->hopcount + 1, sptr->lastnick,
634 user->username, user->host,
635 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
636 inttobase64(ip_base64, ntohl(sptr->ip.s_addr), 6),
637 NumNick(sptr), sptr->info);
639 /* Send umode to client */
642 send_umode(cptr, sptr, 0, ALL_UMODES);
643 if (sptr->snomask != SNO_DEFAULT && (sptr->flags & FLAGS_SERVNOTICE))
644 send_reply(sptr, RPL_SNOMASK, sptr->snomask, sptr->snomask);
651 static const struct UserMode {
656 { FLAGS_LOCOP, 'O' },
657 { FLAGS_INVISIBLE, 'i' },
658 { FLAGS_WALLOP, 'w' },
659 { FLAGS_SERVNOTICE, 's' },
661 { FLAGS_CHSERV, 'k' },
665 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
668 static int user_modes[] = {
671 FLAGS_INVISIBLE, 'i',
673 FLAGS_SERVNOTICE, 's',
682 * XXX - find a way to get rid of this
684 static char umodeBuf[BUFSIZE];
686 int set_nick_name(struct Client* cptr, struct Client* sptr,
687 const char* nick, int parc, char* parv[])
689 if (IsServer(sptr)) {
693 struct Gline *agline = 0;
696 * A server introducing a new client, change source
698 struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
699 assert(0 != new_client);
701 new_client->hopcount = atoi(parv[2]);
702 new_client->lastnick = atoi(parv[3]);
703 if (Protocol(cptr) > 9 && parc > 7 && *parv[6] == '+') {
704 for (p = parv[6] + 1; *p; p++) {
705 for (i = 0; i < USERMODELIST_SIZE; ++i) {
706 if (userModeList[i].c == *p) {
707 new_client->flags |= userModeList[i].flag;
716 strcpy(new_client->name, nick);
717 new_client->user = make_user(new_client);
718 new_client->user->server = sptr;
719 SetRemoteNumNick(new_client, parv[parc - 2]);
721 * IP# of remote client
723 new_client->ip.s_addr = htonl(base64toint(parv[parc - 3]));
725 add_client_to_list(new_client);
726 hAddClient(new_client);
728 sptr->serv->ghost = 0; /* :server NICK means end of net.burst */
729 ircd_strncpy(new_client->username, parv[4], USERLEN);
730 ircd_strncpy(new_client->user->host, parv[5], HOSTLEN);
731 ircd_strncpy(new_client->info, parv[parc - 1], REALLEN);
733 /* Deal with GLINE parameters... */
734 if (*parv[parc - 4] == '%' && (t = strchr(parv[parc - 4] + 1, ':'))) {
738 lastmod = atoi(parv[parc - 4] + 1);
741 (agline = gline_find(t, GLINE_EXACT | GLINE_GLOBAL | GLINE_LASTMOD))
742 && GlineLastMod(agline) > lastmod && !IsBurstOrBurstAck(cptr))
743 gline_resend(cptr, agline);
745 return register_user(cptr, new_client, new_client->name, parv[4], agline);
747 else if (sptr->name[0]) {
749 * Client changing its nick
751 * If the client belongs to me, then check to see
752 * if client is on any channels where it is currently
753 * banned. If so, do not allow the nick change to occur.
756 const char* channel_name;
757 if ((channel_name = find_no_nickchange_channel(sptr))) {
758 return send_reply(cptr, ERR_BANNICKCHANGE, channel_name);
761 * Refuse nick change if the last nick change was less
762 * then 30 seconds ago. This is intended to get rid of
763 * clone bots doing NICK FLOOD. -SeKs
764 * If someone didn't change their nick for more then 60 seconds
765 * however, allow to do two nick changes immedately after another
766 * before limiting the nick flood. -Run
768 if (CurrentTime < cptr->nextnick) {
770 send_reply(cptr, ERR_NICKTOOFAST, parv[1],
771 cptr->nextnick - CurrentTime);
772 /* Send error message */
773 sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cptr->name);
774 /* bounce NICK to user */
775 return 0; /* ignore nick change! */
778 /* Limit total to 1 change per NICK_DELAY seconds: */
779 cptr->nextnick += NICK_DELAY;
780 /* However allow _maximal_ 1 extra consecutive nick change: */
781 if (cptr->nextnick < CurrentTime)
782 cptr->nextnick = CurrentTime;
786 * Also set 'lastnick' to current time, if changed.
788 if (0 != ircd_strcmp(parv[0], nick))
789 sptr->lastnick = (sptr == cptr) ? TStime() : atoi(parv[2]);
792 * Client just changing his/her nick. If he/she is
793 * on a channel, send note of change to all clients
794 * on that channel. Propagate notice to other servers.
797 sendcmdto_common_channels(sptr, CMD_NICK, ":%s", nick);
798 add_history(sptr, 1);
799 sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick,
803 sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick);
807 strcpy(sptr->name, nick);
811 /* Local client setting NICK the first time */
813 strcpy(sptr->name, nick);
815 sptr->user = make_user(sptr);
816 sptr->user->server = &me;
818 SetLocalNumNick(sptr);
822 * If the client hasn't gotten a cookie-ping yet,
823 * choose a cookie and send it. -record!jegelhof@cloud9.net
827 sptr->cookie = (ircrandom() & 0x7fffffff);
828 } while (!sptr->cookie);
829 sendrawto_one(cptr, MSG_PING " :%u", sptr->cookie);
831 else if (*sptr->user->host && sptr->cookie == COOKIE_VERIFIED) {
833 * USER and PONG already received, now we have NICK.
834 * register_user may reject the client and call exit_client
835 * for it - must test this and exit m_nick too !
837 sptr->lastnick = TStime(); /* Always local client */
838 if (register_user(cptr, sptr, nick, sptr->user->username, 0) == CPTR_KILLED)
845 static unsigned char hash_target(unsigned int target)
847 return (unsigned char) (target >> 16) ^ (target >> 8);
853 * sptr must be a local client!
855 * Cannonifies target for client `sptr'.
857 void add_target(struct Client *sptr, void *target)
859 /* Ok, this shouldn't work esp on alpha
861 unsigned char hash = hash_target((unsigned long) target);
862 unsigned char* targets;
867 targets = sptr->targets;
871 for (i = 0; i < MAXTARGETS; ++i) {
872 if (targets[i] == hash)
878 memmove(&targets[RESERVEDTARGETS + 1],
879 &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
880 targets[RESERVEDTARGETS] = hash;
886 * sptr must be a local client !
888 * Returns 'true' (1) when too many targets are addressed.
889 * Returns 'false' (0) when it's ok to send to this target.
891 int check_target_limit(struct Client *sptr, void *target, const char *name,
894 unsigned char hash = hash_target((unsigned long) target);
896 unsigned char* targets;
900 targets = sptr->targets;
903 * Same target as last time?
905 if (targets[0] == hash)
907 for (i = 1; i < MAXTARGETS; ++i) {
908 if (targets[i] == hash) {
909 memmove(&targets[1], &targets[0], i);
918 if (CurrentTime < sptr->nexttarget) {
919 if (sptr->nexttarget - CurrentTime < TARGET_DELAY + 8) {
923 sptr->nexttarget += 2;
924 send_reply(sptr, ERR_TARGETTOOFAST, name,
925 sptr->nexttarget - CurrentTime);
931 /* XXX Let's get rid of GODMODE */
932 sendto_one(sptr, ":%s NOTICE %s :New target: %s; ft " TIME_T_FMT, /* XXX Possibly DEAD */
933 me.name, sptr->name, name, (CurrentTime - sptr->nexttarget) / TARGET_DELAY);
935 sptr->nexttarget += TARGET_DELAY;
936 if (sptr->nexttarget < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)))
937 sptr->nexttarget = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1));
940 memmove(&targets[1], &targets[0], MAXTARGETS - 1);
946 * whisper - called from m_cnotice and m_cprivmsg.
948 * parv[0] = sender prefix
951 * parv[3] = Private message text
953 * Added 971023 by Run.
954 * Reason: Allows channel operators to sent an arbitrary number of private
955 * messages to users on their channel, avoiding the max.targets limit.
956 * Building this into m_private would use too much cpu because we'd have
957 * to a cross channel lookup for every private message!
958 * Note that we can't allow non-chan ops to use this command, it would be
959 * abused by mass advertisers.
962 int whisper(struct Client* source, const char* nick, const char* channel,
963 const char* text, int is_notice)
966 struct Channel* chptr;
967 struct Membership* membership;
971 assert(0 != channel);
972 assert(MyUser(source));
974 if (!(dest = FindUser(nick))) {
975 return send_reply(source, ERR_NOSUCHNICK, nick);
977 if (!(chptr = FindChannel(channel))) {
978 return send_reply(source, ERR_NOSUCHCHANNEL, channel);
981 * compare both users channel lists, instead of the channels user list
982 * since the link is the same, this should be a little faster for channels
983 * with a lot of users
985 for (membership = source->user->channel; membership; membership = membership->next_channel) {
986 if (chptr == membership->channel)
989 if (0 == membership) {
990 return send_reply(source, ERR_NOTONCHANNEL, chptr->chname);
992 if (!IsVoicedOrOpped(membership)) {
993 return send_reply(source, ERR_VOICENEEDED, chptr->chname);
996 * lookup channel in destination
998 assert(0 != dest->user);
999 for (membership = dest->user->channel; membership; membership = membership->next_channel) {
1000 if (chptr == membership->channel)
1003 if (0 == membership || IsZombie(membership)) {
1004 return send_reply(source, ERR_USERNOTINCHANNEL, dest->name, chptr->chname);
1006 if (is_silenced(source, dest))
1009 if (dest->user->away)
1010 send_reply(source, RPL_AWAY, dest->name, dest->user->away);
1012 sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text);
1014 sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text);
1020 * added Sat Jul 25 07:30:42 EST 1992
1022 void send_umode_out(struct Client *cptr, struct Client *sptr, int old)
1025 struct Client *acptr;
1027 send_umode(NULL, sptr, old, SEND_UMODES);
1029 for (i = HighestFd; i >= 0; i--) {
1030 if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
1031 (acptr != cptr) && (acptr != sptr) && *umodeBuf)
1032 sendcmdto_one(sptr, CMD_MODE, acptr, "%s :%s", sptr->name, umodeBuf);
1034 if (cptr && MyUser(cptr))
1035 send_umode(cptr, sptr, old, ALL_UMODES);
1040 * send_user_info - send user info userip/userhost
1041 * NOTE: formatter must put info into buffer and return a pointer to the end of
1042 * the data it put in the buffer.
1044 void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
1049 int users_found = 0;
1050 struct Client* acptr;
1057 mb = msgq_make(sptr, rpl_str(rpl), me.name, sptr->name);
1059 for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
1060 if ((acptr = FindUser(name))) {
1062 msgq_append(0, mb, " ");
1065 if (5 == ++arg_count)
1068 send_buffer(sptr, mb, 0);
1074 * set_user_mode() added 15/10/91 By Darren Reed.
1077 * parv[1] - username to change mode for
1078 * parv[2] - modes to change
1080 int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
1084 struct Client *acptr;
1088 unsigned int tmpmask = 0;
1089 int snomask_given = 0;
1095 return need_more_params(sptr, "MODE");
1097 if (!(acptr = FindUser(parv[1])))
1099 if (MyConnect(sptr))
1100 send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]);
1104 if (IsServer(sptr) || sptr != acptr)
1107 sendcmdto_flag_butone(&me, CMD_WALLOPS, 0, FLAGS_WALLOP,
1108 ":MODE for User %s from %s!%s", parv[1],
1109 cptr->name, sptr->name);
1111 send_reply(sptr, ERR_USERSDONTMATCH);
1119 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1120 if ( (userModeList[i].flag & sptr->flags))
1121 *m++ = userModeList[i].c;
1124 send_reply(sptr, RPL_UMODEIS, buf);
1125 if ((sptr->flags & FLAGS_SERVNOTICE) && MyConnect(sptr)
1127 (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT))
1128 send_reply(sptr, RPL_SNOMASK, sptr->snomask, sptr->snomask);
1133 * find flags already set for user
1134 * why not just copy them?
1136 setflags = sptr->flags;
1139 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1140 if (sptr->flags & userModeList[i].flag)
1141 setflags |= userModeList[i].flag;
1144 if (MyConnect(sptr))
1145 tmpmask = sptr->snomask;
1148 * parse mode change string(s)
1150 for (p = &parv[2]; *p; p++) { /* p is changed in loop too */
1151 for (m = *p; *m; m++) {
1160 if (*(p + 1) && is_snomask(*(p + 1))) {
1162 tmpmask = umode_make_snomask(tmpmask, *++p, what);
1163 tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1166 tmpmask = (what == MODE_ADD) ?
1167 (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1169 sptr->flags |= FLAGS_SERVNOTICE;
1171 sptr->flags &= ~FLAGS_SERVNOTICE;
1174 if (what == MODE_ADD)
1180 if (what == MODE_ADD)
1183 sptr->flags &= ~(FLAGS_OPER | FLAGS_LOCOP);
1184 if (MyConnect(sptr)) {
1185 tmpmask = sptr->snomask & ~SNO_OPER;
1186 sptr->handler = CLIENT_HANDLER;
1191 if (what == MODE_ADD)
1194 sptr->flags &= ~(FLAGS_OPER | FLAGS_LOCOP);
1195 if (MyConnect(sptr)) {
1196 tmpmask = sptr->snomask & ~SNO_OPER;
1197 sptr->handler = CLIENT_HANDLER;
1202 if (what == MODE_ADD)
1205 ClearInvisible(sptr);
1208 if (what == MODE_ADD)
1214 if (what == MODE_ADD)
1215 SetChannelService(sptr);
1217 ClearChannelService(sptr);
1220 if (what == MODE_ADD)
1231 * Evaluate rules for new user mode
1232 * Stop users making themselves operators too easily:
1234 if (!IsServer(cptr)) {
1235 if (!(setflags & FLAGS_OPER) && IsOper(sptr))
1237 if (!(setflags & FLAGS_LOCOP) && IsLocOp(sptr))
1240 * new umode; servers can set it, local users cannot;
1241 * prevents users from /kick'ing or /mode -o'ing
1243 if (!(setflags & FLAGS_CHSERV))
1244 ClearChannelService(sptr);
1245 #ifdef WALLOPS_OPER_ONLY
1247 * only send wallops to opers
1249 if (!IsAnOper(sptr) && !(setflags & FLAGS_WALLOP))
1253 if (MyConnect(sptr)) {
1254 if ((setflags & (FLAGS_OPER | FLAGS_LOCOP)) && !IsAnOper(sptr))
1255 det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPS);
1257 if (tmpmask != sptr->snomask)
1258 set_snomask(sptr, tmpmask, SNO_SET);
1259 if (sptr->snomask && snomask_given)
1260 send_reply(sptr, RPL_SNOMASK, sptr->snomask, sptr->snomask);
1263 * Compare new flags with old flags and send string which
1264 * will cause servers to update correctly.
1266 if ((setflags & FLAGS_OPER) && !IsOper(sptr))
1268 if (!(setflags & FLAGS_OPER) && IsOper(sptr))
1270 if ((setflags & FLAGS_INVISIBLE) && !IsInvisible(sptr))
1271 --UserStats.inv_clients;
1272 if (!(setflags & FLAGS_INVISIBLE) && IsInvisible(sptr))
1273 ++UserStats.inv_clients;
1274 send_umode_out(cptr, sptr, setflags);
1280 * Build umode string for BURST command
1283 char *umode_str(struct Client *cptr)
1285 char* m = umodeBuf; /* Maximum string size: "owidg\0" */
1289 c_flags = cptr->flags & SEND_UMODES; /* cleaning up the original code */
1291 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1292 if ( (c_flags & userModeList[i].flag))
1293 *m++ = userModeList[i].c;
1297 return umodeBuf; /* Note: static buffer, gets
1298 overwritten by send_umode() */
1302 * Send the MODE string for user (user) to connection cptr
1305 void send_umode(struct Client *cptr, struct Client *sptr, int old, int sendmask)
1310 int what = MODE_NULL;
1313 * Build a string in umodeBuf to represent the change in the user's
1314 * mode between the new (sptr->flag) and 'old'.
1318 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1319 flag = userModeList[i].flag;
1320 if (MyUser(sptr) && !(flag & sendmask))
1322 if ( (flag & old) && !(sptr->flags & flag))
1324 if (what == MODE_DEL)
1325 *m++ = userModeList[i].c;
1330 *m++ = userModeList[i].c;
1333 else if (!(flag & old) && (sptr->flags & flag))
1335 if (what == MODE_ADD)
1336 *m++ = userModeList[i].c;
1341 *m++ = userModeList[i].c;
1346 if (*umodeBuf && cptr)
1347 sendcmdto_one(sptr, CMD_MODE, cptr, "%s :%s", sptr->name, umodeBuf);
1351 * Check to see if this resembles a sno_mask. It is if 1) there is
1352 * at least one digit and 2) The first digit occurs before the first
1353 * alphabetic character.
1355 int is_snomask(char *word)
1359 for (; *word; word++)
1362 else if (IsAlpha(*word))
1369 * If it begins with a +, count this as an additive mask instead of just
1370 * a replacement. If what == MODE_DEL, "+" has no special effect.
1372 unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
1374 unsigned int sno_what;
1375 unsigned int newmask;
1379 if (what == MODE_ADD)
1384 else if (*arg == '-')
1387 if (what == MODE_ADD)
1393 sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL;
1394 /* pity we don't have strtoul everywhere */
1395 newmask = (unsigned int)atoi(arg);
1396 if (sno_what == SNO_DEL)
1397 newmask = oldmask & ~newmask;
1398 else if (sno_what == SNO_ADD)
1403 static void delfrom_list(struct Client *cptr, struct SLink **list)
1406 struct SLink* prv = NULL;
1408 for (tmp = *list; tmp; tmp = tmp->next) {
1409 if (tmp->value.cptr == cptr) {
1411 prv->next = tmp->next;
1422 * This function sets a Client's server notices mask, according to
1423 * the parameter 'what'. This could be even faster, but the code
1424 * gets mighty hard to read :)
1426 void set_snomask(struct Client *cptr, unsigned int newmask, int what)
1428 unsigned int oldmask, diffmask; /* unsigned please */
1432 oldmask = cptr->snomask;
1434 if (what == SNO_ADD)
1436 else if (what == SNO_DEL)
1437 newmask = oldmask & ~newmask;
1438 else if (what != SNO_SET) /* absolute set, no math needed */
1439 sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what);
1441 newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
1443 diffmask = oldmask ^ newmask;
1445 for (i = 0; diffmask >> i; i++) {
1446 if (((diffmask >> i) & 1))
1448 if (((newmask >> i) & 1))
1451 tmp->next = opsarray[i];
1452 tmp->value.cptr = cptr;
1456 /* not real portable :( */
1457 delfrom_list(cptr, &opsarray[i]);
1460 cptr->snomask = newmask;
1464 * is_silenced : Does the actual check wether sptr is allowed
1465 * to send a message to acptr.
1466 * Both must be registered persons.
1467 * If sptr is silenced by acptr, his message should not be propagated,
1468 * but more over, if this is detected on a server not local to sptr
1469 * the SILENCE mask is sent upstream.
1471 int is_silenced(struct Client *sptr, struct Client *acptr)
1475 static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
1476 static char senderip[16 + NICKLEN + USERLEN + 5];
1478 if (!(acptr->user) || !(lp = acptr->user->silence) || !(user = sptr->user))
1480 sprintf_irc(sender, "%s!%s@%s", sptr->name, user->username, user->host);
1481 sprintf_irc(senderip, "%s!%s@%s", sptr->name, user->username,
1482 ircd_ntoa((const char*) &sptr->ip));
1483 for (; lp; lp = lp->next)
1485 if ((!(lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, sender)) ||
1486 ((lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, senderip)))
1488 if (!MyConnect(sptr))
1490 sendcmdto_one(acptr, CMD_SILENCE, sptr->from, "%C %s", sptr,
1502 * Removes all silence masks from the list of sptr that fall within `mask'
1503 * Returns -1 if none where found, 0 otherwise.
1505 int del_silence(struct Client *sptr, char *mask)
1511 for (lp = &sptr->user->silence; *lp;) {
1512 if (!mmatch(mask, (*lp)->value.cp))
1516 MyFree(tmp->value.cp);
1526 int add_silence(struct Client* sptr, const char* mask)
1528 struct SLink *lp, **lpp;
1529 int cnt = 0, len = strlen(mask);
1532 for (lpp = &sptr->user->silence, lp = *lpp; lp;)
1534 if (0 == ircd_strcmp(mask, lp->value.cp))
1536 if (!mmatch(mask, lp->value.cp))
1538 struct SLink *tmp = lp;
1539 *lpp = lp = lp->next;
1540 MyFree(tmp->value.cp);
1546 len += strlen(lp->value.cp);
1547 if ((len > MAXSILELENGTH) || (++cnt >= MAXSILES))
1549 send_reply(sptr, ERR_SILELISTFULL, mask);
1552 else if (!mmatch(lp->value.cp, mask))
1559 memset(lp, 0, sizeof(struct SLink));
1560 lp->next = sptr->user->silence;
1561 lp->value.cp = (char*) MyMalloc(strlen(mask) + 1);
1562 assert(0 != lp->value.cp);
1563 strcpy(lp->value.cp, mask);
1564 if ((ip_start = strrchr(mask, '@')) && check_if_ipmask(ip_start + 1))
1565 lp->flags = CHFL_SILENCE_IPMASK;
1566 sptr->user->silence = lp;