fixed propagation of user mode changes (user should ALWAYS be notified)
[ircu2.10.12-pk.git] / ircd / s_user.c
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.
25  * @version $Id: s_user.c 1864 2008-03-15 05:33:22Z entrope $
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"
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"
53 #include "s_auth.h"
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
76 /** Count of allocated User structures. */
77 static int userCount = 0;
78
79 /** Makes sure that \a cptr has a User information block.
80  * If cli_user(cptr) != NULL, does nothing.
81  * @param[in] cptr Client to attach User struct to.
82  * @return User struct associated with \a cptr.
83  */
84 struct User *make_user(struct Client *cptr)
85 {
86   assert(0 != cptr);
87
88   if (!cli_user(cptr)) {
89     cli_user(cptr) = (struct User*) MyMalloc(sizeof(struct User));
90     assert(0 != cli_user(cptr));
91
92     /* All variables are 0 by default */
93     memset(cli_user(cptr), 0, sizeof(struct User));
94     ++userCount;
95     cli_user(cptr)->refcnt = 1;
96   }
97   return cli_user(cptr);
98 }
99
100 /** Dereference \a user.
101  * User structures are reference-counted; if the refcount of \a user
102  * becomes zero, free it.
103  * @param[in] user User to dereference.
104  */
105 void free_user(struct User* user)
106 {
107   assert(0 != user);
108   assert(0 < user->refcnt);
109
110   if (--user->refcnt == 0) {
111     if (user->away)
112       MyFree(user->away);
113     if (user->lastmsg)
114       MyFree(user->lastmsg);
115     /*
116      * sanity check
117      */
118     assert(0 == user->joined);
119     assert(0 == user->invited);
120     assert(0 == user->channel);
121
122     MyFree(user);
123     assert(userCount>0);
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  */
132 void 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  */
150 struct 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  */
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[])
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  */
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,
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 /* Helper function for register_user that splits up the parameters of the
310  * default user modes and sets them on the user.
311  */
312 static void set_initial_user_modes(struct Client *cptr) {
313   char **umodev;
314   int i, num, l, offset;
315   char *tmpstr = (char*)client_get_default_umode(cptr);
316   char *back;
317
318   if(!tmpstr) return;
319   tmpstr = strdup(tmpstr);
320
321   /* \tmpstr may have multiple parameters and \set_user_mode needs them
322    * to be split up. We just copy the default string and then replace the
323    * spaces with \0 to split the strings up.
324    */
325   num = 0;
326   for(i = 0; tmpstr[i]; ++i) {
327     if(tmpstr[i] == ' ') {
328       tmpstr[i] = 0;
329       ++num;
330     }
331   }
332
333   umodev = MyMalloc((6 + num) * sizeof(char*));
334   memset(umodev, 0, (6 + num) * sizeof(char*));
335
336   back = tmpstr;
337   offset = 0;
338   ++num;
339   for(i = 0; i < num; ++i) {
340     for(l = 0; back[l]; ++l) /* do nothing */;
341     /* Ignore empty parameters. */
342     if(l > 0) umodev[2 + i + offset] = back;
343     else --offset;
344     back = back + l + 1;
345   }
346
347   if(num + offset > 0)
348     set_user_mode(&me, cptr, 2 + num + offset, umodev, ALLOWMODES_WITHSECSERV);
349   MyFree(umodev);
350   MyFree(tmpstr);
351 }
352
353 /*
354  * register_user
355  *
356  * This function is called when both NICK and USER messages
357  * have been accepted for the client, in whatever order. Only
358  * after this the USER message is propagated.
359  *
360  * NICK's must be propagated at once when received, although
361  * it would be better to delay them too until full info is
362  * available. Doing it is not so simple though, would have
363  * to implement the following:
364  *
365  * 1) user telnets in and gives only "NICK foobar" and waits
366  * 2) another user far away logs in normally with the nick
367  *    "foobar" (quite legal, as this server didn't propagate it).
368  * 3) now this server gets nick "foobar" from outside, but
369  *    has already the same defined locally. Current server
370  *    would just issue "KILL foobar" to clean out dups. But,
371  *    this is not fair. It should actually request another
372  *    nick from local user or kill him/her...
373  */
374 /** Finish registering a user who has sent both NICK and USER.
375  * For local connections, possibly check IAuth; make sure there is a
376  * matching Client config block; clean the username field; check
377  * K/k-lines; check for "hacked" looking usernames; assign a numnick;
378  * and send greeting (WELCOME, ISUPPORT, MOTD, etc).
379  * For all connections, update the invisible user and operator counts;
380  * run IPcheck against their address; and forward the NICK.
381  *
382  * @param[in] cptr Client who introduced the user.
383  * @param[in,out] sptr Client who has been fully introduced.
384  * @return Zero or CPTR_KILLED.
385  */
386 int register_user(struct Client *cptr, struct Client *sptr)
387 {
388   char*            parv[4];
389   char*            tmpstr;
390   struct User*     user = cli_user(sptr);
391   char             ip_base64[25];
392
393   user->last = CurrentTime;
394   parv[0] = cli_name(sptr);
395   parv[1] = parv[2] = NULL;
396
397   if (MyConnect(sptr))
398   {
399     assert(cptr == sptr);
400
401     Count_unknownbecomesclient(sptr, UserStats);
402
403     /*
404      * Set user's initial modes
405      */
406     set_initial_user_modes(sptr);
407
408     SetUser(sptr);
409     if(MyConnect(sptr) && cli_socket(sptr).ssl)
410       SetSSL(sptr);
411
412     cli_handler(sptr) = CLIENT_HANDLER;
413     SetLocalNumNick(sptr);
414     send_reply(sptr,
415                RPL_WELCOME,
416                feature_str(FEAT_NETWORK),
417                feature_str(FEAT_PROVIDER) ? " via " : "",
418                feature_str(FEAT_PROVIDER) ? feature_str(FEAT_PROVIDER) : "",
419                cli_name(sptr));
420     /*
421      * This is a duplicate of the NOTICE but see below...
422      */
423     send_reply(sptr, RPL_YOURHOST, cli_name(&me), version);
424     send_reply(sptr, RPL_CREATED, creation);
425     send_reply(sptr, RPL_MYINFO, cli_name(&me), version, infousermodes,
426                infochanmodes, infochanmodeswithparams);
427     send_supported(sptr);
428
429     if(IsSSL(sptr))
430       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :You are connected to %s with %s", sptr,
431                     cli_name(&me), ssl_cipherstr(cli_socket(sptr).ssl));
432
433     m_lusers(sptr, sptr, 1, parv);
434     update_load();
435     motd_signon(sptr);
436     if (cli_snomask(sptr) & SNO_NOISY)
437       set_snomask(sptr, cli_snomask(sptr) & SNO_NOISY, SNO_ADD);
438     if (feature_bool(FEAT_CONNEXIT_NOTICES))
439       sendto_opmask_butone(0, SNO_CONNEXIT,
440                            "Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s%s>",
441                            cli_name(sptr), user->username, user->host,
442                            cli_sock_ip(sptr), get_client_class(sptr),
443                            cli_info(sptr), NumNick(cptr) /* two %s's */);
444     IPcheck_connect_succeeded(sptr);
445   }
446   else {
447     struct Client *acptr = user->server;
448
449     if (cli_from(acptr) != cli_from(sptr))
450     {
451       sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])",
452                     sptr, cli_name(&me), cli_name(user->server), cli_name(cli_from(acptr)),
453                     cli_sockhost(cli_from(acptr)));
454       SetFlag(sptr, FLAG_KILLED);
455       return exit_client(cptr, sptr, &me, "NICK server wrong direction");
456     }
457     else if (HasFlag(acptr, FLAG_TS8))
458       SetFlag(sptr, FLAG_TS8);
459
460     /*
461      * Check to see if this user is being propagated
462      * as part of a net.burst, or is using protocol 9.
463      * FIXME: This can be sped up - its stupid to check it for
464      * every NICK message in a burst again  --Run.
465      */
466     for (; acptr != &me; acptr = cli_serv(acptr)->up)
467     {
468       if (IsBurst(acptr) || Protocol(acptr) < 10)
469         break;
470     }
471     if (!IPcheck_remote_connect(sptr, (acptr != &me)))
472     {
473       /*
474        * We ran out of bits to count this
475        */
476       sendcmdto_one(&me, CMD_KILL, sptr, "%C :%s (Too many connections from your host -- Ghost)",
477                     sptr, cli_name(&me));
478       return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled");
479     }
480     SetUser(sptr);
481   }
482
483   /* If they get both +x and an account during registration, hide
484    * their hostmask here.  Calling hide_hostmask() from IAuth's
485    * account assignment causes a numeric reply during registration.
486    */
487   if (HasHiddenHost(sptr))
488     hide_hostmask(sptr, HIDE_HOSTMASK_FLAG_HIDDENHOST);
489   if (IsInvisible(sptr))
490     ++UserStats.inv_clients;
491   if (IsOper(sptr))
492     ++UserStats.opers;
493   if (MyUser(sptr))
494   {
495     if(IsOper(sptr)) {
496       FlagSet(&cli_confs(sptr)->value.aconf->conn_class->privs_dirty, PRIV_PROPAGATE);
497       client_set_privs(sptr, cli_confs(sptr)->value.aconf);
498       cli_handler(sptr) = OPER_HANDLER;
499     }
500     else
501       client_set_uprivs(sptr, cli_confs(sptr)->value.aconf);
502   }
503   if (MyUser(sptr) && HasPriv(sptr, PRIV_SEE_IDLETIME))
504     SetSeeIdletime(sptr);
505
506   tmpstr = umode_str(sptr);
507   /* Send full IP address to IPv6-grokking servers. */
508   sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr,
509                              FLAG_IPV6, FLAG_LAST_FLAG,
510                              "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
511                              cli_name(sptr), cli_hopcount(sptr) + 1,
512                              cli_lastnick(sptr),
513                              user->username, user->realhost,
514                              *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
515                              iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 1),
516                              NumNick(sptr), cli_info(sptr));
517   /* Send fake IPv6 addresses to pre-IPv6 servers. */
518   sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr,
519                              FLAG_LAST_FLAG, FLAG_IPV6,
520                              "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
521                              cli_name(sptr), cli_hopcount(sptr) + 1,
522                              cli_lastnick(sptr),
523                              user->username, user->realhost,
524                              *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
525                              iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 0),
526                              NumNick(sptr), cli_info(sptr));
527
528   /* Send user mode to client */
529   if (MyUser(sptr))
530   {
531     static struct Flags flags; /* automatically initialized to zeros */
532     /* To avoid sending +r to the client due to auth-on-connect, set
533      * the "old" FLAG_ACCOUNT bit to match the client's value.
534      */
535     if (IsAccount(cptr))
536       FlagSet(&flags, FLAG_ACCOUNT);
537     else
538       FlagClr(&flags, FLAG_ACCOUNT);
539
540     if(IsOper(sptr)) {
541       send_reply(sptr, RPL_YOUREOPER);
542       sendto_opmask_butone(0, SNO_OLDSNO, "%s (%s@%s) is now operator (%c)",
543                            cli_name(sptr), cli_user(sptr)->username, cli_sockhost(sptr),
544                            IsOper(sptr) ? 'O' : 'o');
545       log_write(LS_OPER, L_INFO, 0, "OPER (<OOC>) by (%#C)", sptr);
546     }
547     
548     if(*cli_connclass(sptr)) 
549         sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Your connection class is: %s", sptr, cli_connclass(sptr));
550     else
551         sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Your connection class is: %s", sptr, get_client_class(sptr));
552     if(HasPriv(sptr, PRIV_CHAN_LIMIT))
553       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :You have no channel number limitation.", sptr);
554     else {
555         unsigned int maxchan = cli_confs(sptr)->value.aconf ? ConfMaxChannels(cli_confs(sptr)->value.aconf): feature_int(FEAT_MAXCHANNELSPERUSER);
556         if(sptr -> maxchans > 0) 
557             maxchan = sptr->maxchans;
558       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :You may join %d channels.", sptr, maxchan);
559     }
560
561     send_umode(cptr, sptr, &flags, ALL_UMODES, 0);
562     if ((cli_snomask(sptr) != SNO_DEFAULT) && HasFlag(sptr, FLAG_SERVNOTICE))
563       send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
564   }
565   return 0;
566 }
567
568 /** List of user mode characters. */
569 static const struct UserMode {
570   unsigned int flag; /**< User mode constant. */
571   char         c;    /**< Character corresponding to the mode. */
572 } userModeList[] = {
573   { FLAG_OPER,        'o' },
574   { FLAG_LOCOP,       'O' },
575   { FLAG_INVISIBLE,   'i' },
576   { FLAG_WALLOP,      'w' },
577   { FLAG_SERVNOTICE,  's' },
578   { FLAG_DEAF,        'd' },
579   { FLAG_CHSERV,      'k' },
580   { FLAG_DEBUG,       'g' },
581   { FLAG_ACCOUNT,     'r' },
582   { FLAG_FAKEHOST,    'f' },
583   { FLAG_NOCHAN,      'n' },
584   { FLAG_NOIDLE,      'I' },
585   { FLAG_XTRAOP,      'X' },
586   { FLAG_NETSERV,     'S' },
587   { FLAG_HIDDENOPER,  'H' },
588   { FLAG_OVERRIDECC,  'c' },
589   { FLAG_WEBIRC,      'W' },
590   { FLAG_SEE_IDLETIME,'t' },
591   { FLAG_SECURITY_SERV,'D' },
592   { FLAG_KEEPCONN_ENABLED, 'K' },
593   { FLAG_HIDDENHOST,  'x' },
594   { FLAG_NOTCONN,     'Z' }
595 };
596
597 /** Length of #userModeList. */
598 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
599
600 /*
601  * XXX - find a way to get rid of this
602  */
603 /** Nasty global buffer used for communications with umode_str() and others. */
604 static char umodeBuf[BUFSIZE];
605
606 /** Try to set a user's nickname.
607  * If \a sptr is a server, the client is being introduced for the first time.
608  * @param[in] cptr Client to set nickname.
609  * @param[in] sptr Client sending the NICK.
610  * @param[in] nick New nickname.
611  * @param[in] parc Number of arguments to NICK.
612  * @param[in] parv Argument list to NICK.
613  * @param[in] force Boolean; forced nickchange
614  * @return CPTR_KILLED if \a cptr was killed, else 0.
615  */
616 int set_nick_name(struct Client* cptr, struct Client* sptr,
617                   const char* nick, int parc, char* parv[], unsigned int force)
618 {
619   if (IsServer(sptr)) {
620
621     /*
622      * A server introducing a new client, change source
623      */
624     struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
625     assert(0 != new_client);
626
627     cli_hopcount(new_client) = atoi(parv[2]);
628     cli_lastnick(new_client) = atoi(parv[3]);
629
630     /*
631      * Set new nick name.
632      */
633     strcpy(cli_name(new_client), nick);
634     cli_user(new_client) = make_user(new_client);
635     cli_user(new_client)->server = sptr;
636     SetRemoteNumNick(new_client, parv[parc - 2]);
637     /*
638      * IP# of remote client
639      */
640     base64toip(parv[parc - 3], &cli_ip(new_client));
641
642     add_client_to_list(new_client);
643     hAddClient(new_client);
644
645     cli_serv(sptr)->ghost = 0;        /* :server NICK means end of net.burst */
646     ircd_strncpy(cli_username(new_client), parv[4], USERLEN);
647     ircd_strncpy(cli_user(new_client)->username, parv[4], USERLEN);
648     ircd_strncpy(cli_user(new_client)->host, parv[5], HOSTLEN);
649     ircd_strncpy(cli_user(new_client)->realhost, parv[5], HOSTLEN);
650     ircd_strncpy(cli_info(new_client), parv[parc - 1], REALLEN);
651
652     Count_newremoteclient(UserStats, sptr);
653
654     if (parc > 7 && *parv[6] == '+') {
655       /* (parc-4) -3 for the ip, numeric nick, realname */
656       set_user_mode(cptr, new_client, parc-7, parv+4, ALLOWMODES_WITHSECSERV);
657     }
658
659     return register_user(cptr, new_client);
660   }
661   else if ((cli_name(sptr))[0]) {
662     /*
663      * Client changing its nick
664      *
665      * If the client belongs to me, then check to see
666      * if client is on any channels where it is currently
667      * banned.  If so, do not allow the nick change to occur.
668      */
669     if (MyUser(sptr)) {
670       const char* channel_name;
671       struct Membership *member;
672       if (!force && !IsXtraOp(sptr) && (channel_name = find_no_nickchange_channel(sptr, nick))) {
673         return send_reply(cptr, ERR_BANNICKCHANGE, channel_name);
674       }
675       /*
676        * Refuse nick change if the last nick change was less
677        * then 30 seconds ago. This is intended to get rid of
678        * clone bots doing NICK FLOOD. -SeKs
679        * If someone didn't change their nick for more then 60 seconds
680        * however, allow to do two nick changes immediately after another
681        * before limiting the nick flood. -Run
682        */
683       if (!force && (CurrentTime < cli_nextnick(cptr)))
684       {
685         cli_nextnick(cptr) += 2;
686         send_reply(cptr, ERR_NICKTOOFAST, parv[1],
687                    cli_nextnick(cptr) - CurrentTime);
688         /* Send error message */
689         sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cli_name(cptr));
690         /* bounce NICK to user */
691         return 0;                /* ignore nick change! */
692       }
693       else if(!force) {
694         /* Limit total to 1 change per NICK_DELAY seconds: */
695         cli_nextnick(cptr) += NICK_DELAY;
696         /* However allow _maximal_ 1 extra consecutive nick change: */
697         if (cli_nextnick(cptr) < CurrentTime)
698           cli_nextnick(cptr) = CurrentTime;
699       }
700       /* Invalidate all bans against the user so we check them again */
701       for (member = (cli_user(cptr))->channel; member;
702            member = member->next_channel)
703         ClearBanValid(member);
704     }
705     /*
706      * Also set 'lastnick' to current time, if changed.
707      */
708     if (0 != ircd_strcmp(parv[0], nick))
709       cli_lastnick(sptr) = (sptr == cptr) ? TStime() : atoi(parv[2]);
710
711     /*
712      * Client just changing his/her nick. If he/she is
713      * on a channel, send note of change to all clients
714      * on that channel. Propagate notice to other servers.
715      */
716     if (IsUser(sptr)) {
717       sendcmdto_common_channels_butone(sptr, CMD_NICK, NULL, ":%s", nick);
718       add_history(sptr, 1);
719       sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick,
720                             cli_lastnick(sptr));
721     }
722     else
723       sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick);
724
725     if ((cli_name(sptr))[0])
726       hRemClient(sptr);
727     strcpy(cli_name(sptr), nick);
728     hAddClient(sptr);
729   }
730   else {
731     /* Local client setting NICK the first time */
732     if(!force)
733       strcpy(cli_name(sptr), nick);
734     else {
735       /* use a "temponary" nick here (we'll switch later) */
736       char tmpnick[NICKLEN + 2];
737       int tmpnickend; 
738       strcpy(tmpnick, nick);
739       /* we need at least 10 characters */
740       if (strlen(tmpnick) > IRCD_MIN(NICKLEN, feature_int(FEAT_NICKLEN)) - 10)
741         tmpnick[IRCD_MIN(NICKLEN, feature_int(FEAT_NICKLEN))-10] = '\0';
742       tmpnickend = strlen(tmpnick);
743       
744       do { /* get a non-used nick... */
745         sprintf(tmpnick + tmpnickend, "[rz%d]", ircrandom() % 10000);
746       } while(FindClient(tmpnick));
747       strcpy(cli_name(sptr), tmpnick);
748     }
749     hAddClient(sptr);
750     return auth_set_nick(cli_auth(sptr), nick);
751   }
752   return 0;
753 }
754
755 /** Calculate the hash value for a target.
756  * @param[in] target Pointer to target, cast to unsigned int.
757  * @return Hash value constructed from the pointer.
758  */
759 static unsigned char hash_target(unsigned int target)
760 {
761   return (unsigned char) (target >> 16) ^ (target >> 8);
762 }
763
764 /** Records \a target as a recent target for \a sptr.
765  * @param[in] sptr User who has sent to a new target.
766  * @param[in] target Target to add.
767  */
768 void
769 add_target(struct Client *sptr, void *target)
770 {
771   /* Ok, this shouldn't work esp on alpha
772   */
773   unsigned char  hash = hash_target((unsigned long) target);
774   unsigned char* targets;
775   int            i;
776   assert(0 != sptr);
777   assert(cli_local(sptr));
778
779   targets = cli_targets(sptr);
780
781   /* 
782    * Already in table?
783    */
784   for (i = 0; i < MAXTARGETS; ++i) {
785     if (targets[i] == hash)
786       return;
787   }
788   /*
789    * New target
790    */
791   memmove(&targets[RESERVEDTARGETS + 1],
792           &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
793   targets[RESERVEDTARGETS] = hash;
794 }
795
796 /** Check whether \a sptr can send to or join \a target yet.
797  * @param[in] sptr User trying to join a channel or send a message.
798  * @param[in] target Target of the join or message.
799  * @param[in] name Name of the target.
800  * @param[in] created If non-zero, trying to join a new channel.
801  * @return Non-zero if too many target changes; zero if okay to send.
802  */
803 int check_target_limit(struct Client *sptr, void *target, const char *name,
804     int created)
805 {
806   unsigned char hash = hash_target((unsigned long) target);
807   int            i;
808   unsigned char* targets;
809
810   assert(0 != sptr);
811   assert(cli_local(sptr));
812   targets = cli_targets(sptr);
813
814   if(HasPriv(sptr, PRIV_UNLIMITED_TARGET))
815     return 0;
816
817   /*
818    * Same target as last time?
819    */
820   if (targets[0] == hash)
821     return 0;
822   for (i = 1; i < MAXTARGETS; ++i) {
823     if (targets[i] == hash) {
824       memmove(&targets[1], &targets[0], i);
825       targets[0] = hash;
826       return 0;
827     }
828   }
829   /*
830    * New target
831    */
832   if (!created) {
833     if (CurrentTime < cli_nexttarget(sptr)) {
834       /* If user is invited to channel, give him/her a free target */
835       if (IsChannelName(name) && IsInvited(sptr, target))
836         return 0;
837
838       if (cli_nexttarget(sptr) - CurrentTime < TARGET_DELAY + 8) {
839         /*
840          * No server flooding
841          */
842         cli_nexttarget(sptr) += 2;
843         send_reply(sptr, ERR_TARGETTOOFAST, name,
844                    cli_nexttarget(sptr) - CurrentTime);
845       }
846       return 1;
847     }
848     else {
849       cli_nexttarget(sptr) += TARGET_DELAY;
850       if (cli_nexttarget(sptr) < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)))
851         cli_nexttarget(sptr) = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1));
852     }
853   }
854   memmove(&targets[1], &targets[0], MAXTARGETS - 1);
855   targets[0] = hash;
856   return 0;
857 }
858
859 /** Allows a channel operator to avoid target change checks when
860  * sending messages to users on their channel.
861  * @param[in] source User sending the message.
862  * @param[in] nick Destination of the message.
863  * @param[in] channel Name of channel being sent to.
864  * @param[in] text Message to send.
865  * @param[in] is_notice If non-zero, use CNOTICE instead of CPRIVMSG.
866  */
867 /* Added 971023 by Run. */
868 int whisper(struct Client* source, const char* nick, const char* channel,
869             const char* text, int is_notice)
870 {
871   struct Client*     dest;
872   struct Channel*    chptr;
873   struct Membership* membership;
874
875   assert(0 != source);
876   assert(0 != nick);
877   assert(0 != channel);
878   assert(MyUser(source));
879
880   if (!(dest = FindUser(nick))) {
881     return send_reply(source, ERR_NOSUCHNICK, nick);
882   }
883   if (!(chptr = FindChannel(channel))) {
884     return send_reply(source, ERR_NOSUCHCHANNEL, channel);
885   }
886   /*
887    * compare both users channel lists, instead of the channels user list
888    * since the link is the same, this should be a little faster for channels
889    * with a lot of users
890    */
891   for (membership = cli_user(source)->channel; membership; membership = membership->next_channel) {
892     if (chptr == membership->channel)
893       break;
894   }
895   if (0 == membership) {
896     return send_reply(source, ERR_NOTONCHANNEL, chptr->chname);
897   }
898   if (!IsVoicedOrOpped(membership)) {
899     return send_reply(source, ERR_VOICENEEDED, chptr->chname);
900   }
901   /*
902    * lookup channel in destination
903    */
904   assert(0 != cli_user(dest));
905   for (membership = cli_user(dest)->channel; membership; membership = membership->next_channel) {
906     if (chptr == membership->channel)
907       break;
908   }
909   if (0 == membership || IsZombie(membership)) {
910     return send_reply(source, ERR_USERNOTINCHANNEL, cli_name(dest), chptr->chname);
911   }
912   if (is_silenced(source, dest))
913     return 0;
914           
915   if (is_notice)
916     sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text);
917   else
918   {
919     if (cli_user(dest)->away)
920       send_reply(source, RPL_AWAY, cli_name(dest), cli_user(dest)->away);
921     sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text);
922   }
923   return 0;
924 }
925
926
927 /** Send a user mode change for \a cptr to neighboring servers.
928  * @param[in] cptr User whose mode is changing.
929  * @param[in] sptr Client who sent us the mode change message.
930  * @param[in] old Prior set of user flags.
931  * @param[in] prop If non-zero, also include FLAG_OPER.
932  */
933 void send_umode_out(struct Client *cptr, struct Client *sptr,
934                     struct Flags *old, int prop)
935 {
936   int i, server = 0, ret;
937   struct Client *acptr;
938
939   if(cptr && (IsServer(cptr) || IsMe(cptr))) server = 1;
940
941   ret = send_umode(NULL, sptr, old, prop ? SEND_UMODES : SEND_UMODES_BUT_OPER, server);
942
943   for (i = HighestFd; i >= 0; i--)
944   {
945     if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
946         (acptr != cptr) && (acptr != sptr) && *umodeBuf)
947       sendcmdto_one(sptr, CMD_MODE, acptr, ret?"%s %s":"%s :%s", cli_name(sptr), umodeBuf);
948   }
949   if (cptr && MyUser(cptr))
950     send_umode(cptr, sptr, old, ALL_UMODES, 0);
951   if (sptr && sptr != cptr && MyUser(sptr))
952     send_umode(sptr, sptr, old, ALL_UMODES, 0);
953 }
954
955
956 /** Call \a fmt for each Client named in \a names.
957  * @param[in] sptr Client requesting information.
958  * @param[in] names Space-delimited list of nicknames.
959  * @param[in] rpl Base reply string for messages.
960  * @param[in] fmt Formatting callback function.
961  */
962 void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
963 {
964   char*          name;
965   char*          p = 0;
966   int            arg_count = 0;
967   int            users_found = 0;
968   struct Client* acptr;
969   struct MsgBuf* mb;
970
971   assert(0 != sptr);
972   assert(0 != names);
973   assert(0 != fmt);
974
975   mb = msgq_make(sptr, rpl_str(rpl), cli_name(&me), cli_name(sptr));
976
977   for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
978     if ((acptr = FindUser(name))) {
979       if (users_found++)
980         msgq_append(0, mb, " ");
981       (*fmt)(acptr, sptr, mb);
982     }
983     if (5 == ++arg_count)
984       break;
985   }
986   send_buffer(sptr, mb, 0);
987   msgq_clean(mb);
988 }
989
990 /** Set \a flag on \a cptr and possibly hide the client's hostmask.
991  * @param[in,out] cptr User who is getting a new flag.
992  * @param[in] flag Some flag that affects host-hiding (FLAG_HIDDENHOST, FLAG_ACCOUNT, FLAG_FAKEHOST).
993  * @return Zero.
994  */
995  
996 int
997 hide_hostmask(struct Client *cptr, unsigned int flag)
998 {
999   struct Membership *chan;
1000   char buf[HOSTLEN];
1001
1002   if (flag & HIDE_HOSTMASK_FLAG_HIDDENHOST) {
1003     /* Local users cannot set +x unless FEAT_HOST_HIDING is true. */
1004     if (MyConnect(cptr) && !feature_bool(FEAT_HOST_HIDING))
1005       return 0;
1006   }
1007   if (flag & (HIDE_HOSTMASK_FLAG_ACCOUNT | HIDE_HOSTMASK_FLAG_FAKEHOST | HIDE_HOSTMASK_FLAG_FAKEIDENT)) {
1008     /* Invalidate all bans against the user so we check them again */
1009     for (chan = (cli_user(cptr))->channel; chan;
1010          chan = chan->next_channel)
1011       ClearBanValid(chan);
1012   }
1013
1014   /* Set flags and stop if no fakehost has to be applied. */
1015   if(flag & HIDE_HOSTMASK_FLAG_HIDDENHOST)
1016     SetFlag(cptr, FLAG_HIDDENHOST);
1017   if(flag & HIDE_HOSTMASK_FLAG_ACCOUNT)
1018     SetFlag(cptr, FLAG_ACCOUNT);
1019   if(flag & HIDE_HOSTMASK_FLAG_FAKEHOST)
1020     SetFlag(cptr, FLAG_FAKEHOST);
1021   if(flag & HIDE_HOSTMASK_FLAG_FAKEIDENT)
1022     SetFlag(cptr, FLAG_FAKEIDENT);
1023   
1024   if(!HasHiddenHost(cptr))
1025     return 0;
1026
1027   /* Generate new fakehost. */
1028   unsigned int reregister = 0;
1029   if(IsFakeHost(cptr) || IsAccount(cptr)) {
1030     if(IsFakeHost(cptr))
1031       ircd_strncpy(buf, cli_user(cptr)->fakehost, HOSTLEN);
1032     else
1033       ircd_snprintf(0, buf, HOSTLEN, "%s.%s", cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
1034     if(strncmp(buf, cli_user(cptr)->host, HOSTLEN))
1035       reregister |= HIDE_HOSTMASK_FLAG_FAKEHOST;
1036   }
1037   if(IsFakeIdent(cptr)) {
1038     if(strncmp(cli_user(cptr)->fakeuser, cli_user(cptr)->username, USERLEN))
1039       reregister |= HIDE_HOSTMASK_FLAG_FAKEIDENT;
1040   }
1041   
1042   if (!reregister) return 0;
1043
1044   /* Remove all "valid" marks on the bans. This forces them to be
1045    * rechecked if the ban is accessed again.
1046    */
1047   for(chan = (cli_user(cptr))->channel; chan; chan = chan->next_channel) {
1048     ClearBanValid(chan);
1049   }
1050
1051   /* Quit user and set the previously generated fakehost. */
1052   sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Registered");
1053   if(reregister & HIDE_HOSTMASK_FLAG_FAKEHOST)
1054     ircd_strncpy(cli_user(cptr)->host, buf, HOSTLEN);
1055   if(reregister & HIDE_HOSTMASK_FLAG_FAKEIDENT)
1056     ircd_strncpy(cli_user(cptr)->username, cli_user(cptr)->fakeuser, USERLEN);
1057   
1058
1059   /* ok, the client is now fully hidden, so let them know -- hikari */
1060   if(MyConnect(cptr)) {
1061     if (IsFakeIdent(cptr))
1062       send_reply(cptr, RPL_HOSTUSERHIDDEN, cli_user(cptr)->username, cli_user(cptr)->host);
1063     else
1064       send_reply(cptr, RPL_HOSTHIDDEN, cli_user(cptr)->host);
1065   }
1066
1067   /*
1068    * Go through all channels the client was on, rejoin him
1069    * and set the modes, if any
1070    */
1071   for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel)
1072   {
1073     if (IsZombie(chan))
1074       continue;
1075     /* If the channel has delayed joins, we have to handle the join especially. */
1076     if((!(chan->channel->mode.mode & MODE_DELJOINS) && !IsInvisibleJoin(chan)) || IsChanOp(chan) || HasVoice(chan)) {
1077         sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, 0,
1078                                          "%H", chan->channel);
1079         if(IsChanOp(chan) && HasVoice(chan)) {
1080             sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0,
1081                                              "%H +ov %C %C", chan->channel, cptr,
1082                                              cptr);
1083         }
1084         else if(IsChanOp(chan) || HasVoice(chan)) {
1085             sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0,
1086                                              "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr);
1087         }
1088     }
1089     else {
1090         SetDelayedJoin(chan);
1091     }
1092     
1093     /*
1094     * Check if the client is actually overriding a ban with the
1095     * mask change, if so, kick him out of the channel.
1096     * We have to proceed that way to ensure data consistency (join + kick)
1097     */
1098     if (find_ban(cptr, chan->channel->banlist)) {
1099       /* Silentely kick in case of delayed join */
1100       if (chan->channel->mode.mode & MODE_DELJOINS) {
1101         sendcmdto_one(&his, CMD_KICK, cptr, "%H %C :Ban override", chan->channel, cptr);
1102         CheckDelayedJoins(chan->channel);
1103         
1104       } else {
1105         /* Otherwise publicly kick */
1106         sendcmdto_serv_butone(&me, CMD_KICK, NULL, "%H %C :Ban override", chan->channel, cptr);
1107         sendcmdto_channel_butserv_butone(&his, CMD_KICK, chan->channel, NULL, 0, "%H %C :Ban override", chan->channel, cptr);
1108         make_zombie(chan, cptr, &me, &me, chan->channel);
1109       }
1110     }
1111   }
1112   return 0;
1113 }
1114
1115 /** Set a user's mode.  This function checks that \a cptr is trying to
1116  * set his own mode, prevents local users from setting inappropriate
1117  * modes through this function, and applies any other side effects of
1118  * a successful mode change.
1119  *
1120  * @param[in,out] cptr User setting someone's mode.
1121  * @param[in] sptr Client who sent the mode change message.
1122  * @param[in] parc Number of parameters in \a parv.
1123  * @param[in] parv Parameters to MODE.
1124  * @param[in] allow_modes ALLOWMODES_ANY for any mode, ALLOWMODES_DEFAULT for 
1125  *                        only permitting legitimate default user modes.
1126  * @return Zero.
1127  */
1128 int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, 
1129                 char *parv[], int allow_modes)
1130 {
1131   char** p;
1132   char*  m;
1133   int what;
1134   int i;
1135   struct Flags setflags;
1136   unsigned int tmpmask = 0;
1137   int snomask_given = 0;
1138   char buf[BUFSIZE];
1139   int prop = 0;
1140   int do_host_hiding = 0;
1141   char* account = NULL, *fakehost = NULL, *keepconn = NULL;
1142   struct Membership *chan;
1143
1144   what = MODE_ADD;
1145
1146   if (parc < 3)
1147   {
1148     m = buf;
1149     *m++ = '+';
1150     for (i = 0; i < USERMODELIST_SIZE; i++)
1151     {
1152       if (HasFlag(sptr, userModeList[i].flag) &&
1153           (userModeList[i].flag != FLAG_ACCOUNT) &&
1154           (userModeList[i].flag != FLAG_FAKEHOST))
1155         *m++ = userModeList[i].c;
1156     }
1157     *m = '\0';
1158     send_reply(sptr, RPL_UMODEIS, buf);
1159     if (HasFlag(sptr, FLAG_SERVNOTICE) && MyConnect(sptr)
1160         && cli_snomask(sptr) !=
1161         (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT))
1162       send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1163     return 0;
1164   }
1165
1166   /*
1167    * find flags already set for user
1168    * why not just copy them?
1169    */
1170   setflags = cli_flags(sptr);
1171
1172   if (MyConnect(sptr))
1173     tmpmask = cli_snomask(sptr);
1174
1175   /*
1176    * parse mode change string(s)
1177    */
1178   for (p = &parv[2]; *p && p<&parv[parc]; p++) {       /* p is changed in loop too */
1179     for (m = *p; *m; m++) {
1180       switch (*m) {
1181       case '+':
1182         what = MODE_ADD;
1183         break;
1184       case '-':
1185         what = MODE_DEL;
1186         break;
1187       case 's':
1188         if (*(p + 1) && is_snomask(*(p + 1))) {
1189           snomask_given = 1;
1190           tmpmask = umode_make_snomask(tmpmask, *++p, what);
1191           tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1192         }
1193         else
1194           tmpmask = (what == MODE_ADD) ?
1195               (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1196         if (tmpmask)
1197           SetServNotice(sptr);
1198         else
1199           ClearServNotice(sptr);
1200         break;
1201       case 'w':
1202         if (what == MODE_ADD)
1203           SetWallops(sptr);
1204         else
1205           ClearWallops(sptr);
1206         break;
1207       case 'o':
1208         if (what == MODE_ADD)
1209           SetOper(sptr);
1210         else {
1211           ClrFlag(sptr, FLAG_OPER);
1212           ClrFlag(sptr, FLAG_LOCOP);
1213           if (MyConnect(sptr))
1214           {
1215             tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1216             cli_handler(sptr) = CLIENT_HANDLER;
1217           }
1218         }
1219         break;
1220       case 'O':
1221         if (what == MODE_ADD)
1222           SetLocOp(sptr);
1223         else
1224         { 
1225           ClrFlag(sptr, FLAG_OPER);
1226           ClrFlag(sptr, FLAG_LOCOP);
1227           if (MyConnect(sptr))
1228           {
1229             tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1230             cli_handler(sptr) = CLIENT_HANDLER;
1231           }
1232         }
1233         break;
1234       case 'i':
1235         if (what == MODE_ADD)
1236           SetInvisible(sptr);
1237         else
1238           ClearInvisible(sptr);
1239         break;
1240       case 'd':
1241         if (what == MODE_ADD)
1242           SetDeaf(sptr);
1243         else
1244           ClearDeaf(sptr);
1245         break;
1246       case 'c':
1247         if (what == MODE_ADD)
1248             SetOverrideCC(sptr);
1249         else
1250             ClearOverrideCC(sptr);
1251         break;
1252       case 'k':
1253         if (what == MODE_ADD)
1254           SetChannelService(sptr);
1255         else
1256           ClearChannelService(sptr);
1257         break;
1258       case 'g':
1259         if (what == MODE_ADD)
1260           SetDebug(sptr);
1261         else
1262           ClearDebug(sptr);
1263         break;
1264       case 'x':
1265         if(what == MODE_ADD && !FlagHas(&setflags, FLAG_HIDDENHOST) &&
1266            (!MyConnect(sptr) || feature_bool(FEAT_HOST_HIDING))) {
1267             do_host_hiding = 1;
1268             SetHiddenHost(sptr);
1269         }
1270         break;
1271       case 'n':
1272         if (what == MODE_ADD)
1273           SetNoChan(sptr);
1274         else
1275           ClearNoChan(sptr);
1276         break;
1277       case 'I':
1278         if (what == MODE_ADD)
1279           SetNoIdle(sptr);
1280         else
1281           ClearNoIdle(sptr);
1282         break;
1283       case 'X':
1284         if (what == MODE_ADD)
1285            SetXtraOp(sptr);
1286         else
1287            ClearXtraOp(sptr);
1288         break;
1289       case 'S':
1290         if (what == MODE_ADD)
1291            SetNetServ(sptr);
1292         else
1293            ClearNetServ(sptr);
1294         break;
1295       case 'H':
1296         if (what == MODE_ADD)
1297            SetHiddenOper(sptr);
1298         else
1299            ClearHiddenOper(sptr);
1300         break;
1301           case 'D':
1302         if (what == MODE_ADD)
1303            SetSecurityServ(sptr);
1304         else
1305            ClearSecurityServ(sptr);
1306         break;
1307       case 'W':
1308         if (what == MODE_ADD)
1309           SetWebIRC(sptr);
1310         else
1311           ClearWebIRC(sptr);
1312         break;
1313       case 'K':
1314         if (what == MODE_ADD) {
1315           if(*(p + 1) == 0)
1316             break;
1317           keepconn = *(++p);
1318           SetKeepConnEnabled(sptr);
1319         } else {
1320           ClearKeepConnEnabled(sptr);
1321         }
1322         break;
1323       case 'r':
1324         if (*(p + 1) && (what == MODE_ADD)) {
1325           account = *(++p);
1326           SetAccount(sptr);
1327           for(chan = (cli_user(sptr))->channel; chan; chan = chan->next_channel) ClearBanValid(chan);
1328           if(HasFlag(sptr, FLAG_HIDDENHOST))
1329             do_host_hiding = 1;
1330         }
1331         /* There is no -r */
1332         break;
1333       case 'f':
1334         if (*(p + 1) && (what == MODE_ADD)) {
1335           fakehost = *(++p);
1336           SetFakeHost(sptr);
1337           for(chan = (cli_user(sptr))->channel; chan; chan = chan->next_channel) ClearBanValid(chan);
1338           if(HasFlag(sptr, FLAG_HIDDENHOST))
1339             do_host_hiding = 1;
1340         }
1341         /* There is no -f */
1342         break;
1343       case 't': /* This can only be set by servers and cannot be removed. */
1344         if (what == MODE_ADD)
1345           SetSeeIdletime(sptr);
1346         break;
1347 #ifdef OLD_OGN_IRCU_COMPAT
1348       case 'z': /* Formerly SSL mode; we ignore it. */
1349         break;
1350 #endif
1351       case 'Z':
1352         if (what == MODE_ADD)
1353           SetNotConn(sptr);
1354         else
1355           ClearNotConn(sptr);
1356         break;
1357       default:
1358         send_reply(sptr, ERR_UMODEUNKNOWNFLAG, *m);
1359         break;
1360       }
1361     }
1362   }
1363   /*
1364    * Evaluate rules for new user mode
1365    * Stop users making themselves operators too easily:
1366    */
1367   if (!IsServer(cptr) && !IsMe(cptr))
1368   {
1369     if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr))
1370       ClearOper(sptr);
1371     if (!FlagHas(&setflags, FLAG_LOCOP) && IsLocOp(sptr))
1372       ClearLocOp(sptr);
1373     if (!FlagHas(&setflags, FLAG_ACCOUNT) && IsAccount(sptr))
1374       ClrFlag(sptr, FLAG_ACCOUNT);
1375     if (!FlagHas(&setflags, FLAG_FAKEHOST) && IsFakeHost(sptr))
1376       ClrFlag(sptr, FLAG_FAKEHOST);
1377     if (!FlagHas(&setflags, FLAG_SEE_IDLETIME) && IsSeeIdletime(sptr))
1378       ClrFlag(sptr, FLAG_SEE_IDLETIME);
1379     if (!FlagHas(&setflags, FLAG_NOTCONN) && IsNotConn(sptr))
1380       ClrFlag(sptr, FLAG_NOTCONN);
1381     /*
1382      * new umode; servers and privileged opers can set it, local users cannot;
1383      * prevents users from /kick'ing or /mode -o'ing
1384      */
1385     if (!FlagHas(&setflags, FLAG_CHSERV) && IsChannelService(sptr) && !HasPriv(sptr, PRIV_UMODE_CHSERV))
1386       ClearChannelService(sptr);
1387     if (!FlagHas(&setflags, FLAG_NOCHAN) && IsNoChan(sptr) && !HasPriv(sptr, PRIV_UMODE_NOCHAN))
1388       ClearNoChan(sptr);
1389     if (!FlagHas(&setflags, FLAG_NOIDLE) && IsNoIdle(sptr) && !HasPriv(sptr, PRIV_UMODE_NOIDLE))
1390       ClearNoIdle(sptr);
1391     if (!FlagHas(&setflags, FLAG_XTRAOP) && IsXtraOp(sptr) && (!IsOper(sptr) || !HasPriv(sptr, PRIV_UMODE_XTRAOP)))
1392       ClearXtraOp(sptr);
1393     if (!FlagHas(&setflags, FLAG_NETSERV) && IsNetServ(sptr) && (!HasPriv(sptr, PRIV_UMODE_NETSERV) || !HasFlag(sptr, FLAG_SECURITY_SERV)))
1394       ClearNetServ(sptr);
1395     if (!FlagHas(&setflags, FLAG_HIDDENOPER) && IsHiddenOper(sptr) && !IsOper(sptr))
1396       ClearHiddenOper(sptr);
1397     if (!FlagHas(&setflags, FLAG_OVERRIDECC) && IsOverrideCC(sptr) && !HasPriv(sptr, PRIV_UMODE_OVERRIDECC))
1398       ClearOverrideCC(sptr);
1399     if (!FlagHas(&setflags, FLAG_KEEPCONN_ENABLED) && IsKeepConnEnabled(sptr) && !HasPriv(sptr, PRIV_SET_KEEPCONN))
1400       ClearKeepConnEnabled(sptr);
1401     if(keepconn && !HasPriv(sptr, PRIV_SET_KEEPCONN))
1402       keepconn = NULL;
1403     /* Opers are able to fake the webirc usermode only if FEAT_FAKE_WEBIRC is true. */
1404     if (!FlagHas(&setflags, FLAG_WEBIRC) && IsWebIRC(sptr) && !(feature_bool(FEAT_FAKE_WEBIRC) && IsOper(sptr)))
1405       ClearWebIRC(sptr);
1406     /*
1407      * only send wallops to opers
1408      */
1409     if (feature_bool(FEAT_WALLOPS_OPER_ONLY) && !IsAnOper(sptr) &&
1410         !FlagHas(&setflags, FLAG_WALLOP))
1411       ClearWallops(sptr);
1412     if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY) && MyConnect(sptr) &&
1413         !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_SERVNOTICE))
1414     {
1415       ClearServNotice(sptr);
1416       set_snomask(sptr, 0, SNO_SET);
1417     }
1418     if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY) &&
1419         !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_DEBUG))
1420       ClearDebug(sptr);
1421   }
1422   if (allow_modes != ALLOWMODES_WITHSECSERV) {
1423     if (!FlagHas(&setflags, FLAG_SECURITY_SERV) && IsSecurityServ(sptr)) {
1424       ClearSecurityServ(sptr);
1425           sendto_opmask_butone(0, SNO_OLDSNO, "Someone tried to set mode +D to %s - Access denied",cli_name(sptr));
1426           }
1427   }
1428   if (MyConnect(sptr))
1429   {
1430     if ((FlagHas(&setflags, FLAG_OPER) || FlagHas(&setflags, FLAG_LOCOP)) &&
1431         !IsAnOper(sptr))
1432       det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPERATOR);
1433
1434     if (SendServNotice(sptr))
1435     {
1436       if (tmpmask != cli_snomask(sptr))
1437         set_snomask(sptr, tmpmask, SNO_SET);
1438       if (cli_snomask(sptr) && snomask_given)
1439         send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1440     }
1441     else
1442       set_snomask(sptr, 0, SNO_SET);
1443   }
1444   /*
1445    * Compare new flags with old flags and send string which
1446    * will cause servers to update correctly.
1447    */
1448   if (!FlagHas(&setflags, FLAG_ACCOUNT) && IsAccount(sptr)) {
1449       int len = ACCOUNTLEN;
1450       char *ts;
1451       if ((ts = strchr(account, ':'))) {
1452         len = (ts++) - account;
1453         cli_user(sptr)->acc_create = atoi(ts);
1454         Debug((DEBUG_DEBUG, "Received timestamped account in user mode; "
1455               "account \"%s\", timestamp %Tu", account,
1456               cli_user(sptr)->acc_create));
1457       }
1458       ircd_strncpy(cli_user(sptr)->account, account, len);
1459   }
1460   if (!FlagHas(&setflags, FLAG_FAKEHOST) && IsFakeHost(sptr)) {
1461     ircd_strncpy(cli_user(sptr)->fakehost, fakehost, HOSTLEN);
1462   }
1463   if (!FlagHas(&setflags, FLAG_KEEPCONN_ENABLED) && IsKeepConnEnabled(sptr)) {
1464     sptr->keepconn = atoi(keepconn);
1465   } else if(keepconn && sptr->keepconn != atoi(keepconn)) {
1466     FlagClr(&setflags, FLAG_KEEPCONN_ENABLED);
1467     sptr->keepconn = atoi(keepconn);
1468   }
1469
1470   if (IsRegistered(sptr)) {
1471     if(do_host_hiding)
1472       hide_hostmask(sptr, 0);
1473
1474     if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr)) {
1475       /* user now oper */
1476       ++UserStats.opers;
1477       client_set_privs(sptr, NULL); /* may set propagate privilege */
1478       prop = 1;
1479       if(MyConnect(sptr)) {
1480         sendto_opmask_butone(0, SNO_OLDSNO, "%s (%s@%s) is now operator (%c)",
1481                              cli_name(sptr), cli_user(sptr)->username,
1482                              cli_sockhost(sptr), IsOper(sptr) ? 'O' : 'o');
1483         log_write(LS_OPER, L_INFO, 0, "OPER (<OOM>) by (%#C)", sptr);
1484         cli_handler(sptr) = OPER_HANDLER;
1485         send_reply(sptr, RPL_YOUREOPER);
1486       }
1487     }
1488     if (FlagHas(&setflags, FLAG_OPER) && !IsOper(sptr)) {
1489       prop = 1;
1490     }
1491     /* remember propagate privilege setting */
1492     if (HasPriv(sptr, PRIV_PROPAGATE)) {
1493       prop = 1;
1494     }
1495     if (FlagHas(&setflags, FLAG_OPER) && !IsOper(sptr)) {
1496       /* user no longer oper */
1497       assert(UserStats.opers > 0);
1498       --UserStats.opers;
1499       client_set_privs(sptr, NULL); /* will clear propagate privilege */
1500       client_set_uprivs(sptr, cli_confs(sptr)->value.aconf);
1501     }
1502     if (FlagHas(&setflags, FLAG_INVISIBLE) && !IsInvisible(sptr)) {
1503       assert(UserStats.inv_clients > 0);
1504       --UserStats.inv_clients;
1505     }
1506     if (!FlagHas(&setflags, FLAG_INVISIBLE) && IsInvisible(sptr)) {
1507       ++UserStats.inv_clients;
1508     }
1509     assert(UserStats.opers <= UserStats.clients + UserStats.unknowns);
1510     assert(UserStats.inv_clients <= UserStats.clients + UserStats.unknowns);
1511     send_umode_out(cptr, sptr, &setflags, prop);
1512   }
1513
1514   return 0;
1515 }
1516
1517 /** Build a mode string to describe modes for \a cptr.
1518  * @param[in] cptr Some user.
1519  * @return Pointer to a static buffer.
1520  */
1521 char *umode_str(struct Client *cptr)
1522 {
1523   /* Maximum string size: "owidgrx\0" */
1524   char *m = umodeBuf;
1525   int i;
1526   struct Flags c_flags = cli_flags(cptr);
1527
1528   if (!HasPriv(cptr, PRIV_PROPAGATE))
1529     FlagClr(&c_flags, FLAG_OPER);
1530
1531   for (i = 0; i < USERMODELIST_SIZE; ++i)
1532   {
1533     if (FlagHas(&c_flags, userModeList[i].flag) &&
1534         userModeList[i].flag >= FLAG_GLOBAL_UMODES)
1535       *m++ = userModeList[i].c;
1536   }
1537
1538   /* Append the arguments for the flags. They have to be appended
1539    * in the right order. See the order of userModeList[].
1540    */
1541
1542   if (IsAccount(cptr)) {
1543     char* t = cli_user(cptr)->account;
1544
1545     *m++ = ' ';
1546     while ((*m++ = *t++))
1547       ; /* Empty loop */
1548
1549     /* If timestamped, append the timestamp directly to the accountname
1550      * separated with a colon
1551      */
1552     if (cli_user(cptr)->acc_create) {
1553       char nbuf[20];
1554       Debug((DEBUG_DEBUG, "Sending timestamped account in user mode for "
1555             "account \"%s\"; timestamp %Tu", cli_user(cptr)->account,
1556             cli_user(cptr)->acc_create));
1557       ircd_snprintf(0, t = nbuf, sizeof(nbuf), ":%Tu",
1558                     cli_user(cptr)->acc_create);
1559       m--; /* back up over previous nul-termination */
1560       while ((*m++ = *t++))
1561         ; /* Empty loop */
1562     }
1563
1564     --m; /* back up over previous nul-termination */
1565   }
1566
1567   if(IsFakeHost(cptr)) {
1568         char* t = cli_user(cptr)->fakehost;
1569         *m++ = ' ';
1570         while((*m++ = *t++)) ; /* Empty loop */
1571         --m; /* back up over previous nul-termination */
1572   }
1573
1574   *m = '\0';
1575
1576   return umodeBuf;                /* Note: static buffer, gets
1577                                    overwritten by send_umode() */
1578 }
1579
1580 /** Send a mode change string for \a sptr to \a cptr.
1581  * @param[in] cptr Destination of mode change message.
1582  * @param[in] sptr User whose mode has changed.
1583  * @param[in] old Pre-change set of modes for \a sptr.
1584  * @param[in] sendset One of ALL_UMODES, SEND_UMODES_BUT_OPER,
1585  * @param[in] serv_modes Set to 1 if you want to send ACCOUNT and FAKEHOST with this command.
1586  * @return 1 if ":" is already used in the buffer, 0 otherwise. Always 0 if \a serv_modes is 0.
1587  * SEND_UMODES, to select which changed user modes to send.
1588  */
1589 int send_umode(struct Client *cptr, struct Client *sptr, struct Flags *old,
1590                 int sendset, int serv_modes)
1591 {
1592   int i, ret = 0;
1593   int flag;
1594   char *m;
1595   int what = MODE_NULL;
1596   int add_fakehost = 0, add_account = 0, add_keepconn = 0;
1597
1598   /*
1599    * Build a string in umodeBuf to represent the change in the user's
1600    * mode between the new (cli_flags(sptr)) and 'old', but skipping
1601    * the modes indicated by sendset.
1602    */
1603   m = umodeBuf;
1604   *m = '\0';
1605   for (i = 0; i < USERMODELIST_SIZE; ++i)
1606   {
1607     flag = userModeList[i].flag;
1608     if (FlagHas(old, flag)
1609         == HasFlag(sptr, flag))
1610       continue;
1611
1612     /* Account is not shown to the user as umode. */
1613     if(flag == FLAG_ACCOUNT) {
1614       if(!serv_modes || FlagHas(old, flag)) continue;
1615       add_account = 1;
1616     }
1617
1618     if(flag == FLAG_FAKEHOST) {
1619       /* Removing a fakehost is not possible. Ignore it. */
1620       if(!serv_modes || FlagHas(old, flag)) continue;
1621       add_fakehost = 1;
1622     }
1623     
1624     if(flag == FLAG_KEEPCONN_ENABLED) {
1625       add_keepconn = 1;
1626     }
1627
1628     switch (sendset)
1629     {
1630     case ALL_UMODES:
1631       break;
1632     case SEND_UMODES_BUT_OPER:
1633       if (flag == FLAG_OPER)
1634         continue;
1635       /* and fall through */
1636     case SEND_UMODES:
1637       if (flag < FLAG_GLOBAL_UMODES)
1638         continue;
1639       break;      
1640     }
1641     if (FlagHas(old, flag))
1642     {
1643       if (what == MODE_DEL)
1644         *m++ = userModeList[i].c;
1645       else
1646       {
1647         what = MODE_DEL;
1648         *m++ = '-';
1649         *m++ = userModeList[i].c;
1650       }
1651     }
1652     else /* !FlagHas(old, flag) */
1653     {
1654       if (what == MODE_ADD)
1655         *m++ = userModeList[i].c;
1656       else
1657       {
1658         what = MODE_ADD;
1659         *m++ = '+';
1660         *m++ = userModeList[i].c;
1661       }
1662     }
1663   }
1664
1665   if(add_account) {
1666     char* t = cli_user(sptr)->account;
1667
1668     *m++ = ' ';
1669     if(!add_fakehost) {
1670       *m++ = ':';
1671       ret = 1;
1672     }
1673     while ((*m++ = *t++)) /* Empty loop */ ;
1674     if(cli_user(sptr)->acc_create) {
1675       char nbuf[20];
1676       ircd_snprintf(0, t = nbuf, sizeof(nbuf), ":%Tu", cli_user(sptr)->acc_create);
1677       m--; /* back up over previous nul-termination */
1678       while ((*m++ = *t++)) /* Empty loop */ ;
1679     }
1680     --m; /* back up over previous nul-termination */
1681   }
1682
1683   if(add_fakehost) {
1684     char* t = cli_user(sptr)->fakehost;
1685
1686     *m++ = ' ';
1687     *m++ = ':';
1688     ret = 1;
1689     while((*m++ = *t++)) ; /* Empty loop */
1690     --m; /* back up over previous nul-termination */
1691   }
1692   
1693   if(add_keepconn) {
1694     *m++ = ' ';
1695     m += sprintf(m, "%u", sptr->keepconn);
1696   }
1697
1698   *m = '\0';
1699   if (*umodeBuf && cptr)
1700     sendcmdto_one(sptr, CMD_MODE, cptr, ret?"%s %s":"%s :%s", cli_name(sptr), umodeBuf);
1701   return ret;
1702 }
1703
1704 /**
1705  * Check to see if this resembles a sno_mask.  It is if 1) there is
1706  * at least one digit and 2) The first digit occurs before the first
1707  * alphabetic character.
1708  * @param[in] word Word to check for sno_mask-ness.
1709  * @return Non-zero if \a word looks like a server notice mask; zero if not.
1710  */
1711 int is_snomask(char *word)
1712 {
1713   if (word)
1714   {
1715     for (; *word; word++)
1716       if (IsDigit(*word))
1717         return 1;
1718       else if (IsAlpha(*word))
1719         return 0;
1720   }
1721   return 0;
1722 }
1723
1724 /** Update snomask \a oldmask according to \a arg and \a what.
1725  * @param[in] oldmask Original user mask.
1726  * @param[in] arg Update string (either a number or '+'/'-' followed by a number).
1727  * @param[in] what MODE_ADD if adding the mask.
1728  * @return New value of service notice mask.
1729  */
1730 unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
1731 {
1732   unsigned int sno_what;
1733   unsigned int newmask;
1734   if (*arg == '+')
1735   {
1736     arg++;
1737     if (what == MODE_ADD)
1738       sno_what = SNO_ADD;
1739     else
1740       sno_what = SNO_DEL;
1741   }
1742   else if (*arg == '-')
1743   {
1744     arg++;
1745     if (what == MODE_ADD)
1746       sno_what = SNO_DEL;
1747     else
1748       sno_what = SNO_ADD;
1749   }
1750   else
1751     sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL;
1752   /* pity we don't have strtoul everywhere */
1753   newmask = (unsigned int)atoi(arg);
1754   if (sno_what == SNO_DEL)
1755     newmask = oldmask & ~newmask;
1756   else if (sno_what == SNO_ADD)
1757     newmask |= oldmask;
1758   return newmask;
1759 }
1760
1761 /** Remove \a cptr from the singly linked list \a list.
1762  * @param[in] cptr Client to remove from list.
1763  * @param[in,out] list Pointer to head of list containing \a cptr.
1764  */
1765 static void delfrom_list(struct Client *cptr, struct SLink **list)
1766 {
1767   struct SLink* tmp;
1768   struct SLink* prv = NULL;
1769
1770   for (tmp = *list; tmp; tmp = tmp->next) {
1771     if (tmp->value.cptr == cptr) {
1772       if (prv)
1773         prv->next = tmp->next;
1774       else
1775         *list = tmp->next;
1776       free_link(tmp);
1777       break;
1778     }
1779     prv = tmp;
1780   }
1781 }
1782
1783 /** Set \a cptr's server notice mask, according to \a what.
1784  * @param[in,out] cptr Client whose snomask is updating.
1785  * @param[in] newmask Base value for new snomask.
1786  * @param[in] what One of SNO_ADD, SNO_DEL, SNO_SET, to choose operation.
1787  */
1788 void set_snomask(struct Client *cptr, unsigned int newmask, int what)
1789 {
1790   unsigned int oldmask, diffmask;        /* unsigned please */
1791   int i;
1792   struct SLink *tmp;
1793
1794   oldmask = cli_snomask(cptr);
1795
1796   if (what == SNO_ADD)
1797     newmask |= oldmask;
1798   else if (what == SNO_DEL)
1799     newmask = oldmask & ~newmask;
1800   else if (what != SNO_SET)        /* absolute set, no math needed */
1801     sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what);
1802
1803   newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
1804
1805   diffmask = oldmask ^ newmask;
1806
1807   for (i = 0; diffmask >> i; i++) {
1808     if (((diffmask >> i) & 1))
1809     {
1810       if (((newmask >> i) & 1))
1811       {
1812         tmp = make_link();
1813         tmp->next = opsarray[i];
1814         tmp->value.cptr = cptr;
1815         opsarray[i] = tmp;
1816       }
1817       else
1818         /* not real portable :( */
1819         delfrom_list(cptr, &opsarray[i]);
1820     }
1821   }
1822   cli_snomask(cptr) = newmask;
1823 }
1824
1825 /** Check whether \a sptr is allowed to send a message to \a acptr.
1826  * If \a sptr is a remote user, it means some server has an outdated
1827  * SILENCE list for \a acptr, so send the missing SILENCE mask(s) back
1828  * in the direction of \a sptr.  Skip the check if \a sptr is a server.
1829  * @param[in] sptr Client trying to send a message.
1830  * @param[in] acptr Destination of message.
1831  * @return Non-zero if \a sptr is SILENCEd by \a acptr, zero if not.
1832  */
1833 int is_silenced(struct Client *sptr, struct Client *acptr)
1834 {
1835   struct Ban *found;
1836   struct User *user;
1837   size_t buf_used, slen;
1838   char buf[BUFSIZE];
1839
1840   if (IsServer(sptr) || !(user = cli_user(acptr))
1841       || !(found = find_ban(sptr, user->silence)))
1842     return 0;
1843   assert(!(found->flags & BAN_EXCEPTION));
1844   if (!MyConnect(sptr)) {
1845     /* Buffer positive silence to send back. */
1846     buf_used = strlen(found->banstr);
1847     memcpy(buf, found->banstr, buf_used);
1848     /* Add exceptions to buffer. */
1849     for (found = user->silence; found; found = found->next) {
1850       if (!(found->flags & BAN_EXCEPTION))
1851         continue;
1852       slen = strlen(found->banstr);
1853       if (buf_used + slen + 4 > 400) {
1854         buf[buf_used] = '\0';
1855         sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf);
1856         buf_used = 0;
1857       }
1858       if (buf_used)
1859         buf[buf_used++] = ',';
1860       buf[buf_used++] = '+';
1861       buf[buf_used++] = '~';
1862       memcpy(buf + buf_used, found->banstr, slen);
1863       buf_used += slen;
1864     }
1865     /* Flush silence buffer. */
1866     if (buf_used) {
1867       buf[buf_used] = '\0';
1868       sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf);
1869       buf_used = 0;
1870     }
1871   }
1872   return 1;
1873 }
1874
1875 /** Send RPL_ISUPPORT lines to \a cptr.
1876  * @param[in] cptr Client to send ISUPPORT to.
1877  * @return Zero.
1878  */
1879 int
1880 send_supported(struct Client *cptr)
1881 {
1882   char featurebuf[512];
1883
1884   ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES1, FEATURESVALUES1);
1885   send_reply(cptr, RPL_ISUPPORT, featurebuf);
1886   ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES2, FEATURESVALUES2);
1887   send_reply(cptr, RPL_ISUPPORT, featurebuf);
1888
1889   return 0; /* convenience return, if it's ever needed */
1890 }
1891
1892 /* vim: shiftwidth=2 
1893  */