]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/irc-login.c
IRC_NICK(): Code cleanup, new function Change_Nick()
[ngircd-alex.git] / src / ngircd / irc-login.c
index 9e1abdd59e8b0349b535b9b1299989f994a3827c..c3f32b7e8b2516fe383b6482341817ce668269a6 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "imp.h"
 #include <assert.h>
+#include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
 #include <strings.h>
 #include "exp.h"
 #include "irc-login.h"
 
-static void Kill_Nick PARAMS(( char *Nick, char *Reason ));
+static void Kill_Nick PARAMS((char *Nick, char *Reason));
+static void Change_Nick PARAMS((CLIENT * Origin, CLIENT * Target,
+                               char *NewNick, bool RegisterWhowas));
+
 
 /**
  * Handler for the IRC "PASS" command.
@@ -274,40 +278,8 @@ IRC_NICK( CLIENT *Client, REQUEST *Req )
                                Client_SetType( Client, CLIENT_GOTNICK );
                } else {
                        /* Nickname change */
-                       if (Client_Conn(target) > NONE) {
-                               /* Local client */
-                               Log(LOG_INFO,
-                                   "%s \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
-                                   Client_TypeText(target), Client_Mask(target),
-                                   Client_Conn(target), Client_ID(target),
-                                   Req->argv[0]);
-                               Conn_UpdateIdle(Client_Conn(target));
-                       } else {
-                               /* Remote client */
-                               LogDebug("%s \"%s\" changed nick: \"%s\" -> \"%s\".",
-                                        Client_TypeText(target),
-                                        Client_Mask(target), Client_ID(target),
-                                        Req->argv[0]);
-                       }
-
-                       /* Inform all users and servers (which have to know)
-                        * of this nickname change */
-                       if( Client_Type( Client ) == CLIENT_USER )
-                               IRC_WriteStrClientPrefix( Client, Client,
-                                                         "NICK :%s",
-                                                         Req->argv[0] );
-                       IRC_WriteStrServersPrefix( Client, target,
-                                                  "NICK :%s", Req->argv[0] );
-                       IRC_WriteStrRelatedPrefix( target, target, false,
-                                                  "NICK :%s", Req->argv[0] );
-
-                       /* Register old nickname for WHOWAS queries */
-                       Client_RegisterWhowas( target );
-
-                       /* Save new nickname */
-                       Client_SetID( target, Req->argv[0] );
-
-                       IRC_SetPenalty( target, 2 );
+                       Change_Nick(Client, target, Req->argv[0], true);
+                       IRC_SetPenalty(target, 2);
                }
 
                return CONNECTED;
@@ -418,12 +390,13 @@ IRC_USER(CLIENT * Client, REQUEST * Req)
                                                  Client_ID(Client),
                                                  Req->command);
 
-               /* User name: only alphanumeric characters are allowed! */
+               /* User name: only alphanumeric characters and limited
+                  punctuation is allowed.*/
                ptr = Req->argv[0];
                while (*ptr) {
-                       if ((*ptr < '0' || *ptr > '9') &&
-                           (*ptr < 'A' || *ptr > 'Z') &&
-                           (*ptr < 'a' || *ptr > 'z')) {
+                       if (!isalnum(*ptr) &&
+                           *ptr != '+' && *ptr != '-' &&
+                           *ptr != '.' && *ptr != '_') {
                                Conn_Close(Client_Conn(Client), NULL,
                                           "Invalid user name", true);
                                return DISCONNECTED;
@@ -544,10 +517,10 @@ IRC_SERVICE(CLIENT *Client, REQUEST *Req)
        hops = atoi(Req->argv[4]);
        info = Req->argv[5];
 
-       /* Validate service name ("nick name") */
+       /* Validate service name ("nickname") */
        c = Client_Search(nick);
        if(c) {
-               /* Nick name collission: disconnect (KILL) both clients! */
+               /* Nickname collission: disconnect (KILL) both clients! */
                Log(LOG_ERR, "Server %s introduces already registered service \"%s\"!",
                    Client_ID(Client), nick);
                Kill_Nick(nick, "Nick collision");
@@ -898,9 +871,9 @@ IRC_PONG(CLIENT *Client, REQUEST *Req)
 
 
 /**
- * Kill all users with a specific nick name in the network.
+ * Kill all users with a specific nickname in the network.
  *
- * @param Nick         Nick name.
+ * @param Nick         Nickname.
  * @param Reason       Reason for the KILL.
  */
 static void
@@ -923,4 +896,43 @@ Kill_Nick(char *Nick, char *Reason)
 } /* Kill_Nick */
 
 
+/**
+ * Change the nickname of a client.
+ *
+ * @param Origin The client which caused the nickname change.
+ * @param Target The client of which the nickname should be changed.
+ * @param NewNick The new nickname.
+ */
+static void
+Change_Nick(CLIENT *Origin, CLIENT *Target, char *NewNick, bool RegisterWhowas)
+{
+       if (Client_Conn(Target) > NONE) {
+               /* Local client */
+               Log(LOG_INFO,
+                   "%s \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
+                   Client_TypeText(Target), Client_Mask(Target),
+                   Client_Conn(Target), Client_ID(Target), NewNick);
+               Conn_UpdateIdle(Client_Conn(Target));
+       } else {
+               /* Remote client */
+               LogDebug("%s \"%s\" changed nick: \"%s\" -> \"%s\".",
+                        Client_TypeText(Target),
+                        Client_Mask(Target), Client_ID(Target), NewNick);
+       }
+
+       /* Inform all servers and users (which have to know) of the new name */
+       if (Client_Type(Origin) == CLIENT_USER)
+               IRC_WriteStrClientPrefix(Origin, Origin, "NICK :%s", NewNick);
+       IRC_WriteStrServersPrefix(Origin, Target, "NICK :%s", NewNick);
+       IRC_WriteStrRelatedPrefix(Target, Target, false, "NICK :%s", NewNick);
+
+       /* Register old nickname for WHOWAS queries, if required */
+       if (RegisterWhowas)
+               Client_RegisterWhowas(Target);
+
+       /* Save new nickname */
+       Client_SetID(Target, NewNick);
+}
+
+
 /* -eof- */