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 *);
111 * parv[0] = sender prefix
115 mr_nick(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
117 struct Client
*target_p
;
121 if (strlen(client_p
->id
) == 3)
123 exit_client(client_p
, client_p
, client_p
, "Mixing client and server protocol");
127 if(parc
< 2 || EmptyString(parv
[1]) || (parv
[1][0] == '~'))
129 sendto_one(source_p
, form_str(ERR_NONICKNAMEGIVEN
),
130 me
.name
, EmptyString(source_p
->name
) ? "*" : source_p
->name
);
134 /* due to the scandinavian origins, (~ being uppercase of ^) and ~
135 * being disallowed as a nick char, we need to chop the first ~
136 * instead of just erroring.
138 if((s
= strchr(parv
[1], '~')))
141 /* copy the nick and terminate it */
142 rb_strlcpy(nick
, parv
[1], sizeof(nick
));
144 /* check the nickname is ok */
145 if(!clean_nick(nick
, 1))
147 sendto_one(source_p
, form_str(ERR_ERRONEUSNICKNAME
),
148 me
.name
, EmptyString(parv
[0]) ? "*" : parv
[0], parv
[1]);
152 /* check if the nick is resv'd */
153 if(find_nick_resv(nick
))
155 sendto_one(source_p
, form_str(ERR_ERRONEUSNICKNAME
),
156 me
.name
, EmptyString(source_p
->name
) ? "*" : source_p
->name
, nick
);
160 if(irc_dictionary_find(nd_dict
, nick
))
162 sendto_one(source_p
, form_str(ERR_UNAVAILRESOURCE
),
163 me
.name
, EmptyString(source_p
->name
) ? "*" : source_p
->name
, nick
);
167 if((target_p
= find_named_client(nick
)) == NULL
)
168 set_initial_nick(client_p
, source_p
, nick
);
169 else if(source_p
== target_p
)
170 strcpy(source_p
->name
, nick
);
172 sendto_one(source_p
, form_str(ERR_NICKNAMEINUSE
), me
.name
, "*", nick
);
178 * parv[0] = sender prefix
182 m_nick(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
184 struct Client
*target_p
;
188 if(parc
< 2 || EmptyString(parv
[1]) || (parv
[1][0] == '~'))
190 sendto_one(source_p
, form_str(ERR_NONICKNAMEGIVEN
), me
.name
, source_p
->name
);
194 /* due to the scandinavian origins, (~ being uppercase of ^) and ~
195 * being disallowed as a nick char, we need to chop the first ~
196 * instead of just erroring.
198 if((s
= strchr(parv
[1], '~')))
201 /* mark end of grace period, to prevent nickflooding */
202 if(!IsFloodDone(source_p
))
203 flood_endgrace(source_p
);
205 /* terminate nick to NICKLEN, we dont want clean_nick() to error! */
206 rb_strlcpy(nick
, parv
[1], sizeof(nick
));
208 /* check the nickname is ok */
209 if(!clean_nick(nick
, 1))
211 sendto_one(source_p
, form_str(ERR_ERRONEUSNICKNAME
), me
.name
, parv
[0], nick
);
215 if(!IsExemptResv(source_p
) && find_nick_resv(nick
))
217 sendto_one(source_p
, form_str(ERR_ERRONEUSNICKNAME
), me
.name
, source_p
->name
, nick
);
221 if(irc_dictionary_find(nd_dict
, nick
))
223 sendto_one(source_p
, form_str(ERR_UNAVAILRESOURCE
),
224 me
.name
, EmptyString(source_p
->name
) ? "*" : source_p
->name
, nick
);
228 if((target_p
= find_named_client(nick
)))
230 /* If(target_p == source_p) the client is changing nicks between
231 * equivalent nicknames ie: [nick] -> {nick}
233 if(target_p
== source_p
)
235 /* check the nick isnt exactly the same */
236 if(strcmp(target_p
->name
, nick
))
237 change_local_nick(client_p
, source_p
, nick
, 1);
241 /* drop unregged client */
242 else if(IsUnknown(target_p
))
244 exit_client(NULL
, target_p
, &me
, "Overridden");
245 change_local_nick(client_p
, source_p
, nick
, 1);
248 sendto_one(source_p
, form_str(ERR_NICKNAMEINUSE
), me
.name
, parv
[0], nick
);
253 change_local_nick(client_p
, source_p
, nick
, 1);
260 * server -> server nick change
261 * parv[0] = sender prefix
263 * parv[2] = TS when nick change
266 mc_nick(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
268 struct Client
*target_p
;
271 /* if nicks erroneous, or too long, kill */
272 if(!clean_nick(parv
[1], 0))
274 bad_nickname(client_p
, parv
[1]);
278 newts
= atol(parv
[2]);
279 target_p
= find_named_client(parv
[1]);
281 /* if the nick doesnt exist, allow it and process like normal */
284 change_remote_nick(client_p
, source_p
, newts
, parv
[1], 1);
286 else if(IsUnknown(target_p
))
288 exit_client(NULL
, target_p
, &me
, "Overridden");
289 change_remote_nick(client_p
, source_p
, newts
, parv
[1], 1);
291 else if(target_p
== source_p
)
293 /* client changing case of nick */
294 if(strcmp(target_p
->name
, parv
[1]))
295 change_remote_nick(client_p
, source_p
, newts
, parv
[1], 1);
297 /* we've got a collision! */
299 perform_nickchange_collides(source_p
, client_p
, target_p
,
300 parc
, parv
, newts
, parv
[1]);
306 ms_nick(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
308 const char *nick
, *server
;
310 nick
= parc
> 1 ? parv
[1] : "?";
311 server
= parc
> 7 ? parv
[7] : "?";
313 sendto_wallops_flags(UMODE_WALLOP
, &me
,
314 "Link %s cancelled, TS5 nickname %s on %s introduced (old server?)",
315 client_p
->name
, nick
, server
);
316 sendto_server(NULL
, NULL
, CAP_TS6
, NOCAPS
,
317 ":%s WALLOPS :Link %s cancelled, TS5 nickname %s on %s introduced (old server?)",
318 me
.id
, client_p
->name
, nick
, server
);
319 ilog(L_SERVER
, "Link %s cancelled, TS5 nickname %s on %s introduced (old server?)",
320 client_p
->name
, nick
, server
);
322 exit_client(client_p
, client_p
, &me
, "TS5 nickname introduced");
339 ms_uid(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
341 struct Client
*target_p
;
343 char squitreason
[120];
345 newts
= atol(parv
[3]);
349 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
350 "Dropping server %s due to (invalid) command 'UID' "
351 "with %d arguments (expecting 10)", client_p
->name
, parc
);
352 ilog(L_SERVER
, "Excess parameters (%d) for command 'UID' from %s.",
353 parc
, client_p
->name
);
354 rb_snprintf(squitreason
, sizeof squitreason
,
355 "Excess parameters (%d) to %s command, expecting %d",
357 exit_client(client_p
, client_p
, client_p
, squitreason
);
361 /* if nicks erroneous, or too long, kill */
362 if(!clean_nick(parv
[1], 0))
364 bad_nickname(client_p
, parv
[1]);
368 if(!clean_uid(parv
[8]))
370 rb_snprintf(squitreason
, sizeof squitreason
,
371 "Invalid UID %s for nick %s on %s",
372 parv
[8], parv
[1], source_p
->name
);
373 exit_client(client_p
, client_p
, client_p
, squitreason
);
377 if(!clean_username(parv
[5]) || !clean_host(parv
[6]))
379 ServerStats
.is_kill
++;
380 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
381 "Bad user@host: %s@%s From: %s(via %s)",
382 parv
[5], parv
[6], source_p
->name
, client_p
->name
);
383 sendto_one(client_p
, ":%s KILL %s :%s (Bad user@host)", me
.id
, parv
[8], me
.name
);
387 /* check length of clients gecos */
388 if(strlen(parv
[9]) > REALLEN
)
390 char *s
= LOCAL_COPY(parv
[9]);
391 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Long realname from server %s for %s",
397 target_p
= find_named_client(parv
[1]);
401 register_client(client_p
, source_p
, parv
[1], newts
, parc
, parv
);
403 else if(IsUnknown(target_p
))
405 exit_client(NULL
, target_p
, &me
, "Overridden");
406 register_client(client_p
, source_p
, parv
[1], newts
, parc
, parv
);
408 /* we've got a collision! */
410 perform_nick_collides(source_p
, client_p
, target_p
, parc
, parv
,
411 newts
, parv
[1], parv
[8]);
430 ms_euid(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
432 struct Client
*target_p
;
434 char squitreason
[120];
436 newts
= atol(parv
[3]);
440 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
441 "Dropping server %s due to (invalid) command 'EUID' "
442 "with %d arguments (expecting 12)", client_p
->name
, parc
);
443 ilog(L_SERVER
, "Excess parameters (%d) for command 'EUID' from %s.",
444 parc
, client_p
->name
);
445 rb_snprintf(squitreason
, sizeof squitreason
,
446 "Excess parameters (%d) to %s command, expecting %d",
448 exit_client(client_p
, client_p
, client_p
, squitreason
);
452 /* if nicks erroneous, or too long, kill */
453 if(!clean_nick(parv
[1], 0))
455 bad_nickname(client_p
, parv
[1]);
459 if(!clean_uid(parv
[8]))
461 rb_snprintf(squitreason
, sizeof squitreason
,
462 "Invalid UID %s for nick %s on %s",
463 parv
[8], parv
[1], source_p
->name
);
464 exit_client(client_p
, client_p
, client_p
, squitreason
);
468 if(!clean_username(parv
[5]) || !clean_host(parv
[6]))
470 ServerStats
.is_kill
++;
471 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
472 "Bad user@host: %s@%s From: %s(via %s)",
473 parv
[5], parv
[6], source_p
->name
, client_p
->name
);
474 sendto_one(client_p
, ":%s KILL %s :%s (Bad user@host)", me
.id
, parv
[8], me
.name
);
478 if(strcmp(parv
[9], "*") && !clean_host(parv
[9]))
480 ServerStats
.is_kill
++;
481 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
482 "Bad realhost: %s From: %s(via %s)",
483 parv
[9], source_p
->name
, client_p
->name
);
484 sendto_one(client_p
, ":%s KILL %s :%s (Bad user@host)", me
.id
, parv
[8], me
.name
);
488 /* check length of clients gecos */
489 if(strlen(parv
[11]) > REALLEN
)
491 char *s
= LOCAL_COPY(parv
[11]);
492 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Long realname from server %s for %s",
498 target_p
= find_named_client(parv
[1]);
502 register_client(client_p
, source_p
, parv
[1], newts
, parc
, parv
);
504 else if(IsUnknown(target_p
))
506 exit_client(NULL
, target_p
, &me
, "Overridden");
507 register_client(client_p
, source_p
, parv
[1], newts
, parc
, parv
);
509 /* we've got a collision! */
511 perform_nick_collides(source_p
, client_p
, target_p
, parc
, parv
,
512 newts
, parv
[1], parv
[8]);
522 ms_save(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
524 struct Client
*target_p
;
526 target_p
= find_id(parv
[1]);
527 if (target_p
== NULL
)
529 if (!IsPerson(target_p
))
530 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
531 "Ignored SAVE message for non-person %s from %s",
532 target_p
->name
, source_p
->name
);
533 else if (IsDigit(target_p
->name
[0]))
534 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
535 "Ignored noop SAVE message for %s from %s",
536 target_p
->name
, source_p
->name
);
537 else if (target_p
->tsinfo
== atol(parv
[2]))
538 save_user(client_p
, source_p
, target_p
);
540 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
541 "Ignored SAVE message for %s from %s",
542 target_p
->name
, source_p
->name
);
548 * input - nickname to check
549 * output - 0 if erroneous, else 1
553 clean_nick(const char *nick
, int loc_client
)
557 /* nicks cant start with a digit or -, and must have a length */
558 if(*nick
== '-' || *nick
== '\0')
561 if(loc_client
&& IsDigit(*nick
))
567 if(!IsNickChar(*nick
))
580 * input - username to check
581 * output - 0 if erroneous, else 0
585 clean_username(const char *username
)
589 for(; *username
; username
++)
593 if(!IsUserChar(*username
))
605 * input - host to check
606 * output - 0 if erroneous, else 0
610 clean_host(const char *host
)
618 if(!IsHostChar(*host
))
629 clean_uid(const char *uid
)
651 set_initial_nick(struct Client
*client_p
, struct Client
*source_p
, char *nick
)
653 char buf
[USERLEN
+ 1];
654 char note
[NICKLEN
+ 10];
656 /* This had to be copied here to avoid problems.. */
657 source_p
->tsinfo
= rb_current_time();
658 if(source_p
->name
[0])
659 del_from_client_hash(source_p
->name
, source_p
);
661 strcpy(source_p
->name
, nick
);
662 add_to_client_hash(nick
, source_p
);
664 rb_snprintf(note
, sizeof(note
), "Nick: %s", nick
);
665 rb_note(client_p
->localClient
->F
, note
);
667 if(source_p
->flags
& FLAGS_SENTUSER
)
669 rb_strlcpy(buf
, source_p
->username
, sizeof(buf
));
671 /* got user, heres nick. */
672 register_local_user(client_p
, source_p
, buf
);
678 change_local_nick(struct Client
*client_p
, struct Client
*source_p
,
679 char *nick
, int dosend
)
681 struct Client
*target_p
;
682 rb_dlink_node
*ptr
, *next_ptr
;
683 struct Channel
*chptr
;
684 char note
[NICKLEN
+ 10];
689 chptr
= find_bannickchange_channel(source_p
);
692 sendto_one_numeric(source_p
, ERR_BANNICKCHANGE
,
693 form_str(ERR_BANNICKCHANGE
),
694 nick
, chptr
->chname
);
697 if((source_p
->localClient
->last_nick_change
+ ConfigFileEntry
.max_nick_time
) < rb_current_time())
698 source_p
->localClient
->number_of_nick_changes
= 0;
700 source_p
->localClient
->last_nick_change
= rb_current_time();
701 source_p
->localClient
->number_of_nick_changes
++;
703 if(ConfigFileEntry
.anti_nick_flood
&& !IsOper(source_p
) &&
704 source_p
->localClient
->number_of_nick_changes
> ConfigFileEntry
.max_nick_changes
)
706 sendto_one(source_p
, form_str(ERR_NICKTOOFAST
),
707 me
.name
, source_p
->name
, source_p
->name
,
708 nick
, ConfigFileEntry
.max_nick_time
);
713 samenick
= irccmp(source_p
->name
, nick
) ? 0 : 1;
715 /* dont reset TS if theyre just changing case of nick */
718 /* force the TS to increase -- jilles */
719 if (source_p
->tsinfo
>= rb_current_time())
722 source_p
->tsinfo
= rb_current_time();
723 monitor_signoff(source_p
);
724 /* we only do bancache for local users -- jilles */
726 invalidate_bancache_user(source_p
);
729 sendto_realops_snomask(SNO_NCHANGE
, L_ALL
,
730 "Nick change: From %s to %s [%s@%s]",
731 source_p
->name
, nick
, source_p
->username
, source_p
->host
);
733 /* send the nick change to the users channels */
734 sendto_common_channels_local(source_p
, ":%s!%s@%s NICK :%s",
735 source_p
->name
, source_p
->username
, source_p
->host
, nick
);
737 /* send the nick change to servers.. */
740 add_history(source_p
, 1);
744 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
, ":%s NICK %s :%ld",
745 use_id(source_p
), nick
, (long) source_p
->tsinfo
);
749 /* Finally, add to hash */
750 del_from_client_hash(source_p
->name
, source_p
);
751 strcpy(source_p
->name
, nick
);
752 add_to_client_hash(nick
, source_p
);
755 monitor_signon(source_p
);
757 /* Make sure everyone that has this client on its accept list
758 * loses that reference.
760 /* we used to call del_all_accepts() here, but theres no real reason
761 * to clear a clients own list of accepted clients. So just remove
762 * them from everyone elses list --anfl
764 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, source_p
->on_allow_list
.head
)
766 target_p
= ptr
->data
;
768 rb_dlinkFindDestroy(source_p
, &target_p
->localClient
->allow_list
);
769 rb_dlinkDestroy(ptr
, &source_p
->on_allow_list
);
772 rb_snprintf(note
, sizeof(note
), "Nick: %s", nick
);
773 rb_note(client_p
->localClient
->F
, note
);
779 * change_remote_nick()
782 change_remote_nick(struct Client
*client_p
, struct Client
*source_p
,
783 time_t newts
, const char *nick
, int dosend
)
786 int samenick
= irccmp(source_p
->name
, nick
) ? 0 : 1;
788 /* client changing their nick - dont reset ts if its same */
791 source_p
->tsinfo
= newts
? newts
: rb_current_time();
792 monitor_signoff(source_p
);
795 sendto_common_channels_local(source_p
, ":%s!%s@%s NICK :%s",
796 source_p
->name
, source_p
->username
, source_p
->host
, nick
);
800 add_history(source_p
, 1);
803 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
, ":%s NICK %s :%ld",
804 use_id(source_p
), nick
, (long) source_p
->tsinfo
);
808 del_from_client_hash(source_p
->name
, source_p
);
810 /* invalidate nick delay when a remote client uses the nick.. */
811 if((nd
= irc_dictionary_retrieve(nd_dict
, nick
)))
814 strcpy(source_p
->name
, nick
);
815 add_to_client_hash(nick
, source_p
);
818 monitor_signon(source_p
);
820 /* remove all accepts pointing to the client */
821 del_all_accepts(source_p
);
827 perform_nick_collides(struct Client
*source_p
, struct Client
*client_p
,
828 struct Client
*target_p
, int parc
, const char *parv
[],
829 time_t newts
, const char *nick
, const char *uid
)
835 use_save
= ConfigFileEntry
.collision_fnc
&& can_save(target_p
) &&
836 uid
!= NULL
&& can_save(source_p
);
837 action
= use_save
? "saved" : "killed";
839 /* if we dont have a ts, or their TS's are the same, kill both */
840 if(!newts
|| !target_p
->tsinfo
|| (newts
== target_p
->tsinfo
))
842 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
843 "Nick collision on %s(%s <- %s)(both %s)",
844 target_p
->name
, target_p
->from
->name
, client_p
->name
, action
);
848 save_user(&me
, &me
, target_p
);
849 ServerStats
.is_save
++;
850 sendto_one(client_p
, ":%s SAVE %s %ld", me
.id
,
852 register_client(client_p
, source_p
,
853 uid
, SAVE_NICKTS
, parc
, parv
);
857 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
858 form_str(ERR_NICKCOLLISION
), target_p
->name
);
860 /* if the new client being introduced has a UID, we need to
861 * issue a KILL for it..
864 sendto_one(client_p
, ":%s KILL %s :%s (Nick collision (new))",
865 me
.id
, uid
, me
.name
);
867 /* we then need to KILL the old client everywhere */
868 kill_client_serv_butone(NULL
, target_p
, "%s (Nick collision (new))", me
.name
);
869 ServerStats
.is_kill
++;
871 target_p
->flags
|= FLAGS_KILLED
;
872 exit_client(client_p
, target_p
, &me
, "Nick collision (new)");
876 /* the timestamps are different */
879 sameuser
= (target_p
->user
) && !irccmp(target_p
->username
, parv
[5])
880 && !irccmp(target_p
->host
, parv
[6]);
882 if((sameuser
&& newts
< target_p
->tsinfo
) ||
883 (!sameuser
&& newts
> target_p
->tsinfo
))
885 /* if we have a UID, then we need to issue a KILL,
886 * otherwise we do nothing and hope that the other
887 * client will collide it..
891 sendto_one(client_p
, ":%s SAVE %s %ld", me
.id
,
893 register_client(client_p
, source_p
,
894 uid
, SAVE_NICKTS
, parc
, parv
);
898 ":%s KILL %s :%s (Nick collision (new))",
899 me
.id
, uid
, me
.name
);
905 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
906 "Nick collision on %s(%s <- %s)(older %s)",
907 target_p
->name
, target_p
->from
->name
,
908 client_p
->name
, action
);
910 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
911 "Nick collision on %s(%s <- %s)(newer %s)",
912 target_p
->name
, target_p
->from
->name
,
913 client_p
->name
, action
);
917 ServerStats
.is_save
++;
918 save_user(&me
, &me
, target_p
);
922 ServerStats
.is_kill
++;
923 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
924 form_str(ERR_NICKCOLLISION
), target_p
->name
);
926 /* now we just need to kill the existing client */
927 kill_client_serv_butone(client_p
, target_p
,
928 "%s (Nick collision (new))", me
.name
);
930 target_p
->flags
|= FLAGS_KILLED
;
931 (void) exit_client(client_p
, target_p
, &me
, "Nick collision");
934 register_client(client_p
, parc
>= 10 ? source_p
: NULL
,
935 nick
, newts
, parc
, parv
);
944 perform_nickchange_collides(struct Client
*source_p
, struct Client
*client_p
,
945 struct Client
*target_p
, int parc
,
946 const char *parv
[], time_t newts
, const char *nick
)
952 use_save
= ConfigFileEntry
.collision_fnc
&& can_save(target_p
) &&
954 action
= use_save
? "saved" : "killed";
956 /* its a client changing nick and causing a collide */
957 if(!newts
|| !target_p
->tsinfo
|| (newts
== target_p
->tsinfo
) || !source_p
->user
)
959 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
960 "Nick change collision from %s to %s(%s <- %s)(both %s)",
961 source_p
->name
, target_p
->name
, target_p
->from
->name
,
962 client_p
->name
, action
);
966 ServerStats
.is_save
+= 2;
967 save_user(&me
, &me
, target_p
);
968 sendto_one(client_p
, ":%s SAVE %s %ld", me
.id
,
969 source_p
->id
, (long)newts
);
970 /* don't send a redundant nick change */
971 if (!IsDigit(source_p
->name
[0]))
972 change_remote_nick(client_p
, source_p
, SAVE_NICKTS
, source_p
->id
, 1);
976 ServerStats
.is_kill
++;
977 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
978 form_str(ERR_NICKCOLLISION
), target_p
->name
);
980 kill_client_serv_butone(NULL
, source_p
, "%s (Nick change collision)", me
.name
);
982 ServerStats
.is_kill
++;
984 kill_client_serv_butone(NULL
, target_p
, "%s (Nick change collision)", me
.name
);
986 target_p
->flags
|= FLAGS_KILLED
;
987 exit_client(NULL
, target_p
, &me
, "Nick collision(new)");
988 source_p
->flags
|= FLAGS_KILLED
;
989 exit_client(client_p
, source_p
, &me
, "Nick collision(old)");
995 sameuser
= !irccmp(target_p
->username
, source_p
->username
) &&
996 !irccmp(target_p
->host
, source_p
->host
);
998 if((sameuser
&& newts
< target_p
->tsinfo
) ||
999 (!sameuser
&& newts
> target_p
->tsinfo
))
1002 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
1003 "Nick change collision from %s to %s(%s <- %s)(older %s)",
1004 source_p
->name
, target_p
->name
,
1005 target_p
->from
->name
, client_p
->name
, action
);
1007 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
1008 "Nick change collision from %s to %s(%s <- %s)(newer %s)",
1009 source_p
->name
, target_p
->name
,
1010 target_p
->from
->name
, client_p
->name
, action
);
1014 ServerStats
.is_save
++;
1015 /* can't broadcast a SAVE because the
1016 * nickchange has happened at client_p
1017 * but not in other directions -- jilles */
1018 sendto_one(client_p
, ":%s SAVE %s %ld", me
.id
,
1019 source_p
->id
, (long)newts
);
1020 /* send a :<id> NICK <id> <ts> (!) */
1021 if (!IsDigit(source_p
->name
[0]))
1022 change_remote_nick(client_p
, source_p
, SAVE_NICKTS
, source_p
->id
, 1);
1026 ServerStats
.is_kill
++;
1028 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
1029 form_str(ERR_NICKCOLLISION
), target_p
->name
);
1031 /* kill the client issuing the nickchange */
1032 kill_client_serv_butone(client_p
, source_p
,
1033 "%s (Nick change collision)", me
.name
);
1035 source_p
->flags
|= FLAGS_KILLED
;
1038 exit_client(client_p
, source_p
, &me
, "Nick collision(old)");
1040 exit_client(client_p
, source_p
, &me
, "Nick collision(new)");
1047 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
1048 "Nick collision on %s(%s <- %s)(older %s)",
1049 target_p
->name
, target_p
->from
->name
,
1050 client_p
->name
, action
);
1052 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
1053 "Nick collision on %s(%s <- %s)(newer %s)",
1054 target_p
->name
, target_p
->from
->name
,
1055 client_p
->name
, action
);
1059 ServerStats
.is_save
++;
1060 save_user(&me
, &me
, target_p
);
1064 sendto_one_numeric(target_p
, ERR_NICKCOLLISION
,
1065 form_str(ERR_NICKCOLLISION
), target_p
->name
);
1067 /* kill the client who existed before hand */
1068 kill_client_serv_butone(client_p
, target_p
, "%s (Nick collision)", me
.name
);
1070 ServerStats
.is_kill
++;
1072 target_p
->flags
|= FLAGS_KILLED
;
1073 (void) exit_client(client_p
, target_p
, &me
, "Nick collision");
1078 change_remote_nick(client_p
, source_p
, newts
, nick
, 1);
1084 register_client(struct Client
*client_p
, struct Client
*server
,
1085 const char *nick
, time_t newts
, int parc
, const char *parv
[])
1087 struct Client
*source_p
;
1089 struct nd_entry
*nd
;
1095 if((server
= find_server(NULL
, parv
[7])) == NULL
)
1097 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1098 "Ghost killed: %s on invalid server %s",
1100 sendto_one(client_p
, ":%s KILL %s :%s (Server doesn't exist)",
1101 get_id(&me
, client_p
), nick
, me
.name
);
1106 source_p
= make_client(client_p
);
1107 user
= make_user(source_p
);
1108 rb_dlinkAddTail(source_p
, &source_p
->node
, &global_client_list
);
1110 source_p
->hopcount
= atoi(parv
[2]);
1111 source_p
->tsinfo
= newts
;
1113 strcpy(source_p
->name
, nick
);
1114 rb_strlcpy(source_p
->username
, parv
[5], sizeof(source_p
->username
));
1115 rb_strlcpy(source_p
->host
, parv
[6], sizeof(source_p
->host
));
1116 rb_strlcpy(source_p
->orighost
, source_p
->host
, sizeof(source_p
->orighost
));
1120 rb_strlcpy(source_p
->info
, parv
[11], sizeof(source_p
->info
));
1121 rb_strlcpy(source_p
->sockhost
, parv
[7], sizeof(source_p
->sockhost
));
1122 rb_strlcpy(source_p
->id
, parv
[8], sizeof(source_p
->id
));
1123 add_to_id_hash(source_p
->id
, source_p
);
1124 if (strcmp(parv
[9], "*"))
1126 rb_strlcpy(source_p
->orighost
, parv
[9], sizeof(source_p
->orighost
));
1127 if (irccmp(source_p
->host
, source_p
->orighost
))
1128 SetDynSpoof(source_p
);
1130 if (strcmp(parv
[10], "*"))
1131 rb_strlcpy(source_p
->user
->suser
, parv
[10], sizeof(source_p
->user
->suser
));
1135 rb_strlcpy(source_p
->info
, parv
[9], sizeof(source_p
->info
));
1136 rb_strlcpy(source_p
->sockhost
, parv
[7], sizeof(source_p
->sockhost
));
1137 rb_strlcpy(source_p
->id
, parv
[8], sizeof(source_p
->id
));
1138 add_to_id_hash(source_p
->id
, source_p
);
1142 rb_strlcpy(source_p
->info
, parv
[8], sizeof(source_p
->info
));
1145 /* remove any nd entries for this nick */
1146 if((nd
= irc_dictionary_retrieve(nd_dict
, nick
)))
1149 add_to_client_hash(nick
, source_p
);
1150 add_to_hostname_hash(source_p
->orighost
, source_p
);
1151 monitor_signon(source_p
);
1156 flag
= user_modes
[(unsigned char) *m
];
1158 if(flag
& UMODE_SERVICE
)
1163 RB_DLINK_FOREACH(ptr
, service_list
.head
)
1165 if(!irccmp((const char *) ptr
->data
, server
->name
))
1179 /* increment +i count if theyre invis */
1180 if(!(source_p
->umodes
& UMODE_INVISIBLE
) && (flag
& UMODE_INVISIBLE
))
1183 /* increment opered count if theyre opered */
1184 if(!(source_p
->umodes
& UMODE_OPER
) && (flag
& UMODE_OPER
))
1187 source_p
->umodes
|= flag
;
1191 if(IsOper(source_p
) && !IsService(source_p
))
1192 rb_dlinkAddAlloc(source_p
, &oper_list
);
1194 SetRemoteClient(source_p
);
1196 if(++Count
.total
> Count
.max_tot
)
1197 Count
.max_tot
= Count
.total
;
1199 source_p
->servptr
= server
;
1201 rb_dlinkAdd(source_p
, &source_p
->lnode
, &source_p
->servptr
->serv
->users
);
1203 /* fake direction */
1204 if(source_p
->servptr
->from
!= source_p
->from
)
1206 struct Client
*target_p
= source_p
->servptr
->from
;
1208 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
1209 "Bad User [%s] :%s USER %s@%s %s, != %s[%s]",
1210 client_p
->name
, source_p
->name
,
1211 source_p
->username
, source_p
->host
,
1212 server
->name
, target_p
->name
, target_p
->from
->name
);
1213 kill_client(client_p
, source_p
,
1214 "%s (NICK from wrong direction (%s != %s))",
1215 me
.name
, server
->name
, target_p
->from
->name
);
1216 source_p
->flags
|= FLAGS_KILLED
;
1217 return exit_client(source_p
, source_p
, &me
, "USER server wrong direction");
1220 call_hook(h_new_remote_user
, source_p
);
1222 return (introduce_client(client_p
, source_p
, user
, nick
, parc
== 12));
1225 /* Check if we can do SAVE. target_p can be a client to save or a
1226 * server introducing a client -- jilles */
1228 can_save(struct Client
*target_p
)
1230 struct Client
*serv_p
;
1232 if (MyClient(target_p
))
1234 if (!has_id(target_p
))
1236 serv_p
= IsServer(target_p
) ? target_p
: target_p
->servptr
;
1237 while (serv_p
!= NULL
&& serv_p
!= &me
)
1239 if (!(serv_p
->serv
->caps
& CAP_SAVE
))
1241 serv_p
= serv_p
->servptr
;
1243 return serv_p
== &me
;
1247 save_user(struct Client
*client_p
, struct Client
*source_p
,
1248 struct Client
*target_p
)
1250 if (!MyConnect(target_p
) && (!has_id(target_p
) || !IsCapable(target_p
->from
, CAP_SAVE
)))
1252 /* This shouldn't happen */
1253 /* Note we only need SAVE support in this direction */
1254 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1255 "Killed %s!%s@%s for nick collision detected by %s (%s does not support SAVE)",
1256 target_p
->name
, target_p
->username
, target_p
->host
, source_p
->name
, target_p
->from
->name
);
1257 kill_client_serv_butone(NULL
, target_p
, "%s (Nick collision (no SAVE support))", me
.name
);
1258 ServerStats
.is_kill
++;
1260 target_p
->flags
|= FLAGS_KILLED
;
1261 (void) exit_client(NULL
, target_p
, &me
, "Nick collision (no SAVE support)");
1264 sendto_server(client_p
, NULL
, CAP_SAVE
|CAP_TS6
, NOCAPS
, ":%s SAVE %s %ld",
1265 source_p
->id
, target_p
->id
, (long)target_p
->tsinfo
);
1266 sendto_server(client_p
, NULL
, CAP_TS6
, CAP_SAVE
, ":%s NICK %s :%ld",
1267 target_p
->id
, target_p
->id
, (long)SAVE_NICKTS
);
1268 if (!IsMe(client_p
))
1269 sendto_realops_snomask(SNO_SKILL
, L_ALL
,
1270 "Received SAVE message for %s from %s",
1271 target_p
->name
, source_p
->name
);
1272 if (MyClient(target_p
))
1274 sendto_one_numeric(target_p
, RPL_SAVENICK
,
1275 form_str(RPL_SAVENICK
), target_p
->id
);
1276 change_local_nick(target_p
, target_p
, target_p
->id
, 0);
1277 target_p
->tsinfo
= SAVE_NICKTS
;
1280 change_remote_nick(target_p
, target_p
, SAVE_NICKTS
, target_p
->id
, 0);
1283 static void bad_nickname(struct Client
*client_p
, const char *nick
)
1285 char squitreason
[100];
1287 sendto_wallops_flags(UMODE_WALLOP
, &me
,
1288 "Squitting %s because of bad nickname %s (NICKLEN mismatch?)",
1289 client_p
->name
, nick
);
1290 sendto_server(NULL
, NULL
, CAP_TS6
, NOCAPS
,
1291 ":%s WALLOPS :Squitting %s because of bad nickname %s (NICKLEN mismatch?)",
1292 me
.id
, client_p
->name
, nick
);
1293 ilog(L_SERVER
, "Link %s cancelled, bad nickname %s sent (NICKLEN mismatch?)",
1294 client_p
->name
, nick
);
1296 rb_snprintf(squitreason
, sizeof squitreason
,
1297 "Bad nickname introduced [%s]", nick
);
1298 exit_client(client_p
, client_p
, &me
, squitreason
);