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 $
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
*);
67 static void bad_nickname(struct Client
*, const char *);
69 struct Message nick_msgtab
= {
70 "NICK", 0, 0, 0, MFLG_SLOW
,
71 {{mr_nick
, 0}, {m_nick
, 0}, {mc_nick
, 3}, {ms_nick
, 0}, mg_ignore
, {m_nick
, 0}}
73 struct Message uid_msgtab
= {
74 "UID", 0, 0, 0, MFLG_SLOW
,
75 {mg_ignore
, mg_ignore
, mg_ignore
, {ms_uid
, 9}, mg_ignore
, mg_ignore
}
77 struct Message euid_msgtab
= {
78 "EUID", 0, 0, 0, MFLG_SLOW
,
79 {mg_ignore
, mg_ignore
, mg_ignore
, {ms_euid
, 12}, mg_ignore
, mg_ignore
}
81 struct Message save_msgtab
= {
82 "SAVE", 0, 0, 0, MFLG_SLOW
,
83 {mg_ignore
, mg_ignore
, mg_ignore
, {ms_save
, 3}, mg_ignore
, mg_ignore
}
86 mapi_clist_av1 nick_clist
[] = { &nick_msgtab
, &uid_msgtab
, &euid_msgtab
,
89 DECLARE_MODULE_AV1(nick
, NULL
, NULL
, nick_clist
, NULL
, NULL
, "$Revision: 3518 $");
91 static int change_remote_nick(struct Client
*, struct Client
*, time_t,
94 static int clean_nick(const char *, int loc_client
);
95 static int clean_username(const char *);
96 static int clean_host(const char *);
97 static int clean_uid(const char *uid
);
99 static void set_initial_nick(struct Client
*client_p
, struct Client
*source_p
, char *nick
);
100 static void change_local_nick(struct Client
*client_p
, struct Client
*source_p
, char *nick
, int);
101 static int register_client(struct Client
*client_p
, struct Client
*server
,
102 const char *nick
, time_t newts
, int parc
, const char *parv
[]);
104 static int perform_nick_collides(struct Client
*, struct Client
*,
105 struct Client
*, int, const char **,
106 time_t, const char *, const char *);
107 static int perform_nickchange_collides(struct Client
*, struct Client
*,
108 struct Client
*, int, const char **, time_t, const char *);
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 rb_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(source_p
->name
) ? "*" : source_p
->name
, 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
);
180 m_nick(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
182 struct Client
*target_p
;
186 if(parc
< 2 || EmptyString(parv
[1]) || (parv
[1][0] == '~'))
188 sendto_one(source_p
, form_str(ERR_NONICKNAMEGIVEN
), me
.name
, source_p
->name
);
192 /* due to the scandinavian origins, (~ being uppercase of ^) and ~
193 * being disallowed as a nick char, we need to chop the first ~
194 * instead of just erroring.
196 if((s
= strchr(parv
[1], '~')))
199 /* mark end of grace period, to prevent nickflooding */
200 if(!IsFloodDone(source_p
))
201 flood_endgrace(source_p
);
203 /* terminate nick to NICKLEN, we dont want clean_nick() to error! */
204 rb_strlcpy(nick
, parv
[1], sizeof(nick
));
206 /* check the nickname is ok */
207 if(!clean_nick(nick
, 1))
209 sendto_one(source_p
, form_str(ERR_ERRONEUSNICKNAME
), me
.name
, source_p
->name
, nick
);
213 if(!IsExemptResv(source_p
) && find_nick_resv(nick
))
215 sendto_one(source_p
, form_str(ERR_ERRONEUSNICKNAME
), me
.name
, source_p
->name
, nick
);
219 if(irc_dictionary_find(nd_dict
, nick
))
221 sendto_one(source_p
, form_str(ERR_UNAVAILRESOURCE
),
222 me
.name
, EmptyString(source_p
->name
) ? "*" : source_p
->name
, nick
);
226 if((target_p
= find_named_client(nick
)))
228 /* If(target_p == source_p) the client is changing nicks between
229 * equivalent nicknames ie: [nick] -> {nick}
231 if(target_p
== source_p
)
233 /* check the nick isnt exactly the same */
234 if(strcmp(target_p
->name
, nick
))
235 change_local_nick(client_p
, source_p
, nick
, 1);
239 /* drop unregged client */
240 else if(IsUnknown(target_p
))
242 exit_client(NULL
, target_p
, &me
, "Overridden");
243 change_local_nick(client_p
, source_p
, nick
, 1);
246 sendto_one(source_p
, form_str(ERR_NICKNAMEINUSE
), me
.name
, source_p
->name
, nick
);
251 change_local_nick(client_p
, source_p
, nick
, 1);
258 * server -> server nick change
260 * parv[2] = TS when nick change
263 mc_nick(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
265 struct Client
*target_p
;
268 /* if nicks erroneous, or too long, kill */
269 if(!clean_nick(parv
[1], 0))
271 bad_nickname(client_p
, parv
[1]);
275 newts
= atol(parv
[2]);
276 target_p
= find_named_client(parv
[1]);
278 /* if the nick doesnt exist, allow it and process like normal */
281 change_remote_nick(client_p
, source_p
, newts
, parv
[1], 1);
283 else if(IsUnknown(target_p
))
285 exit_client(NULL
, target_p
, &me
, "Overridden");
286 change_remote_nick(client_p
, source_p
, newts
, parv
[1], 1);
288 else if(target_p
== source_p
)
290 /* client changing case of nick */
291 if(strcmp(target_p
->name
, parv
[1]))
292 change_remote_nick(client_p
, source_p
, newts
, parv
[1], 1);
294 /* we've got a collision! */
296 perform_nickchange_collides(source_p
, client_p
, target_p
,
297 parc
, parv
, newts
, parv
[1]);
303 ms_nick(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
305 const char *nick
, *server
;
307 nick
= parc
> 1 ? parv
[1] : "?";
308 server
= parc
> 7 ? parv
[7] : "?";
310 sendto_wallops_flags(UMODE_WALLOP
, &me
,
311 "Link %s cancelled, TS5 nickname %s on %s introduced (old server?)",
312 client_p
->name
, nick
, server
);
313 sendto_server(NULL
, NULL
, CAP_TS6
, NOCAPS
,
314 ":%s WALLOPS :Link %s cancelled, TS5 nickname %s on %s introduced (old server?)",
315 me
.id
, client_p
->name
, nick
, server
);
316 ilog(L_SERVER
, "Link %s cancelled, TS5 nickname %s on %s introduced (old server?)",
317 client_p
->name
, nick
, server
);
319 exit_client(client_p
, client_p
, &me
, "TS5 nickname introduced");
336 ms_uid(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
338 struct Client
*target_p
;
340 char squitreason
[120];
342 newts
= atol(parv
[3]);
346 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
347 "Dropping server %s due to (invalid) command 'UID' "
348 "with %d arguments (expecting 10)", client_p
->name
, parc
);
349 ilog(L_SERVER
, "Excess parameters (%d) for command 'UID' from %s.",
350 parc
, client_p
->name
);
351 rb_snprintf(squitreason
, sizeof squitreason
,
352 "Excess parameters (%d) to %s command, expecting %d",
354 exit_client(client_p
, client_p
, client_p
, squitreason
);
358 /* if nicks erroneous, or too long, kill */
359 if(!clean_nick(parv
[1], 0))
361 bad_nickname(client_p
, parv
[1]);
365 if(!clean_uid(parv
[8]))
367 rb_snprintf(squitreason
, sizeof squitreason
,
368 "Invalid UID %s for nick %s on %s",
369 parv
[8], parv
[1], source_p
->name
);
370 exit_client(client_p
, client_p
, client_p
, squitreason
);
374 if(!clean_username(parv
[5]) || !clean_host(parv
[6]))
376 ServerStats
.is_kill
++;
377 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
378 "Bad user@host: %s@%s From: %s(via %s)",
379 parv
[5], parv
[6], source_p
->name
, client_p
->name
);
380 sendto_one(client_p
, ":%s KILL %s :%s (Bad user@host)", me
.id
, parv
[8], me
.name
);
384 /* check length of clients gecos */
385 if(strlen(parv
[9]) > REALLEN
)
387 char *s
= LOCAL_COPY(parv
[9]);
388 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Long realname from server %s for %s",
389 source_p
->name
, parv
[1]);
394 target_p
= find_named_client(parv
[1]);
398 register_client(client_p
, source_p
, parv
[1], newts
, parc
, parv
);
400 else if(IsUnknown(target_p
))
402 exit_client(NULL
, target_p
, &me
, "Overridden");
403 register_client(client_p
, source_p
, parv
[1], newts
, parc
, parv
);
405 /* we've got a collision! */
407 perform_nick_collides(source_p
, client_p
, target_p
, parc
, parv
,
408 newts
, parv
[1], parv
[8]);
427 ms_euid(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
429 struct Client
*target_p
;
431 char squitreason
[120];
433 newts
= atol(parv
[3]);
437 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
438 "Dropping server %s due to (invalid) command 'EUID' "
439 "with %d arguments (expecting 12)", client_p
->name
, parc
);
440 ilog(L_SERVER
, "Excess parameters (%d) for command 'EUID' from %s.",
441 parc
, client_p
->name
);
442 rb_snprintf(squitreason
, sizeof squitreason
,
443 "Excess parameters (%d) to %s command, expecting %d",
445 exit_client(client_p
, client_p
, client_p
, squitreason
);
449 /* if nicks erroneous, or too long, kill */
450 if(!clean_nick(parv
[1], 0))
452 bad_nickname(client_p
, parv
[1]);
456 if(!clean_uid(parv
[8]))
458 rb_snprintf(squitreason
, sizeof squitreason
,
459 "Invalid UID %s for nick %s on %s",
460 parv
[8], parv
[1], source_p
->name
);
461 exit_client(client_p
, client_p
, client_p
, squitreason
);
465 if(!clean_username(parv
[5]) || !clean_host(parv
[6]))
467 ServerStats
.is_kill
++;
468 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
469 "Bad user@host: %s@%s From: %s(via %s)",
470 parv
[5], parv
[6], source_p
->name
, client_p
->name
);
471 sendto_one(client_p
, ":%s KILL %s :%s (Bad user@host)", me
.id
, parv
[8], me
.name
);
475 if(strcmp(parv
[9], "*") && !clean_host(parv
[9]))
477 ServerStats
.is_kill
++;
478 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
479 "Bad realhost: %s From: %s(via %s)",
480 parv
[9], source_p
->name
, client_p
->name
);
481 sendto_one(client_p
, ":%s KILL %s :%s (Bad user@host)", me
.id
, parv
[8], me
.name
);
485 /* check length of clients gecos */
486 if(strlen(parv
[11]) > REALLEN
)
488 char *s
= LOCAL_COPY(parv
[11]);
489 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Long realname from server %s for %s",
490 source_p
->name
, parv
[1]);
495 target_p
= find_named_client(parv
[1]);
499 register_client(client_p
, source_p
, parv
[1], newts
, parc
, parv
);
501 else if(IsUnknown(target_p
))
503 exit_client(NULL
, target_p
, &me
, "Overridden");
504 register_client(client_p
, source_p
, parv
[1], newts
, parc
, parv
);
506 /* we've got a collision! */
508 perform_nick_collides(source_p
, client_p
, target_p
, parc
, parv
,
509 newts
, parv
[1], parv
[8]);
519 ms_save(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
521 struct Client
*target_p
;
523 target_p
= find_id(parv
[1]);
524 if (target_p
== NULL
)
526 if (!IsPerson(target_p
))
527 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
528 "Ignored SAVE message for non-person %s from %s",
529 target_p
->name
, source_p
->name
);
530 else if (IsDigit(target_p
->name
[0]))
531 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
532 "Ignored noop SAVE message for %s from %s",
533 target_p
->name
, source_p
->name
);
534 else if (target_p
->tsinfo
== atol(parv
[2]))
535 save_user(client_p
, source_p
, target_p
);
537 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
538 "Ignored SAVE message for %s from %s",
539 target_p
->name
, source_p
->name
);
545 * input - nickname to check
546 * output - 0 if erroneous, else 1
550 clean_nick(const char *nick
, int loc_client
)
554 /* nicks cant start with a digit or -, and must have a length */
555 if(*nick
== '-' || *nick
== '\0')
558 if(loc_client
&& IsDigit(*nick
))
564 if(!IsNickChar(*nick
))
577 * input - username to check
578 * output - 0 if erroneous, else 0
582 clean_username(const char *username
)
586 for(; *username
; username
++)
590 if(!IsUserChar(*username
))
602 * input - host to check
603 * output - 0 if erroneous, else 0
607 clean_host(const char *host
)
615 if(!IsHostChar(*host
))
626 clean_uid(const char *uid
)
648 set_initial_nick(struct Client
*client_p
, struct Client
*source_p
, char *nick
)
650 char buf
[USERLEN
+ 1];
651 char note
[NICKLEN
+ 10];
653 /* This had to be copied here to avoid problems.. */
654 source_p
->tsinfo
= rb_current_time();
655 if(source_p
->name
[0])
656 del_from_client_hash(source_p
->name
, source_p
);
658 strcpy(source_p
->name
, nick
);
659 add_to_client_hash(nick
, source_p
);
661 rb_snprintf(note
, sizeof(note
), "Nick: %s", nick
);
662 rb_note(client_p
->localClient
->F
, note
);
664 if(source_p
->flags
& FLAGS_SENTUSER
)
666 rb_strlcpy(buf
, source_p
->username
, sizeof(buf
));
668 /* got user, heres nick. */
669 register_local_user(client_p
, source_p
, buf
);
675 change_local_nick(struct Client
*client_p
, struct Client
*source_p
,
676 char *nick
, int dosend
)
678 struct Client
*target_p
;
679 rb_dlink_node
*ptr
, *next_ptr
;
680 struct Channel
*chptr
;
681 char note
[NICKLEN
+ 10];
686 chptr
= find_bannickchange_channel(source_p
);
689 sendto_one_numeric(source_p
, ERR_BANNICKCHANGE
,
690 form_str(ERR_BANNICKCHANGE
),
691 nick
, chptr
->chname
);
694 if((source_p
->localClient
->last_nick_change
+ ConfigFileEntry
.max_nick_time
) < rb_current_time())
695 source_p
->localClient
->number_of_nick_changes
= 0;
697 source_p
->localClient
->last_nick_change
= rb_current_time();
698 source_p
->localClient
->number_of_nick_changes
++;
700 if(ConfigFileEntry
.anti_nick_flood
&& !IsOper(source_p
) &&
701 source_p
->localClient
->number_of_nick_changes
> ConfigFileEntry
.max_nick_changes
)
703 sendto_one(source_p
, form_str(ERR_NICKTOOFAST
),
704 me
.name
, source_p
->name
, source_p
->name
,
705 nick
, ConfigFileEntry
.max_nick_time
);
710 samenick
= irccmp(source_p
->name
, nick
) ? 0 : 1;
712 /* dont reset TS if theyre just changing case of nick */
715 /* force the TS to increase -- jilles */
716 if (source_p
->tsinfo
>= rb_current_time())
719 source_p
->tsinfo
= rb_current_time();
720 monitor_signoff(source_p
);
721 /* we only do bancache for local users -- jilles */
723 invalidate_bancache_user(source_p
);
726 sendto_realops_snomask(SNO_NCHANGE
, L_ALL
,
727 "Nick change: From %s to %s [%s@%s]",
728 source_p
->name
, nick
, source_p
->username
, source_p
->host
);
730 /* send the nick change to the users channels */
731 sendto_common_channels_local(source_p
, ":%s!%s@%s NICK :%s",
732 source_p
->name
, source_p
->username
, source_p
->host
, nick
);
734 /* send the nick change to servers.. */
737 add_history(source_p
, 1);
741 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
, ":%s NICK %s :%ld",
742 use_id(source_p
), nick
, (long) source_p
->tsinfo
);
746 /* Finally, add to hash */
747 del_from_client_hash(source_p
->name
, source_p
);
748 strcpy(source_p
->name
, nick
);
749 add_to_client_hash(nick
, source_p
);
752 monitor_signon(source_p
);
754 /* Make sure everyone that has this client on its accept list
755 * loses that reference.
757 /* we used to call del_all_accepts() here, but theres no real reason
758 * to clear a clients own list of accepted clients. So just remove
759 * them from everyone elses list --anfl
761 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, source_p
->on_allow_list
.head
)
763 target_p
= ptr
->data
;
765 rb_dlinkFindDestroy(source_p
, &target_p
->localClient
->allow_list
);
766 rb_dlinkDestroy(ptr
, &source_p
->on_allow_list
);
769 rb_snprintf(note
, sizeof(note
), "Nick: %s", nick
);
770 rb_note(client_p
->localClient
->F
, note
);
776 * change_remote_nick()
779 change_remote_nick(struct Client
*client_p
, struct Client
*source_p
,
780 time_t newts
, const char *nick
, int dosend
)
783 int samenick
= irccmp(source_p
->name
, nick
) ? 0 : 1;
785 /* client changing their nick - dont reset ts if its same */
788 source_p
->tsinfo
= newts
? newts
: rb_current_time();
789 monitor_signoff(source_p
);
792 sendto_common_channels_local(source_p
, ":%s!%s@%s NICK :%s",
793 source_p
->name
, source_p
->username
, source_p
->host
, nick
);
797 add_history(source_p
, 1);
800 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
, ":%s NICK %s :%ld",
801 use_id(source_p
), nick
, (long) source_p
->tsinfo
);
805 del_from_client_hash(source_p
->name
, source_p
);
807 /* invalidate nick delay when a remote client uses the nick.. */
808 if((nd
= irc_dictionary_retrieve(nd_dict
, nick
)))
811 strcpy(source_p
->name
, nick
);
812 add_to_client_hash(nick
, source_p
);
815 monitor_signon(source_p
);
817 /* remove all accepts pointing to the client */
818 del_all_accepts(source_p
);
824 perform_nick_collides(struct Client
*source_p
, struct Client
*client_p
,
825 struct Client
*target_p
, int parc
, const char *parv
[],
826 time_t newts
, const char *nick
, const char *uid
)
832 use_save
= ConfigFileEntry
.collision_fnc
&& can_save(target_p
) &&
833 uid
!= NULL
&& can_save(source_p
);
834 action
= use_save
? "saved" : "killed";
836 /* if we dont have a ts, or their TS's are the same, kill both */
837 if(!newts
|| !target_p
->tsinfo
|| (newts
== target_p
->tsinfo
))
839 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
840 "Nick collision on %s(%s <- %s)(both %s)",
841 target_p
->name
, target_p
->from
->name
, client_p
->name
, action
);
845 save_user(&me
, &me
, target_p
);
846 ServerStats
.is_save
++;
847 sendto_one(client_p
, ":%s SAVE %s %ld", me
.id
,
849 register_client(client_p
, source_p
,
850 uid
, SAVE_NICKTS
, parc
, parv
);
854 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
855 form_str(ERR_NICKCOLLISION
), target_p
->name
);
857 /* if the new client being introduced has a UID, we need to
858 * issue a KILL for it..
861 sendto_one(client_p
, ":%s KILL %s :%s (Nick collision (new))",
862 me
.id
, uid
, me
.name
);
864 /* we then need to KILL the old client everywhere */
865 kill_client_serv_butone(NULL
, target_p
, "%s (Nick collision (new))", me
.name
);
866 ServerStats
.is_kill
++;
868 target_p
->flags
|= FLAGS_KILLED
;
869 exit_client(client_p
, target_p
, &me
, "Nick collision (new)");
873 /* the timestamps are different */
876 sameuser
= (target_p
->user
) && !irccmp(target_p
->username
, parv
[5])
877 && !irccmp(target_p
->host
, parv
[6]);
879 if((sameuser
&& newts
< target_p
->tsinfo
) ||
880 (!sameuser
&& newts
> target_p
->tsinfo
))
882 /* if we have a UID, then we need to issue a KILL,
883 * otherwise we do nothing and hope that the other
884 * client will collide it..
888 sendto_one(client_p
, ":%s SAVE %s %ld", me
.id
,
890 register_client(client_p
, source_p
,
891 uid
, SAVE_NICKTS
, parc
, parv
);
895 ":%s KILL %s :%s (Nick collision (new))",
896 me
.id
, uid
, me
.name
);
902 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
903 "Nick collision on %s(%s <- %s)(older %s)",
904 target_p
->name
, target_p
->from
->name
,
905 client_p
->name
, action
);
907 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
908 "Nick collision on %s(%s <- %s)(newer %s)",
909 target_p
->name
, target_p
->from
->name
,
910 client_p
->name
, action
);
914 ServerStats
.is_save
++;
915 save_user(&me
, &me
, target_p
);
919 ServerStats
.is_kill
++;
920 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
921 form_str(ERR_NICKCOLLISION
), target_p
->name
);
923 /* now we just need to kill the existing client */
924 kill_client_serv_butone(client_p
, target_p
,
925 "%s (Nick collision (new))", me
.name
);
927 target_p
->flags
|= FLAGS_KILLED
;
928 (void) exit_client(client_p
, target_p
, &me
, "Nick collision");
931 register_client(client_p
, source_p
,
932 nick
, newts
, parc
, parv
);
941 perform_nickchange_collides(struct Client
*source_p
, struct Client
*client_p
,
942 struct Client
*target_p
, int parc
,
943 const char *parv
[], time_t newts
, const char *nick
)
949 use_save
= ConfigFileEntry
.collision_fnc
&& can_save(target_p
) &&
951 action
= use_save
? "saved" : "killed";
953 /* its a client changing nick and causing a collide */
954 if(!newts
|| !target_p
->tsinfo
|| (newts
== target_p
->tsinfo
) || !source_p
->user
)
956 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
957 "Nick change collision from %s to %s(%s <- %s)(both %s)",
958 source_p
->name
, target_p
->name
, target_p
->from
->name
,
959 client_p
->name
, action
);
963 ServerStats
.is_save
+= 2;
964 save_user(&me
, &me
, target_p
);
965 sendto_one(client_p
, ":%s SAVE %s %ld", me
.id
,
966 source_p
->id
, (long)newts
);
967 /* don't send a redundant nick change */
968 if (!IsDigit(source_p
->name
[0]))
969 change_remote_nick(client_p
, source_p
, SAVE_NICKTS
, source_p
->id
, 1);
973 ServerStats
.is_kill
++;
974 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
975 form_str(ERR_NICKCOLLISION
), target_p
->name
);
977 kill_client_serv_butone(NULL
, source_p
, "%s (Nick change collision)", me
.name
);
979 ServerStats
.is_kill
++;
981 kill_client_serv_butone(NULL
, target_p
, "%s (Nick change collision)", me
.name
);
983 target_p
->flags
|= FLAGS_KILLED
;
984 exit_client(NULL
, target_p
, &me
, "Nick collision(new)");
985 source_p
->flags
|= FLAGS_KILLED
;
986 exit_client(client_p
, source_p
, &me
, "Nick collision(old)");
992 sameuser
= !irccmp(target_p
->username
, source_p
->username
) &&
993 !irccmp(target_p
->host
, source_p
->host
);
995 if((sameuser
&& newts
< target_p
->tsinfo
) ||
996 (!sameuser
&& newts
> target_p
->tsinfo
))
999 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
1000 "Nick change collision from %s to %s(%s <- %s)(older %s)",
1001 source_p
->name
, target_p
->name
,
1002 target_p
->from
->name
, client_p
->name
, action
);
1004 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
1005 "Nick change collision from %s to %s(%s <- %s)(newer %s)",
1006 source_p
->name
, target_p
->name
,
1007 target_p
->from
->name
, client_p
->name
, action
);
1011 ServerStats
.is_save
++;
1012 /* can't broadcast a SAVE because the
1013 * nickchange has happened at client_p
1014 * but not in other directions -- jilles */
1015 sendto_one(client_p
, ":%s SAVE %s %ld", me
.id
,
1016 source_p
->id
, (long)newts
);
1017 /* send a :<id> NICK <id> <ts> (!) */
1018 if (!IsDigit(source_p
->name
[0]))
1019 change_remote_nick(client_p
, source_p
, SAVE_NICKTS
, source_p
->id
, 1);
1023 ServerStats
.is_kill
++;
1025 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
1026 form_str(ERR_NICKCOLLISION
), target_p
->name
);
1028 /* kill the client issuing the nickchange */
1029 kill_client_serv_butone(client_p
, source_p
,
1030 "%s (Nick change collision)", me
.name
);
1032 source_p
->flags
|= FLAGS_KILLED
;
1035 exit_client(client_p
, source_p
, &me
, "Nick collision(old)");
1037 exit_client(client_p
, source_p
, &me
, "Nick collision(new)");
1044 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
1045 "Nick collision on %s(%s <- %s)(older %s)",
1046 target_p
->name
, target_p
->from
->name
,
1047 client_p
->name
, action
);
1049 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
1050 "Nick collision on %s(%s <- %s)(newer %s)",
1051 target_p
->name
, target_p
->from
->name
,
1052 client_p
->name
, action
);
1056 ServerStats
.is_save
++;
1057 save_user(&me
, &me
, target_p
);
1061 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
1062 form_str(ERR_NICKCOLLISION
), target_p
->name
);
1064 /* kill the client who existed before hand */
1065 kill_client_serv_butone(client_p
, target_p
, "%s (Nick collision)", me
.name
);
1067 ServerStats
.is_kill
++;
1069 target_p
->flags
|= FLAGS_KILLED
;
1070 (void) exit_client(client_p
, target_p
, &me
, "Nick collision");
1075 change_remote_nick(client_p
, source_p
, newts
, nick
, 1);
1081 register_client(struct Client
*client_p
, struct Client
*server
,
1082 const char *nick
, time_t newts
, int parc
, const char *parv
[])
1084 struct Client
*source_p
;
1086 struct nd_entry
*nd
;
1090 source_p
= make_client(client_p
);
1091 user
= make_user(source_p
);
1092 rb_dlinkAddTail(source_p
, &source_p
->node
, &global_client_list
);
1094 source_p
->hopcount
= atoi(parv
[2]);
1095 source_p
->tsinfo
= newts
;
1097 strcpy(source_p
->name
, nick
);
1098 rb_strlcpy(source_p
->username
, parv
[5], sizeof(source_p
->username
));
1099 rb_strlcpy(source_p
->host
, parv
[6], sizeof(source_p
->host
));
1100 rb_strlcpy(source_p
->orighost
, source_p
->host
, sizeof(source_p
->orighost
));
1104 rb_strlcpy(source_p
->info
, parv
[11], sizeof(source_p
->info
));
1105 rb_strlcpy(source_p
->sockhost
, parv
[7], sizeof(source_p
->sockhost
));
1106 rb_strlcpy(source_p
->id
, parv
[8], sizeof(source_p
->id
));
1107 add_to_id_hash(source_p
->id
, source_p
);
1108 if (strcmp(parv
[9], "*"))
1110 rb_strlcpy(source_p
->orighost
, parv
[9], sizeof(source_p
->orighost
));
1111 if (irccmp(source_p
->host
, source_p
->orighost
))
1112 SetDynSpoof(source_p
);
1114 if (strcmp(parv
[10], "*"))
1115 rb_strlcpy(source_p
->user
->suser
, parv
[10], sizeof(source_p
->user
->suser
));
1119 rb_strlcpy(source_p
->info
, parv
[9], sizeof(source_p
->info
));
1120 rb_strlcpy(source_p
->sockhost
, parv
[7], sizeof(source_p
->sockhost
));
1121 rb_strlcpy(source_p
->id
, parv
[8], sizeof(source_p
->id
));
1122 add_to_id_hash(source_p
->id
, source_p
);
1129 /* remove any nd entries for this nick */
1130 if((nd
= irc_dictionary_retrieve(nd_dict
, nick
)))
1133 add_to_client_hash(nick
, source_p
);
1134 add_to_hostname_hash(source_p
->orighost
, source_p
);
1135 monitor_signon(source_p
);
1140 flag
= user_modes
[(unsigned char) *m
];
1142 if(flag
& UMODE_SERVICE
)
1147 RB_DLINK_FOREACH(ptr
, service_list
.head
)
1149 if(!irccmp((const char *) ptr
->data
, server
->name
))
1163 /* increment +i count if theyre invis */
1164 if(!(source_p
->umodes
& UMODE_INVISIBLE
) && (flag
& UMODE_INVISIBLE
))
1167 /* increment opered count if theyre opered */
1168 if(!(source_p
->umodes
& UMODE_OPER
) && (flag
& UMODE_OPER
))
1171 source_p
->umodes
|= flag
;
1175 if(IsOper(source_p
) && !IsService(source_p
))
1176 rb_dlinkAddAlloc(source_p
, &oper_list
);
1178 SetRemoteClient(source_p
);
1180 if(++Count
.total
> Count
.max_tot
)
1181 Count
.max_tot
= Count
.total
;
1183 source_p
->servptr
= server
;
1185 rb_dlinkAdd(source_p
, &source_p
->lnode
, &source_p
->servptr
->serv
->users
);
1187 call_hook(h_new_remote_user
, source_p
);
1189 return (introduce_client(client_p
, source_p
, user
, nick
, parc
== 12));
1192 /* Check if we can do SAVE. target_p can be a client to save or a
1193 * server introducing a client -- jilles */
1195 can_save(struct Client
*target_p
)
1197 struct Client
*serv_p
;
1199 if (MyClient(target_p
))
1201 if (!has_id(target_p
))
1203 serv_p
= IsServer(target_p
) ? target_p
: target_p
->servptr
;
1204 while (serv_p
!= NULL
&& serv_p
!= &me
)
1206 if (!(serv_p
->serv
->caps
& CAP_SAVE
))
1208 serv_p
= serv_p
->servptr
;
1210 return serv_p
== &me
;
1214 save_user(struct Client
*client_p
, struct Client
*source_p
,
1215 struct Client
*target_p
)
1217 if (!MyConnect(target_p
) && (!has_id(target_p
) || !IsCapable(target_p
->from
, CAP_SAVE
)))
1219 /* This shouldn't happen */
1220 /* Note we only need SAVE support in this direction */
1221 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1222 "Killed %s!%s@%s for nick collision detected by %s (%s does not support SAVE)",
1223 target_p
->name
, target_p
->username
, target_p
->host
, source_p
->name
, target_p
->from
->name
);
1224 kill_client_serv_butone(NULL
, target_p
, "%s (Nick collision (no SAVE support))", me
.name
);
1225 ServerStats
.is_kill
++;
1227 target_p
->flags
|= FLAGS_KILLED
;
1228 (void) exit_client(NULL
, target_p
, &me
, "Nick collision (no SAVE support)");
1231 sendto_server(client_p
, NULL
, CAP_SAVE
|CAP_TS6
, NOCAPS
, ":%s SAVE %s %ld",
1232 source_p
->id
, target_p
->id
, (long)target_p
->tsinfo
);
1233 sendto_server(client_p
, NULL
, CAP_TS6
, CAP_SAVE
, ":%s NICK %s :%ld",
1234 target_p
->id
, target_p
->id
, (long)SAVE_NICKTS
);
1235 if (!IsMe(client_p
))
1236 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
1237 "Received SAVE message for %s from %s",
1238 target_p
->name
, source_p
->name
);
1239 if (MyClient(target_p
))
1241 sendto_one_numeric(target_p
, RPL_SAVENICK
,
1242 form_str(RPL_SAVENICK
), target_p
->id
);
1243 change_local_nick(target_p
, target_p
, target_p
->id
, 0);
1244 target_p
->tsinfo
= SAVE_NICKTS
;
1247 change_remote_nick(target_p
, target_p
, SAVE_NICKTS
, target_p
->id
, 0);
1250 static void bad_nickname(struct Client
*client_p
, const char *nick
)
1252 char squitreason
[100];
1254 sendto_wallops_flags(UMODE_WALLOP
, &me
,
1255 "Squitting %s because of bad nickname %s (NICKLEN mismatch?)",
1256 client_p
->name
, nick
);
1257 sendto_server(NULL
, NULL
, CAP_TS6
, NOCAPS
,
1258 ":%s WALLOPS :Squitting %s because of bad nickname %s (NICKLEN mismatch?)",
1259 me
.id
, client_p
->name
, nick
);
1260 ilog(L_SERVER
, "Link %s cancelled, bad nickname %s sent (NICKLEN mismatch?)",
1261 client_p
->name
, nick
);
1263 rb_snprintf(squitreason
, sizeof squitreason
,
1264 "Bad nickname introduced [%s]", nick
);
1265 exit_client(client_p
, client_p
, &me
, squitreason
);