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 2005/08/17 01:57:03 entrope Exp $
36 #include "ircd_alloc.h"
37 #include "ircd_auth.h"
38 #include "ircd_chattr.h"
39 #include "ircd_features.h"
41 #include "ircd_reply.h"
42 #include "ircd_snprintf.h"
43 #include "ircd_string.h"
52 #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
);
310 /** Copy a cleaned-up version of a username.
311 * Replace all instances of '~' and "invalid" username characters
312 * (!isIrcUi()) with underscores, truncating at USERLEN or the first
313 * control character. \a dest and \a source may be the same buffer.
314 * @param[out] dest Destination buffer.
315 * @param[in] source Source buffer.
316 * @param[in] tilde If non-zero, prepend a '~' to \a dest.
318 static char *clean_user_id(char *dest
, char *source
, int tilde
)
325 ch
= *s
++; /* Store first character to copy: */
328 *d
++ = '~'; /* If `dest' == `source', then this overwrites `ch' */
331 while (ch
&& !IsCntrl(ch
) && rlen
--)
333 char nch
= *s
++; /* Store next character to copy */
334 *d
++ = IsUserChar(ch
) ? ch
: '_'; /* This possibly overwrites it */
347 * This function is called when both NICK and USER messages
348 * have been accepted for the client, in whatever order. Only
349 * after this the USER message is propagated.
351 * NICK's must be propagated at once when received, although
352 * it would be better to delay them too until full info is
353 * available. Doing it is not so simple though, would have
354 * to implement the following:
356 * 1) user telnets in and gives only "NICK foobar" and waits
357 * 2) another user far away logs in normally with the nick
358 * "foobar" (quite legal, as this server didn't propagate it).
359 * 3) now this server gets nick "foobar" from outside, but
360 * has already the same defined locally. Current server
361 * would just issue "KILL foobar" to clean out dups. But,
362 * this is not fair. It should actually request another
363 * nick from local user or kill him/her...
365 /** Finish registering a user who has sent both NICK and USER.
366 * For local connections, possibly check IAuth; make sure there is a
367 * matching Client config block; clean the username field; check
368 * K/k-lines; check for "hacked" looking usernames; assign a numnick;
369 * and send greeting (WELCOME, ISUPPORT, MOTD, etc).
370 * For all connections, update the invisible user and operator counts;
371 * run IPcheck against their address; and forward the NICK.
373 * @param[in] cptr Client who introduced the user.
374 * @param[in,out] sptr Client who has been fully introduced.
375 * @param[in] nick Client's new nickname.
376 * @param[in] username Client's username.
377 * @return Zero or CPTR_KILLED.
379 int register_user(struct Client
*cptr
, struct Client
*sptr
,
380 const char *nick
, char *username
)
382 struct ConfItem
* aconf
;
386 char c
= 0; /* not alphanum */
387 char d
= 'a'; /* not a digit */
395 short digitgroups
= 0;
396 struct User
* user
= cli_user(sptr
);
400 user
->last
= CurrentTime
;
401 parv
[0] = cli_name(sptr
);
402 parv
[1] = parv
[2] = NULL
;
406 static time_t last_too_many1
;
407 static time_t last_too_many2
;
409 assert(cptr
== sptr
);
410 assert(cli_unreg(sptr
) == 0);
411 if (!IsIAuthed(sptr
)) {
413 return iauth_start_client(iauth_active
, sptr
);
417 switch (conf_check_client(sptr
))
421 case ACR_NO_AUTHORIZATION
:
422 sendto_opmask_butone(0, SNO_UNAUTH
, "Unauthorized connection from %s.",
423 get_client_name(sptr
, HIDE_IP
));
424 ++ServerStats
->is_ref
;
425 return exit_client(cptr
, sptr
, &me
,
426 "No Authorization - use another server");
427 case ACR_TOO_MANY_IN_CLASS
:
428 if (CurrentTime
- last_too_many1
>= (time_t) 60)
430 last_too_many1
= CurrentTime
;
431 sendto_opmask_butone(0, SNO_TOOMANY
, "Too many connections in "
432 "class %s for %s.", get_client_class(sptr
),
433 get_client_name(sptr
, SHOW_IP
));
435 ++ServerStats
->is_ref
;
436 IPcheck_connect_fail(sptr
);
437 return exit_client(cptr
, sptr
, &me
,
438 "Sorry, your connection class is full - try "
439 "again later or try another server");
440 case ACR_TOO_MANY_FROM_IP
:
441 if (CurrentTime
- last_too_many2
>= (time_t) 60)
443 last_too_many2
= CurrentTime
;
444 sendto_opmask_butone(0, SNO_TOOMANY
, "Too many connections from "
446 get_client_name(sptr
, SHOW_IP
));
448 ++ServerStats
->is_ref
;
449 return exit_client(cptr
, sptr
, &me
,
450 "Too many connections from your host");
451 case ACR_ALREADY_AUTHORIZED
:
452 /* Can this ever happen? */
454 ++ServerStats
->is_ref
;
455 IPcheck_connect_fail(sptr
);
456 return exit_client(cptr
, sptr
, &me
, "Unknown error -- Try again");
458 ircd_strncpy(user
->host
, cli_sockhost(sptr
), HOSTLEN
);
459 ircd_strncpy(user
->realhost
, cli_sockhost(sptr
), HOSTLEN
);
460 aconf
= cli_confs(sptr
)->value
.aconf
;
462 clean_user_id(user
->username
,
463 HasFlag(sptr
, FLAG_GOTID
) ? cli_username(sptr
) : username
,
464 HasFlag(sptr
, FLAG_DOID
) && !HasFlag(sptr
, FLAG_GOTID
)
465 && !(HasSetHost(sptr
))); /* No tilde for S-lined users. */
467 /* Have to set up "realusername" before doing the gline check below */
468 ircd_strncpy(user
->realusername
, user
->username
, USERLEN
);
470 if ((user
->username
[0] == '\0')
471 || ((user
->username
[0] == '~') && (user
->username
[1] == '\000')))
472 return exit_client(cptr
, sptr
, &me
, "USER: Bogus userid.");
474 if (!EmptyString(aconf
->passwd
)
475 && strcmp(cli_passwd(sptr
), aconf
->passwd
))
477 ServerStats
->is_ref
++;
478 send_reply(sptr
, ERR_PASSWDMISMATCH
);
479 return exit_client(cptr
, sptr
, &me
, "Bad Password");
481 memset(cli_passwd(sptr
), 0, sizeof(cli_passwd(sptr
)));
483 * following block for the benefit of time-dependent K:-lines
485 killreason
= find_kill(sptr
, 1);
487 ServerStats
->is_ref
++;
488 return exit_client(cptr
, sptr
, &me
,
489 (killreason
== -1 ? "K-lined" : "G-lined"));
492 * Check for mixed case usernames, meaning probably hacked. Jon2 3-94
493 * Summary of rules now implemented in this patch: Ensor 11-94
494 * In a mixed-case name, if first char is upper, one more upper may
495 * appear anywhere. (A mixed-case name *must* have an upper first
496 * char, and may have one other upper.)
497 * A third upper may appear if all 3 appear at the beginning of the
498 * name, separated only by "others" (-/_/.).
499 * A single group of digits is allowed anywhere.
500 * Two groups of digits are allowed if at least one of the groups is
501 * at the beginning or the end.
502 * Only one '-', '_', or '.' is allowed (or two, if not consecutive).
503 * But not as the first or last char.
504 * No other special characters are allowed.
505 * Name must contain at least one letter.
507 tmpstr2
= tmpstr
= (username
[0] == '~' ? &username
[1] : username
);
508 while (*tmpstr
&& !badid
)
520 if ((leadcaps
|| pos
== 1) && !lower
&& !digits
)
526 if (pos
== 1 || !IsDigit(d
))
533 else if (c
== '-' || c
== '_' || c
== '.')
538 else if (d
== '-' || d
== '_' || d
== '.' || other
> 2)
547 if (lower
&& upper
&& (!leadcaps
|| leadcaps
> 3 ||
548 (upper
> 2 && upper
> leadcaps
)))
550 else if (digitgroups
== 2 && !(IsDigit(tmpstr2
[0]) || IsDigit(c
)))
552 else if ((!lower
&& !upper
) || !IsAlnum(c
))
555 if (badid
&& (!HasFlag(sptr
, FLAG_GOTID
) ||
556 strcmp(cli_username(sptr
), username
) != 0))
558 ServerStats
->is_ref
++;
560 send_reply(cptr
, SND_EXPLICIT
| ERR_INVALIDUSERNAME
,
561 ":Your username is invalid.");
562 send_reply(cptr
, SND_EXPLICIT
| ERR_INVALIDUSERNAME
,
563 ":Connect with your real username, in lowercase.");
564 send_reply(cptr
, SND_EXPLICIT
| ERR_INVALIDUSERNAME
,
565 ":If your mail address were foo@bar.com, your username "
567 return exit_client(cptr
, sptr
, &me
, "USER: Bad username");
569 Count_unknownbecomesclient(sptr
, UserStats
);
571 if (MyConnect(sptr
) && feature_bool(FEAT_AUTOINVISIBLE
))
574 if(MyConnect(sptr
) && feature_bool(FEAT_SETHOST_AUTO
)) {
575 if (conf_check_slines(sptr
)) {
576 send_reply(sptr
, RPL_USINGSLINE
);
582 cli_handler(sptr
) = CLIENT_HANDLER
;
583 SetLocalNumNick(sptr
);
586 feature_str(FEAT_NETWORK
),
587 feature_str(FEAT_PROVIDER
) ? " via " : "",
588 feature_str(FEAT_PROVIDER
) ? feature_str(FEAT_PROVIDER
) : "",
591 * This is a duplicate of the NOTICE but see below...
593 send_reply(sptr
, RPL_YOURHOST
, cli_name(&me
), version
);
594 send_reply(sptr
, RPL_CREATED
, creation
);
595 send_reply(sptr
, RPL_MYINFO
, cli_name(&me
), version
, infousermodes
,
596 infochanmodes
, infochanmodeswithparams
);
597 send_supported(sptr
);
598 m_lusers(sptr
, sptr
, 1, parv
);
601 if (cli_snomask(sptr
) & SNO_NOISY
)
602 set_snomask(sptr
, cli_snomask(sptr
) & SNO_NOISY
, SNO_ADD
);
603 if (feature_bool(FEAT_CONNEXIT_NOTICES
))
604 sendto_opmask_butone(0, SNO_CONNEXIT
,
605 "Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s%s>",
606 cli_name(sptr
), user
->username
, user
->host
,
607 cli_sock_ip(sptr
), get_client_class(sptr
),
608 cli_info(sptr
), NumNick(cptr
) /* two %s's */);
610 IPcheck_connect_succeeded(sptr
);
612 * Set user's initial modes
614 tmpstr
= (char*)client_get_default_umode(sptr
);
615 if (tmpstr
) for (; *tmpstr
; ++tmpstr
) {
618 if (!feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY
)) {
620 set_snomask(sptr
, SNO_DEFAULT
, SNO_SET
);
624 if (!feature_bool(FEAT_WALLOPS_OPER_ONLY
))
634 if (!feature_bool(FEAT_HIS_DEBUG_OPER_ONLY
))
641 struct Client
*acptr
;
643 ircd_strncpy(user
->username
, username
, USERLEN
);
644 Count_newremoteclient(UserStats
, user
->server
);
646 acptr
= user
->server
;
647 if (cli_from(acptr
) != cli_from(sptr
))
649 sendcmdto_one(&me
, CMD_KILL
, cptr
, "%C :%s (%s != %s[%s])",
650 sptr
, cli_name(&me
), cli_name(user
->server
), cli_name(cli_from(acptr
)),
651 cli_sockhost(cli_from(acptr
)));
652 SetFlag(sptr
, FLAG_KILLED
);
653 return exit_client(cptr
, sptr
, &me
, "NICK server wrong direction");
655 else if (HasFlag(acptr
, FLAG_TS8
))
656 SetFlag(sptr
, FLAG_TS8
);
659 * Check to see if this user is being propagated
660 * as part of a net.burst, or is using protocol 9.
661 * FIXME: This can be sped up - its stupid to check it for
662 * every NICK message in a burst again --Run.
664 for (; acptr
!= &me
; acptr
= cli_serv(acptr
)->up
)
666 if (IsBurst(acptr
) || Protocol(acptr
) < 10)
669 if (!IPcheck_remote_connect(sptr
, (acptr
!= &me
)))
672 * We ran out of bits to count this
674 sendcmdto_one(&me
, CMD_KILL
, sptr
, "%C :%s (Too many connections from your host -- Ghost)",
675 sptr
, cli_name(&me
));
676 return exit_client(cptr
, sptr
, &me
,"Too many connections from your host -- throttled");
679 if(MyConnect(sptr
) && feature_bool(FEAT_SETHOST_AUTO
)) {
680 if (conf_check_slines(sptr
)) {
681 send_reply(sptr
, RPL_USINGSLINE
);
689 if (IsInvisible(sptr
))
690 ++UserStats
.inv_clients
;
694 tmpstr
= umode_str(sptr
);
695 /* Send full IP address to IPv6-grokking servers. */
696 sendcmdto_flag_serv_butone(user
->server
, CMD_NICK
, cptr
,
697 FLAG_IPV6
, FLAG_LAST_FLAG
,
698 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
699 nick
, cli_hopcount(sptr
) + 1, cli_lastnick(sptr
),
700 user
->realusername
, user
->realhost
,
701 *tmpstr
? "+" : "", tmpstr
, *tmpstr
? " " : "",
702 iptobase64(ip_base64
, &cli_ip(sptr
), sizeof(ip_base64
), 1),
703 NumNick(sptr
), cli_info(sptr
));
704 /* Send fake IPv6 addresses to pre-IPv6 servers. */
705 sendcmdto_flag_serv_butone(user
->server
, CMD_NICK
, cptr
,
706 FLAG_LAST_FLAG
, FLAG_IPV6
,
707 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
708 nick
, cli_hopcount(sptr
) + 1, cli_lastnick(sptr
),
709 user
->realusername
, user
->realhost
,
710 *tmpstr
? "+" : "", tmpstr
, *tmpstr
? " " : "",
711 iptobase64(ip_base64
, &cli_ip(sptr
), sizeof(ip_base64
), 0),
712 NumNick(sptr
), cli_info(sptr
));
714 /* Send user mode to client */
717 static struct Flags flags
; /* automatically initialized to zeros */
718 send_umode(cptr
, sptr
, &flags
, ALL_UMODES
);
719 if ((cli_snomask(sptr
) != SNO_DEFAULT
) && HasFlag(sptr
, FLAG_SERVNOTICE
))
720 send_reply(sptr
, RPL_SNOMASK
, cli_snomask(sptr
), cli_snomask(sptr
));
725 /** List of user mode characters. */
726 static const struct UserMode
{
727 unsigned int flag
; /**< User mode constant. */
728 char c
; /**< Character corresponding to the mode. */
732 { FLAG_INVISIBLE
, 'i' },
733 { FLAG_WALLOP
, 'w' },
734 { FLAG_SERVNOTICE
, 's' },
736 { FLAG_CHSERV
, 'k' },
738 { FLAG_ACCOUNT
, 'r' },
739 { FLAG_HIDDENHOST
, 'x' },
740 { FLAG_ACCOUNTONLY
, 'R' },
741 { FLAG_XTRAOP
, 'X' },
742 { FLAG_NOCHAN
, 'n' },
743 { FLAG_NOIDLE
, 'I' },
744 { FLAG_SETHOST
, 'h' },
745 { FLAG_PARANOID
, 'P' }
748 /** Length of #userModeList. */
749 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
752 * XXX - find a way to get rid of this
754 /** Nasty global buffer used for communications with umode_str() and others. */
755 static char umodeBuf
[BUFSIZE
];
757 /** Try to set a user's nickname.
758 * If \a sptr is a server, the client is being introduced for the first time.
759 * @param[in] cptr Client to set nickname.
760 * @param[in] sptr Client sending the NICK.
761 * @param[in] nick New nickname.
762 * @param[in] parc Number of arguments to NICK.
763 * @param[in] parv Argument list to NICK.
764 * @return CPTR_KILLED if \a cptr was killed, else 0.
766 int set_nick_name(struct Client
* cptr
, struct Client
* sptr
,
767 const char* nick
, int parc
, char* parv
[])
769 if (IsServer(sptr
)) {
771 const char* account
= 0;
777 * A server introducing a new client, change source
779 struct Client
* new_client
= make_client(cptr
, STAT_UNKNOWN
);
780 assert(0 != new_client
);
782 cli_hopcount(new_client
) = atoi(parv
[2]);
783 cli_lastnick(new_client
) = atoi(parv
[3]);
784 if (Protocol(cptr
) > 9 && parc
> 7 && *parv
[6] == '+')
786 for (p
= parv
[6] + 1; *p
; p
++)
788 for (i
= 0; i
< USERMODELIST_SIZE
; ++i
)
790 if (userModeList
[i
].c
== *p
)
792 SetFlag(new_client
, userModeList
[i
].flag
);
793 if (userModeList
[i
].flag
== FLAG_ACCOUNT
)
795 if (userModeList
[i
].flag
== FLAG_SETHOST
)
796 hostmask
= parv
[parc
- 4];
802 client_set_privs(new_client
, NULL
); /* set privs on user */
806 strcpy(cli_name(new_client
), nick
);
807 cli_user(new_client
) = make_user(new_client
);
808 cli_user(new_client
)->server
= sptr
;
809 SetRemoteNumNick(new_client
, parv
[parc
- 2]);
811 * IP# of remote client
813 base64toip(parv
[parc
- 3], &cli_ip(new_client
));
815 add_client_to_list(new_client
);
816 hAddClient(new_client
);
818 cli_serv(sptr
)->ghost
= 0; /* :server NICK means end of net.burst */
819 ircd_strncpy(cli_username(new_client
), parv
[4], USERLEN
);
820 ircd_strncpy(cli_user(new_client
)->realusername
, parv
[4], USERLEN
);
821 ircd_strncpy(cli_user(new_client
)->host
, parv
[5], HOSTLEN
);
822 ircd_strncpy(cli_user(new_client
)->realhost
, parv
[5], HOSTLEN
);
823 ircd_strncpy(cli_info(new_client
), parv
[parc
- 1], REALLEN
);
825 int len
= ACCOUNTLEN
;
826 if ((p
= strchr(account
, ':'))) {
827 len
= (p
++) - account
;
828 cli_user(new_client
)->acc_create
= atoi(p
);
829 Debug((DEBUG_DEBUG
, "Received timestamped account in user mode; "
830 "account \"%s\", timestamp %Tu", account
,
831 cli_user(new_client
)->acc_create
));
833 ircd_strncpy(cli_user(new_client
)->account
, account
, len
);
835 if (HasHiddenHost(new_client
))
836 ircd_snprintf(0, cli_user(new_client
)->host
, HOSTLEN
, "%s.%s",
837 account
, feature_str(FEAT_HIDDEN_HOST
));
838 if (HasSetHost(new_client
)) {
839 if ((host
= strrchr(hostmask
, '@')) != NULL
) {
841 ircd_strncpy(cli_username(new_client
), hostmask
, USERLEN
);
842 ircd_strncpy(cli_user(new_client
)->host
, host
, HOSTLEN
);
846 return register_user(cptr
, new_client
, cli_name(new_client
), cli_username(new_client
));
848 else if ((cli_name(sptr
))[0]) {
850 * Client changing its nick
852 * If the client belongs to me, then check to see
853 * if client is on any channels where it is currently
854 * banned. If so, do not allow the nick change to occur.
857 const char* channel_name
;
858 struct Membership
*member
;
859 if ((channel_name
= find_no_nickchange_channel(sptr
)) && !IsXtraOp(sptr
)) {
860 return send_reply(cptr
, ERR_BANNICKCHANGE
, channel_name
);
863 * Refuse nick change if the last nick change was less
864 * then 30 seconds ago. This is intended to get rid of
865 * clone bots doing NICK FLOOD. -SeKs
866 * If someone didn't change their nick for more then 60 seconds
867 * however, allow to do two nick changes immediately after another
868 * before limiting the nick flood. -Run
870 if (CurrentTime
< cli_nextnick(cptr
))
872 cli_nextnick(cptr
) += 2;
873 send_reply(cptr
, ERR_NICKTOOFAST
, parv
[1],
874 cli_nextnick(cptr
) - CurrentTime
);
875 /* Send error message */
876 sendcmdto_one(cptr
, CMD_NICK
, cptr
, "%s", cli_name(cptr
));
877 /* bounce NICK to user */
878 return 0; /* ignore nick change! */
881 /* Limit total to 1 change per NICK_DELAY seconds: */
882 cli_nextnick(cptr
) += NICK_DELAY
;
883 /* However allow _maximal_ 1 extra consecutive nick change: */
884 if (cli_nextnick(cptr
) < CurrentTime
)
885 cli_nextnick(cptr
) = CurrentTime
;
887 /* Invalidate all bans against the user so we check them again */
888 for (member
= (cli_user(cptr
))->channel
; member
;
889 member
= member
->next_channel
)
890 ClearBanValid(member
);
893 * Also set 'lastnick' to current time, if changed.
895 if (0 != ircd_strcmp(parv
[0], nick
))
896 cli_lastnick(sptr
) = (sptr
== cptr
) ? TStime() : atoi(parv
[2]);
899 * Client just changing his/her nick. If he/she is
900 * on a channel, send note of change to all clients
901 * on that channel. Propagate notice to other servers.
904 sendcmdto_common_channels_butone(sptr
, CMD_NICK
, NULL
, ":%s", nick
);
905 add_history(sptr
, 1);
906 sendcmdto_serv_butone(sptr
, CMD_NICK
, cptr
, "%s %Tu", nick
,
910 sendcmdto_one(sptr
, CMD_NICK
, sptr
, ":%s", nick
);
912 if ((cli_name(sptr
))[0])
914 strcpy(cli_name(sptr
), nick
);
918 /* Local client setting NICK the first time */
920 strcpy(cli_name(sptr
), nick
);
921 if (!cli_user(sptr
)) {
922 cli_user(sptr
) = make_user(sptr
);
923 cli_user(sptr
)->server
= &me
;
927 cli_unreg(sptr
) &= ~CLIREG_NICK
; /* nickname now set */
930 * If the client hasn't gotten a cookie-ping yet,
931 * choose a cookie and send it. -record!jegelhof@cloud9.net
933 if (!cli_cookie(sptr
)) {
935 cli_cookie(sptr
) = (ircrandom() & 0x7fffffff);
936 } while (!cli_cookie(sptr
));
937 sendrawto_one(cptr
, MSG_PING
" :%u", cli_cookie(sptr
));
939 else if (!cli_unreg(sptr
)) {
941 * USER and PONG already received, now we have NICK.
942 * register_user may reject the client and call exit_client
943 * for it - must test this and exit m_nick too !
945 cli_lastnick(sptr
) = TStime(); /* Always local client */
946 if (register_user(cptr
, sptr
, nick
, cli_user(sptr
)->username
) == CPTR_KILLED
)
953 /** Calculate the hash value for a target.
954 * @param[in] target Pointer to target, cast to unsigned int.
955 * @return Hash value constructed from the pointer.
957 static unsigned char hash_target(unsigned int target
)
959 return (unsigned char) (target
>> 16) ^ (target
>> 8);
962 /** Records \a target as a recent target for \a sptr.
963 * @param[in] sptr User who has sent to a new target.
964 * @param[in] target Target to add.
967 add_target(struct Client
*sptr
, void *target
)
969 /* Ok, this shouldn't work esp on alpha
971 unsigned char hash
= hash_target((unsigned long) target
);
972 unsigned char* targets
;
975 assert(cli_local(sptr
));
977 targets
= cli_targets(sptr
);
982 for (i
= 0; i
< MAXTARGETS
; ++i
) {
983 if (targets
[i
] == hash
)
989 memmove(&targets
[RESERVEDTARGETS
+ 1],
990 &targets
[RESERVEDTARGETS
], MAXTARGETS
- RESERVEDTARGETS
- 1);
991 targets
[RESERVEDTARGETS
] = hash
;
994 /** Check whether \a sptr can send to or join \a target yet.
995 * @param[in] sptr User trying to join a channel or send a message.
996 * @param[in] target Target of the join or message.
997 * @param[in] name Name of the target.
998 * @param[in] created If non-zero, trying to join a new channel.
999 * @return Non-zero if too many target changes; zero if okay to send.
1001 int check_target_limit(struct Client
*sptr
, void *target
, const char *name
,
1004 unsigned char hash
= hash_target((unsigned long) target
);
1006 unsigned char* targets
;
1009 assert(cli_local(sptr
));
1010 targets
= cli_targets(sptr
);
1012 /* If user is invited to channel, give him/her a free target */
1013 if (IsChannelName(name
) && IsInvited(sptr
, target
))
1016 /* opers always have a free target */
1021 * Same target as last time?
1023 if (targets
[0] == hash
)
1025 for (i
= 1; i
< MAXTARGETS
; ++i
) {
1026 if (targets
[i
] == hash
) {
1027 memmove(&targets
[1], &targets
[0], i
);
1036 if (CurrentTime
< cli_nexttarget(sptr
)) {
1037 if (cli_nexttarget(sptr
) - CurrentTime
< TARGET_DELAY
+ 8) {
1039 * No server flooding
1041 cli_nexttarget(sptr
) += 2;
1042 send_reply(sptr
, ERR_TARGETTOOFAST
, name
,
1043 cli_nexttarget(sptr
) - CurrentTime
);
1048 cli_nexttarget(sptr
) += TARGET_DELAY
;
1049 if (cli_nexttarget(sptr
) < CurrentTime
- (TARGET_DELAY
* (MAXTARGETS
- 1)))
1050 cli_nexttarget(sptr
) = CurrentTime
- (TARGET_DELAY
* (MAXTARGETS
- 1));
1053 memmove(&targets
[1], &targets
[0], MAXTARGETS
- 1);
1058 /** Allows a channel operator to avoid target change checks when
1059 * sending messages to users on their channel.
1060 * @param[in] source User sending the message.
1061 * @param[in] nick Destination of the message.
1062 * @param[in] channel Name of channel being sent to.
1063 * @param[in] text Message to send.
1064 * @param[in] is_notice If non-zero, use CNOTICE instead of CPRIVMSG.
1066 /* Added 971023 by Run. */
1067 int whisper(struct Client
* source
, const char* nick
, const char* channel
,
1068 const char* text
, int is_notice
)
1070 struct Client
* dest
;
1071 struct Channel
* chptr
;
1072 struct Membership
* membership
;
1074 assert(0 != source
);
1076 assert(0 != channel
);
1077 assert(MyUser(source
));
1079 if (!(dest
= FindUser(nick
))) {
1080 return send_reply(source
, ERR_NOSUCHNICK
, nick
);
1082 if (!(chptr
= FindChannel(channel
))) {
1083 return send_reply(source
, ERR_NOSUCHCHANNEL
, channel
);
1086 * compare both users channel lists, instead of the channels user list
1087 * since the link is the same, this should be a little faster for channels
1088 * with a lot of users
1090 for (membership
= cli_user(source
)->channel
; membership
; membership
= membership
->next_channel
) {
1091 if (chptr
== membership
->channel
)
1094 if (0 == membership
) {
1095 return send_reply(source
, ERR_NOTONCHANNEL
, chptr
->chname
);
1097 if (!IsVoicedOrOpped(membership
)) {
1098 return send_reply(source
, ERR_VOICENEEDED
, chptr
->chname
);
1101 * lookup channel in destination
1103 assert(0 != cli_user(dest
));
1104 for (membership
= cli_user(dest
)->channel
; membership
; membership
= membership
->next_channel
) {
1105 if (chptr
== membership
->channel
)
1108 if (0 == membership
|| IsZombie(membership
)) {
1109 return send_reply(source
, ERR_USERNOTINCHANNEL
, cli_name(dest
), chptr
->chname
);
1111 if (is_silenced(source
, dest
))
1114 if (cli_user(dest
)->away
)
1115 send_reply(source
, RPL_AWAY
, cli_name(dest
), cli_user(dest
)->away
);
1117 sendcmdto_one(source
, CMD_NOTICE
, dest
, "%C :%s", dest
, text
);
1119 sendcmdto_one(source
, CMD_PRIVATE
, dest
, "%C :%s", dest
, text
);
1124 /** Send a user mode change for \a cptr to neighboring servers.
1125 * @param[in] cptr User whose mode is changing.
1126 * @param[in] sptr Client who sent us the mode change message.
1127 * @param[in] old Prior set of user flags.
1128 * @param[in] prop If non-zero, also include FLAG_OPER.
1130 void send_umode_out(struct Client
*cptr
, struct Client
*sptr
,
1131 struct Flags
*old
, int prop
)
1134 struct Client
*acptr
;
1136 send_umode(NULL
, sptr
, old
, prop
? SEND_UMODES
: SEND_UMODES_BUT_OPER
);
1138 for (i
= HighestFd
; i
>= 0; i
--)
1140 if ((acptr
= LocalClientArray
[i
]) && IsServer(acptr
) &&
1141 (acptr
!= cptr
) && (acptr
!= sptr
) && *umodeBuf
)
1142 sendcmdto_one(sptr
, CMD_MODE
, acptr
, "%s %s", cli_name(sptr
), umodeBuf
);
1144 if (cptr
&& MyUser(cptr
))
1145 send_umode(cptr
, sptr
, old
, ALL_UMODES
);
1149 /** Call \a fmt for each Client named in \a names.
1150 * @param[in] sptr Client requesting information.
1151 * @param[in] names Space-delimited list of nicknames.
1152 * @param[in] rpl Base reply string for messages.
1153 * @param[in] fmt Formatting callback function.
1155 void send_user_info(struct Client
* sptr
, char* names
, int rpl
, InfoFormatter fmt
)
1160 int users_found
= 0;
1161 struct Client
* acptr
;
1168 mb
= msgq_make(sptr
, rpl_str(rpl
), cli_name(&me
), cli_name(sptr
));
1170 for (name
= ircd_strtok(&p
, names
, " "); name
; name
= ircd_strtok(&p
, 0, " ")) {
1171 if ((acptr
= FindUser(name
))) {
1173 msgq_append(0, mb
, " ");
1174 (*fmt
)(acptr
, sptr
, mb
);
1176 if (5 == ++arg_count
)
1179 send_buffer(sptr
, mb
, 0);
1183 /** Set \a flag on \a cptr and possibly hide the client's hostmask.
1184 * @param[in,out] cptr User who is getting a new flag.
1185 * @param[in] flag Some flag that affects host-hiding (FLAG_HIDDENHOST, FLAG_ACCOUNT).
1189 hide_hostmask(struct Client
*cptr
, unsigned int flag
)
1191 struct Membership
*chan
;
1194 case FLAG_HIDDENHOST
:
1195 /* Local users cannot set +x unless FEAT_HOST_HIDING is true. */
1196 if (MyConnect(cptr
) && !feature_bool(FEAT_HOST_HIDING
))
1198 /* If the user is +h, we don't hide the hostmask. Set the flag to keep sync though */
1199 if (HasSetHost(cptr
)) {
1200 SetFlag(cptr
, flag
);
1205 /* Invalidate all bans against the user so we check them again */
1206 for (chan
= (cli_user(cptr
))->channel
; chan
;
1207 chan
= chan
->next_channel
)
1208 ClearBanValid(chan
);
1214 SetFlag(cptr
, flag
);
1215 if (!HasFlag(cptr
, FLAG_HIDDENHOST
) || !HasFlag(cptr
, FLAG_ACCOUNT
))
1218 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Registered");
1219 ircd_snprintf(0, cli_user(cptr
)->host
, HOSTLEN
, "%s.%s",
1220 cli_user(cptr
)->account
, feature_str(FEAT_HIDDEN_HOST
));
1222 /* ok, the client is now fully hidden, so let them know -- hikari */
1223 if (MyConnect(cptr
))
1224 send_reply(cptr
, RPL_HOSTHIDDEN
, cli_user(cptr
)->host
);
1227 * Go through all channels the client was on, rejoin him
1228 * and set the modes, if any
1230 for (chan
= cli_user(cptr
)->channel
; chan
; chan
= chan
->next_channel
)
1234 /* Send a JOIN unless the user's join has been delayed. */
1235 if (!IsDelayedJoin(chan
))
1236 sendcmdto_channel_butserv_butone(cptr
, CMD_JOIN
, chan
->channel
, cptr
, 0,
1237 "%H", chan
->channel
);
1238 if (IsChanOp(chan
) && HasVoice(chan
))
1239 sendcmdto_channel_butserv_butone(&his
, CMD_MODE
, chan
->channel
, cptr
, 0,
1240 "%H +ov %C %C", chan
->channel
, cptr
,
1242 else if (IsChanOp(chan
) || HasVoice(chan
))
1243 sendcmdto_channel_butserv_butone(&his
, CMD_MODE
, chan
->channel
, cptr
, 0,
1244 "%H +%c %C", chan
->channel
, IsChanOp(chan
) ? 'o' : 'v', cptr
);
1250 * set_hostmask() - derived from hide_hostmask()
1253 int set_hostmask(struct Client
*cptr
, char *hostmask
, char *password
)
1257 char *host
, *new_vhost
, *vhost_pass
;
1258 char hiddenhost
[USERLEN
+ HOSTLEN
+ 2];
1259 struct Membership
*chan
;
1261 Debug((DEBUG_INFO
, "set_hostmask() %C, %s, %s", cptr
, hostmask
, password
));
1263 /* sethost enabled? */
1264 if (MyConnect(cptr
) && !feature_bool(FEAT_SETHOST
)) {
1265 send_reply(cptr
, ERR_DISABLED
, "SETHOST");
1269 /* sethost enabled for users? */
1270 if (MyConnect(cptr
) && !IsAnOper(cptr
) && !feature_bool(FEAT_SETHOST_USER
)) {
1271 send_reply(cptr
, ERR_NOPRIVILEGES
);
1275 /* MODE_DEL: restore original hostmask */
1276 if (EmptyString(hostmask
)) {
1277 /* is already sethost'ed? */
1278 if (IsSetHost(cptr
)) {
1280 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Host change");
1281 /* If they are +rx, we need to return to their +x host, not their "real" host */
1282 if (HasHiddenHost(cptr
))
1283 ircd_snprintf(0, cli_user(cptr
)->host
, HOSTLEN
, "%s.%s",
1284 cli_user(cptr
)->account
, feature_str(FEAT_HIDDEN_HOST
));
1286 strncpy(cli_user(cptr
)->host
, cli_user(cptr
)->realhost
, HOSTLEN
);
1287 strncpy(cli_user(cptr
)->username
, cli_user(cptr
)->realusername
, USERLEN
);
1289 if (MyConnect(cptr
))
1290 log_write(LS_SETHOST
, L_INFO
, LOG_NOSNOTICE
,
1291 "SETHOST (%s@%s) by (%#R): restoring real hostmask",
1292 cli_user(cptr
)->username
, cli_user(cptr
)->host
, cptr
);
1295 /* MODE_ADD: set a new hostmask */
1297 /* chop up ident and host.cc */
1298 if ((host
= strrchr(hostmask
, '@'))) /* oper can specifiy ident@host.cc */
1300 else /* user can only specifiy host.cc [password] */
1305 if (MyConnect(cptr
)) {
1306 if (IsAnOper(cptr
)) {
1307 if ((new_vhost
= IsVhost(host
, 1)) == NULL
) {
1308 if (!feature_bool(FEAT_SETHOST_FREEFORM
)) {
1309 send_reply(cptr
, ERR_HOSTUNAVAIL
, hostmask
);
1310 log_write(LS_SETHOST
, L_INFO
, LOG_NOSNOTICE
,
1311 "SETHOST (%s@%s) by (%#R): no such s-line",
1312 (host
!= hostmask
) ? hostmask
: cli_user(cptr
)->username
, host
, cptr
);
1314 } else /* freeform active, log and go */
1317 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Host change");
1318 /* set the new ident and host */
1319 if (host
!= hostmask
) /* oper only specified host.cc */
1320 strncpy(cli_user(cptr
)->username
, hostmask
, USERLEN
);
1321 strncpy(cli_user(cptr
)->host
, host
, HOSTLEN
);
1323 log_write(LS_SETHOST
, (freeform
) ? L_NOTICE
: L_INFO
,
1324 (freeform
) ? 0 : LOG_NOSNOTICE
, "SETHOST (%s@%s) by (%#R)%s",
1325 cli_user(cptr
)->username
, cli_user(cptr
)->host
, cptr
,
1326 (freeform
) ? ": using freeform" : "");
1328 * plain user sethost, handled here
1331 /* empty password? */
1332 if (EmptyString(password
)) {
1333 send_reply(cptr
, ERR_NEEDMOREPARAMS
, "MODE");
1336 /* no such s-line */
1337 if ((new_vhost
= IsVhost(host
, 0)) == NULL
) {
1338 send_reply(cptr
, ERR_HOSTUNAVAIL
, hostmask
);
1339 log_write(LS_SETHOST
, L_INFO
, LOG_NOSNOTICE
, "SETHOST (%s@%s %s) by (%#R): no such s-line",
1340 cli_user(cptr
)->username
, host
, password
, cptr
);
1344 if ((vhost_pass
= IsVhostPass(new_vhost
)) == NULL
) {
1345 send_reply(cptr
, ERR_PASSWDMISMATCH
);
1346 log_write(LS_SETHOST
, L_INFO
, 0, "SETHOST (%s@%s %s) by (%#R): trying to use an oper s-line",
1347 cli_user(cptr
)->username
, host
, password
, cptr
);
1350 /* incorrect password */
1351 if (strCasediff(vhost_pass
, password
)) {
1352 send_reply(cptr
, ERR_PASSWDMISMATCH
);
1353 log_write(LS_SETHOST
, L_NOTICE
, 0, "SETHOST (%s@%s %s) by (%#R): incorrect password",
1354 cli_user(cptr
)->username
, host
, password
, cptr
);
1357 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Host change");
1358 /* set the new host */
1359 strncpy(cli_user(cptr
)->host
, new_vhost
, HOSTLEN
);
1361 log_write(LS_SETHOST
, L_INFO
, LOG_NOSNOTICE
, "SETHOST (%s@%s) by (%#R)",
1362 cli_user(cptr
)->username
, cli_user(cptr
)->host
, cptr
);
1364 } else { /* remote user */
1365 sendcmdto_common_channels_butone(cptr
, CMD_QUIT
, cptr
, ":Host change");
1366 if (host
!= hostmask
) /* oper only specified host.cc */
1367 strncpy(cli_user(cptr
)->username
, hostmask
, USERLEN
);
1368 strncpy(cli_user(cptr
)->host
, host
, HOSTLEN
);
1377 if (MyConnect(cptr
)) {
1378 ircd_snprintf(0, hiddenhost
, HOSTLEN
+ USERLEN
+ 2, "%s@%s",
1379 cli_user(cptr
)->username
, cli_user(cptr
)->host
);
1380 send_reply(cptr
, RPL_HOSTHIDDEN
, hiddenhost
);
1384 /* Code copied from hide_hostmask(). This is the old (pre-delayedjoin)
1385 * version. Switch this in if you're not using the delayed join patch. */
1387 * Go through all channels the client was on, rejoin him
1388 * and set the modes, if any
1390 for (chan
= cli_user(cptr
)->channel
; chan
; chan
= chan
->next_channel
) {
1393 sendcmdto_channel_butserv_butone(cptr
, CMD_JOIN
, chan
->channel
, cptr
,
1394 "%H", chan
->channel
);
1395 if (IsChanOp(chan
) && HasVoice(chan
)) {
1396 sendcmdto_channel_butserv_butone(&me
, CMD_MODE
, chan
->channel
, cptr
,
1397 "%H +ov %C %C", chan
->channel
, cptr
, cptr
);
1398 } else if (IsChanOp(chan
) || HasVoice(chan
)) {
1399 sendcmdto_channel_butserv_butone(&me
, CMD_MODE
, chan
->channel
, cptr
,
1400 "%H +%c %C", chan
->channel
, IsChanOp(chan
) ? 'o' : 'v', cptr
);
1406 * Go through all channels the client was on, rejoin him
1407 * and set the modes, if any
1409 for (chan
= cli_user(cptr
)->channel
; chan
; chan
= chan
->next_channel
) {
1412 /* If this channel has delayed joins and the user has no modes, just set
1413 * the delayed join flag rather than showing the join, even if the user
1414 * was visible before */
1415 if (!IsChanOp(chan
) && !HasVoice(chan
)
1416 && (chan
->channel
->mode
.mode
& MODE_DELJOINS
)) {
1417 SetDelayedJoin(chan
);
1419 sendcmdto_channel_butserv_butone(cptr
, CMD_JOIN
, chan
->channel
, cptr
, 0,
1420 "%H", chan
->channel
);
1422 if (IsChanOp(chan
) && HasVoice(chan
)) {
1423 sendcmdto_channel_butserv_butone(&me
, CMD_MODE
, chan
->channel
, cptr
, 0,
1424 "%H +ov %C %C", chan
->channel
, cptr
, cptr
);
1425 } else if (IsChanOp(chan
) || HasVoice(chan
)) {
1426 sendcmdto_channel_butserv_butone(&me
, CMD_MODE
, chan
->channel
, cptr
, 0,
1427 "%H +%c %C", chan
->channel
, IsChanOp(chan
) ? 'o' : 'v', cptr
);
1433 /** Set a user's mode. This function checks that \a cptr is trying to
1434 * set his own mode, prevents local users from setting inappropriate
1435 * modes through this function, and applies any other side effects of
1436 * a successful mode change.
1438 * @param[in,out] cptr User setting someone's mode.
1439 * @param[in] sptr Client who sent the mode change message.
1440 * @param[in] parc Number of parameters in \a parv.
1441 * @param[in] parv Parameters to MODE.
1444 int set_user_mode(struct Client
*cptr
, struct Client
*sptr
, int parc
, char *parv
[])
1448 struct Client
*acptr
;
1451 struct Flags setflags
;
1452 unsigned int tmpmask
= 0;
1453 int snomask_given
= 0;
1455 char *hostmask
, *password
;
1457 int do_host_hiding
= 0;
1458 int do_set_host
= 0;
1460 hostmask
= password
= NULL
;
1464 return need_more_params(sptr
, "MODE");
1466 if (!(acptr
= FindUser(parv
[1])))
1468 if (MyConnect(sptr
))
1469 send_reply(sptr
, ERR_NOSUCHCHANNEL
, parv
[1]);
1473 if (IsServer(sptr
) || sptr
!= acptr
)
1476 sendwallto_group_butone(&me
, WALL_WALLOPS
, 0,
1477 "MODE for User %s from %s!%s", parv
[1],
1478 cli_name(cptr
), cli_name(sptr
));
1480 send_reply(sptr
, ERR_USERSDONTMATCH
);
1488 for (i
= 0; i
< USERMODELIST_SIZE
; i
++)
1490 if (HasFlag(sptr
, userModeList
[i
].flag
) &&
1491 ((userModeList
[i
].flag
!= FLAG_ACCOUNT
) &&
1492 (userModeList
[i
].flag
!= FLAG_SETHOST
)))
1493 *m
++ = userModeList
[i
].c
;
1496 send_reply(sptr
, RPL_UMODEIS
, buf
);
1497 if (HasFlag(sptr
, FLAG_SERVNOTICE
) && MyConnect(sptr
)
1498 && cli_snomask(sptr
) !=
1499 (unsigned int)(IsOper(sptr
) ? SNO_OPERDEFAULT
: SNO_DEFAULT
))
1500 send_reply(sptr
, RPL_SNOMASK
, cli_snomask(sptr
), cli_snomask(sptr
));
1505 * find flags already set for user
1506 * why not just copy them?
1508 setflags
= cli_flags(sptr
);
1510 if (MyConnect(sptr
))
1511 tmpmask
= cli_snomask(sptr
);
1514 * parse mode change string(s)
1516 for (p
= &parv
[2]; *p
; p
++) { /* p is changed in loop too */
1517 for (m
= *p
; *m
; m
++) {
1526 if (*(p
+ 1) && is_snomask(*(p
+ 1))) {
1528 tmpmask
= umode_make_snomask(tmpmask
, *++p
, what
);
1529 tmpmask
&= (IsAnOper(sptr
) ? SNO_ALL
: SNO_USER
);
1532 tmpmask
= (what
== MODE_ADD
) ?
1533 (IsAnOper(sptr
) ? SNO_OPERDEFAULT
: SNO_DEFAULT
) : 0;
1535 SetServNotice(sptr
);
1537 ClearServNotice(sptr
);
1540 if (what
== MODE_ADD
)
1546 if (what
== MODE_ADD
)
1549 ClrFlag(sptr
, FLAG_OPER
);
1550 ClrFlag(sptr
, FLAG_LOCOP
);
1551 if (MyConnect(sptr
))
1553 tmpmask
= cli_snomask(sptr
) & ~SNO_OPER
;
1554 cli_handler(sptr
) = CLIENT_HANDLER
;
1559 if (what
== MODE_ADD
)
1563 ClrFlag(sptr
, FLAG_OPER
);
1564 ClrFlag(sptr
, FLAG_LOCOP
);
1565 if (MyConnect(sptr
))
1567 tmpmask
= cli_snomask(sptr
) & ~SNO_OPER
;
1568 cli_handler(sptr
) = CLIENT_HANDLER
;
1573 if (what
== MODE_ADD
)
1576 if (!feature_bool(FEAT_AUTOINVISIBLE
) || IsOper(sptr
)) /* Don't allow non-opers to -i if FEAT_AUTOINVISIBLE is set */
1577 ClearInvisible(sptr
);
1580 if (what
== MODE_ADD
)
1586 if (what
== MODE_ADD
)
1587 SetChannelService(sptr
);
1589 ClearChannelService(sptr
);
1592 if (what
== MODE_ADD
)
1598 if (what
== MODE_ADD
)
1604 if (what
== MODE_ADD
)
1610 if (what
== MODE_ADD
)
1616 if (what
== MODE_ADD
)
1620 if (what
== MODE_ADD
) {
1621 if (*(p
+ 1) && is_hostmask(*(p
+ 1))) {
1624 /* DON'T step p onto the trailing NULL in the parameter array! - splidge */
1631 send_reply(sptr
, ERR_NEEDMOREPARAMS
, "SETHOST");
1633 send_reply(sptr
, ERR_BADHOSTMASK
, *(p
+1));
1634 p
++; /* Swallow the arg anyway */
1637 } else { /* MODE_DEL */
1644 if (what
== MODE_ADD
)
1645 SetAccountOnly(sptr
);
1647 ClearAccountOnly(sptr
);
1650 if (what
== MODE_ADD
)
1653 ClearParanoid(sptr
);
1656 send_reply(sptr
, ERR_UMODEUNKNOWNFLAG
, *m
);
1662 * Evaluate rules for new user mode
1663 * Stop users making themselves operators too easily:
1665 if (!IsServer(cptr
))
1667 if (!FlagHas(&setflags
, FLAG_OPER
) && IsOper(sptr
))
1669 if (!FlagHas(&setflags
, FLAG_LOCOP
) && IsLocOp(sptr
))
1672 * new umode; servers can set it, local users cannot;
1673 * prevents users from /kick'ing or /mode -o'ing
1675 if (!FlagHas(&setflags
, FLAG_CHSERV
) && !IsOper(sptr
))
1676 ClearChannelService(sptr
);
1677 if (!FlagHas(&setflags
, FLAG_XTRAOP
) && !IsOper(sptr
))
1679 if (!FlagHas(&setflags
, FLAG_NOCHAN
) && !(IsOper(sptr
) || feature_bool(FEAT_USER_HIDECHANS
)))
1681 if (!FlagHas(&setflags
, FLAG_NOIDLE
) && !IsOper(sptr
))
1683 if (!FlagHas(&setflags
, FLAG_PARANOID
) && !IsOper(sptr
))
1684 ClearParanoid(sptr
);
1687 * only send wallops to opers
1689 if (feature_bool(FEAT_WALLOPS_OPER_ONLY
) && !IsAnOper(sptr
) &&
1690 !FlagHas(&setflags
, FLAG_WALLOP
))
1692 if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY
) && MyConnect(sptr
) &&
1693 !IsAnOper(sptr
) && !FlagHas(&setflags
, FLAG_SERVNOTICE
))
1695 ClearServNotice(sptr
);
1696 set_snomask(sptr
, 0, SNO_SET
);
1698 if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY
) &&
1699 !IsAnOper(sptr
) && !FlagHas(&setflags
, FLAG_DEBUG
))
1702 if (MyConnect(sptr
))
1704 if ((FlagHas(&setflags
, FLAG_OPER
) || FlagHas(&setflags
, FLAG_LOCOP
)) &&
1706 det_confs_butmask(sptr
, CONF_CLIENT
& ~CONF_OPERATOR
);
1708 if (SendServNotice(sptr
))
1710 if (tmpmask
!= cli_snomask(sptr
))
1711 set_snomask(sptr
, tmpmask
, SNO_SET
);
1712 if (cli_snomask(sptr
) && snomask_given
)
1713 send_reply(sptr
, RPL_SNOMASK
, cli_snomask(sptr
), cli_snomask(sptr
));
1716 set_snomask(sptr
, 0, SNO_SET
);
1719 * Compare new flags with old flags and send string which
1720 * will cause servers to update correctly.
1722 if (!FlagHas(&setflags
, FLAG_OPER
) && IsOper(sptr
))
1726 client_set_privs(sptr
, NULL
); /* may set propagate privilege */
1728 /* remember propagate privilege setting */
1729 if (HasPriv(sptr
, PRIV_PROPAGATE
))
1731 if (FlagHas(&setflags
, FLAG_OPER
) && !IsOper(sptr
))
1733 /* user no longer oper */
1735 client_set_privs(sptr
, NULL
); /* will clear propagate privilege */
1737 if (FlagHas(&setflags
, FLAG_INVISIBLE
) && !IsInvisible(sptr
))
1738 --UserStats
.inv_clients
;
1739 if (!FlagHas(&setflags
, FLAG_INVISIBLE
) && IsInvisible(sptr
))
1740 ++UserStats
.inv_clients
;
1741 if (!FlagHas(&setflags
, FLAG_HIDDENHOST
) && do_host_hiding
)
1742 hide_hostmask(sptr
, FLAG_HIDDENHOST
);
1744 /* We clear the flag in the old mask, so that the +h will be sent */
1745 /* Only do this if we're SETTING +h and it succeeded */
1746 if (set_hostmask(sptr
, hostmask
, password
) && hostmask
)
1747 FlagClr(&setflags
, FLAG_SETHOST
);
1749 send_umode_out(cptr
, sptr
, &setflags
, prop
);
1754 /** Build a mode string to describe modes for \a cptr.
1755 * @param[in] cptr Some user.
1756 * @return Pointer to a static buffer.
1758 char *umode_str(struct Client
*cptr
)
1760 /* Maximum string size: "owidgrx\0" */
1763 struct Flags c_flags
= cli_flags(cptr
);
1765 if (!HasPriv(cptr
, PRIV_PROPAGATE
))
1766 FlagClr(&c_flags
, FLAG_OPER
);
1768 for (i
= 0; i
< USERMODELIST_SIZE
; ++i
)
1770 if (FlagHas(&c_flags
, userModeList
[i
].flag
) &&
1771 userModeList
[i
].flag
>= FLAG_GLOBAL_UMODES
)
1772 *m
++ = userModeList
[i
].c
;
1775 if (IsAccount(cptr
))
1777 char* t
= cli_user(cptr
)->account
;
1780 while ((*m
++ = *t
++))
1783 if (cli_user(cptr
)->acc_create
) {
1785 Debug((DEBUG_DEBUG
, "Sending timestamped account in user mode for "
1786 "account \"%s\"; timestamp %Tu", cli_user(cptr
)->account
,
1787 cli_user(cptr
)->acc_create
));
1788 ircd_snprintf(0, t
= nbuf
, sizeof(nbuf
), ":%Tu",
1789 cli_user(cptr
)->acc_create
);
1790 m
--; /* back up over previous nul-termination */
1791 while ((*m
++ = *t
++))
1794 m
--; /* Step back over the '\0' */
1797 if (IsSetHost(cptr
)) {
1799 ircd_snprintf(0, m
, USERLEN
+ HOSTLEN
+ 2, "%s@%s", cli_user(cptr
)->username
,
1800 cli_user(cptr
)->host
);
1803 return umodeBuf
; /* Note: static buffer, gets
1804 overwritten by send_umode() */
1807 /** Send a mode change string for \a sptr to \a cptr.
1808 * @param[in] cptr Destination of mode change message.
1809 * @param[in] sptr User whose mode has changed.
1810 * @param[in] old Pre-change set of modes for \a sptr.
1811 * @param[in] sendset One of ALL_UMODES, SEND_UMODES_BUT_OPER,
1812 * SEND_UMODES, to select which changed user modes to send.
1814 void send_umode(struct Client
*cptr
, struct Client
*sptr
, struct Flags
*old
,
1821 int what
= MODE_NULL
;
1824 * Build a string in umodeBuf to represent the change in the user's
1825 * mode between the new (cli_flags(sptr)) and 'old', but skipping
1826 * the modes indicated by sendset.
1830 for (i
= 0; i
< USERMODELIST_SIZE
; ++i
)
1832 flag
= userModeList
[i
].flag
;
1833 if (FlagHas(old
, flag
)
1834 == HasFlag(sptr
, flag
))
1840 case SEND_UMODES_BUT_OPER
:
1841 if (flag
== FLAG_OPER
)
1843 /* and fall through */
1845 if (flag
< FLAG_GLOBAL_UMODES
)
1849 /* Special case for SETHOST.. */
1850 if (flag
== FLAG_SETHOST
) {
1851 /* Don't send to users */
1852 if (cptr
&& MyUser(cptr
))
1855 /* If we're setting +h, add the parameter later */
1856 if (!FlagHas(old
, flag
))
1859 if (FlagHas(old
, flag
))
1861 if (what
== MODE_DEL
)
1862 *m
++ = userModeList
[i
].c
;
1867 *m
++ = userModeList
[i
].c
;
1870 else /* !FlagHas(old, flag) */
1872 if (what
== MODE_ADD
)
1873 *m
++ = userModeList
[i
].c
;
1878 *m
++ = userModeList
[i
].c
;
1884 ircd_snprintf(0, m
, USERLEN
+ HOSTLEN
+ 1, "%s@%s", cli_user(sptr
)->username
,
1885 cli_user(sptr
)->host
);
1888 if (*umodeBuf
&& cptr
)
1889 sendcmdto_one(sptr
, CMD_MODE
, cptr
, "%s %s", cli_name(sptr
), umodeBuf
);
1893 * Check to see if this resembles a sno_mask. It is if 1) there is
1894 * at least one digit and 2) The first digit occurs before the first
1895 * alphabetic character.
1896 * @param[in] word Word to check for sno_mask-ness.
1897 * @return Non-zero if \a word looks like a server notice mask; zero if not.
1899 int is_snomask(char *word
)
1903 for (; *word
; word
++)
1906 else if (IsAlpha(*word
))
1913 * Check to see if it resembles a valid hostmask.
1915 int is_hostmask(char *word
)
1920 Debug((DEBUG_INFO
, "is_hostmask() %s", word
));
1922 if (strlen(word
) > (HOSTLEN
+ USERLEN
+ 1) || strlen(word
) <= 0)
1925 /* if a host is specified, make sure it's valid */
1926 host
= strrchr(word
, '@');
1928 if (strlen(++host
) < 1)
1930 if (strlen(host
) > HOSTLEN
)
1935 if ('@' == *word
) /* no leading @'s */
1938 if ('#' == *word
) { /* numeric index given? */
1939 for (word
++; *word
; word
++) {
1940 if (!IsDigit(*word
))
1946 /* normal hostmask, account for at most one '@' */
1947 for (; *word
; word
++) {
1952 if (!IsHostChar(*word
))
1955 return (1 < i
) ? 0 : 1; /* no more than on '@' */
1961 * IsVhost() - Check if given host is a valid spoofhost
1962 * (ie: configured thru a S:line)
1964 static char *IsVhost(char *hostmask
, int oper
)
1966 unsigned int i
= 0, y
= 0;
1967 struct sline
*sconf
;
1969 Debug((DEBUG_INFO
, "IsVhost() %s", hostmask
));
1971 if (EmptyString(hostmask
))
1974 /* spoofhost specified as index, ie: #27 */
1975 if ('#' == hostmask
[0]) {
1976 y
= atoi(hostmask
+ 1);
1977 for (i
= 0, sconf
= GlobalSList
; sconf
; sconf
= sconf
->next
) {
1978 if (!oper
&& EmptyString(sconf
->passwd
))
1981 return sconf
->spoofhost
;
1986 /* spoofhost specified as host, ie: host.cc */
1987 for (sconf
= GlobalSList
; sconf
; sconf
= sconf
->next
)
1988 if (strCasediff(hostmask
, sconf
->spoofhost
) == 0)
1989 return sconf
->spoofhost
;
1995 * IsVhostPass() - Check if given spoofhost has a password
1996 * associated with it, and if, return the password (cleartext)
1998 static char *IsVhostPass(char *hostmask
)
2000 struct sline
*sconf
;
2002 Debug((DEBUG_INFO
, "IsVhostPass() %s", hostmask
));
2004 if (EmptyString(hostmask
))
2007 for (sconf
= GlobalSList
; sconf
; sconf
= sconf
->next
)
2008 if (strCasediff(hostmask
, sconf
->spoofhost
) == 0) {
2009 Debug((DEBUG_INFO
, "sconf->passwd %s", sconf
->passwd
));
2010 return EmptyString(sconf
->passwd
) ? NULL
: sconf
->passwd
;
2016 /** Update snomask \a oldmask according to \a arg and \a what.
2017 * @param[in] oldmask Original user mask.
2018 * @param[in] arg Update string (either a number or '+'/'-' followed by a number).
2019 * @param[in] what MODE_ADD if adding the mask.
2020 * @return New value of service notice mask.
2022 unsigned int umode_make_snomask(unsigned int oldmask
, char *arg
, int what
)
2024 unsigned int sno_what
;
2025 unsigned int newmask
;
2029 if (what
== MODE_ADD
)
2034 else if (*arg
== '-')
2037 if (what
== MODE_ADD
)
2043 sno_what
= (what
== MODE_ADD
) ? SNO_SET
: SNO_DEL
;
2044 /* pity we don't have strtoul everywhere */
2045 newmask
= (unsigned int)atoi(arg
);
2046 if (sno_what
== SNO_DEL
)
2047 newmask
= oldmask
& ~newmask
;
2048 else if (sno_what
== SNO_ADD
)
2053 /** Remove \a cptr from the singly linked list \a list.
2054 * @param[in] cptr Client to remove from list.
2055 * @param[in,out] list Pointer to head of list containing \a cptr.
2057 static void delfrom_list(struct Client
*cptr
, struct SLink
**list
)
2060 struct SLink
* prv
= NULL
;
2062 for (tmp
= *list
; tmp
; tmp
= tmp
->next
) {
2063 if (tmp
->value
.cptr
== cptr
) {
2065 prv
->next
= tmp
->next
;
2075 /** Set \a cptr's server notice mask, according to \a what.
2076 * @param[in,out] cptr Client whose snomask is updating.
2077 * @param[in] newmask Base value for new snomask.
2078 * @param[in] what One of SNO_ADD, SNO_DEL, SNO_SET, to choose operation.
2080 void set_snomask(struct Client
*cptr
, unsigned int newmask
, int what
)
2082 unsigned int oldmask
, diffmask
; /* unsigned please */
2086 oldmask
= cli_snomask(cptr
);
2088 if (what
== SNO_ADD
)
2090 else if (what
== SNO_DEL
)
2091 newmask
= oldmask
& ~newmask
;
2092 else if (what
!= SNO_SET
) /* absolute set, no math needed */
2093 sendto_opmask_butone(0, SNO_OLDSNO
, "setsnomask called with %d ?!", what
);
2095 newmask
&= (IsAnOper(cptr
) ? SNO_ALL
: SNO_USER
);
2097 diffmask
= oldmask
^ newmask
;
2099 for (i
= 0; diffmask
>> i
; i
++) {
2100 if (((diffmask
>> i
) & 1))
2102 if (((newmask
>> i
) & 1))
2105 tmp
->next
= opsarray
[i
];
2106 tmp
->value
.cptr
= cptr
;
2110 /* not real portable :( */
2111 delfrom_list(cptr
, &opsarray
[i
]);
2114 cli_snomask(cptr
) = newmask
;
2117 /** Check whether \a sptr is allowed to send a message to \a acptr.
2118 * If \a sptr is a remote user, it means some server has an outdated
2119 * SILENCE list for \a acptr, so send the missing SILENCE mask(s) back
2120 * in the direction of \a sptr. Skip the check if \a sptr is a server.
2121 * @param[in] sptr Client trying to send a message.
2122 * @param[in] acptr Destination of message.
2123 * @return Non-zero if \a sptr is SILENCEd by \a acptr, zero if not.
2125 int is_silenced(struct Client
*sptr
, struct Client
*acptr
)
2129 size_t buf_used
, slen
;
2132 if (IsServer(sptr
) || !(user
= cli_user(acptr
))
2133 || !(found
= find_ban(sptr
, user
->silence
)))
2135 assert(!(found
->flags
& BAN_EXCEPTION
));
2136 if (!MyConnect(sptr
)) {
2137 /* Buffer positive silence to send back. */
2138 buf_used
= strlen(found
->banstr
);
2139 memcpy(buf
, found
->banstr
, buf_used
);
2140 /* Add exceptions to buffer. */
2141 for (found
= user
->silence
; found
; found
= found
->next
) {
2142 if (!(found
->flags
& BAN_EXCEPTION
))
2144 slen
= strlen(found
->banstr
);
2145 if (buf_used
+ slen
+ 4 > 400) {
2146 buf
[buf_used
] = '\0';
2147 sendcmdto_one(acptr
, CMD_SILENCE
, cli_from(sptr
), "%C %s", sptr
, buf
);
2151 buf
[buf_used
++] = ',';
2152 buf
[buf_used
++] = '+';
2153 buf
[buf_used
++] = '~';
2154 memcpy(buf
+ buf_used
, found
->banstr
, slen
);
2157 /* Flush silence buffer. */
2159 buf
[buf_used
] = '\0';
2160 sendcmdto_one(acptr
, CMD_SILENCE
, cli_from(sptr
), "%C %s", sptr
, buf
);
2167 /** Send RPL_ISUPPORT lines to \a cptr.
2168 * @param[in] cptr Client to send ISUPPORT to.
2172 send_supported(struct Client
*cptr
)
2174 char featurebuf
[512];
2176 ircd_snprintf(0, featurebuf
, sizeof(featurebuf
), FEATURES1
, FEATURESVALUES1
);
2177 send_reply(cptr
, RPL_ISUPPORT
, featurebuf
);
2178 ircd_snprintf(0, featurebuf
, sizeof(featurebuf
), FEATURES2
, FEATURESVALUES2
);
2179 send_reply(cptr
, RPL_ISUPPORT
, featurebuf
);
2181 return 0; /* convenience return, if it's ever needed */