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"
44 #include "querycmds.h"
50 #include "s_serv.h" /* max_client_count */
52 #include "sprintf_irc.h"
55 #include "supported.h"
61 #include "handlers.h" /* m_motd and m_lusers */
71 static int userCount = 0;
74 * 'make_user' add's an User information block to a client
75 * if it was not previously allocated.
77 struct User *make_user(struct Client *cptr)
82 cptr->user = (struct User*) MyMalloc(sizeof(struct User));
83 assert(0 != cptr->user);
85 /* All variables are 0 by default */
86 memset(cptr->user, 0, sizeof(struct User));
90 cptr->user->refcnt = 1;
98 * Decrease user reference count by one and release block, if count reaches 0.
100 void free_user(struct User* user)
103 assert(0 < user->refcnt);
105 if (--user->refcnt == 0) {
111 assert(0 == user->joined);
112 assert(0 == user->invited);
113 assert(0 == user->channel);
122 void user_count_memory(size_t* count_out, size_t* bytes_out)
124 assert(0 != count_out);
125 assert(0 != bytes_out);
126 *count_out = userCount;
127 *bytes_out = userCount * sizeof(struct User);
131 * user_set_away - set user away state
132 * returns 1 if client is away or changed away message, 0 if
133 * client is removing away status.
134 * NOTE: this function may modify user and message, so they
137 int user_set_away(struct User* user, char* message)
144 if (EmptyString(message)) {
146 * Marking as not away
157 unsigned int len = strlen(message);
159 if (len > TOPICLEN) {
160 message[TOPICLEN] = '\0';
164 away = (char*) MyRealloc(away, len + 1);
166 away = (char*) MyMalloc(len + 1);
170 strcpy(away, message);
172 return (user->away != 0);
178 * Local function to find the next matching client. The search
179 * can be continued from the specified client entry. Normal
182 * for (x = client; x = next_client(x,mask); x = x->next)
183 * HandleMatchingClient;
186 struct Client *next_client(struct Client *next, const char* ch)
188 struct Client *tmp = next;
193 next = FindClient(ch);
194 next = next ? next : tmp;
195 if (tmp->prev == next)
199 for (; next; next = next->next)
200 if (!match(ch, next->name))
208 * Do the basic thing in delivering the message (command)
209 * across the relays to the specific server (server) for
212 * Note: The command is a format string and *MUST* be
213 * of prefixed style (e.g. ":%s COMMAND %s ...").
214 * Command can have only max 8 parameters.
216 * server parv[server] is the parameter identifying the
220 * parv[server] is replaced with the pointer to the
221 * real servername from the matched client (I'm lazy
224 * returns: (see #defines)
226 int hunt_server(int MustBeOper, struct Client *cptr, struct Client *sptr, char *command,
227 int server, int parc, char *parv[])
229 struct Client *acptr;
232 /* Assume it's me, if no server or an unregistered client */
233 if (parc <= server || EmptyString(parv[server]) || IsUnknown(sptr))
234 return (HUNTED_ISME);
236 /* Make sure it's a server */
237 if (MyUser(sptr) || Protocol(cptr) < 10)
239 /* Make sure it's a server */
240 if (!strchr(parv[server], '*')) {
241 if (0 == (acptr = FindClient(parv[server])))
242 return HUNTED_NOSUCH;
244 acptr = acptr->user->server;
246 else if (!(acptr = find_match_server(parv[server])))
248 send_reply(sptr, ERR_NOSUCHSERVER, parv[server]);
249 return (HUNTED_NOSUCH);
252 else if (!(acptr = FindNServer(parv[server])))
253 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
256 return (HUNTED_ISME);
258 if (MustBeOper && !IsPrivileged(sptr))
260 send_reply(sptr, ERR_NOPRIVILEGES);
261 return HUNTED_NOSUCH;
264 strcpy(y, acptr->yxx);
267 assert(!IsServer(sptr));
268 /* XXX sendto_one used with explicit command; must be very careful */
269 sendto_one(acptr, command, NumNick(sptr), parv[1], parv[2], parv[3], parv[4],
270 parv[5], parv[6], parv[7], parv[8]);
272 return (HUNTED_PASS);
276 * 'do_nick_name' ensures that the given parameter (nick) is really a proper
277 * string for a nickname (note, the 'nick' may be modified in the process...)
279 * RETURNS the length of the final NICKNAME (0, if nickname is invalid)
281 * Nickname characters are in range 'A'..'}', '_', '-', '0'..'9'
282 * anything outside the above set will terminate nickname.
283 * In addition, the first character cannot be '-' or a Digit.
286 * The '~'-character should be allowed, but a change should be global,
287 * some confusion would result if only few servers allowed it...
289 int do_nick_name(char* nick)
292 char* end = ch + NICKLEN;
295 if (*ch == '-' || IsDigit(*ch)) /* first character in [0..9-] */
298 for ( ; (ch < end) && *ch; ++ch)
299 if (!IsNickChar(*ch))
310 * Copy `source' to `dest', replacing all occurances of '~' and characters that
311 * are not `isIrcUi' by an underscore.
312 * Copies at most USERLEN - 1 characters or up till the first control character.
313 * If `tilde' is true, then a tilde is prepended to `dest'.
314 * Note that `dest' and `source' can point to the same area or to different
315 * non-overlapping areas.
317 static char *clean_user_id(char *dest, char *source, int tilde)
324 ch = *s++; /* Store first character to copy: */
327 *d++ = '~'; /* If `dest' == `source', then this overwrites `ch' */
330 while (ch && !IsCntrl(ch) && rlen--)
332 char nch = *s++; /* Store next character to copy */
333 *d++ = IsUserChar(ch) ? ch : '_'; /* This possibly overwrites it */
346 * This function is called when both NICK and USER messages
347 * have been accepted for the client, in whatever order. Only
348 * after this the USER message is propagated.
350 * NICK's must be propagated at once when received, although
351 * it would be better to delay them too until full info is
352 * available. Doing it is not so simple though, would have
353 * to implement the following:
355 * 1) user telnets in and gives only "NICK foobar" and waits
356 * 2) another user far away logs in normally with the nick
357 * "foobar" (quite legal, as this server didn't propagate it).
358 * 3) now this server gets nick "foobar" from outside, but
359 * has already the same defined locally. Current server
360 * would just issue "KILL foobar" to clean out dups. But,
361 * this is not fair. It should actually request another
362 * nick from local user or kill him/her...
364 int register_user(struct Client *cptr, struct Client *sptr,
365 const char *nick, char *username)
367 struct ConfItem* aconf;
371 char c = 0; /* not alphanum */
372 char d = 'a'; /* not a digit */
380 short digitgroups = 0;
381 struct User* user = sptr->user;
383 char featurebuf[512];
386 user->last = CurrentTime;
387 parv[0] = sptr->name;
388 parv[1] = parv[2] = NULL;
392 static time_t last_too_many1;
393 static time_t last_too_many2;
395 assert(cptr == sptr);
396 switch (conf_check_client(sptr))
400 case ACR_NO_AUTHORIZATION:
401 sendto_opmask_butone(0, SNO_UNAUTH, "Unauthorized connection from %s.",
402 get_client_name(sptr, HIDE_IP));
403 ++ServerStats->is_ref;
404 return exit_client(cptr, sptr, &me,
405 "No Authorization - use another server");
406 case ACR_TOO_MANY_IN_CLASS:
407 if (CurrentTime - last_too_many1 >= (time_t) 60)
409 last_too_many1 = CurrentTime;
410 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections in "
412 get_client_name(sptr, HIDE_IP));
414 ++ServerStats->is_ref;
415 IPcheck_connect_fail(sptr->ip);
416 return exit_client(cptr, sptr, &me,
417 "Sorry, your connection class is full - try "
418 "again later or try another server");
419 case ACR_TOO_MANY_FROM_IP:
420 if (CurrentTime - last_too_many2 >= (time_t) 60)
422 last_too_many2 = CurrentTime;
423 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections from "
425 get_client_name(sptr, HIDE_IP));
427 ++ServerStats->is_ref;
428 return exit_client(cptr, sptr, &me,
429 "Too many connections from your host");
430 case ACR_ALREADY_AUTHORIZED:
431 /* Can this ever happen? */
433 ++ServerStats->is_ref;
434 IPcheck_connect_fail(sptr->ip);
435 return exit_client(cptr, sptr, &me, "Unknown error -- Try again");
437 ircd_strncpy(user->host, sptr->sockhost, HOSTLEN);
438 aconf = sptr->confs->value.aconf;
440 clean_user_id(user->username,
441 (sptr->flags & FLAGS_GOTID) ? sptr->username : username,
442 (sptr->flags & FLAGS_DOID) && !(sptr->flags & FLAGS_GOTID));
444 if ((user->username[0] == '\0')
445 || ((user->username[0] == '~') && (user->username[1] == '\000')))
446 return exit_client(cptr, sptr, &me, "USER: Bogus userid.");
448 if (!EmptyString(aconf->passwd)
449 && !(IsDigit(*aconf->passwd) && !aconf->passwd[1])
451 && strcmp("ONE", aconf->passwd)
453 && strcmp(sptr->passwd, aconf->passwd))
455 ServerStats->is_ref++;
456 IPcheck_connect_fail(sptr->ip);
457 send_reply(sptr, ERR_PASSWDMISMATCH);
458 return exit_client(cptr, sptr, &me, "Bad Password");
460 memset(sptr->passwd, 0, sizeof(sptr->passwd));
462 * following block for the benefit of time-dependent K:-lines
464 if (find_kill(sptr)) {
465 ServerStats->is_ref++;
466 IPcheck_connect_fail(sptr->ip);
467 return exit_client(cptr, sptr, &me, "K-lined");
470 * Check for mixed case usernames, meaning probably hacked. Jon2 3-94
471 * Summary of rules now implemented in this patch: Ensor 11-94
472 * In a mixed-case name, if first char is upper, one more upper may
473 * appear anywhere. (A mixed-case name *must* have an upper first
474 * char, and may have one other upper.)
475 * A third upper may appear if all 3 appear at the beginning of the
476 * name, separated only by "others" (-/_/.).
477 * A single group of digits is allowed anywhere.
478 * Two groups of digits are allowed if at least one of the groups is
479 * at the beginning or the end.
480 * Only one '-', '_', or '.' is allowed (or two, if not consecutive).
481 * But not as the first or last char.
482 * No other special characters are allowed.
483 * Name must contain at least one letter.
485 tmpstr2 = tmpstr = (username[0] == '~' ? &username[1] : username);
486 while (*tmpstr && !badid)
498 if ((leadcaps || pos == 1) && !lower && !digits)
504 if (pos == 1 || !IsDigit(d))
511 else if (c == '-' || c == '_' || c == '.')
516 else if (d == '-' || d == '_' || d == '.' || other > 2)
525 if (lower && upper && (!leadcaps || leadcaps > 3 ||
526 (upper > 2 && upper > leadcaps)))
528 else if (digitgroups == 2 && !(IsDigit(tmpstr2[0]) || IsDigit(c)))
530 else if ((!lower && !upper) || !IsAlnum(c))
533 if (badid && (!(sptr->flags & FLAGS_GOTID) ||
534 strcmp(sptr->username, username) != 0))
536 ServerStats->is_ref++;
538 send_reply(cptr, RPL_EXPLICIT | ERR_INVALIDUSERNAME,
539 ":Your username is invalid.");
540 send_reply(cptr, RPL_EXPLICIT | ERR_INVALIDUSERNAME,
541 ":Connect with your real username, in lowercase.");
542 send_reply(cptr, RPL_EXPLICIT | ERR_INVALIDUSERNAME,
543 ":If your mail address were foo@bar.com, your username "
545 return exit_client(cptr, sptr, &me, "USER: Bad username");
547 Count_unknownbecomesclient(sptr, UserStats);
550 ircd_strncpy(user->username, username, USERLEN);
551 Count_newremoteclient(UserStats, user->server);
552 if ((gline = gline_lookup(sptr)) && GlineIsActive(gline))
553 gline_resend(cptr, gline);
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 m_motd(sptr, NULL, 1, parv);
580 m_motd(sptr, sptr, 1, parv);
582 nextping = CurrentTime;
583 if (sptr->snomask & SNO_NOISY)
584 set_snomask(sptr, sptr->snomask & SNO_NOISY, SNO_ADD);
585 #ifdef ALLOW_SNO_CONNEXIT
586 #ifdef SNO_CONNEXIT_IP
587 sendto_opmask_butone(0, SNO_CONNEXIT, "Client connecting: %s (%s@%s) "
588 "[%s] {%d}", nick, user->username, user->host,
589 cptr->sock_ip, get_client_class(sptr));
590 #else /* SNO_CONNEXIT_IP */
591 sendto_opmask_butone(0, SNO_CONEXIT, "Client connecting: %s (%s@%s)",
592 nick, user->username, user->host);
593 #endif /* SNO_CONNEXIT_IP */
594 #endif /* ALLOW_SNO_CONNEXIT */
595 IPcheck_connect_succeeded(sptr);
598 /* if (IsServer(cptr)) */
600 struct Client *acptr;
602 acptr = user->server;
603 if (acptr->from != sptr->from)
605 sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])",
606 sptr, me.name, user->server->name, acptr->from->name,
607 acptr->from->sockhost);
608 sptr->flags |= FLAGS_KILLED;
609 return exit_client(cptr, sptr, &me, "NICK server wrong direction");
612 sptr->flags |= (acptr->flags & FLAGS_TS8);
615 * Check to see if this user is being propogated
616 * as part of a net.burst, or is using protocol 9.
617 * FIXME: This can be speeded up - its stupid to check it for
618 * every NICK message in a burst again --Run.
620 for (acptr = user->server; acptr != &me; acptr = acptr->serv->up) {
621 if (IsBurst(acptr) || Protocol(acptr) < 10)
624 if (!IPcheck_remote_connect(sptr, (acptr != &me)))
626 * We ran out of bits to count this
628 return exit_client(cptr, sptr, &me, "More than 255 connections from this address");
631 tmpstr = umode_str(sptr);
632 sendcmdto_serv_butone(user->server, CMD_NICK, cptr, *tmpstr ?
633 "%s %d %d %s %s +%s %s %s%s :%s" :
634 "%s %d %d %s %s %s%s %s%s :%s",
635 nick, sptr->hopcount + 1, sptr->lastnick,
636 user->username, user->host, tmpstr,
637 inttobase64(ip_base64, ntohl(sptr->ip.s_addr), 6),
638 NumNick(sptr), sptr->info);
640 /* Send umode to client */
643 send_umode(cptr, sptr, 0, ALL_UMODES);
644 if (sptr->snomask != SNO_DEFAULT && (sptr->flags & FLAGS_SERVNOTICE))
645 send_reply(sptr, RPL_SNOMASK, sptr->snomask, sptr->snomask);
652 static const struct UserMode {
657 { FLAGS_LOCOP, 'O' },
658 { FLAGS_INVISIBLE, 'i' },
659 { FLAGS_WALLOP, 'w' },
660 { FLAGS_SERVNOTICE, 's' },
662 { FLAGS_CHSERV, 'k' },
666 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
669 static int user_modes[] = {
672 FLAGS_INVISIBLE, 'i',
674 FLAGS_SERVNOTICE, 's',
683 * XXX - find a way to get rid of this
685 static char umodeBuf[BUFSIZE];
687 int set_nick_name(struct Client* cptr, struct Client* sptr,
688 const char* nick, int parc, char* parv[])
690 if (IsServer(sptr)) {
695 * A server introducing a new client, change source
697 struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
698 assert(0 != new_client);
700 new_client->hopcount = atoi(parv[2]);
701 new_client->lastnick = atoi(parv[3]);
702 if (Protocol(cptr) > 9 && parc > 7 && *parv[6] == '+') {
703 for (p = parv[6] + 1; *p; p++) {
704 for (i = 0; i < USERMODELIST_SIZE; ++i) {
705 if (userModeList[i].c == *p) {
706 new_client->flags |= userModeList[i].flag;
715 strcpy(new_client->name, nick);
716 new_client->user = make_user(new_client);
717 new_client->user->server = sptr;
718 SetRemoteNumNick(new_client, parv[parc - 2]);
720 * IP# of remote client
722 new_client->ip.s_addr = htonl(base64toint(parv[parc - 3]));
724 add_client_to_list(new_client);
725 hAddClient(new_client);
727 sptr->serv->ghost = 0; /* :server NICK means end of net.burst */
728 ircd_strncpy(new_client->username, parv[4], USERLEN);
729 ircd_strncpy(new_client->user->host, parv[5], HOSTLEN);
730 ircd_strncpy(new_client->info, parv[parc - 1], REALLEN);
731 return register_user(cptr, new_client, new_client->name, parv[4]);
733 else if (sptr->name[0]) {
735 * Client changing its nick
737 * If the client belongs to me, then check to see
738 * if client is on any channels where it is currently
739 * banned. If so, do not allow the nick change to occur.
742 const char* channel_name;
743 if ((channel_name = find_no_nickchange_channel(sptr))) {
744 return send_reply(cptr, ERR_BANNICKCHANGE, channel_name);
747 * Refuse nick change if the last nick change was less
748 * then 30 seconds ago. This is intended to get rid of
749 * clone bots doing NICK FLOOD. -SeKs
750 * If someone didn't change their nick for more then 60 seconds
751 * however, allow to do two nick changes immedately after another
752 * before limiting the nick flood. -Run
754 if (CurrentTime < cptr->nextnick) {
756 send_reply(cptr, ERR_NICKTOOFAST, parv[1],
757 cptr->nextnick - CurrentTime);
758 /* Send error message */
759 sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cptr->name);
760 /* bounce NICK to user */
761 return 0; /* ignore nick change! */
764 /* Limit total to 1 change per NICK_DELAY seconds: */
765 cptr->nextnick += NICK_DELAY;
766 /* However allow _maximal_ 1 extra consecutive nick change: */
767 if (cptr->nextnick < CurrentTime)
768 cptr->nextnick = CurrentTime;
772 * Also set 'lastnick' to current time, if changed.
774 if (0 != ircd_strcmp(parv[0], nick))
775 sptr->lastnick = (sptr == cptr) ? TStime() : atoi(parv[2]);
778 * Client just changing his/her nick. If he/she is
779 * on a channel, send note of change to all clients
780 * on that channel. Propagate notice to other servers.
783 sendcmdto_common_channels(sptr, CMD_NICK, ":%s", nick);
784 add_history(sptr, 1);
785 sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick,
789 sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick);
793 strcpy(sptr->name, nick);
797 /* Local client setting NICK the first time */
799 strcpy(sptr->name, nick);
801 sptr->user = make_user(sptr);
802 sptr->user->server = &me;
804 SetLocalNumNick(sptr);
808 * If the client hasn't gotten a cookie-ping yet,
809 * choose a cookie and send it. -record!jegelhof@cloud9.net
813 sptr->cookie = (ircrandom() & 0x7fffffff);
814 } while (!sptr->cookie);
815 sendrawto_one(cptr, MSG_PING " :%u", sptr->cookie);
817 else if (*sptr->user->host && sptr->cookie == COOKIE_VERIFIED) {
819 * USER and PONG already received, now we have NICK.
820 * register_user may reject the client and call exit_client
821 * for it - must test this and exit m_nick too !
823 sptr->lastnick = TStime(); /* Always local client */
824 if (register_user(cptr, sptr, nick, sptr->user->username) == CPTR_KILLED)
831 static unsigned char hash_target(unsigned int target)
833 return (unsigned char) (target >> 16) ^ (target >> 8);
839 * sptr must be a local client!
841 * Cannonifies target for client `sptr'.
843 void add_target(struct Client *sptr, void *target)
845 unsigned char hash = hash_target((unsigned int) target);
846 unsigned char* targets;
851 targets = sptr->targets;
855 for (i = 0; i < MAXTARGETS; ++i) {
856 if (targets[i] == hash)
862 memmove(&targets[RESERVEDTARGETS + 1],
863 &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
864 targets[RESERVEDTARGETS] = hash;
870 * sptr must be a local client !
872 * Returns 'true' (1) when too many targets are addressed.
873 * Returns 'false' (0) when it's ok to send to this target.
875 int check_target_limit(struct Client *sptr, void *target, const char *name,
878 unsigned char hash = hash_target((unsigned int) target);
880 unsigned char* targets;
884 targets = sptr->targets;
887 * Same target as last time?
889 if (targets[0] == hash)
891 for (i = 1; i < MAXTARGETS; ++i) {
892 if (targets[i] == hash) {
893 memmove(&targets[1], &targets[0], i);
902 if (CurrentTime < sptr->nexttarget) {
903 if (sptr->nexttarget - CurrentTime < TARGET_DELAY + 8) {
907 sptr->nexttarget += 2;
908 send_reply(sptr, ERR_TARGETTOOFAST, name,
909 sptr->nexttarget - CurrentTime);
915 /* XXX Let's get rid of GODMODE */
916 sendto_one(sptr, ":%s NOTICE %s :New target: %s; ft " TIME_T_FMT,
917 me.name, sptr->name, name, (CurrentTime - sptr->nexttarget) / TARGET_DELAY);
919 sptr->nexttarget += TARGET_DELAY;
920 if (sptr->nexttarget < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)))
921 sptr->nexttarget = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1));
924 memmove(&targets[1], &targets[0], MAXTARGETS - 1);
930 * whisper - called from m_cnotice and m_cprivmsg.
932 * parv[0] = sender prefix
935 * parv[3] = Private message text
937 * Added 971023 by Run.
938 * Reason: Allows channel operators to sent an arbitrary number of private
939 * messages to users on their channel, avoiding the max.targets limit.
940 * Building this into m_private would use too much cpu because we'd have
941 * to a cross channel lookup for every private message!
942 * Note that we can't allow non-chan ops to use this command, it would be
943 * abused by mass advertisers.
946 int whisper(struct Client* source, const char* nick, const char* channel,
947 const char* text, int is_notice)
950 struct Channel* chptr;
951 struct Membership* membership;
955 assert(0 != channel);
956 assert(MyUser(source));
958 if (!(dest = FindUser(nick))) {
959 return send_reply(source, ERR_NOSUCHNICK, nick);
961 if (!(chptr = FindChannel(channel))) {
962 return send_reply(source, ERR_NOSUCHCHANNEL, channel);
965 * compare both users channel lists, instead of the channels user list
966 * since the link is the same, this should be a little faster for channels
967 * with a lot of users
969 for (membership = source->user->channel; membership; membership = membership->next_channel) {
970 if (chptr == membership->channel)
973 if (0 == membership) {
974 return send_reply(source, ERR_NOTONCHANNEL, chptr->chname);
976 if (!IsVoicedOrOpped(membership)) {
977 return send_reply(source, ERR_VOICENEEDED, chptr->chname);
980 * lookup channel in destination
982 assert(0 != dest->user);
983 for (membership = dest->user->channel; membership; membership = membership->next_channel) {
984 if (chptr == membership->channel)
987 if (0 == membership || IsZombie(membership)) {
988 return send_reply(source, ERR_USERNOTINCHANNEL, dest->name, chptr->chname);
990 if (is_silenced(source, dest))
993 if (dest->user->away)
994 send_reply(source, RPL_AWAY, dest->name, dest->user->away);
996 sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text);
998 sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text);
1004 * added Sat Jul 25 07:30:42 EST 1992
1006 void send_umode_out(struct Client *cptr, struct Client *sptr, int old)
1009 struct Client *acptr;
1011 send_umode(NULL, sptr, old, SEND_UMODES);
1013 for (i = HighestFd; i >= 0; i--) {
1014 if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
1015 (acptr != cptr) && (acptr != sptr) && *umodeBuf)
1016 sendcmdto_one(sptr, CMD_MODE, acptr, "%s :%s", sptr->name, umodeBuf);
1018 if (cptr && MyUser(cptr))
1019 send_umode(cptr, sptr, old, ALL_UMODES);
1024 * send_user_info - send user info userip/userhost
1025 * NOTE: formatter must put info into buffer and return a pointer to the end of
1026 * the data it put in the buffer.
1028 void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
1034 int users_found = 0;
1035 struct Client* acptr;
1036 char buf[BUFSIZE * 2];
1042 sbuf = sprintf_irc(buf, rpl_str(rpl), me.name, sptr->name);
1044 for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
1045 if ((acptr = FindUser(name))) {
1048 sbuf = (*fmt)(acptr, sbuf);
1050 if (5 == ++arg_count)
1054 send_buffer(sptr, buf);
1059 * set_user_mode() added 15/10/91 By Darren Reed.
1062 * parv[1] - username to change mode for
1063 * parv[2] - modes to change
1065 int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
1069 struct Client *acptr;
1073 unsigned int tmpmask = 0;
1074 int snomask_given = 0;
1080 return need_more_params(sptr, "MODE");
1082 if (!(acptr = FindUser(parv[1])))
1084 if (MyConnect(sptr))
1085 send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]);
1089 if (IsServer(sptr) || sptr != acptr)
1092 sendcmdto_flag_butone(&me, CMD_WALLOPS, 0, FLAGS_WALLOP,
1093 ":MODE for User %s from %s!%s", parv[1],
1094 cptr->name, sptr->name);
1096 send_reply(sptr, ERR_USERSDONTMATCH);
1104 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1105 if ( (userModeList[i].flag & sptr->flags))
1106 *m++ = userModeList[i].c;
1109 send_reply(sptr, RPL_UMODEIS, buf);
1110 if ((sptr->flags & FLAGS_SERVNOTICE) && MyConnect(sptr)
1112 (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT))
1113 send_reply(sptr, RPL_SNOMASK, sptr->snomask, sptr->snomask);
1118 * find flags already set for user
1119 * why not just copy them?
1121 setflags = sptr->flags;
1124 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1125 if (sptr->flags & userModeList[i].flag)
1126 setflags |= userModeList[i].flag;
1129 if (MyConnect(sptr))
1130 tmpmask = sptr->snomask;
1133 * parse mode change string(s)
1135 for (p = &parv[2]; *p; p++) { /* p is changed in loop too */
1136 for (m = *p; *m; m++) {
1145 if (*(p + 1) && is_snomask(*(p + 1))) {
1147 tmpmask = umode_make_snomask(tmpmask, *++p, what);
1148 tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1151 tmpmask = (what == MODE_ADD) ?
1152 (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1154 sptr->flags |= FLAGS_SERVNOTICE;
1156 sptr->flags &= ~FLAGS_SERVNOTICE;
1159 if (what == MODE_ADD)
1165 if (what == MODE_ADD)
1168 sptr->flags &= ~(FLAGS_OPER | FLAGS_LOCOP);
1169 if (MyConnect(sptr)) {
1170 tmpmask = sptr->snomask & ~SNO_OPER;
1171 sptr->handler = CLIENT_HANDLER;
1176 if (what == MODE_ADD)
1179 sptr->flags &= ~(FLAGS_OPER | FLAGS_LOCOP);
1180 if (MyConnect(sptr)) {
1181 tmpmask = sptr->snomask & ~SNO_OPER;
1182 sptr->handler = CLIENT_HANDLER;
1187 if (what == MODE_ADD)
1190 ClearInvisible(sptr);
1193 if (what == MODE_ADD)
1199 if (what == MODE_ADD)
1200 SetChannelService(sptr);
1202 ClearChannelService(sptr);
1205 if (what == MODE_ADD)
1216 * Evaluate rules for new user mode
1217 * Stop users making themselves operators too easily:
1219 if (!(setflags & FLAGS_OPER) && IsOper(sptr) && !IsServer(cptr))
1221 if (!(setflags & FLAGS_LOCOP) && IsLocOp(sptr) && !IsServer(cptr))
1223 #ifdef WALLOPS_OPER_ONLY
1225 * only send wallops to opers
1227 if (!IsAnOper(sptr) && !(setflags & FLAGS_WALLOP) && !IsServer(cptr))
1230 if ((setflags & (FLAGS_OPER | FLAGS_LOCOP)) && !IsAnOper(sptr) &&
1232 det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPS);
1234 * new umode; servers can set it, local users cannot;
1235 * prevents users from /kick'ing or /mode -o'ing
1237 if (!(setflags & FLAGS_CHSERV) && !IsServer(cptr))
1238 ClearChannelService(sptr);
1240 * Compare new flags with old flags and send string which
1241 * will cause servers to update correctly.
1243 if ((setflags & FLAGS_OPER) && !IsOper(sptr))
1245 if (!(setflags & FLAGS_OPER) && IsOper(sptr))
1247 if ((setflags & FLAGS_INVISIBLE) && !IsInvisible(sptr))
1248 --UserStats.inv_clients;
1249 if (!(setflags & FLAGS_INVISIBLE) && IsInvisible(sptr))
1250 ++UserStats.inv_clients;
1251 send_umode_out(cptr, sptr, setflags);
1253 if (MyConnect(sptr)) {
1254 if (tmpmask != sptr->snomask)
1255 set_snomask(sptr, tmpmask, SNO_SET);
1256 if (sptr->snomask && snomask_given)
1257 send_reply(sptr, RPL_SNOMASK, sptr->snomask, sptr->snomask);
1264 * Build umode string for BURST command
1267 char *umode_str(struct Client *cptr)
1269 char* m = umodeBuf; /* Maximum string size: "owidg\0" */
1273 c_flags = cptr->flags & SEND_UMODES; /* cleaning up the original code */
1275 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1276 if ( (c_flags & userModeList[i].flag))
1277 *m++ = userModeList[i].c;
1281 return umodeBuf; /* Note: static buffer, gets
1282 overwritten by send_umode() */
1286 * Send the MODE string for user (user) to connection cptr
1289 void send_umode(struct Client *cptr, struct Client *sptr, int old, int sendmask)
1294 int what = MODE_NULL;
1297 * Build a string in umodeBuf to represent the change in the user's
1298 * mode between the new (sptr->flag) and 'old'.
1302 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1303 flag = userModeList[i].flag;
1304 if (MyUser(sptr) && !(flag & sendmask))
1306 if ( (flag & old) && !(sptr->flags & flag))
1308 if (what == MODE_DEL)
1309 *m++ = userModeList[i].c;
1314 *m++ = userModeList[i].c;
1317 else if (!(flag & old) && (sptr->flags & flag))
1319 if (what == MODE_ADD)
1320 *m++ = userModeList[i].c;
1325 *m++ = userModeList[i].c;
1330 if (*umodeBuf && cptr)
1331 sendcmdto_one(sptr, CMD_MODE, cptr, "%s :%s", sptr->name, umodeBuf);
1335 * Check to see if this resembles a sno_mask. It is if 1) there is
1336 * at least one digit and 2) The first digit occurs before the first
1337 * alphabetic character.
1339 int is_snomask(char *word)
1343 for (; *word; word++)
1346 else if (IsAlpha(*word))
1353 * If it begins with a +, count this as an additive mask instead of just
1354 * a replacement. If what == MODE_DEL, "+" has no special effect.
1356 unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
1358 unsigned int sno_what;
1359 unsigned int newmask;
1363 if (what == MODE_ADD)
1368 else if (*arg == '-')
1371 if (what == MODE_ADD)
1377 sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL;
1378 /* pity we don't have strtoul everywhere */
1379 newmask = (unsigned int)atoi(arg);
1380 if (sno_what == SNO_DEL)
1381 newmask = oldmask & ~newmask;
1382 else if (sno_what == SNO_ADD)
1387 static void delfrom_list(struct Client *cptr, struct SLink **list)
1390 struct SLink* prv = NULL;
1392 for (tmp = *list; tmp; tmp = tmp->next) {
1393 if (tmp->value.cptr == cptr) {
1395 prv->next = tmp->next;
1406 * This function sets a Client's server notices mask, according to
1407 * the parameter 'what'. This could be even faster, but the code
1408 * gets mighty hard to read :)
1410 void set_snomask(struct Client *cptr, unsigned int newmask, int what)
1412 unsigned int oldmask, diffmask; /* unsigned please */
1416 oldmask = cptr->snomask;
1418 if (what == SNO_ADD)
1420 else if (what == SNO_DEL)
1421 newmask = oldmask & ~newmask;
1422 else if (what != SNO_SET) /* absolute set, no math needed */
1423 sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what);
1425 newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
1427 diffmask = oldmask ^ newmask;
1429 for (i = 0; diffmask >> i; i++) {
1430 if (((diffmask >> i) & 1))
1432 if (((newmask >> i) & 1))
1435 tmp->next = opsarray[i];
1436 tmp->value.cptr = cptr;
1440 /* not real portable :( */
1441 delfrom_list(cptr, &opsarray[i]);
1444 cptr->snomask = newmask;
1448 * is_silenced : Does the actual check wether sptr is allowed
1449 * to send a message to acptr.
1450 * Both must be registered persons.
1451 * If sptr is silenced by acptr, his message should not be propagated,
1452 * but more over, if this is detected on a server not local to sptr
1453 * the SILENCE mask is sent upstream.
1455 int is_silenced(struct Client *sptr, struct Client *acptr)
1459 static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
1460 static char senderip[16 + NICKLEN + USERLEN + 5];
1462 if (!(acptr->user) || !(lp = acptr->user->silence) || !(user = sptr->user))
1464 sprintf_irc(sender, "%s!%s@%s", sptr->name, user->username, user->host);
1465 sprintf_irc(senderip, "%s!%s@%s", sptr->name, user->username,
1466 ircd_ntoa((const char*) &sptr->ip));
1467 for (; lp; lp = lp->next)
1469 if ((!(lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, sender)) ||
1470 ((lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, senderip)))
1472 if (!MyConnect(sptr))
1474 sendcmdto_one(acptr, CMD_SILENCE, sptr->from, "%C %s", sptr,
1486 * Removes all silence masks from the list of sptr that fall within `mask'
1487 * Returns -1 if none where found, 0 otherwise.
1489 int del_silence(struct Client *sptr, char *mask)
1495 for (lp = &sptr->user->silence; *lp;) {
1496 if (!mmatch(mask, (*lp)->value.cp))
1500 MyFree(tmp->value.cp);
1510 int add_silence(struct Client* sptr, const char* mask)
1512 struct SLink *lp, **lpp;
1513 int cnt = 0, len = strlen(mask);
1516 for (lpp = &sptr->user->silence, lp = *lpp; lp;)
1518 if (0 == ircd_strcmp(mask, lp->value.cp))
1520 if (!mmatch(mask, lp->value.cp))
1522 struct SLink *tmp = lp;
1523 *lpp = lp = lp->next;
1524 MyFree(tmp->value.cp);
1530 len += strlen(lp->value.cp);
1531 if ((len > MAXSILELENGTH) || (++cnt >= MAXSILES))
1533 send_reply(sptr, ERR_SILELISTFULL, mask);
1536 else if (!mmatch(lp->value.cp, mask))
1543 memset(lp, 0, sizeof(struct SLink));
1544 lp->next = sptr->user->silence;
1545 lp->value.cp = (char*) MyMalloc(strlen(mask) + 1);
1546 assert(0 != lp->value.cp);
1547 strcpy(lp->value.cp, mask);
1548 if ((ip_start = strrchr(mask, '@')) && check_if_ipmask(ip_start + 1))
1549 lp->flags = CHFL_SILENCE_IPMASK;
1550 sptr->user->silence = lp;