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], /* XXX hunt_server */
270 parv[5], parv[6], parv[7], parv[8]);
272 return (HUNTED_PASS);
275 int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok,
276 struct Client *one, int MustBeOper, const char *pattern,
277 int server, int parc, char *parv[])
279 struct Client *acptr;
282 /* Assume it's me, if no server or an unregistered client */
283 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
284 return (HUNTED_ISME);
286 /* Make sure it's a server */
288 /* Make sure it's a server */
289 if (!strchr(to, '*')) {
290 if (0 == (acptr = FindClient(to)))
291 return HUNTED_NOSUCH;
294 acptr = acptr->user->server;
295 } else if (!(acptr = find_match_server(to))) {
296 send_reply(from, ERR_NOSUCHSERVER, to);
297 return (HUNTED_NOSUCH);
299 } else if (!(acptr = FindNServer(to)))
300 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
303 return (HUNTED_ISME);
305 if (MustBeOper && !IsPrivileged(from)) {
306 send_reply(from, ERR_NOPRIVILEGES);
307 return HUNTED_NOSUCH;
310 assert(!IsServer(from));
312 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
314 sendcmdto_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
315 parv[4], parv[5], parv[6], parv[7], parv[8]);
317 return (HUNTED_PASS);
321 * 'do_nick_name' ensures that the given parameter (nick) is really a proper
322 * string for a nickname (note, the 'nick' may be modified in the process...)
324 * RETURNS the length of the final NICKNAME (0, if nickname is invalid)
326 * Nickname characters are in range 'A'..'}', '_', '-', '0'..'9'
327 * anything outside the above set will terminate nickname.
328 * In addition, the first character cannot be '-' or a Digit.
331 * The '~'-character should be allowed, but a change should be global,
332 * some confusion would result if only few servers allowed it...
334 int do_nick_name(char* nick)
337 char* end = ch + NICKLEN;
340 if (*ch == '-' || IsDigit(*ch)) /* first character in [0..9-] */
343 for ( ; (ch < end) && *ch; ++ch)
344 if (!IsNickChar(*ch))
355 * Copy `source' to `dest', replacing all occurances of '~' and characters that
356 * are not `isIrcUi' by an underscore.
357 * Copies at most USERLEN - 1 characters or up till the first control character.
358 * If `tilde' is true, then a tilde is prepended to `dest'.
359 * Note that `dest' and `source' can point to the same area or to different
360 * non-overlapping areas.
362 static char *clean_user_id(char *dest, char *source, int tilde)
369 ch = *s++; /* Store first character to copy: */
372 *d++ = '~'; /* If `dest' == `source', then this overwrites `ch' */
375 while (ch && !IsCntrl(ch) && rlen--)
377 char nch = *s++; /* Store next character to copy */
378 *d++ = IsUserChar(ch) ? ch : '_'; /* This possibly overwrites it */
391 * This function is called when both NICK and USER messages
392 * have been accepted for the client, in whatever order. Only
393 * after this the USER message is propagated.
395 * NICK's must be propagated at once when received, although
396 * it would be better to delay them too until full info is
397 * available. Doing it is not so simple though, would have
398 * to implement the following:
400 * 1) user telnets in and gives only "NICK foobar" and waits
401 * 2) another user far away logs in normally with the nick
402 * "foobar" (quite legal, as this server didn't propagate it).
403 * 3) now this server gets nick "foobar" from outside, but
404 * has already the same defined locally. Current server
405 * would just issue "KILL foobar" to clean out dups. But,
406 * this is not fair. It should actually request another
407 * nick from local user or kill him/her...
409 int register_user(struct Client *cptr, struct Client *sptr,
410 const char *nick, char *username, struct Gline *agline)
412 struct ConfItem* aconf;
416 char c = 0; /* not alphanum */
417 char d = 'a'; /* not a digit */
425 short digitgroups = 0;
426 struct User* user = sptr->user;
428 char featurebuf[512];
430 user->last = CurrentTime;
431 parv[0] = sptr->name;
432 parv[1] = parv[2] = NULL;
436 static time_t last_too_many1;
437 static time_t last_too_many2;
439 assert(cptr == sptr);
440 switch (conf_check_client(sptr))
444 case ACR_NO_AUTHORIZATION:
445 sendto_opmask_butone(0, SNO_UNAUTH, "Unauthorized connection from %s.",
446 get_client_name(sptr, HIDE_IP));
447 ++ServerStats->is_ref;
448 return exit_client(cptr, sptr, &me,
449 "No Authorization - use another server");
450 case ACR_TOO_MANY_IN_CLASS:
451 if (CurrentTime - last_too_many1 >= (time_t) 60)
453 last_too_many1 = CurrentTime;
454 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections in "
456 get_client_name(sptr, HIDE_IP));
458 ++ServerStats->is_ref;
459 return exit_client(cptr, sptr, &me,
460 "Sorry, your connection class is full - try "
461 "again later or try another server");
462 case ACR_TOO_MANY_FROM_IP:
463 if (CurrentTime - last_too_many2 >= (time_t) 60)
465 last_too_many2 = CurrentTime;
466 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections from "
468 get_client_name(sptr, HIDE_IP));
470 ++ServerStats->is_ref;
471 return exit_client(cptr, sptr, &me,
472 "Too many connections from your host");
473 case ACR_ALREADY_AUTHORIZED:
474 /* Can this ever happen? */
476 ++ServerStats->is_ref;
477 return exit_client(cptr, sptr, &me, "Unknown error -- Try again");
479 ircd_strncpy(user->host, sptr->sockhost, HOSTLEN);
480 aconf = sptr->confs->value.aconf;
482 clean_user_id(user->username,
483 (sptr->flags & FLAGS_GOTID) ? sptr->username : username,
484 (sptr->flags & FLAGS_DOID) && !(sptr->flags & FLAGS_GOTID));
486 if ((user->username[0] == '\0')
487 || ((user->username[0] == '~') && (user->username[1] == '\000')))
488 return exit_client(cptr, sptr, &me, "USER: Bogus userid.");
490 if (!EmptyString(aconf->passwd)
491 && !(IsDigit(*aconf->passwd) && !aconf->passwd[1])
492 && strcmp(sptr->passwd, aconf->passwd))
494 ServerStats->is_ref++;
495 send_reply(sptr, ERR_PASSWDMISMATCH);
496 return exit_client(cptr, sptr, &me, "Bad Password");
498 memset(sptr->passwd, 0, sizeof(sptr->passwd));
500 * following block for the benefit of time-dependent K:-lines
502 if (find_kill(sptr)) {
503 ServerStats->is_ref++;
504 return exit_client(cptr, sptr, &me, "K-lined");
507 * Check for mixed case usernames, meaning probably hacked. Jon2 3-94
508 * Summary of rules now implemented in this patch: Ensor 11-94
509 * In a mixed-case name, if first char is upper, one more upper may
510 * appear anywhere. (A mixed-case name *must* have an upper first
511 * char, and may have one other upper.)
512 * A third upper may appear if all 3 appear at the beginning of the
513 * name, separated only by "others" (-/_/.).
514 * A single group of digits is allowed anywhere.
515 * Two groups of digits are allowed if at least one of the groups is
516 * at the beginning or the end.
517 * Only one '-', '_', or '.' is allowed (or two, if not consecutive).
518 * But not as the first or last char.
519 * No other special characters are allowed.
520 * Name must contain at least one letter.
522 tmpstr2 = tmpstr = (username[0] == '~' ? &username[1] : username);
523 while (*tmpstr && !badid)
535 if ((leadcaps || pos == 1) && !lower && !digits)
541 if (pos == 1 || !IsDigit(d))
548 else if (c == '-' || c == '_' || c == '.')
553 else if (d == '-' || d == '_' || d == '.' || other > 2)
562 if (lower && upper && (!leadcaps || leadcaps > 3 ||
563 (upper > 2 && upper > leadcaps)))
565 else if (digitgroups == 2 && !(IsDigit(tmpstr2[0]) || IsDigit(c)))
567 else if ((!lower && !upper) || !IsAlnum(c))
570 if (badid && (!(sptr->flags & FLAGS_GOTID) ||
571 strcmp(sptr->username, username) != 0))
573 ServerStats->is_ref++;
575 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
576 ":Your username is invalid.");
577 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
578 ":Connect with your real username, in lowercase.");
579 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
580 ":If your mail address were foo@bar.com, your username "
582 return exit_client(cptr, sptr, &me, "USER: Bad username");
584 Count_unknownbecomesclient(sptr, UserStats);
587 ircd_strncpy(user->username, username, USERLEN);
588 Count_newremoteclient(UserStats, user->server);
592 /* a gline wasn't passed in, so find a matching global one that isn't
593 * a Uworld-set one, and propagate it if there is such an animal.
596 (agline = gline_lookup(sptr, GLINE_GLOBAL | GLINE_LASTMOD)) &&
597 !IsBurstOrBurstAck(cptr))
598 gline_resend(cptr, agline);
600 if (IsInvisible(sptr))
601 ++UserStats.inv_clients;
605 if (MyConnect(sptr)) {
606 sptr->handler = CLIENT_HANDLER;
607 release_dns_reply(sptr);
609 send_reply(sptr, RPL_WELCOME, nick);
611 * This is a duplicate of the NOTICE but see below...
613 send_reply(sptr, RPL_YOURHOST, me.name, version);
614 send_reply(sptr, RPL_CREATED, creation);
615 send_reply(sptr, RPL_MYINFO, me.name, version);
616 sprintf_irc(featurebuf,FEATURES,FEATURESVALUES);
617 send_reply(sptr, RPL_ISUPPORT, featurebuf);
618 m_lusers(sptr, sptr, 1, parv);
621 m_motd(sptr, NULL, 1, parv);
623 m_motd(sptr, sptr, 1, parv);
625 nextping = CurrentTime;
626 if (sptr->snomask & SNO_NOISY)
627 set_snomask(sptr, sptr->snomask & SNO_NOISY, SNO_ADD);
628 ip_registry_connect_succeeded(sptr);
631 /* if (IsServer(cptr)) */
633 struct Client *acptr;
635 acptr = user->server;
636 if (acptr->from != sptr->from)
638 sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])",
639 sptr, me.name, user->server->name, acptr->from->name,
640 acptr->from->sockhost);
641 sptr->flags |= FLAGS_KILLED;
642 return exit_client(cptr, sptr, &me, "NICK server wrong direction");
645 sptr->flags |= (acptr->flags & FLAGS_TS8);
648 * Check to see if this user is being propogated
649 * as part of a net.burst, or is using protocol 9.
650 * FIXME: This can be speeded up - its stupid to check it for
651 * every NICK message in a burst again --Run.
653 for (acptr = user->server; acptr != &me; acptr = acptr->serv->up) {
654 if (IsBurst(acptr) || Protocol(acptr) < 10)
658 tmpstr = umode_str(sptr);
660 sendcmdto_serv_butone(user->server, CMD_NICK, cptr,
661 "%s %d %Tu %s %s %s%s%s%%%Tu:%s@%s %s %s%s :%s",
662 nick, sptr->hopcount + 1, sptr->lastnick,
663 user->username, user->host,
664 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
665 GlineLastMod(agline), GlineUser(agline),
667 inttobase64(ip_base64, ntohl(sptr->ip.s_addr), 6),
668 NumNick(sptr), sptr->info);
670 sendcmdto_serv_butone(user->server, CMD_NICK, cptr,
671 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
672 nick, sptr->hopcount + 1, sptr->lastnick,
673 user->username, user->host,
674 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
675 inttobase64(ip_base64, ntohl(sptr->ip.s_addr), 6),
676 NumNick(sptr), sptr->info);
678 /* Send umode to client */
681 send_umode(cptr, sptr, 0, ALL_UMODES);
682 if (sptr->snomask != SNO_DEFAULT && (sptr->flags & FLAGS_SERVNOTICE))
683 send_reply(sptr, RPL_SNOMASK, sptr->snomask, sptr->snomask);
690 static const struct UserMode {
695 { FLAGS_LOCOP, 'O' },
696 { FLAGS_INVISIBLE, 'i' },
697 { FLAGS_WALLOP, 'w' },
698 { FLAGS_SERVNOTICE, 's' },
700 { FLAGS_CHSERV, 'k' },
704 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
707 static int user_modes[] = {
710 FLAGS_INVISIBLE, 'i',
712 FLAGS_SERVNOTICE, 's',
721 * XXX - find a way to get rid of this
723 static char umodeBuf[BUFSIZE];
725 int set_nick_name(struct Client* cptr, struct Client* sptr,
726 const char* nick, int parc, char* parv[])
728 if (IsServer(sptr)) {
732 struct Gline *agline = 0;
735 * A server introducing a new client, change source
737 struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
738 assert(0 != new_client);
740 new_client->hopcount = atoi(parv[2]);
741 new_client->lastnick = atoi(parv[3]);
742 if (Protocol(cptr) > 9 && parc > 7 && *parv[6] == '+') {
743 for (p = parv[6] + 1; *p; p++) {
744 for (i = 0; i < USERMODELIST_SIZE; ++i) {
745 if (userModeList[i].c == *p) {
746 new_client->flags |= userModeList[i].flag;
755 strcpy(new_client->name, nick);
756 new_client->user = make_user(new_client);
757 new_client->user->server = sptr;
758 SetRemoteNumNick(new_client, parv[parc - 2]);
760 * IP# of remote client
762 new_client->ip.s_addr = htonl(base64toint(parv[parc - 3]));
764 add_client_to_list(new_client);
765 hAddClient(new_client);
767 sptr->serv->ghost = 0; /* :server NICK means end of net.burst */
768 ircd_strncpy(new_client->username, parv[4], USERLEN);
769 ircd_strncpy(new_client->user->host, parv[5], HOSTLEN);
770 ircd_strncpy(new_client->info, parv[parc - 1], REALLEN);
772 /* Deal with GLINE parameters... */
773 if (*parv[parc - 4] == '%' && (t = strchr(parv[parc - 4] + 1, ':'))) {
777 lastmod = atoi(parv[parc - 4] + 1);
780 (agline = gline_find(t, GLINE_EXACT | GLINE_GLOBAL | GLINE_LASTMOD))
781 && GlineLastMod(agline) > lastmod && !IsBurstOrBurstAck(cptr))
782 gline_resend(cptr, agline);
784 if (!ip_registry_remote_connect(sptr)) {
785 sendcmdto_one(&me, CMD_KILL, new_client, "%C :%s (Too many connections from your host -- Ghost)",
787 return exit_client(cptr,new_client,&me,"Too many connections from your host -- throttled");
789 sendto_ops("Registering new remote client");
790 return register_user(cptr, new_client, new_client->name, parv[4], agline);
792 else if (sptr->name[0]) {
794 * Client changing its nick
796 * If the client belongs to me, then check to see
797 * if client is on any channels where it is currently
798 * banned. If so, do not allow the nick change to occur.
801 const char* channel_name;
802 if ((channel_name = find_no_nickchange_channel(sptr))) {
803 return send_reply(cptr, ERR_BANNICKCHANGE, channel_name);
806 * Refuse nick change if the last nick change was less
807 * then 30 seconds ago. This is intended to get rid of
808 * clone bots doing NICK FLOOD. -SeKs
809 * If someone didn't change their nick for more then 60 seconds
810 * however, allow to do two nick changes immedately after another
811 * before limiting the nick flood. -Run
813 if (CurrentTime < cptr->nextnick) {
815 send_reply(cptr, ERR_NICKTOOFAST, parv[1],
816 cptr->nextnick - CurrentTime);
817 /* Send error message */
818 sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cptr->name);
819 /* bounce NICK to user */
820 return 0; /* ignore nick change! */
823 /* Limit total to 1 change per NICK_DELAY seconds: */
824 cptr->nextnick += NICK_DELAY;
825 /* However allow _maximal_ 1 extra consecutive nick change: */
826 if (cptr->nextnick < CurrentTime)
827 cptr->nextnick = CurrentTime;
831 * Also set 'lastnick' to current time, if changed.
833 if (0 != ircd_strcmp(parv[0], nick))
834 sptr->lastnick = (sptr == cptr) ? TStime() : atoi(parv[2]);
837 * Client just changing his/her nick. If he/she is
838 * on a channel, send note of change to all clients
839 * on that channel. Propagate notice to other servers.
842 sendcmdto_common_channels(sptr, CMD_NICK, ":%s", nick);
843 add_history(sptr, 1);
844 sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick,
848 sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick);
852 strcpy(sptr->name, nick);
856 /* Local client setting NICK the first time */
858 strcpy(sptr->name, nick);
860 sptr->user = make_user(sptr);
861 sptr->user->server = &me;
863 SetLocalNumNick(sptr);
867 * If the client hasn't gotten a cookie-ping yet,
868 * choose a cookie and send it. -record!jegelhof@cloud9.net
872 sptr->cookie = (ircrandom() & 0x7fffffff);
873 } while (!sptr->cookie);
874 sendrawto_one(cptr, MSG_PING " :%u", sptr->cookie);
876 else if (*sptr->user->host && sptr->cookie == COOKIE_VERIFIED) {
878 * USER and PONG already received, now we have NICK.
879 * register_user may reject the client and call exit_client
880 * for it - must test this and exit m_nick too !
882 sptr->lastnick = TStime(); /* Always local client */
883 if (register_user(cptr, sptr, nick, sptr->user->username, 0) == CPTR_KILLED)
890 static unsigned char hash_target(unsigned int target)
892 return (unsigned char) (target >> 16) ^ (target >> 8);
898 * sptr must be a local client!
900 * Cannonifies target for client `sptr'.
902 void add_target(struct Client *sptr, void *target)
904 unsigned char hash = hash_target((unsigned int) target);
905 unsigned char* targets;
910 targets = sptr->targets;
914 for (i = 0; i < MAXTARGETS; ++i) {
915 if (targets[i] == hash)
921 memmove(&targets[RESERVEDTARGETS + 1],
922 &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
923 targets[RESERVEDTARGETS] = hash;
929 * sptr must be a local client !
931 * Returns 'true' (1) when too many targets are addressed.
932 * Returns 'false' (0) when it's ok to send to this target.
934 int check_target_limit(struct Client *sptr, void *target, const char *name,
937 unsigned char hash = hash_target((unsigned int) target);
939 unsigned char* targets;
943 targets = sptr->targets;
946 * Same target as last time?
948 if (targets[0] == hash)
950 for (i = 1; i < MAXTARGETS; ++i) {
951 if (targets[i] == hash) {
952 memmove(&targets[1], &targets[0], i);
961 if (CurrentTime < sptr->nexttarget) {
962 if (sptr->nexttarget - CurrentTime < TARGET_DELAY + 8) {
966 sptr->nexttarget += 2;
967 send_reply(sptr, ERR_TARGETTOOFAST, name,
968 sptr->nexttarget - CurrentTime);
974 /* XXX Let's get rid of GODMODE */
975 sendto_one(sptr, ":%s NOTICE %s :New target: %s; ft " TIME_T_FMT, /* XXX Possibly DEAD */
976 me.name, sptr->name, name, (CurrentTime - sptr->nexttarget) / TARGET_DELAY);
978 sptr->nexttarget += TARGET_DELAY;
979 if (sptr->nexttarget < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)))
980 sptr->nexttarget = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1));
983 memmove(&targets[1], &targets[0], MAXTARGETS - 1);
989 * whisper - called from m_cnotice and m_cprivmsg.
991 * parv[0] = sender prefix
994 * parv[3] = Private message text
996 * Added 971023 by Run.
997 * Reason: Allows channel operators to sent an arbitrary number of private
998 * messages to users on their channel, avoiding the max.targets limit.
999 * Building this into m_private would use too much cpu because we'd have
1000 * to a cross channel lookup for every private message!
1001 * Note that we can't allow non-chan ops to use this command, it would be
1002 * abused by mass advertisers.
1005 int whisper(struct Client* source, const char* nick, const char* channel,
1006 const char* text, int is_notice)
1008 struct Client* dest;
1009 struct Channel* chptr;
1010 struct Membership* membership;
1012 assert(0 != source);
1014 assert(0 != channel);
1015 assert(MyUser(source));
1017 if (!(dest = FindUser(nick))) {
1018 return send_reply(source, ERR_NOSUCHNICK, nick);
1020 if (!(chptr = FindChannel(channel))) {
1021 return send_reply(source, ERR_NOSUCHCHANNEL, channel);
1024 * compare both users channel lists, instead of the channels user list
1025 * since the link is the same, this should be a little faster for channels
1026 * with a lot of users
1028 for (membership = source->user->channel; membership; membership = membership->next_channel) {
1029 if (chptr == membership->channel)
1032 if (0 == membership) {
1033 return send_reply(source, ERR_NOTONCHANNEL, chptr->chname);
1035 if (!IsVoicedOrOpped(membership)) {
1036 return send_reply(source, ERR_VOICENEEDED, chptr->chname);
1039 * lookup channel in destination
1041 assert(0 != dest->user);
1042 for (membership = dest->user->channel; membership; membership = membership->next_channel) {
1043 if (chptr == membership->channel)
1046 if (0 == membership || IsZombie(membership)) {
1047 return send_reply(source, ERR_USERNOTINCHANNEL, dest->name, chptr->chname);
1049 if (is_silenced(source, dest))
1052 if (dest->user->away)
1053 send_reply(source, RPL_AWAY, dest->name, dest->user->away);
1055 sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text);
1057 sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text);
1063 * added Sat Jul 25 07:30:42 EST 1992
1065 void send_umode_out(struct Client *cptr, struct Client *sptr, int old)
1068 struct Client *acptr;
1070 send_umode(NULL, sptr, old, SEND_UMODES);
1072 for (i = HighestFd; i >= 0; i--) {
1073 if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
1074 (acptr != cptr) && (acptr != sptr) && *umodeBuf)
1075 sendcmdto_one(sptr, CMD_MODE, acptr, "%s :%s", sptr->name, umodeBuf);
1077 if (cptr && MyUser(cptr))
1078 send_umode(cptr, sptr, old, ALL_UMODES);
1083 * send_user_info - send user info userip/userhost
1084 * NOTE: formatter must put info into buffer and return a pointer to the end of
1085 * the data it put in the buffer.
1087 void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
1093 int users_found = 0;
1094 struct Client* acptr;
1095 char buf[BUFSIZE * 2];
1101 sbuf = sprintf_irc(buf, rpl_str(rpl), me.name, sptr->name);
1103 for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
1104 if ((acptr = FindUser(name))) {
1107 sbuf = (*fmt)(acptr, sbuf);
1109 if (5 == ++arg_count)
1113 send_buffer(sptr, buf);
1118 * set_user_mode() added 15/10/91 By Darren Reed.
1121 * parv[1] - username to change mode for
1122 * parv[2] - modes to change
1124 int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
1128 struct Client *acptr;
1132 unsigned int tmpmask = 0;
1133 int snomask_given = 0;
1139 return need_more_params(sptr, "MODE");
1141 if (!(acptr = FindUser(parv[1])))
1143 if (MyConnect(sptr))
1144 send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]);
1148 if (IsServer(sptr) || sptr != acptr)
1151 sendcmdto_flag_butone(&me, CMD_WALLOPS, 0, FLAGS_WALLOP,
1152 ":MODE for User %s from %s!%s", parv[1],
1153 cptr->name, sptr->name);
1155 send_reply(sptr, ERR_USERSDONTMATCH);
1163 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1164 if ( (userModeList[i].flag & sptr->flags))
1165 *m++ = userModeList[i].c;
1168 send_reply(sptr, RPL_UMODEIS, buf);
1169 if ((sptr->flags & FLAGS_SERVNOTICE) && MyConnect(sptr)
1171 (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT))
1172 send_reply(sptr, RPL_SNOMASK, sptr->snomask, sptr->snomask);
1177 * find flags already set for user
1178 * why not just copy them?
1180 setflags = sptr->flags;
1183 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1184 if (sptr->flags & userModeList[i].flag)
1185 setflags |= userModeList[i].flag;
1188 if (MyConnect(sptr))
1189 tmpmask = sptr->snomask;
1192 * parse mode change string(s)
1194 for (p = &parv[2]; *p; p++) { /* p is changed in loop too */
1195 for (m = *p; *m; m++) {
1204 if (*(p + 1) && is_snomask(*(p + 1))) {
1206 tmpmask = umode_make_snomask(tmpmask, *++p, what);
1207 tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1210 tmpmask = (what == MODE_ADD) ?
1211 (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1213 sptr->flags |= FLAGS_SERVNOTICE;
1215 sptr->flags &= ~FLAGS_SERVNOTICE;
1218 if (what == MODE_ADD)
1224 if (what == MODE_ADD)
1227 sptr->flags &= ~(FLAGS_OPER | FLAGS_LOCOP);
1228 if (MyConnect(sptr)) {
1229 tmpmask = sptr->snomask & ~SNO_OPER;
1230 sptr->handler = CLIENT_HANDLER;
1235 if (what == MODE_ADD)
1238 sptr->flags &= ~(FLAGS_OPER | FLAGS_LOCOP);
1239 if (MyConnect(sptr)) {
1240 tmpmask = sptr->snomask & ~SNO_OPER;
1241 sptr->handler = CLIENT_HANDLER;
1246 if (what == MODE_ADD)
1249 ClearInvisible(sptr);
1252 if (what == MODE_ADD)
1258 if (what == MODE_ADD)
1259 SetChannelService(sptr);
1261 ClearChannelService(sptr);
1264 if (what == MODE_ADD)
1275 * Evaluate rules for new user mode
1276 * Stop users making themselves operators too easily:
1278 if (!(setflags & FLAGS_OPER) && IsOper(sptr) && !IsServer(cptr))
1280 if (!(setflags & FLAGS_LOCOP) && IsLocOp(sptr) && !IsServer(cptr))
1282 #ifdef WALLOPS_OPER_ONLY
1284 * only send wallops to opers
1286 if (!IsAnOper(sptr) && !(setflags & FLAGS_WALLOP) && !IsServer(cptr))
1289 if ((setflags & (FLAGS_OPER | FLAGS_LOCOP)) && !IsAnOper(sptr) &&
1291 det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPS);
1293 * new umode; servers can set it, local users cannot;
1294 * prevents users from /kick'ing or /mode -o'ing
1296 if (!(setflags & FLAGS_CHSERV) && !IsServer(cptr))
1297 ClearChannelService(sptr);
1299 * Compare new flags with old flags and send string which
1300 * will cause servers to update correctly.
1302 if ((setflags & FLAGS_OPER) && !IsOper(sptr))
1304 if (!(setflags & FLAGS_OPER) && IsOper(sptr))
1306 if ((setflags & FLAGS_INVISIBLE) && !IsInvisible(sptr))
1307 --UserStats.inv_clients;
1308 if (!(setflags & FLAGS_INVISIBLE) && IsInvisible(sptr))
1309 ++UserStats.inv_clients;
1310 send_umode_out(cptr, sptr, setflags);
1312 if (MyConnect(sptr)) {
1313 if (tmpmask != sptr->snomask)
1314 set_snomask(sptr, tmpmask, SNO_SET);
1315 if (sptr->snomask && snomask_given)
1316 send_reply(sptr, RPL_SNOMASK, sptr->snomask, sptr->snomask);
1323 * Build umode string for BURST command
1326 char *umode_str(struct Client *cptr)
1328 char* m = umodeBuf; /* Maximum string size: "owidg\0" */
1332 c_flags = cptr->flags & SEND_UMODES; /* cleaning up the original code */
1334 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1335 if ( (c_flags & userModeList[i].flag))
1336 *m++ = userModeList[i].c;
1340 return umodeBuf; /* Note: static buffer, gets
1341 overwritten by send_umode() */
1345 * Send the MODE string for user (user) to connection cptr
1348 void send_umode(struct Client *cptr, struct Client *sptr, int old, int sendmask)
1353 int what = MODE_NULL;
1356 * Build a string in umodeBuf to represent the change in the user's
1357 * mode between the new (sptr->flag) and 'old'.
1361 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1362 flag = userModeList[i].flag;
1363 if (MyUser(sptr) && !(flag & sendmask))
1365 if ( (flag & old) && !(sptr->flags & flag))
1367 if (what == MODE_DEL)
1368 *m++ = userModeList[i].c;
1373 *m++ = userModeList[i].c;
1376 else if (!(flag & old) && (sptr->flags & flag))
1378 if (what == MODE_ADD)
1379 *m++ = userModeList[i].c;
1384 *m++ = userModeList[i].c;
1389 if (*umodeBuf && cptr)
1390 sendcmdto_one(sptr, CMD_MODE, cptr, "%s :%s", sptr->name, umodeBuf);
1394 * Check to see if this resembles a sno_mask. It is if 1) there is
1395 * at least one digit and 2) The first digit occurs before the first
1396 * alphabetic character.
1398 int is_snomask(char *word)
1402 for (; *word; word++)
1405 else if (IsAlpha(*word))
1412 * If it begins with a +, count this as an additive mask instead of just
1413 * a replacement. If what == MODE_DEL, "+" has no special effect.
1415 unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
1417 unsigned int sno_what;
1418 unsigned int newmask;
1422 if (what == MODE_ADD)
1427 else if (*arg == '-')
1430 if (what == MODE_ADD)
1436 sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL;
1437 /* pity we don't have strtoul everywhere */
1438 newmask = (unsigned int)atoi(arg);
1439 if (sno_what == SNO_DEL)
1440 newmask = oldmask & ~newmask;
1441 else if (sno_what == SNO_ADD)
1446 static void delfrom_list(struct Client *cptr, struct SLink **list)
1449 struct SLink* prv = NULL;
1451 for (tmp = *list; tmp; tmp = tmp->next) {
1452 if (tmp->value.cptr == cptr) {
1454 prv->next = tmp->next;
1465 * This function sets a Client's server notices mask, according to
1466 * the parameter 'what'. This could be even faster, but the code
1467 * gets mighty hard to read :)
1469 void set_snomask(struct Client *cptr, unsigned int newmask, int what)
1471 unsigned int oldmask, diffmask; /* unsigned please */
1475 oldmask = cptr->snomask;
1477 if (what == SNO_ADD)
1479 else if (what == SNO_DEL)
1480 newmask = oldmask & ~newmask;
1481 else if (what != SNO_SET) /* absolute set, no math needed */
1482 sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what);
1484 newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
1486 diffmask = oldmask ^ newmask;
1488 for (i = 0; diffmask >> i; i++) {
1489 if (((diffmask >> i) & 1))
1491 if (((newmask >> i) & 1))
1494 tmp->next = opsarray[i];
1495 tmp->value.cptr = cptr;
1499 /* not real portable :( */
1500 delfrom_list(cptr, &opsarray[i]);
1503 cptr->snomask = newmask;
1507 * is_silenced : Does the actual check wether sptr is allowed
1508 * to send a message to acptr.
1509 * Both must be registered persons.
1510 * If sptr is silenced by acptr, his message should not be propagated,
1511 * but more over, if this is detected on a server not local to sptr
1512 * the SILENCE mask is sent upstream.
1514 int is_silenced(struct Client *sptr, struct Client *acptr)
1518 static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
1519 static char senderip[16 + NICKLEN + USERLEN + 5];
1521 if (!(acptr->user) || !(lp = acptr->user->silence) || !(user = sptr->user))
1523 sprintf_irc(sender, "%s!%s@%s", sptr->name, user->username, user->host);
1524 sprintf_irc(senderip, "%s!%s@%s", sptr->name, user->username,
1525 ircd_ntoa((const char*) &sptr->ip));
1526 for (; lp; lp = lp->next)
1528 if ((!(lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, sender)) ||
1529 ((lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, senderip)))
1531 if (!MyConnect(sptr))
1533 sendcmdto_one(acptr, CMD_SILENCE, sptr->from, "%C %s", sptr,
1545 * Removes all silence masks from the list of sptr that fall within `mask'
1546 * Returns -1 if none where found, 0 otherwise.
1548 int del_silence(struct Client *sptr, char *mask)
1554 for (lp = &sptr->user->silence; *lp;) {
1555 if (!mmatch(mask, (*lp)->value.cp))
1559 MyFree(tmp->value.cp);
1569 int add_silence(struct Client* sptr, const char* mask)
1571 struct SLink *lp, **lpp;
1572 int cnt = 0, len = strlen(mask);
1575 for (lpp = &sptr->user->silence, lp = *lpp; lp;)
1577 if (0 == ircd_strcmp(mask, lp->value.cp))
1579 if (!mmatch(mask, lp->value.cp))
1581 struct SLink *tmp = lp;
1582 *lpp = lp = lp->next;
1583 MyFree(tmp->value.cp);
1589 len += strlen(lp->value.cp);
1590 if ((len > MAXSILELENGTH) || (++cnt >= MAXSILES))
1592 send_reply(sptr, ERR_SILELISTFULL, mask);
1595 else if (!mmatch(lp->value.cp, mask))
1602 memset(lp, 0, sizeof(struct SLink));
1603 lp->next = sptr->user->silence;
1604 lp->value.cp = (char*) MyMalloc(strlen(mask) + 1);
1605 assert(0 != lp->value.cp);
1606 strcpy(lp->value.cp, mask);
1607 if ((ip_start = strrchr(mask, '@')) && check_if_ipmask(ip_start + 1))
1608 lp->flags = CHFL_SILENCE_IPMASK;
1609 sptr->user->silence = lp;