1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2020 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file user.c |
23 |
* \brief User related functions. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "list.h" |
29 |
#include "user.h" |
30 |
#include "channel.h" |
31 |
#include "channel_mode.h" |
32 |
#include "client.h" |
33 |
#include "hash.h" |
34 |
#include "id.h" |
35 |
#include "irc_string.h" |
36 |
#include "ircd.h" |
37 |
#include "listener.h" |
38 |
#include "motd.h" |
39 |
#include "numeric.h" |
40 |
#include "conf.h" |
41 |
#include "conf_gecos.h" |
42 |
#include "log.h" |
43 |
#include "server.h" |
44 |
#include "server_capab.h" /* TBR: RHOST compatibility mode */ |
45 |
#include "send.h" |
46 |
#include "memory.h" |
47 |
#include "packet.h" |
48 |
#include "rng_mt.h" |
49 |
#include "misc.h" |
50 |
#include "parse.h" |
51 |
#include "watch.h" |
52 |
#include "isupport.h" |
53 |
#include "tls.h" |
54 |
#include "patchlevel.h" |
55 |
|
56 |
static char umode_buffer[UMODE_MAX_STR]; |
57 |
|
58 |
const struct user_modes *umode_map[256]; |
59 |
const struct user_modes umode_tab[] = |
60 |
{ |
61 |
{ 'D', UMODE_DEAF }, |
62 |
{ 'F', UMODE_FARCONNECT }, |
63 |
{ 'G', UMODE_SOFTCALLERID }, |
64 |
{ 'H', UMODE_HIDDEN }, |
65 |
{ 'X', UMODE_EXPIRATION }, |
66 |
{ 'R', UMODE_REGONLY }, |
67 |
{ 'S', UMODE_SECURE }, |
68 |
{ 'W', UMODE_WEBIRC }, |
69 |
{ 'a', UMODE_ADMIN }, |
70 |
{ 'b', UMODE_BOTS }, |
71 |
{ 'c', UMODE_CCONN }, |
72 |
{ 'd', UMODE_DEBUG }, |
73 |
{ 'e', UMODE_EXTERNAL }, |
74 |
{ 'f', UMODE_FULL }, |
75 |
{ 'g', UMODE_CALLERID }, |
76 |
{ 'i', UMODE_INVISIBLE }, |
77 |
{ 'j', UMODE_REJ }, |
78 |
{ 'k', UMODE_SKILL }, |
79 |
{ 'l', UMODE_LOCOPS }, |
80 |
{ 'n', UMODE_NCHANGE }, |
81 |
{ 'o', UMODE_OPER }, |
82 |
{ 'p', UMODE_HIDECHANS }, |
83 |
{ 'q', UMODE_HIDEIDLE }, |
84 |
{ 'r', UMODE_REGISTERED }, |
85 |
{ 's', UMODE_SERVNOTICE }, |
86 |
{ 'u', UMODE_UNAUTH }, |
87 |
{ 'w', UMODE_WALLOP }, |
88 |
{ 'y', UMODE_SPY }, |
89 |
{ '\0', 0 } |
90 |
}; |
91 |
|
92 |
void |
93 |
user_modes_init(void) |
94 |
{ |
95 |
char *umode_buffer_ptr = umode_buffer; |
96 |
|
97 |
for (const struct user_modes *tab = umode_tab; tab->c; ++tab) |
98 |
{ |
99 |
umode_map[tab->c] = tab; |
100 |
*umode_buffer_ptr++ = tab->c; |
101 |
} |
102 |
|
103 |
*umode_buffer_ptr = '\0'; |
104 |
} |
105 |
|
106 |
/* show_lusers() |
107 |
* |
108 |
* inputs - pointer to client |
109 |
* output - NONE |
110 |
* side effects - display to client user counts etc. |
111 |
*/ |
112 |
void |
113 |
show_lusers(struct Client *client) |
114 |
{ |
115 |
if (ConfigServerHide.hide_servers == 0 || HasUMode(client, UMODE_OPER)) |
116 |
sendto_one_numeric(client, &me, RPL_LUSERCLIENT, (dlink_list_length(&global_client_list) - Count.invisi), |
117 |
Count.invisi, dlink_list_length(&global_server_list)); |
118 |
else |
119 |
sendto_one_numeric(client, &me, RPL_LUSERCLIENT, (dlink_list_length(&global_client_list) - Count.invisi), |
120 |
Count.invisi, 1); |
121 |
|
122 |
if (Count.oper) |
123 |
sendto_one_numeric(client, &me, RPL_LUSEROP, Count.oper); |
124 |
|
125 |
if (dlink_list_length(&unknown_list)) |
126 |
sendto_one_numeric(client, &me, RPL_LUSERUNKNOWN, dlink_list_length(&unknown_list)); |
127 |
|
128 |
if (dlink_list_length(channel_get_list())) |
129 |
sendto_one_numeric(client, &me, RPL_LUSERCHANNELS, dlink_list_length(channel_get_list())); |
130 |
|
131 |
if (ConfigServerHide.hide_servers == 0 || HasUMode(client, UMODE_OPER)) |
132 |
{ |
133 |
sendto_one_numeric(client, &me, RPL_LUSERME, dlink_list_length(&local_client_list), dlink_list_length(&local_server_list)); |
134 |
sendto_one_numeric(client, &me, RPL_LOCALUSERS, dlink_list_length(&local_client_list), Count.max_loc); |
135 |
sendto_one_numeric(client, &me, RPL_GLOBALUSERS, dlink_list_length(&global_client_list), Count.max_tot); |
136 |
sendto_one_numeric(client, &me, RPL_STATSCONN, Count.max_loc_con, Count.max_loc, Count.totalrestartcount); |
137 |
} |
138 |
else |
139 |
{ |
140 |
sendto_one_numeric(client, &me, RPL_LUSERME, dlink_list_length(&global_client_list), 0); |
141 |
sendto_one_numeric(client, &me, RPL_LOCALUSERS, dlink_list_length(&global_client_list), Count.max_tot); |
142 |
sendto_one_numeric(client, &me, RPL_GLOBALUSERS, dlink_list_length(&global_client_list), Count.max_tot); |
143 |
} |
144 |
} |
145 |
|
146 |
/* report_and_set_user_flags() |
147 |
* |
148 |
* inputs - pointer to client |
149 |
* - pointer to conf for this user |
150 |
* output - NONE |
151 |
* side effects - Report to user any special flags |
152 |
* they are getting, and set them. |
153 |
*/ |
154 |
static void |
155 |
report_and_set_user_flags(struct Client *client, const struct MaskItem *conf) |
156 |
{ |
157 |
/* If this user is being spoofed, tell them so */ |
158 |
if (IsConfDoSpoofIp(conf)) |
159 |
sendto_one_notice(client, &me, ":*** Spoofing your IP"); |
160 |
|
161 |
/* If this user is in the exception class, set it "E lined" */ |
162 |
if (IsConfExemptKline(conf)) |
163 |
{ |
164 |
AddFlag(client, FLAGS_EXEMPTKLINE); |
165 |
sendto_one_notice(client, &me, ":*** You are exempt from K/D lines"); |
166 |
} |
167 |
|
168 |
if (IsConfExemptXline(conf)) |
169 |
{ |
170 |
AddFlag(client, FLAGS_EXEMPTXLINE); |
171 |
sendto_one_notice(client, &me, ":*** You are exempt from X lines"); |
172 |
} |
173 |
|
174 |
if (IsConfExemptResv(conf)) |
175 |
{ |
176 |
AddFlag(client, FLAGS_EXEMPTRESV); |
177 |
sendto_one_notice(client, &me, ":*** You are exempt from resvs"); |
178 |
} |
179 |
|
180 |
/* If this user is exempt from user limits set it "F lined" */ |
181 |
if (IsConfExemptLimits(conf)) |
182 |
{ |
183 |
AddFlag(client, FLAGS_NOLIMIT); |
184 |
sendto_one_notice(client, &me, ":*** You are exempt from user limits"); |
185 |
} |
186 |
|
187 |
if (IsConfCanFlood(conf)) |
188 |
{ |
189 |
AddFlag(client, FLAGS_CANFLOOD); |
190 |
sendto_one_notice(client, &me, ":*** You are exempt from flood protection"); |
191 |
} |
192 |
} |
193 |
|
194 |
/* introduce_client() |
195 |
* |
196 |
* inputs - client |
197 |
* output - NONE |
198 |
* side effects - This common function introduces a client to the rest |
199 |
* of the net, either from a local client connect or |
200 |
* from a remote connect. |
201 |
*/ |
202 |
static void |
203 |
introduce_client(struct Client *client) |
204 |
{ |
205 |
dlink_node *node; |
206 |
char buf[UMODE_MAX_STR] = ""; |
207 |
|
208 |
send_umode(client, MyConnect(client), 0, buf); |
209 |
watch_check_hash(client, RPL_LOGON); |
210 |
|
211 |
if (buf[0] == '\0') |
212 |
{ |
213 |
buf[0] = '+'; |
214 |
buf[1] = '\0'; |
215 |
} |
216 |
|
217 |
DLINK_FOREACH(node, local_server_list.head) |
218 |
{ |
219 |
struct Client *server = node->data; |
220 |
|
221 |
if (server == client->from) |
222 |
continue; |
223 |
|
224 |
/* TBR: compatibility mode */ |
225 |
if (IsCapable(server, CAPAB_RHOST)) |
226 |
sendto_one(server, ":%s UID %s %u %ju %s %s %s %s %s %s %s :%s", |
227 |
client->servptr->id, |
228 |
client->name, client->hopcount+1, |
229 |
client->tsinfo, |
230 |
buf, client->username, client->host, client->realhost, |
231 |
client->sockhost, client->id, |
232 |
client->account, |
233 |
client->info); |
234 |
else |
235 |
sendto_one(server, ":%s UID %s %u %ju %s %s %s %s %s %s :%s", |
236 |
client->servptr->id, |
237 |
client->name, client->hopcount+1, |
238 |
client->tsinfo, |
239 |
buf, client->username, client->host, |
240 |
client->sockhost, client->id, |
241 |
client->account, |
242 |
client->info); |
243 |
|
244 |
if (!EmptyString(client->tls_certfp)) |
245 |
sendto_one(server, ":%s CERTFP %s", client->id, client->tls_certfp); |
246 |
} |
247 |
} |
248 |
|
249 |
/* user_welcome() |
250 |
* |
251 |
* inputs - client pointer to client to welcome |
252 |
* output - NONE |
253 |
* side effects - |
254 |
*/ |
255 |
static void |
256 |
user_welcome(struct Client *client) |
257 |
{ |
258 |
static const char built_date[] = __DATE__ " at " __TIME__; |
259 |
|
260 |
if (HasFlag(client, FLAGS_TLS)) |
261 |
{ |
262 |
AddUMode(client, UMODE_SECURE); |
263 |
|
264 |
client->tls_cipher = xstrdup(tls_get_cipher(&client->connection->fd->tls)); |
265 |
sendto_one_notice(client, &me, ":*** Connected securely via %s", |
266 |
client->tls_cipher); |
267 |
} |
268 |
|
269 |
sendto_one_numeric(client, &me, RPL_WELCOME, ConfigServerInfo.network_name, |
270 |
client->name, client->username, client->realhost); |
271 |
sendto_one_numeric(client, &me, RPL_YOURHOST, |
272 |
listener_get_name(client->connection->listener), PATCHLEVEL); |
273 |
sendto_one_numeric(client, &me, RPL_CREATED, built_date); |
274 |
sendto_one_numeric(client, &me, RPL_MYINFO, me.name, PATCHLEVEL, umode_buffer); |
275 |
|
276 |
isupport_show(client); |
277 |
show_lusers(client); |
278 |
motd_signon(client); |
279 |
} |
280 |
|
281 |
/*! \brief This function is called when both NICK and USER messages |
282 |
* have been accepted for the client, in whatever order. Only |
283 |
* after this, is the UID message propagated. |
284 |
* \param client Pointer to given client to introduce |
285 |
*/ |
286 |
void |
287 |
register_local_user(struct Client *client) |
288 |
{ |
289 |
const struct MaskItem *conf = NULL; |
290 |
|
291 |
assert(client == client->from); |
292 |
assert(client->connection->registration == 0); |
293 |
assert(MyConnect(client)); |
294 |
assert(IsUnknown(client)); |
295 |
|
296 |
if (ConfigGeneral.ping_cookie) |
297 |
{ |
298 |
if (!HasFlag(client, FLAGS_PINGSENT) && client->connection->random_ping == 0) |
299 |
{ |
300 |
do |
301 |
client->connection->random_ping = genrand_int32(); |
302 |
while (client->connection->random_ping == 0); |
303 |
|
304 |
sendto_one(client, "PING :%u", client->connection->random_ping); |
305 |
AddFlag(client, FLAGS_PINGSENT); |
306 |
return; |
307 |
} |
308 |
|
309 |
if (!HasFlag(client, FLAGS_PING_COOKIE)) |
310 |
return; |
311 |
} |
312 |
|
313 |
if (conf_check_client(client) == false) |
314 |
return; |
315 |
|
316 |
conf = client->connection->confs.head->data; |
317 |
|
318 |
if (!HasFlag(client, FLAGS_GOTID)) |
319 |
{ |
320 |
char username[USERLEN + 1]; |
321 |
unsigned int i = 0; |
322 |
|
323 |
if (IsNeedIdentd(conf)) |
324 |
{ |
325 |
++ServerStats.is_ref; |
326 |
sendto_one_notice(client, &me, ":*** Notice -- You need to install " |
327 |
"identd to use this server"); |
328 |
exit_client(client, "Install identd"); |
329 |
return; |
330 |
} |
331 |
|
332 |
strlcpy(username, client->username, sizeof(username)); |
333 |
|
334 |
if (!IsNoTilde(conf)) |
335 |
client->username[i++] = '~'; |
336 |
|
337 |
for (const char *p = username; *p && i < USERLEN; ++p) |
338 |
client->username[i++] = *p; |
339 |
|
340 |
client->username[i] = '\0'; |
341 |
} |
342 |
|
343 |
/* Password check */ |
344 |
if (!EmptyString(conf->passwd)) |
345 |
{ |
346 |
if (match_conf_password(client->connection->password, conf) == false) |
347 |
{ |
348 |
++ServerStats.is_ref; |
349 |
|
350 |
sendto_one_numeric(client, &me, ERR_PASSWDMISMATCH); |
351 |
exit_client(client, "Bad Password"); |
352 |
return; |
353 |
} |
354 |
} |
355 |
|
356 |
xfree(client->connection->password); |
357 |
client->connection->password = NULL; |
358 |
|
359 |
/* |
360 |
* Report if user has &^>= etc. and set flags as needed in client |
361 |
*/ |
362 |
report_and_set_user_flags(client, conf); |
363 |
|
364 |
if (IsDead(client)) |
365 |
return; |
366 |
|
367 |
/* |
368 |
* Limit clients - |
369 |
* We want to be able to have servers and F-line clients |
370 |
* connect, so save room for "buffer" connections. |
371 |
* Smaller servers may want to decrease this, and it should |
372 |
* probably be just a percentage of the MAXCLIENTS... |
373 |
* -Taner |
374 |
*/ |
375 |
if ((dlink_list_length(&local_client_list) >= GlobalSetOptions.maxclients + MAX_BUFFER) || |
376 |
(dlink_list_length(&local_client_list) >= GlobalSetOptions.maxclients && !HasFlag(client, FLAGS_NOLIMIT))) |
377 |
{ |
378 |
sendto_realops_flags(UMODE_FULL, L_ALL, SEND_NOTICE, |
379 |
"Too many clients, rejecting %s[%s].", |
380 |
client->name, client->host); |
381 |
++ServerStats.is_ref; |
382 |
exit_client(client, "Sorry, server is full - try later"); |
383 |
return; |
384 |
} |
385 |
|
386 |
if (valid_username(client->username, true) == false) |
387 |
{ |
388 |
char buf[IRCD_BUFSIZE]; |
389 |
|
390 |
sendto_realops_flags(UMODE_REJ, L_ALL, SEND_NOTICE, |
391 |
"Invalid username: %s (%s@%s)", |
392 |
client->name, client->username, client->host); |
393 |
++ServerStats.is_ref; |
394 |
snprintf(buf, sizeof(buf), "Invalid username [%s]", client->username); |
395 |
exit_client(client, buf); |
396 |
return; |
397 |
} |
398 |
|
399 |
if (!HasFlag(client, FLAGS_EXEMPTXLINE)) |
400 |
{ |
401 |
const struct GecosItem *gecos = gecos_find(client->info, match); |
402 |
if (gecos) |
403 |
{ |
404 |
sendto_realops_flags(UMODE_REJ, L_ALL, SEND_NOTICE, |
405 |
"X-line Rejecting [%s] [%s], user %s [%s]", |
406 |
client->info, gecos->reason, |
407 |
client_get_name(client, HIDE_IP), |
408 |
client->sockhost); |
409 |
++ServerStats.is_ref; |
410 |
exit_client(client, "Bad user info"); |
411 |
return; |
412 |
} |
413 |
} |
414 |
|
415 |
const char *id; |
416 |
while (hash_find_id((id = uid_get()))) |
417 |
; |
418 |
|
419 |
strlcpy(client->id, id, sizeof(client->id)); |
420 |
hash_add_id(client); |
421 |
|
422 |
sendto_realops_flags(UMODE_CCONN, L_ALL, SEND_NOTICE, |
423 |
"Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s>", |
424 |
client->name, client->username, client->realhost, |
425 |
client->sockhost, |
426 |
get_client_class(&client->connection->confs), |
427 |
client->info, client->id); |
428 |
|
429 |
if (ConfigGeneral.invisible_on_connect) |
430 |
{ |
431 |
AddUMode(client, UMODE_INVISIBLE); |
432 |
++Count.invisi; |
433 |
} |
434 |
|
435 |
SetClient(client); |
436 |
|
437 |
client->servptr = &me; |
438 |
client->connection->last_privmsg = event_base->time.sec_monotonic; |
439 |
|
440 |
dlinkAdd(client, &client->lnode, &client->servptr->serv->client_list); |
441 |
dlinkAdd(client, &client->node, &global_client_list); |
442 |
|
443 |
assert(dlinkFind(&unknown_list, client)); |
444 |
|
445 |
dlink_move_node(&client->connection->lclient_node, |
446 |
&unknown_list, &local_client_list); |
447 |
|
448 |
if (dlink_list_length(&local_client_list) > Count.max_loc) |
449 |
{ |
450 |
Count.max_loc = dlink_list_length(&local_client_list); |
451 |
|
452 |
if (!(Count.max_loc % 10)) |
453 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
454 |
"New maximum local client connections: %u", |
455 |
Count.max_loc); |
456 |
} |
457 |
|
458 |
if ((dlink_list_length(&local_client_list) + |
459 |
dlink_list_length(&local_server_list)) > Count.max_loc_con) |
460 |
Count.max_loc_con = dlink_list_length(&local_client_list) + |
461 |
dlink_list_length(&local_server_list); |
462 |
|
463 |
if (dlink_list_length(&global_client_list) > Count.max_tot) |
464 |
Count.max_tot = dlink_list_length(&global_client_list); |
465 |
++Count.totalrestartcount; |
466 |
|
467 |
user_welcome(client); |
468 |
|
469 |
introduce_client(client); |
470 |
} |
471 |
|
472 |
/* register_remote_user() |
473 |
* |
474 |
* inputs - client remote or directly connected client |
475 |
* - username to register as |
476 |
* - host name to register as |
477 |
* - server name |
478 |
* output - NONE |
479 |
* side effects - This function is called when a remote client |
480 |
* is introduced by a server. |
481 |
*/ |
482 |
void |
483 |
register_remote_user(struct Client *client) |
484 |
{ |
485 |
assert(client->servptr->from == client->from); |
486 |
|
487 |
/* |
488 |
* If the nick has been introduced by a services server, |
489 |
* make it a service as well. |
490 |
*/ |
491 |
if (HasFlag(client->servptr, FLAGS_SERVICE)) |
492 |
AddFlag(client, FLAGS_SERVICE); |
493 |
|
494 |
SetClient(client); |
495 |
dlinkAdd(client, &client->lnode, &client->servptr->serv->client_list); |
496 |
dlinkAdd(client, &client->node, &global_client_list); |
497 |
|
498 |
if (dlink_list_length(&global_client_list) > Count.max_tot) |
499 |
Count.max_tot = dlink_list_length(&global_client_list); |
500 |
|
501 |
if (HasFlag(client->servptr, FLAGS_EOB)) |
502 |
sendto_realops_flags(UMODE_FARCONNECT, L_ALL, SEND_NOTICE, |
503 |
"Client connecting at %s: %s (%s@%s) [%s] [%s] <%s>", |
504 |
client->servptr->name, |
505 |
client->name, client->username, client->realhost, |
506 |
client->sockhost, client->info, client->id); |
507 |
|
508 |
introduce_client(client); |
509 |
} |
510 |
|
511 |
/* valid_hostname() |
512 |
* |
513 |
* Inputs - pointer to hostname |
514 |
* Output - 1 if valid, 0 if not |
515 |
* Side effects - check hostname for validity |
516 |
* |
517 |
* NOTE: this doesn't allow a hostname to begin with a dot and |
518 |
* will not allow more dots than chars. |
519 |
*/ |
520 |
bool |
521 |
valid_hostname(const char *hostname) |
522 |
{ |
523 |
const char *p = hostname; |
524 |
|
525 |
assert(p); |
526 |
|
527 |
if (EmptyString(p) || *p == '.' || *p == ':') |
528 |
return false; |
529 |
|
530 |
for (; *p; ++p) |
531 |
if (!IsHostChar(*p)) |
532 |
return false; |
533 |
|
534 |
return p - hostname <= HOSTLEN; |
535 |
} |
536 |
|
537 |
/* valid_username() |
538 |
* |
539 |
* Inputs - pointer to user |
540 |
* Output - 1 if valid, 0 if not |
541 |
* Side effects - check username for validity |
542 |
* |
543 |
* Absolutely always reject any '*' '!' '?' '@' in an user name |
544 |
* reject any odd control characters names. |
545 |
* Allow '.' in username to allow for "first.last" |
546 |
* style of username |
547 |
*/ |
548 |
bool |
549 |
valid_username(const char *username, bool local) |
550 |
{ |
551 |
const char *p = username; |
552 |
|
553 |
assert(p); |
554 |
|
555 |
if (*p == '~') |
556 |
++p; |
557 |
|
558 |
/* |
559 |
* Reject usernames that don't start with an alphanum |
560 |
* i.e. reject jokers who have '-@somehost' or '.@somehost' |
561 |
* or "-hi-@somehost", "h-----@somehost" would still be accepted. |
562 |
*/ |
563 |
if (!IsAlNum(*p)) |
564 |
return false; |
565 |
|
566 |
if (local) |
567 |
{ |
568 |
unsigned int dots = 0; |
569 |
|
570 |
while (*++p) |
571 |
{ |
572 |
if (*p == '.' && ConfigGeneral.dots_in_ident) |
573 |
{ |
574 |
if (++dots > ConfigGeneral.dots_in_ident) |
575 |
return false; |
576 |
if (!IsUserChar(*(p + 1))) |
577 |
return false; |
578 |
} |
579 |
else if (!IsUserChar(*p)) |
580 |
return false; |
581 |
} |
582 |
} |
583 |
else |
584 |
{ |
585 |
while (*++p) |
586 |
if (!IsUserChar(*p)) |
587 |
return false; |
588 |
} |
589 |
|
590 |
return p - username <= USERLEN; |
591 |
} |
592 |
|
593 |
/* clean_nick_name() |
594 |
* |
595 |
* input - nickname |
596 |
* - whether it's a local nick (1) or remote (0) |
597 |
* output - none |
598 |
* side effects - walks through the nickname, returning 0 if erroneous |
599 |
*/ |
600 |
bool |
601 |
valid_nickname(const char *nickname, bool local) |
602 |
{ |
603 |
const char *p = nickname; |
604 |
|
605 |
assert(p); |
606 |
|
607 |
/* |
608 |
* Nicks can't start with a digit or - or be 0 length. |
609 |
*/ |
610 |
if (EmptyString(p) || *p == '-' || (IsDigit(*p) && local)) |
611 |
return false; |
612 |
|
613 |
for (; *p; ++p) |
614 |
if (!IsNickChar(*p)) |
615 |
return false; |
616 |
|
617 |
return p - nickname <= NICKLEN; |
618 |
} |
619 |
|
620 |
/*! \brief Builds a mode change string to buffer pointed by \a buf |
621 |
* \param client Pointer to client |
622 |
* \param dispatch Whether to send a MODE message to client |
623 |
* \param old Old user mode to compare against when building new mode buffer |
624 |
* \param buf Pointer to buffer to build string in |
625 |
*/ |
626 |
void |
627 |
send_umode(struct Client *client, bool dispatch, unsigned int old, char *buf) |
628 |
{ |
629 |
char *m = buf; |
630 |
int what = 0; |
631 |
|
632 |
/* |
633 |
* Build a string in umode_buf to represent the change in the user's |
634 |
* mode between the new (client->umodes) and 'old'. |
635 |
*/ |
636 |
for (const struct user_modes *tab = umode_tab; tab->c; ++tab) |
637 |
{ |
638 |
if ((tab->flag & old) && !HasUMode(client, tab->flag)) |
639 |
{ |
640 |
if (what == MODE_DEL) |
641 |
*m++ = tab->c; |
642 |
else |
643 |
{ |
644 |
what = MODE_DEL; |
645 |
*m++ = '-'; |
646 |
*m++ = tab->c; |
647 |
} |
648 |
} |
649 |
else if (!(tab->flag & old) && HasUMode(client, tab->flag)) |
650 |
{ |
651 |
if (what == MODE_ADD) |
652 |
*m++ = tab->c; |
653 |
else |
654 |
{ |
655 |
what = MODE_ADD; |
656 |
*m++ = '+'; |
657 |
*m++ = tab->c; |
658 |
} |
659 |
} |
660 |
} |
661 |
|
662 |
*m = '\0'; |
663 |
|
664 |
if (dispatch == true && *buf) |
665 |
sendto_one(client, ":%s!%s@%s MODE %s :%s", |
666 |
client->name, client->username, |
667 |
client->host, client->name, buf); |
668 |
} |
669 |
|
670 |
/* send_umode_out() |
671 |
* |
672 |
* inputs - |
673 |
* output - NONE |
674 |
* side effects - Only send ubuf out to servers that know about this client |
675 |
*/ |
676 |
void |
677 |
send_umode_out(struct Client *client, unsigned int old) |
678 |
{ |
679 |
char buf[UMODE_MAX_STR] = ""; |
680 |
|
681 |
send_umode(client, MyConnect(client), old, buf); |
682 |
|
683 |
if (buf[0]) |
684 |
sendto_server(client, 0, 0, ":%s MODE %s :%s", |
685 |
client->id, client->id, buf); |
686 |
} |
687 |
|
688 |
void |
689 |
user_set_hostmask(struct Client *client, const char *hostname) |
690 |
{ |
691 |
dlink_node *node; |
692 |
|
693 |
if (strcmp(client->host, hostname) == 0) |
694 |
return; |
695 |
|
696 |
if (ConfigGeneral.cycle_on_host_change) |
697 |
sendto_common_channels_local(client, false, 0, CAP_CHGHOST, ":%s!%s@%s QUIT :Changing hostname", |
698 |
client->name, client->username, client->host); |
699 |
|
700 |
sendto_common_channels_local(client, true, CAP_CHGHOST, 0, ":%s!%s@%s CHGHOST %s %s", |
701 |
client->name, client->username, |
702 |
client->host, client->username, hostname); |
703 |
|
704 |
strlcpy(client->host, hostname, sizeof(client->host)); |
705 |
|
706 |
if (MyConnect(client)) |
707 |
{ |
708 |
sendto_one_numeric(client, &me, RPL_VISIBLEHOST, client->host); |
709 |
clear_ban_cache_list(&client->channel); |
710 |
} |
711 |
|
712 |
if (ConfigGeneral.cycle_on_host_change == 0) |
713 |
return; |
714 |
|
715 |
DLINK_FOREACH(node, client->channel.head) |
716 |
{ |
717 |
char modebuf[CMEMBER_STATUS_FLAGS_LEN + 1]; |
718 |
char nickbuf[CMEMBER_STATUS_FLAGS_LEN * NICKLEN + CMEMBER_STATUS_FLAGS_LEN] = ""; |
719 |
char *p = modebuf; |
720 |
int len = 0; |
721 |
const struct ChannelMember *member = node->data; |
722 |
|
723 |
if (has_member_flags(member, CHFL_CHANOP)) |
724 |
{ |
725 |
*p++ = 'o'; |
726 |
len += snprintf(nickbuf + len, sizeof(nickbuf) - len, len ? " %s" : "%s", client->name); |
727 |
} |
728 |
|
729 |
if (has_member_flags(member, CHFL_HALFOP)) |
730 |
{ |
731 |
*p++ = 'h'; |
732 |
len += snprintf(nickbuf + len, sizeof(nickbuf) - len, len ? " %s" : "%s", client->name); |
733 |
} |
734 |
|
735 |
if (has_member_flags(member, CHFL_VOICE)) |
736 |
{ |
737 |
*p++ = 'v'; |
738 |
len += snprintf(nickbuf + len, sizeof(nickbuf) - len, len ? " %s" : "%s", client->name); |
739 |
} |
740 |
|
741 |
*p = '\0'; |
742 |
|
743 |
sendto_channel_local(client, member->channel, 0, CAP_EXTENDED_JOIN, CAP_CHGHOST, ":%s!%s@%s JOIN %s %s :%s", |
744 |
client->name, client->username, |
745 |
client->host, member->channel->name, |
746 |
client->account, client->info); |
747 |
sendto_channel_local(client, member->channel, 0, 0, CAP_EXTENDED_JOIN | CAP_CHGHOST, ":%s!%s@%s JOIN :%s", |
748 |
client->name, client->username, |
749 |
client->host, member->channel->name); |
750 |
|
751 |
if (nickbuf[0]) |
752 |
sendto_channel_local(client, member->channel, 0, 0, CAP_CHGHOST, ":%s MODE %s +%s %s", |
753 |
client->servptr->name, member->channel->name, |
754 |
modebuf, nickbuf); |
755 |
} |
756 |
|
757 |
if (client->away[0]) |
758 |
sendto_common_channels_local(client, false, CAP_AWAY_NOTIFY, CAP_CHGHOST, |
759 |
":%s!%s@%s AWAY :%s", |
760 |
client->name, client->username, |
761 |
client->host, client->away); |
762 |
} |