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 sendto_one(sptr, err_str(ERR_NOSUCHSERVER),
243 me.name, parv[0], parv[server]);
244 return HUNTED_NOSUCH;
247 acptr = acptr->user->server;
249 else if (!(acptr = find_match_server(parv[server])))
251 send_reply(sptr, ERR_NOSUCHSERVER, parv[server]);
252 return (HUNTED_NOSUCH);
255 else if (!(acptr = FindNServer(parv[server])))
256 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
259 return (HUNTED_ISME);
261 if (MustBeOper && !IsPrivileged(sptr))
263 send_reply(sptr, ERR_NOPRIVILEGES);
264 return HUNTED_NOSUCH;
267 strcpy(y, acptr->yxx);
270 assert(!IsServer(sptr));
271 /* XXX sendto_one used with explicit command; must be very careful */
272 sendto_one(acptr, command, NumNick(sptr), parv[1], parv[2], parv[3], parv[4], /* XXX hunt_server */
273 parv[5], parv[6], parv[7], parv[8]);
275 return (HUNTED_PASS);
278 int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok,
279 struct Client *one, int MustBeOper, const char *pattern,
280 int server, int parc, char *parv[])
282 struct Client *acptr;
285 /* Assume it's me, if no server or an unregistered client */
286 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
287 return (HUNTED_ISME);
289 /* Make sure it's a server */
291 /* Make sure it's a server */
292 if (!strchr(to, '*')) {
293 if (0 == (acptr = FindClient(to)))
294 return HUNTED_NOSUCH;
297 acptr = acptr->user->server;
298 } else if (!(acptr = find_match_server(to))) {
299 send_reply(from, ERR_NOSUCHSERVER, to);
300 return (HUNTED_NOSUCH);
302 } else if (!(acptr = FindNServer(to)))
303 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
306 return (HUNTED_ISME);
308 if (MustBeOper && !IsPrivileged(from)) {
309 send_reply(from, ERR_NOPRIVILEGES);
310 return HUNTED_NOSUCH;
313 assert(!IsServer(from));
315 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
317 sendcmdto_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
318 parv[4], parv[5], parv[6], parv[7], parv[8]);
320 return (HUNTED_PASS);
324 * 'do_nick_name' ensures that the given parameter (nick) is really a proper
325 * string for a nickname (note, the 'nick' may be modified in the process...)
327 * RETURNS the length of the final NICKNAME (0, if nickname is invalid)
329 * Nickname characters are in range 'A'..'}', '_', '-', '0'..'9'
330 * anything outside the above set will terminate nickname.
331 * In addition, the first character cannot be '-' or a Digit.
334 * The '~'-character should be allowed, but a change should be global,
335 * some confusion would result if only few servers allowed it...
337 int do_nick_name(char* nick)
340 char* end = ch + NICKLEN;
343 if (*ch == '-' || IsDigit(*ch)) /* first character in [0..9-] */
346 for ( ; (ch < end) && *ch; ++ch)
347 if (!IsNickChar(*ch))
358 * Copy `source' to `dest', replacing all occurances of '~' and characters that
359 * are not `isIrcUi' by an underscore.
360 * Copies at most USERLEN - 1 characters or up till the first control character.
361 * If `tilde' is true, then a tilde is prepended to `dest'.
362 * Note that `dest' and `source' can point to the same area or to different
363 * non-overlapping areas.
365 static char *clean_user_id(char *dest, char *source, int tilde)
372 ch = *s++; /* Store first character to copy: */
375 *d++ = '~'; /* If `dest' == `source', then this overwrites `ch' */
378 while (ch && !IsCntrl(ch) && rlen--)
380 char nch = *s++; /* Store next character to copy */
381 *d++ = IsUserChar(ch) ? ch : '_'; /* This possibly overwrites it */
394 * This function is called when both NICK and USER messages
395 * have been accepted for the client, in whatever order. Only
396 * after this the USER message is propagated.
398 * NICK's must be propagated at once when received, although
399 * it would be better to delay them too until full info is
400 * available. Doing it is not so simple though, would have
401 * to implement the following:
403 * 1) user telnets in and gives only "NICK foobar" and waits
404 * 2) another user far away logs in normally with the nick
405 * "foobar" (quite legal, as this server didn't propagate it).
406 * 3) now this server gets nick "foobar" from outside, but
407 * has already the same defined locally. Current server
408 * would just issue "KILL foobar" to clean out dups. But,
409 * this is not fair. It should actually request another
410 * nick from local user or kill him/her...
412 int register_user(struct Client *cptr, struct Client *sptr,
413 const char *nick, char *username, struct Gline *agline)
415 struct ConfItem* aconf;
419 char c = 0; /* not alphanum */
420 char d = 'a'; /* not a digit */
428 short digitgroups = 0;
429 struct User* user = sptr->user;
431 char featurebuf[512];
433 user->last = CurrentTime;
434 parv[0] = sptr->name;
435 parv[1] = parv[2] = NULL;
439 static time_t last_too_many1;
440 static time_t last_too_many2;
442 assert(cptr == sptr);
443 switch (conf_check_client(sptr))
447 case ACR_NO_AUTHORIZATION:
448 sendto_opmask_butone(0, SNO_UNAUTH, "Unauthorized connection from %s.",
449 get_client_name(sptr, HIDE_IP));
450 ++ServerStats->is_ref;
451 return exit_client(cptr, sptr, &me,
452 "No Authorization - use another server");
453 case ACR_TOO_MANY_IN_CLASS:
454 if (CurrentTime - last_too_many1 >= (time_t) 60)
456 last_too_many1 = CurrentTime;
457 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections in "
459 get_client_name(sptr, HIDE_IP));
461 ++ServerStats->is_ref;
462 IPcheck_connect_fail(sptr->ip);
463 return exit_client(cptr, sptr, &me,
464 "Sorry, your connection class is full - try "
465 "again later or try another server");
466 case ACR_TOO_MANY_FROM_IP:
467 if (CurrentTime - last_too_many2 >= (time_t) 60)
469 last_too_many2 = CurrentTime;
470 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections from "
472 get_client_name(sptr, HIDE_IP));
474 ++ServerStats->is_ref;
475 return exit_client(cptr, sptr, &me,
476 "Too many connections from your host");
477 case ACR_ALREADY_AUTHORIZED:
478 /* Can this ever happen? */
480 ++ServerStats->is_ref;
481 IPcheck_connect_fail(sptr->ip);
482 return exit_client(cptr, sptr, &me, "Unknown error -- Try again");
484 ircd_strncpy(user->host, sptr->sockhost, HOSTLEN);
485 aconf = sptr->confs->value.aconf;
487 clean_user_id(user->username,
488 (sptr->flags & FLAGS_GOTID) ? sptr->username : username,
489 (sptr->flags & FLAGS_DOID) && !(sptr->flags & FLAGS_GOTID));
491 if ((user->username[0] == '\0')
492 || ((user->username[0] == '~') && (user->username[1] == '\000')))
493 return exit_client(cptr, sptr, &me, "USER: Bogus userid.");
495 if (!EmptyString(aconf->passwd)
496 && !(IsDigit(*aconf->passwd) && !aconf->passwd[1])
497 && strcmp(sptr->passwd, aconf->passwd))
499 ServerStats->is_ref++;
500 IPcheck_connect_fail(sptr->ip);
501 send_reply(sptr, ERR_PASSWDMISMATCH);
502 return exit_client(cptr, sptr, &me, "Bad Password");
504 memset(sptr->passwd, 0, sizeof(sptr->passwd));
506 * following block for the benefit of time-dependent K:-lines
508 if (find_kill(sptr)) {
509 ServerStats->is_ref++;
510 IPcheck_connect_fail(sptr->ip);
511 return exit_client(cptr, sptr, &me, "K-lined");
514 * Check for mixed case usernames, meaning probably hacked. Jon2 3-94
515 * Summary of rules now implemented in this patch: Ensor 11-94
516 * In a mixed-case name, if first char is upper, one more upper may
517 * appear anywhere. (A mixed-case name *must* have an upper first
518 * char, and may have one other upper.)
519 * A third upper may appear if all 3 appear at the beginning of the
520 * name, separated only by "others" (-/_/.).
521 * A single group of digits is allowed anywhere.
522 * Two groups of digits are allowed if at least one of the groups is
523 * at the beginning or the end.
524 * Only one '-', '_', or '.' is allowed (or two, if not consecutive).
525 * But not as the first or last char.
526 * No other special characters are allowed.
527 * Name must contain at least one letter.
529 tmpstr2 = tmpstr = (username[0] == '~' ? &username[1] : username);
530 while (*tmpstr && !badid)
542 if ((leadcaps || pos == 1) && !lower && !digits)
548 if (pos == 1 || !IsDigit(d))
555 else if (c == '-' || c == '_' || c == '.')
560 else if (d == '-' || d == '_' || d == '.' || other > 2)
569 if (lower && upper && (!leadcaps || leadcaps > 3 ||
570 (upper > 2 && upper > leadcaps)))
572 else if (digitgroups == 2 && !(IsDigit(tmpstr2[0]) || IsDigit(c)))
574 else if ((!lower && !upper) || !IsAlnum(c))
577 if (badid && (!(sptr->flags & FLAGS_GOTID) ||
578 strcmp(sptr->username, username) != 0))
580 ServerStats->is_ref++;
582 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
583 ":Your username is invalid.");
584 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
585 ":Connect with your real username, in lowercase.");
586 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
587 ":If your mail address were foo@bar.com, your username "
589 return exit_client(cptr, sptr, &me, "USER: Bad username");
591 Count_unknownbecomesclient(sptr, UserStats);
594 ircd_strncpy(user->username, username, USERLEN);
595 Count_newremoteclient(UserStats, user->server);
599 /* a gline wasn't passed in, so find a matching global one that isn't
600 * a Uworld-set one, and propagate it if there is such an animal.
603 (agline = gline_lookup(sptr, GLINE_GLOBAL | GLINE_LASTMOD)) &&
604 !IsBurstOrBurstAck(cptr))
605 gline_resend(cptr, agline);
607 if (IsInvisible(sptr))
608 ++UserStats.inv_clients;
612 if (MyConnect(sptr)) {
613 sptr->handler = CLIENT_HANDLER;
614 release_dns_reply(sptr);
616 send_reply(sptr, RPL_WELCOME, nick);
618 * This is a duplicate of the NOTICE but see below...
620 send_reply(sptr, RPL_YOURHOST, me.name, version);
621 send_reply(sptr, RPL_CREATED, creation);
622 send_reply(sptr, RPL_MYINFO, me.name, version);
623 sprintf_irc(featurebuf,FEATURES,FEATURESVALUES);
624 send_reply(sptr, RPL_ISUPPORT, featurebuf);
625 m_lusers(sptr, sptr, 1, parv);
628 m_motd(sptr, NULL, 1, parv);
630 m_motd(sptr, sptr, 1, parv);
632 nextping = CurrentTime;
633 if (sptr->snomask & SNO_NOISY)
634 set_snomask(sptr, sptr->snomask & SNO_NOISY, SNO_ADD);
635 IPcheck_connect_succeeded(sptr);
638 /* if (IsServer(cptr)) */
640 struct Client *acptr;
642 acptr = user->server;
643 if (acptr->from != sptr->from)
645 sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])",
646 sptr, me.name, user->server->name, acptr->from->name,
647 acptr->from->sockhost);
648 sptr->flags |= FLAGS_KILLED;
649 return exit_client(cptr, sptr, &me, "NICK server wrong direction");
652 sptr->flags |= (acptr->flags & FLAGS_TS8);
655 * Check to see if this user is being propogated
656 * as part of a net.burst, or is using protocol 9.
657 * FIXME: This can be speeded up - its stupid to check it for
658 * every NICK message in a burst again --Run.
660 for (acptr = user->server; acptr != &me; acptr = acptr->serv->up) {
661 if (IsBurst(acptr) || Protocol(acptr) < 10)
664 if (!IPcheck_remote_connect(sptr, (acptr != &me))) {
666 * We ran out of bits to count this
668 sendcmdto_one(&me, CMD_KILL, sptr, "%C :%s (Too many connections from your host -- Ghost)",
670 return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled");
673 tmpstr = umode_str(sptr);
675 sendcmdto_serv_butone(user->server, CMD_NICK, cptr,
676 "%s %d %Tu %s %s %s%s%s%%%Tu:%s@%s %s %s%s :%s",
677 nick, sptr->hopcount + 1, sptr->lastnick,
678 user->username, user->host,
679 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
680 GlineLastMod(agline), GlineUser(agline),
682 inttobase64(ip_base64, ntohl(sptr->ip.s_addr), 6),
683 NumNick(sptr), sptr->info);
685 sendcmdto_serv_butone(user->server, CMD_NICK, cptr,
686 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
687 nick, sptr->hopcount + 1, sptr->lastnick,
688 user->username, user->host,
689 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
690 inttobase64(ip_base64, ntohl(sptr->ip.s_addr), 6),
691 NumNick(sptr), sptr->info);
693 /* Send umode to client */
696 send_umode(cptr, sptr, 0, ALL_UMODES);
697 if (sptr->snomask != SNO_DEFAULT && (sptr->flags & FLAGS_SERVNOTICE))
698 send_reply(sptr, RPL_SNOMASK, sptr->snomask, sptr->snomask);
705 static const struct UserMode {
710 { FLAGS_LOCOP, 'O' },
711 { FLAGS_INVISIBLE, 'i' },
712 { FLAGS_WALLOP, 'w' },
713 { FLAGS_SERVNOTICE, 's' },
715 { FLAGS_CHSERV, 'k' },
719 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
722 static int user_modes[] = {
725 FLAGS_INVISIBLE, 'i',
727 FLAGS_SERVNOTICE, 's',
736 * XXX - find a way to get rid of this
738 static char umodeBuf[BUFSIZE];
740 int set_nick_name(struct Client* cptr, struct Client* sptr,
741 const char* nick, int parc, char* parv[])
743 if (IsServer(sptr)) {
747 struct Gline *agline = 0;
750 * A server introducing a new client, change source
752 struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
753 assert(0 != new_client);
755 new_client->hopcount = atoi(parv[2]);
756 new_client->lastnick = atoi(parv[3]);
757 if (Protocol(cptr) > 9 && parc > 7 && *parv[6] == '+') {
758 for (p = parv[6] + 1; *p; p++) {
759 for (i = 0; i < USERMODELIST_SIZE; ++i) {
760 if (userModeList[i].c == *p) {
761 new_client->flags |= userModeList[i].flag;
770 strcpy(new_client->name, nick);
771 new_client->user = make_user(new_client);
772 new_client->user->server = sptr;
773 SetRemoteNumNick(new_client, parv[parc - 2]);
775 * IP# of remote client
777 new_client->ip.s_addr = htonl(base64toint(parv[parc - 3]));
779 add_client_to_list(new_client);
780 hAddClient(new_client);
782 sptr->serv->ghost = 0; /* :server NICK means end of net.burst */
783 ircd_strncpy(new_client->username, parv[4], USERLEN);
784 ircd_strncpy(new_client->user->host, parv[5], HOSTLEN);
785 ircd_strncpy(new_client->info, parv[parc - 1], REALLEN);
787 /* Deal with GLINE parameters... */
788 if (*parv[parc - 4] == '%' && (t = strchr(parv[parc - 4] + 1, ':'))) {
792 lastmod = atoi(parv[parc - 4] + 1);
795 (agline = gline_find(t, GLINE_EXACT | GLINE_GLOBAL | GLINE_LASTMOD))
796 && GlineLastMod(agline) > lastmod && !IsBurstOrBurstAck(cptr))
797 gline_resend(cptr, agline);
799 return register_user(cptr, new_client, new_client->name, parv[4], agline);
801 else if (sptr->name[0]) {
803 * Client changing its nick
805 * If the client belongs to me, then check to see
806 * if client is on any channels where it is currently
807 * banned. If so, do not allow the nick change to occur.
810 const char* channel_name;
811 if ((channel_name = find_no_nickchange_channel(sptr))) {
812 return send_reply(cptr, ERR_BANNICKCHANGE, channel_name);
815 * Refuse nick change if the last nick change was less
816 * then 30 seconds ago. This is intended to get rid of
817 * clone bots doing NICK FLOOD. -SeKs
818 * If someone didn't change their nick for more then 60 seconds
819 * however, allow to do two nick changes immedately after another
820 * before limiting the nick flood. -Run
822 if (CurrentTime < cptr->nextnick) {
824 send_reply(cptr, ERR_NICKTOOFAST, parv[1],
825 cptr->nextnick - CurrentTime);
826 /* Send error message */
827 sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cptr->name);
828 /* bounce NICK to user */
829 return 0; /* ignore nick change! */
832 /* Limit total to 1 change per NICK_DELAY seconds: */
833 cptr->nextnick += NICK_DELAY;
834 /* However allow _maximal_ 1 extra consecutive nick change: */
835 if (cptr->nextnick < CurrentTime)
836 cptr->nextnick = CurrentTime;
840 * Also set 'lastnick' to current time, if changed.
842 if (0 != ircd_strcmp(parv[0], nick))
843 sptr->lastnick = (sptr == cptr) ? TStime() : atoi(parv[2]);
846 * Client just changing his/her nick. If he/she is
847 * on a channel, send note of change to all clients
848 * on that channel. Propagate notice to other servers.
851 sendcmdto_common_channels(sptr, CMD_NICK, ":%s", nick);
852 add_history(sptr, 1);
853 sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick,
857 sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick);
861 strcpy(sptr->name, nick);
865 /* Local client setting NICK the first time */
867 strcpy(sptr->name, nick);
869 sptr->user = make_user(sptr);
870 sptr->user->server = &me;
872 SetLocalNumNick(sptr);
876 * If the client hasn't gotten a cookie-ping yet,
877 * choose a cookie and send it. -record!jegelhof@cloud9.net
881 sptr->cookie = (ircrandom() & 0x7fffffff);
882 } while (!sptr->cookie);
883 sendrawto_one(cptr, MSG_PING " :%u", sptr->cookie);
885 else if (*sptr->user->host && sptr->cookie == COOKIE_VERIFIED) {
887 * USER and PONG already received, now we have NICK.
888 * register_user may reject the client and call exit_client
889 * for it - must test this and exit m_nick too !
891 sptr->lastnick = TStime(); /* Always local client */
892 if (register_user(cptr, sptr, nick, sptr->user->username, 0) == CPTR_KILLED)
899 static unsigned char hash_target(unsigned int target)
901 return (unsigned char) (target >> 16) ^ (target >> 8);
907 * sptr must be a local client!
909 * Cannonifies target for client `sptr'.
911 void add_target(struct Client *sptr, void *target)
913 /* Ok, this shouldn't work esp on alpha
915 unsigned char hash = hash_target((unsigned long) target);
916 unsigned char* targets;
921 targets = sptr->targets;
925 for (i = 0; i < MAXTARGETS; ++i) {
926 if (targets[i] == hash)
932 memmove(&targets[RESERVEDTARGETS + 1],
933 &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
934 targets[RESERVEDTARGETS] = hash;
940 * sptr must be a local client !
942 * Returns 'true' (1) when too many targets are addressed.
943 * Returns 'false' (0) when it's ok to send to this target.
945 int check_target_limit(struct Client *sptr, void *target, const char *name,
948 unsigned char hash = hash_target((unsigned long) target);
950 unsigned char* targets;
954 targets = sptr->targets;
957 * Same target as last time?
959 if (targets[0] == hash)
961 for (i = 1; i < MAXTARGETS; ++i) {
962 if (targets[i] == hash) {
963 memmove(&targets[1], &targets[0], i);
972 if (CurrentTime < sptr->nexttarget) {
973 if (sptr->nexttarget - CurrentTime < TARGET_DELAY + 8) {
977 sptr->nexttarget += 2;
978 send_reply(sptr, ERR_TARGETTOOFAST, name,
979 sptr->nexttarget - CurrentTime);
985 /* XXX Let's get rid of GODMODE */
986 sendto_one(sptr, ":%s NOTICE %s :New target: %s; ft " TIME_T_FMT, /* XXX Possibly DEAD */
987 me.name, sptr->name, name, (CurrentTime - sptr->nexttarget) / TARGET_DELAY);
989 sptr->nexttarget += TARGET_DELAY;
990 if (sptr->nexttarget < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)))
991 sptr->nexttarget = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1));
994 memmove(&targets[1], &targets[0], MAXTARGETS - 1);
1000 * whisper - called from m_cnotice and m_cprivmsg.
1002 * parv[0] = sender prefix
1004 * parv[2] = #channel
1005 * parv[3] = Private message text
1007 * Added 971023 by Run.
1008 * Reason: Allows channel operators to sent an arbitrary number of private
1009 * messages to users on their channel, avoiding the max.targets limit.
1010 * Building this into m_private would use too much cpu because we'd have
1011 * to a cross channel lookup for every private message!
1012 * Note that we can't allow non-chan ops to use this command, it would be
1013 * abused by mass advertisers.
1016 int whisper(struct Client* source, const char* nick, const char* channel,
1017 const char* text, int is_notice)
1019 struct Client* dest;
1020 struct Channel* chptr;
1021 struct Membership* membership;
1023 assert(0 != source);
1025 assert(0 != channel);
1026 assert(MyUser(source));
1028 if (!(dest = FindUser(nick))) {
1029 return send_reply(source, ERR_NOSUCHNICK, nick);
1031 if (!(chptr = FindChannel(channel))) {
1032 return send_reply(source, ERR_NOSUCHCHANNEL, channel);
1035 * compare both users channel lists, instead of the channels user list
1036 * since the link is the same, this should be a little faster for channels
1037 * with a lot of users
1039 for (membership = source->user->channel; membership; membership = membership->next_channel) {
1040 if (chptr == membership->channel)
1043 if (0 == membership) {
1044 return send_reply(source, ERR_NOTONCHANNEL, chptr->chname);
1046 if (!IsVoicedOrOpped(membership)) {
1047 return send_reply(source, ERR_VOICENEEDED, chptr->chname);
1050 * lookup channel in destination
1052 assert(0 != dest->user);
1053 for (membership = dest->user->channel; membership; membership = membership->next_channel) {
1054 if (chptr == membership->channel)
1057 if (0 == membership || IsZombie(membership)) {
1058 return send_reply(source, ERR_USERNOTINCHANNEL, dest->name, chptr->chname);
1060 if (is_silenced(source, dest))
1063 if (dest->user->away)
1064 send_reply(source, RPL_AWAY, dest->name, dest->user->away);
1066 sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text);
1068 sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text);
1074 * added Sat Jul 25 07:30:42 EST 1992
1076 void send_umode_out(struct Client *cptr, struct Client *sptr, int old)
1079 struct Client *acptr;
1081 send_umode(NULL, sptr, old, SEND_UMODES);
1083 for (i = HighestFd; i >= 0; i--) {
1084 if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
1085 (acptr != cptr) && (acptr != sptr) && *umodeBuf)
1086 sendcmdto_one(sptr, CMD_MODE, acptr, "%s :%s", sptr->name, umodeBuf);
1088 if (cptr && MyUser(cptr))
1089 send_umode(cptr, sptr, old, ALL_UMODES);
1094 * send_user_info - send user info userip/userhost
1095 * NOTE: formatter must put info into buffer and return a pointer to the end of
1096 * the data it put in the buffer.
1098 void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
1104 int users_found = 0;
1105 struct Client* acptr;
1106 char buf[BUFSIZE * 2];
1112 sbuf = sprintf_irc(buf, rpl_str(rpl), me.name, sptr->name);
1114 for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
1115 if ((acptr = FindUser(name))) {
1118 sbuf = (*fmt)(acptr, sbuf);
1120 if (5 == ++arg_count)
1123 send_buffer(sptr, buf);
1128 * set_user_mode() added 15/10/91 By Darren Reed.
1131 * parv[1] - username to change mode for
1132 * parv[2] - modes to change
1134 int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
1138 struct Client *acptr;
1142 unsigned int tmpmask = 0;
1143 int snomask_given = 0;
1149 return need_more_params(sptr, "MODE");
1151 if (!(acptr = FindUser(parv[1])))
1153 if (MyConnect(sptr))
1154 send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]);
1158 if (IsServer(sptr) || sptr != acptr)
1161 sendcmdto_flag_butone(&me, CMD_WALLOPS, 0, FLAGS_WALLOP,
1162 ":MODE for User %s from %s!%s", parv[1],
1163 cptr->name, sptr->name);
1165 send_reply(sptr, ERR_USERSDONTMATCH);
1173 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1174 if ( (userModeList[i].flag & sptr->flags))
1175 *m++ = userModeList[i].c;
1178 send_reply(sptr, RPL_UMODEIS, buf);
1179 if ((sptr->flags & FLAGS_SERVNOTICE) && MyConnect(sptr)
1181 (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT))
1182 send_reply(sptr, RPL_SNOMASK, sptr->snomask, sptr->snomask);
1187 * find flags already set for user
1188 * why not just copy them?
1190 setflags = sptr->flags;
1193 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1194 if (sptr->flags & userModeList[i].flag)
1195 setflags |= userModeList[i].flag;
1198 if (MyConnect(sptr))
1199 tmpmask = sptr->snomask;
1202 * parse mode change string(s)
1204 for (p = &parv[2]; *p; p++) { /* p is changed in loop too */
1205 for (m = *p; *m; m++) {
1214 if (*(p + 1) && is_snomask(*(p + 1))) {
1216 tmpmask = umode_make_snomask(tmpmask, *++p, what);
1217 tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1220 tmpmask = (what == MODE_ADD) ?
1221 (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1223 sptr->flags |= FLAGS_SERVNOTICE;
1225 sptr->flags &= ~FLAGS_SERVNOTICE;
1228 if (what == MODE_ADD)
1234 if (what == MODE_ADD)
1237 sptr->flags &= ~(FLAGS_OPER | FLAGS_LOCOP);
1238 if (MyConnect(sptr)) {
1239 tmpmask = sptr->snomask & ~SNO_OPER;
1240 sptr->handler = CLIENT_HANDLER;
1245 if (what == MODE_ADD)
1248 sptr->flags &= ~(FLAGS_OPER | FLAGS_LOCOP);
1249 if (MyConnect(sptr)) {
1250 tmpmask = sptr->snomask & ~SNO_OPER;
1251 sptr->handler = CLIENT_HANDLER;
1256 if (what == MODE_ADD)
1259 ClearInvisible(sptr);
1262 if (what == MODE_ADD)
1268 if (what == MODE_ADD)
1269 SetChannelService(sptr);
1271 ClearChannelService(sptr);
1274 if (what == MODE_ADD)
1285 * Evaluate rules for new user mode
1286 * Stop users making themselves operators too easily:
1288 if (!(setflags & FLAGS_OPER) && IsOper(sptr) && !IsServer(cptr))
1290 if (!(setflags & FLAGS_LOCOP) && IsLocOp(sptr) && !IsServer(cptr))
1292 #ifdef WALLOPS_OPER_ONLY
1294 * only send wallops to opers
1296 if (!IsAnOper(sptr) && !(setflags & FLAGS_WALLOP) && !IsServer(cptr))
1299 if ((setflags & (FLAGS_OPER | FLAGS_LOCOP)) && !IsAnOper(sptr) &&
1301 det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPS);
1303 * new umode; servers can set it, local users cannot;
1304 * prevents users from /kick'ing or /mode -o'ing
1306 if (!(setflags & FLAGS_CHSERV) && !IsServer(cptr))
1307 ClearChannelService(sptr);
1309 * Compare new flags with old flags and send string which
1310 * will cause servers to update correctly.
1312 if ((setflags & FLAGS_OPER) && !IsOper(sptr))
1314 if (!(setflags & FLAGS_OPER) && IsOper(sptr))
1316 if ((setflags & FLAGS_INVISIBLE) && !IsInvisible(sptr))
1317 --UserStats.inv_clients;
1318 if (!(setflags & FLAGS_INVISIBLE) && IsInvisible(sptr))
1319 ++UserStats.inv_clients;
1320 send_umode_out(cptr, sptr, setflags);
1322 if (MyConnect(sptr)) {
1323 if (tmpmask != sptr->snomask)
1324 set_snomask(sptr, tmpmask, SNO_SET);
1325 if (sptr->snomask && snomask_given)
1326 send_reply(sptr, RPL_SNOMASK, sptr->snomask, sptr->snomask);
1333 * Build umode string for BURST command
1336 char *umode_str(struct Client *cptr)
1338 char* m = umodeBuf; /* Maximum string size: "owidg\0" */
1342 c_flags = cptr->flags & SEND_UMODES; /* cleaning up the original code */
1344 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1345 if ( (c_flags & userModeList[i].flag))
1346 *m++ = userModeList[i].c;
1350 return umodeBuf; /* Note: static buffer, gets
1351 overwritten by send_umode() */
1355 * Send the MODE string for user (user) to connection cptr
1358 void send_umode(struct Client *cptr, struct Client *sptr, int old, int sendmask)
1363 int what = MODE_NULL;
1366 * Build a string in umodeBuf to represent the change in the user's
1367 * mode between the new (sptr->flag) and 'old'.
1371 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1372 flag = userModeList[i].flag;
1373 if (MyUser(sptr) && !(flag & sendmask))
1375 if ( (flag & old) && !(sptr->flags & flag))
1377 if (what == MODE_DEL)
1378 *m++ = userModeList[i].c;
1383 *m++ = userModeList[i].c;
1386 else if (!(flag & old) && (sptr->flags & flag))
1388 if (what == MODE_ADD)
1389 *m++ = userModeList[i].c;
1394 *m++ = userModeList[i].c;
1399 if (*umodeBuf && cptr)
1400 sendcmdto_one(sptr, CMD_MODE, cptr, "%s :%s", sptr->name, umodeBuf);
1404 * Check to see if this resembles a sno_mask. It is if 1) there is
1405 * at least one digit and 2) The first digit occurs before the first
1406 * alphabetic character.
1408 int is_snomask(char *word)
1412 for (; *word; word++)
1415 else if (IsAlpha(*word))
1422 * If it begins with a +, count this as an additive mask instead of just
1423 * a replacement. If what == MODE_DEL, "+" has no special effect.
1425 unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
1427 unsigned int sno_what;
1428 unsigned int newmask;
1432 if (what == MODE_ADD)
1437 else if (*arg == '-')
1440 if (what == MODE_ADD)
1446 sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL;
1447 /* pity we don't have strtoul everywhere */
1448 newmask = (unsigned int)atoi(arg);
1449 if (sno_what == SNO_DEL)
1450 newmask = oldmask & ~newmask;
1451 else if (sno_what == SNO_ADD)
1456 static void delfrom_list(struct Client *cptr, struct SLink **list)
1459 struct SLink* prv = NULL;
1461 for (tmp = *list; tmp; tmp = tmp->next) {
1462 if (tmp->value.cptr == cptr) {
1464 prv->next = tmp->next;
1475 * This function sets a Client's server notices mask, according to
1476 * the parameter 'what'. This could be even faster, but the code
1477 * gets mighty hard to read :)
1479 void set_snomask(struct Client *cptr, unsigned int newmask, int what)
1481 unsigned int oldmask, diffmask; /* unsigned please */
1485 oldmask = cptr->snomask;
1487 if (what == SNO_ADD)
1489 else if (what == SNO_DEL)
1490 newmask = oldmask & ~newmask;
1491 else if (what != SNO_SET) /* absolute set, no math needed */
1492 sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what);
1494 newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
1496 diffmask = oldmask ^ newmask;
1498 for (i = 0; diffmask >> i; i++) {
1499 if (((diffmask >> i) & 1))
1501 if (((newmask >> i) & 1))
1504 tmp->next = opsarray[i];
1505 tmp->value.cptr = cptr;
1509 /* not real portable :( */
1510 delfrom_list(cptr, &opsarray[i]);
1513 cptr->snomask = newmask;
1517 * is_silenced : Does the actual check wether sptr is allowed
1518 * to send a message to acptr.
1519 * Both must be registered persons.
1520 * If sptr is silenced by acptr, his message should not be propagated,
1521 * but more over, if this is detected on a server not local to sptr
1522 * the SILENCE mask is sent upstream.
1524 int is_silenced(struct Client *sptr, struct Client *acptr)
1528 static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
1529 static char senderip[16 + NICKLEN + USERLEN + 5];
1531 if (!(acptr->user) || !(lp = acptr->user->silence) || !(user = sptr->user))
1533 sprintf_irc(sender, "%s!%s@%s", sptr->name, user->username, user->host);
1534 sprintf_irc(senderip, "%s!%s@%s", sptr->name, user->username,
1535 ircd_ntoa((const char*) &sptr->ip));
1536 for (; lp; lp = lp->next)
1538 if ((!(lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, sender)) ||
1539 ((lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, senderip)))
1541 if (!MyConnect(sptr))
1543 sendcmdto_one(acptr, CMD_SILENCE, sptr->from, "%C %s", sptr,
1555 * Removes all silence masks from the list of sptr that fall within `mask'
1556 * Returns -1 if none where found, 0 otherwise.
1558 int del_silence(struct Client *sptr, char *mask)
1564 for (lp = &sptr->user->silence; *lp;) {
1565 if (!mmatch(mask, (*lp)->value.cp))
1569 MyFree(tmp->value.cp);
1579 int add_silence(struct Client* sptr, const char* mask)
1581 struct SLink *lp, **lpp;
1582 int cnt = 0, len = strlen(mask);
1585 for (lpp = &sptr->user->silence, lp = *lpp; lp;)
1587 if (0 == ircd_strcmp(mask, lp->value.cp))
1589 if (!mmatch(mask, lp->value.cp))
1591 struct SLink *tmp = lp;
1592 *lpp = lp = lp->next;
1593 MyFree(tmp->value.cp);
1599 len += strlen(lp->value.cp);
1600 if ((len > MAXSILELENGTH) || (++cnt >= MAXSILES))
1602 send_reply(sptr, ERR_SILELISTFULL, mask);
1605 else if (!mmatch(lp->value.cp, mask))
1612 memset(lp, 0, sizeof(struct SLink));
1613 lp->next = sptr->user->silence;
1614 lp->value.cp = (char*) MyMalloc(strlen(mask) + 1);
1615 assert(0 != lp->value.cp);
1616 strcpy(lp->value.cp, mask);
1617 if ((ip_start = strrchr(mask, '@')) && check_if_ipmask(ip_start + 1))
1618 lp->flags = CHFL_SILENCE_IPMASK;
1619 sptr->user->silence = lp;