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"
35 #include "ircd_features.h"
37 #include "ircd_reply.h"
38 #include "ircd_string.h"
47 #include "querycmds.h"
53 #include "s_serv.h" /* max_client_count */
55 #include "sprintf_irc.h"
58 #include "supported.h"
64 #include "handlers.h" /* m_motd and m_lusers */
74 static int userCount = 0;
77 * 'make_user' add's an User information block to a client
78 * if it was not previously allocated.
80 struct User *make_user(struct Client *cptr)
84 if (!cli_user(cptr)) {
85 cli_user(cptr) = (struct User*) MyMalloc(sizeof(struct User));
86 assert(0 != cli_user(cptr));
88 /* All variables are 0 by default */
89 memset(cli_user(cptr), 0, sizeof(struct User));
93 cli_user(cptr)->refcnt = 1;
95 return cli_user(cptr);
101 * Decrease user reference count by one and release block, if count reaches 0.
103 void free_user(struct User* user)
106 assert(0 < user->refcnt);
108 if (--user->refcnt == 0) {
114 assert(0 == user->joined);
115 assert(0 == user->invited);
116 assert(0 == user->channel);
125 void user_count_memory(size_t* count_out, size_t* bytes_out)
127 assert(0 != count_out);
128 assert(0 != bytes_out);
129 *count_out = userCount;
130 *bytes_out = userCount * sizeof(struct User);
137 * Local function to find the next matching client. The search
138 * can be continued from the specified client entry. Normal
141 * for (x = client; x = next_client(x,mask); x = x->next)
142 * HandleMatchingClient;
145 struct Client *next_client(struct Client *next, const char* ch)
147 struct Client *tmp = next;
152 next = FindClient(ch);
153 next = next ? next : tmp;
154 if (cli_prev(tmp) == next)
158 for (; next; next = cli_next(next))
159 if (!match(ch, cli_name(next)))
167 * Do the basic thing in delivering the message (command)
168 * across the relays to the specific server (server) for
171 * Note: The command is a format string and *MUST* be
172 * of prefixed style (e.g. ":%s COMMAND %s ...").
173 * Command can have only max 8 parameters.
175 * server parv[server] is the parameter identifying the
179 * parv[server] is replaced with the pointer to the
180 * real servername from the matched client (I'm lazy
183 * returns: (see #defines)
185 int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok,
186 struct Client *one, int MustBeOper, const char *pattern,
187 int server, int parc, char *parv[])
189 struct Client *acptr;
192 /* Assume it's me, if no server or an unregistered client */
193 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
194 return (HUNTED_ISME);
196 /* Make sure it's a server */
198 /* Make sure it's a server */
199 if (!strchr(to, '*')) {
200 if (0 == (acptr = FindClient(to)))
201 return HUNTED_NOSUCH;
204 acptr = cli_user(acptr)->server;
205 } else if (!(acptr = find_match_server(to))) {
206 send_reply(from, ERR_NOSUCHSERVER, to);
207 return (HUNTED_NOSUCH);
209 } else if (!(acptr = FindNServer(to)))
210 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
213 return (HUNTED_ISME);
215 if (MustBeOper && !IsPrivileged(from)) {
216 send_reply(from, ERR_NOPRIVILEGES);
217 return HUNTED_NOSUCH;
220 assert(!IsServer(from));
222 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
224 sendcmdto_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
225 parv[4], parv[5], parv[6], parv[7], parv[8]);
227 return (HUNTED_PASS);
231 * 'do_nick_name' ensures that the given parameter (nick) is really a proper
232 * string for a nickname (note, the 'nick' may be modified in the process...)
234 * RETURNS the length of the final NICKNAME (0, if nickname is invalid)
236 * Nickname characters are in range 'A'..'}', '_', '-', '0'..'9'
237 * anything outside the above set will terminate nickname.
238 * In addition, the first character cannot be '-' or a Digit.
241 * The '~'-character should be allowed, but a change should be global,
242 * some confusion would result if only few servers allowed it...
244 int do_nick_name(char* nick)
247 char* end = ch + NICKLEN;
250 if (*ch == '-' || IsDigit(*ch)) /* first character in [0..9-] */
253 for ( ; (ch < end) && *ch; ++ch)
254 if (!IsNickChar(*ch))
265 * Copy `source' to `dest', replacing all occurances of '~' and characters that
266 * are not `isIrcUi' by an underscore.
267 * Copies at most USERLEN - 1 characters or up till the first control character.
268 * If `tilde' is true, then a tilde is prepended to `dest'.
269 * Note that `dest' and `source' can point to the same area or to different
270 * non-overlapping areas.
272 static char *clean_user_id(char *dest, char *source, int tilde)
279 ch = *s++; /* Store first character to copy: */
282 *d++ = '~'; /* If `dest' == `source', then this overwrites `ch' */
285 while (ch && !IsCntrl(ch) && rlen--)
287 char nch = *s++; /* Store next character to copy */
288 *d++ = IsUserChar(ch) ? ch : '_'; /* This possibly overwrites it */
301 * This function is called when both NICK and USER messages
302 * have been accepted for the client, in whatever order. Only
303 * after this the USER message is propagated.
305 * NICK's must be propagated at once when received, although
306 * it would be better to delay them too until full info is
307 * available. Doing it is not so simple though, would have
308 * to implement the following:
310 * 1) user telnets in and gives only "NICK foobar" and waits
311 * 2) another user far away logs in normally with the nick
312 * "foobar" (quite legal, as this server didn't propagate it).
313 * 3) now this server gets nick "foobar" from outside, but
314 * has already the same defined locally. Current server
315 * would just issue "KILL foobar" to clean out dups. But,
316 * this is not fair. It should actually request another
317 * nick from local user or kill him/her...
319 int register_user(struct Client *cptr, struct Client *sptr,
320 const char *nick, char *username, struct Gline *agline)
322 struct ConfItem* aconf;
326 char c = 0; /* not alphanum */
327 char d = 'a'; /* not a digit */
335 short digitgroups = 0;
336 struct User* user = cli_user(sptr);
338 char featurebuf[512];
340 user->last = CurrentTime;
341 parv[0] = cli_name(sptr);
342 parv[1] = parv[2] = NULL;
346 static time_t last_too_many1;
347 static time_t last_too_many2;
349 assert(cptr == sptr);
350 switch (conf_check_client(sptr))
354 case ACR_NO_AUTHORIZATION:
355 sendto_opmask_butone(0, SNO_UNAUTH, "Unauthorized connection from %s.",
356 get_client_name(sptr, HIDE_IP));
357 ++ServerStats->is_ref;
358 return exit_client(cptr, sptr, &me,
359 "No Authorization - use another server");
360 case ACR_TOO_MANY_IN_CLASS:
361 if (CurrentTime - last_too_many1 >= (time_t) 60)
363 last_too_many1 = CurrentTime;
364 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections in "
366 get_client_name(sptr, HIDE_IP));
368 ++ServerStats->is_ref;
369 IPcheck_connect_fail(cli_ip(sptr));
370 return exit_client(cptr, sptr, &me,
371 "Sorry, your connection class is full - try "
372 "again later or try another server");
373 case ACR_TOO_MANY_FROM_IP:
374 if (CurrentTime - last_too_many2 >= (time_t) 60)
376 last_too_many2 = CurrentTime;
377 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections from "
379 get_client_name(sptr, HIDE_IP));
381 ++ServerStats->is_ref;
382 return exit_client(cptr, sptr, &me,
383 "Too many connections from your host");
384 case ACR_ALREADY_AUTHORIZED:
385 /* Can this ever happen? */
387 ++ServerStats->is_ref;
388 IPcheck_connect_fail(cli_ip(sptr));
389 return exit_client(cptr, sptr, &me, "Unknown error -- Try again");
391 ircd_strncpy(user->host, cli_sockhost(sptr), HOSTLEN);
392 aconf = cli_confs(sptr)->value.aconf;
394 clean_user_id(user->username,
395 (cli_flags(sptr) & FLAGS_GOTID) ? cli_username(sptr) : username,
396 (cli_flags(sptr) & FLAGS_DOID) && !(cli_flags(sptr) & FLAGS_GOTID));
398 if ((user->username[0] == '\0')
399 || ((user->username[0] == '~') && (user->username[1] == '\000')))
400 return exit_client(cptr, sptr, &me, "USER: Bogus userid.");
402 if (!EmptyString(aconf->passwd)
403 && !(IsDigit(*aconf->passwd) && !aconf->passwd[1])
404 && strcmp(cli_passwd(sptr), aconf->passwd))
406 ServerStats->is_ref++;
407 IPcheck_connect_fail(cli_ip(sptr));
408 send_reply(sptr, ERR_PASSWDMISMATCH);
409 return exit_client(cptr, sptr, &me, "Bad Password");
411 memset(cli_passwd(sptr), 0, sizeof(cli_passwd(sptr)));
413 * following block for the benefit of time-dependent K:-lines
415 if (find_kill(sptr)) {
416 ServerStats->is_ref++;
417 IPcheck_connect_fail(cli_ip(sptr));
418 return exit_client(cptr, sptr, &me, "K-lined");
421 * Check for mixed case usernames, meaning probably hacked. Jon2 3-94
422 * Summary of rules now implemented in this patch: Ensor 11-94
423 * In a mixed-case name, if first char is upper, one more upper may
424 * appear anywhere. (A mixed-case name *must* have an upper first
425 * char, and may have one other upper.)
426 * A third upper may appear if all 3 appear at the beginning of the
427 * name, separated only by "others" (-/_/.).
428 * A single group of digits is allowed anywhere.
429 * Two groups of digits are allowed if at least one of the groups is
430 * at the beginning or the end.
431 * Only one '-', '_', or '.' is allowed (or two, if not consecutive).
432 * But not as the first or last char.
433 * No other special characters are allowed.
434 * Name must contain at least one letter.
436 tmpstr2 = tmpstr = (username[0] == '~' ? &username[1] : username);
437 while (*tmpstr && !badid)
449 if ((leadcaps || pos == 1) && !lower && !digits)
455 if (pos == 1 || !IsDigit(d))
462 else if (c == '-' || c == '_' || c == '.')
467 else if (d == '-' || d == '_' || d == '.' || other > 2)
476 if (lower && upper && (!leadcaps || leadcaps > 3 ||
477 (upper > 2 && upper > leadcaps)))
479 else if (digitgroups == 2 && !(IsDigit(tmpstr2[0]) || IsDigit(c)))
481 else if ((!lower && !upper) || !IsAlnum(c))
484 if (badid && (!(cli_flags(sptr) & FLAGS_GOTID) ||
485 strcmp(cli_username(sptr), username) != 0))
487 ServerStats->is_ref++;
489 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
490 ":Your username is invalid.");
491 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
492 ":Connect with your real username, in lowercase.");
493 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
494 ":If your mail address were foo@bar.com, your username "
496 return exit_client(cptr, sptr, &me, "USER: Bad username");
498 Count_unknownbecomesclient(sptr, UserStats);
501 ircd_strncpy(user->username, username, USERLEN);
502 Count_newremoteclient(UserStats, user->server);
506 /* a gline wasn't passed in, so find a matching global one that isn't
507 * a Uworld-set one, and propagate it if there is such an animal.
510 (agline = gline_lookup(sptr, GLINE_GLOBAL | GLINE_LASTMOD)) &&
511 !IsBurstOrBurstAck(cptr))
512 gline_resend(cptr, agline);
514 if (IsInvisible(sptr))
515 ++UserStats.inv_clients;
519 if (MyConnect(sptr)) {
520 cli_handler(sptr) = CLIENT_HANDLER;
521 release_dns_reply(sptr);
523 send_reply(sptr, RPL_WELCOME, nick);
525 * This is a duplicate of the NOTICE but see below...
527 send_reply(sptr, RPL_YOURHOST, cli_name(&me), version);
528 send_reply(sptr, RPL_CREATED, creation);
529 send_reply(sptr, RPL_MYINFO, cli_name(&me), version);
530 sprintf_irc(featurebuf,FEATURES,FEATURESVALUES);
531 send_reply(sptr, RPL_ISUPPORT, featurebuf);
532 m_lusers(sptr, sptr, 1, parv);
535 nextping = CurrentTime;
536 if (cli_snomask(sptr) & SNO_NOISY)
537 set_snomask(sptr, cli_snomask(sptr) & SNO_NOISY, SNO_ADD);
538 IPcheck_connect_succeeded(sptr);
541 /* if (IsServer(cptr)) */
543 struct Client *acptr;
545 acptr = user->server;
546 if (cli_from(acptr) != cli_from(sptr))
548 sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])",
549 sptr, cli_name(&me), cli_name(user->server), cli_name(cli_from(acptr)),
550 cli_sockhost(cli_from(acptr)));
551 cli_flags(sptr) |= FLAGS_KILLED;
552 return exit_client(cptr, sptr, &me, "NICK server wrong direction");
555 cli_flags(sptr) |= (cli_flags(acptr) & FLAGS_TS8);
558 * Check to see if this user is being propogated
559 * as part of a net.burst, or is using protocol 9.
560 * FIXME: This can be speeded up - its stupid to check it for
561 * every NICK message in a burst again --Run.
563 for (acptr = user->server; acptr != &me; acptr = cli_serv(acptr)->up) {
564 if (IsBurst(acptr) || Protocol(acptr) < 10)
567 if (!IPcheck_remote_connect(sptr, (acptr != &me))) {
569 * We ran out of bits to count this
571 sendcmdto_one(&me, CMD_KILL, sptr, "%C :%s (Too many connections from your host -- Ghost)",
572 sptr, cli_name(&me));
573 return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled");
576 tmpstr = umode_str(sptr);
578 sendcmdto_serv_butone(user->server, CMD_NICK, cptr,
579 "%s %d %Tu %s %s %s%s%s%%%Tu:%s@%s %s %s%s :%s",
580 nick, cli_hopcount(sptr) + 1, cli_lastnick(sptr),
581 user->username, user->host,
582 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
583 GlineLastMod(agline), GlineUser(agline),
585 inttobase64(ip_base64, ntohl(cli_ip(sptr).s_addr), 6),
586 NumNick(sptr), cli_info(sptr));
588 sendcmdto_serv_butone(user->server, CMD_NICK, cptr,
589 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
590 nick, cli_hopcount(sptr) + 1, cli_lastnick(sptr),
591 user->username, user->host,
592 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
593 inttobase64(ip_base64, ntohl(cli_ip(sptr).s_addr), 6),
594 NumNick(sptr), cli_info(sptr));
596 /* Send umode to client */
599 send_umode(cptr, sptr, 0, ALL_UMODES);
600 if (cli_snomask(sptr) != SNO_DEFAULT && (cli_flags(sptr) & FLAGS_SERVNOTICE))
601 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
608 static const struct UserMode {
613 { FLAGS_LOCOP, 'O' },
614 { FLAGS_INVISIBLE, 'i' },
615 { FLAGS_WALLOP, 'w' },
616 { FLAGS_SERVNOTICE, 's' },
618 { FLAGS_CHSERV, 'k' },
622 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
625 * XXX - find a way to get rid of this
627 static char umodeBuf[BUFSIZE];
629 int set_nick_name(struct Client* cptr, struct Client* sptr,
630 const char* nick, int parc, char* parv[])
632 if (IsServer(sptr)) {
636 struct Gline *agline = 0;
639 * A server introducing a new client, change source
641 struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
642 assert(0 != new_client);
644 cli_hopcount(new_client) = atoi(parv[2]);
645 cli_lastnick(new_client) = atoi(parv[3]);
646 if (Protocol(cptr) > 9 && parc > 7 && *parv[6] == '+') {
647 for (p = parv[6] + 1; *p; p++) {
648 for (i = 0; i < USERMODELIST_SIZE; ++i) {
649 if (userModeList[i].c == *p) {
650 cli_flags(new_client) |= userModeList[i].flag;
656 client_set_privs(new_client); /* set privs on user */
660 strcpy(cli_name(new_client), nick);
661 cli_user(new_client) = make_user(new_client);
662 cli_user(new_client)->server = sptr;
663 SetRemoteNumNick(new_client, parv[parc - 2]);
665 * IP# of remote client
667 cli_ip(new_client).s_addr = htonl(base64toint(parv[parc - 3]));
669 add_client_to_list(new_client);
670 hAddClient(new_client);
672 cli_serv(sptr)->ghost = 0; /* :server NICK means end of net.burst */
673 ircd_strncpy(cli_username(new_client), parv[4], USERLEN);
674 ircd_strncpy(cli_user(new_client)->host, parv[5], HOSTLEN);
675 ircd_strncpy(cli_info(new_client), parv[parc - 1], REALLEN);
677 /* Deal with GLINE parameters... */
678 if (*parv[parc - 4] == '%' && (t = strchr(parv[parc - 4] + 1, ':'))) {
682 lastmod = atoi(parv[parc - 4] + 1);
685 (agline = gline_find(t, GLINE_EXACT | GLINE_GLOBAL | GLINE_LASTMOD))
686 && GlineLastMod(agline) > lastmod && !IsBurstOrBurstAck(cptr))
687 gline_resend(cptr, agline);
689 return register_user(cptr, new_client, cli_name(new_client), parv[4], agline);
691 else if ((cli_name(sptr))[0]) {
693 * Client changing its nick
695 * If the client belongs to me, then check to see
696 * if client is on any channels where it is currently
697 * banned. If so, do not allow the nick change to occur.
700 const char* channel_name;
701 if ((channel_name = find_no_nickchange_channel(sptr))) {
702 return send_reply(cptr, ERR_BANNICKCHANGE, channel_name);
705 * Refuse nick change if the last nick change was less
706 * then 30 seconds ago. This is intended to get rid of
707 * clone bots doing NICK FLOOD. -SeKs
708 * If someone didn't change their nick for more then 60 seconds
709 * however, allow to do two nick changes immedately after another
710 * before limiting the nick flood. -Run
712 if (CurrentTime < cli_nextnick(cptr)) {
713 cli_nextnick(cptr) += 2;
714 send_reply(cptr, ERR_NICKTOOFAST, parv[1],
715 cli_nextnick(cptr) - CurrentTime);
716 /* Send error message */
717 sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cli_name(cptr));
718 /* bounce NICK to user */
719 return 0; /* ignore nick change! */
722 /* Limit total to 1 change per NICK_DELAY seconds: */
723 cli_nextnick(cptr) += NICK_DELAY;
724 /* However allow _maximal_ 1 extra consecutive nick change: */
725 if (cli_nextnick(cptr) < CurrentTime)
726 cli_nextnick(cptr) = CurrentTime;
730 * Also set 'lastnick' to current time, if changed.
732 if (0 != ircd_strcmp(parv[0], nick))
733 cli_lastnick(sptr) = (sptr == cptr) ? TStime() : atoi(parv[2]);
736 * Client just changing his/her nick. If he/she is
737 * on a channel, send note of change to all clients
738 * on that channel. Propagate notice to other servers.
741 sendcmdto_common_channels(sptr, CMD_NICK, ":%s", nick);
742 add_history(sptr, 1);
743 sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick,
747 sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick);
749 if ((cli_name(sptr))[0])
751 strcpy(cli_name(sptr), nick);
755 /* Local client setting NICK the first time */
757 strcpy(cli_name(sptr), nick);
758 if (!cli_user(sptr)) {
759 cli_user(sptr) = make_user(sptr);
760 cli_user(sptr)->server = &me;
762 SetLocalNumNick(sptr);
766 * If the client hasn't gotten a cookie-ping yet,
767 * choose a cookie and send it. -record!jegelhof@cloud9.net
769 if (!cli_cookie(sptr)) {
771 cli_cookie(sptr) = (ircrandom() & 0x7fffffff);
772 } while (!cli_cookie(sptr));
773 sendrawto_one(cptr, MSG_PING " :%u", cli_cookie(sptr));
775 else if (*(cli_user(sptr))->host && cli_cookie(sptr) == COOKIE_VERIFIED) {
777 * USER and PONG already received, now we have NICK.
778 * register_user may reject the client and call exit_client
779 * for it - must test this and exit m_nick too !
781 cli_lastnick(sptr) = TStime(); /* Always local client */
782 if (register_user(cptr, sptr, nick, cli_user(sptr)->username, 0) == CPTR_KILLED)
789 static unsigned char hash_target(unsigned int target)
791 return (unsigned char) (target >> 16) ^ (target >> 8);
797 * sptr must be a local client!
799 * Cannonifies target for client `sptr'.
801 void add_target(struct Client *sptr, void *target)
803 /* Ok, this shouldn't work esp on alpha
805 unsigned char hash = hash_target((unsigned long) target);
806 unsigned char* targets;
809 assert(cli_local(sptr));
811 targets = cli_targets(sptr);
815 for (i = 0; i < MAXTARGETS; ++i) {
816 if (targets[i] == hash)
822 memmove(&targets[RESERVEDTARGETS + 1],
823 &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
824 targets[RESERVEDTARGETS] = hash;
830 * sptr must be a local client !
832 * Returns 'true' (1) when too many targets are addressed.
833 * Returns 'false' (0) when it's ok to send to this target.
835 int check_target_limit(struct Client *sptr, void *target, const char *name,
838 unsigned char hash = hash_target((unsigned long) target);
840 unsigned char* targets;
843 assert(cli_local(sptr));
844 targets = cli_targets(sptr);
847 * Same target as last time?
849 if (targets[0] == hash)
851 for (i = 1; i < MAXTARGETS; ++i) {
852 if (targets[i] == hash) {
853 memmove(&targets[1], &targets[0], i);
862 if (CurrentTime < cli_nexttarget(sptr)) {
863 if (cli_nexttarget(sptr) - CurrentTime < TARGET_DELAY + 8) {
867 cli_nexttarget(sptr) += 2;
868 send_reply(sptr, ERR_TARGETTOOFAST, name,
869 cli_nexttarget(sptr) - CurrentTime);
874 cli_nexttarget(sptr) += TARGET_DELAY;
875 if (cli_nexttarget(sptr) < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)))
876 cli_nexttarget(sptr) = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1));
879 memmove(&targets[1], &targets[0], MAXTARGETS - 1);
885 * whisper - called from m_cnotice and m_cprivmsg.
887 * parv[0] = sender prefix
890 * parv[3] = Private message text
892 * Added 971023 by Run.
893 * Reason: Allows channel operators to sent an arbitrary number of private
894 * messages to users on their channel, avoiding the max.targets limit.
895 * Building this into m_private would use too much cpu because we'd have
896 * to a cross channel lookup for every private message!
897 * Note that we can't allow non-chan ops to use this command, it would be
898 * abused by mass advertisers.
901 int whisper(struct Client* source, const char* nick, const char* channel,
902 const char* text, int is_notice)
905 struct Channel* chptr;
906 struct Membership* membership;
910 assert(0 != channel);
911 assert(MyUser(source));
913 if (!(dest = FindUser(nick))) {
914 return send_reply(source, ERR_NOSUCHNICK, nick);
916 if (!(chptr = FindChannel(channel))) {
917 return send_reply(source, ERR_NOSUCHCHANNEL, channel);
920 * compare both users channel lists, instead of the channels user list
921 * since the link is the same, this should be a little faster for channels
922 * with a lot of users
924 for (membership = cli_user(source)->channel; membership; membership = membership->next_channel) {
925 if (chptr == membership->channel)
928 if (0 == membership) {
929 return send_reply(source, ERR_NOTONCHANNEL, chptr->chname);
931 if (!IsVoicedOrOpped(membership)) {
932 return send_reply(source, ERR_VOICENEEDED, chptr->chname);
935 * lookup channel in destination
937 assert(0 != cli_user(dest));
938 for (membership = cli_user(dest)->channel; membership; membership = membership->next_channel) {
939 if (chptr == membership->channel)
942 if (0 == membership || IsZombie(membership)) {
943 return send_reply(source, ERR_USERNOTINCHANNEL, cli_name(dest), chptr->chname);
945 if (is_silenced(source, dest))
948 if (cli_user(dest)->away)
949 send_reply(source, RPL_AWAY, cli_name(dest), cli_user(dest)->away);
951 sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text);
953 sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text);
959 * added Sat Jul 25 07:30:42 EST 1992
961 void send_umode_out(struct Client *cptr, struct Client *sptr, int old,
965 struct Client *acptr;
967 send_umode(NULL, sptr, old, SEND_UMODES & ~(prop ? 0 : FLAGS_OPER));
969 for (i = HighestFd; i >= 0; i--) {
970 if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
971 (acptr != cptr) && (acptr != sptr) && *umodeBuf)
972 sendcmdto_one(sptr, CMD_MODE, acptr, "%s :%s", cli_name(sptr), umodeBuf);
974 if (cptr && MyUser(cptr))
975 send_umode(cptr, sptr, old, ALL_UMODES);
980 * send_user_info - send user info userip/userhost
981 * NOTE: formatter must put info into buffer and return a pointer to the end of
982 * the data it put in the buffer.
984 void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
990 struct Client* acptr;
997 mb = msgq_make(sptr, rpl_str(rpl), cli_name(&me), cli_name(sptr));
999 for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
1000 if ((acptr = FindUser(name))) {
1002 msgq_append(0, mb, " ");
1005 if (5 == ++arg_count)
1008 send_buffer(sptr, mb, 0);
1014 * set_user_mode() added 15/10/91 By Darren Reed.
1017 * parv[1] - username to change mode for
1018 * parv[2] - modes to change
1020 int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
1024 struct Client *acptr;
1028 unsigned int tmpmask = 0;
1029 int snomask_given = 0;
1036 return need_more_params(sptr, "MODE");
1038 if (!(acptr = FindUser(parv[1])))
1040 if (MyConnect(sptr))
1041 send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]);
1045 if (IsServer(sptr) || sptr != acptr)
1048 sendcmdto_flag_butone(&me, CMD_WALLOPS, 0, FLAGS_WALLOP,
1049 ":MODE for User %s from %s!%s", parv[1],
1050 cli_name(cptr), cli_name(sptr));
1052 send_reply(sptr, ERR_USERSDONTMATCH);
1060 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1061 if ( (userModeList[i].flag & cli_flags(sptr)))
1062 *m++ = userModeList[i].c;
1065 send_reply(sptr, RPL_UMODEIS, buf);
1066 if ((cli_flags(sptr) & FLAGS_SERVNOTICE) && MyConnect(sptr)
1067 && cli_snomask(sptr) !=
1068 (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT))
1069 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1074 * find flags already set for user
1075 * why not just copy them?
1077 setflags = cli_flags(sptr);
1079 if (MyConnect(sptr))
1080 tmpmask = cli_snomask(sptr);
1083 * parse mode change string(s)
1085 for (p = &parv[2]; *p; p++) { /* p is changed in loop too */
1086 for (m = *p; *m; m++) {
1095 if (*(p + 1) && is_snomask(*(p + 1))) {
1097 tmpmask = umode_make_snomask(tmpmask, *++p, what);
1098 tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1101 tmpmask = (what == MODE_ADD) ?
1102 (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1104 cli_flags(sptr) |= FLAGS_SERVNOTICE;
1106 cli_flags(sptr) &= ~FLAGS_SERVNOTICE;
1109 if (what == MODE_ADD)
1115 if (what == MODE_ADD)
1118 cli_flags(sptr) &= ~(FLAGS_OPER | FLAGS_LOCOP);
1119 if (MyConnect(sptr)) {
1120 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1121 cli_handler(sptr) = CLIENT_HANDLER;
1126 if (what == MODE_ADD)
1129 cli_flags(sptr) &= ~(FLAGS_OPER | FLAGS_LOCOP);
1130 if (MyConnect(sptr)) {
1131 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1132 cli_handler(sptr) = CLIENT_HANDLER;
1137 if (what == MODE_ADD)
1140 ClearInvisible(sptr);
1143 if (what == MODE_ADD)
1149 if (what == MODE_ADD)
1150 SetChannelService(sptr);
1152 ClearChannelService(sptr);
1155 if (what == MODE_ADD)
1166 * Evaluate rules for new user mode
1167 * Stop users making themselves operators too easily:
1169 if (!IsServer(cptr)) {
1170 if (!(setflags & FLAGS_OPER) && IsOper(sptr))
1172 if (!(setflags & FLAGS_LOCOP) && IsLocOp(sptr))
1175 * new umode; servers can set it, local users cannot;
1176 * prevents users from /kick'ing or /mode -o'ing
1178 if (!(setflags & FLAGS_CHSERV))
1179 ClearChannelService(sptr);
1181 * only send wallops to opers
1183 if (feature_bool(FEAT_WALLOPS_OPER_ONLY) && !IsAnOper(sptr) &&
1184 !(setflags & FLAGS_WALLOP))
1187 if (MyConnect(sptr)) {
1188 if ((setflags & (FLAGS_OPER | FLAGS_LOCOP)) && !IsAnOper(sptr))
1189 det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPS);
1191 if (tmpmask != cli_snomask(sptr))
1192 set_snomask(sptr, tmpmask, SNO_SET);
1193 if (cli_snomask(sptr) && snomask_given)
1194 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1197 * Compare new flags with old flags and send string which
1198 * will cause servers to update correctly.
1200 if (!(setflags & FLAGS_OPER) && IsOper(sptr)) { /* user now oper */
1202 client_set_privs(sptr); /* may set propagate privilege */
1204 if (HasPriv(sptr, PRIV_PROPAGATE)) /* remember propagate privilege setting */
1206 if ((setflags & FLAGS_OPER) && !IsOper(sptr)) { /* user no longer oper */
1208 client_set_privs(sptr); /* will clear propagate privilege */
1210 if ((setflags & FLAGS_INVISIBLE) && !IsInvisible(sptr))
1211 --UserStats.inv_clients;
1212 if (!(setflags & FLAGS_INVISIBLE) && IsInvisible(sptr))
1213 ++UserStats.inv_clients;
1214 send_umode_out(cptr, sptr, setflags, prop);
1220 * Build umode string for BURST command
1223 char *umode_str(struct Client *cptr)
1225 char* m = umodeBuf; /* Maximum string size: "owidg\0" */
1229 c_flags = cli_flags(cptr) & SEND_UMODES; /* cleaning up the original code */
1230 if (HasPriv(cptr, PRIV_PROPAGATE))
1231 c_flags |= FLAGS_OPER;
1233 c_flags &= ~FLAGS_OPER;
1235 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1236 if ( (c_flags & userModeList[i].flag))
1237 *m++ = userModeList[i].c;
1241 return umodeBuf; /* Note: static buffer, gets
1242 overwritten by send_umode() */
1246 * Send the MODE string for user (user) to connection cptr
1249 void send_umode(struct Client *cptr, struct Client *sptr, int old, int sendmask)
1254 int what = MODE_NULL;
1257 * Build a string in umodeBuf to represent the change in the user's
1258 * mode between the new (sptr->flag) and 'old'.
1262 for (i = 0; i < USERMODELIST_SIZE; ++i) {
1263 flag = userModeList[i].flag;
1264 if (MyUser(sptr) && !(flag & sendmask))
1266 if ( (flag & old) && !(cli_flags(sptr) & flag))
1268 if (what == MODE_DEL)
1269 *m++ = userModeList[i].c;
1274 *m++ = userModeList[i].c;
1277 else if (!(flag & old) && (cli_flags(sptr) & flag))
1279 if (what == MODE_ADD)
1280 *m++ = userModeList[i].c;
1285 *m++ = userModeList[i].c;
1290 if (*umodeBuf && cptr)
1291 sendcmdto_one(sptr, CMD_MODE, cptr, "%s :%s", cli_name(sptr), umodeBuf);
1295 * Check to see if this resembles a sno_mask. It is if 1) there is
1296 * at least one digit and 2) The first digit occurs before the first
1297 * alphabetic character.
1299 int is_snomask(char *word)
1303 for (; *word; word++)
1306 else if (IsAlpha(*word))
1313 * If it begins with a +, count this as an additive mask instead of just
1314 * a replacement. If what == MODE_DEL, "+" has no special effect.
1316 unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
1318 unsigned int sno_what;
1319 unsigned int newmask;
1323 if (what == MODE_ADD)
1328 else if (*arg == '-')
1331 if (what == MODE_ADD)
1337 sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL;
1338 /* pity we don't have strtoul everywhere */
1339 newmask = (unsigned int)atoi(arg);
1340 if (sno_what == SNO_DEL)
1341 newmask = oldmask & ~newmask;
1342 else if (sno_what == SNO_ADD)
1347 static void delfrom_list(struct Client *cptr, struct SLink **list)
1350 struct SLink* prv = NULL;
1352 for (tmp = *list; tmp; tmp = tmp->next) {
1353 if (tmp->value.cptr == cptr) {
1355 prv->next = tmp->next;
1366 * This function sets a Client's server notices mask, according to
1367 * the parameter 'what'. This could be even faster, but the code
1368 * gets mighty hard to read :)
1370 void set_snomask(struct Client *cptr, unsigned int newmask, int what)
1372 unsigned int oldmask, diffmask; /* unsigned please */
1376 oldmask = cli_snomask(cptr);
1378 if (what == SNO_ADD)
1380 else if (what == SNO_DEL)
1381 newmask = oldmask & ~newmask;
1382 else if (what != SNO_SET) /* absolute set, no math needed */
1383 sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what);
1385 newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
1387 diffmask = oldmask ^ newmask;
1389 for (i = 0; diffmask >> i; i++) {
1390 if (((diffmask >> i) & 1))
1392 if (((newmask >> i) & 1))
1395 tmp->next = opsarray[i];
1396 tmp->value.cptr = cptr;
1400 /* not real portable :( */
1401 delfrom_list(cptr, &opsarray[i]);
1404 cli_snomask(cptr) = newmask;
1408 * is_silenced : Does the actual check wether sptr is allowed
1409 * to send a message to acptr.
1410 * Both must be registered persons.
1411 * If sptr is silenced by acptr, his message should not be propagated,
1412 * but more over, if this is detected on a server not local to sptr
1413 * the SILENCE mask is sent upstream.
1415 int is_silenced(struct Client *sptr, struct Client *acptr)
1419 static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
1420 static char senderip[16 + NICKLEN + USERLEN + 5];
1422 if (!cli_user(acptr) || !(lp = cli_user(acptr)->silence) || !(user = cli_user(sptr)))
1424 sprintf_irc(sender, "%s!%s@%s", cli_name(sptr), user->username, user->host);
1425 sprintf_irc(senderip, "%s!%s@%s", cli_name(sptr), user->username,
1426 ircd_ntoa((const char*) &(cli_ip(sptr))));
1427 for (; lp; lp = lp->next)
1429 if ((!(lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, sender)) ||
1430 ((lp->flags & CHFL_SILENCE_IPMASK) && !match(lp->value.cp, senderip)))
1432 if (!MyConnect(sptr))
1434 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr,
1446 * Removes all silence masks from the list of sptr that fall within `mask'
1447 * Returns -1 if none where found, 0 otherwise.
1449 int del_silence(struct Client *sptr, char *mask)
1455 for (lp = &(cli_user(sptr))->silence; *lp;) {
1456 if (!mmatch(mask, (*lp)->value.cp))
1460 MyFree(tmp->value.cp);
1470 int add_silence(struct Client* sptr, const char* mask)
1472 struct SLink *lp, **lpp;
1473 int cnt = 0, len = strlen(mask);
1476 for (lpp = &(cli_user(sptr))->silence, lp = *lpp; lp;)
1478 if (0 == ircd_strcmp(mask, lp->value.cp))
1480 if (!mmatch(mask, lp->value.cp))
1482 struct SLink *tmp = lp;
1483 *lpp = lp = lp->next;
1484 MyFree(tmp->value.cp);
1490 len += strlen(lp->value.cp);
1491 if ((len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXSILES))) ||
1492 (++cnt >= feature_int(FEAT_MAXSILES)))
1494 send_reply(sptr, ERR_SILELISTFULL, mask);
1497 else if (!mmatch(lp->value.cp, mask))
1504 memset(lp, 0, sizeof(struct SLink));
1505 lp->next = cli_user(sptr)->silence;
1506 lp->value.cp = (char*) MyMalloc(strlen(mask) + 1);
1507 assert(0 != lp->value.cp);
1508 strcpy(lp->value.cp, mask);
1509 if ((ip_start = strrchr(mask, '@')) && check_if_ipmask(ip_start + 1))
1510 lp->flags = CHFL_SILENCE_IPMASK;
1511 cli_user(sptr)->silence = lp;