]> jfr.im git - irc/quakenet/snircd.git/blame - ircd/s_user.c
add sbounce from asuka into snircd tree
[irc/quakenet/snircd.git] / ircd / s_user.c
CommitLineData
189935b1 1/*
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
5 *
6 * See file AUTHORS in IRC package for additional names of
7 * the programmers.
8 *
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)
12 * any later version.
13 *
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.
18 *
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.
22 */
23/** @file
24 * @brief Miscellaneous user-related helper functions.
9f8856e9 25 * @version $Id: s_user.c,v 1.99.2.2 2006/02/16 03:49:54 entrope Exp $
189935b1 26 */
27#include "config.h"
28
29#include "s_user.h"
30#include "IPcheck.h"
31#include "channel.h"
32#include "class.h"
33#include "client.h"
34#include "hash.h"
35#include "ircd.h"
36#include "ircd_alloc.h"
189935b1 37#include "ircd_chattr.h"
38#include "ircd_features.h"
39#include "ircd_log.h"
40#include "ircd_reply.h"
41#include "ircd_snprintf.h"
42#include "ircd_string.h"
43#include "list.h"
44#include "match.h"
45#include "motd.h"
46#include "msg.h"
47#include "msgq.h"
48#include "numeric.h"
49#include "numnicks.h"
50#include "parse.h"
51#include "querycmds.h"
52#include "random.h"
9f8856e9 53#include "s_auth.h"
189935b1 54#include "s_bsd.h"
55#include "s_conf.h"
56#include "s_debug.h"
57#include "s_misc.h"
58#include "s_serv.h" /* max_client_count */
59#include "send.h"
60#include "struct.h"
61#include "supported.h"
62#include "sys.h"
63#include "userload.h"
64#include "version.h"
65#include "whowas.h"
66
67#include "handlers.h" /* m_motd and m_lusers */
68
69/* #include <assert.h> -- Now using assert in ircd_log.h */
70#include <fcntl.h>
71#include <stdio.h>
72#include <stdlib.h>
73#include <string.h>
74#include <sys/stat.h>
75
d8e74551 76static char *IsVhost(char *hostmask, int oper);
77static char *IsVhostPass(char *hostmask);
78
189935b1 79/** Count of allocated User structures. */
80static int userCount = 0;
81
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.
86 */
87struct User *make_user(struct Client *cptr)
88{
89 assert(0 != cptr);
90
91 if (!cli_user(cptr)) {
92 cli_user(cptr) = (struct User*) MyMalloc(sizeof(struct User));
93 assert(0 != cli_user(cptr));
94
95 /* All variables are 0 by default */
96 memset(cli_user(cptr), 0, sizeof(struct User));
97 ++userCount;
98 cli_user(cptr)->refcnt = 1;
99 }
100 return cli_user(cptr);
101}
102
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.
107 */
108void free_user(struct User* user)
109{
110 assert(0 != user);
111 assert(0 < user->refcnt);
112
113 if (--user->refcnt == 0) {
114 if (user->away)
115 MyFree(user->away);
116 /*
117 * sanity check
118 */
119 assert(0 == user->joined);
120 assert(0 == user->invited);
121 assert(0 == user->channel);
122
123 MyFree(user);
124 --userCount;
125 }
126}
127
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.
131 */
132void user_count_memory(size_t* count_out, size_t* bytes_out)
133{
134 assert(0 != count_out);
135 assert(0 != bytes_out);
136 *count_out = userCount;
137 *bytes_out = userCount * sizeof(struct User);
138}
139
140
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;
145 *
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.
149 */
150struct Client *next_client(struct Client *next, const char* ch)
151{
152 struct Client *tmp = next;
153
154 if (!tmp)
155 return NULL;
156
157 next = FindClient(ch);
158 next = next ? next : tmp;
159 if (cli_prev(tmp) == next)
160 return NULL;
161 if (next != tmp)
162 return next;
163 for (; next; next = cli_next(next))
164 if (!match(ch, cli_name(next)))
165 break;
166 return next;
167}
168
169/** Find the destination server for a command, and forward it if that is not us.
170 *
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
173 * user).
174 *
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.
185 */
186int 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[])
189{
190 struct Client *acptr;
191 char *to;
192
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);
196
197 if (MustBeOper && !IsPrivileged(from))
198 {
199 send_reply(from, ERR_NOPRIVILEGES);
200 return HUNTED_NOSUCH;
201 }
202
203 /* Make sure it's a server */
204 if (MyUser(from)) {
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;
210 }
211
212 if (cli_user(acptr))
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);
217 }
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 */
221 }
222
223 if (IsMe(acptr))
224 return (HUNTED_ISME);
225
226 if (MustBeOper && !IsPrivileged(from)) {
227 send_reply(from, ERR_NOPRIVILEGES);
228 return HUNTED_NOSUCH;
229 }
230
231 /* assert(!IsServer(from)); */
232
233 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
234
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]);
237
238 return (HUNTED_PASS);
239}
240
241/** Find the destination server for a command, and forward it (as a
242 * high-priority command) if that is not us.
243 *
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
246 * user).
247 * Unlike hunt_server_cmd(), this appends the message to the
248 * high-priority message queue for the destination server.
249 *
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.
260 */
261int 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,
264 char *parv[])
265{
266 struct Client *acptr;
267 char *to;
268
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);
272
273 /* Make sure it's a server */
274 if (MyUser(from)) {
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;
280 }
281
282 if (cli_user(acptr))
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);
287 }
288 } else if (!(acptr = FindNServer(to)))
289 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
290
291 if (IsMe(acptr))
292 return (HUNTED_ISME);
293
294 if (MustBeOper && !IsPrivileged(from)) {
295 send_reply(from, ERR_NOPRIVILEGES);
296 return HUNTED_NOSUCH;
297 }
298
299 /* assert(!IsServer(from)); SETTIME to particular destinations permitted */
300
301 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
302
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]);
305
306 return (HUNTED_PASS);
307}
308
309
189935b1 310/*
311 * register_user
312 *
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.
316 *
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:
321 *
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...
330 */
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.
338 *
339 * @param[in] cptr Client who introduced the user.
340 * @param[in,out] sptr Client who has been fully introduced.
189935b1 341 * @return Zero or CPTR_KILLED.
342 */
9f8856e9 343int register_user(struct Client *cptr, struct Client *sptr)
189935b1 344{
189935b1 345 char* parv[4];
346 char* tmpstr;
189935b1 347 struct User* user = cli_user(sptr);
189935b1 348 char ip_base64[25];
349
350 user->last = CurrentTime;
351 parv[0] = cli_name(sptr);
352 parv[1] = parv[2] = NULL;
353
354 if (MyConnect(sptr))
355 {
189935b1 356 assert(cptr == sptr);
189935b1 357
7fafc378 358 /* Have to set up "realusername" */
359 ircd_strncpy(user->realusername, user->username, USERLEN);
360
189935b1 361 Count_unknownbecomesclient(sptr, UserStats);
362
d8e74551 363 if (MyConnect(sptr) && feature_bool(FEAT_AUTOINVISIBLE))
364 SetInvisible(sptr);
365
366 if(MyConnect(sptr) && feature_bool(FEAT_SETHOST_AUTO)) {
367 if (conf_check_slines(sptr)) {
368 send_reply(sptr, RPL_USINGSLINE);
369 SetSetHost(sptr);
370 }
371 }
372
189935b1 373 SetUser(sptr);
374 cli_handler(sptr) = CLIENT_HANDLER;
375 SetLocalNumNick(sptr);
376 send_reply(sptr,
377 RPL_WELCOME,
378 feature_str(FEAT_NETWORK),
379 feature_str(FEAT_PROVIDER) ? " via " : "",
380 feature_str(FEAT_PROVIDER) ? feature_str(FEAT_PROVIDER) : "",
9f8856e9 381 cli_name(sptr));
189935b1 382 /*
383 * This is a duplicate of the NOTICE but see below...
384 */
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);
391 update_load();
392 motd_signon(sptr);
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 */);
401
402 IPcheck_connect_succeeded(sptr);
403 /*
404 * Set user's initial modes
405 */
406 tmpstr = (char*)client_get_default_umode(sptr);
407 if (tmpstr) for (; *tmpstr; ++tmpstr) {
408 switch (*tmpstr) {
409 case 's':
410 if (!feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY)) {
411 SetServNotice(sptr);
412 set_snomask(sptr, SNO_DEFAULT, SNO_SET);
413 }
414 break;
415 case 'w':
416 if (!feature_bool(FEAT_WALLOPS_OPER_ONLY))
417 SetWallops(sptr);
418 break;
419 case 'i':
420 SetInvisible(sptr);
421 break;
422 case 'd':
423 SetDeaf(sptr);
424 break;
425 case 'g':
426 if (!feature_bool(FEAT_HIS_DEBUG_OPER_ONLY))
427 SetDebug(sptr);
428 break;
429 }
430 }
431 }
432 else {
9f8856e9 433 struct Client *acptr = user->server;
189935b1 434
7fafc378 435 ircd_strncpy(user->username, cli_username(sptr), USERLEN);
9f8856e9 436 Count_newremoteclient(UserStats, acptr);
189935b1 437
189935b1 438 if (cli_from(acptr) != cli_from(sptr))
439 {
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");
445 }
446 else if (HasFlag(acptr, FLAG_TS8))
447 SetFlag(sptr, FLAG_TS8);
448
449 /*
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.
454 */
455 for (; acptr != &me; acptr = cli_serv(acptr)->up)
456 {
457 if (IsBurst(acptr) || Protocol(acptr) < 10)
458 break;
459 }
460 if (!IPcheck_remote_connect(sptr, (acptr != &me)))
461 {
462 /*
463 * We ran out of bits to count this
464 */
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");
468 }
d8e74551 469
470 if(MyConnect(sptr) && feature_bool(FEAT_SETHOST_AUTO)) {
471 if (conf_check_slines(sptr)) {
472 send_reply(sptr, RPL_USINGSLINE);
473 SetSetHost(sptr);
474 }
475 }
476
189935b1 477 SetUser(sptr);
478 }
479
480 if (IsInvisible(sptr))
481 ++UserStats.inv_clients;
482 if (IsOper(sptr))
483 ++UserStats.opers;
484
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",
9f8856e9 490 cli_name(sptr), cli_hopcount(sptr) + 1,
491 cli_lastnick(sptr),
d8e74551 492 user->realusername, user->realhost,
189935b1 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",
9f8856e9 500 cli_name(sptr), cli_hopcount(sptr) + 1,
501 cli_lastnick(sptr),
d8e74551 502 user->realusername, user->realhost,
189935b1 503 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
504 iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 0),
505 NumNick(sptr), cli_info(sptr));
506
507 /* Send user mode to client */
508 if (MyUser(sptr))
509 {
510 static struct Flags flags; /* automatically initialized to zeros */
052b069e 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.
513 */
514 if (IsAccount(cptr))
515 FlagSet(&flags, FLAG_ACCOUNT);
516 else
517 FlagClr(&flags, FLAG_ACCOUNT);
189935b1 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));
521 }
522 return 0;
523}
524
525/** List of user mode characters. */
526static const struct UserMode {
527 unsigned int flag; /**< User mode constant. */
528 char c; /**< Character corresponding to the mode. */
529} userModeList[] = {
530 { FLAG_OPER, 'o' },
531 { FLAG_LOCOP, 'O' },
532 { FLAG_INVISIBLE, 'i' },
533 { FLAG_WALLOP, 'w' },
534 { FLAG_SERVNOTICE, 's' },
535 { FLAG_DEAF, 'd' },
536 { FLAG_CHSERV, 'k' },
537 { FLAG_DEBUG, 'g' },
538 { FLAG_ACCOUNT, 'r' },
d8e74551 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' }
189935b1 546};
547
548/** Length of #userModeList. */
549#define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
550
551/*
552 * XXX - find a way to get rid of this
553 */
554/** Nasty global buffer used for communications with umode_str() and others. */
555static char umodeBuf[BUFSIZE];
556
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.
565 */
566int set_nick_name(struct Client* cptr, struct Client* sptr,
567 const char* nick, int parc, char* parv[])
568{
569 if (IsServer(sptr)) {
570 int i;
571 const char* account = 0;
d8e74551 572 char* hostmask = 0;
573 char* host = 0;
f687a4d7 574 const char* p, *pp;
189935b1 575
576 /*
577 * A server introducing a new client, change source
578 */
579 struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
580 assert(0 != new_client);
581
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] == '+')
585 {
586 for (p = parv[6] + 1; *p; p++)
587 {
588 for (i = 0; i < USERMODELIST_SIZE; ++i)
589 {
590 if (userModeList[i].c == *p)
591 {
592 SetFlag(new_client, userModeList[i].flag);
593 if (userModeList[i].flag == FLAG_ACCOUNT)
594 account = parv[7];
d8e74551 595 if (userModeList[i].flag == FLAG_SETHOST)
596 hostmask = parv[parc - 4];
189935b1 597 break;
598 }
599 }
600 }
601 }
602 client_set_privs(new_client, NULL); /* set privs on user */
603 /*
604 * Set new nick name.
605 */
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]);
610 /*
611 * IP# of remote client
612 */
613 base64toip(parv[parc - 3], &cli_ip(new_client));
614
615 add_client_to_list(new_client);
616 hAddClient(new_client);
617
618 cli_serv(sptr)->ghost = 0; /* :server NICK means end of net.burst */
619 ircd_strncpy(cli_username(new_client), parv[4], USERLEN);
d8e74551 620 ircd_strncpy(cli_user(new_client)->realusername, parv[4], USERLEN);
189935b1 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);
624 if (account) {
625 int len = ACCOUNTLEN;
626 if ((p = strchr(account, ':'))) {
627 len = (p++) - account;
628 cli_user(new_client)->acc_create = atoi(p);
f687a4d7 629 if ((pp = strchr(p, ':')))
630 cli_user(new_client)->acc_id = atoi(pp + 1);
189935b1 631 Debug((DEBUG_DEBUG, "Received timestamped account in user mode; "
f687a4d7 632 "account \"%s\", timestamp %Tu id %u", account,
633 cli_user(new_client)->acc_create,
634 cli_user(new_client)->acc_id));
189935b1 635 }
636 ircd_strncpy(cli_user(new_client)->account, account, len);
637 }
638 if (HasHiddenHost(new_client))
639 ircd_snprintf(0, cli_user(new_client)->host, HOSTLEN, "%s.%s",
640 account, feature_str(FEAT_HIDDEN_HOST));
d8e74551 641 if (HasSetHost(new_client)) {
642 if ((host = strrchr(hostmask, '@')) != NULL) {
643 *host++ = '\0';
644 ircd_strncpy(cli_username(new_client), hostmask, USERLEN);
645 ircd_strncpy(cli_user(new_client)->host, host, HOSTLEN);
646 }
647 }
189935b1 648
9f8856e9 649 return register_user(cptr, new_client);
189935b1 650 }
651 else if ((cli_name(sptr))[0]) {
652 /*
653 * Client changing its nick
654 *
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.
658 */
659 if (MyUser(sptr)) {
660 const char* channel_name;
661 struct Membership *member;
d8e74551 662 if ((channel_name = find_no_nickchange_channel(sptr)) && !IsXtraOp(sptr)) {
189935b1 663 return send_reply(cptr, ERR_BANNICKCHANGE, channel_name);
664 }
665 /*
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
672 */
673 if (CurrentTime < cli_nextnick(cptr))
674 {
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! */
682 }
683 else {
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;
689 }
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);
694 }
695 /*
696 * Also set 'lastnick' to current time, if changed.
697 */
698 if (0 != ircd_strcmp(parv[0], nick))
699 cli_lastnick(sptr) = (sptr == cptr) ? TStime() : atoi(parv[2]);
700
701 /*
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.
705 */
706 if (IsUser(sptr)) {
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,
710 cli_lastnick(sptr));
711 }
712 else
713 sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick);
714
715 if ((cli_name(sptr))[0])
716 hRemClient(sptr);
717 strcpy(cli_name(sptr), nick);
718 hAddClient(sptr);
719 }
720 else {
721 /* Local client setting NICK the first time */
189935b1 722 strcpy(cli_name(sptr), nick);
189935b1 723 hAddClient(sptr);
9f8856e9 724 return auth_set_nick(cli_auth(sptr), nick);
189935b1 725 }
726 return 0;
727}
728
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.
732 */
733static unsigned char hash_target(unsigned int target)
734{
735 return (unsigned char) (target >> 16) ^ (target >> 8);
736}
737
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.
741 */
742void
743add_target(struct Client *sptr, void *target)
744{
745 /* Ok, this shouldn't work esp on alpha
746 */
747 unsigned char hash = hash_target((unsigned long) target);
748 unsigned char* targets;
749 int i;
750 assert(0 != sptr);
751 assert(cli_local(sptr));
752
753 targets = cli_targets(sptr);
754
755 /*
756 * Already in table?
757 */
758 for (i = 0; i < MAXTARGETS; ++i) {
759 if (targets[i] == hash)
760 return;
761 }
762 /*
763 * New target
764 */
765 memmove(&targets[RESERVEDTARGETS + 1],
766 &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
767 targets[RESERVEDTARGETS] = hash;
768}
769
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.
776 */
777int check_target_limit(struct Client *sptr, void *target, const char *name,
778 int created)
779{
780 unsigned char hash = hash_target((unsigned long) target);
781 int i;
782 unsigned char* targets;
783
784 assert(0 != sptr);
785 assert(cli_local(sptr));
786 targets = cli_targets(sptr);
787
788 /* If user is invited to channel, give him/her a free target */
789 if (IsChannelName(name) && IsInvited(sptr, target))
790 return 0;
791
d8e74551 792 /* opers always have a free target */
793 if (IsAnOper(sptr))
794 return 0;
795
189935b1 796 /*
797 * Same target as last time?
798 */
799 if (targets[0] == hash)
800 return 0;
801 for (i = 1; i < MAXTARGETS; ++i) {
802 if (targets[i] == hash) {
803 memmove(&targets[1], &targets[0], i);
804 targets[0] = hash;
805 return 0;
806 }
807 }
808 /*
809 * New target
810 */
811 if (!created) {
812 if (CurrentTime < cli_nexttarget(sptr)) {
813 if (cli_nexttarget(sptr) - CurrentTime < TARGET_DELAY + 8) {
814 /*
815 * No server flooding
816 */
817 cli_nexttarget(sptr) += 2;
818 send_reply(sptr, ERR_TARGETTOOFAST, name,
819 cli_nexttarget(sptr) - CurrentTime);
820 }
821 return 1;
822 }
823 else {
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));
827 }
828 }
829 memmove(&targets[1], &targets[0], MAXTARGETS - 1);
830 targets[0] = hash;
831 return 0;
832}
833
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.
841 */
842/* Added 971023 by Run. */
843int whisper(struct Client* source, const char* nick, const char* channel,
844 const char* text, int is_notice)
845{
846 struct Client* dest;
847 struct Channel* chptr;
848 struct Membership* membership;
849
850 assert(0 != source);
851 assert(0 != nick);
852 assert(0 != channel);
853 assert(MyUser(source));
854
855 if (!(dest = FindUser(nick))) {
856 return send_reply(source, ERR_NOSUCHNICK, nick);
857 }
858 if (!(chptr = FindChannel(channel))) {
859 return send_reply(source, ERR_NOSUCHCHANNEL, channel);
860 }
861 /*
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
865 */
866 for (membership = cli_user(source)->channel; membership; membership = membership->next_channel) {
867 if (chptr == membership->channel)
868 break;
869 }
870 if (0 == membership) {
871 return send_reply(source, ERR_NOTONCHANNEL, chptr->chname);
872 }
873 if (!IsVoicedOrOpped(membership)) {
874 return send_reply(source, ERR_VOICENEEDED, chptr->chname);
875 }
876 /*
877 * lookup channel in destination
878 */
879 assert(0 != cli_user(dest));
880 for (membership = cli_user(dest)->channel; membership; membership = membership->next_channel) {
881 if (chptr == membership->channel)
882 break;
883 }
884 if (0 == membership || IsZombie(membership)) {
885 return send_reply(source, ERR_USERNOTINCHANNEL, cli_name(dest), chptr->chname);
886 }
887 if (is_silenced(source, dest))
888 return 0;
889
890 if (cli_user(dest)->away)
891 send_reply(source, RPL_AWAY, cli_name(dest), cli_user(dest)->away);
892 if (is_notice)
893 sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text);
894 else
895 sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text);
896 return 0;
897}
898
899
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.
905 */
906void send_umode_out(struct Client *cptr, struct Client *sptr,
907 struct Flags *old, int prop)
908{
909 int i;
910 struct Client *acptr;
911
912 send_umode(NULL, sptr, old, prop ? SEND_UMODES : SEND_UMODES_BUT_OPER);
913
914 for (i = HighestFd; i >= 0; i--)
915 {
916 if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
917 (acptr != cptr) && (acptr != sptr) && *umodeBuf)
d8e74551 918 sendcmdto_one(sptr, CMD_MODE, acptr, "%s %s", cli_name(sptr), umodeBuf);
189935b1 919 }
920 if (cptr && MyUser(cptr))
921 send_umode(cptr, sptr, old, ALL_UMODES);
922}
923
924
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.
930 */
931void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
932{
933 char* name;
934 char* p = 0;
935 int arg_count = 0;
936 int users_found = 0;
937 struct Client* acptr;
938 struct MsgBuf* mb;
939
940 assert(0 != sptr);
941 assert(0 != names);
942 assert(0 != fmt);
943
944 mb = msgq_make(sptr, rpl_str(rpl), cli_name(&me), cli_name(sptr));
945
946 for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
947 if ((acptr = FindUser(name))) {
948 if (users_found++)
949 msgq_append(0, mb, " ");
950 (*fmt)(acptr, sptr, mb);
951 }
952 if (5 == ++arg_count)
953 break;
954 }
955 send_buffer(sptr, mb, 0);
956 msgq_clean(mb);
957}
958
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).
962 * @return Zero.
963 */
964int
965hide_hostmask(struct Client *cptr, unsigned int flag)
966{
967 struct Membership *chan;
968
969 switch (flag) {
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))
973 return 0;
d8e74551 974 /* If the user is +h, we don't hide the hostmask. Set the flag to keep sync though */
975 if (HasSetHost(cptr)) {
976 SetFlag(cptr, flag);
977 return 0;
978 }
189935b1 979 break;
980 case FLAG_ACCOUNT:
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)
984 ClearBanValid(chan);
985 break;
986 default:
987 return 0;
988 }
989
990 SetFlag(cptr, flag);
991 if (!HasFlag(cptr, FLAG_HIDDENHOST) || !HasFlag(cptr, FLAG_ACCOUNT))
992 return 0;
993
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));
997
998 /* ok, the client is now fully hidden, so let them know -- hikari */
999 if (MyConnect(cptr))
1000 send_reply(cptr, RPL_HOSTHIDDEN, cli_user(cptr)->host);
1001
1002 /*
1003 * Go through all channels the client was on, rejoin him
1004 * and set the modes, if any
1005 */
1006 for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel)
1007 {
1008 if (IsZombie(chan))
1009 continue;
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,
1017 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);
1021 }
1022 return 0;
1023}
1024
d8e74551 1025/*
1026 * set_hostmask() - derived from hide_hostmask()
1027 *
1028 */
1029int set_hostmask(struct Client *cptr, char *hostmask, char *password)
1030{
1031 int restore = 0;
1032 int freeform = 0;
1033 char *host, *new_vhost, *vhost_pass;
1034 char hiddenhost[USERLEN + HOSTLEN + 2];
1035 struct Membership *chan;
1036
1037 Debug((DEBUG_INFO, "set_hostmask() %C, %s, %s", cptr, hostmask, password));
1038
1039 /* sethost enabled? */
1040 if (MyConnect(cptr) && !feature_bool(FEAT_SETHOST)) {
1041 send_reply(cptr, ERR_DISABLED, "SETHOST");
1042 return 0;
1043 }
1044
1045 /* sethost enabled for users? */
1046 if (MyConnect(cptr) && !IsAnOper(cptr) && !feature_bool(FEAT_SETHOST_USER)) {
1047 send_reply(cptr, ERR_NOPRIVILEGES);
1048 return 0;
1049 }
1050
1051 /* MODE_DEL: restore original hostmask */
1052 if (EmptyString(hostmask)) {
1053 /* is already sethost'ed? */
1054 if (IsSetHost(cptr)) {
1055 restore = 1;
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));
1061 else
1062 strncpy(cli_user(cptr)->host, cli_user(cptr)->realhost, HOSTLEN);
1063 strncpy(cli_user(cptr)->username, cli_user(cptr)->realusername, USERLEN);
1064 /* log it */
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);
1069 } else
1070 return 0;
1071 /* MODE_ADD: set a new hostmask */
1072 } else {
1073 /* chop up ident and host.cc */
1074 if ((host = strrchr(hostmask, '@'))) /* oper can specifiy ident@host.cc */
1075 *host++ = '\0';
1076 else /* user can only specifiy host.cc [password] */
1077 host = hostmask;
1078 /*
1079 * Oper sethost
1080 */
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);
1089 return 0;
1090 } else /* freeform active, log and go */
1091 freeform = 1;
1092 }
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);
1098 /* log it */
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" : "");
1103 /*
1104 * plain user sethost, handled here
1105 */
1106 } else {
1107 /* empty password? */
1108 if (EmptyString(password)) {
1109 send_reply(cptr, ERR_NEEDMOREPARAMS, "MODE");
1110 return 0;
1111 }
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);
1117 return 0;
1118 }
1119 /* no password */
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);
1124 return 0;
1125 }
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);
1131 return 0;
1132 }
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);
1136 /* log it */
1137 log_write(LS_SETHOST, L_INFO, LOG_NOSNOTICE, "SETHOST (%s@%s) by (%#R)",
1138 cli_user(cptr)->username, cli_user(cptr)->host, cptr);
1139 }
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);
1145 }
1146 }
1147
1148 if (restore)
1149 ClearSetHost(cptr);
1150 else
1151 SetSetHost(cptr);
1152
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);
1157 }
1158
1159#if 0
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. */
1162 /*
1163 * Go through all channels the client was on, rejoin him
1164 * and set the modes, if any
1165 */
1166 for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel) {
1167 if (IsZombie(chan))
1168 continue;
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);
1177 }
1178 }
1179#endif
1180
1181 /*
1182 * Go through all channels the client was on, rejoin him
1183 * and set the modes, if any
1184 */
1185 for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel) {
1186 if (IsZombie(chan))
1187 continue;
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);
1194 } else {
1195 sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, 0,
1196 "%H", chan->channel);
1197 }
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);
1204 }
1205 }
1206 return 1;
1207}
1208
189935b1 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.
1213 *
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.
1218 * @return Zero.
1219 */
1220int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
1221{
1222 char** p;
1223 char* m;
1224 struct Client *acptr;
1225 int what;
1226 int i;
1227 struct Flags setflags;
1228 unsigned int tmpmask = 0;
1229 int snomask_given = 0;
1230 char buf[BUFSIZE];
d8e74551 1231 char *hostmask, *password;
189935b1 1232 int prop = 0;
1233 int do_host_hiding = 0;
d8e74551 1234 int do_set_host = 0;
189935b1 1235
d8e74551 1236 hostmask = password = NULL;
189935b1 1237 what = MODE_ADD;
1238
1239 if (parc < 2)
1240 return need_more_params(sptr, "MODE");
1241
1242 if (!(acptr = FindUser(parv[1])))
1243 {
1244 if (MyConnect(sptr))
1245 send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]);
1246 return 0;
1247 }
1248
1249 if (IsServer(sptr) || sptr != acptr)
1250 {
1251 if (IsServer(cptr))
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));
1255 else
1256 send_reply(sptr, ERR_USERSDONTMATCH);
1257 return 0;
1258 }
1259
1260 if (parc < 3)
1261 {
1262 m = buf;
1263 *m++ = '+';
1264 for (i = 0; i < USERMODELIST_SIZE; i++)
1265 {
1266 if (HasFlag(sptr, userModeList[i].flag) &&
d8e74551 1267 ((userModeList[i].flag != FLAG_ACCOUNT) &&
1268 (userModeList[i].flag != FLAG_SETHOST)))
189935b1 1269 *m++ = userModeList[i].c;
1270 }
1271 *m = '\0';
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));
1277 return 0;
1278 }
1279
1280 /*
1281 * find flags already set for user
1282 * why not just copy them?
1283 */
1284 setflags = cli_flags(sptr);
1285
1286 if (MyConnect(sptr))
1287 tmpmask = cli_snomask(sptr);
1288
1289 /*
1290 * parse mode change string(s)
1291 */
1292 for (p = &parv[2]; *p; p++) { /* p is changed in loop too */
1293 for (m = *p; *m; m++) {
1294 switch (*m) {
1295 case '+':
1296 what = MODE_ADD;
1297 break;
1298 case '-':
1299 what = MODE_DEL;
1300 break;
1301 case 's':
1302 if (*(p + 1) && is_snomask(*(p + 1))) {
1303 snomask_given = 1;
1304 tmpmask = umode_make_snomask(tmpmask, *++p, what);
1305 tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1306 }
1307 else
1308 tmpmask = (what == MODE_ADD) ?
1309 (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1310 if (tmpmask)
1311 SetServNotice(sptr);
1312 else
1313 ClearServNotice(sptr);
1314 break;
1315 case 'w':
1316 if (what == MODE_ADD)
1317 SetWallops(sptr);
1318 else
1319 ClearWallops(sptr);
1320 break;
1321 case 'o':
1322 if (what == MODE_ADD)
1323 SetOper(sptr);
1324 else {
1325 ClrFlag(sptr, FLAG_OPER);
1326 ClrFlag(sptr, FLAG_LOCOP);
1327 if (MyConnect(sptr))
1328 {
1329 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1330 cli_handler(sptr) = CLIENT_HANDLER;
1331 }
1332 }
1333 break;
1334 case 'O':
1335 if (what == MODE_ADD)
1336 SetLocOp(sptr);
1337 else
1338 {
1339 ClrFlag(sptr, FLAG_OPER);
1340 ClrFlag(sptr, FLAG_LOCOP);
1341 if (MyConnect(sptr))
1342 {
1343 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1344 cli_handler(sptr) = CLIENT_HANDLER;
1345 }
1346 }
1347 break;
1348 case 'i':
1349 if (what == MODE_ADD)
1350 SetInvisible(sptr);
1351 else
d8e74551 1352 if (!feature_bool(FEAT_AUTOINVISIBLE) || IsOper(sptr)) /* Don't allow non-opers to -i if FEAT_AUTOINVISIBLE is set */
1353 ClearInvisible(sptr);
189935b1 1354 break;
1355 case 'd':
1356 if (what == MODE_ADD)
1357 SetDeaf(sptr);
1358 else
1359 ClearDeaf(sptr);
1360 break;
1361 case 'k':
1362 if (what == MODE_ADD)
1363 SetChannelService(sptr);
1364 else
1365 ClearChannelService(sptr);
1366 break;
d8e74551 1367 case 'X':
1368 if (what == MODE_ADD)
1369 SetXtraOp(sptr);
1370 else
1371 ClearXtraOp(sptr);
1372 break;
1373 case 'n':
1374 if (what == MODE_ADD)
1375 SetNoChan(sptr);
1376 else
1377 ClearNoChan(sptr);
1378 break;
1379 case 'I':
1380 if (what == MODE_ADD)
1381 SetNoIdle(sptr);
1382 else
1383 ClearNoIdle(sptr);
1384 break;
189935b1 1385 case 'g':
1386 if (what == MODE_ADD)
1387 SetDebug(sptr);
1388 else
1389 ClearDebug(sptr);
1390 break;
1391 case 'x':
1392 if (what == MODE_ADD)
d8e74551 1393 do_host_hiding = 1;
1394 break;
1395 case 'h':
1396 if (what == MODE_ADD) {
1397 if (*(p + 1) && is_hostmask(*(p + 1))) {
1398 do_set_host = 1;
1399 hostmask = *++p;
1400 /* DON'T step p onto the trailing NULL in the parameter array! - splidge */
1401 if (*(p+1))
1402 password = *++p;
1403 else
1404 password = NULL;
1405 } else {
1406 if (!*(p+1))
1407 send_reply(sptr, ERR_NEEDMOREPARAMS, "SETHOST");
1408 else {
1409 send_reply(sptr, ERR_BADHOSTMASK, *(p+1));
1410 p++; /* Swallow the arg anyway */
1411 }
1412 }
1413 } else { /* MODE_DEL */
1414 do_set_host = 1;
1415 hostmask = NULL;
1416 password = NULL;
1417 }
1418 break;
1419 case 'R':
1420 if (what == MODE_ADD)
1421 SetAccountOnly(sptr);
1422 else
1423 ClearAccountOnly(sptr);
1424 break;
1425 case 'P':
1426 if (what == MODE_ADD)
1427 SetParanoid(sptr);
1428 else
1429 ClearParanoid(sptr);
ebebfa5f 1430 break;
189935b1 1431 default:
1432 send_reply(sptr, ERR_UMODEUNKNOWNFLAG, *m);
1433 break;
1434 }
1435 }
1436 }
1437 /*
1438 * Evaluate rules for new user mode
1439 * Stop users making themselves operators too easily:
1440 */
1441 if (!IsServer(cptr))
1442 {
1443 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr))
1444 ClearOper(sptr);
1445 if (!FlagHas(&setflags, FLAG_LOCOP) && IsLocOp(sptr))
1446 ClearLocOp(sptr);
1447 /*
1448 * new umode; servers can set it, local users cannot;
1449 * prevents users from /kick'ing or /mode -o'ing
1450 */
d8e74551 1451 if (!FlagHas(&setflags, FLAG_CHSERV) && !IsOper(sptr))
189935b1 1452 ClearChannelService(sptr);
d8e74551 1453 if (!FlagHas(&setflags, FLAG_XTRAOP) && !IsOper(sptr))
1454 ClearXtraOp(sptr);
1455 if (!FlagHas(&setflags, FLAG_NOCHAN) && !(IsOper(sptr) || feature_bool(FEAT_USER_HIDECHANS)))
1456 ClearNoChan(sptr);
caa12862 1457 if (!FlagHas(&setflags, FLAG_NOIDLE) && !(IsOper(sptr) || feature_bool(FEAT_USER_HIDEIDLETIME)))
d8e74551 1458 ClearNoIdle(sptr);
1459 if (!FlagHas(&setflags, FLAG_PARANOID) && !IsOper(sptr))
1460 ClearParanoid(sptr);
1461
189935b1 1462 /*
1463 * only send wallops to opers
1464 */
1465 if (feature_bool(FEAT_WALLOPS_OPER_ONLY) && !IsAnOper(sptr) &&
1466 !FlagHas(&setflags, FLAG_WALLOP))
1467 ClearWallops(sptr);
1468 if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY) && MyConnect(sptr) &&
1469 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_SERVNOTICE))
1470 {
1471 ClearServNotice(sptr);
1472 set_snomask(sptr, 0, SNO_SET);
1473 }
1474 if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY) &&
1475 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_DEBUG))
1476 ClearDebug(sptr);
1477 }
1478 if (MyConnect(sptr))
1479 {
1480 if ((FlagHas(&setflags, FLAG_OPER) || FlagHas(&setflags, FLAG_LOCOP)) &&
1481 !IsAnOper(sptr))
1482 det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPERATOR);
1483
1484 if (SendServNotice(sptr))
1485 {
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));
1490 }
1491 else
1492 set_snomask(sptr, 0, SNO_SET);
1493 }
1494 /*
1495 * Compare new flags with old flags and send string which
1496 * will cause servers to update correctly.
1497 */
1498 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr))
1499 {
1500 /* user now oper */
1501 ++UserStats.opers;
1502 client_set_privs(sptr, NULL); /* may set propagate privilege */
1503 }
1504 /* remember propagate privilege setting */
1505 if (HasPriv(sptr, PRIV_PROPAGATE))
1506 prop = 1;
1507 if (FlagHas(&setflags, FLAG_OPER) && !IsOper(sptr))
1508 {
1509 /* user no longer oper */
1510 --UserStats.opers;
1511 client_set_privs(sptr, NULL); /* will clear propagate privilege */
1512 }
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);
d8e74551 1519 if (do_set_host) {
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);
1524 }
189935b1 1525 send_umode_out(cptr, sptr, &setflags, prop);
1526
1527 return 0;
1528}
1529
1530/** Build a mode string to describe modes for \a cptr.
1531 * @param[in] cptr Some user.
1532 * @return Pointer to a static buffer.
1533 */
1534char *umode_str(struct Client *cptr)
1535{
1536 /* Maximum string size: "owidgrx\0" */
1537 char *m = umodeBuf;
1538 int i;
1539 struct Flags c_flags = cli_flags(cptr);
1540
1541 if (!HasPriv(cptr, PRIV_PROPAGATE))
1542 FlagClr(&c_flags, FLAG_OPER);
1543
1544 for (i = 0; i < USERMODELIST_SIZE; ++i)
1545 {
1546 if (FlagHas(&c_flags, userModeList[i].flag) &&
1547 userModeList[i].flag >= FLAG_GLOBAL_UMODES)
1548 *m++ = userModeList[i].c;
1549 }
1550
1551 if (IsAccount(cptr))
1552 {
1553 char* t = cli_user(cptr)->account;
1554
1555 *m++ = ' ';
1556 while ((*m++ = *t++))
1557 ; /* Empty loop */
1558
1559 if (cli_user(cptr)->acc_create) {
1560 char nbuf[20];
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));
f687a4d7 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);
1567 } else {
1568 ircd_snprintf(0, t = nbuf, sizeof(nbuf), ":%Tu",
1569 cli_user(cptr)->acc_create);
1570 }
189935b1 1571 m--; /* back up over previous nul-termination */
1572 while ((*m++ = *t++))
1573 ; /* Empty loop */
1574 }
d8e74551 1575 m--; /* Step back over the '\0' */
189935b1 1576 }
1577
d8e74551 1578 if (IsSetHost(cptr)) {
1579 *m++ = ' ';
1580 ircd_snprintf(0, m, USERLEN + HOSTLEN + 2, "%s@%s", cli_user(cptr)->username,
1581 cli_user(cptr)->host);
1582 } else
1583 *m = '\0';
189935b1 1584 return umodeBuf; /* Note: static buffer, gets
1585 overwritten by send_umode() */
1586}
1587
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.
1594 */
1595void send_umode(struct Client *cptr, struct Client *sptr, struct Flags *old,
1596 int sendset)
1597{
1598 int i;
1599 int flag;
d8e74551 1600 int needhost = 0;
189935b1 1601 char *m;
1602 int what = MODE_NULL;
1603
1604 /*
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.
1608 */
1609 m = umodeBuf;
1610 *m = '\0';
1611 for (i = 0; i < USERMODELIST_SIZE; ++i)
1612 {
1613 flag = userModeList[i].flag;
1614 if (FlagHas(old, flag)
1615 == HasFlag(sptr, flag))
1616 continue;
1617 switch (sendset)
1618 {
1619 case ALL_UMODES:
1620 break;
1621 case SEND_UMODES_BUT_OPER:
1622 if (flag == FLAG_OPER)
1623 continue;
1624 /* and fall through */
1625 case SEND_UMODES:
1626 if (flag < FLAG_GLOBAL_UMODES)
1627 continue;
1628 break;
1629 }
d8e74551 1630 /* Special case for SETHOST.. */
1631 if (flag == FLAG_SETHOST) {
1632 /* Don't send to users */
1633 if (cptr && MyUser(cptr))
1634 continue;
1635
1636 /* If we're setting +h, add the parameter later */
1637 if (!FlagHas(old, flag))
1638 needhost++;
1639 }
189935b1 1640 if (FlagHas(old, flag))
1641 {
1642 if (what == MODE_DEL)
1643 *m++ = userModeList[i].c;
1644 else
1645 {
1646 what = MODE_DEL;
1647 *m++ = '-';
1648 *m++ = userModeList[i].c;
1649 }
1650 }
1651 else /* !FlagHas(old, flag) */
1652 {
1653 if (what == MODE_ADD)
1654 *m++ = userModeList[i].c;
1655 else
1656 {
1657 what = MODE_ADD;
1658 *m++ = '+';
1659 *m++ = userModeList[i].c;
1660 }
1661 }
1662 }
d8e74551 1663 if (needhost) {
1664 *m++ = ' ';
1665 ircd_snprintf(0, m, USERLEN + HOSTLEN + 1, "%s@%s", cli_user(sptr)->username,
1666 cli_user(sptr)->host);
1667 } else
1668 *m = '\0';
189935b1 1669 if (*umodeBuf && cptr)
d8e74551 1670 sendcmdto_one(sptr, CMD_MODE, cptr, "%s %s", cli_name(sptr), umodeBuf);
189935b1 1671}
1672
1673/**
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.
1679 */
1680int is_snomask(char *word)
1681{
1682 if (word)
1683 {
1684 for (; *word; word++)
1685 if (IsDigit(*word))
1686 return 1;
1687 else if (IsAlpha(*word))
1688 return 0;
1689 }
1690 return 0;
1691}
1692
d8e74551 1693 /*
1694 * Check to see if it resembles a valid hostmask.
1695 */
1696int is_hostmask(char *word)
1697{
1698 int i = 0;
1699 char *host;
1700
1701 Debug((DEBUG_INFO, "is_hostmask() %s", word));
1702
1703 if (strlen(word) > (HOSTLEN + USERLEN + 1) || strlen(word) <= 0)
1704 return 0;
1705
1706 /* if a host is specified, make sure it's valid */
1707 host = strrchr(word, '@');
1708 if (host) {
1709 if (strlen(++host) < 1)
1710 return 0;
1711 if (strlen(host) > HOSTLEN)
1712 return 0;
1713 }
1714
1715 if (word) {
1716 if ('@' == *word) /* no leading @'s */
1717 return 0;
1718
1719 if ('#' == *word) { /* numeric index given? */
1720 for (word++; *word; word++) {
1721 if (!IsDigit(*word))
1722 return 0;
1723 }
1724 return 1;
1725 }
1726
1727 /* normal hostmask, account for at most one '@' */
1728 for (; *word; word++) {
1729 if ('@' == *word) {
1730 i++;
1731 continue;
1732 }
1733 if (!IsHostChar(*word))
1734 return 0;
1735 }
1736 return (1 < i) ? 0 : 1; /* no more than on '@' */
1737 }
1738 return 0;
1739}
1740
1741 /*
1742 * IsVhost() - Check if given host is a valid spoofhost
1743 * (ie: configured thru a S:line)
1744 */
1745static char *IsVhost(char *hostmask, int oper)
1746{
1747 unsigned int i = 0, y = 0;
1748 struct sline *sconf;
1749
1750 Debug((DEBUG_INFO, "IsVhost() %s", hostmask));
1751
1752 if (EmptyString(hostmask))
1753 return NULL;
1754
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))
1760 continue;
1761 if (y == ++i)
1762 return sconf->spoofhost;
1763 }
1764 return NULL;
1765 }
1766
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;
1771
1772 return NULL;
1773}
1774
1775 /*
1776 * IsVhostPass() - Check if given spoofhost has a password
1777 * associated with it, and if, return the password (cleartext)
1778 */
1779static char *IsVhostPass(char *hostmask)
1780{
1781 struct sline *sconf;
1782
1783 Debug((DEBUG_INFO, "IsVhostPass() %s", hostmask));
1784
1785 if (EmptyString(hostmask))
1786 return NULL;
1787
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;
1792 }
1793
1794 return NULL;
1795}
1796
189935b1 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.
1802 */
1803unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
1804{
1805 unsigned int sno_what;
1806 unsigned int newmask;
1807 if (*arg == '+')
1808 {
1809 arg++;
1810 if (what == MODE_ADD)
1811 sno_what = SNO_ADD;
1812 else
1813 sno_what = SNO_DEL;
1814 }
1815 else if (*arg == '-')
1816 {
1817 arg++;
1818 if (what == MODE_ADD)
1819 sno_what = SNO_DEL;
1820 else
1821 sno_what = SNO_ADD;
1822 }
1823 else
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)
1830 newmask |= oldmask;
1831 return newmask;
1832}
1833
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.
1837 */
1838static void delfrom_list(struct Client *cptr, struct SLink **list)
1839{
1840 struct SLink* tmp;
1841 struct SLink* prv = NULL;
1842
1843 for (tmp = *list; tmp; tmp = tmp->next) {
1844 if (tmp->value.cptr == cptr) {
1845 if (prv)
1846 prv->next = tmp->next;
1847 else
1848 *list = tmp->next;
1849 free_link(tmp);
1850 break;
1851 }
1852 prv = tmp;
1853 }
1854}
1855
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.
1860 */
1861void set_snomask(struct Client *cptr, unsigned int newmask, int what)
1862{
1863 unsigned int oldmask, diffmask; /* unsigned please */
1864 int i;
1865 struct SLink *tmp;
1866
1867 oldmask = cli_snomask(cptr);
1868
1869 if (what == SNO_ADD)
1870 newmask |= oldmask;
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);
1875
1876 newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
1877
1878 diffmask = oldmask ^ newmask;
1879
1880 for (i = 0; diffmask >> i; i++) {
1881 if (((diffmask >> i) & 1))
1882 {
1883 if (((newmask >> i) & 1))
1884 {
1885 tmp = make_link();
1886 tmp->next = opsarray[i];
1887 tmp->value.cptr = cptr;
1888 opsarray[i] = tmp;
1889 }
1890 else
1891 /* not real portable :( */
1892 delfrom_list(cptr, &opsarray[i]);
1893 }
1894 }
1895 cli_snomask(cptr) = newmask;
1896}
1897
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.
1905 */
1906int is_silenced(struct Client *sptr, struct Client *acptr)
1907{
1908 struct Ban *found;
1909 struct User *user;
1910 size_t buf_used, slen;
1911 char buf[BUFSIZE];
1912
1913 if (IsServer(sptr) || !(user = cli_user(acptr))
1914 || !(found = find_ban(sptr, user->silence)))
1915 return 0;
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))
1924 continue;
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);
1929 buf_used = 0;
1930 }
1931 if (buf_used)
1932 buf[buf_used++] = ',';
1933 buf[buf_used++] = '+';
1934 buf[buf_used++] = '~';
1935 memcpy(buf + buf_used, found->banstr, slen);
1936 buf_used += slen;
1937 }
1938 /* Flush silence buffer. */
1939 if (buf_used) {
1940 buf[buf_used] = '\0';
1941 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf);
1942 buf_used = 0;
1943 }
1944 }
1945 return 1;
1946}
1947
1948/** Send RPL_ISUPPORT lines to \a cptr.
1949 * @param[in] cptr Client to send ISUPPORT to.
1950 * @return Zero.
1951 */
1952int
1953send_supported(struct Client *cptr)
1954{
1955 char featurebuf[512];
1956
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);
1961
1962 return 0; /* convenience return, if it's ever needed */
1963}