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.
24 * @brief Miscellaneous user-related helper functions.
25 * @version $Id: s_user.c 1864 2008-03-15 05:33:22Z entrope $
36 #include "ircd_alloc.h"
37 #include "ircd_chattr.h"
38 #include "ircd_features.h"
40 #include "ircd_reply.h"
41 #include "ircd_snprintf.h"
42 #include "ircd_string.h"
51 #include "querycmds.h"
58 #include "s_serv.h" /* max_client_count */
61 #include "supported.h"
67 #include "handlers.h" /* m_motd and m_lusers */
69 /* #include <assert.h> -- Now using assert in ircd_log.h */
76 /** Count of allocated User structures. */
77 static int userCount = 0;
79 /** Makes sure that \a cptr has a User information block.
80 * If cli_user(cptr) != NULL, does nothing.
81 * @param[in] cptr Client to attach User struct to.
82 * @return User struct associated with \a cptr.
84 struct User *make_user(struct Client *cptr)
88 if (!cli_user(cptr)) {
89 cli_user(cptr) = (struct User*) MyMalloc(sizeof(struct User));
90 assert(0 != cli_user(cptr));
92 /* All variables are 0 by default */
93 memset(cli_user(cptr), 0, sizeof(struct User));
95 cli_user(cptr)->refcnt = 1;
97 return cli_user(cptr);
100 /** Dereference \a user.
101 * User structures are reference-counted; if the refcount of \a user
102 * becomes zero, free it.
103 * @param[in] user User to dereference.
105 void free_user(struct User* user)
108 assert(0 < user->refcnt);
110 if (--user->refcnt == 0) {
114 MyFree(user->lastmsg);
118 assert(0 == user->joined);
119 assert(0 == user->invited);
120 assert(0 == user->channel);
128 /** Find number of User structs allocated and memory used by them.
129 * @param[out] count_out Receives number of User structs allocated.
130 * @param[out] bytes_out Receives number of bytes used by User structs.
132 void user_count_memory(size_t* count_out, size_t* bytes_out)
134 assert(0 != count_out);
135 assert(0 != bytes_out);
136 *count_out = userCount;
137 *bytes_out = userCount * sizeof(struct User);
141 /** Find the next client (starting at \a next) with a name that matches \a ch.
142 * Normal usage loop is:
143 * for (x = client; x = next_client(x,mask); x = x->next)
144 * HandleMatchingClient;
146 * @param[in] next First client to check.
147 * @param[in] ch Name mask to check against.
148 * @return Next matching client found, or NULL if none.
150 struct Client *next_client(struct Client *next, const char* ch)
152 struct Client *tmp = next;
157 next = FindClient(ch);
158 next = next ? next : tmp;
159 if (cli_prev(tmp) == next)
163 for (; next; next = cli_next(next))
164 if (!match(ch, cli_name(next)))
169 /** Find the destination server for a command, and forward it if that is not us.
171 * \a server may be a nickname, server name, server mask (if \a from
172 * is a local user) or server numnick (if \a is a server or remote
175 * @param[in] from Client that sent the command to us.
176 * @param[in] cmd Long-form command text.
177 * @param[in] tok Token-form command text.
178 * @param[in] one Client that originated the command (ignored).
179 * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH.
180 * @param[in] pattern Format string of arguments to command.
181 * @param[in] server Index of target name or mask in \a parv.
182 * @param[in] parc Number of valid elements in \a parv (must be less than 9).
183 * @param[in] parv Array of arguments to command.
184 * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS.
186 int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok,
187 struct Client *one, int MustBeOper, const char *pattern,
188 int server, int parc, char *parv[])
190 struct Client *acptr;
193 /* Assume it's me, if no server or an unregistered client */
194 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
195 return (HUNTED_ISME);
197 if (MustBeOper && !IsPrivileged(from))
199 send_reply(from, ERR_NOPRIVILEGES);
200 return HUNTED_NOSUCH;
203 /* Make sure it's a server */
205 /* Make sure it's a server */
206 if (!strchr(to, '*')) {
207 if (0 == (acptr = FindClient(to))) {
208 send_reply(from, ERR_NOSUCHSERVER, to);
209 return HUNTED_NOSUCH;
213 acptr = cli_user(acptr)->server;
214 } else if (!(acptr = find_match_server(to))) {
215 send_reply(from, ERR_NOSUCHSERVER, to);
216 return (HUNTED_NOSUCH);
218 } else if (!(acptr = FindNServer(to))) {
219 send_reply(from, SND_EXPLICIT | ERR_NOSUCHSERVER, "* :Server has disconnected");
220 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
224 return (HUNTED_ISME);
226 if (MustBeOper && !IsPrivileged(from)) {
227 send_reply(from, ERR_NOPRIVILEGES);
228 return HUNTED_NOSUCH;
231 /* assert(!IsServer(from)); */
233 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
235 sendcmdto_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
236 parv[4], parv[5], parv[6], parv[7], parv[8]);
238 return (HUNTED_PASS);
241 /** Find the destination server for a command, and forward it (as a
242 * high-priority command) if that is not us.
244 * \a server may be a nickname, server name, server mask (if \a from
245 * is a local user) or server numnick (if \a is a server or remote
247 * Unlike hunt_server_cmd(), this appends the message to the
248 * high-priority message queue for the destination server.
250 * @param[in] from Client that sent the command to us.
251 * @param[in] cmd Long-form command text.
252 * @param[in] tok Token-form command text.
253 * @param[in] one Client that originated the command (ignored).
254 * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH.
255 * @param[in] pattern Format string of arguments to command.
256 * @param[in] server Index of target name or mask in \a parv.
257 * @param[in] parc Number of valid elements in \a parv (must be less than 9).
258 * @param[in] parv Array of arguments to command.
259 * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS.
261 int hunt_server_prio_cmd(struct Client *from, const char *cmd, const char *tok,
262 struct Client *one, int MustBeOper,
263 const char *pattern, int server, int parc,
266 struct Client *acptr;
269 /* Assume it's me, if no server or an unregistered client */
270 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
271 return (HUNTED_ISME);
273 /* Make sure it's a server */
275 /* Make sure it's a server */
276 if (!strchr(to, '*')) {
277 if (0 == (acptr = FindClient(to))) {
278 send_reply(from, ERR_NOSUCHSERVER, to);
279 return HUNTED_NOSUCH;
283 acptr = cli_user(acptr)->server;
284 } else if (!(acptr = find_match_server(to))) {
285 send_reply(from, ERR_NOSUCHSERVER, to);
286 return (HUNTED_NOSUCH);
288 } else if (!(acptr = FindNServer(to)))
289 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
292 return (HUNTED_ISME);
294 if (MustBeOper && !IsPrivileged(from)) {
295 send_reply(from, ERR_NOPRIVILEGES);
296 return HUNTED_NOSUCH;
299 /* assert(!IsServer(from)); SETTIME to particular destinations permitted */
301 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
303 sendcmdto_prio_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
304 parv[4], parv[5], parv[6], parv[7], parv[8]);
306 return (HUNTED_PASS);
309 /* Helper function for register_user that splits up the parameters of the
310 * default user modes and sets them on the user.
312 static void set_initial_user_modes(struct Client *cptr) {
314 int i, num, l, offset;
315 char *tmpstr = (char*)client_get_default_umode(cptr);
319 tmpstr = strdup(tmpstr);
321 /* \tmpstr may have multiple parameters and \set_user_mode needs them
322 * to be split up. We just copy the default string and then replace the
323 * spaces with \0 to split the strings up.
326 for(i = 0; tmpstr[i]; ++i) {
327 if(tmpstr[i] == ' ') {
333 umodev = MyMalloc((6 + num) * sizeof(char*));
334 memset(umodev, 0, (6 + num) * sizeof(char*));
339 for(i = 0; i < num; ++i) {
340 for(l = 0; back[l]; ++l) /* do nothing */;
341 /* Ignore empty parameters. */
342 if(l > 0) umodev[2 + i + offset] = back;
348 set_user_mode(&me, cptr, 2 + num + offset, umodev, ALLOWMODES_WITHSECSERV);
356 * This function is called when both NICK and USER messages
357 * have been accepted for the client, in whatever order. Only
358 * after this the USER message is propagated.
360 * NICK's must be propagated at once when received, although
361 * it would be better to delay them too until full info is
362 * available. Doing it is not so simple though, would have
363 * to implement the following:
365 * 1) user telnets in and gives only "NICK foobar" and waits
366 * 2) another user far away logs in normally with the nick
367 * "foobar" (quite legal, as this server didn't propagate it).
368 * 3) now this server gets nick "foobar" from outside, but
369 * has already the same defined locally. Current server
370 * would just issue "KILL foobar" to clean out dups. But,
371 * this is not fair. It should actually request another
372 * nick from local user or kill him/her...
374 /** Finish registering a user who has sent both NICK and USER.
375 * For local connections, possibly check IAuth; make sure there is a
376 * matching Client config block; clean the username field; check
377 * K/k-lines; check for "hacked" looking usernames; assign a numnick;
378 * and send greeting (WELCOME, ISUPPORT, MOTD, etc).
379 * For all connections, update the invisible user and operator counts;
380 * run IPcheck against their address; and forward the NICK.
382 * @param[in] cptr Client who introduced the user.
383 * @param[in,out] sptr Client who has been fully introduced.
384 * @return Zero or CPTR_KILLED.
386 int register_user(struct Client *cptr, struct Client *sptr)
390 struct User* user = cli_user(sptr);
393 user->last = CurrentTime;
394 parv[0] = cli_name(sptr);
395 parv[1] = parv[2] = NULL;
399 assert(cptr == sptr);
401 Count_unknownbecomesclient(sptr, UserStats);
404 * Set user's initial modes
406 set_initial_user_modes(sptr);
409 if(MyConnect(sptr) && cli_socket(sptr).ssl)
412 cli_handler(sptr) = CLIENT_HANDLER;
413 SetLocalNumNick(sptr);
416 feature_str(FEAT_NETWORK),
417 feature_str(FEAT_PROVIDER) ? " via " : "",
418 feature_str(FEAT_PROVIDER) ? feature_str(FEAT_PROVIDER) : "",
421 * This is a duplicate of the NOTICE but see below...
423 send_reply(sptr, RPL_YOURHOST, cli_name(&me), version);
424 send_reply(sptr, RPL_CREATED, creation);
425 send_reply(sptr, RPL_MYINFO, cli_name(&me), version, infousermodes,
426 infochanmodes, infochanmodeswithparams);
427 send_supported(sptr);
430 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :You are connected to %s with %s", sptr,
431 cli_name(&me), ssl_cipherstr(cli_socket(sptr).ssl));
433 m_lusers(sptr, sptr, 1, parv);
436 if (cli_snomask(sptr) & SNO_NOISY)
437 set_snomask(sptr, cli_snomask(sptr) & SNO_NOISY, SNO_ADD);
438 if (feature_bool(FEAT_CONNEXIT_NOTICES))
439 sendto_opmask_butone(0, SNO_CONNEXIT,
440 "Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s%s>",
441 cli_name(sptr), user->username, user->host,
442 cli_sock_ip(sptr), get_client_class(sptr),
443 cli_info(sptr), NumNick(cptr) /* two %s's */);
444 IPcheck_connect_succeeded(sptr);
447 struct Client *acptr = user->server;
449 if (cli_from(acptr) != cli_from(sptr))
451 sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])",
452 sptr, cli_name(&me), cli_name(user->server), cli_name(cli_from(acptr)),
453 cli_sockhost(cli_from(acptr)));
454 SetFlag(sptr, FLAG_KILLED);
455 return exit_client(cptr, sptr, &me, "NICK server wrong direction");
457 else if (HasFlag(acptr, FLAG_TS8))
458 SetFlag(sptr, FLAG_TS8);
461 * Check to see if this user is being propagated
462 * as part of a net.burst, or is using protocol 9.
463 * FIXME: This can be sped up - its stupid to check it for
464 * every NICK message in a burst again --Run.
466 for (; acptr != &me; acptr = cli_serv(acptr)->up)
468 if (IsBurst(acptr) || Protocol(acptr) < 10)
471 if (!IPcheck_remote_connect(sptr, (acptr != &me)))
474 * We ran out of bits to count this
476 sendcmdto_one(&me, CMD_KILL, sptr, "%C :%s (Too many connections from your host -- Ghost)",
477 sptr, cli_name(&me));
478 return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled");
483 /* If they get both +x and an account during registration, hide
484 * their hostmask here. Calling hide_hostmask() from IAuth's
485 * account assignment causes a numeric reply during registration.
487 if (HasHiddenHost(sptr))
488 hide_hostmask(sptr, FLAG_HIDDENHOST);
489 if (IsInvisible(sptr))
490 ++UserStats.inv_clients;
494 client_set_uprivs(sptr, cli_confs(sptr)->value.aconf);
495 if (MyUser(sptr) && HasPriv(sptr, PRIV_SEE_IDLETIME))
496 SetSeeIdletime(sptr);
498 tmpstr = umode_str(sptr);
499 /* Send full IP address to IPv6-grokking servers. */
500 sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr,
501 FLAG_IPV6, FLAG_LAST_FLAG,
502 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
503 cli_name(sptr), cli_hopcount(sptr) + 1,
505 user->username, user->realhost,
506 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
507 iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 1),
508 NumNick(sptr), cli_info(sptr));
509 /* Send fake IPv6 addresses to pre-IPv6 servers. */
510 sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr,
511 FLAG_LAST_FLAG, FLAG_IPV6,
512 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
513 cli_name(sptr), cli_hopcount(sptr) + 1,
515 user->username, user->realhost,
516 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
517 iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 0),
518 NumNick(sptr), cli_info(sptr));
520 /* Send user mode to client */
523 static struct Flags flags; /* automatically initialized to zeros */
524 /* To avoid sending +r to the client due to auth-on-connect, set
525 * the "old" FLAG_ACCOUNT bit to match the client's value.
528 FlagSet(&flags, FLAG_ACCOUNT);
530 FlagClr(&flags, FLAG_ACCOUNT);
533 send_reply(sptr, RPL_YOUREOPER);
534 FlagSet(&cli_confs(sptr)->value.aconf->conn_class->privs_dirty, PRIV_PROPAGATE);
535 client_set_privs(sptr, cli_confs(sptr)->value.aconf);
536 sendto_opmask_butone(0, SNO_OLDSNO, "%s (%s@%s) is now operator (%c)",
537 cli_name(sptr), cli_user(sptr)->username, cli_sockhost(sptr),
538 IsOper(sptr) ? 'O' : 'o');
539 log_write(LS_OPER, L_INFO, 0, "OPER (<OOC>) by (%#C)", sptr);
540 cli_handler(sptr) = OPER_HANDLER;
543 if(*cli_connclass(sptr))
544 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Your connection class is: %s", sptr, cli_connclass(sptr));
546 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Your connection class is: %s", sptr, get_client_class(sptr));
547 if(HasPriv(sptr, PRIV_CHAN_LIMIT))
548 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :You have no channel number limitation.", sptr);
550 unsigned int maxchan = cli_confs(sptr)->value.aconf ? ConfMaxChannels(cli_confs(sptr)->value.aconf): feature_int(FEAT_MAXCHANNELSPERUSER);
551 if(sptr -> maxchans > 0)
552 maxchan = sptr->maxchans;
553 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :You may join %d channels.", sptr, maxchan);
556 send_umode(cptr, sptr, &flags, ALL_UMODES, 0);
557 if ((cli_snomask(sptr) != SNO_DEFAULT) && HasFlag(sptr, FLAG_SERVNOTICE))
558 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
563 /** List of user mode characters. */
564 static const struct UserMode {
565 unsigned int flag; /**< User mode constant. */
566 char c; /**< Character corresponding to the mode. */
570 { FLAG_INVISIBLE, 'i' },
571 { FLAG_WALLOP, 'w' },
572 { FLAG_SERVNOTICE, 's' },
574 { FLAG_CHSERV, 'k' },
576 { FLAG_ACCOUNT, 'r' },
577 { FLAG_FAKEHOST, 'f' },
578 { FLAG_NOCHAN, 'n' },
579 { FLAG_NOIDLE, 'I' },
580 { FLAG_XTRAOP, 'X' },
581 { FLAG_NETSERV, 'S' },
582 { FLAG_HIDDENOPER, 'H' },
583 { FLAG_OVERRIDECC, 'c' },
584 { FLAG_WEBIRC, 'W' },
585 { FLAG_SEE_IDLETIME,'t' },
586 { FLAG_SECURITY_SERV,'D' },
587 { FLAG_HIDDENHOST, 'x' }
590 /** Length of #userModeList. */
591 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
594 * XXX - find a way to get rid of this
596 /** Nasty global buffer used for communications with umode_str() and others. */
597 static char umodeBuf[BUFSIZE];
599 /** Try to set a user's nickname.
600 * If \a sptr is a server, the client is being introduced for the first time.
601 * @param[in] cptr Client to set nickname.
602 * @param[in] sptr Client sending the NICK.
603 * @param[in] nick New nickname.
604 * @param[in] parc Number of arguments to NICK.
605 * @param[in] parv Argument list to NICK.
606 * @param[in] force Boolean; forced nickchange
607 * @return CPTR_KILLED if \a cptr was killed, else 0.
609 int set_nick_name(struct Client* cptr, struct Client* sptr,
610 const char* nick, int parc, char* parv[], unsigned int force)
612 if (IsServer(sptr)) {
615 * A server introducing a new client, change source
617 struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
618 assert(0 != new_client);
620 cli_hopcount(new_client) = atoi(parv[2]);
621 cli_lastnick(new_client) = atoi(parv[3]);
626 strcpy(cli_name(new_client), nick);
627 cli_user(new_client) = make_user(new_client);
628 cli_user(new_client)->server = sptr;
629 SetRemoteNumNick(new_client, parv[parc - 2]);
631 * IP# of remote client
633 base64toip(parv[parc - 3], &cli_ip(new_client));
635 add_client_to_list(new_client);
636 hAddClient(new_client);
638 cli_serv(sptr)->ghost = 0; /* :server NICK means end of net.burst */
639 ircd_strncpy(cli_username(new_client), parv[4], USERLEN);
640 ircd_strncpy(cli_user(new_client)->username, parv[4], USERLEN);
641 ircd_strncpy(cli_user(new_client)->host, parv[5], HOSTLEN);
642 ircd_strncpy(cli_user(new_client)->realhost, parv[5], HOSTLEN);
643 ircd_strncpy(cli_info(new_client), parv[parc - 1], REALLEN);
645 Count_newremoteclient(UserStats, sptr);
647 if (parc > 7 && *parv[6] == '+') {
648 /* (parc-4) -3 for the ip, numeric nick, realname */
649 set_user_mode(cptr, new_client, parc-7, parv+4, ALLOWMODES_WITHSECSERV);
652 return register_user(cptr, new_client);
654 else if ((cli_name(sptr))[0]) {
656 * Client changing its nick
658 * If the client belongs to me, then check to see
659 * if client is on any channels where it is currently
660 * banned. If so, do not allow the nick change to occur.
663 const char* channel_name;
664 struct Membership *member;
665 if (!force && !IsXtraOp(sptr) && (channel_name = find_no_nickchange_channel(sptr))) {
666 return send_reply(cptr, ERR_BANNICKCHANGE, channel_name);
669 * Refuse nick change if the last nick change was less
670 * then 30 seconds ago. This is intended to get rid of
671 * clone bots doing NICK FLOOD. -SeKs
672 * If someone didn't change their nick for more then 60 seconds
673 * however, allow to do two nick changes immediately after another
674 * before limiting the nick flood. -Run
676 if (!force && (CurrentTime < cli_nextnick(cptr)))
678 cli_nextnick(cptr) += 2;
679 send_reply(cptr, ERR_NICKTOOFAST, parv[1],
680 cli_nextnick(cptr) - CurrentTime);
681 /* Send error message */
682 sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cli_name(cptr));
683 /* bounce NICK to user */
684 return 0; /* ignore nick change! */
687 /* Limit total to 1 change per NICK_DELAY seconds: */
688 cli_nextnick(cptr) += NICK_DELAY;
689 /* However allow _maximal_ 1 extra consecutive nick change: */
690 if (cli_nextnick(cptr) < CurrentTime)
691 cli_nextnick(cptr) = CurrentTime;
693 /* Invalidate all bans against the user so we check them again */
694 for (member = (cli_user(cptr))->channel; member;
695 member = member->next_channel)
696 ClearBanValid(member);
699 * Also set 'lastnick' to current time, if changed.
701 if (0 != ircd_strcmp(parv[0], nick))
702 cli_lastnick(sptr) = (sptr == cptr) ? TStime() : atoi(parv[2]);
705 * Client just changing his/her nick. If he/she is
706 * on a channel, send note of change to all clients
707 * on that channel. Propagate notice to other servers.
710 sendcmdto_common_channels_butone(sptr, CMD_NICK, NULL, ":%s", nick);
711 add_history(sptr, 1);
712 sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick,
716 sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick);
718 if ((cli_name(sptr))[0])
720 strcpy(cli_name(sptr), nick);
724 /* Local client setting NICK the first time */
725 strcpy(cli_name(sptr), nick);
727 return auth_set_nick(cli_auth(sptr), nick);
732 /* Refreshs the users host to the current fakehost. If no fakehost
733 * is set, the account-host is created. If no Account is set,
735 * Returns 1 if the host changed and 0 if not.
737 int apply_fakehost(struct Client *cptr) {
739 if(IsFakeHost(cptr)) {
740 ircd_strncpy(buf, cli_user(cptr)->fakehost, HOSTLEN);
742 else if (IsAccount(cptr)) {
743 ircd_snprintf(0, buf, HOSTLEN, "%s.%s", cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
745 if(strncmp(buf, cli_user(cptr)->host, HOSTLEN) == 0) return 0;
746 ircd_strncpy(cli_user(cptr)->host, buf, HOSTLEN);
750 /** Calculate the hash value for a target.
751 * @param[in] target Pointer to target, cast to unsigned int.
752 * @return Hash value constructed from the pointer.
754 static unsigned char hash_target(unsigned int target)
756 return (unsigned char) (target >> 16) ^ (target >> 8);
759 /** Records \a target as a recent target for \a sptr.
760 * @param[in] sptr User who has sent to a new target.
761 * @param[in] target Target to add.
764 add_target(struct Client *sptr, void *target)
766 /* Ok, this shouldn't work esp on alpha
768 unsigned char hash = hash_target((unsigned long) target);
769 unsigned char* targets;
772 assert(cli_local(sptr));
774 targets = cli_targets(sptr);
779 for (i = 0; i < MAXTARGETS; ++i) {
780 if (targets[i] == hash)
786 memmove(&targets[RESERVEDTARGETS + 1],
787 &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
788 targets[RESERVEDTARGETS] = hash;
791 /** Check whether \a sptr can send to or join \a target yet.
792 * @param[in] sptr User trying to join a channel or send a message.
793 * @param[in] target Target of the join or message.
794 * @param[in] name Name of the target.
795 * @param[in] created If non-zero, trying to join a new channel.
796 * @return Non-zero if too many target changes; zero if okay to send.
798 int check_target_limit(struct Client *sptr, void *target, const char *name,
801 unsigned char hash = hash_target((unsigned long) target);
803 unsigned char* targets;
806 assert(cli_local(sptr));
807 targets = cli_targets(sptr);
809 if(HasPriv(sptr, PRIV_UNLIMITED_TARGET))
813 * Same target as last time?
815 if (targets[0] == hash)
817 for (i = 1; i < MAXTARGETS; ++i) {
818 if (targets[i] == hash) {
819 memmove(&targets[1], &targets[0], i);
828 if (CurrentTime < cli_nexttarget(sptr)) {
829 /* If user is invited to channel, give him/her a free target */
830 if (IsChannelName(name) && IsInvited(sptr, target))
833 if (cli_nexttarget(sptr) - CurrentTime < TARGET_DELAY + 8) {
837 cli_nexttarget(sptr) += 2;
838 send_reply(sptr, ERR_TARGETTOOFAST, name,
839 cli_nexttarget(sptr) - CurrentTime);
844 cli_nexttarget(sptr) += TARGET_DELAY;
845 if (cli_nexttarget(sptr) < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)))
846 cli_nexttarget(sptr) = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1));
849 memmove(&targets[1], &targets[0], MAXTARGETS - 1);
854 /** Allows a channel operator to avoid target change checks when
855 * sending messages to users on their channel.
856 * @param[in] source User sending the message.
857 * @param[in] nick Destination of the message.
858 * @param[in] channel Name of channel being sent to.
859 * @param[in] text Message to send.
860 * @param[in] is_notice If non-zero, use CNOTICE instead of CPRIVMSG.
862 /* Added 971023 by Run. */
863 int whisper(struct Client* source, const char* nick, const char* channel,
864 const char* text, int is_notice)
867 struct Channel* chptr;
868 struct Membership* membership;
872 assert(0 != channel);
873 assert(MyUser(source));
875 if (!(dest = FindUser(nick))) {
876 return send_reply(source, ERR_NOSUCHNICK, nick);
878 if (!(chptr = FindChannel(channel))) {
879 return send_reply(source, ERR_NOSUCHCHANNEL, channel);
882 * compare both users channel lists, instead of the channels user list
883 * since the link is the same, this should be a little faster for channels
884 * with a lot of users
886 for (membership = cli_user(source)->channel; membership; membership = membership->next_channel) {
887 if (chptr == membership->channel)
890 if (0 == membership) {
891 return send_reply(source, ERR_NOTONCHANNEL, chptr->chname);
893 if (!IsVoicedOrOpped(membership)) {
894 return send_reply(source, ERR_VOICENEEDED, chptr->chname);
897 * lookup channel in destination
899 assert(0 != cli_user(dest));
900 for (membership = cli_user(dest)->channel; membership; membership = membership->next_channel) {
901 if (chptr == membership->channel)
904 if (0 == membership || IsZombie(membership)) {
905 return send_reply(source, ERR_USERNOTINCHANNEL, cli_name(dest), chptr->chname);
907 if (is_silenced(source, dest))
911 sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text);
914 if (cli_user(dest)->away)
915 send_reply(source, RPL_AWAY, cli_name(dest), cli_user(dest)->away);
916 sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text);
922 /** Send a user mode change for \a cptr to neighboring servers.
923 * @param[in] cptr User whose mode is changing.
924 * @param[in] sptr Client who sent us the mode change message.
925 * @param[in] old Prior set of user flags.
926 * @param[in] prop If non-zero, also include FLAG_OPER.
928 void send_umode_out(struct Client *cptr, struct Client *sptr,
929 struct Flags *old, int prop)
931 int i, server = 0, ret;
932 struct Client *acptr;
934 if(cptr && (IsServer(cptr) || IsMe(cptr))) server = 1;
936 ret = send_umode(NULL, sptr, old, prop ? SEND_UMODES : SEND_UMODES_BUT_OPER, server);
938 for (i = HighestFd; i >= 0; i--)
940 if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
941 (acptr != cptr) && (acptr != sptr) && *umodeBuf)
942 sendcmdto_one(sptr, CMD_MODE, acptr, ret?"%s %s":"%s :%s", cli_name(sptr), umodeBuf);
944 if (cptr && MyUser(cptr))
945 send_umode(cptr, sptr, old, ALL_UMODES, 0);
949 /** Call \a fmt for each Client named in \a names.
950 * @param[in] sptr Client requesting information.
951 * @param[in] names Space-delimited list of nicknames.
952 * @param[in] rpl Base reply string for messages.
953 * @param[in] fmt Formatting callback function.
955 void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
961 struct Client* acptr;
968 mb = msgq_make(sptr, rpl_str(rpl), cli_name(&me), cli_name(sptr));
970 for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
971 if ((acptr = FindUser(name))) {
973 msgq_append(0, mb, " ");
974 (*fmt)(acptr, sptr, mb);
976 if (5 == ++arg_count)
979 send_buffer(sptr, mb, 0);
983 /** Set \a flag on \a cptr and possibly hide the client's hostmask.
984 * @param[in,out] cptr User who is getting a new flag.
985 * @param[in] flag Some flag that affects host-hiding (FLAG_HIDDENHOST, FLAG_ACCOUNT, FLAG_FAKEHOST).
989 hide_hostmask(struct Client *cptr, unsigned int flag)
991 struct Membership *chan;
995 case FLAG_HIDDENHOST:
996 /* Local users cannot set +x unless FEAT_HOST_HIDING is true. */
997 if (MyConnect(cptr) && !feature_bool(FEAT_HOST_HIDING))
1002 /* Invalidate all bans against the user so we check them again */
1003 for (chan = (cli_user(cptr))->channel; chan;
1004 chan = chan->next_channel)
1005 ClearBanValid(chan);
1008 /* default: no special handling */
1012 /* Set flags and stop if no fakehost has to be applied. */
1013 SetFlag(cptr, flag);
1014 if(!HasHiddenHost(cptr))
1017 /* Generate new fakehost. */
1018 if(IsFakeHost(cptr)) ircd_strncpy(buf, cli_user(cptr)->fakehost, HOSTLEN);
1019 else if (IsAccount(cptr)) ircd_snprintf(0, buf, HOSTLEN, "%s.%s", cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
1021 if(strncmp(buf, cli_user(cptr)->host, HOSTLEN) == 0) return 0;
1023 /* Remove all "valid" marks on the bans. This forces them to be
1024 * rechecked if the ban is accessed again.
1026 for(chan = (cli_user(cptr))->channel; chan; chan = chan->next_channel) {
1027 ClearBanValid(chan);
1030 /* Quit user and set the previously generated fakehost. */
1031 sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Registered");
1032 ircd_strncpy(cli_user(cptr)->host, buf, HOSTLEN);
1034 /* ok, the client is now fully hidden, so let them know -- hikari */
1035 if (MyConnect(cptr))
1036 send_reply(cptr, RPL_HOSTHIDDEN, cli_user(cptr)->host);
1039 * Go through all channels the client was on, rejoin him
1040 * and set the modes, if any
1042 for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel)
1046 /* If the channel has delayed joins, we have to handle the join especially. */
1047 if((!(chan->channel->mode.mode & MODE_DELJOINS) && !IsInvisibleJoin(chan)) || IsChanOp(chan) || HasVoice(chan)) {
1048 sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, 0,
1049 "%H", chan->channel);
1050 if(IsChanOp(chan) && HasVoice(chan)) {
1051 sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0,
1052 "%H +ov %C %C", chan->channel, cptr,
1055 else if(IsChanOp(chan) || HasVoice(chan)) {
1056 sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0,
1057 "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr);
1061 SetDelayedJoin(chan);
1067 /** Set a user's mode. This function checks that \a cptr is trying to
1068 * set his own mode, prevents local users from setting inappropriate
1069 * modes through this function, and applies any other side effects of
1070 * a successful mode change.
1072 * @param[in,out] cptr User setting someone's mode.
1073 * @param[in] sptr Client who sent the mode change message.
1074 * @param[in] parc Number of parameters in \a parv.
1075 * @param[in] parv Parameters to MODE.
1076 * @param[in] allow_modes ALLOWMODES_ANY for any mode, ALLOWMODES_DEFAULT for
1077 * only permitting legitimate default user modes.
1080 int set_user_mode(struct Client *cptr, struct Client *sptr, int parc,
1081 char *parv[], int allow_modes)
1087 struct Flags setflags;
1088 unsigned int tmpmask = 0;
1089 int snomask_given = 0;
1092 int do_host_hiding = 0;
1093 char* account = NULL, *fakehost = NULL;
1094 struct Membership *chan;
1102 for (i = 0; i < USERMODELIST_SIZE; i++)
1104 if (HasFlag(sptr, userModeList[i].flag) &&
1105 (userModeList[i].flag != FLAG_ACCOUNT) &&
1106 (userModeList[i].flag != FLAG_FAKEHOST))
1107 *m++ = userModeList[i].c;
1110 send_reply(sptr, RPL_UMODEIS, buf);
1111 if (HasFlag(sptr, FLAG_SERVNOTICE) && MyConnect(sptr)
1112 && cli_snomask(sptr) !=
1113 (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT))
1114 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1119 * find flags already set for user
1120 * why not just copy them?
1122 setflags = cli_flags(sptr);
1124 if (MyConnect(sptr))
1125 tmpmask = cli_snomask(sptr);
1128 * parse mode change string(s)
1130 for (p = &parv[2]; *p && p<&parv[parc]; p++) { /* p is changed in loop too */
1131 for (m = *p; *m; m++) {
1140 if (*(p + 1) && is_snomask(*(p + 1))) {
1142 tmpmask = umode_make_snomask(tmpmask, *++p, what);
1143 tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1146 tmpmask = (what == MODE_ADD) ?
1147 (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1149 SetServNotice(sptr);
1151 ClearServNotice(sptr);
1154 if (what == MODE_ADD)
1160 if (what == MODE_ADD)
1163 ClrFlag(sptr, FLAG_OPER);
1164 ClrFlag(sptr, FLAG_LOCOP);
1165 if (MyConnect(sptr))
1167 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1168 cli_handler(sptr) = CLIENT_HANDLER;
1173 if (what == MODE_ADD)
1177 ClrFlag(sptr, FLAG_OPER);
1178 ClrFlag(sptr, FLAG_LOCOP);
1179 if (MyConnect(sptr))
1181 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1182 cli_handler(sptr) = CLIENT_HANDLER;
1187 if (what == MODE_ADD)
1190 ClearInvisible(sptr);
1193 if (what == MODE_ADD)
1199 if (what == MODE_ADD)
1200 SetOverrideCC(sptr);
1202 ClearOverrideCC(sptr);
1205 if (what == MODE_ADD)
1206 SetChannelService(sptr);
1208 ClearChannelService(sptr);
1211 if (what == MODE_ADD)
1217 if(what == MODE_ADD && !FlagHas(&setflags, FLAG_HIDDENHOST) &&
1218 (!MyConnect(sptr) || feature_bool(FEAT_HOST_HIDING))) {
1220 SetHiddenHost(sptr);
1224 if (what == MODE_ADD)
1230 if (what == MODE_ADD)
1236 if (what == MODE_ADD)
1242 if (what == MODE_ADD)
1248 if (what == MODE_ADD)
1249 SetHiddenOper(sptr);
1251 ClearHiddenOper(sptr);
1254 if (what == MODE_ADD)
1255 SetSecurityServ(sptr);
1257 ClearSecurityServ(sptr);
1260 if (what == MODE_ADD)
1266 if (*(p + 1) && (what == MODE_ADD)) {
1269 for(chan = (cli_user(sptr))->channel; chan; chan = chan->next_channel) ClearBanValid(chan);
1270 if(HasFlag(sptr, FLAG_HIDDENHOST))
1273 /* There is no -r */
1276 if (*(p + 1) && (what == MODE_ADD)) {
1279 for(chan = (cli_user(sptr))->channel; chan; chan = chan->next_channel) ClearBanValid(chan);
1280 if(HasFlag(sptr, FLAG_HIDDENHOST))
1283 /* There is no -f */
1285 case 't': /* This can only be set by servers and cannot be removed. */
1286 if (what == MODE_ADD)
1287 SetSeeIdletime(sptr);
1289 #ifdef OLD_OGN_IRCU_COMPAT
1290 case 'z': /* Formerly SSL mode; we ignore it. */
1294 send_reply(sptr, ERR_UMODEUNKNOWNFLAG, *m);
1300 * Evaluate rules for new user mode
1301 * Stop users making themselves operators too easily:
1303 if (!IsServer(cptr) && !IsMe(cptr))
1305 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr))
1307 if (!FlagHas(&setflags, FLAG_LOCOP) && IsLocOp(sptr))
1309 if (!FlagHas(&setflags, FLAG_ACCOUNT) && IsAccount(sptr))
1310 ClrFlag(sptr, FLAG_ACCOUNT);
1311 if (!FlagHas(&setflags, FLAG_FAKEHOST) && IsFakeHost(sptr))
1312 ClrFlag(sptr, FLAG_FAKEHOST);
1313 if (!FlagHas(&setflags, FLAG_SEE_IDLETIME) && IsSeeIdletime(sptr))
1314 ClrFlag(sptr, FLAG_SEE_IDLETIME);
1316 * new umode; servers and privileged opers can set it, local users cannot;
1317 * prevents users from /kick'ing or /mode -o'ing
1319 if (!FlagHas(&setflags, FLAG_CHSERV) && IsChannelService(sptr) && !HasPriv(sptr, PRIV_UMODE_CHSERV))
1320 ClearChannelService(sptr);
1321 if (!FlagHas(&setflags, FLAG_NOCHAN) && IsNoChan(sptr) && !HasPriv(sptr, PRIV_UMODE_NOCHAN))
1323 if (!FlagHas(&setflags, FLAG_NOIDLE) && IsNoIdle(sptr) && !HasPriv(sptr, PRIV_UMODE_NOIDLE))
1325 if (!FlagHas(&setflags, FLAG_XTRAOP) && IsXtraOp(sptr) && (!IsOper(sptr) || !HasPriv(sptr, PRIV_UMODE_XTRAOP)))
1327 if (!FlagHas(&setflags, FLAG_NETSERV) && IsNetServ(sptr) && (!HasPriv(sptr, PRIV_UMODE_NETSERV) || !HasFlag(sptr, FLAG_SECURITY_SERV)))
1329 if (!FlagHas(&setflags, FLAG_HIDDENOPER) && IsHiddenOper(sptr) && !IsOper(sptr))
1330 ClearHiddenOper(sptr);
1331 if (!FlagHas(&setflags, FLAG_OVERRIDECC) && IsOverrideCC(sptr) && !HasPriv(sptr, PRIV_UMODE_OVERRIDECC))
1332 ClearOverrideCC(sptr);
1333 /* Opers are able to fake the webirc usermode only if FEAT_FAKE_WEBIRC is true. */
1334 if (!FlagHas(&setflags, FLAG_WEBIRC) && IsWebIRC(sptr) && !(feature_bool(FEAT_FAKE_WEBIRC) && IsOper(sptr)))
1337 * only send wallops to opers
1339 if (feature_bool(FEAT_WALLOPS_OPER_ONLY) && !IsAnOper(sptr) &&
1340 !FlagHas(&setflags, FLAG_WALLOP))
1342 if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY) && MyConnect(sptr) &&
1343 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_SERVNOTICE))
1345 ClearServNotice(sptr);
1346 set_snomask(sptr, 0, SNO_SET);
1348 if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY) &&
1349 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_DEBUG))
1352 if (allow_modes != ALLOWMODES_WITHSECSERV) {
1353 if (!FlagHas(&setflags, FLAG_SECURITY_SERV) && IsSecurityServ(sptr)) {
1354 ClearSecurityServ(sptr);
1355 sendto_opmask_butone(0, SNO_OLDSNO, "Someone tried to set mode +D to %s - Access denied",cli_name(sptr));
1358 if (MyConnect(sptr))
1360 if ((FlagHas(&setflags, FLAG_OPER) || FlagHas(&setflags, FLAG_LOCOP)) &&
1362 det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPERATOR);
1364 if (SendServNotice(sptr))
1366 if (tmpmask != cli_snomask(sptr))
1367 set_snomask(sptr, tmpmask, SNO_SET);
1368 if (cli_snomask(sptr) && snomask_given)
1369 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1372 set_snomask(sptr, 0, SNO_SET);
1375 * Compare new flags with old flags and send string which
1376 * will cause servers to update correctly.
1378 if (!FlagHas(&setflags, FLAG_ACCOUNT) && IsAccount(sptr)) {
1379 int len = ACCOUNTLEN;
1381 if ((ts = strchr(account, ':'))) {
1382 len = (ts++) - account;
1383 cli_user(sptr)->acc_create = atoi(ts);
1384 Debug((DEBUG_DEBUG, "Received timestamped account in user mode; "
1385 "account \"%s\", timestamp %Tu", account,
1386 cli_user(sptr)->acc_create));
1388 ircd_strncpy(cli_user(sptr)->account, account, len);
1390 if (!FlagHas(&setflags, FLAG_FAKEHOST) && IsFakeHost(sptr)) {
1391 ircd_strncpy(cli_user(sptr)->fakehost, fakehost, HOSTLEN);
1394 if (IsRegistered(sptr)) {
1396 hide_hostmask(sptr, 0);
1398 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr)) {
1401 client_set_privs(sptr, NULL); /* may set propagate privilege */
1403 if(MyConnect(sptr)) {
1404 sendto_opmask_butone(0, SNO_OLDSNO, "%s (%s@%s) is now operator (%c)",
1405 cli_name(sptr), cli_user(sptr)->username,
1406 cli_sockhost(sptr), IsOper(sptr) ? 'O' : 'o');
1407 log_write(LS_OPER, L_INFO, 0, "OPER (<OOM>) by (%#C)", sptr);
1408 cli_handler(sptr) = OPER_HANDLER;
1409 send_reply(sptr, RPL_YOUREOPER);
1412 if (FlagHas(&setflags, FLAG_OPER) && !IsOper(sptr)) {
1415 /* remember propagate privilege setting */
1416 if (HasPriv(sptr, PRIV_PROPAGATE)) {
1419 if (FlagHas(&setflags, FLAG_OPER) && !IsOper(sptr)) {
1420 /* user no longer oper */
1421 assert(UserStats.opers > 0);
1423 client_set_privs(sptr, NULL); /* will clear propagate privilege */
1424 client_set_uprivs(sptr, cli_confs(sptr)->value.aconf);
1426 if (FlagHas(&setflags, FLAG_INVISIBLE) && !IsInvisible(sptr)) {
1427 assert(UserStats.inv_clients > 0);
1428 --UserStats.inv_clients;
1430 if (!FlagHas(&setflags, FLAG_INVISIBLE) && IsInvisible(sptr)) {
1431 ++UserStats.inv_clients;
1433 assert(UserStats.opers <= UserStats.clients + UserStats.unknowns);
1434 assert(UserStats.inv_clients <= UserStats.clients + UserStats.unknowns);
1435 send_umode_out(cptr, sptr, &setflags, prop);
1441 /** Build a mode string to describe modes for \a cptr.
1442 * @param[in] cptr Some user.
1443 * @return Pointer to a static buffer.
1445 char *umode_str(struct Client *cptr)
1447 /* Maximum string size: "owidgrx\0" */
1450 struct Flags c_flags = cli_flags(cptr);
1452 if (!HasPriv(cptr, PRIV_PROPAGATE))
1453 FlagClr(&c_flags, FLAG_OPER);
1455 for (i = 0; i < USERMODELIST_SIZE; ++i)
1457 if (FlagHas(&c_flags, userModeList[i].flag) &&
1458 userModeList[i].flag >= FLAG_GLOBAL_UMODES)
1459 *m++ = userModeList[i].c;
1462 /* Append the arguments for the flags. They have to be appended
1463 * in the right order. See the order of userModeList[].
1466 if (IsAccount(cptr)) {
1467 char* t = cli_user(cptr)->account;
1470 while ((*m++ = *t++))
1473 /* If timestamped, append the timestamp directly to the accountname
1474 * separated with a colon
1476 if (cli_user(cptr)->acc_create) {
1478 Debug((DEBUG_DEBUG, "Sending timestamped account in user mode for "
1479 "account \"%s\"; timestamp %Tu", cli_user(cptr)->account,
1480 cli_user(cptr)->acc_create));
1481 ircd_snprintf(0, t = nbuf, sizeof(nbuf), ":%Tu",
1482 cli_user(cptr)->acc_create);
1483 m--; /* back up over previous nul-termination */
1484 while ((*m++ = *t++))
1488 --m; /* back up over previous nul-termination */
1491 if(IsFakeHost(cptr)) {
1492 char* t = cli_user(cptr)->fakehost;
1494 while((*m++ = *t++)) ; /* Empty loop */
1495 --m; /* back up over previous nul-termination */
1500 return umodeBuf; /* Note: static buffer, gets
1501 overwritten by send_umode() */
1504 /** Send a mode change string for \a sptr to \a cptr.
1505 * @param[in] cptr Destination of mode change message.
1506 * @param[in] sptr User whose mode has changed.
1507 * @param[in] old Pre-change set of modes for \a sptr.
1508 * @param[in] sendset One of ALL_UMODES, SEND_UMODES_BUT_OPER,
1509 * @param[in] serv_modes Set to 1 if you want to send ACCOUNT and FAKEHOST with this command.
1510 * @return 1 if ":" is already used in the buffer, 0 otherwise. Always 0 if \a serv_modes is 0.
1511 * SEND_UMODES, to select which changed user modes to send.
1513 int send_umode(struct Client *cptr, struct Client *sptr, struct Flags *old,
1514 int sendset, int serv_modes)
1519 int what = MODE_NULL;
1520 int add_fakehost = 0, add_account = 0;
1523 * Build a string in umodeBuf to represent the change in the user's
1524 * mode between the new (cli_flags(sptr)) and 'old', but skipping
1525 * the modes indicated by sendset.
1529 for (i = 0; i < USERMODELIST_SIZE; ++i)
1531 flag = userModeList[i].flag;
1532 if (FlagHas(old, flag)
1533 == HasFlag(sptr, flag))
1536 /* Account is not shown to the user as umode. */
1537 if(flag == FLAG_ACCOUNT) {
1538 if(!serv_modes || FlagHas(old, flag)) continue;
1542 if(flag == FLAG_FAKEHOST) {
1543 /* Removing a fakehost is not possible. Ignore it. */
1544 if(!serv_modes || FlagHas(old, flag)) continue;
1552 case SEND_UMODES_BUT_OPER:
1553 if (flag == FLAG_OPER)
1555 /* and fall through */
1557 if (flag < FLAG_GLOBAL_UMODES)
1561 if (FlagHas(old, flag))
1563 if (what == MODE_DEL)
1564 *m++ = userModeList[i].c;
1569 *m++ = userModeList[i].c;
1572 else /* !FlagHas(old, flag) */
1574 if (what == MODE_ADD)
1575 *m++ = userModeList[i].c;
1580 *m++ = userModeList[i].c;
1586 char* t = cli_user(sptr)->account;
1593 while ((*m++ = *t++)) /* Empty loop */ ;
1594 if(cli_user(sptr)->acc_create) {
1596 ircd_snprintf(0, t = nbuf, sizeof(nbuf), ":%Tu", cli_user(sptr)->acc_create);
1597 m--; /* back up over previous nul-termination */
1598 while ((*m++ = *t++)) /* Empty loop */ ;
1600 --m; /* back up over previous nul-termination */
1604 char* t = cli_user(sptr)->fakehost;
1609 while((*m++ = *t++)) ; /* Empty loop */
1610 --m; /* back up over previous nul-termination */
1614 if (*umodeBuf && cptr)
1615 sendcmdto_one(sptr, CMD_MODE, cptr, ret?"%s %s":"%s :%s", cli_name(sptr), umodeBuf);
1620 * Check to see if this resembles a sno_mask. It is if 1) there is
1621 * at least one digit and 2) The first digit occurs before the first
1622 * alphabetic character.
1623 * @param[in] word Word to check for sno_mask-ness.
1624 * @return Non-zero if \a word looks like a server notice mask; zero if not.
1626 int is_snomask(char *word)
1630 for (; *word; word++)
1633 else if (IsAlpha(*word))
1639 /** Update snomask \a oldmask according to \a arg and \a what.
1640 * @param[in] oldmask Original user mask.
1641 * @param[in] arg Update string (either a number or '+'/'-' followed by a number).
1642 * @param[in] what MODE_ADD if adding the mask.
1643 * @return New value of service notice mask.
1645 unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
1647 unsigned int sno_what;
1648 unsigned int newmask;
1652 if (what == MODE_ADD)
1657 else if (*arg == '-')
1660 if (what == MODE_ADD)
1666 sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL;
1667 /* pity we don't have strtoul everywhere */
1668 newmask = (unsigned int)atoi(arg);
1669 if (sno_what == SNO_DEL)
1670 newmask = oldmask & ~newmask;
1671 else if (sno_what == SNO_ADD)
1676 /** Remove \a cptr from the singly linked list \a list.
1677 * @param[in] cptr Client to remove from list.
1678 * @param[in,out] list Pointer to head of list containing \a cptr.
1680 static void delfrom_list(struct Client *cptr, struct SLink **list)
1683 struct SLink* prv = NULL;
1685 for (tmp = *list; tmp; tmp = tmp->next) {
1686 if (tmp->value.cptr == cptr) {
1688 prv->next = tmp->next;
1698 /** Set \a cptr's server notice mask, according to \a what.
1699 * @param[in,out] cptr Client whose snomask is updating.
1700 * @param[in] newmask Base value for new snomask.
1701 * @param[in] what One of SNO_ADD, SNO_DEL, SNO_SET, to choose operation.
1703 void set_snomask(struct Client *cptr, unsigned int newmask, int what)
1705 unsigned int oldmask, diffmask; /* unsigned please */
1709 oldmask = cli_snomask(cptr);
1711 if (what == SNO_ADD)
1713 else if (what == SNO_DEL)
1714 newmask = oldmask & ~newmask;
1715 else if (what != SNO_SET) /* absolute set, no math needed */
1716 sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what);
1718 newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
1720 diffmask = oldmask ^ newmask;
1722 for (i = 0; diffmask >> i; i++) {
1723 if (((diffmask >> i) & 1))
1725 if (((newmask >> i) & 1))
1728 tmp->next = opsarray[i];
1729 tmp->value.cptr = cptr;
1733 /* not real portable :( */
1734 delfrom_list(cptr, &opsarray[i]);
1737 cli_snomask(cptr) = newmask;
1740 /** Check whether \a sptr is allowed to send a message to \a acptr.
1741 * If \a sptr is a remote user, it means some server has an outdated
1742 * SILENCE list for \a acptr, so send the missing SILENCE mask(s) back
1743 * in the direction of \a sptr. Skip the check if \a sptr is a server.
1744 * @param[in] sptr Client trying to send a message.
1745 * @param[in] acptr Destination of message.
1746 * @return Non-zero if \a sptr is SILENCEd by \a acptr, zero if not.
1748 int is_silenced(struct Client *sptr, struct Client *acptr)
1752 size_t buf_used, slen;
1755 if (IsServer(sptr) || !(user = cli_user(acptr))
1756 || !(found = find_ban(sptr, user->silence)))
1758 assert(!(found->flags & BAN_EXCEPTION));
1759 if (!MyConnect(sptr)) {
1760 /* Buffer positive silence to send back. */
1761 buf_used = strlen(found->banstr);
1762 memcpy(buf, found->banstr, buf_used);
1763 /* Add exceptions to buffer. */
1764 for (found = user->silence; found; found = found->next) {
1765 if (!(found->flags & BAN_EXCEPTION))
1767 slen = strlen(found->banstr);
1768 if (buf_used + slen + 4 > 400) {
1769 buf[buf_used] = '\0';
1770 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf);
1774 buf[buf_used++] = ',';
1775 buf[buf_used++] = '+';
1776 buf[buf_used++] = '~';
1777 memcpy(buf + buf_used, found->banstr, slen);
1780 /* Flush silence buffer. */
1782 buf[buf_used] = '\0';
1783 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf);
1790 /** Send RPL_ISUPPORT lines to \a cptr.
1791 * @param[in] cptr Client to send ISUPPORT to.
1795 send_supported(struct Client *cptr)
1797 char featurebuf[512];
1799 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES1, FEATURESVALUES1);
1800 send_reply(cptr, RPL_ISUPPORT, featurebuf);
1801 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES2, FEATURESVALUES2);
1802 send_reply(cptr, RPL_ISUPPORT, featurebuf);
1804 return 0; /* convenience return, if it's ever needed */
1807 /* vim: shiftwidth=2