]>
jfr.im git - irc/quakenet/snircd.git/blob - ircd/m_nick.c
2 * IRC - Internet Relay Chat, ircd/m_nick.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
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 1, or (at your option)
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., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * $Id: m_nick.c,v 1.25.2.1 2005/11/16 04:18:47 entrope Exp $
27 * m_functions execute protocol messages on this server:
29 * cptr is always NON-NULL, pointing to a *LOCAL* client
30 * structure (with an open socket connected!). This
31 * identifies the physical socket where the message
32 * originated (or which caused the m_function to be
33 * executed--some m_functions may call others...).
35 * sptr is the source of the message, defined by the
36 * prefix part of the message if present. If not
37 * or prefix not found, then sptr==cptr.
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
43 * (sptr == cptr) => the message didn't
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
59 * *Always* true (if 'parse' and others are working correct):
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
63 * 2) MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64 * *cannot* be a local connection, unless it's
65 * actually cptr!). [MyConnect(x) should probably
66 * be defined as (x == x->from) --msa ]
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
71 * parv a NULL terminated list of parameter pointers,
73 * parv[0], sender (prefix string), if not present
74 * this points to an empty string.
75 * parv[1]...parv[parc-1]
76 * pointers to additional parameters
77 * parv[parc] == NULL, *always*
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
88 #include "ircd_chattr.h"
89 #include "ircd_features.h"
91 #include "ircd_reply.h"
92 #include "ircd_string.h"
103 /* #include <assert.h> -- Now using assert in ircd_log.h */
108 * 'do_nick_name' ensures that the given parameter (nick) is really a proper
109 * string for a nickname (note, the 'nick' may be modified in the process...)
111 * RETURNS the length of the final NICKNAME (0, if nickname is invalid)
113 * Nickname characters are in range 'A'..'}', '_', '-', '0'..'9'
114 * anything outside the above set will terminate nickname.
115 * In addition, the first character cannot be '-' or a Digit.
118 * The '~'-character should be allowed, but a change should be global,
119 * some confusion would result if only few servers allowed it...
121 static int do_nick_name(char* nick
)
124 char* end
= ch
+ NICKLEN
;
127 /* first character in [0..9-] */
128 if (*ch
== '-' || IsDigit(*ch
))
130 for ( ; (ch
< end
) && *ch
; ++ch
)
131 if (!IsNickChar(*ch
))
140 * m_nick - message handler for local clients
141 * parv[0] = sender prefix
144 int m_nick(struct Client
* cptr
, struct Client
* sptr
, int parc
, char* parv
[])
146 struct Client
* acptr
;
147 char nick
[NICKLEN
+ 2];
150 const char* client_name
;
153 assert(cptr
== sptr
);
156 * parv[0] will be empty for clients connecting for the first time
158 client_name
= (*(cli_name(sptr
))) ? cli_name(sptr
) : "*";
161 send_reply(sptr
, ERR_NONICKNAMEGIVEN
);
165 * Don't let them send make us send back a really long string of
169 if (strlen(arg
) > IRCD_MIN(NICKLEN
, feature_int(FEAT_NICKLEN
)))
170 arg
[IRCD_MIN(NICKLEN
, feature_int(FEAT_NICKLEN
))] = '\0';
172 if ((s
= strchr(arg
, '~')))
178 * If do_nick_name() returns a null name then reject it.
180 if (0 == do_nick_name(nick
)) {
181 send_reply(sptr
, ERR_ERRONEUSNICKNAME
, arg
);
185 if (IsRegistered(sptr
) && !IsAnOper(sptr
) && IsNickGlined(sptr
, nick
)) {
186 send_reply(sptr
, ERR_ERRONEUSNICKNAME
, nick
);
191 * Check if this is a LOCAL user trying to use a reserved (Juped)
192 * nick, if so tell him that it's a nick in use...
194 if (isNickJuped(nick
)) {
195 send_reply(sptr
, ERR_NICKNAMEINUSE
, nick
);
196 return 0; /* NICK message ignored */
199 if (!(acptr
= FindClient(nick
))) {
201 * No collisions, all clear...
203 return set_nick_name(cptr
, sptr
, nick
, parc
, parv
);
205 if (IsServer(acptr
)) {
206 send_reply(sptr
, ERR_NICKNAMEINUSE
, nick
);
207 return 0; /* NICK message ignored */
210 * If acptr == sptr, then we have a client doing a nick
211 * change between *equivalent* nicknames as far as server
212 * is concerned (user is changing the case of his/her
213 * nickname or somesuch)
217 * If acptr == sptr, then we have a client doing a nick
218 * change between *equivalent* nicknames as far as server
219 * is concerned (user is changing the case of his/her
220 * nickname or somesuch)
222 if (0 != strcmp(cli_name(acptr
), nick
)) {
224 * Allows change of case in his/her nick
226 return set_nick_name(cptr
, sptr
, nick
, parc
, parv
);
229 * This is just ':old NICK old' type thing.
230 * Just forget the whole thing here. There is
231 * no point forwarding it to anywhere,
232 * especially since servers prior to this
233 * version would treat it as nick collision.
238 * Note: From this point forward it can be assumed that
239 * acptr != sptr (point to different client structures).
241 assert(acptr
!= sptr
);
243 * If the older one is "non-person", the new entry is just
244 * allowed to overwrite it. Just silently drop non-person,
245 * and proceed with the nick. This should take care of the
246 * "dormant nick" way of generating collisions...
248 * XXX - hmmm can this happen after one is registered?
250 * Yes, client 1 connects to IRC and registers, client 2 connects and
251 * sends "NICK foo" but doesn't send anything more. client 1 now does
252 * /nick foo, they should succeed and client 2 gets disconnected with
255 if (IsUnknown(acptr
) && MyConnect(acptr
)) {
256 ServerStats
->is_ref
++;
257 IPcheck_connect_fail(acptr
);
258 exit_client(cptr
, acptr
, &me
, "Overridden by other sign on");
259 return set_nick_name(cptr
, sptr
, nick
, parc
, parv
);
262 * NICK is coming from local client connection. Just
263 * send error reply and ignore the command.
265 send_reply(sptr
, ERR_NICKNAMEINUSE
, nick
);
266 return 0; /* NICK message ignored */
271 * ms_nick - server message handler for nicks
272 * parv[0] = sender prefix
275 * If from server, source is client:
276 * parv[2] = timestamp
280 * parv[3] = timestamp
283 * parv[6] = umode (optional)
284 * parv[parc-3] = IP# <- Only Protocol >= 10
285 * parv[parc-2] = YXX, numeric nick <- Only Protocol >= 10
286 * parv[parc-1] = info
289 int ms_nick(struct Client
* cptr
, struct Client
* sptr
, int parc
, char* parv
[])
291 struct Client
*acptr
;
292 char nick
[NICKLEN
+ 2];
299 assert(IsServer(cptr
));
301 if ((IsServer(sptr
) && parc
< 8) || parc
< 3)
303 sendto_opmask_butone(0, SNO_OLDSNO
, "bad NICK param count for %s from %C",
305 return need_more_params(sptr
, "NICK");
308 ircd_strncpy(nick
, parv
[1], NICKLEN
);
309 nick
[NICKLEN
] = '\0';
313 lastnick
= atoi(parv
[3]);
314 if (lastnick
> OLDEST_TS
&& !IsBurstOrBurstAck(sptr
))
315 cli_serv(sptr
)->lag
= TStime() - lastnick
;
319 lastnick
= atoi(parv
[2]);
320 if (lastnick
> OLDEST_TS
&& !IsBurstOrBurstAck(sptr
))
321 cli_serv(cli_user(sptr
)->server
)->lag
= TStime() - lastnick
;
324 * If do_nick_name() returns a null name OR if the server sent a nick
325 * name and do_nick_name() changed it in some way (due to rules of nick
326 * creation) then reject it. If from a server and we reject it,
327 * and KILL it. -avalon 4/4/92
329 if (!do_nick_name(nick
) || strcmp(nick
, parv
[1]))
331 send_reply(sptr
, ERR_ERRONEUSNICKNAME
, parv
[1]);
333 ++ServerStats
->is_kill
;
334 sendto_opmask_butone(0, SNO_OLDSNO
, "Bad Nick: %s From: %s %C", parv
[1],
336 sendcmdto_one(&me
, CMD_KILL
, cptr
, "%s :%s (%s <- %s[%s])",
337 IsServer(sptr
) ? parv
[parc
- 2] : parv
[0], cli_name(&me
), parv
[1],
338 nick
, cli_name(cptr
));
344 sendcmdto_serv_butone(&me
, CMD_KILL
, 0, "%s :%s (%s <- %s!%s@%s)",
345 parv
[0], cli_name(&me
), cli_name(cptr
), parv
[0],
346 cli_user(sptr
) ? cli_username(sptr
) : "",
347 cli_user(sptr
) ? cli_name(cli_user(sptr
)->server
) :
352 /* Check against nick name collisions. */
353 if ((acptr
= FindClient(nick
)) == NULL
)
354 /* No collisions, all clear... */
355 return set_nick_name(cptr
, sptr
, nick
, parc
, parv
);
358 * If acptr == sptr, then we have a client doing a nick
359 * change between *equivalent* nicknames as far as server
360 * is concerned (user is changing the case of his/her
361 * nickname or somesuch)
365 if (strcmp(cli_name(acptr
), nick
) != 0)
366 /* Allows change of case in his/her nick */
367 return set_nick_name(cptr
, sptr
, nick
, parc
, parv
);
369 /* Setting their nick to what it already is? Ignore it. */
372 /* now we know we have a real collision. */
374 * Note: From this point forward it can be assumed that
375 * acptr != sptr (point to different client structures).
377 assert(acptr
!= sptr
);
379 * If the older one is "non-person", the new entry is just
380 * allowed to overwrite it. Just silently drop non-person,
381 * and proceed with the nick. This should take care of the
382 * "dormant nick" way of generating collisions...
384 if (IsUnknown(acptr
) && MyConnect(acptr
))
386 ServerStats
->is_ref
++;
387 IPcheck_connect_fail(acptr
);
388 exit_client(cptr
, acptr
, &me
, "Overridden by other sign on");
389 return set_nick_name(cptr
, sptr
, nick
, parc
, parv
);
392 * Decide, we really have a nick collision and deal with it
395 * NICK was coming from a server connection.
396 * This means we have a race condition (two users signing on
397 * at the same time), or two net fragments reconnecting with the same nick.
398 * The latter can happen because two different users connected
399 * or because one and the same user switched server during a net break.
400 * If the TimeStamps are equal, we kill both (or only 'new'
401 * if it was a ":server NICK new ...").
402 * Otherwise we kill the youngest when user@host differ,
403 * or the oldest when they are the same.
404 * We treat user and ~user as different, because if it wasn't
405 * a faked ~user the AUTH wouldn't have added the '~'.
411 struct irc_in_addr ip
;
413 * A new NICK being introduced by a neighbouring
414 * server (e.g. message type ":server NICK new ..." received)
416 * compare IP address and username
418 base64toip(parv
[parc
- 3], &ip
);
419 differ
= (0 != memcmp(&cli_ip(acptr
), &ip
, sizeof(cli_ip(acptr
)))) ||
420 (0 != ircd_strcmp(cli_user(acptr
)->username
, parv
[4]));
421 sendto_opmask_butone(0, SNO_OLDSNO
, "Nick collision on %C (%C %Tu <- "
422 "%C %Tu (%s user@host))", acptr
, cli_from(acptr
),
423 cli_lastnick(acptr
), cptr
, lastnick
,
424 differ
? "Different" : "Same");
429 * A NICK change has collided (e.g. message type ":old NICK new").
431 * compare IP address and username
433 differ
= (0 != memcmp(&cli_ip(acptr
), &cli_ip(sptr
), sizeof(cli_ip(acptr
)))) ||
434 (0 != ircd_strcmp(cli_user(acptr
)->username
, cli_user(sptr
)->username
));
435 sendto_opmask_butone(0, SNO_OLDSNO
, "Nick change collision from %C to "
436 "%C (%C %Tu <- %C %Tu)", sptr
, acptr
, cli_from(acptr
),
437 cli_lastnick(acptr
), cptr
, lastnick
);
439 type
= differ
? "overruled by older nick" : "nick collision from same user@host";
441 * Now remove (kill) the nick on our side if it is the youngest.
442 * If no timestamp was received, we ignore the incoming nick
443 * (and expect a KILL for our legit nick soon ):
444 * When the timestamps are equal we kill both nicks. --Run
445 * acptr->from != cptr should *always* be true (?).
447 * This exits the client sending the NICK message
449 if ((differ
&& lastnick
>= cli_lastnick(acptr
)) ||
450 (!differ
&& lastnick
<= cli_lastnick(acptr
)))
452 ServerStats
->is_kill
++;
455 /* If this was a nick change and not a nick introduction, we
456 * need to ensure that we remove our record of the client, and
457 * send a KILL to the whole network.
459 assert(!MyConnect(sptr
));
460 /* Inform the rest of the net... */
461 sendcmdto_serv_butone(&me
, CMD_KILL
, 0, "%C :%s (%s)",
462 sptr
, cli_name(&me
), type
);
463 /* Don't go sending off a QUIT message... */
464 SetFlag(sptr
, FLAG_KILLED
);
465 /* Remove them locally. */
466 exit_client_msg(cptr
, sptr
, &me
,
468 feature_str(FEAT_HIS_SERVERNAME
), type
);
472 /* If the origin is a server, this was a new client, so we only
473 * send the KILL in the direction it came from. We have no
474 * client record that we would have to clean up.
476 sendcmdto_one(&me
, CMD_KILL
, cptr
, "%s :%s (%s)",
477 parv
[parc
- 2], cli_name(&me
), type
);
479 /* If the timestamps differ and we just killed sptr, we don't need to kill
482 if (lastnick
!= cli_lastnick(acptr
))
485 /* Tell acptr why we are killing it. */
486 send_reply(acptr
, ERR_NICKCOLLISION
, nick
);
488 ServerStats
->is_kill
++;
489 SetFlag(acptr
, FLAG_KILLED
);
491 * This exits the client we had before getting the NICK message
493 sendcmdto_serv_butone(&me
, CMD_KILL
, NULL
, "%C :%s (%s)",
494 acptr
, feature_str(FEAT_HIS_SERVERNAME
),
496 exit_client_msg(cptr
, acptr
, &me
, "Killed (%s (%s))",
497 feature_str(FEAT_HIS_SERVERNAME
), type
);
498 if (lastnick
== cli_lastnick(acptr
))
502 return set_nick_name(cptr
, sptr
, nick
, parc
, parv
);