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(irc_dictionary_find(nd_dict
, 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(irc_dictionary_find(nd_dict
, 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];
734 char note
[NICKLEN
+ 10];
736 /* This had to be copied here to avoid problems.. */
737 source_p
->tsinfo
= CurrentTime
;
738 if(source_p
->name
[0])
739 del_from_client_hash(source_p
->name
, source_p
);
741 strcpy(source_p
->name
, nick
);
742 add_to_client_hash(nick
, source_p
);
744 rb_snprintf(note
, sizeof(note
), "Nick: %s", nick
);
745 rb_note(client_p
->localClient
->F
, note
);
747 if(source_p
->flags
& FLAGS_SENTUSER
)
749 strlcpy(buf
, source_p
->username
, sizeof(buf
));
751 /* got user, heres nick. */
752 register_local_user(client_p
, source_p
, buf
);
758 change_local_nick(struct Client
*client_p
, struct Client
*source_p
,
759 char *nick
, int dosend
)
761 struct Client
*target_p
;
762 rb_dlink_node
*ptr
, *next_ptr
;
763 struct Channel
*chptr
;
764 char note
[NICKLEN
+ 10];
769 chptr
= find_bannickchange_channel(source_p
);
772 sendto_one_numeric(source_p
, ERR_BANNICKCHANGE
,
773 form_str(ERR_BANNICKCHANGE
),
774 nick
, chptr
->chname
);
777 if((source_p
->localClient
->last_nick_change
+ ConfigFileEntry
.max_nick_time
) < CurrentTime
)
778 source_p
->localClient
->number_of_nick_changes
= 0;
780 source_p
->localClient
->last_nick_change
= CurrentTime
;
781 source_p
->localClient
->number_of_nick_changes
++;
783 if(ConfigFileEntry
.anti_nick_flood
&& !IsOper(source_p
) &&
784 source_p
->localClient
->number_of_nick_changes
> ConfigFileEntry
.max_nick_changes
)
786 sendto_one(source_p
, form_str(ERR_NICKTOOFAST
),
787 me
.name
, source_p
->name
, source_p
->name
,
788 nick
, ConfigFileEntry
.max_nick_time
);
793 samenick
= irccmp(source_p
->name
, nick
) ? 0 : 1;
795 /* dont reset TS if theyre just changing case of nick */
798 /* force the TS to increase -- jilles */
799 if (source_p
->tsinfo
>= CurrentTime
)
802 source_p
->tsinfo
= CurrentTime
;
803 monitor_signoff(source_p
);
804 /* we only do bancache for local users -- jilles */
806 invalidate_bancache_user(source_p
);
809 sendto_realops_snomask(SNO_NCHANGE
, L_ALL
,
810 "Nick change: From %s to %s [%s@%s]",
811 source_p
->name
, nick
, source_p
->username
, source_p
->host
);
813 /* send the nick change to the users channels */
814 sendto_common_channels_local(source_p
, ":%s!%s@%s NICK :%s",
815 source_p
->name
, source_p
->username
, source_p
->host
, nick
);
817 /* send the nick change to servers.. */
820 add_history(source_p
, 1);
824 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
, ":%s NICK %s :%ld",
825 use_id(source_p
), nick
, (long) source_p
->tsinfo
);
826 sendto_server(client_p
, NULL
, NOCAPS
, CAP_TS6
, ":%s NICK %s :%ld",
827 source_p
->name
, nick
, (long) source_p
->tsinfo
);
831 /* Finally, add to hash */
832 del_from_client_hash(source_p
->name
, source_p
);
833 strcpy(source_p
->name
, nick
);
834 add_to_client_hash(nick
, source_p
);
837 monitor_signon(source_p
);
839 /* Make sure everyone that has this client on its accept list
840 * loses that reference.
842 /* we used to call del_all_accepts() here, but theres no real reason
843 * to clear a clients own list of accepted clients. So just remove
844 * them from everyone elses list --anfl
846 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, source_p
->on_allow_list
.head
)
848 target_p
= ptr
->data
;
850 rb_dlinkFindDestroy(source_p
, &target_p
->localClient
->allow_list
);
851 rb_dlinkDestroy(ptr
, &source_p
->on_allow_list
);
854 rb_snprintf(note
, sizeof(note
), "Nick: %s", nick
);
855 rb_note(client_p
->localClient
->F
, note
);
861 * change_remote_nick()
864 change_remote_nick(struct Client
*client_p
, struct Client
*source_p
,
865 time_t newts
, const char *nick
, int dosend
)
868 int samenick
= irccmp(source_p
->name
, nick
) ? 0 : 1;
870 /* client changing their nick - dont reset ts if its same */
873 source_p
->tsinfo
= newts
? newts
: CurrentTime
;
874 monitor_signoff(source_p
);
877 sendto_common_channels_local(source_p
, ":%s!%s@%s NICK :%s",
878 source_p
->name
, source_p
->username
, source_p
->host
, nick
);
882 add_history(source_p
, 1);
885 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
, ":%s NICK %s :%ld",
886 use_id(source_p
), nick
, (long) source_p
->tsinfo
);
887 sendto_server(client_p
, NULL
, NOCAPS
, CAP_TS6
, ":%s NICK %s :%ld",
888 source_p
->name
, nick
, (long) source_p
->tsinfo
);
892 del_from_client_hash(source_p
->name
, source_p
);
894 /* invalidate nick delay when a remote client uses the nick.. */
895 if((nd
= irc_dictionary_retrieve(nd_dict
, nick
)))
898 strcpy(source_p
->name
, nick
);
899 add_to_client_hash(nick
, source_p
);
902 monitor_signon(source_p
);
904 /* remove all accepts pointing to the client */
905 del_all_accepts(source_p
);
911 perform_nick_collides(struct Client
*source_p
, struct Client
*client_p
,
912 struct Client
*target_p
, int parc
, const char *parv
[],
913 time_t newts
, const char *nick
, const char *uid
)
919 use_save
= ConfigFileEntry
.collision_fnc
&& can_save(target_p
) &&
920 uid
!= NULL
&& can_save(source_p
);
921 action
= use_save
? "saved" : "killed";
923 /* if we dont have a ts, or their TS's are the same, kill both */
924 if(!newts
|| !target_p
->tsinfo
|| (newts
== target_p
->tsinfo
))
926 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
927 "Nick collision on %s(%s <- %s)(both %s)",
928 target_p
->name
, target_p
->from
->name
, client_p
->name
, action
);
932 save_user(&me
, &me
, target_p
);
933 ServerStats
->is_save
++;
934 sendto_one(client_p
, ":%s SAVE %s %ld", me
.id
,
936 register_client(client_p
, source_p
,
937 uid
, SAVE_NICKTS
, parc
, parv
);
941 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
942 form_str(ERR_NICKCOLLISION
), target_p
->name
);
944 /* if the new client being introduced has a UID, we need to
945 * issue a KILL for it..
948 sendto_one(client_p
, ":%s KILL %s :%s (Nick collision (new))",
949 me
.id
, uid
, me
.name
);
951 /* we then need to KILL the old client everywhere */
952 kill_client_serv_butone(NULL
, target_p
, "%s (Nick collision (new))", me
.name
);
953 ServerStats
->is_kill
++;
955 target_p
->flags
|= FLAGS_KILLED
;
956 exit_client(client_p
, target_p
, &me
, "Nick collision (new)");
960 /* the timestamps are different */
963 sameuser
= (target_p
->user
) && !irccmp(target_p
->username
, parv
[5])
964 && !irccmp(target_p
->host
, parv
[6]);
966 if((sameuser
&& newts
< target_p
->tsinfo
) ||
967 (!sameuser
&& newts
> target_p
->tsinfo
))
969 /* if we have a UID, then we need to issue a KILL,
970 * otherwise we do nothing and hope that the other
971 * client will collide it..
975 sendto_one(client_p
, ":%s SAVE %s %ld", me
.id
,
977 register_client(client_p
, source_p
,
978 uid
, SAVE_NICKTS
, parc
, parv
);
982 ":%s KILL %s :%s (Nick collision (new))",
983 me
.id
, uid
, me
.name
);
989 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
990 "Nick collision on %s(%s <- %s)(older %s)",
991 target_p
->name
, target_p
->from
->name
,
992 client_p
->name
, action
);
994 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
995 "Nick collision on %s(%s <- %s)(newer %s)",
996 target_p
->name
, target_p
->from
->name
,
997 client_p
->name
, action
);
1001 ServerStats
->is_save
++;
1002 save_user(&me
, &me
, target_p
);
1006 ServerStats
->is_kill
++;
1007 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
1008 form_str(ERR_NICKCOLLISION
), target_p
->name
);
1010 /* now we just need to kill the existing client */
1011 kill_client_serv_butone(client_p
, target_p
,
1012 "%s (Nick collision (new))", me
.name
);
1014 target_p
->flags
|= FLAGS_KILLED
;
1015 (void) exit_client(client_p
, target_p
, &me
, "Nick collision");
1018 register_client(client_p
, parc
>= 10 ? source_p
: NULL
,
1019 nick
, newts
, parc
, parv
);
1028 perform_nickchange_collides(struct Client
*source_p
, struct Client
*client_p
,
1029 struct Client
*target_p
, int parc
,
1030 const char *parv
[], time_t newts
, const char *nick
)
1036 use_save
= ConfigFileEntry
.collision_fnc
&& can_save(target_p
) &&
1038 action
= use_save
? "saved" : "killed";
1040 /* its a client changing nick and causing a collide */
1041 if(!newts
|| !target_p
->tsinfo
|| (newts
== target_p
->tsinfo
) || !source_p
->user
)
1043 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1044 "Nick change collision from %s to %s(%s <- %s)(both %s)",
1045 source_p
->name
, target_p
->name
, target_p
->from
->name
,
1046 client_p
->name
, action
);
1050 ServerStats
->is_save
+= 2;
1051 save_user(&me
, &me
, target_p
);
1052 sendto_one(client_p
, ":%s SAVE %s %ld", me
.id
,
1053 source_p
->id
, (long)newts
);
1054 /* don't send a redundant nick change */
1055 if (!IsDigit(source_p
->name
[0]))
1056 change_remote_nick(client_p
, source_p
, SAVE_NICKTS
, source_p
->id
, 1);
1060 ServerStats
->is_kill
++;
1061 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
1062 form_str(ERR_NICKCOLLISION
), target_p
->name
);
1064 kill_client_serv_butone(NULL
, source_p
, "%s (Nick change collision)", me
.name
);
1066 ServerStats
->is_kill
++;
1068 kill_client_serv_butone(NULL
, target_p
, "%s (Nick change collision)", me
.name
);
1070 target_p
->flags
|= FLAGS_KILLED
;
1071 exit_client(NULL
, target_p
, &me
, "Nick collision(new)");
1072 source_p
->flags
|= FLAGS_KILLED
;
1073 exit_client(client_p
, source_p
, &me
, "Nick collision(old)");
1079 sameuser
= !irccmp(target_p
->username
, source_p
->username
) &&
1080 !irccmp(target_p
->host
, source_p
->host
);
1082 if((sameuser
&& newts
< target_p
->tsinfo
) ||
1083 (!sameuser
&& newts
> target_p
->tsinfo
))
1086 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1087 "Nick change collision from %s to %s(%s <- %s)(older %s)",
1088 source_p
->name
, target_p
->name
,
1089 target_p
->from
->name
, client_p
->name
, action
);
1091 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1092 "Nick change collision from %s to %s(%s <- %s)(newer %s)",
1093 source_p
->name
, target_p
->name
,
1094 target_p
->from
->name
, client_p
->name
, action
);
1098 ServerStats
->is_save
++;
1099 /* can't broadcast a SAVE because the
1100 * nickchange has happened at client_p
1101 * but not in other directions -- jilles */
1102 sendto_one(client_p
, ":%s SAVE %s %ld", me
.id
,
1103 source_p
->id
, (long)newts
);
1104 /* send a :<id> NICK <id> <ts> (!) */
1105 if (!IsDigit(source_p
->name
[0]))
1106 change_remote_nick(client_p
, source_p
, SAVE_NICKTS
, source_p
->id
, 1);
1110 ServerStats
->is_kill
++;
1112 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
1113 form_str(ERR_NICKCOLLISION
), target_p
->name
);
1115 /* kill the client issuing the nickchange */
1116 kill_client_serv_butone(client_p
, source_p
,
1117 "%s (Nick change collision)", me
.name
);
1119 source_p
->flags
|= FLAGS_KILLED
;
1122 exit_client(client_p
, source_p
, &me
, "Nick collision(old)");
1124 exit_client(client_p
, source_p
, &me
, "Nick collision(new)");
1131 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1132 "Nick collision on %s(%s <- %s)(older %s)",
1133 target_p
->name
, target_p
->from
->name
,
1134 client_p
->name
, action
);
1136 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1137 "Nick collision on %s(%s <- %s)(newer %s)",
1138 target_p
->name
, target_p
->from
->name
,
1139 client_p
->name
, action
);
1143 ServerStats
->is_save
++;
1144 save_user(&me
, &me
, target_p
);
1148 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
1149 form_str(ERR_NICKCOLLISION
), target_p
->name
);
1151 /* kill the client who existed before hand */
1152 kill_client_serv_butone(client_p
, target_p
, "%s (Nick collision)", me
.name
);
1154 ServerStats
->is_kill
++;
1156 target_p
->flags
|= FLAGS_KILLED
;
1157 (void) exit_client(client_p
, target_p
, &me
, "Nick collision");
1162 change_remote_nick(client_p
, source_p
, newts
, nick
, 1);
1168 register_client(struct Client
*client_p
, struct Client
*server
,
1169 const char *nick
, time_t newts
, int parc
, const char *parv
[])
1171 struct Client
*source_p
;
1173 struct nd_entry
*nd
;
1179 if((server
= find_server(NULL
, parv
[7])) == NULL
)
1181 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1182 "Ghost killed: %s on invalid server %s",
1184 sendto_one(client_p
, ":%s KILL %s :%s (Server doesn't exist)",
1185 get_id(&me
, client_p
), nick
, me
.name
);
1190 source_p
= make_client(client_p
);
1191 user
= make_user(source_p
);
1192 rb_dlinkAddTail(source_p
, &source_p
->node
, &global_client_list
);
1194 source_p
->hopcount
= atoi(parv
[2]);
1195 source_p
->tsinfo
= newts
;
1197 strcpy(source_p
->name
, nick
);
1198 strlcpy(source_p
->username
, parv
[5], sizeof(source_p
->username
));
1199 strlcpy(source_p
->host
, parv
[6], sizeof(source_p
->host
));
1200 strlcpy(source_p
->orighost
, source_p
->host
, sizeof(source_p
->orighost
));
1204 strlcpy(source_p
->info
, parv
[11], 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
);
1208 if (strcmp(parv
[9], "*"))
1210 strlcpy(source_p
->orighost
, parv
[9], sizeof(source_p
->orighost
));
1211 if (irccmp(source_p
->host
, source_p
->orighost
))
1212 SetDynSpoof(source_p
);
1214 if (strcmp(parv
[10], "*"))
1215 strlcpy(source_p
->user
->suser
, parv
[10], sizeof(source_p
->user
->suser
));
1219 strlcpy(source_p
->info
, parv
[9], sizeof(source_p
->info
));
1220 strlcpy(source_p
->sockhost
, parv
[7], sizeof(source_p
->sockhost
));
1221 strlcpy(source_p
->id
, parv
[8], sizeof(source_p
->id
));
1222 add_to_id_hash(source_p
->id
, source_p
);
1226 strlcpy(source_p
->info
, parv
[8], sizeof(source_p
->info
));
1229 /* remove any nd entries for this nick */
1230 if((nd
= irc_dictionary_retrieve(nd_dict
, nick
)))
1233 add_to_client_hash(nick
, source_p
);
1234 add_to_hostname_hash(source_p
->orighost
, source_p
);
1235 monitor_signon(source_p
);
1240 flag
= user_modes
[(unsigned char) *m
];
1242 if(flag
& UMODE_SERVICE
)
1247 RB_DLINK_FOREACH(ptr
, service_list
.head
)
1249 if(!irccmp((const char *) ptr
->data
, server
->name
))
1263 /* increment +i count if theyre invis */
1264 if(!(source_p
->umodes
& UMODE_INVISIBLE
) && (flag
& UMODE_INVISIBLE
))
1267 /* increment opered count if theyre opered */
1268 if(!(source_p
->umodes
& UMODE_OPER
) && (flag
& UMODE_OPER
))
1271 source_p
->umodes
|= flag
;
1275 if(IsOper(source_p
) && !IsService(source_p
))
1276 rb_dlinkAddAlloc(source_p
, &oper_list
);
1278 SetRemoteClient(source_p
);
1280 if(++Count
.total
> Count
.max_tot
)
1281 Count
.max_tot
= Count
.total
;
1283 source_p
->servptr
= server
;
1285 rb_dlinkAdd(source_p
, &source_p
->lnode
, &source_p
->servptr
->serv
->users
);
1287 /* fake direction */
1288 if(source_p
->servptr
->from
!= source_p
->from
)
1290 struct Client
*target_p
= source_p
->servptr
->from
;
1292 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
1293 "Bad User [%s] :%s USER %s@%s %s, != %s[%s]",
1294 client_p
->name
, source_p
->name
,
1295 source_p
->username
, source_p
->host
,
1296 server
->name
, target_p
->name
, target_p
->from
->name
);
1297 kill_client(client_p
, source_p
,
1298 "%s (NICK from wrong direction (%s != %s))",
1299 me
.name
, server
->name
, target_p
->from
->name
);
1300 source_p
->flags
|= FLAGS_KILLED
;
1301 return exit_client(source_p
, source_p
, &me
, "USER server wrong direction");
1304 call_hook(h_new_remote_user
, source_p
);
1306 return (introduce_client(client_p
, source_p
, user
, nick
, parc
== 12));
1309 /* Check if we can do SAVE. target_p can be a client to save or a
1310 * server introducing a client -- jilles */
1312 can_save(struct Client
*target_p
)
1314 struct Client
*serv_p
;
1316 if (MyClient(target_p
))
1318 if (!has_id(target_p
))
1320 serv_p
= IsServer(target_p
) ? target_p
: target_p
->servptr
;
1321 while (serv_p
!= NULL
&& serv_p
!= &me
)
1323 if (!(serv_p
->serv
->caps
& CAP_SAVE
))
1325 serv_p
= serv_p
->servptr
;
1327 return serv_p
== &me
;
1331 save_user(struct Client
*client_p
, struct Client
*source_p
,
1332 struct Client
*target_p
)
1334 if (!MyConnect(target_p
) && (!has_id(target_p
) || !IsCapable(target_p
->from
, CAP_SAVE
)))
1336 /* This shouldn't happen */
1337 /* Note we only need SAVE support in this direction */
1338 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1339 "Killed %s!%s@%s for nick collision detected by %s (%s does not support SAVE)",
1340 target_p
->name
, target_p
->username
, target_p
->host
, source_p
->name
, target_p
->from
->name
);
1341 kill_client_serv_butone(NULL
, target_p
, "%s (Nick collision (no SAVE support))", me
.name
);
1342 ServerStats
->is_kill
++;
1344 target_p
->flags
|= FLAGS_KILLED
;
1345 (void) exit_client(NULL
, target_p
, &me
, "Nick collision (no SAVE support)");
1348 sendto_server(client_p
, NULL
, CAP_SAVE
|CAP_TS6
, NOCAPS
, ":%s SAVE %s %ld",
1349 source_p
->id
, target_p
->id
, (long)target_p
->tsinfo
);
1350 sendto_server(client_p
, NULL
, CAP_TS6
, CAP_SAVE
, ":%s NICK %s :%ld",
1351 target_p
->id
, target_p
->id
, (long)SAVE_NICKTS
);
1352 sendto_server(client_p
, NULL
, NOCAPS
, CAP_TS6
, ":%s NICK %s :%ld",
1353 target_p
->name
, target_p
->id
, (long)SAVE_NICKTS
);
1354 if (!IsMe(client_p
))
1355 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
1356 "Received SAVE message for %s from %s",
1357 target_p
->name
, source_p
->name
);
1358 if (MyClient(target_p
))
1360 sendto_one_numeric(target_p
, RPL_SAVENICK
,
1361 form_str(RPL_SAVENICK
), target_p
->id
);
1362 change_local_nick(target_p
, target_p
, target_p
->id
, 0);
1363 target_p
->tsinfo
= SAVE_NICKTS
;
1366 change_remote_nick(target_p
, target_p
, SAVE_NICKTS
, target_p
->id
, 0);