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.
32 #include "ircd_alloc.h"
33 #include "ircd_chattr.h"
35 #include "ircd_reply.h"
36 #include "ircd_string.h"
43 #include "querycmds.h"
49 #include "s_serv.h" /* max_client_count */
51 #include "sprintf_irc.h"
54 #include "supported.h"
68 static int userCount = 0;
71 * 'make_user' add's an User information block to a client
72 * if it was not previously allocated.
74 struct User *make_user(struct Client *cptr)
79 cptr->user = (struct User*) MyMalloc(sizeof(struct User));
80 assert(0 != cptr->user);
82 /* All variables are 0 by default */
83 memset(cptr->user, 0, sizeof(struct User));
87 cptr->user->refcnt = 1;
95 * Decrease user reference count by one and release block, if count reaches 0.
97 void free_user(struct User* user)
100 assert(0 < user->refcnt);
102 if (--user->refcnt == 0) {
108 assert(0 == user->joined);
109 assert(0 == user->invited);
110 assert(0 == user->channel);
119 void user_count_memory(size_t* count_out, size_t* bytes_out)
121 assert(0 != count_out);
122 assert(0 != bytes_out);
123 *count_out = userCount;
124 *bytes_out = userCount * sizeof(struct User);
128 * user_set_away - set user away state
129 * returns 1 if client is away or changed away message, 0 if
130 * client is removing away status.
131 * NOTE: this function may modify user and message, so they
134 int user_set_away(struct User* user, char* message)
141 if (EmptyString(message)) {
143 * Marking as not away
154 unsigned int len = strlen(message);
156 if (len > TOPICLEN) {
157 message[TOPICLEN] = '\0';
161 away = (char*) MyRealloc(away, len + 1);
163 away = (char*) MyMalloc(len + 1);
167 strcpy(away, message);
169 return (user->away != 0);
175 * Local function to find the next matching client. The search
176 * can be continued from the specified client entry. Normal
179 * for (x = client; x = next_client(x,mask); x = x->next)
180 * HandleMatchingClient;
183 struct Client *next_client(struct Client *next, const char* ch)
185 struct Client *tmp = next;
190 next = FindClient(ch);
191 next = next ? next : tmp;
192 if (tmp->prev == next)
196 for (; next; next = next->next)
197 if (!match(ch, next->name))
205 * Do the basic thing in delivering the message (command)
206 * across the relays to the specific server (server) for
209 * Note: The command is a format string and *MUST* be
210 * of prefixed style (e.g. ":%s COMMAND %s ...").
211 * Command can have only max 8 parameters.
213 * server parv[server] is the parameter identifying the
217 * parv[server] is replaced with the pointer to the
218 * real servername from the matched client (I'm lazy
221 * returns: (see #defines)
223 int hunt_server(int MustBeOper, struct Client *cptr, struct Client *sptr, char *command,
224 int server, int parc, char *parv[])
226 struct Client *acptr;
229 /* Assume it's me, if no server or an unregistered client */
230 if (parc <= server || EmptyString(parv[server]) || IsUnknown(sptr))
231 return (HUNTED_ISME);
233 /* Make sure it's a server */
234 if (MyUser(sptr) || Protocol(cptr) < 10)
236 /* Make sure it's a server */
237 if (!strchr(parv[server], '*')) {
238 if (0 == (acptr = FindClient(parv[server])))
239 return HUNTED_NOSUCH;
241 acptr = acptr->user->server;
243 else if (!(acptr = find_match_server(parv[server])))
245 sendto_one(sptr, err_str(ERR_NOSUCHSERVER),
246 me.name, parv[0], parv[server]);
247 return (HUNTED_NOSUCH);
250 else if (!(acptr = FindNServer(parv[server])))
251 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
254 return (HUNTED_ISME);
256 if (MustBeOper && !IsPrivileged(sptr))
258 sendto_one(sptr, err_str(ERR_NOPRIVILEGES), me.name, sptr->name);
259 return HUNTED_NOSUCH;
262 strcpy(y, acptr->yxx);
265 assert(!IsServer(sptr));
266 sendto_one(acptr, command, NumNick(sptr), parv[1], parv[2], parv[3], parv[4],
267 parv[5], parv[6], parv[7], parv[8]);
269 return (HUNTED_PASS);
273 * 'do_nick_name' ensures that the given parameter (nick) is really a proper
274 * string for a nickname (note, the 'nick' may be modified in the process...)
276 * RETURNS the length of the final NICKNAME (0, if nickname is invalid)
278 * Nickname characters are in range 'A'..'}', '_', '-', '0'..'9'
279 * anything outside the above set will terminate nickname.
280 * In addition, the first character cannot be '-' or a Digit.
283 * The '~'-character should be allowed, but a change should be global,
284 * some confusion would result if only few servers allowed it...
286 int do_nick_name(char* nick)
289 char* end = ch + NICKLEN;
292 if (*ch == '-' || IsDigit(*ch)) /* first character in [0..9-] */
295 for ( ; (ch < end) && *ch; ++ch)
296 if (!IsNickChar(*ch))
307 * Copy `source' to `dest', replacing all occurances of '~' and characters that
308 * are not `isIrcUi' by an underscore.
309 * Copies at most USERLEN - 1 characters or up till the first control character.
310 * If `tilde' is true, then a tilde is prepended to `dest'.
311 * Note that `dest' and `source' can point to the same area or to different
312 * non-overlapping areas.
314 static char *clean_user_id(char *dest, char *source, int tilde)
321 ch = *s++; /* Store first character to copy: */
324 *d++ = '~'; /* If `dest' == `source', then this overwrites `ch' */
327 while (ch && !IsCntrl(ch) && rlen--)
329 char nch = *s++; /* Store next character to copy */
330 *d++ = IsUserChar(ch) ? ch : '_'; /* This possibly overwrites it */
343 * This function is called when both NICK and USER messages
344 * have been accepted for the client, in whatever order. Only
345 * after this the USER message is propagated.
347 * NICK's must be propagated at once when received, although
348 * it would be better to delay them too until full info is
349 * available. Doing it is not so simple though, would have
350 * to implement the following:
352 * 1) user telnets in and gives only "NICK foobar" and waits
353 * 2) another user far away logs in normally with the nick
354 * "foobar" (quite legal, as this server didn't propagate it).
355 * 3) now this server gets nick "foobar" from outside, but
356 * has already the same defined locally. Current server
357 * would just issue "KILL foobar" to clean out dups. But,
358 * this is not fair. It should actually request another
359 * nick from local user or kill him/her...
361 int register_user(struct Client *cptr, struct Client *sptr,
362 const char *nick, char *username)
364 struct ConfItem* aconf;
368 char c = 0; /* not alphanum */
369 char d = 'a'; /* not a digit */
377 short digitgroups = 0;
378 struct User* user = sptr->user;
380 char featurebuf[512];
382 user->last = CurrentTime;
383 parv[0] = sptr->name;
384 parv[1] = parv[2] = NULL;
388 static time_t last_too_many1;
389 static time_t last_too_many2;
391 assert(cptr == sptr);
392 switch (conf_check_client(sptr))
396 case ACR_NO_AUTHORIZATION:
397 sendto_op_mask(SNO_UNAUTH, "Unauthorized connection from %s.",
398 get_client_name(sptr, HIDE_IP));
399 ++ServerStats->is_ref;
400 return exit_client(cptr, sptr, &me,
401 "No Authorization - use another server");
402 case ACR_TOO_MANY_IN_CLASS:
403 if (CurrentTime - last_too_many1 >= (time_t) 60)
405 last_too_many1 = CurrentTime;
406 sendto_op_mask(SNO_TOOMANY, "Too many connections in class for %s.",
407 get_client_name(sptr, HIDE_IP));
409 ++ServerStats->is_ref;
410 IPcheck_connect_fail(sptr->ip);
411 return exit_client(cptr, sptr, &me,
412 "Sorry, your connection class is full - try again later or try another server");
413 case ACR_TOO_MANY_FROM_IP:
414 if (CurrentTime - last_too_many2 >= (time_t) 60)
416 last_too_many2 = CurrentTime;
417 sendto_op_mask(SNO_TOOMANY, "Too many connections from same IP for %s.",
418 get_client_name(sptr, HIDE_IP));
420 ++ServerStats->is_ref;
421 return exit_client(cptr, sptr, &me,
422 "Too many connections from your host");
423 case ACR_ALREADY_AUTHORIZED:
424 /* Can this ever happen? */
426 ++ServerStats->is_ref;
427 IPcheck_connect_fail(sptr->ip);
428 return exit_client(cptr, sptr, &me, "Unknown error -- Try again");
430 ircd_strncpy(user->host, sptr->sockhost, HOSTLEN);
431 aconf = sptr->confs->value.aconf;
433 clean_user_id(user->username,
434 (sptr->flags & FLAGS_GOTID) ? sptr->username : username,
435 (sptr->flags & FLAGS_DOID) && !(sptr->flags & FLAGS_GOTID));
437 if ((user->username[0] == '\0')
438 || ((user->username[0] == '~') && (user->username[1] == '\000')))
439 return exit_client(cptr, sptr, &me, "USER: Bogus userid.");
441 if (!EmptyString(aconf->passwd)
442 && !(IsDigit(*aconf->passwd) && !aconf->passwd[1])
444 && strcmp("ONE", aconf->passwd)
446 && strcmp(sptr->passwd, aconf->passwd))
448 ServerStats->is_ref++;
449 IPcheck_connect_fail(sptr->ip);
450 sendto_one(sptr, err_str(ERR_PASSWDMISMATCH), me.name, parv[0]);
451 return exit_client(cptr, sptr, &me, "Bad Password");
453 memset(sptr->passwd, 0, sizeof(sptr->passwd));
455 * following block for the benefit of time-dependent K:-lines
457 if (find_kill(sptr)) {
458 ServerStats->is_ref++;
459 IPcheck_connect_fail(sptr->ip);
460 return exit_client(cptr, sptr, &me, "K-lined");
463 * Check for mixed case usernames, meaning probably hacked. Jon2 3-94
464 * Summary of rules now implemented in this patch: Ensor 11-94
465 * In a mixed-case name, if first char is upper, one more upper may
466 * appear anywhere. (A mixed-case name *must* have an upper first
467 * char, and may have one other upper.)
468 * A third upper may appear if all 3 appear at the beginning of the
469 * name, separated only by "others" (-/_/.).
470 * A single group of digits is allowed anywhere.
471 * Two groups of digits are allowed if at least one of the groups is
472 * at the beginning or the end.
473 * Only one '-', '_', or '.' is allowed (or two, if not consecutive).
474 * But not as the first or last char.
475 * No other special characters are allowed.
476 * Name must contain at least one letter.
478 tmpstr2 = tmpstr = (username[0] == '~' ? &username[1] : username);
479 while (*tmpstr && !badid)
491 if ((leadcaps || pos == 1) && !lower && !digits)
497 if (pos == 1 || !IsDigit(d))
504 else if (c == '-' || c == '_' || c == '.')
509 else if (d == '-' || d == '_' || d == '.' || other > 2)
518 if (lower && upper && (!leadcaps || leadcaps > 3 ||
519 (upper > 2 && upper > leadcaps)))
521 else if (digitgroups == 2 && !(IsDigit(tmpstr2[0]) || IsDigit(c)))
523 else if ((!lower && !upper) || !IsAlnum(c))
526 if (badid && (!(sptr->flags & FLAGS_GOTID) ||
527 strcmp(sptr->username, username) != 0))
529 ServerStats->is_ref++;
530 sendto_one(cptr, ":%s %d %s :Your username is invalid.",
531 me.name, ERR_INVALIDUSERNAME, cptr->name);
533 ":%s %d %s :Connect with your real username, in lowercase.",
534 me.name, ERR_INVALIDUSERNAME, cptr->name);
535 sendto_one(cptr, ":%s %d %s :If your mail address were foo@bar.com, "
536 "your username would be foo.",
537 me.name, ERR_INVALIDUSERNAME, cptr->name);
538 return exit_client(cptr, sptr, &me, "USER: Bad username");
540 Count_unknownbecomesclient(sptr, UserStats);
543 ircd_strncpy(user->username, username, USERLEN);
544 Count_newremoteclient(UserStats, user->server);
548 if (IsInvisible(sptr))
549 ++UserStats.inv_clients;
553 if (MyConnect(sptr)) {
554 sptr->handler = CLIENT_HANDLER;
555 release_dns_reply(sptr);
557 sendto_one(sptr, rpl_str(RPL_WELCOME), me.name, nick, nick);
559 * This is a duplicate of the NOTICE but see below...
561 sendto_one(sptr, rpl_str(RPL_YOURHOST), me.name, nick,
563 sendto_one(sptr, rpl_str(RPL_CREATED), me.name, nick, creation);
564 sendto_one(sptr, rpl_str(RPL_MYINFO), me.name, parv[0], me.name, version);
565 sprintf_irc(featurebuf,FEATURES,FEATURESVALUES);
566 sendto_one(sptr, rpl_str(RPL_ISUPPORT), me.name, nick, featurebuf);
567 m_lusers(sptr, sptr, 1, parv);
570 m_motd(sptr, NULL, 1, parv);
572 m_motd(sptr, sptr, 1, parv);
574 nextping = CurrentTime;
575 if (sptr->snomask & SNO_NOISY)
576 set_snomask(sptr, sptr->snomask & SNO_NOISY, SNO_ADD);
577 #ifdef ALLOW_SNO_CONNEXIT
578 #ifdef SNO_CONNEXIT_IP
580 ":%s NOTICE * :*** Notice -- Client connecting: %s (%s@%s) [%s] {%d}",
581 me.name, nick, user->username, user->host, cptr->sock_ip,
582 get_client_class(sptr));
583 sendbufto_op_mask(SNO_CONNEXIT);
584 #else /* SNO_CONNEXIT_IP */
586 ":%s NOTICE * :*** Notice -- Client connecting: %s (%s@%s)",
587 me.name, nick, user->username, user->host);
588 sendbufto_op_mask(SNO_CONNEXIT);
589 #endif /* SNO_CONNEXIT_IP */
590 #endif /* ALLOW_SNO_CONNEXIT */
591 IPcheck_connect_succeeded(sptr);
594 /* if (IsServer(cptr)) */
596 struct Client *acptr;
598 acptr = user->server;
599 if (acptr->from != sptr->from)
601 sendto_one(cptr, "%s " TOK_KILL " %s%s :%s (%s != %s[%s])",
602 NumServ(&me), NumNick(sptr), me.name, user->server->name,
603 acptr->from->name, acptr->from->sockhost);
604 sptr->flags |= FLAGS_KILLED;
605 return exit_client(cptr, sptr, &me, "NICK server wrong direction");
608 sptr->flags |= (acptr->flags & FLAGS_TS8);
611 * Check to see if this user is being propogated
612 * as part of a net.burst, or is using protocol 9.
613 * FIXME: This can be speeded up - its stupid to check it for
614 * every NICK message in a burst again --Run.
616 for (acptr = user->server; acptr != &me; acptr = acptr->serv->up) {
617 if (IsBurst(acptr) || Protocol(acptr) < 10)
620 if (IPcheck_remote_connect(sptr, user->host, (acptr != &me)) == -1)
622 * We ran out of bits to count this
624 return exit_client(cptr, sptr, &me, "More than 255 connections from this address");
627 tmpstr = umode_str(sptr);
628 sendto_serv_butone(cptr, *tmpstr ?
629 "%s " TOK_NICK " %s %d %d %s %s +%s %s %s%s :%s" :
630 "%s " TOK_NICK " %s %d %d %s %s %s%s %s%s :%s",
631 NumServ(user->server), nick, sptr->hopcount + 1, sptr->lastnick,
632 user->username, user->host, tmpstr,
633 inttobase64(ip_base64, ntohl(sptr->ip.s_addr), 6),
634 NumNick(sptr), sptr->info);
636 /* Send umode to client */
639 send_umode(cptr, sptr, 0, ALL_UMODES);
640 if (sptr->snomask != SNO_DEFAULT && (sptr->flags & FLAGS_SERVNOTICE))
641 sendto_one(sptr, rpl_str(RPL_SNOMASK), me.name, sptr->name,
642 sptr->snomask, sptr->snomask);
649 static const struct UserMode {
654 { FLAGS_LOCOP, 'O' },
655 { FLAGS_INVISIBLE, 'i' },
656 { FLAGS_WALLOP, 'w' },
657 { FLAGS_SERVNOTICE, 's' },
659 { FLAGS_CHSERV, 'k' },
663 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
666 static int user_modes[] = {
669 FLAGS_INVISIBLE, 'i',
671 FLAGS_SERVNOTICE, 's',
680 * XXX - find a way to get rid of this
682 static char umodeBuf[BUFSIZE];
684 int set_nick_name(struct Client* cptr, struct Client* sptr,
685 const char* nick, int parc, char* parv[])
687 if (IsServer(sptr)) {
692 * A server introducing a new client, change source
694 struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
695 assert(0 != new_client);
697 new_client->hopcount = atoi(parv[2]);
698 new_client->lastnick = atoi(parv[3]);
699 if (Protocol(cptr) > 9 && parc > 7 && *parv[6] == '+') {
700 for (p = parv[6] + 1; *p; p++) {
701 for (i = 0; i < USERMODELIST_SIZE; ++i) {
702 if (userModeList[i].c == *p) {
703 new_client->flags |= userModeList[i].flag;
712 strcpy(new_client->name, nick);
713 new_client->user = make_user(new_client);
714 new_client->user->server = sptr;
715 SetRemoteNumNick(new_client, parv[parc - 2]);
717 * IP# of remote client
719 new_client->ip.s_addr = htonl(base64toint(parv[parc - 3]));
721 add_client_to_list(new_client);
722 hAddClient(new_client);
724 sptr->serv->ghost = 0; /* :server NICK means end of net.burst */
725 ircd_strncpy(new_client->username, parv[4], USERLEN);
726 ircd_strncpy(new_client->user->host, parv[5], HOSTLEN);
727 ircd_strncpy(new_client->info, parv[parc - 1], REALLEN);
728 return register_user(cptr, new_client, new_client->name, parv[4]);
730 else if (sptr->name[0]) {
732 * Client changing its nick
734 * If the client belongs to me, then check to see
735 * if client is on any channels where it is currently
736 * banned. If so, do not allow the nick change to occur.
739 const char* channel_name;
740 if ((channel_name = find_no_nickchange_channel(sptr))) {
741 sendto_one(cptr, err_str(ERR_BANNICKCHANGE), me.name, parv[0],
746 * Refuse nick change if the last nick change was less
747 * then 30 seconds ago. This is intended to get rid of
748 * clone bots doing NICK FLOOD. -SeKs
749 * If someone didn't change their nick for more then 60 seconds
750 * however, allow to do two nick changes immedately after another
751 * before limiting the nick flood. -Run
753 if (CurrentTime < cptr->nextnick) {
755 sendto_one(cptr, err_str(ERR_NICKTOOFAST),
756 me.name, parv[0], parv[1], cptr->nextnick - CurrentTime);
757 /* Send error message */
758 sendto_prefix_one(cptr, cptr, ":%s NICK %s", parv[0], parv[0]);
759 /* bounce NICK to user */
760 return 0; /* ignore nick change! */
763 /* Limit total to 1 change per NICK_DELAY seconds: */
764 cptr->nextnick += NICK_DELAY;
765 /* However allow _maximal_ 1 extra consecutive nick change: */
766 if (cptr->nextnick < CurrentTime)
767 cptr->nextnick = CurrentTime;
771 * Also set 'lastnick' to current time, if changed.
773 if (0 != ircd_strcmp(parv[0], nick))
774 sptr->lastnick = (sptr == cptr) ? TStime() : atoi(parv[2]);
777 * Client just changing his/her nick. If he/she is
778 * on a channel, send note of change to all clients
779 * on that channel. Propagate notice to other servers.
782 sendto_common_channels(sptr, ":%s NICK :%s", parv[0], nick);
783 add_history(sptr, 1);
784 sendto_serv_butone(cptr,
785 "%s%s " TOK_NICK " %s " TIME_T_FMT, NumNick(sptr), nick, sptr->lastnick);
788 sendto_one(sptr, ":%s NICK :%s", parv[0], nick);
792 strcpy(sptr->name, nick);
796 /* Local client setting NICK the first time */
798 strcpy(sptr->name, nick);
800 sptr->user = make_user(sptr);
801 sptr->user->server = &me;
803 SetLocalNumNick(sptr);
807 * If the client hasn't gotten a cookie-ping yet,
808 * choose a cookie and send it. -record!jegelhof@cloud9.net
812 sptr->cookie = (ircrandom() & 0x7fffffff);
813 } while (!sptr->cookie);
814 sendto_one(cptr, "PING :%u", sptr->cookie);
816 else if (*sptr->user->host && sptr->cookie == COOKIE_VERIFIED) {
818 * USER and PONG already received, now we have NICK.
819 * register_user may reject the client and call exit_client
820 * for it - must test this and exit m_nick too !
822 sptr->lastnick = TStime(); /* Always local client */
823 if (register_user(cptr, sptr, nick, sptr->user->username) == CPTR_KILLED)
834 * sptr must be a local client!
836 * Cannonifies target for client `sptr'.
838 void add_target(struct Client *sptr, void *target)
841 unsigned int tmp = ((size_t) target & 0xffff00) >> 8;
842 unsigned char hash = (tmp * tmp) >> 12;
843 if (sptr->targets[0] == hash) /* Last person that we messaged ourself? */
845 for (p = sptr->targets; p < &sptr->targets[MAXTARGETS - 1];)
847 return; /* Already in table */
850 memmove(&sptr->targets[RESERVEDTARGETS + 1],
851 &sptr->targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
852 sptr->targets[RESERVEDTARGETS] = hash;
859 * sptr must be a local client !
861 * Returns 'true' (1) when too many targets are addressed.
862 * Returns 'false' (0) when it's ok to send to this target.
864 int check_target_limit(struct Client *sptr, void *target, const char *name,
868 unsigned int tmp = ((size_t)target & 0xffff00) >> 8;
869 unsigned char hash = (tmp * tmp) >> 12;
870 if (sptr->targets[0] == hash) /* Same target as last time ? */
872 for (p = sptr->targets; p < &sptr->targets[MAXTARGETS - 1];)
875 memmove(&sptr->targets[1], &sptr->targets[0], p - sptr->targets);
876 sptr->targets[0] = hash;
883 if (CurrentTime < sptr->nexttarget)
885 if (sptr->nexttarget - CurrentTime < TARGET_DELAY + 8) /* No server flooding */
887 sptr->nexttarget += 2;
888 sendto_one(sptr, err_str(ERR_TARGETTOOFAST),
889 me.name, sptr->name, name, sptr->nexttarget - CurrentTime);
896 sendto_one(sptr, ":%s NOTICE %s :New target: %s; ft " TIME_T_FMT,
897 me.name, sptr->name, name, (CurrentTime - sptr->nexttarget) / TARGET_DELAY);
899 sptr->nexttarget += TARGET_DELAY;
900 if (sptr->nexttarget < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)))
901 sptr->nexttarget = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1));
904 memmove(&sptr->targets[1], &sptr->targets[0], MAXTARGETS - 1);
905 sptr->targets[0] = hash;
910 * whisper - called from m_cnotice and m_cprivmsg.
912 * parv[0] = sender prefix
915 * parv[3] = Private message text
917 * Added 971023 by Run.
918 * Reason: Allows channel operators to sent an arbitrary number of private
919 * messages to users on their channel, avoiding the max.targets limit.
920 * Building this into m_private would use too much cpu because we'd have
921 * to a cross channel lookup for every private message!
922 * Note that we can't allow non-chan ops to use this command, it would be
923 * abused by mass advertisers.
926 int whisper(struct Client* source, const char* nick, const char* channel,
927 const char* text, int is_notice)
930 struct Channel* chptr;
931 struct Membership* membership;
935 assert(0 != channel);
936 assert(MyUser(source));
938 if (!(dest = FindUser(nick))) {
939 sendto_one(source, err_str(ERR_NOSUCHNICK), me.name, source->name, nick);
942 if (!(chptr = FindChannel(channel))) {
943 sendto_one(source, err_str(ERR_NOSUCHCHANNEL), me.name, source->name, channel);
947 * compare both users channel lists, instead of the channels user list
948 * since the link is the same, this should be a little faster for channels
949 * with a lot of users
951 for (membership = source->user->channel; membership; membership = membership->next_channel) {
952 if (chptr == membership->channel)
955 if (0 == membership) {
956 sendto_one(source, err_str(ERR_NOTONCHANNEL), me.name, source->name, chptr->chname);
959 if (!IsVoicedOrOpped(membership)) {
960 sendto_one(source, err_str(ERR_VOICENEEDED), me.name, source->name, chptr->chname);
964 * lookup channel in destination
966 assert(0 != dest->user);
967 for (membership = dest->user->channel; membership; membership = membership->next_channel) {
968 if (chptr == membership->channel)
971 if (0 == membership || IsZombie(membership)) {
972 sendto_one(source, err_str(ERR_USERNOTINCHANNEL), me.name,
973 source->name, dest->name, chptr->chname);
976 if (is_silenced(source, dest))
979 if (dest->user->away)
980 sendto_one(source, rpl_str(RPL_AWAY), me.name, source->name,
981 dest->name, dest->user->away);
983 sendto_prefix_one(dest, source, ":%s %s %s :%s",
984 source->name, is_notice ? MSG_NOTICE : MSG_PRIVATE,
987 sendto_one(dest, "%s%s %s %s%s :%s",
988 NumNick(source), is_notice ? TOK_NOTICE : TOK_PRIVATE,
989 NumNick(dest), text);
995 * added Sat Jul 25 07:30:42 EST 1992
997 void send_umode_out(struct Client *cptr, struct Client *sptr, int old)
1000 struct Client *acptr;
1002 send_umode(NULL, sptr, old, SEND_UMODES);
1004 for (i = HighestFd; i >= 0; i--) {
1005 if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
1006 (acptr != cptr) && (acptr != sptr) && *umodeBuf)
1007 sendto_one(acptr, "%s%s " TOK_MODE " %s :%s", NumNick(sptr), sptr->name, umodeBuf);
1009 if (cptr && MyUser(cptr))
1010 send_umode(cptr, sptr, old, ALL_UMODES);
1015 * send_user_info - send user info userip/userhost
1016 * NOTE: formatter must put info into buffer and return a pointer to the end of
1017 * the data it put in the buffer.
1019 void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
1025 int users_found = 0;
1026 struct Client* acptr;
1027 char buf[BUFSIZE * 2];
1033 sbuf = sprintf_irc(buf, rpl_str(rpl), me.name, sptr->name);
1035 for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
1036 if ((acptr = FindUser(name))) {
1039 sbuf = (*fmt)(acptr, sbuf);
1042 send_error_to_client(sptr, ERR_NOSUCHNICK, name);
1043 if (5 == ++arg_count)
1047 send_buffer(sptr, buf);
1052 * set_user_mode() added 15/10/91 By Darren Reed.
1055 * parv[1] - username to change mode for
1056 * parv[2] - modes to change
1058 int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
1062 struct Client *acptr;
1066 unsigned int tmpmask = 0;
1067 int snomask_given = 0;
1073 return need_more_params(sptr, "MODE");
1075 if (!(acptr = FindUser(parv[1])))
1077 if (MyConnect(sptr))
1078 sendto_one(sptr, err_str(ERR_NOSUCHCHANNEL), me.name, parv[0], parv[1]);
1082 if (IsServer(sptr) || sptr != acptr)
1085 sendto_ops_butone(NULL, &me, ":%s WALLOPS :MODE for User %s From %s!%s",
1086 me.name, parv[1], cptr->name, sptr->name);
1088 sendto_one(sptr, err_str(ERR_USERSDONTMATCH), me.name, parv[0]);
1096 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1097 if ( (userModeList[i].flag & sptr->flags))
1098 *m++ = userModeList[i].c;
1101 sendto_one(sptr, rpl_str(RPL_UMODEIS), me.name, parv[0], buf);
1102 if ((sptr->flags & FLAGS_SERVNOTICE) && MyConnect(sptr)
1104 (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT))
1105 sendto_one(sptr, rpl_str(RPL_SNOMASK), me.name, parv[0], sptr->snomask,
1111 * find flags already set for user
1112 * why not just copy them?
1114 setflags = sptr->flags;
1117 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1118 if (sptr->flags & userModeList[i].flag)
1119 setflags |= userModeList[i].flag;
1122 if (MyConnect(sptr))
1123 tmpmask = sptr->snomask;
1126 * parse mode change string(s)
1128 for (p = &parv[2]; *p; p++) { /* p is changed in loop too */
1129 for (m = *p; *m; m++) {
1138 if (*(p + 1) && is_snomask(*(p + 1))) {
1140 tmpmask = umode_make_snomask(tmpmask, *++p, what);
1141 tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1144 tmpmask = (what == MODE_ADD) ?
1145 (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1147 sptr->flags |= FLAGS_SERVNOTICE;
1149 sptr->flags &= ~FLAGS_SERVNOTICE;
1152 if (what == MODE_ADD)
1158 if (what == MODE_ADD)
1161 sptr->flags &= ~(FLAGS_OPER | FLAGS_LOCOP);
1162 if (MyConnect(sptr)) {
1163 tmpmask = sptr->snomask & ~SNO_OPER;
1164 sptr->handler = CLIENT_HANDLER;
1169 if (what == MODE_ADD)
1172 sptr->flags &= ~(FLAGS_OPER | FLAGS_LOCOP);
1173 if (MyConnect(sptr)) {
1174 tmpmask = sptr->snomask & ~SNO_OPER;
1175 sptr->handler = CLIENT_HANDLER;
1180 if (what == MODE_ADD)
1183 ClearInvisible(sptr);
1186 if (what == MODE_ADD)
1192 if (what == MODE_ADD)
1193 SetChannelService(sptr);
1195 ClearChannelService(sptr);
1198 if (what == MODE_ADD)
1209 * Evaluate rules for new user mode
1210 * Stop users making themselves operators too easily:
1212 if (!(setflags & FLAGS_OPER) && IsOper(sptr) && !IsServer(cptr))
1214 if (!(setflags & FLAGS_LOCOP) && IsLocOp(sptr) && !IsServer(cptr))
1216 #ifdef WALLOPS_OPER_ONLY
1218 * only send wallops to opers
1220 if (!IsAnOper(sptr) && !(setflags & FLAGS_WALLOP) && !IsServer(cptr))
1223 if ((setflags & (FLAGS_OPER | FLAGS_LOCOP)) && !IsAnOper(sptr) &&
1225 det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPS);
1227 * new umode; servers can set it, local users cannot;
1228 * prevents users from /kick'ing or /mode -o'ing
1230 if (!(setflags & FLAGS_CHSERV) && !IsServer(cptr))
1231 ClearChannelService(sptr);
1233 * Compare new flags with old flags and send string which
1234 * will cause servers to update correctly.
1236 if ((setflags & FLAGS_OPER) && !IsOper(sptr))
1238 if (!(setflags & FLAGS_OPER) && IsOper(sptr))
1240 if ((setflags & FLAGS_INVISIBLE) && !IsInvisible(sptr))
1241 --UserStats.inv_clients;
1242 if (!(setflags & FLAGS_INVISIBLE) && IsInvisible(sptr))
1243 ++UserStats.inv_clients;
1244 send_umode_out(cptr, sptr, setflags);
1246 if (MyConnect(sptr)) {
1247 if (tmpmask != sptr->snomask)
1248 set_snomask(sptr, tmpmask, SNO_SET);
1249 if (sptr->snomask && snomask_given)
1250 sendto_one(sptr, rpl_str(RPL_SNOMASK), me.name, sptr->name,
1251 sptr->snomask, sptr->snomask);
1258 * Build umode string for BURST command
1261 char *umode_str(struct Client *cptr)
1263 char* m = umodeBuf; /* Maximum string size: "owidg\0" */
1267 c_flags = cptr->flags & SEND_UMODES; /* cleaning up the original code */
1269 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1270 if ( (c_flags & userModeList[i].flag))
1271 *m++ = userModeList[i].c;
1275 return umodeBuf; /* Note: static buffer, gets
1276 overwritten by send_umode() */
1280 * Send the MODE string for user (user) to connection cptr
1283 void send_umode(struct Client *cptr, struct Client *sptr, int old, int sendmask)
1288 int what = MODE_NULL;
1291 * Build a string in umodeBuf to represent the change in the user's
1292 * mode between the new (sptr->flag) and 'old'.
1296 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1297 flag = userModeList[i].flag;
1298 if (MyUser(sptr) && !(flag & sendmask))
1300 if ( (flag & old) && !(sptr->flags & flag))
1302 if (what == MODE_DEL)
1303 *m++ = userModeList[i].c;
1308 *m++ = userModeList[i].c;
1311 else if (!(flag & old) && (sptr->flags & flag))
1313 if (what == MODE_ADD)
1314 *m++ = userModeList[i].c;
1319 *m++ = userModeList[i].c;
1324 if (*umodeBuf && cptr)
1325 sendto_one(cptr, ":%s MODE %s :%s", sptr->name, sptr->name, umodeBuf);
1329 * Check to see if this resembles a sno_mask. It is if 1) there is
1330 * at least one digit and 2) The first digit occurs before the first
1331 * alphabetic character.
1333 int is_snomask(char *word)
1337 for (; *word; word++)
1340 else if (IsAlpha(*word))
1347 * If it begins with a +, count this as an additive mask instead of just
1348 * a replacement. If what == MODE_DEL, "+" has no special effect.
1350 unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
1352 unsigned int sno_what;
1353 unsigned int newmask;
1357 if (what == MODE_ADD)
1362 else if (*arg == '-')
1365 if (what == MODE_ADD)
1371 sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL;
1372 /* pity we don't have strtoul everywhere */
1373 newmask = (unsigned int)atoi(arg);
1374 if (sno_what == SNO_DEL)
1375 newmask = oldmask & ~newmask;
1376 else if (sno_what == SNO_ADD)
1381 static void delfrom_list(struct Client *cptr, struct SLink **list)
1384 struct SLink* prv = NULL;
1386 for (tmp = *list; tmp; tmp = tmp->next) {
1387 if (tmp->value.cptr == cptr) {
1389 prv->next = tmp->next;
1400 * This function sets a Client's server notices mask, according to
1401 * the parameter 'what'. This could be even faster, but the code
1402 * gets mighty hard to read :)
1404 void set_snomask(struct Client *cptr, unsigned int newmask, int what)
1406 unsigned int oldmask, diffmask; /* unsigned please */
1410 oldmask = cptr->snomask;
1412 if (what == SNO_ADD)
1414 else if (what == SNO_DEL)
1415 newmask = oldmask & ~newmask;
1416 else if (what != SNO_SET) /* absolute set, no math needed */
1417 sendto_ops("setsnomask called with %d ?!", what);
1419 newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
1421 diffmask = oldmask ^ newmask;
1423 for (i = 0; diffmask >> i; i++) {
1424 if (((diffmask >> i) & 1))
1426 if (((newmask >> i) & 1))
1429 tmp->next = opsarray[i];
1430 tmp->value.cptr = cptr;
1434 /* not real portable :( */
1435 delfrom_list(cptr, &opsarray[i]);
1438 cptr->snomask = newmask;
1442 * is_silenced : Does the actual check wether sptr is allowed
1443 * to send a message to acptr.
1444 * Both must be registered persons.
1445 * If sptr is silenced by acptr, his message should not be propagated,
1446 * but more over, if this is detected on a server not local to sptr
1447 * the SILENCE mask is sent upstream.
1449 int is_silenced(struct Client *sptr, struct Client *acptr)
1453 static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
1454 static char senderip[16 + NICKLEN + USERLEN + 5];
1456 if (!(acptr->user) || !(lp = acptr->user->silence) || !(user = sptr->user))
1458 sprintf_irc(sender, "%s!%s@%s", sptr->name, user->username, user->host);
1459 sprintf_irc(senderip, "%s!%s@%s", sptr->name, user->username,
1460 ircd_ntoa((const char*) &sptr->ip));
1461 for (; lp; lp = lp->next)
1463 if ((!(lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, sender)) ||
1464 ((lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, senderip)))
1466 if (!MyConnect(sptr))
1468 if (Protocol(sptr->from) < 10)
1469 sendto_one(sptr->from, ":%s SILENCE %s %s", acptr->name,
1470 sptr->name, lp->value.cp);
1472 sendto_one(sptr->from, ":%s SILENCE %s%s %s", acptr->name,
1473 NumNick(sptr), lp->value.cp);
1484 * Removes all silence masks from the list of sptr that fall within `mask'
1485 * Returns -1 if none where found, 0 otherwise.
1487 int del_silence(struct Client *sptr, char *mask)
1493 for (lp = &sptr->user->silence; *lp;) {
1494 if (!mmatch(mask, (*lp)->value.cp))
1498 MyFree(tmp->value.cp);
1508 int add_silence(struct Client* sptr, const char* mask)
1510 struct SLink *lp, **lpp;
1511 int cnt = 0, len = strlen(mask);
1514 for (lpp = &sptr->user->silence, lp = *lpp; lp;)
1516 if (0 == ircd_strcmp(mask, lp->value.cp))
1518 if (!mmatch(mask, lp->value.cp))
1520 struct SLink *tmp = lp;
1521 *lpp = lp = lp->next;
1522 MyFree(tmp->value.cp);
1528 len += strlen(lp->value.cp);
1529 if ((len > MAXSILELENGTH) || (++cnt >= MAXSILES))
1531 sendto_one(sptr, err_str(ERR_SILELISTFULL), me.name, sptr->name, mask);
1534 else if (!mmatch(lp->value.cp, mask))
1541 memset(lp, 0, sizeof(struct SLink));
1542 lp->next = sptr->user->silence;
1543 lp->value.cp = (char*) MyMalloc(strlen(mask) + 1);
1544 assert(0 != lp->value.cp);
1545 strcpy(lp->value.cp, mask);
1546 if ((ip_start = strrchr(mask, '@')) && check_if_ipmask(ip_start + 1))
1547 lp->flags = CHFL_SILENCE_IPMASK;
1548 sptr->user->silence = lp;