2 * ircd-ratbox: A slightly useful ircd.
3 * m_nick.c: Sets a users nick.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
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 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 * $Id: m_nick.c 3518 2007-06-22 21:59:09Z jilles $
30 #include "irc_string.h"
48 #include "s_newconf.h"
51 /* Give all UID nicks the same TS. This ensures nick TS is always the same on
52 * all servers for each nick-user pair, also if a user with a UID nick changes
53 * their nick but is collided again (the server detecting the collision will
54 * not propagate the nick change further). -- jilles
56 #define SAVE_NICKTS 100
58 static int mr_nick(struct Client
*, struct Client
*, int, const char **);
59 static int m_nick(struct Client
*, struct Client
*, int, const char **);
60 static int mc_nick(struct Client
*, struct Client
*, int, const char **);
61 static int ms_nick(struct Client
*, struct Client
*, int, const char **);
62 static int ms_uid(struct Client
*, struct Client
*, int, const char **);
63 static int ms_euid(struct Client
*, struct Client
*, int, const char **);
64 static int ms_save(struct Client
*, struct Client
*, int, const char **);
65 static int can_save(struct Client
*);
66 static void save_user(struct Client
*, struct Client
*, struct Client
*);
68 struct Message nick_msgtab
= {
69 "NICK", 0, 0, 0, MFLG_SLOW
,
70 {{mr_nick
, 0}, {m_nick
, 0}, {mc_nick
, 3}, {ms_nick
, 8}, mg_ignore
, {m_nick
, 0}}
72 struct Message uid_msgtab
= {
73 "UID", 0, 0, 0, MFLG_SLOW
,
74 {mg_ignore
, mg_ignore
, mg_ignore
, {ms_uid
, 9}, mg_ignore
, mg_ignore
}
76 struct Message euid_msgtab
= {
77 "EUID", 0, 0, 0, MFLG_SLOW
,
78 {mg_ignore
, mg_ignore
, mg_ignore
, {ms_euid
, 12}, mg_ignore
, mg_ignore
}
80 struct Message save_msgtab
= {
81 "SAVE", 0, 0, 0, MFLG_SLOW
,
82 {mg_ignore
, mg_ignore
, mg_ignore
, {ms_save
, 3}, mg_ignore
, mg_ignore
}
85 mapi_clist_av1 nick_clist
[] = { &nick_msgtab
, &uid_msgtab
, &euid_msgtab
,
88 DECLARE_MODULE_AV1(nick
, NULL
, NULL
, nick_clist
, NULL
, NULL
, "$Revision: 3518 $");
90 static int change_remote_nick(struct Client
*, struct Client
*, time_t,
93 static int clean_nick(const char *, int loc_client
);
94 static int clean_username(const char *);
95 static int clean_host(const char *);
96 static int clean_uid(const char *uid
);
98 static void set_initial_nick(struct Client
*client_p
, struct Client
*source_p
, char *nick
);
99 static void change_local_nick(struct Client
*client_p
, struct Client
*source_p
, char *nick
, int);
100 static int register_client(struct Client
*client_p
, struct Client
*server
,
101 const char *nick
, time_t newts
, int parc
, const char *parv
[]);
103 static int perform_nick_collides(struct Client
*, struct Client
*,
104 struct Client
*, int, const char **,
105 time_t, const char *, const char *);
106 static int perform_nickchange_collides(struct Client
*, struct Client
*,
107 struct Client
*, int, const char **, time_t, const char *);
110 * parv[0] = sender prefix
114 mr_nick(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
116 struct Client
*target_p
;
120 if (strlen(client_p
->id
) == 3)
122 exit_client(client_p
, client_p
, client_p
, "Mixing client and server protocol");
126 if(parc
< 2 || EmptyString(parv
[1]) || (parv
[1][0] == '~'))
128 sendto_one(source_p
, form_str(ERR_NONICKNAMEGIVEN
),
129 me
.name
, EmptyString(source_p
->name
) ? "*" : source_p
->name
);
133 /* due to the scandinavian origins, (~ being uppercase of ^) and ~
134 * being disallowed as a nick char, we need to chop the first ~
135 * instead of just erroring.
137 if((s
= strchr(parv
[1], '~')))
140 /* copy the nick and terminate it */
141 strlcpy(nick
, parv
[1], sizeof(nick
));
143 /* check the nickname is ok */
144 if(!clean_nick(nick
, 1))
146 sendto_one(source_p
, form_str(ERR_ERRONEUSNICKNAME
),
147 me
.name
, EmptyString(parv
[0]) ? "*" : parv
[0], parv
[1]);
151 /* check if the nick is resv'd */
152 if(find_nick_resv(nick
))
154 sendto_one(source_p
, form_str(ERR_ERRONEUSNICKNAME
),
155 me
.name
, EmptyString(source_p
->name
) ? "*" : source_p
->name
, nick
);
159 if(hash_find_nd(nick
))
161 sendto_one(source_p
, form_str(ERR_UNAVAILRESOURCE
),
162 me
.name
, EmptyString(source_p
->name
) ? "*" : source_p
->name
, nick
);
166 if((target_p
= find_named_client(nick
)) == NULL
)
167 set_initial_nick(client_p
, source_p
, nick
);
168 else if(source_p
== target_p
)
169 strcpy(source_p
->name
, nick
);
171 sendto_one(source_p
, form_str(ERR_NICKNAMEINUSE
), me
.name
, "*", nick
);
177 * parv[0] = sender prefix
181 m_nick(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
183 struct Client
*target_p
;
187 if(parc
< 2 || EmptyString(parv
[1]) || (parv
[1][0] == '~'))
189 sendto_one(source_p
, form_str(ERR_NONICKNAMEGIVEN
), me
.name
, source_p
->name
);
193 /* due to the scandinavian origins, (~ being uppercase of ^) and ~
194 * being disallowed as a nick char, we need to chop the first ~
195 * instead of just erroring.
197 if((s
= strchr(parv
[1], '~')))
200 /* mark end of grace period, to prevent nickflooding */
201 if(!IsFloodDone(source_p
))
202 flood_endgrace(source_p
);
204 /* terminate nick to NICKLEN, we dont want clean_nick() to error! */
205 strlcpy(nick
, parv
[1], sizeof(nick
));
207 /* check the nickname is ok */
208 if(!clean_nick(nick
, 1))
210 sendto_one(source_p
, form_str(ERR_ERRONEUSNICKNAME
), me
.name
, parv
[0], nick
);
214 if(!IsExemptResv(source_p
) && find_nick_resv(nick
))
216 sendto_one(source_p
, form_str(ERR_ERRONEUSNICKNAME
), me
.name
, source_p
->name
, nick
);
220 if(hash_find_nd(nick
))
222 sendto_one(source_p
, form_str(ERR_UNAVAILRESOURCE
),
223 me
.name
, EmptyString(source_p
->name
) ? "*" : source_p
->name
, nick
);
227 if((target_p
= find_named_client(nick
)))
229 /* If(target_p == source_p) the client is changing nicks between
230 * equivalent nicknames ie: [nick] -> {nick}
232 if(target_p
== source_p
)
234 /* check the nick isnt exactly the same */
235 if(strcmp(target_p
->name
, nick
))
236 change_local_nick(client_p
, source_p
, nick
, 1);
240 /* drop unregged client */
241 else if(IsUnknown(target_p
))
243 exit_client(NULL
, target_p
, &me
, "Overridden");
244 change_local_nick(client_p
, source_p
, nick
, 1);
247 sendto_one(source_p
, form_str(ERR_NICKNAMEINUSE
), me
.name
, parv
[0], nick
);
252 change_local_nick(client_p
, source_p
, nick
, 1);
259 * server -> server nick change
260 * parv[0] = sender prefix
262 * parv[2] = TS when nick change
264 * server introducing new nick
265 * parv[0] = sender prefix
267 * parv[2] = hop count
276 mc_nick(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
278 struct Client
*target_p
;
281 /* if nicks erroneous, or too long, kill */
282 if(!clean_nick(parv
[1], 0))
284 ServerStats
->is_kill
++;
285 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
286 "Bad Nick: %s From: %s(via %s)",
287 parv
[1], source_p
->servptr
->name
, client_p
->name
);
288 sendto_one(client_p
, ":%s KILL %s :%s (Bad Nickname)", me
.name
, parv
[1], me
.name
);
290 /* bad nick change, issue kill for the old nick to the rest
293 kill_client_serv_butone(client_p
, source_p
, "%s (Bad Nickname)", me
.name
);
294 source_p
->flags
|= FLAGS_KILLED
;
295 exit_client(client_p
, source_p
, &me
, "Bad Nickname");
299 newts
= atol(parv
[2]);
300 target_p
= find_named_client(parv
[1]);
302 /* if the nick doesnt exist, allow it and process like normal */
305 change_remote_nick(client_p
, source_p
, newts
, parv
[1], 1);
307 else if(IsUnknown(target_p
))
309 exit_client(NULL
, target_p
, &me
, "Overridden");
310 change_remote_nick(client_p
, source_p
, newts
, parv
[1], 1);
312 else if(target_p
== source_p
)
314 /* client changing case of nick */
315 if(strcmp(target_p
->name
, parv
[1]))
316 change_remote_nick(client_p
, source_p
, newts
, parv
[1], 1);
318 /* we've got a collision! */
320 perform_nickchange_collides(source_p
, client_p
, target_p
,
321 parc
, parv
, newts
, parv
[1]);
327 ms_nick(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
329 struct Client
*target_p
;
334 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
335 "Dropping server %s due to (invalid) command 'NICK' "
336 "with %d arguments (expecting 9)", client_p
->name
, parc
);
337 ilog(L_SERVER
, "Excess parameters (%d) for command 'NICK' from %s.",
338 parc
, client_p
->name
);
339 exit_client(client_p
, client_p
, client_p
, "Excess parameters to NICK command");
343 /* if nicks empty, erroneous, or too long, kill */
344 if(!clean_nick(parv
[1], 0))
346 ServerStats
->is_kill
++;
347 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
348 "Bad Nick: %s From: %s(via %s)",
349 parv
[1], parv
[7], client_p
->name
);
350 sendto_one(client_p
, ":%s KILL %s :%s (Bad Nickname)", me
.name
, parv
[1], me
.name
);
354 /* invalid username or host? */
355 if(!clean_username(parv
[5]) || !clean_host(parv
[6]))
357 ServerStats
->is_kill
++;
358 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
359 "Bad user@host: %s@%s From: %s(via %s)",
360 parv
[5], parv
[6], parv
[7], client_p
->name
);
361 sendto_one(client_p
, ":%s KILL %s :%s (Bad user@host)", me
.name
, parv
[1], me
.name
);
365 /* check the length of the clients gecos */
366 if(strlen(parv
[8]) > REALLEN
)
368 char *s
= LOCAL_COPY(parv
[8]);
369 /* why exactly do we care? --fl */
370 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
371 "Long realname from server %s for %s", parv
[7], parv
[1]);
377 newts
= atol(parv
[3]);
379 target_p
= find_named_client(parv
[1]);
381 /* if the nick doesnt exist, allow it and process like normal */
384 register_client(client_p
, NULL
, parv
[1], newts
, parc
, parv
);
386 else if(IsUnknown(target_p
))
388 exit_client(NULL
, target_p
, &me
, "Overridden");
389 register_client(client_p
, NULL
, parv
[1], newts
, parc
, parv
);
391 else if(target_p
== source_p
)
393 /* client changing case of nick */
394 if(strcmp(target_p
->name
, parv
[1]))
395 register_client(client_p
, NULL
, parv
[1], newts
, parc
, parv
);
397 /* we've got a collision! */
399 perform_nick_collides(source_p
, client_p
, target_p
, parc
, parv
,
400 newts
, parv
[1], NULL
);
417 ms_uid(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
419 struct Client
*target_p
;
422 newts
= atol(parv
[3]);
426 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
427 "Dropping server %s due to (invalid) command 'UID' "
428 "with %d arguments (expecting 10)", client_p
->name
, parc
);
429 ilog(L_SERVER
, "Excess parameters (%d) for command 'UID' from %s.",
430 parc
, client_p
->name
);
431 exit_client(client_p
, client_p
, client_p
, "Excess parameters to UID command");
435 /* if nicks erroneous, or too long, kill */
436 if(!clean_nick(parv
[1], 0))
438 ServerStats
->is_kill
++;
439 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
440 "Bad Nick: %s From: %s(via %s)",
441 parv
[1], source_p
->name
, client_p
->name
);
442 sendto_one(client_p
, ":%s KILL %s :%s (Bad Nickname)", me
.id
, parv
[8], me
.name
);
446 if(!clean_username(parv
[5]) || !clean_host(parv
[6]))
448 ServerStats
->is_kill
++;
449 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
450 "Bad user@host: %s@%s From: %s(via %s)",
451 parv
[5], parv
[6], source_p
->name
, client_p
->name
);
452 sendto_one(client_p
, ":%s KILL %s :%s (Bad user@host)", me
.id
, parv
[8], me
.name
);
456 if(!clean_uid(parv
[8]))
458 ServerStats
->is_kill
++;
459 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
460 "Bad UID: %s From: %s(via %s)",
461 parv
[8], source_p
->name
, client_p
->name
);
462 sendto_one(client_p
, ":%s KILL %s :%s (Bad UID)", me
.id
, parv
[8], me
.name
);
466 /* check length of clients gecos */
467 if(strlen(parv
[9]) > REALLEN
)
469 char *s
= LOCAL_COPY(parv
[9]);
470 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Long realname from server %s for %s",
476 target_p
= find_named_client(parv
[1]);
480 register_client(client_p
, source_p
, parv
[1], newts
, parc
, parv
);
482 else if(IsUnknown(target_p
))
484 exit_client(NULL
, target_p
, &me
, "Overridden");
485 register_client(client_p
, source_p
, parv
[1], newts
, parc
, parv
);
487 /* we've got a collision! */
489 perform_nick_collides(source_p
, client_p
, target_p
, parc
, parv
,
490 newts
, parv
[1], parv
[8]);
509 ms_euid(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
511 struct Client
*target_p
;
514 newts
= atol(parv
[3]);
518 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
519 "Dropping server %s due to (invalid) command 'EUID' "
520 "with %d arguments (expecting 12)", client_p
->name
, parc
);
521 ilog(L_SERVER
, "Excess parameters (%d) for command 'EUID' from %s.",
522 parc
, client_p
->name
);
523 exit_client(client_p
, client_p
, client_p
, "Excess parameters to EUID command");
527 /* if nicks erroneous, or too long, kill */
528 if(!clean_nick(parv
[1], 0))
530 ServerStats
->is_kill
++;
531 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
532 "Bad Nick: %s From: %s(via %s)",
533 parv
[1], source_p
->name
, client_p
->name
);
534 sendto_one(client_p
, ":%s KILL %s :%s (Bad Nickname)", me
.id
, parv
[8], me
.name
);
538 if(!clean_username(parv
[5]) || !clean_host(parv
[6]))
540 ServerStats
->is_kill
++;
541 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
542 "Bad user@host: %s@%s From: %s(via %s)",
543 parv
[5], parv
[6], source_p
->name
, client_p
->name
);
544 sendto_one(client_p
, ":%s KILL %s :%s (Bad user@host)", me
.id
, parv
[8], me
.name
);
548 if(!clean_uid(parv
[8]))
550 ServerStats
->is_kill
++;
551 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
552 "Bad UID: %s From: %s(via %s)",
553 parv
[8], source_p
->name
, client_p
->name
);
554 sendto_one(client_p
, ":%s KILL %s :%s (Bad UID)", me
.id
, parv
[8], me
.name
);
558 if(strcmp(parv
[9], "*") && !clean_host(parv
[9]))
560 ServerStats
->is_kill
++;
561 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
562 "Bad realhost: %s From: %s(via %s)",
563 parv
[9], source_p
->name
, client_p
->name
);
564 sendto_one(client_p
, ":%s KILL %s :%s (Bad user@host)", me
.id
, parv
[8], me
.name
);
568 /* check length of clients gecos */
569 if(strlen(parv
[11]) > REALLEN
)
571 char *s
= LOCAL_COPY(parv
[11]);
572 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Long realname from server %s for %s",
578 target_p
= find_named_client(parv
[1]);
582 register_client(client_p
, source_p
, parv
[1], newts
, parc
, parv
);
584 else if(IsUnknown(target_p
))
586 exit_client(NULL
, target_p
, &me
, "Overridden");
587 register_client(client_p
, source_p
, parv
[1], newts
, parc
, parv
);
589 /* we've got a collision! */
591 perform_nick_collides(source_p
, client_p
, target_p
, parc
, parv
,
592 newts
, parv
[1], parv
[8]);
602 ms_save(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
604 struct Client
*target_p
;
606 target_p
= find_id(parv
[1]);
607 if (target_p
== NULL
)
609 if (!IsPerson(target_p
))
610 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
611 "Ignored SAVE message for non-person %s from %s",
612 target_p
->name
, source_p
->name
);
613 else if (IsDigit(target_p
->name
[0]))
614 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
615 "Ignored noop SAVE message for %s from %s",
616 target_p
->name
, source_p
->name
);
617 else if (target_p
->tsinfo
== atol(parv
[2]))
618 save_user(client_p
, source_p
, target_p
);
620 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
621 "Ignored SAVE message for %s from %s",
622 target_p
->name
, source_p
->name
);
628 * input - nickname to check
629 * output - 0 if erroneous, else 1
633 clean_nick(const char *nick
, int loc_client
)
637 /* nicks cant start with a digit or -, and must have a length */
638 if(*nick
== '-' || *nick
== '\0')
641 if(loc_client
&& IsDigit(*nick
))
647 if(!IsNickChar(*nick
))
660 * input - username to check
661 * output - 0 if erroneous, else 0
665 clean_username(const char *username
)
669 for(; *username
; username
++)
673 if(!IsUserChar(*username
))
685 * input - host to check
686 * output - 0 if erroneous, else 0
690 clean_host(const char *host
)
698 if(!IsHostChar(*host
))
709 clean_uid(const char *uid
)
731 set_initial_nick(struct Client
*client_p
, struct Client
*source_p
, char *nick
)
733 char buf
[USERLEN
+ 1];
735 /* This had to be copied here to avoid problems.. */
736 source_p
->tsinfo
= CurrentTime
;
737 if(source_p
->name
[0])
738 del_from_client_hash(source_p
->name
, source_p
);
740 strcpy(source_p
->name
, nick
);
741 add_to_client_hash(nick
, source_p
);
743 /* fd_desc is long enough */
744 comm_note(client_p
->localClient
->fd
, "Nick: %s", nick
);
746 if(source_p
->flags
& FLAGS_SENTUSER
)
748 strlcpy(buf
, source_p
->username
, sizeof(buf
));
750 /* got user, heres nick. */
751 register_local_user(client_p
, source_p
, buf
);
757 change_local_nick(struct Client
*client_p
, struct Client
*source_p
,
758 char *nick
, int dosend
)
760 struct Client
*target_p
;
761 dlink_node
*ptr
, *next_ptr
;
762 struct Channel
*chptr
;
767 chptr
= find_bannickchange_channel(source_p
);
770 sendto_one_numeric(source_p
, ERR_BANNICKCHANGE
,
771 form_str(ERR_BANNICKCHANGE
),
772 nick
, chptr
->chname
);
775 if((source_p
->localClient
->last_nick_change
+ ConfigFileEntry
.max_nick_time
) < CurrentTime
)
776 source_p
->localClient
->number_of_nick_changes
= 0;
778 source_p
->localClient
->last_nick_change
= CurrentTime
;
779 source_p
->localClient
->number_of_nick_changes
++;
781 if(ConfigFileEntry
.anti_nick_flood
&& !IsOper(source_p
) &&
782 source_p
->localClient
->number_of_nick_changes
> ConfigFileEntry
.max_nick_changes
)
784 sendto_one(source_p
, form_str(ERR_NICKTOOFAST
),
785 me
.name
, source_p
->name
, source_p
->name
,
786 nick
, ConfigFileEntry
.max_nick_time
);
791 samenick
= irccmp(source_p
->name
, nick
) ? 0 : 1;
793 /* dont reset TS if theyre just changing case of nick */
796 /* force the TS to increase -- jilles */
797 if (source_p
->tsinfo
>= CurrentTime
)
800 source_p
->tsinfo
= CurrentTime
;
801 monitor_signoff(source_p
);
802 /* we only do bancache for local users -- jilles */
804 invalidate_bancache_user(source_p
);
807 sendto_realops_snomask(SNO_NCHANGE
, L_ALL
,
808 "Nick change: From %s to %s [%s@%s]",
809 source_p
->name
, nick
, source_p
->username
, source_p
->host
);
811 /* send the nick change to the users channels */
812 sendto_common_channels_local(source_p
, ":%s!%s@%s NICK :%s",
813 source_p
->name
, source_p
->username
, source_p
->host
, nick
);
815 /* send the nick change to servers.. */
818 add_history(source_p
, 1);
822 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
, ":%s NICK %s :%ld",
823 use_id(source_p
), nick
, (long) source_p
->tsinfo
);
824 sendto_server(client_p
, NULL
, NOCAPS
, CAP_TS6
, ":%s NICK %s :%ld",
825 source_p
->name
, nick
, (long) source_p
->tsinfo
);
829 /* Finally, add to hash */
830 del_from_client_hash(source_p
->name
, source_p
);
831 strcpy(source_p
->name
, nick
);
832 add_to_client_hash(nick
, source_p
);
835 monitor_signon(source_p
);
837 /* Make sure everyone that has this client on its accept list
838 * loses that reference.
840 /* we used to call del_all_accepts() here, but theres no real reason
841 * to clear a clients own list of accepted clients. So just remove
842 * them from everyone elses list --anfl
844 DLINK_FOREACH_SAFE(ptr
, next_ptr
, source_p
->on_allow_list
.head
)
846 target_p
= ptr
->data
;
848 dlinkFindDestroy(source_p
, &target_p
->localClient
->allow_list
);
849 dlinkDestroy(ptr
, &source_p
->on_allow_list
);
852 /* fd_desc is long enough */
853 comm_note(client_p
->localClient
->fd
, "Nick: %s", nick
);
859 * change_remote_nick()
862 change_remote_nick(struct Client
*client_p
, struct Client
*source_p
,
863 time_t newts
, const char *nick
, int dosend
)
866 int samenick
= irccmp(source_p
->name
, nick
) ? 0 : 1;
868 /* client changing their nick - dont reset ts if its same */
871 source_p
->tsinfo
= newts
? newts
: CurrentTime
;
872 monitor_signoff(source_p
);
875 sendto_common_channels_local(source_p
, ":%s!%s@%s NICK :%s",
876 source_p
->name
, source_p
->username
, source_p
->host
, nick
);
880 add_history(source_p
, 1);
883 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
, ":%s NICK %s :%ld",
884 use_id(source_p
), nick
, (long) source_p
->tsinfo
);
885 sendto_server(client_p
, NULL
, NOCAPS
, CAP_TS6
, ":%s NICK %s :%ld",
886 source_p
->name
, nick
, (long) source_p
->tsinfo
);
890 del_from_client_hash(source_p
->name
, source_p
);
892 /* invalidate nick delay when a remote client uses the nick.. */
893 if((nd
= hash_find_nd(nick
)))
896 strcpy(source_p
->name
, nick
);
897 add_to_client_hash(nick
, source_p
);
900 monitor_signon(source_p
);
902 /* remove all accepts pointing to the client */
903 del_all_accepts(source_p
);
909 perform_nick_collides(struct Client
*source_p
, struct Client
*client_p
,
910 struct Client
*target_p
, int parc
, const char *parv
[],
911 time_t newts
, const char *nick
, const char *uid
)
917 use_save
= ConfigFileEntry
.collision_fnc
&& can_save(target_p
) &&
918 uid
!= NULL
&& can_save(source_p
);
919 action
= use_save
? "saved" : "killed";
921 /* if we dont have a ts, or their TS's are the same, kill both */
922 if(!newts
|| !target_p
->tsinfo
|| (newts
== target_p
->tsinfo
))
924 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
925 "Nick collision on %s(%s <- %s)(both %s)",
926 target_p
->name
, target_p
->from
->name
, client_p
->name
, action
);
930 save_user(&me
, &me
, target_p
);
931 ServerStats
->is_save
++;
932 sendto_one(client_p
, ":%s SAVE %s %ld", me
.id
,
934 register_client(client_p
, source_p
,
935 uid
, SAVE_NICKTS
, parc
, parv
);
939 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
940 form_str(ERR_NICKCOLLISION
), target_p
->name
);
942 /* if the new client being introduced has a UID, we need to
943 * issue a KILL for it..
946 sendto_one(client_p
, ":%s KILL %s :%s (Nick collision (new))",
947 me
.id
, uid
, me
.name
);
949 /* we then need to KILL the old client everywhere */
950 kill_client_serv_butone(NULL
, target_p
, "%s (Nick collision (new))", me
.name
);
951 ServerStats
->is_kill
++;
953 target_p
->flags
|= FLAGS_KILLED
;
954 exit_client(client_p
, target_p
, &me
, "Nick collision (new)");
958 /* the timestamps are different */
961 sameuser
= (target_p
->user
) && !irccmp(target_p
->username
, parv
[5])
962 && !irccmp(target_p
->host
, parv
[6]);
964 if((sameuser
&& newts
< target_p
->tsinfo
) ||
965 (!sameuser
&& newts
> target_p
->tsinfo
))
967 /* if we have a UID, then we need to issue a KILL,
968 * otherwise we do nothing and hope that the other
969 * client will collide it..
973 sendto_one(client_p
, ":%s SAVE %s %ld", me
.id
,
975 register_client(client_p
, source_p
,
976 uid
, SAVE_NICKTS
, parc
, parv
);
980 ":%s KILL %s :%s (Nick collision (new))",
981 me
.id
, uid
, me
.name
);
987 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
988 "Nick collision on %s(%s <- %s)(older %s)",
989 target_p
->name
, target_p
->from
->name
,
990 client_p
->name
, action
);
992 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
993 "Nick collision on %s(%s <- %s)(newer %s)",
994 target_p
->name
, target_p
->from
->name
,
995 client_p
->name
, action
);
999 ServerStats
->is_save
++;
1000 save_user(&me
, &me
, target_p
);
1004 ServerStats
->is_kill
++;
1005 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
1006 form_str(ERR_NICKCOLLISION
), target_p
->name
);
1008 /* now we just need to kill the existing client */
1009 kill_client_serv_butone(client_p
, target_p
,
1010 "%s (Nick collision (new))", me
.name
);
1012 target_p
->flags
|= FLAGS_KILLED
;
1013 (void) exit_client(client_p
, target_p
, &me
, "Nick collision");
1016 register_client(client_p
, parc
>= 10 ? source_p
: NULL
,
1017 nick
, newts
, parc
, parv
);
1026 perform_nickchange_collides(struct Client
*source_p
, struct Client
*client_p
,
1027 struct Client
*target_p
, int parc
,
1028 const char *parv
[], time_t newts
, const char *nick
)
1034 use_save
= ConfigFileEntry
.collision_fnc
&& can_save(target_p
) &&
1036 action
= use_save
? "saved" : "killed";
1038 /* its a client changing nick and causing a collide */
1039 if(!newts
|| !target_p
->tsinfo
|| (newts
== target_p
->tsinfo
) || !source_p
->user
)
1041 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1042 "Nick change collision from %s to %s(%s <- %s)(both %s)",
1043 source_p
->name
, target_p
->name
, target_p
->from
->name
,
1044 client_p
->name
, action
);
1048 ServerStats
->is_save
+= 2;
1049 save_user(&me
, &me
, target_p
);
1050 sendto_one(client_p
, ":%s SAVE %s %ld", me
.id
,
1051 source_p
->id
, (long)newts
);
1052 /* don't send a redundant nick change */
1053 if (!IsDigit(source_p
->name
[0]))
1054 change_remote_nick(client_p
, source_p
, SAVE_NICKTS
, source_p
->id
, 1);
1058 ServerStats
->is_kill
++;
1059 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
1060 form_str(ERR_NICKCOLLISION
), target_p
->name
);
1062 kill_client_serv_butone(NULL
, source_p
, "%s (Nick change collision)", me
.name
);
1064 ServerStats
->is_kill
++;
1066 kill_client_serv_butone(NULL
, target_p
, "%s (Nick change collision)", me
.name
);
1068 target_p
->flags
|= FLAGS_KILLED
;
1069 exit_client(NULL
, target_p
, &me
, "Nick collision(new)");
1070 source_p
->flags
|= FLAGS_KILLED
;
1071 exit_client(client_p
, source_p
, &me
, "Nick collision(old)");
1077 sameuser
= !irccmp(target_p
->username
, source_p
->username
) &&
1078 !irccmp(target_p
->host
, source_p
->host
);
1080 if((sameuser
&& newts
< target_p
->tsinfo
) ||
1081 (!sameuser
&& newts
> target_p
->tsinfo
))
1084 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1085 "Nick change collision from %s to %s(%s <- %s)(older %s)",
1086 source_p
->name
, target_p
->name
,
1087 target_p
->from
->name
, client_p
->name
, action
);
1089 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1090 "Nick change collision from %s to %s(%s <- %s)(newer %s)",
1091 source_p
->name
, target_p
->name
,
1092 target_p
->from
->name
, client_p
->name
, action
);
1096 ServerStats
->is_save
++;
1097 /* can't broadcast a SAVE because the
1098 * nickchange has happened at client_p
1099 * but not in other directions -- jilles */
1100 sendto_one(client_p
, ":%s SAVE %s %ld", me
.id
,
1101 source_p
->id
, (long)newts
);
1102 /* send a :<id> NICK <id> <ts> (!) */
1103 if (!IsDigit(source_p
->name
[0]))
1104 change_remote_nick(client_p
, source_p
, SAVE_NICKTS
, source_p
->id
, 1);
1108 ServerStats
->is_kill
++;
1110 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
1111 form_str(ERR_NICKCOLLISION
), target_p
->name
);
1113 /* kill the client issuing the nickchange */
1114 kill_client_serv_butone(client_p
, source_p
,
1115 "%s (Nick change collision)", me
.name
);
1117 source_p
->flags
|= FLAGS_KILLED
;
1120 exit_client(client_p
, source_p
, &me
, "Nick collision(old)");
1122 exit_client(client_p
, source_p
, &me
, "Nick collision(new)");
1129 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1130 "Nick collision on %s(%s <- %s)(older %s)",
1131 target_p
->name
, target_p
->from
->name
,
1132 client_p
->name
, action
);
1134 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1135 "Nick collision on %s(%s <- %s)(newer %s)",
1136 target_p
->name
, target_p
->from
->name
,
1137 client_p
->name
, action
);
1141 ServerStats
->is_save
++;
1142 save_user(&me
, &me
, target_p
);
1146 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
1147 form_str(ERR_NICKCOLLISION
), target_p
->name
);
1149 /* kill the client who existed before hand */
1150 kill_client_serv_butone(client_p
, target_p
, "%s (Nick collision)", me
.name
);
1152 ServerStats
->is_kill
++;
1154 target_p
->flags
|= FLAGS_KILLED
;
1155 (void) exit_client(client_p
, target_p
, &me
, "Nick collision");
1160 change_remote_nick(client_p
, source_p
, newts
, nick
, 1);
1166 register_client(struct Client
*client_p
, struct Client
*server
,
1167 const char *nick
, time_t newts
, int parc
, const char *parv
[])
1169 struct Client
*source_p
;
1171 struct nd_entry
*nd
;
1175 source_p
= make_client(client_p
);
1176 user
= make_user(source_p
);
1177 dlinkAddTail(source_p
, &source_p
->node
, &global_client_list
);
1179 source_p
->hopcount
= atoi(parv
[2]);
1180 source_p
->tsinfo
= newts
;
1182 strcpy(source_p
->name
, nick
);
1183 strlcpy(source_p
->username
, parv
[5], sizeof(source_p
->username
));
1184 strlcpy(source_p
->host
, parv
[6], sizeof(source_p
->host
));
1185 strlcpy(source_p
->orighost
, source_p
->host
, sizeof(source_p
->orighost
));
1189 strlcpy(source_p
->info
, parv
[11], sizeof(source_p
->info
));
1190 strlcpy(source_p
->sockhost
, parv
[7], sizeof(source_p
->sockhost
));
1191 strlcpy(source_p
->id
, parv
[8], sizeof(source_p
->id
));
1192 add_to_id_hash(source_p
->id
, source_p
);
1193 if (strcmp(parv
[9], "*"))
1195 strlcpy(source_p
->orighost
, parv
[9], sizeof(source_p
->orighost
));
1196 if (irccmp(source_p
->host
, source_p
->orighost
))
1197 SetDynSpoof(source_p
);
1199 if (strcmp(parv
[10], "*"))
1200 strlcpy(source_p
->user
->suser
, parv
[10], sizeof(source_p
->user
->suser
));
1204 strlcpy(source_p
->info
, parv
[9], sizeof(source_p
->info
));
1205 strlcpy(source_p
->sockhost
, parv
[7], sizeof(source_p
->sockhost
));
1206 strlcpy(source_p
->id
, parv
[8], sizeof(source_p
->id
));
1207 add_to_id_hash(source_p
->id
, source_p
);
1211 strlcpy(source_p
->info
, parv
[8], sizeof(source_p
->info
));
1214 /* remove any nd entries for this nick */
1215 if((nd
= hash_find_nd(nick
)))
1218 add_to_client_hash(nick
, source_p
);
1219 add_to_hostname_hash(source_p
->orighost
, source_p
);
1220 monitor_signon(source_p
);
1225 flag
= user_modes
[(unsigned char) *m
];
1227 if(flag
& UMODE_SERVICE
)
1232 DLINK_FOREACH(ptr
, service_list
.head
)
1234 if(!irccmp((const char *) ptr
->data
, server
->name
))
1248 /* increment +i count if theyre invis */
1249 if(!(source_p
->umodes
& UMODE_INVISIBLE
) && (flag
& UMODE_INVISIBLE
))
1252 /* increment opered count if theyre opered */
1253 if(!(source_p
->umodes
& UMODE_OPER
) && (flag
& UMODE_OPER
))
1256 source_p
->umodes
|= flag
;
1260 if(IsOper(source_p
) && !IsService(source_p
))
1261 dlinkAddAlloc(source_p
, &oper_list
);
1263 SetRemoteClient(source_p
);
1265 if(++Count
.total
> Count
.max_tot
)
1266 Count
.max_tot
= Count
.total
;
1270 if((source_p
->servptr
= find_server(NULL
, server
->name
)) == NULL
)
1272 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1273 "Ghost killed: %s on invalid server %s",
1274 source_p
->name
, server
->name
);
1275 kill_client(client_p
, source_p
, "%s (Server doesn't exist)", me
.name
);
1276 source_p
->flags
|= FLAGS_KILLED
;
1277 return exit_client(NULL
, source_p
, &me
, "Ghosted Client");
1281 source_p
->servptr
= server
;
1283 dlinkAdd(source_p
, &source_p
->lnode
, &source_p
->servptr
->serv
->users
);
1285 /* fake direction */
1286 if(source_p
->servptr
->from
!= source_p
->from
)
1288 struct Client
*target_p
= source_p
->servptr
->from
;
1290 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
1291 "Bad User [%s] :%s USER %s@%s %s, != %s[%s]",
1292 client_p
->name
, source_p
->name
,
1293 source_p
->username
, source_p
->host
,
1294 server
->name
, target_p
->name
, target_p
->from
->name
);
1295 kill_client(client_p
, source_p
,
1296 "%s (NICK from wrong direction (%s != %s))",
1297 me
.name
, server
->name
, target_p
->from
->name
);
1298 source_p
->flags
|= FLAGS_KILLED
;
1299 return exit_client(source_p
, source_p
, &me
, "USER server wrong direction");
1302 call_hook(h_new_remote_user
, source_p
);
1304 return (introduce_client(client_p
, source_p
, user
, nick
, parc
== 12));
1307 /* Check if we can do SAVE. target_p can be a client to save or a
1308 * server introducing a client -- jilles */
1310 can_save(struct Client
*target_p
)
1312 struct Client
*serv_p
;
1314 if (MyClient(target_p
))
1316 if (!has_id(target_p
))
1318 serv_p
= IsServer(target_p
) ? target_p
: target_p
->servptr
;
1319 while (serv_p
!= NULL
&& serv_p
!= &me
)
1321 if (!(serv_p
->serv
->caps
& CAP_SAVE
))
1323 serv_p
= serv_p
->servptr
;
1325 return serv_p
== &me
;
1329 save_user(struct Client
*client_p
, struct Client
*source_p
,
1330 struct Client
*target_p
)
1332 if (!MyConnect(target_p
) && (!has_id(target_p
) || !IsCapable(target_p
->from
, CAP_SAVE
)))
1334 /* This shouldn't happen */
1335 /* Note we only need SAVE support in this direction */
1336 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1337 "Killed %s!%s@%s for nick collision detected by %s (%s does not support SAVE)",
1338 target_p
->name
, target_p
->username
, target_p
->host
, source_p
->name
, target_p
->from
->name
);
1339 kill_client_serv_butone(NULL
, target_p
, "%s (Nick collision (no SAVE support))", me
.name
);
1340 ServerStats
->is_kill
++;
1342 target_p
->flags
|= FLAGS_KILLED
;
1343 (void) exit_client(NULL
, target_p
, &me
, "Nick collision (no SAVE support)");
1346 sendto_server(client_p
, NULL
, CAP_SAVE
|CAP_TS6
, NOCAPS
, ":%s SAVE %s %ld",
1347 source_p
->id
, target_p
->id
, (long)target_p
->tsinfo
);
1348 sendto_server(client_p
, NULL
, CAP_TS6
, CAP_SAVE
, ":%s NICK %s :%ld",
1349 target_p
->id
, target_p
->id
, (long)SAVE_NICKTS
);
1350 sendto_server(client_p
, NULL
, NOCAPS
, CAP_TS6
, ":%s NICK %s :%ld",
1351 target_p
->name
, target_p
->id
, (long)SAVE_NICKTS
);
1352 if (!IsMe(client_p
))
1353 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
1354 "Received SAVE message for %s from %s",
1355 target_p
->name
, source_p
->name
);
1356 if (MyClient(target_p
))
1358 sendto_one_numeric(target_p
, RPL_SAVENICK
,
1359 form_str(RPL_SAVENICK
), target_p
->id
);
1360 change_local_nick(target_p
, target_p
, target_p
->id
, 0);
1361 target_p
->tsinfo
= SAVE_NICKTS
;
1364 change_remote_nick(target_p
, target_p
, SAVE_NICKTS
, target_p
->id
, 0);