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 /* Have to set up "realusername" */
359 ircd_strncpy(user
->realusername
, user
->username
, USERLEN
);
361 Count_unknownbecomesclient(sptr
, UserStats
);
363 if (MyConnect(sptr
) && feature_bool(FEAT_AUTOINVISIBLE
))
366 if(MyConnect(sptr
) && feature_bool(FEAT_SETHOST_AUTO
)) {
367 if (conf_check_slines(sptr
)) {
368 send_reply(sptr
, RPL_USINGSLINE
);
374 cli_handler(sptr
) = CLIENT_HANDLER
;
375 SetLocalNumNick(sptr
);
378 feature_str(FEAT_NETWORK
),
379 feature_str(FEAT_PROVIDER
) ? " via " : "",
380 feature_str(FEAT_PROVIDER
) ? feature_str(FEAT_PROVIDER
) : "",
383 * This is a duplicate of the NOTICE but see below...
385 send_reply(sptr
, RPL_YOURHOST
, cli_name(&me
), version
);
386 send_reply(sptr
, RPL_CREATED
, creation
);
387 send_reply(sptr
, RPL_MYINFO
, cli_name(&me
), version
, infousermodes
,
388 infochanmodes
, infochanmodeswithparams
);
389 send_supported(sptr
);
390 m_lusers(sptr
, sptr
, 1, parv
);
393 if (cli_snomask(sptr
) & SNO_NOISY
)
394 set_snomask(sptr
, cli_snomask(sptr
) & SNO_NOISY
, SNO_ADD
);
395 if (feature_bool(FEAT_CONNEXIT_NOTICES
))
396 sendto_opmask_butone(0, SNO_CONNEXIT
,
397 "Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s%s>",
398 cli_name(sptr
), user
->username
, user
->host
,
399 cli_sock_ip(sptr
), get_client_class(sptr
),
400 cli_info(sptr
), NumNick(cptr
) /* two %s's */);
402 IPcheck_connect_succeeded(sptr
);
404 * Set user's initial modes
406 tmpstr
= (char*)client_get_default_umode(sptr
);
407 if (tmpstr
) for (; *tmpstr
; ++tmpstr
) {
410 if (!feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY
)) {
412 set_snomask(sptr
, SNO_DEFAULT
, SNO_SET
);
416 if (!feature_bool(FEAT_WALLOPS_OPER_ONLY
))
426 if (!feature_bool(FEAT_HIS_DEBUG_OPER_ONLY
))
433 struct Client
*acptr
= user
->server
;
435 ircd_strncpy(user
->username
, cli_username(sptr
), USERLEN
);
436 Count_newremoteclient(UserStats
, acptr
);
438 if (cli_from(acptr
) != cli_from(sptr
))
440 sendcmdto_one(&me
, CMD_KILL
, cptr
, "%C :%s (%s != %s[%s])",
441 sptr
, cli_name(&me
), cli_name(user
->server
), cli_name(cli_from(acptr
)),
442 cli_sockhost(cli_from(acptr
)));
443 SetFlag(sptr
, FLAG_KILLED
);
444 return exit_client(cptr
, sptr
, &me
, "NICK server wrong direction");
446 else if (HasFlag(acptr
, FLAG_TS8
))
447 SetFlag(sptr
, FLAG_TS8
);
450 * Check to see if this user is being propagated
451 * as part of a net.burst, or is using protocol 9.
452 * FIXME: This can be sped up - its stupid to check it for
453 * every NICK message in a burst again --Run.
455 for (; acptr
!= &me
; acptr
= cli_serv(acptr
)->up
)
457 if (IsBurst(acptr
) || Protocol(acptr
) < 10)
460 if (!IPcheck_remote_connect(sptr
, (acptr
!= &me
)))
463 * We ran out of bits to count this
465 sendcmdto_one(&me
, CMD_KILL
, sptr
, "%C :%s (Too many connections from your host -- Ghost)",
466 sptr
, cli_name(&me
));
467 return exit_client(cptr
, sptr
, &me
,"Too many connections from your host -- throttled");
470 if(MyConnect(sptr
) && feature_bool(FEAT_SETHOST_AUTO
)) {
471 if (conf_check_slines(sptr
)) {
472 send_reply(sptr
, RPL_USINGSLINE
);
480 if (IsInvisible(sptr
))
481 ++UserStats
.inv_clients
;
485 tmpstr
= umode_str(sptr
);
486 /* Send full IP address to IPv6-grokking servers. */
487 sendcmdto_flag_serv_butone(user
->server
, CMD_NICK
, cptr
,
488 FLAG_IPV6
, FLAG_LAST_FLAG
,
489 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
490 cli_name(sptr
), cli_hopcount(sptr
) + 1,
492 user
->realusername
, user
->realhost
,
493 *tmpstr
? "+" : "", tmpstr
, *tmpstr
? " " : "",
494 iptobase64(ip_base64
, &cli_ip(sptr
), sizeof(ip_base64
), 1),
495 NumNick(sptr
), cli_info(sptr
));
496 /* Send fake IPv6 addresses to pre-IPv6 servers. */
497 sendcmdto_flag_serv_butone(user
->server
, CMD_NICK
, cptr
,
498 FLAG_LAST_FLAG
, FLAG_IPV6
,
499 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
500 cli_name(sptr
), cli_hopcount(sptr
) + 1,
502 user
->realusername
, user
->realhost
,
503 *tmpstr
? "+" : "", tmpstr
, *tmpstr
? " " : "",
504 iptobase64(ip_base64
, &cli_ip(sptr
), sizeof(ip_base64
), 0),
505 NumNick(sptr
), cli_info(sptr
));
507 /* Send user mode to client */
510 static struct Flags flags
; /* automatically initialized to zeros */
511 /* To avoid sending +r to the client due to auth-on-connect, set
512 * the "old" FLAG_ACCOUNT bit to match the client's value.
515 FlagSet(&flags
, FLAG_ACCOUNT
);
517 FlagClr(&flags
, FLAG_ACCOUNT
);
518 send_umode(cptr
, sptr
, &flags
, ALL_UMODES
);
519 if ((cli_snomask(sptr
) != SNO_DEFAULT
) && HasFlag(sptr
, FLAG_SERVNOTICE
))
520 send_reply(sptr
, RPL_SNOMASK
, cli_snomask(sptr
), cli_snomask(sptr
));
525 /** List of user mode characters. */
526 static const struct UserMode
{
527 unsigned int flag
; /**< User mode constant. */
528 char c
; /**< Character corresponding to the mode. */
532 { FLAG_INVISIBLE
, 'i' },
533 { FLAG_WALLOP
, 'w' },
534 { FLAG_SERVNOTICE
, 's' },
536 { FLAG_CHSERV
, 'k' },
538 { FLAG_ACCOUNT
, 'r' },
539 { FLAG_HIDDENHOST
, 'x' },
540 { FLAG_ACCOUNTONLY
, 'R' },
541 { FLAG_XTRAOP
, 'X' },
542 { FLAG_NOCHAN
, 'n' },
543 { FLAG_NOIDLE
, 'I' },
544 { FLAG_SETHOST
, 'h' },
545 { FLAG_PARANOID
, 'P' }
548 /** Length of #userModeList. */
549 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
552 * XXX - find a way to get rid of this
554 /** Nasty global buffer used for communications with umode_str() and others. */
555 static char umodeBuf
[BUFSIZE
];
557 /** Try to set a user's nickname.
558 * If \a sptr is a server, the client is being introduced for the first time.
559 * @param[in] cptr Client to set nickname.
560 * @param[in] sptr Client sending the NICK.
561 * @param[in] nick New nickname.
562 * @param[in] parc Number of arguments to NICK.
563 * @param[in] parv Argument list to NICK.
564 * @return CPTR_KILLED if \a cptr was killed, else 0.
566 int set_nick_name(struct Client
* cptr
, struct Client
* sptr
,
567 const char* nick
, int parc
, char* parv
[])
569 if (IsServer(sptr
)) {
571 const char* account
= 0;
577 * A server introducing a new client, change source
579 struct Client
* new_client
= make_client(cptr
, STAT_UNKNOWN
);
580 assert(0 != new_client
);
582 cli_hopcount(new_client
) = atoi(parv
[2]);
583 cli_lastnick(new_client
) = atoi(parv
[3]);
584 if (Protocol(cptr
) > 9 && parc
> 7 && *parv
[6] == '+')
586 for (p
= parv
[6] + 1; *p
; p
++)
588 for (i
= 0; i
< USERMODELIST_SIZE
; ++i
)
590 if (userModeList
[i
].c
== *p
)
592 SetFlag(new_client
, userModeList
[i
].flag
);
593 if (userModeList
[i
].flag
== FLAG_ACCOUNT
)
595 if (userModeList
[i
].flag
== FLAG_SETHOST
)
596 hostmask
= parv
[parc
- 4];
602 client_set_privs(new_client
, NULL
); /* set privs on user */
606 strcpy(cli_name(new_client
), nick
);
607 cli_user(new_client
) = make_user(new_client
);
608 cli_user(new_client
)->server
= sptr
;
609 SetRemoteNumNick(new_client
, parv
[parc
- 2]);
611 * IP# of remote client
613 base64toip(parv
[parc
- 3], &cli_ip(new_client
));
615 add_client_to_list(new_client
);
616 hAddClient(new_client
);
618 cli_serv(sptr
)->ghost
= 0; /* :server NICK means end of net.burst */
619 ircd_strncpy(cli_username(new_client
), parv
[4], USERLEN
);
620 ircd_strncpy(cli_user(new_client
)->realusername
, parv
[4], USERLEN
);
621 ircd_strncpy(cli_user(new_client
)->host
, parv
[5], HOSTLEN
);
622 ircd_strncpy(cli_user(new_client
)->realhost
, parv
[5], HOSTLEN
);
623 ircd_strncpy(cli_info(new_client
), parv
[parc
- 1], REALLEN
);
625 int len
= ACCOUNTLEN
;
626 if ((p
= strchr(account
, ':'))) {
627 len
= (p
++) - account
;
628 cli_user(new_client
)->acc_create
= atoi(p
);
629 if ((pp
= strchr(p
, ':')))
630 cli_user(new_client
)->acc_id
= atoi(pp
+ 1);
631 Debug((DEBUG_DEBUG
, "Received timestamped account in user mode; "
632 "account \"%s\", timestamp %Tu id %u", account
,
633 cli_user(new_client
)->acc_create
,
634 cli_user(new_client
)->acc_id
));
636 ircd_strncpy(cli_user(new_client
)->account
, account
, len
);
638 if (HasHiddenHost(new_client
))
639 ircd_snprintf(0, cli_user(new_client
)->host
, HOSTLEN
, "%s.%s",
640 account
, feature_str(FEAT_HIDDEN_HOST
));
641 if (HasSetHost(new_client
)) {
642 if ((host
= strrchr(hostmask
, '@')) != NULL
) {
644 ircd_strncpy(cli_username(new_client
), hostmask
, USERLEN
);
645 ircd_strncpy(cli_user(new_client
)->host
, host
, HOSTLEN
);
649 return register_user(cptr
, new_client
);
651 else if ((cli_name(sptr
))[0]) {
653 * Client changing its nick
655 * If the client belongs to me, then check to see
656 * if client is on any channels where it is currently
657 * banned. If so, do not allow the nick change to occur.
660 const char* channel_name
;
661 struct Membership
*member
;
662 if ((channel_name
= find_no_nickchange_channel(sptr
)) && !IsXtraOp(sptr
)) {
663 return send_reply(cptr
, ERR_BANNICKCHANGE
, channel_name
);
666 * Refuse nick change if the last nick change was less
667 * then 30 seconds ago. This is intended to get rid of
668 * clone bots doing NICK FLOOD. -SeKs
669 * If someone didn't change their nick for more then 60 seconds
670 * however, allow to do two nick changes immediately after another
671 * before limiting the nick flood. -Run
673 if (CurrentTime
< cli_nextnick(cptr
))
675 cli_nextnick(cptr
) += 2;
676 send_reply(cptr
, ERR_NICKTOOFAST
, parv
[1],
677 cli_nextnick(cptr
) - CurrentTime
);
678 /* Send error message */
679 sendcmdto_one(cptr
, CMD_NICK
, cptr
, "%s", cli_name(cptr
));
680 /* bounce NICK to user */
681 return 0; /* ignore nick change! */
684 /* Limit total to 1 change per NICK_DELAY seconds: */
685 cli_nextnick(cptr
) += NICK_DELAY
;
686 /* However allow _maximal_ 1 extra consecutive nick change: */
687 if (cli_nextnick(cptr
) < CurrentTime
)
688 cli_nextnick(cptr
) = CurrentTime
;
690 /* Invalidate all bans against the user so we check them again */
691 for (member
= (cli_user(cptr
))->channel
; member
;
692 member
= member
->next_channel
)
693 ClearBanValid(member
);
696 * Also set 'lastnick' to current time, if changed.
698 if (0 != ircd_strcmp(parv
[0], nick
))
699 cli_lastnick(sptr
) = (sptr
== cptr
) ? TStime() : atoi(parv
[2]);
702 * Client just changing his/her nick. If he/she is
703 * on a channel, send note of change to all clients
704 * on that channel. Propagate notice to other servers.
707 sendcmdto_common_channels_butone(sptr
, CMD_NICK
, NULL
, ":%s", nick
);
708 add_history(sptr
, 1);
709 sendcmdto_serv_butone(sptr
, CMD_NICK
, cptr
, "%s %Tu", nick
,
713 sendcmdto_one(sptr
, CMD_NICK
, sptr
, ":%s", nick
);
715 if ((cli_name(sptr
))[0])
717 strcpy(cli_name(sptr
), nick
);
721 /* Local client setting NICK the first time */
722 strcpy(cli_name(sptr
), nick
);
724 return auth_set_nick(cli_auth(sptr
), nick
);
729 /** Calculate the hash value for a target.
730 * @param[in] target Pointer to target, cast to unsigned int.
731 * @return Hash value constructed from the pointer.
733 static unsigned char hash_target(unsigned int target
)
735 return (unsigned char) (target
>> 16) ^ (target
>> 8);
738 /** Records \a target as a recent target for \a sptr.
739 * @param[in] sptr User who has sent to a new target.
740 * @param[in] target Target to add.
743 add_target(struct Client
*sptr
, void *target
)
745 /* Ok, this shouldn't work esp on alpha
747 unsigned char hash
= hash_target((unsigned long) target
);
748 unsigned char* targets
;
751 assert(cli_local(sptr
));
753 targets
= cli_targets(sptr
);
758 for (i
= 0; i
< MAXTARGETS
; ++i
) {
759 if (targets
[i
] == hash
)
765 memmove(&targets
[RESERVEDTARGETS
+ 1],
766 &targets
[RESERVEDTARGETS
], MAXTARGETS
- RESERVEDTARGETS
- 1);
767 targets
[RESERVEDTARGETS
] = hash
;
770 /** Check whether \a sptr can send to or join \a target yet.
771 * @param[in] sptr User trying to join a channel or send a message.
772 * @param[in] target Target of the join or message.
773 * @param[in] name Name of the target.
774 * @param[in] created If non-zero, trying to join a new channel.
775 * @return Non-zero if too many target changes; zero if okay to send.
777 int check_target_limit(struct Client
*sptr
, void *target
, const char *name
,
780 unsigned char hash
= hash_target((unsigned long) target
);
782 unsigned char* targets
;
785 assert(cli_local(sptr
));
786 targets
= cli_targets(sptr
);
788 /* If user is invited to channel, give him/her a free target */
789 if (IsChannelName(name
) && IsInvited(sptr
, target
))
792 /* opers always have a free target */
797 * Same target as last time?
799 if (targets
[0] == hash
)
801 for (i
= 1; i
< MAXTARGETS
; ++i
) {
802 if (targets
[i
] == hash
) {
803 memmove(&targets
[1], &targets
[0], i
);
812 if (CurrentTime
< cli_nexttarget(sptr
)) {
813 if (cli_nexttarget(sptr
) - CurrentTime
< TARGET_DELAY
+ 8) {
817 cli_nexttarget(sptr
) += 2;
818 send_reply(sptr
, ERR_TARGETTOOFAST
, name
,
819 cli_nexttarget(sptr
) - CurrentTime
);
824 cli_nexttarget(sptr
) += TARGET_DELAY
;
825 if (cli_nexttarget(sptr
) < CurrentTime
- (TARGET_DELAY
* (MAXTARGETS
- 1)))
826 cli_nexttarget(sptr
) = CurrentTime
- (TARGET_DELAY
* (MAXTARGETS
- 1));
829 memmove(&targets
[1], &targets
[0], MAXTARGETS
- 1);
834 /** Allows a channel operator to avoid target change checks when
835 * sending messages to users on their channel.
836 * @param[in] source User sending the message.
837 * @param[in] nick Destination of the message.
838 * @param[in] channel Name of channel being sent to.
839 * @param[in] text Message to send.
840 * @param[in] is_notice If non-zero, use CNOTICE instead of CPRIVMSG.
842 /* Added 971023 by Run. */
843 int whisper(struct Client
* source
, const char* nick
, const char* channel
,
844 const char* text
, int is_notice
)
847 struct Channel
* chptr
;
848 struct Membership
* membership
;
852 assert(0 != channel
);
853 assert(MyUser(source
));
855 if (!(dest
= FindUser(nick
))) {
856 return send_reply(source
, ERR_NOSUCHNICK
, nick
);
858 if (!(chptr
= FindChannel(channel
))) {
859 return send_reply(source
, ERR_NOSUCHCHANNEL
, channel
);
862 * compare both users channel lists, instead of the channels user list
863 * since the link is the same, this should be a little faster for channels
864 * with a lot of users
866 for (membership
= cli_user(source
)->channel
; membership
; membership
= membership
->next_channel
) {
867 if (chptr
== membership
->channel
)
870 if (0 == membership
) {
871 return send_reply(source
, ERR_NOTONCHANNEL
, chptr
->chname
);
873 if (!IsVoicedOrOpped(membership
)) {
874 return send_reply(source
, ERR_VOICENEEDED
, chptr
->chname
);
877 * lookup channel in destination
879 assert(0 != cli_user(dest
));
880 for (membership
= cli_user(dest
)->channel
; membership
; membership
= membership
->next_channel
) {
881 if (chptr
== membership
->channel
)
884 if (0 == membership
|| IsZombie(membership
)) {
885 return send_reply(source
, ERR_USERNOTINCHANNEL
, cli_name(dest
), chptr
->chname
);
887 if (is_silenced(source
, dest
))
890 if (cli_user(dest
)->away
)
891 send_reply(source
, RPL_AWAY
, cli_name(dest
), cli_user(dest
)->away
);
893 sendcmdto_one(source
, CMD_NOTICE
, dest
, "%C :%s", dest
, text
);
895 sendcmdto_one(source
, CMD_PRIVATE
, dest
, "%C :%s", dest
, text
);
900 /** Send a user mode change for \a cptr to neighboring servers.
901 * @param[in] cptr User whose mode is changing.
902 * @param[in] sptr Client who sent us the mode change message.
903 * @param[in] old Prior set of user flags.
904 * @param[in] prop If non-zero, also include FLAG_OPER.
906 void send_umode_out(struct Client
*cptr
, struct Client
*sptr
,
907 struct Flags
*old
, int prop
)
910 struct Client
*acptr
;
912 send_umode(NULL
, sptr
, old
, prop
? SEND_UMODES
: SEND_UMODES_BUT_OPER
);
914 for (i
= HighestFd
; i
>= 0; i
--)
916 if ((acptr
= LocalClientArray
[i
]) && IsServer(acptr
) &&
917 (acptr
!= cptr
) && (acptr
!= sptr
) && *umodeBuf
)
918 sendcmdto_one(sptr
, CMD_MODE
, acptr
, "%s %s", cli_name(sptr
), umodeBuf
);
920 if (cptr
&& MyUser(cptr
))
921 send_umode(cptr
, sptr
, old
, ALL_UMODES
);
925 /** Call \a fmt for each Client named in \a names.
926 * @param[in] sptr Client requesting information.
927 * @param[in] names Space-delimited list of nicknames.
928 * @param[in] rpl Base reply string for messages.
929 * @param[in] fmt Formatting callback function.
931 void send_user_info(struct Client
* sptr
, char* names
, int rpl
, InfoFormatter fmt
)
937 struct Client
* acptr
;
944 mb
= msgq_make(sptr
, rpl_str(rpl
), cli_name(&me
), cli_name(sptr
));
946 for (name
= ircd_strtok(&p
, names
, " "); name
; name
= ircd_strtok(&p
, 0, " ")) {
947 if ((acptr
= FindUser(name
))) {
949 msgq_append(0, mb
, " ");
950 (*fmt
)(acptr
, sptr
, mb
);
952 if (5 == ++arg_count
)
955 send_buffer(sptr
, mb
, 0);
959 /** Set \a flag on \a cptr and possibly hide the client's hostmask.
960 * @param[in,out] cptr User who is getting a new flag.
961 * @param[in] flag Some flag that affects host-hiding (FLAG_HIDDENHOST, FLAG_ACCOUNT).
965 hide_hostmask(struct Client
*cptr
, unsigned int flag
)
967 struct Membership
*chan
;
970 case FLAG_HIDDENHOST
:
971 /* Local users cannot set +x unless FEAT_HOST_HIDING is true. */
972 if (MyConnect(cptr
) && !feature_bool(FEAT_HOST_HIDING
))
974 /* If the user is +h, we don't hide the hostmask. Set the flag to keep sync though */
975 if (HasSetHost(cptr
)) {
981 /* Invalidate all bans against the user so we check them again */
982 for (chan
= (cli_user(cptr
))->channel
; chan
;
983 chan
= chan
->next_channel
)
991 if (!HasFlag(cptr
, FLAG_HIDDENHOST
) || !HasFlag(cptr
, FLAG_ACCOUNT
))
994 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Registered");
995 ircd_snprintf(0, cli_user(cptr
)->host
, HOSTLEN
, "%s.%s",
996 cli_user(cptr
)->account
, feature_str(FEAT_HIDDEN_HOST
));
998 /* ok, the client is now fully hidden, so let them know -- hikari */
1000 send_reply(cptr
, RPL_HOSTHIDDEN
, cli_user(cptr
)->host
);
1003 * Go through all channels the client was on, rejoin him
1004 * and set the modes, if any
1006 for (chan
= cli_user(cptr
)->channel
; chan
; chan
= chan
->next_channel
)
1010 /* Send a JOIN unless the user's join has been delayed. */
1011 if (!IsDelayedJoin(chan
))
1012 sendcmdto_channel_butserv_butone(cptr
, CMD_JOIN
, chan
->channel
, cptr
, 0,
1013 "%H", chan
->channel
);
1014 if (IsChanOp(chan
) && HasVoice(chan
))
1015 sendcmdto_channel_butserv_butone(&his
, CMD_MODE
, chan
->channel
, cptr
, 0,
1016 "%H +ov %C %C", chan
->channel
, cptr
,
1018 else if (IsChanOp(chan
) || HasVoice(chan
))
1019 sendcmdto_channel_butserv_butone(&his
, CMD_MODE
, chan
->channel
, cptr
, 0,
1020 "%H +%c %C", chan
->channel
, IsChanOp(chan
) ? 'o' : 'v', cptr
);
1026 * set_hostmask() - derived from hide_hostmask()
1029 int set_hostmask(struct Client
*cptr
, char *hostmask
, char *password
)
1033 char *host
, *new_vhost
, *vhost_pass
;
1034 char hiddenhost
[USERLEN
+ HOSTLEN
+ 2];
1035 struct Membership
*chan
;
1037 Debug((DEBUG_INFO
, "set_hostmask() %C, %s, %s", cptr
, hostmask
, password
));
1039 /* sethost enabled? */
1040 if (MyConnect(cptr
) && !feature_bool(FEAT_SETHOST
)) {
1041 send_reply(cptr
, ERR_DISABLED
, "SETHOST");
1045 /* sethost enabled for users? */
1046 if (MyConnect(cptr
) && !IsAnOper(cptr
) && !feature_bool(FEAT_SETHOST_USER
)) {
1047 send_reply(cptr
, ERR_NOPRIVILEGES
);
1051 /* MODE_DEL: restore original hostmask */
1052 if (EmptyString(hostmask
)) {
1053 /* is already sethost'ed? */
1054 if (IsSetHost(cptr
)) {
1056 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Host change");
1057 /* If they are +rx, we need to return to their +x host, not their "real" host */
1058 if (HasHiddenHost(cptr
))
1059 ircd_snprintf(0, cli_user(cptr
)->host
, HOSTLEN
, "%s.%s",
1060 cli_user(cptr
)->account
, feature_str(FEAT_HIDDEN_HOST
));
1062 strncpy(cli_user(cptr
)->host
, cli_user(cptr
)->realhost
, HOSTLEN
);
1063 strncpy(cli_user(cptr
)->username
, cli_user(cptr
)->realusername
, USERLEN
);
1065 if (MyConnect(cptr
))
1066 log_write(LS_SETHOST
, L_INFO
, LOG_NOSNOTICE
,
1067 "SETHOST (%s@%s) by (%#R): restoring real hostmask",
1068 cli_user(cptr
)->username
, cli_user(cptr
)->host
, cptr
);
1071 /* MODE_ADD: set a new hostmask */
1073 /* chop up ident and host.cc */
1074 if ((host
= strrchr(hostmask
, '@'))) /* oper can specifiy ident@host.cc */
1076 else /* user can only specifiy host.cc [password] */
1081 if (MyConnect(cptr
)) {
1082 if (IsAnOper(cptr
)) {
1083 if ((new_vhost
= IsVhost(host
, 1)) == NULL
) {
1084 if (!feature_bool(FEAT_SETHOST_FREEFORM
)) {
1085 send_reply(cptr
, ERR_HOSTUNAVAIL
, hostmask
);
1086 log_write(LS_SETHOST
, L_INFO
, LOG_NOSNOTICE
,
1087 "SETHOST (%s@%s) by (%#R): no such s-line",
1088 (host
!= hostmask
) ? hostmask
: cli_user(cptr
)->username
, host
, cptr
);
1090 } else /* freeform active, log and go */
1093 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Host change");
1094 /* set the new ident and host */
1095 if (host
!= hostmask
) /* oper only specified host.cc */
1096 strncpy(cli_user(cptr
)->username
, hostmask
, USERLEN
);
1097 strncpy(cli_user(cptr
)->host
, host
, HOSTLEN
);
1099 log_write(LS_SETHOST
, (freeform
) ? L_NOTICE
: L_INFO
,
1100 (freeform
) ? 0 : LOG_NOSNOTICE
, "SETHOST (%s@%s) by (%#R)%s",
1101 cli_user(cptr
)->username
, cli_user(cptr
)->host
, cptr
,
1102 (freeform
) ? ": using freeform" : "");
1104 * plain user sethost, handled here
1107 /* empty password? */
1108 if (EmptyString(password
)) {
1109 send_reply(cptr
, ERR_NEEDMOREPARAMS
, "MODE");
1112 /* no such s-line */
1113 if ((new_vhost
= IsVhost(host
, 0)) == NULL
) {
1114 send_reply(cptr
, ERR_HOSTUNAVAIL
, hostmask
);
1115 log_write(LS_SETHOST
, L_INFO
, LOG_NOSNOTICE
, "SETHOST (%s@%s %s) by (%#R): no such s-line",
1116 cli_user(cptr
)->username
, host
, password
, cptr
);
1120 if ((vhost_pass
= IsVhostPass(new_vhost
)) == NULL
) {
1121 send_reply(cptr
, ERR_PASSWDMISMATCH
);
1122 log_write(LS_SETHOST
, L_INFO
, 0, "SETHOST (%s@%s %s) by (%#R): trying to use an oper s-line",
1123 cli_user(cptr
)->username
, host
, password
, cptr
);
1126 /* incorrect password */
1127 if (strCasediff(vhost_pass
, password
)) {
1128 send_reply(cptr
, ERR_PASSWDMISMATCH
);
1129 log_write(LS_SETHOST
, L_NOTICE
, 0, "SETHOST (%s@%s %s) by (%#R): incorrect password",
1130 cli_user(cptr
)->username
, host
, password
, cptr
);
1133 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Host change");
1134 /* set the new host */
1135 strncpy(cli_user(cptr
)->host
, new_vhost
, HOSTLEN
);
1137 log_write(LS_SETHOST
, L_INFO
, LOG_NOSNOTICE
, "SETHOST (%s@%s) by (%#R)",
1138 cli_user(cptr
)->username
, cli_user(cptr
)->host
, cptr
);
1140 } else { /* remote user */
1141 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Host change");
1142 if (host
!= hostmask
) /* oper only specified host.cc */
1143 strncpy(cli_user(cptr
)->username
, hostmask
, USERLEN
);
1144 strncpy(cli_user(cptr
)->host
, host
, HOSTLEN
);
1153 if (MyConnect(cptr
)) {
1154 ircd_snprintf(0, hiddenhost
, HOSTLEN
+ USERLEN
+ 2, "%s@%s",
1155 cli_user(cptr
)->username
, cli_user(cptr
)->host
);
1156 send_reply(cptr
, RPL_HOSTHIDDEN
, hiddenhost
);
1160 /* Code copied from hide_hostmask(). This is the old (pre-delayedjoin)
1161 * version. Switch this in if you're not using the delayed join patch. */
1163 * Go through all channels the client was on, rejoin him
1164 * and set the modes, if any
1166 for (chan
= cli_user(cptr
)->channel
; chan
; chan
= chan
->next_channel
) {
1169 sendcmdto_channel_butserv_butone(cptr
, CMD_JOIN
, chan
->channel
, cptr
,
1170 "%H", chan
->channel
);
1171 if (IsChanOp(chan
) && HasVoice(chan
)) {
1172 sendcmdto_channel_butserv_butone(&me
, CMD_MODE
, chan
->channel
, cptr
,
1173 "%H +ov %C %C", chan
->channel
, cptr
, cptr
);
1174 } else if (IsChanOp(chan
) || HasVoice(chan
)) {
1175 sendcmdto_channel_butserv_butone(&me
, CMD_MODE
, chan
->channel
, cptr
,
1176 "%H +%c %C", chan
->channel
, IsChanOp(chan
) ? 'o' : 'v', cptr
);
1182 * Go through all channels the client was on, rejoin him
1183 * and set the modes, if any
1185 for (chan
= cli_user(cptr
)->channel
; chan
; chan
= chan
->next_channel
) {
1188 /* If this channel has delayed joins and the user has no modes, just set
1189 * the delayed join flag rather than showing the join, even if the user
1190 * was visible before */
1191 if (!IsChanOp(chan
) && !HasVoice(chan
)
1192 && (chan
->channel
->mode
.mode
& MODE_DELJOINS
)) {
1193 SetDelayedJoin(chan
);
1195 sendcmdto_channel_butserv_butone(cptr
, CMD_JOIN
, chan
->channel
, cptr
, 0,
1196 "%H", chan
->channel
);
1198 if (IsChanOp(chan
) && HasVoice(chan
)) {
1199 sendcmdto_channel_butserv_butone(&me
, CMD_MODE
, chan
->channel
, cptr
, 0,
1200 "%H +ov %C %C", chan
->channel
, cptr
, cptr
);
1201 } else if (IsChanOp(chan
) || HasVoice(chan
)) {
1202 sendcmdto_channel_butserv_butone(&me
, CMD_MODE
, chan
->channel
, cptr
, 0,
1203 "%H +%c %C", chan
->channel
, IsChanOp(chan
) ? 'o' : 'v', cptr
);
1209 /** Set a user's mode. This function checks that \a cptr is trying to
1210 * set his own mode, prevents local users from setting inappropriate
1211 * modes through this function, and applies any other side effects of
1212 * a successful mode change.
1214 * @param[in,out] cptr User setting someone's mode.
1215 * @param[in] sptr Client who sent the mode change message.
1216 * @param[in] parc Number of parameters in \a parv.
1217 * @param[in] parv Parameters to MODE.
1220 int set_user_mode(struct Client
*cptr
, struct Client
*sptr
, int parc
, char *parv
[])
1224 struct Client
*acptr
;
1227 struct Flags setflags
;
1228 unsigned int tmpmask
= 0;
1229 int snomask_given
= 0;
1231 char *hostmask
, *password
;
1233 int do_host_hiding
= 0;
1234 int do_set_host
= 0;
1236 hostmask
= password
= NULL
;
1240 return need_more_params(sptr
, "MODE");
1242 if (!(acptr
= FindUser(parv
[1])))
1244 if (MyConnect(sptr
))
1245 send_reply(sptr
, ERR_NOSUCHCHANNEL
, parv
[1]);
1249 if (IsServer(sptr
) || sptr
!= acptr
)
1252 sendwallto_group_butone(&me
, WALL_WALLOPS
, 0,
1253 "MODE for User %s from %s!%s", parv
[1],
1254 cli_name(cptr
), cli_name(sptr
));
1256 send_reply(sptr
, ERR_USERSDONTMATCH
);
1264 for (i
= 0; i
< USERMODELIST_SIZE
; i
++)
1266 if (HasFlag(sptr
, userModeList
[i
].flag
) &&
1267 ((userModeList
[i
].flag
!= FLAG_ACCOUNT
) &&
1268 (userModeList
[i
].flag
!= FLAG_SETHOST
)))
1269 *m
++ = userModeList
[i
].c
;
1272 send_reply(sptr
, RPL_UMODEIS
, buf
);
1273 if (HasFlag(sptr
, FLAG_SERVNOTICE
) && MyConnect(sptr
)
1274 && cli_snomask(sptr
) !=
1275 (unsigned int)(IsOper(sptr
) ? SNO_OPERDEFAULT
: SNO_DEFAULT
))
1276 send_reply(sptr
, RPL_SNOMASK
, cli_snomask(sptr
), cli_snomask(sptr
));
1281 * find flags already set for user
1282 * why not just copy them?
1284 setflags
= cli_flags(sptr
);
1286 if (MyConnect(sptr
))
1287 tmpmask
= cli_snomask(sptr
);
1290 * parse mode change string(s)
1292 for (p
= &parv
[2]; *p
; p
++) { /* p is changed in loop too */
1293 for (m
= *p
; *m
; m
++) {
1302 if (*(p
+ 1) && is_snomask(*(p
+ 1))) {
1304 tmpmask
= umode_make_snomask(tmpmask
, *++p
, what
);
1305 tmpmask
&= (IsAnOper(sptr
) ? SNO_ALL
: SNO_USER
);
1308 tmpmask
= (what
== MODE_ADD
) ?
1309 (IsAnOper(sptr
) ? SNO_OPERDEFAULT
: SNO_DEFAULT
) : 0;
1311 SetServNotice(sptr
);
1313 ClearServNotice(sptr
);
1316 if (what
== MODE_ADD
)
1322 if (what
== MODE_ADD
)
1325 ClrFlag(sptr
, FLAG_OPER
);
1326 ClrFlag(sptr
, FLAG_LOCOP
);
1327 if (MyConnect(sptr
))
1329 tmpmask
= cli_snomask(sptr
) & ~SNO_OPER
;
1330 cli_handler(sptr
) = CLIENT_HANDLER
;
1335 if (what
== MODE_ADD
)
1339 ClrFlag(sptr
, FLAG_OPER
);
1340 ClrFlag(sptr
, FLAG_LOCOP
);
1341 if (MyConnect(sptr
))
1343 tmpmask
= cli_snomask(sptr
) & ~SNO_OPER
;
1344 cli_handler(sptr
) = CLIENT_HANDLER
;
1349 if (what
== MODE_ADD
)
1352 if (!feature_bool(FEAT_AUTOINVISIBLE
) || IsOper(sptr
)) /* Don't allow non-opers to -i if FEAT_AUTOINVISIBLE is set */
1353 ClearInvisible(sptr
);
1356 if (what
== MODE_ADD
)
1362 if (what
== MODE_ADD
)
1363 SetChannelService(sptr
);
1365 ClearChannelService(sptr
);
1368 if (what
== MODE_ADD
)
1374 if (what
== MODE_ADD
)
1380 if (what
== MODE_ADD
)
1386 if (what
== MODE_ADD
)
1392 if (what
== MODE_ADD
)
1396 if (what
== MODE_ADD
) {
1397 if (*(p
+ 1) && is_hostmask(*(p
+ 1))) {
1400 /* DON'T step p onto the trailing NULL in the parameter array! - splidge */
1407 send_reply(sptr
, ERR_NEEDMOREPARAMS
, "SETHOST");
1409 send_reply(sptr
, ERR_BADHOSTMASK
, *(p
+1));
1410 p
++; /* Swallow the arg anyway */
1413 } else { /* MODE_DEL */
1420 if (what
== MODE_ADD
)
1421 SetAccountOnly(sptr
);
1423 ClearAccountOnly(sptr
);
1426 if (what
== MODE_ADD
)
1429 ClearParanoid(sptr
);
1432 send_reply(sptr
, ERR_UMODEUNKNOWNFLAG
, *m
);
1438 * Evaluate rules for new user mode
1439 * Stop users making themselves operators too easily:
1441 if (!IsServer(cptr
))
1443 if (!FlagHas(&setflags
, FLAG_OPER
) && IsOper(sptr
))
1445 if (!FlagHas(&setflags
, FLAG_LOCOP
) && IsLocOp(sptr
))
1448 * new umode; servers can set it, local users cannot;
1449 * prevents users from /kick'ing or /mode -o'ing
1451 if (!FlagHas(&setflags
, FLAG_CHSERV
) && !IsOper(sptr
))
1452 ClearChannelService(sptr
);
1453 if (!FlagHas(&setflags
, FLAG_XTRAOP
) && !IsOper(sptr
))
1455 if (!FlagHas(&setflags
, FLAG_NOCHAN
) && !(IsOper(sptr
) || feature_bool(FEAT_USER_HIDECHANS
)))
1457 if (!FlagHas(&setflags
, FLAG_NOIDLE
) && !(IsOper(sptr
) || feature_bool(FEAT_USER_HIDEIDLETIME
)))
1459 if (!FlagHas(&setflags
, FLAG_PARANOID
) && !IsOper(sptr
))
1460 ClearParanoid(sptr
);
1463 * only send wallops to opers
1465 if (feature_bool(FEAT_WALLOPS_OPER_ONLY
) && !IsAnOper(sptr
) &&
1466 !FlagHas(&setflags
, FLAG_WALLOP
))
1468 if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY
) && MyConnect(sptr
) &&
1469 !IsAnOper(sptr
) && !FlagHas(&setflags
, FLAG_SERVNOTICE
))
1471 ClearServNotice(sptr
);
1472 set_snomask(sptr
, 0, SNO_SET
);
1474 if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY
) &&
1475 !IsAnOper(sptr
) && !FlagHas(&setflags
, FLAG_DEBUG
))
1478 if (MyConnect(sptr
))
1480 if ((FlagHas(&setflags
, FLAG_OPER
) || FlagHas(&setflags
, FLAG_LOCOP
)) &&
1482 det_confs_butmask(sptr
, CONF_CLIENT
& ~CONF_OPERATOR
);
1484 if (SendServNotice(sptr
))
1486 if (tmpmask
!= cli_snomask(sptr
))
1487 set_snomask(sptr
, tmpmask
, SNO_SET
);
1488 if (cli_snomask(sptr
) && snomask_given
)
1489 send_reply(sptr
, RPL_SNOMASK
, cli_snomask(sptr
), cli_snomask(sptr
));
1492 set_snomask(sptr
, 0, SNO_SET
);
1495 * Compare new flags with old flags and send string which
1496 * will cause servers to update correctly.
1498 if (!FlagHas(&setflags
, FLAG_OPER
) && IsOper(sptr
))
1502 client_set_privs(sptr
, NULL
); /* may set propagate privilege */
1504 /* remember propagate privilege setting */
1505 if (HasPriv(sptr
, PRIV_PROPAGATE
))
1507 if (FlagHas(&setflags
, FLAG_OPER
) && !IsOper(sptr
))
1509 /* user no longer oper */
1511 client_set_privs(sptr
, NULL
); /* will clear propagate privilege */
1513 if (FlagHas(&setflags
, FLAG_INVISIBLE
) && !IsInvisible(sptr
))
1514 --UserStats
.inv_clients
;
1515 if (!FlagHas(&setflags
, FLAG_INVISIBLE
) && IsInvisible(sptr
))
1516 ++UserStats
.inv_clients
;
1517 if (!FlagHas(&setflags
, FLAG_HIDDENHOST
) && do_host_hiding
)
1518 hide_hostmask(sptr
, FLAG_HIDDENHOST
);
1520 /* We clear the flag in the old mask, so that the +h will be sent */
1521 /* Only do this if we're SETTING +h and it succeeded */
1522 if (set_hostmask(sptr
, hostmask
, password
) && hostmask
)
1523 FlagClr(&setflags
, FLAG_SETHOST
);
1525 send_umode_out(cptr
, sptr
, &setflags
, prop
);
1530 /** Build a mode string to describe modes for \a cptr.
1531 * @param[in] cptr Some user.
1532 * @return Pointer to a static buffer.
1534 char *umode_str(struct Client
*cptr
)
1536 /* Maximum string size: "owidgrx\0" */
1539 struct Flags c_flags
= cli_flags(cptr
);
1541 if (!HasPriv(cptr
, PRIV_PROPAGATE
))
1542 FlagClr(&c_flags
, FLAG_OPER
);
1544 for (i
= 0; i
< USERMODELIST_SIZE
; ++i
)
1546 if (FlagHas(&c_flags
, userModeList
[i
].flag
) &&
1547 userModeList
[i
].flag
>= FLAG_GLOBAL_UMODES
)
1548 *m
++ = userModeList
[i
].c
;
1551 if (IsAccount(cptr
))
1553 char* t
= cli_user(cptr
)->account
;
1556 while ((*m
++ = *t
++))
1559 if (cli_user(cptr
)->acc_create
) {
1561 Debug((DEBUG_DEBUG
, "Sending timestamped account in user mode for "
1562 "account \"%s\"; timestamp %Tu", cli_user(cptr
)->account
,
1563 cli_user(cptr
)->acc_create
));
1564 if(cli_user(cptr
)->acc_id
) {
1565 ircd_snprintf(0, t
= nbuf
, sizeof(nbuf
), ":%Tu:%lu",
1566 cli_user(cptr
)->acc_create
, cli_user(cptr
)->acc_id
);
1568 ircd_snprintf(0, t
= nbuf
, sizeof(nbuf
), ":%Tu",
1569 cli_user(cptr
)->acc_create
);
1571 m
--; /* back up over previous nul-termination */
1572 while ((*m
++ = *t
++))
1575 m
--; /* Step back over the '\0' */
1578 if (IsSetHost(cptr
)) {
1580 ircd_snprintf(0, m
, USERLEN
+ HOSTLEN
+ 2, "%s@%s", cli_user(cptr
)->username
,
1581 cli_user(cptr
)->host
);
1584 return umodeBuf
; /* Note: static buffer, gets
1585 overwritten by send_umode() */
1588 /** Send a mode change string for \a sptr to \a cptr.
1589 * @param[in] cptr Destination of mode change message.
1590 * @param[in] sptr User whose mode has changed.
1591 * @param[in] old Pre-change set of modes for \a sptr.
1592 * @param[in] sendset One of ALL_UMODES, SEND_UMODES_BUT_OPER,
1593 * SEND_UMODES, to select which changed user modes to send.
1595 void send_umode(struct Client
*cptr
, struct Client
*sptr
, struct Flags
*old
,
1602 int what
= MODE_NULL
;
1605 * Build a string in umodeBuf to represent the change in the user's
1606 * mode between the new (cli_flags(sptr)) and 'old', but skipping
1607 * the modes indicated by sendset.
1611 for (i
= 0; i
< USERMODELIST_SIZE
; ++i
)
1613 flag
= userModeList
[i
].flag
;
1614 if (FlagHas(old
, flag
)
1615 == HasFlag(sptr
, flag
))
1621 case SEND_UMODES_BUT_OPER
:
1622 if (flag
== FLAG_OPER
)
1624 /* and fall through */
1626 if (flag
< FLAG_GLOBAL_UMODES
)
1630 /* Special case for SETHOST.. */
1631 if (flag
== FLAG_SETHOST
) {
1632 /* Don't send to users */
1633 if (cptr
&& MyUser(cptr
))
1636 /* If we're setting +h, add the parameter later */
1637 if (!FlagHas(old
, flag
))
1640 if (FlagHas(old
, flag
))
1642 if (what
== MODE_DEL
)
1643 *m
++ = userModeList
[i
].c
;
1648 *m
++ = userModeList
[i
].c
;
1651 else /* !FlagHas(old, flag) */
1653 if (what
== MODE_ADD
)
1654 *m
++ = userModeList
[i
].c
;
1659 *m
++ = userModeList
[i
].c
;
1665 ircd_snprintf(0, m
, USERLEN
+ HOSTLEN
+ 1, "%s@%s", cli_user(sptr
)->username
,
1666 cli_user(sptr
)->host
);
1669 if (*umodeBuf
&& cptr
)
1670 sendcmdto_one(sptr
, CMD_MODE
, cptr
, "%s %s", cli_name(sptr
), umodeBuf
);
1674 * Check to see if this resembles a sno_mask. It is if 1) there is
1675 * at least one digit and 2) The first digit occurs before the first
1676 * alphabetic character.
1677 * @param[in] word Word to check for sno_mask-ness.
1678 * @return Non-zero if \a word looks like a server notice mask; zero if not.
1680 int is_snomask(char *word
)
1684 for (; *word
; word
++)
1687 else if (IsAlpha(*word
))
1694 * Check to see if it resembles a valid hostmask.
1696 int is_hostmask(char *word
)
1701 Debug((DEBUG_INFO
, "is_hostmask() %s", word
));
1703 if (strlen(word
) > (HOSTLEN
+ USERLEN
+ 1) || strlen(word
) <= 0)
1706 /* if a host is specified, make sure it's valid */
1707 host
= strrchr(word
, '@');
1709 if (strlen(++host
) < 1)
1711 if (strlen(host
) > HOSTLEN
)
1716 if ('@' == *word
) /* no leading @'s */
1719 if ('#' == *word
) { /* numeric index given? */
1720 for (word
++; *word
; word
++) {
1721 if (!IsDigit(*word
))
1727 /* normal hostmask, account for at most one '@' */
1728 for (; *word
; word
++) {
1733 if (!IsHostChar(*word
))
1736 return (1 < i
) ? 0 : 1; /* no more than on '@' */
1742 * IsVhost() - Check if given host is a valid spoofhost
1743 * (ie: configured thru a S:line)
1745 static char *IsVhost(char *hostmask
, int oper
)
1747 unsigned int i
= 0, y
= 0;
1748 struct sline
*sconf
;
1750 Debug((DEBUG_INFO
, "IsVhost() %s", hostmask
));
1752 if (EmptyString(hostmask
))
1755 /* spoofhost specified as index, ie: #27 */
1756 if ('#' == hostmask
[0]) {
1757 y
= atoi(hostmask
+ 1);
1758 for (i
= 0, sconf
= GlobalSList
; sconf
; sconf
= sconf
->next
) {
1759 if (!oper
&& EmptyString(sconf
->passwd
))
1762 return sconf
->spoofhost
;
1767 /* spoofhost specified as host, ie: host.cc */
1768 for (sconf
= GlobalSList
; sconf
; sconf
= sconf
->next
)
1769 if (strCasediff(hostmask
, sconf
->spoofhost
) == 0)
1770 return sconf
->spoofhost
;
1776 * IsVhostPass() - Check if given spoofhost has a password
1777 * associated with it, and if, return the password (cleartext)
1779 static char *IsVhostPass(char *hostmask
)
1781 struct sline
*sconf
;
1783 Debug((DEBUG_INFO
, "IsVhostPass() %s", hostmask
));
1785 if (EmptyString(hostmask
))
1788 for (sconf
= GlobalSList
; sconf
; sconf
= sconf
->next
)
1789 if (strCasediff(hostmask
, sconf
->spoofhost
) == 0) {
1790 Debug((DEBUG_INFO
, "sconf->passwd %s", sconf
->passwd
));
1791 return EmptyString(sconf
->passwd
) ? NULL
: sconf
->passwd
;
1797 /** Update snomask \a oldmask according to \a arg and \a what.
1798 * @param[in] oldmask Original user mask.
1799 * @param[in] arg Update string (either a number or '+'/'-' followed by a number).
1800 * @param[in] what MODE_ADD if adding the mask.
1801 * @return New value of service notice mask.
1803 unsigned int umode_make_snomask(unsigned int oldmask
, char *arg
, int what
)
1805 unsigned int sno_what
;
1806 unsigned int newmask
;
1810 if (what
== MODE_ADD
)
1815 else if (*arg
== '-')
1818 if (what
== MODE_ADD
)
1824 sno_what
= (what
== MODE_ADD
) ? SNO_SET
: SNO_DEL
;
1825 /* pity we don't have strtoul everywhere */
1826 newmask
= (unsigned int)atoi(arg
);
1827 if (sno_what
== SNO_DEL
)
1828 newmask
= oldmask
& ~newmask
;
1829 else if (sno_what
== SNO_ADD
)
1834 /** Remove \a cptr from the singly linked list \a list.
1835 * @param[in] cptr Client to remove from list.
1836 * @param[in,out] list Pointer to head of list containing \a cptr.
1838 static void delfrom_list(struct Client
*cptr
, struct SLink
**list
)
1841 struct SLink
* prv
= NULL
;
1843 for (tmp
= *list
; tmp
; tmp
= tmp
->next
) {
1844 if (tmp
->value
.cptr
== cptr
) {
1846 prv
->next
= tmp
->next
;
1856 /** Set \a cptr's server notice mask, according to \a what.
1857 * @param[in,out] cptr Client whose snomask is updating.
1858 * @param[in] newmask Base value for new snomask.
1859 * @param[in] what One of SNO_ADD, SNO_DEL, SNO_SET, to choose operation.
1861 void set_snomask(struct Client
*cptr
, unsigned int newmask
, int what
)
1863 unsigned int oldmask
, diffmask
; /* unsigned please */
1867 oldmask
= cli_snomask(cptr
);
1869 if (what
== SNO_ADD
)
1871 else if (what
== SNO_DEL
)
1872 newmask
= oldmask
& ~newmask
;
1873 else if (what
!= SNO_SET
) /* absolute set, no math needed */
1874 sendto_opmask_butone(0, SNO_OLDSNO
, "setsnomask called with %d ?!", what
);
1876 newmask
&= (IsAnOper(cptr
) ? SNO_ALL
: SNO_USER
);
1878 diffmask
= oldmask
^ newmask
;
1880 for (i
= 0; diffmask
>> i
; i
++) {
1881 if (((diffmask
>> i
) & 1))
1883 if (((newmask
>> i
) & 1))
1886 tmp
->next
= opsarray
[i
];
1887 tmp
->value
.cptr
= cptr
;
1891 /* not real portable :( */
1892 delfrom_list(cptr
, &opsarray
[i
]);
1895 cli_snomask(cptr
) = newmask
;
1898 /** Check whether \a sptr is allowed to send a message to \a acptr.
1899 * If \a sptr is a remote user, it means some server has an outdated
1900 * SILENCE list for \a acptr, so send the missing SILENCE mask(s) back
1901 * in the direction of \a sptr. Skip the check if \a sptr is a server.
1902 * @param[in] sptr Client trying to send a message.
1903 * @param[in] acptr Destination of message.
1904 * @return Non-zero if \a sptr is SILENCEd by \a acptr, zero if not.
1906 int is_silenced(struct Client
*sptr
, struct Client
*acptr
)
1910 size_t buf_used
, slen
;
1913 if (IsServer(sptr
) || !(user
= cli_user(acptr
))
1914 || !(found
= find_ban(sptr
, user
->silence
)))
1916 assert(!(found
->flags
& BAN_EXCEPTION
));
1917 if (!MyConnect(sptr
)) {
1918 /* Buffer positive silence to send back. */
1919 buf_used
= strlen(found
->banstr
);
1920 memcpy(buf
, found
->banstr
, buf_used
);
1921 /* Add exceptions to buffer. */
1922 for (found
= user
->silence
; found
; found
= found
->next
) {
1923 if (!(found
->flags
& BAN_EXCEPTION
))
1925 slen
= strlen(found
->banstr
);
1926 if (buf_used
+ slen
+ 4 > 400) {
1927 buf
[buf_used
] = '\0';
1928 sendcmdto_one(acptr
, CMD_SILENCE
, cli_from(sptr
), "%C %s", sptr
, buf
);
1932 buf
[buf_used
++] = ',';
1933 buf
[buf_used
++] = '+';
1934 buf
[buf_used
++] = '~';
1935 memcpy(buf
+ buf_used
, found
->banstr
, slen
);
1938 /* Flush silence buffer. */
1940 buf
[buf_used
] = '\0';
1941 sendcmdto_one(acptr
, CMD_SILENCE
, cli_from(sptr
), "%C %s", sptr
, buf
);
1948 /** Send RPL_ISUPPORT lines to \a cptr.
1949 * @param[in] cptr Client to send ISUPPORT to.
1953 send_supported(struct Client
*cptr
)
1955 char featurebuf
[512];
1957 ircd_snprintf(0, featurebuf
, sizeof(featurebuf
), FEATURES1
, FEATURESVALUES1
);
1958 send_reply(cptr
, RPL_ISUPPORT
, featurebuf
);
1959 ircd_snprintf(0, featurebuf
, sizeof(featurebuf
), FEATURES2
, FEATURESVALUES2
);
1960 send_reply(cptr
, RPL_ISUPPORT
, featurebuf
);
1962 return 0; /* convenience return, if it's ever needed */