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,v 1.99.2.2 2006/02/16 03:49:54 entrope Exp $
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 static char *IsVhost(char *hostmask
, int oper
);
77 static char *IsVhostPass(char *hostmask
);
79 /** Count of allocated User structures. */
80 static int userCount
= 0;
82 /** Makes sure that \a cptr has a User information block.
83 * If cli_user(cptr) != NULL, does nothing.
84 * @param[in] cptr Client to attach User struct to.
85 * @return User struct associated with \a cptr.
87 struct User
*make_user(struct Client
*cptr
)
91 if (!cli_user(cptr
)) {
92 cli_user(cptr
) = (struct User
*) MyMalloc(sizeof(struct User
));
93 assert(0 != cli_user(cptr
));
95 /* All variables are 0 by default */
96 memset(cli_user(cptr
), 0, sizeof(struct User
));
98 cli_user(cptr
)->refcnt
= 1;
100 return cli_user(cptr
);
103 /** Dereference \a user.
104 * User structures are reference-counted; if the refcount of \a user
105 * becomes zero, free it.
106 * @param[in] user User to dereference.
108 void free_user(struct User
* user
)
111 assert(0 < user
->refcnt
);
113 if (--user
->refcnt
== 0) {
119 assert(0 == user
->joined
);
120 assert(0 == user
->invited
);
121 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
);
313 * This function is called when both NICK and USER messages
314 * have been accepted for the client, in whatever order. Only
315 * after this the USER message is propagated.
317 * NICK's must be propagated at once when received, although
318 * it would be better to delay them too until full info is
319 * available. Doing it is not so simple though, would have
320 * to implement the following:
322 * 1) user telnets in and gives only "NICK foobar" and waits
323 * 2) another user far away logs in normally with the nick
324 * "foobar" (quite legal, as this server didn't propagate it).
325 * 3) now this server gets nick "foobar" from outside, but
326 * has already the same defined locally. Current server
327 * would just issue "KILL foobar" to clean out dups. But,
328 * this is not fair. It should actually request another
329 * nick from local user or kill him/her...
331 /** Finish registering a user who has sent both NICK and USER.
332 * For local connections, possibly check IAuth; make sure there is a
333 * matching Client config block; clean the username field; check
334 * K/k-lines; check for "hacked" looking usernames; assign a numnick;
335 * and send greeting (WELCOME, ISUPPORT, MOTD, etc).
336 * For all connections, update the invisible user and operator counts;
337 * run IPcheck against their address; and forward the NICK.
339 * @param[in] cptr Client who introduced the user.
340 * @param[in,out] sptr Client who has been fully introduced.
341 * @return Zero or CPTR_KILLED.
343 int register_user(struct Client
*cptr
, struct Client
*sptr
)
347 struct User
* user
= cli_user(sptr
);
350 user
->last
= CurrentTime
;
351 parv
[0] = cli_name(sptr
);
352 parv
[1] = parv
[2] = NULL
;
356 assert(cptr
== sptr
);
358 Count_unknownbecomesclient(sptr
, UserStats
);
360 if (MyConnect(sptr
) && feature_bool(FEAT_AUTOINVISIBLE
))
363 if(MyConnect(sptr
) && feature_bool(FEAT_SETHOST_AUTO
)) {
364 if (conf_check_slines(sptr
)) {
365 send_reply(sptr
, RPL_USINGSLINE
);
371 cli_handler(sptr
) = CLIENT_HANDLER
;
372 SetLocalNumNick(sptr
);
375 feature_str(FEAT_NETWORK
),
376 feature_str(FEAT_PROVIDER
) ? " via " : "",
377 feature_str(FEAT_PROVIDER
) ? feature_str(FEAT_PROVIDER
) : "",
380 * This is a duplicate of the NOTICE but see below...
382 send_reply(sptr
, RPL_YOURHOST
, cli_name(&me
), version
);
383 send_reply(sptr
, RPL_CREATED
, creation
);
384 send_reply(sptr
, RPL_MYINFO
, cli_name(&me
), version
, infousermodes
,
385 infochanmodes
, infochanmodeswithparams
);
386 send_supported(sptr
);
387 m_lusers(sptr
, sptr
, 1, parv
);
390 if (cli_snomask(sptr
) & SNO_NOISY
)
391 set_snomask(sptr
, cli_snomask(sptr
) & SNO_NOISY
, SNO_ADD
);
392 if (feature_bool(FEAT_CONNEXIT_NOTICES
))
393 sendto_opmask_butone(0, SNO_CONNEXIT
,
394 "Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s%s>",
395 cli_name(sptr
), user
->username
, user
->host
,
396 cli_sock_ip(sptr
), get_client_class(sptr
),
397 cli_info(sptr
), NumNick(cptr
) /* two %s's */);
399 IPcheck_connect_succeeded(sptr
);
401 * Set user's initial modes
403 tmpstr
= (char*)client_get_default_umode(sptr
);
404 if (tmpstr
) for (; *tmpstr
; ++tmpstr
) {
407 if (!feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY
)) {
409 set_snomask(sptr
, SNO_DEFAULT
, SNO_SET
);
413 if (!feature_bool(FEAT_WALLOPS_OPER_ONLY
))
423 if (!feature_bool(FEAT_HIS_DEBUG_OPER_ONLY
))
430 struct Client
*acptr
= user
->server
;
432 Count_newremoteclient(UserStats
, acptr
);
434 if (cli_from(acptr
) != cli_from(sptr
))
436 sendcmdto_one(&me
, CMD_KILL
, cptr
, "%C :%s (%s != %s[%s])",
437 sptr
, cli_name(&me
), cli_name(user
->server
), cli_name(cli_from(acptr
)),
438 cli_sockhost(cli_from(acptr
)));
439 SetFlag(sptr
, FLAG_KILLED
);
440 return exit_client(cptr
, sptr
, &me
, "NICK server wrong direction");
442 else if (HasFlag(acptr
, FLAG_TS8
))
443 SetFlag(sptr
, FLAG_TS8
);
446 * Check to see if this user is being propagated
447 * as part of a net.burst, or is using protocol 9.
448 * FIXME: This can be sped up - its stupid to check it for
449 * every NICK message in a burst again --Run.
451 for (; acptr
!= &me
; acptr
= cli_serv(acptr
)->up
)
453 if (IsBurst(acptr
) || Protocol(acptr
) < 10)
456 if (!IPcheck_remote_connect(sptr
, (acptr
!= &me
)))
459 * We ran out of bits to count this
461 sendcmdto_one(&me
, CMD_KILL
, sptr
, "%C :%s (Too many connections from your host -- Ghost)",
462 sptr
, cli_name(&me
));
463 return exit_client(cptr
, sptr
, &me
,"Too many connections from your host -- throttled");
466 if(MyConnect(sptr
) && feature_bool(FEAT_SETHOST_AUTO
)) {
467 if (conf_check_slines(sptr
)) {
468 send_reply(sptr
, RPL_USINGSLINE
);
476 if (IsInvisible(sptr
))
477 ++UserStats
.inv_clients
;
481 tmpstr
= umode_str(sptr
);
482 /* Send full IP address to IPv6-grokking servers. */
483 sendcmdto_flag_serv_butone(user
->server
, CMD_NICK
, cptr
,
484 FLAG_IPV6
, FLAG_LAST_FLAG
,
485 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
486 cli_name(sptr
), cli_hopcount(sptr
) + 1,
488 user
->realusername
, user
->realhost
,
489 *tmpstr
? "+" : "", tmpstr
, *tmpstr
? " " : "",
490 iptobase64(ip_base64
, &cli_ip(sptr
), sizeof(ip_base64
), 1),
491 NumNick(sptr
), cli_info(sptr
));
492 /* Send fake IPv6 addresses to pre-IPv6 servers. */
493 sendcmdto_flag_serv_butone(user
->server
, CMD_NICK
, cptr
,
494 FLAG_LAST_FLAG
, FLAG_IPV6
,
495 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
496 cli_name(sptr
), cli_hopcount(sptr
) + 1,
498 user
->realusername
, user
->realhost
,
499 *tmpstr
? "+" : "", tmpstr
, *tmpstr
? " " : "",
500 iptobase64(ip_base64
, &cli_ip(sptr
), sizeof(ip_base64
), 0),
501 NumNick(sptr
), cli_info(sptr
));
503 /* Send user mode to client */
506 static struct Flags flags
; /* automatically initialized to zeros */
507 /* To avoid sending +r to the client due to auth-on-connect, set
508 * the "old" FLAG_ACCOUNT bit to match the client's value.
511 FlagSet(&flags
, FLAG_ACCOUNT
);
513 FlagClr(&flags
, FLAG_ACCOUNT
);
514 send_umode(cptr
, sptr
, &flags
, ALL_UMODES
);
515 if ((cli_snomask(sptr
) != SNO_DEFAULT
) && HasFlag(sptr
, FLAG_SERVNOTICE
))
516 send_reply(sptr
, RPL_SNOMASK
, cli_snomask(sptr
), cli_snomask(sptr
));
521 /** List of user mode characters. */
522 static const struct UserMode
{
523 unsigned int flag
; /**< User mode constant. */
524 char c
; /**< Character corresponding to the mode. */
528 { FLAG_INVISIBLE
, 'i' },
529 { FLAG_WALLOP
, 'w' },
530 { FLAG_SERVNOTICE
, 's' },
532 { FLAG_CHSERV
, 'k' },
534 { FLAG_ACCOUNT
, 'r' },
535 { FLAG_HIDDENHOST
, 'x' },
536 { FLAG_ACCOUNTONLY
, 'R' },
537 { FLAG_XTRAOP
, 'X' },
538 { FLAG_NOCHAN
, 'n' },
539 { FLAG_NOIDLE
, 'I' },
540 { FLAG_SETHOST
, 'h' },
541 { FLAG_PARANOID
, 'P' }
544 /** Length of #userModeList. */
545 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
548 * XXX - find a way to get rid of this
550 /** Nasty global buffer used for communications with umode_str() and others. */
551 static char umodeBuf
[BUFSIZE
];
553 /** Try to set a user's nickname.
554 * If \a sptr is a server, the client is being introduced for the first time.
555 * @param[in] cptr Client to set nickname.
556 * @param[in] sptr Client sending the NICK.
557 * @param[in] nick New nickname.
558 * @param[in] parc Number of arguments to NICK.
559 * @param[in] parv Argument list to NICK.
560 * @return CPTR_KILLED if \a cptr was killed, else 0.
562 int set_nick_name(struct Client
* cptr
, struct Client
* sptr
,
563 const char* nick
, int parc
, char* parv
[])
565 if (IsServer(sptr
)) {
567 const char* account
= 0;
573 * A server introducing a new client, change source
575 struct Client
* new_client
= make_client(cptr
, STAT_UNKNOWN
);
576 assert(0 != new_client
);
578 cli_hopcount(new_client
) = atoi(parv
[2]);
579 cli_lastnick(new_client
) = atoi(parv
[3]);
580 if (Protocol(cptr
) > 9 && parc
> 7 && *parv
[6] == '+')
582 for (p
= parv
[6] + 1; *p
; p
++)
584 for (i
= 0; i
< USERMODELIST_SIZE
; ++i
)
586 if (userModeList
[i
].c
== *p
)
588 SetFlag(new_client
, userModeList
[i
].flag
);
589 if (userModeList
[i
].flag
== FLAG_ACCOUNT
)
591 if (userModeList
[i
].flag
== FLAG_SETHOST
)
592 hostmask
= parv
[parc
- 4];
598 client_set_privs(new_client
, NULL
); /* set privs on user */
602 strcpy(cli_name(new_client
), nick
);
603 cli_user(new_client
) = make_user(new_client
);
604 cli_user(new_client
)->server
= sptr
;
605 SetRemoteNumNick(new_client
, parv
[parc
- 2]);
607 * IP# of remote client
609 base64toip(parv
[parc
- 3], &cli_ip(new_client
));
611 add_client_to_list(new_client
);
612 hAddClient(new_client
);
614 cli_serv(sptr
)->ghost
= 0; /* :server NICK means end of net.burst */
615 ircd_strncpy(cli_username(new_client
), parv
[4], USERLEN
);
616 ircd_strncpy(cli_user(new_client
)->realusername
, parv
[4], USERLEN
);
617 ircd_strncpy(cli_user(new_client
)->host
, parv
[5], HOSTLEN
);
618 ircd_strncpy(cli_user(new_client
)->realhost
, parv
[5], HOSTLEN
);
619 ircd_strncpy(cli_info(new_client
), parv
[parc
- 1], REALLEN
);
621 int len
= ACCOUNTLEN
;
622 if ((p
= strchr(account
, ':'))) {
623 len
= (p
++) - account
;
624 cli_user(new_client
)->acc_create
= atoi(p
);
625 Debug((DEBUG_DEBUG
, "Received timestamped account in user mode; "
626 "account \"%s\", timestamp %Tu", account
,
627 cli_user(new_client
)->acc_create
));
629 ircd_strncpy(cli_user(new_client
)->account
, account
, len
);
631 if (HasHiddenHost(new_client
))
632 ircd_snprintf(0, cli_user(new_client
)->host
, HOSTLEN
, "%s.%s",
633 account
, feature_str(FEAT_HIDDEN_HOST
));
634 if (HasSetHost(new_client
)) {
635 if ((host
= strrchr(hostmask
, '@')) != NULL
) {
637 ircd_strncpy(cli_username(new_client
), hostmask
, USERLEN
);
638 ircd_strncpy(cli_user(new_client
)->host
, host
, HOSTLEN
);
642 return register_user(cptr
, new_client
);
644 else if ((cli_name(sptr
))[0]) {
646 * Client changing its nick
648 * If the client belongs to me, then check to see
649 * if client is on any channels where it is currently
650 * banned. If so, do not allow the nick change to occur.
653 const char* channel_name
;
654 struct Membership
*member
;
655 if ((channel_name
= find_no_nickchange_channel(sptr
)) && !IsXtraOp(sptr
)) {
656 return send_reply(cptr
, ERR_BANNICKCHANGE
, channel_name
);
659 * Refuse nick change if the last nick change was less
660 * then 30 seconds ago. This is intended to get rid of
661 * clone bots doing NICK FLOOD. -SeKs
662 * If someone didn't change their nick for more then 60 seconds
663 * however, allow to do two nick changes immediately after another
664 * before limiting the nick flood. -Run
666 if (CurrentTime
< cli_nextnick(cptr
))
668 cli_nextnick(cptr
) += 2;
669 send_reply(cptr
, ERR_NICKTOOFAST
, parv
[1],
670 cli_nextnick(cptr
) - CurrentTime
);
671 /* Send error message */
672 sendcmdto_one(cptr
, CMD_NICK
, cptr
, "%s", cli_name(cptr
));
673 /* bounce NICK to user */
674 return 0; /* ignore nick change! */
677 /* Limit total to 1 change per NICK_DELAY seconds: */
678 cli_nextnick(cptr
) += NICK_DELAY
;
679 /* However allow _maximal_ 1 extra consecutive nick change: */
680 if (cli_nextnick(cptr
) < CurrentTime
)
681 cli_nextnick(cptr
) = CurrentTime
;
683 /* Invalidate all bans against the user so we check them again */
684 for (member
= (cli_user(cptr
))->channel
; member
;
685 member
= member
->next_channel
)
686 ClearBanValid(member
);
689 * Also set 'lastnick' to current time, if changed.
691 if (0 != ircd_strcmp(parv
[0], nick
))
692 cli_lastnick(sptr
) = (sptr
== cptr
) ? TStime() : atoi(parv
[2]);
695 * Client just changing his/her nick. If he/she is
696 * on a channel, send note of change to all clients
697 * on that channel. Propagate notice to other servers.
700 sendcmdto_common_channels_butone(sptr
, CMD_NICK
, NULL
, ":%s", nick
);
701 add_history(sptr
, 1);
702 sendcmdto_serv_butone(sptr
, CMD_NICK
, cptr
, "%s %Tu", nick
,
706 sendcmdto_one(sptr
, CMD_NICK
, sptr
, ":%s", nick
);
708 if ((cli_name(sptr
))[0])
710 strcpy(cli_name(sptr
), nick
);
714 /* Local client setting NICK the first time */
715 strcpy(cli_name(sptr
), nick
);
717 return auth_set_nick(cli_auth(sptr
), nick
);
722 /** Calculate the hash value for a target.
723 * @param[in] target Pointer to target, cast to unsigned int.
724 * @return Hash value constructed from the pointer.
726 static unsigned char hash_target(unsigned int target
)
728 return (unsigned char) (target
>> 16) ^ (target
>> 8);
731 /** Records \a target as a recent target for \a sptr.
732 * @param[in] sptr User who has sent to a new target.
733 * @param[in] target Target to add.
736 add_target(struct Client
*sptr
, void *target
)
738 /* Ok, this shouldn't work esp on alpha
740 unsigned char hash
= hash_target((unsigned long) target
);
741 unsigned char* targets
;
744 assert(cli_local(sptr
));
746 targets
= cli_targets(sptr
);
751 for (i
= 0; i
< MAXTARGETS
; ++i
) {
752 if (targets
[i
] == hash
)
758 memmove(&targets
[RESERVEDTARGETS
+ 1],
759 &targets
[RESERVEDTARGETS
], MAXTARGETS
- RESERVEDTARGETS
- 1);
760 targets
[RESERVEDTARGETS
] = hash
;
763 /** Check whether \a sptr can send to or join \a target yet.
764 * @param[in] sptr User trying to join a channel or send a message.
765 * @param[in] target Target of the join or message.
766 * @param[in] name Name of the target.
767 * @param[in] created If non-zero, trying to join a new channel.
768 * @return Non-zero if too many target changes; zero if okay to send.
770 int check_target_limit(struct Client
*sptr
, void *target
, const char *name
,
773 unsigned char hash
= hash_target((unsigned long) target
);
775 unsigned char* targets
;
778 assert(cli_local(sptr
));
779 targets
= cli_targets(sptr
);
781 /* If user is invited to channel, give him/her a free target */
782 if (IsChannelName(name
) && IsInvited(sptr
, target
))
785 /* opers always have a free target */
790 * Same target as last time?
792 if (targets
[0] == hash
)
794 for (i
= 1; i
< MAXTARGETS
; ++i
) {
795 if (targets
[i
] == hash
) {
796 memmove(&targets
[1], &targets
[0], i
);
805 if (CurrentTime
< cli_nexttarget(sptr
)) {
806 if (cli_nexttarget(sptr
) - CurrentTime
< TARGET_DELAY
+ 8) {
810 cli_nexttarget(sptr
) += 2;
811 send_reply(sptr
, ERR_TARGETTOOFAST
, name
,
812 cli_nexttarget(sptr
) - CurrentTime
);
817 cli_nexttarget(sptr
) += TARGET_DELAY
;
818 if (cli_nexttarget(sptr
) < CurrentTime
- (TARGET_DELAY
* (MAXTARGETS
- 1)))
819 cli_nexttarget(sptr
) = CurrentTime
- (TARGET_DELAY
* (MAXTARGETS
- 1));
822 memmove(&targets
[1], &targets
[0], MAXTARGETS
- 1);
827 /** Allows a channel operator to avoid target change checks when
828 * sending messages to users on their channel.
829 * @param[in] source User sending the message.
830 * @param[in] nick Destination of the message.
831 * @param[in] channel Name of channel being sent to.
832 * @param[in] text Message to send.
833 * @param[in] is_notice If non-zero, use CNOTICE instead of CPRIVMSG.
835 /* Added 971023 by Run. */
836 int whisper(struct Client
* source
, const char* nick
, const char* channel
,
837 const char* text
, int is_notice
)
840 struct Channel
* chptr
;
841 struct Membership
* membership
;
845 assert(0 != channel
);
846 assert(MyUser(source
));
848 if (!(dest
= FindUser(nick
))) {
849 return send_reply(source
, ERR_NOSUCHNICK
, nick
);
851 if (!(chptr
= FindChannel(channel
))) {
852 return send_reply(source
, ERR_NOSUCHCHANNEL
, channel
);
855 * compare both users channel lists, instead of the channels user list
856 * since the link is the same, this should be a little faster for channels
857 * with a lot of users
859 for (membership
= cli_user(source
)->channel
; membership
; membership
= membership
->next_channel
) {
860 if (chptr
== membership
->channel
)
863 if (0 == membership
) {
864 return send_reply(source
, ERR_NOTONCHANNEL
, chptr
->chname
);
866 if (!IsVoicedOrOpped(membership
)) {
867 return send_reply(source
, ERR_VOICENEEDED
, chptr
->chname
);
870 * lookup channel in destination
872 assert(0 != cli_user(dest
));
873 for (membership
= cli_user(dest
)->channel
; membership
; membership
= membership
->next_channel
) {
874 if (chptr
== membership
->channel
)
877 if (0 == membership
|| IsZombie(membership
)) {
878 return send_reply(source
, ERR_USERNOTINCHANNEL
, cli_name(dest
), chptr
->chname
);
880 if (is_silenced(source
, dest
))
883 if (cli_user(dest
)->away
)
884 send_reply(source
, RPL_AWAY
, cli_name(dest
), cli_user(dest
)->away
);
886 sendcmdto_one(source
, CMD_NOTICE
, dest
, "%C :%s", dest
, text
);
888 sendcmdto_one(source
, CMD_PRIVATE
, dest
, "%C :%s", dest
, text
);
893 /** Send a user mode change for \a cptr to neighboring servers.
894 * @param[in] cptr User whose mode is changing.
895 * @param[in] sptr Client who sent us the mode change message.
896 * @param[in] old Prior set of user flags.
897 * @param[in] prop If non-zero, also include FLAG_OPER.
899 void send_umode_out(struct Client
*cptr
, struct Client
*sptr
,
900 struct Flags
*old
, int prop
)
903 struct Client
*acptr
;
905 send_umode(NULL
, sptr
, old
, prop
? SEND_UMODES
: SEND_UMODES_BUT_OPER
);
907 for (i
= HighestFd
; i
>= 0; i
--)
909 if ((acptr
= LocalClientArray
[i
]) && IsServer(acptr
) &&
910 (acptr
!= cptr
) && (acptr
!= sptr
) && *umodeBuf
)
911 sendcmdto_one(sptr
, CMD_MODE
, acptr
, "%s %s", cli_name(sptr
), umodeBuf
);
913 if (cptr
&& MyUser(cptr
))
914 send_umode(cptr
, sptr
, old
, ALL_UMODES
);
918 /** Call \a fmt for each Client named in \a names.
919 * @param[in] sptr Client requesting information.
920 * @param[in] names Space-delimited list of nicknames.
921 * @param[in] rpl Base reply string for messages.
922 * @param[in] fmt Formatting callback function.
924 void send_user_info(struct Client
* sptr
, char* names
, int rpl
, InfoFormatter fmt
)
930 struct Client
* acptr
;
937 mb
= msgq_make(sptr
, rpl_str(rpl
), cli_name(&me
), cli_name(sptr
));
939 for (name
= ircd_strtok(&p
, names
, " "); name
; name
= ircd_strtok(&p
, 0, " ")) {
940 if ((acptr
= FindUser(name
))) {
942 msgq_append(0, mb
, " ");
943 (*fmt
)(acptr
, sptr
, mb
);
945 if (5 == ++arg_count
)
948 send_buffer(sptr
, mb
, 0);
952 /** Set \a flag on \a cptr and possibly hide the client's hostmask.
953 * @param[in,out] cptr User who is getting a new flag.
954 * @param[in] flag Some flag that affects host-hiding (FLAG_HIDDENHOST, FLAG_ACCOUNT).
958 hide_hostmask(struct Client
*cptr
, unsigned int flag
)
960 struct Membership
*chan
;
963 case FLAG_HIDDENHOST
:
964 /* Local users cannot set +x unless FEAT_HOST_HIDING is true. */
965 if (MyConnect(cptr
) && !feature_bool(FEAT_HOST_HIDING
))
967 /* If the user is +h, we don't hide the hostmask. Set the flag to keep sync though */
968 if (HasSetHost(cptr
)) {
974 /* Invalidate all bans against the user so we check them again */
975 for (chan
= (cli_user(cptr
))->channel
; chan
;
976 chan
= chan
->next_channel
)
984 if (!HasFlag(cptr
, FLAG_HIDDENHOST
) || !HasFlag(cptr
, FLAG_ACCOUNT
))
987 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Registered");
988 ircd_snprintf(0, cli_user(cptr
)->host
, HOSTLEN
, "%s.%s",
989 cli_user(cptr
)->account
, feature_str(FEAT_HIDDEN_HOST
));
991 /* ok, the client is now fully hidden, so let them know -- hikari */
993 send_reply(cptr
, RPL_HOSTHIDDEN
, cli_user(cptr
)->host
);
996 * Go through all channels the client was on, rejoin him
997 * and set the modes, if any
999 for (chan
= cli_user(cptr
)->channel
; chan
; chan
= chan
->next_channel
)
1003 /* Send a JOIN unless the user's join has been delayed. */
1004 if (!IsDelayedJoin(chan
))
1005 sendcmdto_channel_butserv_butone(cptr
, CMD_JOIN
, chan
->channel
, cptr
, 0,
1006 "%H", chan
->channel
);
1007 if (IsChanOp(chan
) && HasVoice(chan
))
1008 sendcmdto_channel_butserv_butone(&his
, CMD_MODE
, chan
->channel
, cptr
, 0,
1009 "%H +ov %C %C", chan
->channel
, cptr
,
1011 else if (IsChanOp(chan
) || HasVoice(chan
))
1012 sendcmdto_channel_butserv_butone(&his
, CMD_MODE
, chan
->channel
, cptr
, 0,
1013 "%H +%c %C", chan
->channel
, IsChanOp(chan
) ? 'o' : 'v', cptr
);
1019 * set_hostmask() - derived from hide_hostmask()
1022 int set_hostmask(struct Client
*cptr
, char *hostmask
, char *password
)
1026 char *host
, *new_vhost
, *vhost_pass
;
1027 char hiddenhost
[USERLEN
+ HOSTLEN
+ 2];
1028 struct Membership
*chan
;
1030 Debug((DEBUG_INFO
, "set_hostmask() %C, %s, %s", cptr
, hostmask
, password
));
1032 /* sethost enabled? */
1033 if (MyConnect(cptr
) && !feature_bool(FEAT_SETHOST
)) {
1034 send_reply(cptr
, ERR_DISABLED
, "SETHOST");
1038 /* sethost enabled for users? */
1039 if (MyConnect(cptr
) && !IsAnOper(cptr
) && !feature_bool(FEAT_SETHOST_USER
)) {
1040 send_reply(cptr
, ERR_NOPRIVILEGES
);
1044 /* MODE_DEL: restore original hostmask */
1045 if (EmptyString(hostmask
)) {
1046 /* is already sethost'ed? */
1047 if (IsSetHost(cptr
)) {
1049 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Host change");
1050 /* If they are +rx, we need to return to their +x host, not their "real" host */
1051 if (HasHiddenHost(cptr
))
1052 ircd_snprintf(0, cli_user(cptr
)->host
, HOSTLEN
, "%s.%s",
1053 cli_user(cptr
)->account
, feature_str(FEAT_HIDDEN_HOST
));
1055 strncpy(cli_user(cptr
)->host
, cli_user(cptr
)->realhost
, HOSTLEN
);
1056 strncpy(cli_user(cptr
)->username
, cli_user(cptr
)->realusername
, USERLEN
);
1058 if (MyConnect(cptr
))
1059 log_write(LS_SETHOST
, L_INFO
, LOG_NOSNOTICE
,
1060 "SETHOST (%s@%s) by (%#R): restoring real hostmask",
1061 cli_user(cptr
)->username
, cli_user(cptr
)->host
, cptr
);
1064 /* MODE_ADD: set a new hostmask */
1066 /* chop up ident and host.cc */
1067 if ((host
= strrchr(hostmask
, '@'))) /* oper can specifiy ident@host.cc */
1069 else /* user can only specifiy host.cc [password] */
1074 if (MyConnect(cptr
)) {
1075 if (IsAnOper(cptr
)) {
1076 if ((new_vhost
= IsVhost(host
, 1)) == NULL
) {
1077 if (!feature_bool(FEAT_SETHOST_FREEFORM
)) {
1078 send_reply(cptr
, ERR_HOSTUNAVAIL
, hostmask
);
1079 log_write(LS_SETHOST
, L_INFO
, LOG_NOSNOTICE
,
1080 "SETHOST (%s@%s) by (%#R): no such s-line",
1081 (host
!= hostmask
) ? hostmask
: cli_user(cptr
)->username
, host
, cptr
);
1083 } else /* freeform active, log and go */
1086 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Host change");
1087 /* set the new ident and host */
1088 if (host
!= hostmask
) /* oper only specified host.cc */
1089 strncpy(cli_user(cptr
)->username
, hostmask
, USERLEN
);
1090 strncpy(cli_user(cptr
)->host
, host
, HOSTLEN
);
1092 log_write(LS_SETHOST
, (freeform
) ? L_NOTICE
: L_INFO
,
1093 (freeform
) ? 0 : LOG_NOSNOTICE
, "SETHOST (%s@%s) by (%#R)%s",
1094 cli_user(cptr
)->username
, cli_user(cptr
)->host
, cptr
,
1095 (freeform
) ? ": using freeform" : "");
1097 * plain user sethost, handled here
1100 /* empty password? */
1101 if (EmptyString(password
)) {
1102 send_reply(cptr
, ERR_NEEDMOREPARAMS
, "MODE");
1105 /* no such s-line */
1106 if ((new_vhost
= IsVhost(host
, 0)) == NULL
) {
1107 send_reply(cptr
, ERR_HOSTUNAVAIL
, hostmask
);
1108 log_write(LS_SETHOST
, L_INFO
, LOG_NOSNOTICE
, "SETHOST (%s@%s %s) by (%#R): no such s-line",
1109 cli_user(cptr
)->username
, host
, password
, cptr
);
1113 if ((vhost_pass
= IsVhostPass(new_vhost
)) == NULL
) {
1114 send_reply(cptr
, ERR_PASSWDMISMATCH
);
1115 log_write(LS_SETHOST
, L_INFO
, 0, "SETHOST (%s@%s %s) by (%#R): trying to use an oper s-line",
1116 cli_user(cptr
)->username
, host
, password
, cptr
);
1119 /* incorrect password */
1120 if (strCasediff(vhost_pass
, password
)) {
1121 send_reply(cptr
, ERR_PASSWDMISMATCH
);
1122 log_write(LS_SETHOST
, L_NOTICE
, 0, "SETHOST (%s@%s %s) by (%#R): incorrect password",
1123 cli_user(cptr
)->username
, host
, password
, cptr
);
1126 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Host change");
1127 /* set the new host */
1128 strncpy(cli_user(cptr
)->host
, new_vhost
, HOSTLEN
);
1130 log_write(LS_SETHOST
, L_INFO
, LOG_NOSNOTICE
, "SETHOST (%s@%s) by (%#R)",
1131 cli_user(cptr
)->username
, cli_user(cptr
)->host
, cptr
);
1133 } else { /* remote user */
1134 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Host change");
1135 if (host
!= hostmask
) /* oper only specified host.cc */
1136 strncpy(cli_user(cptr
)->username
, hostmask
, USERLEN
);
1137 strncpy(cli_user(cptr
)->host
, host
, HOSTLEN
);
1146 if (MyConnect(cptr
)) {
1147 ircd_snprintf(0, hiddenhost
, HOSTLEN
+ USERLEN
+ 2, "%s@%s",
1148 cli_user(cptr
)->username
, cli_user(cptr
)->host
);
1149 send_reply(cptr
, RPL_HOSTHIDDEN
, hiddenhost
);
1153 /* Code copied from hide_hostmask(). This is the old (pre-delayedjoin)
1154 * version. Switch this in if you're not using the delayed join patch. */
1156 * Go through all channels the client was on, rejoin him
1157 * and set the modes, if any
1159 for (chan
= cli_user(cptr
)->channel
; chan
; chan
= chan
->next_channel
) {
1162 sendcmdto_channel_butserv_butone(cptr
, CMD_JOIN
, chan
->channel
, cptr
,
1163 "%H", chan
->channel
);
1164 if (IsChanOp(chan
) && HasVoice(chan
)) {
1165 sendcmdto_channel_butserv_butone(&me
, CMD_MODE
, chan
->channel
, cptr
,
1166 "%H +ov %C %C", chan
->channel
, cptr
, cptr
);
1167 } else if (IsChanOp(chan
) || HasVoice(chan
)) {
1168 sendcmdto_channel_butserv_butone(&me
, CMD_MODE
, chan
->channel
, cptr
,
1169 "%H +%c %C", chan
->channel
, IsChanOp(chan
) ? 'o' : 'v', cptr
);
1175 * Go through all channels the client was on, rejoin him
1176 * and set the modes, if any
1178 for (chan
= cli_user(cptr
)->channel
; chan
; chan
= chan
->next_channel
) {
1181 /* If this channel has delayed joins and the user has no modes, just set
1182 * the delayed join flag rather than showing the join, even if the user
1183 * was visible before */
1184 if (!IsChanOp(chan
) && !HasVoice(chan
)
1185 && (chan
->channel
->mode
.mode
& MODE_DELJOINS
)) {
1186 SetDelayedJoin(chan
);
1188 sendcmdto_channel_butserv_butone(cptr
, CMD_JOIN
, chan
->channel
, cptr
, 0,
1189 "%H", chan
->channel
);
1191 if (IsChanOp(chan
) && HasVoice(chan
)) {
1192 sendcmdto_channel_butserv_butone(&me
, CMD_MODE
, chan
->channel
, cptr
, 0,
1193 "%H +ov %C %C", chan
->channel
, cptr
, cptr
);
1194 } else if (IsChanOp(chan
) || HasVoice(chan
)) {
1195 sendcmdto_channel_butserv_butone(&me
, CMD_MODE
, chan
->channel
, cptr
, 0,
1196 "%H +%c %C", chan
->channel
, IsChanOp(chan
) ? 'o' : 'v', cptr
);
1202 /** Set a user's mode. This function checks that \a cptr is trying to
1203 * set his own mode, prevents local users from setting inappropriate
1204 * modes through this function, and applies any other side effects of
1205 * a successful mode change.
1207 * @param[in,out] cptr User setting someone's mode.
1208 * @param[in] sptr Client who sent the mode change message.
1209 * @param[in] parc Number of parameters in \a parv.
1210 * @param[in] parv Parameters to MODE.
1213 int set_user_mode(struct Client
*cptr
, struct Client
*sptr
, int parc
, char *parv
[])
1217 struct Client
*acptr
;
1220 struct Flags setflags
;
1221 unsigned int tmpmask
= 0;
1222 int snomask_given
= 0;
1224 char *hostmask
, *password
;
1226 int do_host_hiding
= 0;
1227 int do_set_host
= 0;
1229 hostmask
= password
= NULL
;
1233 return need_more_params(sptr
, "MODE");
1235 if (!(acptr
= FindUser(parv
[1])))
1237 if (MyConnect(sptr
))
1238 send_reply(sptr
, ERR_NOSUCHCHANNEL
, parv
[1]);
1242 if (IsServer(sptr
) || sptr
!= acptr
)
1245 sendwallto_group_butone(&me
, WALL_WALLOPS
, 0,
1246 "MODE for User %s from %s!%s", parv
[1],
1247 cli_name(cptr
), cli_name(sptr
));
1249 send_reply(sptr
, ERR_USERSDONTMATCH
);
1257 for (i
= 0; i
< USERMODELIST_SIZE
; i
++)
1259 if (HasFlag(sptr
, userModeList
[i
].flag
) &&
1260 ((userModeList
[i
].flag
!= FLAG_ACCOUNT
) &&
1261 (userModeList
[i
].flag
!= FLAG_SETHOST
)))
1262 *m
++ = userModeList
[i
].c
;
1265 send_reply(sptr
, RPL_UMODEIS
, buf
);
1266 if (HasFlag(sptr
, FLAG_SERVNOTICE
) && MyConnect(sptr
)
1267 && cli_snomask(sptr
) !=
1268 (unsigned int)(IsOper(sptr
) ? SNO_OPERDEFAULT
: SNO_DEFAULT
))
1269 send_reply(sptr
, RPL_SNOMASK
, cli_snomask(sptr
), cli_snomask(sptr
));
1274 * find flags already set for user
1275 * why not just copy them?
1277 setflags
= cli_flags(sptr
);
1279 if (MyConnect(sptr
))
1280 tmpmask
= cli_snomask(sptr
);
1283 * parse mode change string(s)
1285 for (p
= &parv
[2]; *p
; p
++) { /* p is changed in loop too */
1286 for (m
= *p
; *m
; m
++) {
1295 if (*(p
+ 1) && is_snomask(*(p
+ 1))) {
1297 tmpmask
= umode_make_snomask(tmpmask
, *++p
, what
);
1298 tmpmask
&= (IsAnOper(sptr
) ? SNO_ALL
: SNO_USER
);
1301 tmpmask
= (what
== MODE_ADD
) ?
1302 (IsAnOper(sptr
) ? SNO_OPERDEFAULT
: SNO_DEFAULT
) : 0;
1304 SetServNotice(sptr
);
1306 ClearServNotice(sptr
);
1309 if (what
== MODE_ADD
)
1315 if (what
== MODE_ADD
)
1318 ClrFlag(sptr
, FLAG_OPER
);
1319 ClrFlag(sptr
, FLAG_LOCOP
);
1320 if (MyConnect(sptr
))
1322 tmpmask
= cli_snomask(sptr
) & ~SNO_OPER
;
1323 cli_handler(sptr
) = CLIENT_HANDLER
;
1328 if (what
== MODE_ADD
)
1332 ClrFlag(sptr
, FLAG_OPER
);
1333 ClrFlag(sptr
, FLAG_LOCOP
);
1334 if (MyConnect(sptr
))
1336 tmpmask
= cli_snomask(sptr
) & ~SNO_OPER
;
1337 cli_handler(sptr
) = CLIENT_HANDLER
;
1342 if (what
== MODE_ADD
)
1345 if (!feature_bool(FEAT_AUTOINVISIBLE
) || IsOper(sptr
)) /* Don't allow non-opers to -i if FEAT_AUTOINVISIBLE is set */
1346 ClearInvisible(sptr
);
1349 if (what
== MODE_ADD
)
1355 if (what
== MODE_ADD
)
1356 SetChannelService(sptr
);
1358 ClearChannelService(sptr
);
1361 if (what
== MODE_ADD
)
1367 if (what
== MODE_ADD
)
1373 if (what
== MODE_ADD
)
1379 if (what
== MODE_ADD
)
1385 if (what
== MODE_ADD
)
1389 if (what
== MODE_ADD
) {
1390 if (*(p
+ 1) && is_hostmask(*(p
+ 1))) {
1393 /* DON'T step p onto the trailing NULL in the parameter array! - splidge */
1400 send_reply(sptr
, ERR_NEEDMOREPARAMS
, "SETHOST");
1402 send_reply(sptr
, ERR_BADHOSTMASK
, *(p
+1));
1403 p
++; /* Swallow the arg anyway */
1406 } else { /* MODE_DEL */
1413 if (what
== MODE_ADD
)
1414 SetAccountOnly(sptr
);
1416 ClearAccountOnly(sptr
);
1419 if (what
== MODE_ADD
)
1422 ClearParanoid(sptr
);
1425 send_reply(sptr
, ERR_UMODEUNKNOWNFLAG
, *m
);
1431 * Evaluate rules for new user mode
1432 * Stop users making themselves operators too easily:
1434 if (!IsServer(cptr
))
1436 if (!FlagHas(&setflags
, FLAG_OPER
) && IsOper(sptr
))
1438 if (!FlagHas(&setflags
, FLAG_LOCOP
) && IsLocOp(sptr
))
1441 * new umode; servers can set it, local users cannot;
1442 * prevents users from /kick'ing or /mode -o'ing
1444 if (!FlagHas(&setflags
, FLAG_CHSERV
) && !IsOper(sptr
))
1445 ClearChannelService(sptr
);
1446 if (!FlagHas(&setflags
, FLAG_XTRAOP
) && !IsOper(sptr
))
1448 if (!FlagHas(&setflags
, FLAG_NOCHAN
) && !(IsOper(sptr
) || feature_bool(FEAT_USER_HIDECHANS
)))
1450 if (!FlagHas(&setflags
, FLAG_NOIDLE
) && !IsOper(sptr
))
1452 if (!FlagHas(&setflags
, FLAG_PARANOID
) && !IsOper(sptr
))
1453 ClearParanoid(sptr
);
1456 * only send wallops to opers
1458 if (feature_bool(FEAT_WALLOPS_OPER_ONLY
) && !IsAnOper(sptr
) &&
1459 !FlagHas(&setflags
, FLAG_WALLOP
))
1461 if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY
) && MyConnect(sptr
) &&
1462 !IsAnOper(sptr
) && !FlagHas(&setflags
, FLAG_SERVNOTICE
))
1464 ClearServNotice(sptr
);
1465 set_snomask(sptr
, 0, SNO_SET
);
1467 if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY
) &&
1468 !IsAnOper(sptr
) && !FlagHas(&setflags
, FLAG_DEBUG
))
1471 if (MyConnect(sptr
))
1473 if ((FlagHas(&setflags
, FLAG_OPER
) || FlagHas(&setflags
, FLAG_LOCOP
)) &&
1475 det_confs_butmask(sptr
, CONF_CLIENT
& ~CONF_OPERATOR
);
1477 if (SendServNotice(sptr
))
1479 if (tmpmask
!= cli_snomask(sptr
))
1480 set_snomask(sptr
, tmpmask
, SNO_SET
);
1481 if (cli_snomask(sptr
) && snomask_given
)
1482 send_reply(sptr
, RPL_SNOMASK
, cli_snomask(sptr
), cli_snomask(sptr
));
1485 set_snomask(sptr
, 0, SNO_SET
);
1488 * Compare new flags with old flags and send string which
1489 * will cause servers to update correctly.
1491 if (!FlagHas(&setflags
, FLAG_OPER
) && IsOper(sptr
))
1495 client_set_privs(sptr
, NULL
); /* may set propagate privilege */
1497 /* remember propagate privilege setting */
1498 if (HasPriv(sptr
, PRIV_PROPAGATE
))
1500 if (FlagHas(&setflags
, FLAG_OPER
) && !IsOper(sptr
))
1502 /* user no longer oper */
1504 client_set_privs(sptr
, NULL
); /* will clear propagate privilege */
1506 if (FlagHas(&setflags
, FLAG_INVISIBLE
) && !IsInvisible(sptr
))
1507 --UserStats
.inv_clients
;
1508 if (!FlagHas(&setflags
, FLAG_INVISIBLE
) && IsInvisible(sptr
))
1509 ++UserStats
.inv_clients
;
1510 if (!FlagHas(&setflags
, FLAG_HIDDENHOST
) && do_host_hiding
)
1511 hide_hostmask(sptr
, FLAG_HIDDENHOST
);
1513 /* We clear the flag in the old mask, so that the +h will be sent */
1514 /* Only do this if we're SETTING +h and it succeeded */
1515 if (set_hostmask(sptr
, hostmask
, password
) && hostmask
)
1516 FlagClr(&setflags
, FLAG_SETHOST
);
1518 send_umode_out(cptr
, sptr
, &setflags
, prop
);
1523 /** Build a mode string to describe modes for \a cptr.
1524 * @param[in] cptr Some user.
1525 * @return Pointer to a static buffer.
1527 char *umode_str(struct Client
*cptr
)
1529 /* Maximum string size: "owidgrx\0" */
1532 struct Flags c_flags
= cli_flags(cptr
);
1534 if (!HasPriv(cptr
, PRIV_PROPAGATE
))
1535 FlagClr(&c_flags
, FLAG_OPER
);
1537 for (i
= 0; i
< USERMODELIST_SIZE
; ++i
)
1539 if (FlagHas(&c_flags
, userModeList
[i
].flag
) &&
1540 userModeList
[i
].flag
>= FLAG_GLOBAL_UMODES
)
1541 *m
++ = userModeList
[i
].c
;
1544 if (IsAccount(cptr
))
1546 char* t
= cli_user(cptr
)->account
;
1549 while ((*m
++ = *t
++))
1552 if (cli_user(cptr
)->acc_create
) {
1554 Debug((DEBUG_DEBUG
, "Sending timestamped account in user mode for "
1555 "account \"%s\"; timestamp %Tu", cli_user(cptr
)->account
,
1556 cli_user(cptr
)->acc_create
));
1557 ircd_snprintf(0, t
= nbuf
, sizeof(nbuf
), ":%Tu",
1558 cli_user(cptr
)->acc_create
);
1559 m
--; /* back up over previous nul-termination */
1560 while ((*m
++ = *t
++))
1563 m
--; /* Step back over the '\0' */
1566 if (IsSetHost(cptr
)) {
1568 ircd_snprintf(0, m
, USERLEN
+ HOSTLEN
+ 2, "%s@%s", cli_user(cptr
)->username
,
1569 cli_user(cptr
)->host
);
1572 return umodeBuf
; /* Note: static buffer, gets
1573 overwritten by send_umode() */
1576 /** Send a mode change string for \a sptr to \a cptr.
1577 * @param[in] cptr Destination of mode change message.
1578 * @param[in] sptr User whose mode has changed.
1579 * @param[in] old Pre-change set of modes for \a sptr.
1580 * @param[in] sendset One of ALL_UMODES, SEND_UMODES_BUT_OPER,
1581 * SEND_UMODES, to select which changed user modes to send.
1583 void send_umode(struct Client
*cptr
, struct Client
*sptr
, struct Flags
*old
,
1590 int what
= MODE_NULL
;
1593 * Build a string in umodeBuf to represent the change in the user's
1594 * mode between the new (cli_flags(sptr)) and 'old', but skipping
1595 * the modes indicated by sendset.
1599 for (i
= 0; i
< USERMODELIST_SIZE
; ++i
)
1601 flag
= userModeList
[i
].flag
;
1602 if (FlagHas(old
, flag
)
1603 == HasFlag(sptr
, flag
))
1609 case SEND_UMODES_BUT_OPER
:
1610 if (flag
== FLAG_OPER
)
1612 /* and fall through */
1614 if (flag
< FLAG_GLOBAL_UMODES
)
1618 /* Special case for SETHOST.. */
1619 if (flag
== FLAG_SETHOST
) {
1620 /* Don't send to users */
1621 if (cptr
&& MyUser(cptr
))
1624 /* If we're setting +h, add the parameter later */
1625 if (!FlagHas(old
, flag
))
1628 if (FlagHas(old
, flag
))
1630 if (what
== MODE_DEL
)
1631 *m
++ = userModeList
[i
].c
;
1636 *m
++ = userModeList
[i
].c
;
1639 else /* !FlagHas(old, flag) */
1641 if (what
== MODE_ADD
)
1642 *m
++ = userModeList
[i
].c
;
1647 *m
++ = userModeList
[i
].c
;
1653 ircd_snprintf(0, m
, USERLEN
+ HOSTLEN
+ 1, "%s@%s", cli_user(sptr
)->username
,
1654 cli_user(sptr
)->host
);
1657 if (*umodeBuf
&& cptr
)
1658 sendcmdto_one(sptr
, CMD_MODE
, cptr
, "%s %s", cli_name(sptr
), umodeBuf
);
1662 * Check to see if this resembles a sno_mask. It is if 1) there is
1663 * at least one digit and 2) The first digit occurs before the first
1664 * alphabetic character.
1665 * @param[in] word Word to check for sno_mask-ness.
1666 * @return Non-zero if \a word looks like a server notice mask; zero if not.
1668 int is_snomask(char *word
)
1672 for (; *word
; word
++)
1675 else if (IsAlpha(*word
))
1682 * Check to see if it resembles a valid hostmask.
1684 int is_hostmask(char *word
)
1689 Debug((DEBUG_INFO
, "is_hostmask() %s", word
));
1691 if (strlen(word
) > (HOSTLEN
+ USERLEN
+ 1) || strlen(word
) <= 0)
1694 /* if a host is specified, make sure it's valid */
1695 host
= strrchr(word
, '@');
1697 if (strlen(++host
) < 1)
1699 if (strlen(host
) > HOSTLEN
)
1704 if ('@' == *word
) /* no leading @'s */
1707 if ('#' == *word
) { /* numeric index given? */
1708 for (word
++; *word
; word
++) {
1709 if (!IsDigit(*word
))
1715 /* normal hostmask, account for at most one '@' */
1716 for (; *word
; word
++) {
1721 if (!IsHostChar(*word
))
1724 return (1 < i
) ? 0 : 1; /* no more than on '@' */
1730 * IsVhost() - Check if given host is a valid spoofhost
1731 * (ie: configured thru a S:line)
1733 static char *IsVhost(char *hostmask
, int oper
)
1735 unsigned int i
= 0, y
= 0;
1736 struct sline
*sconf
;
1738 Debug((DEBUG_INFO
, "IsVhost() %s", hostmask
));
1740 if (EmptyString(hostmask
))
1743 /* spoofhost specified as index, ie: #27 */
1744 if ('#' == hostmask
[0]) {
1745 y
= atoi(hostmask
+ 1);
1746 for (i
= 0, sconf
= GlobalSList
; sconf
; sconf
= sconf
->next
) {
1747 if (!oper
&& EmptyString(sconf
->passwd
))
1750 return sconf
->spoofhost
;
1755 /* spoofhost specified as host, ie: host.cc */
1756 for (sconf
= GlobalSList
; sconf
; sconf
= sconf
->next
)
1757 if (strCasediff(hostmask
, sconf
->spoofhost
) == 0)
1758 return sconf
->spoofhost
;
1764 * IsVhostPass() - Check if given spoofhost has a password
1765 * associated with it, and if, return the password (cleartext)
1767 static char *IsVhostPass(char *hostmask
)
1769 struct sline
*sconf
;
1771 Debug((DEBUG_INFO
, "IsVhostPass() %s", hostmask
));
1773 if (EmptyString(hostmask
))
1776 for (sconf
= GlobalSList
; sconf
; sconf
= sconf
->next
)
1777 if (strCasediff(hostmask
, sconf
->spoofhost
) == 0) {
1778 Debug((DEBUG_INFO
, "sconf->passwd %s", sconf
->passwd
));
1779 return EmptyString(sconf
->passwd
) ? NULL
: sconf
->passwd
;
1785 /** Update snomask \a oldmask according to \a arg and \a what.
1786 * @param[in] oldmask Original user mask.
1787 * @param[in] arg Update string (either a number or '+'/'-' followed by a number).
1788 * @param[in] what MODE_ADD if adding the mask.
1789 * @return New value of service notice mask.
1791 unsigned int umode_make_snomask(unsigned int oldmask
, char *arg
, int what
)
1793 unsigned int sno_what
;
1794 unsigned int newmask
;
1798 if (what
== MODE_ADD
)
1803 else if (*arg
== '-')
1806 if (what
== MODE_ADD
)
1812 sno_what
= (what
== MODE_ADD
) ? SNO_SET
: SNO_DEL
;
1813 /* pity we don't have strtoul everywhere */
1814 newmask
= (unsigned int)atoi(arg
);
1815 if (sno_what
== SNO_DEL
)
1816 newmask
= oldmask
& ~newmask
;
1817 else if (sno_what
== SNO_ADD
)
1822 /** Remove \a cptr from the singly linked list \a list.
1823 * @param[in] cptr Client to remove from list.
1824 * @param[in,out] list Pointer to head of list containing \a cptr.
1826 static void delfrom_list(struct Client
*cptr
, struct SLink
**list
)
1829 struct SLink
* prv
= NULL
;
1831 for (tmp
= *list
; tmp
; tmp
= tmp
->next
) {
1832 if (tmp
->value
.cptr
== cptr
) {
1834 prv
->next
= tmp
->next
;
1844 /** Set \a cptr's server notice mask, according to \a what.
1845 * @param[in,out] cptr Client whose snomask is updating.
1846 * @param[in] newmask Base value for new snomask.
1847 * @param[in] what One of SNO_ADD, SNO_DEL, SNO_SET, to choose operation.
1849 void set_snomask(struct Client
*cptr
, unsigned int newmask
, int what
)
1851 unsigned int oldmask
, diffmask
; /* unsigned please */
1855 oldmask
= cli_snomask(cptr
);
1857 if (what
== SNO_ADD
)
1859 else if (what
== SNO_DEL
)
1860 newmask
= oldmask
& ~newmask
;
1861 else if (what
!= SNO_SET
) /* absolute set, no math needed */
1862 sendto_opmask_butone(0, SNO_OLDSNO
, "setsnomask called with %d ?!", what
);
1864 newmask
&= (IsAnOper(cptr
) ? SNO_ALL
: SNO_USER
);
1866 diffmask
= oldmask
^ newmask
;
1868 for (i
= 0; diffmask
>> i
; i
++) {
1869 if (((diffmask
>> i
) & 1))
1871 if (((newmask
>> i
) & 1))
1874 tmp
->next
= opsarray
[i
];
1875 tmp
->value
.cptr
= cptr
;
1879 /* not real portable :( */
1880 delfrom_list(cptr
, &opsarray
[i
]);
1883 cli_snomask(cptr
) = newmask
;
1886 /** Check whether \a sptr is allowed to send a message to \a acptr.
1887 * If \a sptr is a remote user, it means some server has an outdated
1888 * SILENCE list for \a acptr, so send the missing SILENCE mask(s) back
1889 * in the direction of \a sptr. Skip the check if \a sptr is a server.
1890 * @param[in] sptr Client trying to send a message.
1891 * @param[in] acptr Destination of message.
1892 * @return Non-zero if \a sptr is SILENCEd by \a acptr, zero if not.
1894 int is_silenced(struct Client
*sptr
, struct Client
*acptr
)
1898 size_t buf_used
, slen
;
1901 if (IsServer(sptr
) || !(user
= cli_user(acptr
))
1902 || !(found
= find_ban(sptr
, user
->silence
)))
1904 assert(!(found
->flags
& BAN_EXCEPTION
));
1905 if (!MyConnect(sptr
)) {
1906 /* Buffer positive silence to send back. */
1907 buf_used
= strlen(found
->banstr
);
1908 memcpy(buf
, found
->banstr
, buf_used
);
1909 /* Add exceptions to buffer. */
1910 for (found
= user
->silence
; found
; found
= found
->next
) {
1911 if (!(found
->flags
& BAN_EXCEPTION
))
1913 slen
= strlen(found
->banstr
);
1914 if (buf_used
+ slen
+ 4 > 400) {
1915 buf
[buf_used
] = '\0';
1916 sendcmdto_one(acptr
, CMD_SILENCE
, cli_from(sptr
), "%C %s", sptr
, buf
);
1920 buf
[buf_used
++] = ',';
1921 buf
[buf_used
++] = '+';
1922 buf
[buf_used
++] = '~';
1923 memcpy(buf
+ buf_used
, found
->banstr
, slen
);
1926 /* Flush silence buffer. */
1928 buf
[buf_used
] = '\0';
1929 sendcmdto_one(acptr
, CMD_SILENCE
, cli_from(sptr
), "%C %s", sptr
, buf
);
1936 /** Send RPL_ISUPPORT lines to \a cptr.
1937 * @param[in] cptr Client to send ISUPPORT to.
1941 send_supported(struct Client
*cptr
)
1943 char featurebuf
[512];
1945 ircd_snprintf(0, featurebuf
, sizeof(featurebuf
), FEATURES1
, FEATURESVALUES1
);
1946 send_reply(cptr
, RPL_ISUPPORT
, featurebuf
);
1947 ircd_snprintf(0, featurebuf
, sizeof(featurebuf
), FEATURES2
, FEATURESVALUES2
);
1948 send_reply(cptr
, RPL_ISUPPORT
, featurebuf
);
1950 return 0; /* convenience return, if it's ever needed */