+GLOBAL char *
+Client_Info( CLIENT *Client )
+{
+ assert( Client != NULL );
+ return Client->info;
+} /* Client_Info */
+
+
+GLOBAL char *
+Client_User( CLIENT *Client )
+{
+ assert( Client != NULL );
+ return Client->user[0] ? Client->user : "~";
+} /* Client_User */
+
+
+#ifdef PAM
+
+/**
+ * Get the "original" user name as supplied by the USER command.
+ * The user name as given by the client is used for authentication instead
+ * of the one detected using IDENT requests.
+ * @param Client The client.
+ * @return Original user name.
+ */
+GLOBAL char *
+Client_OrigUser(CLIENT *Client) {
+#ifndef IDENTAUTH
+ char *user = Client->user;
+
+ if (user[0] == '~')
+ user++;
+ return user;
+#else
+ return Client->orig_user;
+#endif
+} /* Client_OrigUser */
+
+#endif
+
+
+/**
+ * Return the hostname of a client.
+ * @param Client Pointer to client structure
+ * @return Pointer to client hostname
+ */
+GLOBAL char *
+Client_Hostname(CLIENT *Client)
+{
+ assert (Client != NULL);
+ return Client->host;
+} /* Client_Hostname */
+
+
+/**
+ * Get potentially cloaked hostname of a client.
+ *
+ * If the client has not enabled cloaking, the real hostname is used.
+ * Please note that this function uses a global static buffer, so you can't
+ * nest invocations without overwriting earlier results!
+ *
+ * @param Client Pointer to client structure
+ * @return Pointer to client hostname
+ */
+GLOBAL char *
+Client_HostnameCloaked(CLIENT *Client)
+{
+ static char Cloak_Buffer[CLIENT_HOST_LEN];
+
+ assert(Client != NULL);
+
+ if (!Client_HasMode(Client, 'x'))
+ return Client_Hostname(Client);
+
+ /* Do simple mapping to the server ID? */
+ if (!*Conf_CloakHostModeX)
+ return Client_ID(Client->introducer);
+
+ strlcpy(Cloak_Buffer, Client->host, CLIENT_HOST_LEN);
+ strlcat(Cloak_Buffer, Conf_CloakHostSalt, CLIENT_HOST_LEN);
+
+ snprintf(Cloak_Buffer, CLIENT_HOST_LEN, Conf_CloakHostModeX,
+ Hash(Cloak_Buffer));
+
+ return Cloak_Buffer;
+} /* Client_HostnameCloaked */
+
+
+GLOBAL char *
+Client_Modes( CLIENT *Client )
+{
+ assert( Client != NULL );
+ return Client->modes;
+} /* Client_Modes */
+
+
+GLOBAL char *
+Client_Flags( CLIENT *Client )
+{
+ assert( Client != NULL );
+ return Client->flags;
+} /* Client_Flags */
+
+
+GLOBAL bool
+Client_OperByMe( CLIENT *Client )
+{
+ assert( Client != NULL );
+ return Client->oper_by_me;
+} /* Client_OperByMe */
+
+
+GLOBAL int
+Client_Hops( CLIENT *Client )
+{
+ assert( Client != NULL );
+ return Client->hops;
+} /* Client_Hops */
+
+
+GLOBAL int
+Client_Token( CLIENT *Client )
+{
+ assert( Client != NULL );
+ return Client->token;
+} /* Client_Token */
+
+
+GLOBAL int
+Client_MyToken( CLIENT *Client )
+{
+ assert( Client != NULL );
+ return Client->mytoken;
+} /* Client_MyToken */
+
+
+GLOBAL CLIENT *
+Client_NextHop( CLIENT *Client )
+{
+ CLIENT *c;
+
+ assert( Client != NULL );
+
+ c = Client;
+ while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
+ c = c->introducer;
+
+ return c;
+} /* Client_NextHop */
+
+
+/**
+ * Return ID of a client: "client!user@host"
+ * This client ID is used for IRC prefixes, for example.
+ * Please note that this function uses a global static buffer, so you can't
+ * nest invocations without overwriting earlier results!
+ * @param Client Pointer to client structure
+ * @return Pointer to global buffer containing the client ID
+ */
+GLOBAL char *
+Client_Mask( CLIENT *Client )
+{
+ static char Mask_Buffer[GETID_LEN];
+
+ assert (Client != NULL);
+
+ /* Servers: return name only, there is no "mask" */
+ if (Client->type == CLIENT_SERVER)
+ return Client->id;
+
+ snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
+ Client->id, Client->user, Client->host);
+ return Mask_Buffer;
+} /* Client_Mask */
+
+
+/**
+ * Return ID of a client with cloaked hostname: "client!user@server-name"
+ *
+ * This client ID is used for IRC prefixes, for example.
+ * Please note that this function uses a global static buffer, so you can't
+ * nest invocations without overwriting earlier results!
+ * If the client has not enabled cloaking, the real hostname is used.
+ *
+ * @param Client Pointer to client structure
+ * @return Pointer to global buffer containing the client ID
+ */
+GLOBAL char *
+Client_MaskCloaked(CLIENT *Client)
+{
+ static char Mask_Buffer[GETID_LEN];
+
+ assert (Client != NULL);
+
+ /* Is the client using cloaking at all? */
+ if (!Client_HasMode(Client, 'x'))
+ return Client_Mask(Client);
+
+ snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user,
+ Client_HostnameCloaked(Client));
+
+ return Mask_Buffer;
+} /* Client_MaskCloaked */
+
+
+GLOBAL CLIENT *
+Client_Introducer( CLIENT *Client )
+{
+ assert( Client != NULL );
+ return Client->introducer;
+} /* Client_Introducer */
+
+
+GLOBAL CLIENT *
+Client_TopServer( CLIENT *Client )
+{
+ assert( Client != NULL );
+ return Client->topserver;
+} /* Client_TopServer */
+
+
+GLOBAL bool
+Client_HasMode( CLIENT *Client, char Mode )
+{
+ assert( Client != NULL );
+ return strchr( Client->modes, Mode ) != NULL;
+} /* Client_HasMode */
+
+
+GLOBAL char *
+Client_Away( CLIENT *Client )
+{
+ assert( Client != NULL );
+ return Client->away;
+} /* Client_Away */
+
+
+/**
+ * Make sure that a given nickname is valid.
+ *
+ * If the nickname is not valid for the given client, this function sends back
+ * the appropriate error messages.
+ *
+ * @param Client Client that wants to change the nickname.
+ * @param Nick New nick name.
+ * @returns true if nickname is valid, false otherwise.
+ */
+GLOBAL bool
+Client_CheckNick(CLIENT *Client, char *Nick)
+{
+ assert(Client != NULL);
+ assert(Nick != NULL);
+
+ if (!Client_IsValidNick(Nick)) {
+ if (strlen(Nick ) >= Conf_MaxNickLength)
+ IRC_WriteStrClient(Client, ERR_NICKNAMETOOLONG_MSG,
+ Client_ID(Client), Nick,
+ Conf_MaxNickLength - 1);
+ else
+ IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
+ Client_ID(Client), Nick);
+ return false;
+ }
+
+ if (Client_Type(Client) != CLIENT_SERVER
+ && Client_Type(Client) != CLIENT_SERVICE) {
+ /* Make sure that this isn't a restricted/forbidden nick name */
+ if (Conf_NickIsBlocked(Nick)) {
+ IRC_WriteStrClient(Client, ERR_FORBIDDENNICKNAME_MSG,
+ Client_ID(Client), Nick);
+ return false;
+ }
+ }
+
+ /* Nickname already registered? */
+ if (Client_Search(Nick)) {
+ IRC_WriteStrClient(Client, ERR_NICKNAMEINUSE_MSG,
+ Client_ID(Client), Nick);
+ return false;
+ }
+
+ return true;
+} /* Client_CheckNick */
+
+
+GLOBAL bool
+Client_CheckID( CLIENT *Client, char *ID )
+{
+ char str[COMMAND_LEN];
+ CLIENT *c;
+
+ assert( Client != NULL );
+ assert( Client->conn_id > NONE );
+ assert( ID != NULL );
+
+ /* ID too long? */
+ if (strlen(ID) > CLIENT_ID_LEN) {
+ IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID(Client), ID);
+ return false;
+ }
+
+ /* ID already in use? */
+ c = My_Clients;
+ while (c) {
+ if (strcasecmp(c->id, ID) == 0) {
+ snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
+ if (c->conn_id != NONE)
+ Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
+ else
+ Log(LOG_ERR, "%s (via network)!", str);
+ Conn_Close(Client->conn_id, str, str, true);
+ return false;
+ }
+ c = (CLIENT *)c->next;
+ }
+
+ return true;
+} /* Client_CheckID */
+
+
+GLOBAL CLIENT *
+Client_First( void )
+{
+ return My_Clients;
+} /* Client_First */
+
+
+GLOBAL CLIENT *
+Client_Next( CLIENT *c )
+{
+ assert( c != NULL );
+ return (CLIENT *)c->next;
+} /* Client_Next */
+
+
+GLOBAL long
+Client_UserCount( void )
+{
+ return Count( CLIENT_USER );
+} /* Client_UserCount */
+
+
+GLOBAL long
+Client_ServiceCount( void )
+{
+ return Count( CLIENT_SERVICE );;
+} /* Client_ServiceCount */
+
+
+GLOBAL long
+Client_ServerCount( void )
+{
+ return Count( CLIENT_SERVER );
+} /* Client_ServerCount */
+
+
+GLOBAL long
+Client_MyUserCount( void )
+{
+ return MyCount( CLIENT_USER );
+} /* Client_MyUserCount */
+
+
+GLOBAL long
+Client_MyServiceCount( void )
+{
+ return MyCount( CLIENT_SERVICE );
+} /* Client_MyServiceCount */
+
+
+GLOBAL unsigned long
+Client_MyServerCount( void )
+{
+ CLIENT *c;
+ unsigned long cnt = 0;
+
+ c = My_Clients;
+ while( c )
+ {
+ if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
+ c = (CLIENT *)c->next;
+ }
+ return cnt;
+} /* Client_MyServerCount */
+
+
+GLOBAL unsigned long
+Client_OperCount( void )
+{
+ CLIENT *c;
+ unsigned long cnt = 0;
+
+ c = My_Clients;
+ while( c )
+ {
+ if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
+ c = (CLIENT *)c->next;
+ }
+ return cnt;
+} /* Client_OperCount */
+
+
+GLOBAL unsigned long
+Client_UnknownCount( void )
+{
+ CLIENT *c;
+ unsigned long cnt = 0;
+
+ c = My_Clients;
+ while( c )
+ {
+ if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
+ c = (CLIENT *)c->next;
+ }
+
+ return cnt;
+} /* Client_UnknownCount */
+
+
+GLOBAL long
+Client_MaxUserCount( void )
+{
+ return Max_Users;
+} /* Client_MaxUserCount */
+
+
+GLOBAL long
+Client_MyMaxUserCount( void )
+{
+ return My_Max_Users;
+} /* Client_MyMaxUserCount */
+
+
+/**
+ * Check that a given nickname is valid.
+ *
+ * @param Nick the nickname to check.
+ * @returns true if nickname is valid, false otherwise.
+ */
+GLOBAL bool
+Client_IsValidNick(const char *Nick)
+{
+ const char *ptr;
+ static const char goodchars[] = ";0123456789-";
+
+ assert (Nick != NULL);
+
+ if (strchr(goodchars, Nick[0]))
+ return false;
+ if (strlen(Nick ) >= Conf_MaxNickLength)
+ return false;
+
+ ptr = Nick;
+ while (*ptr) {
+ if (*ptr < 'A' && !strchr(goodchars, *ptr ))
+ return false;
+ if (*ptr > '}')
+ return false;
+ ptr++;
+ }
+
+ return true;
+} /* Client_IsValidNick */
+
+
+/**
+ * Return pointer to "My_Whowas" structure.
+ */
+GLOBAL WHOWAS *
+Client_GetWhowas( void )
+{
+ return My_Whowas;
+} /* Client_GetWhowas */
+
+/**
+ * Return the index of the last used WHOWAS entry.
+ */
+GLOBAL int
+Client_GetLastWhowasIndex( void )
+{
+ return Last_Whowas;
+} /* Client_GetLastWhowasIndex */
+
+
+/**
+ * Get the start time of this client.
+ * The result is the start time in seconds since 1970-01-01, as reported
+ * by the C function time(NULL).
+ */
+GLOBAL time_t
+Client_StartTime(CLIENT *Client)
+{
+ assert( Client != NULL );
+ return Client->starttime;
+} /* Client_Uptime */
+
+
+/**
+ * Reject a client when logging in.
+ *
+ * This function is called when a client isn't allowed to connect to this
+ * server. Possible reasons are bad server password, bad PAM password,
+ * or that the client is G/K-Line'd.
+ *
+ * After calling this function, the client isn't connected any more.
+ *
+ * @param Client The client to reject.
+ * @param Reason The reason why the client has been rejected.
+ * @param InformClient If true, send the exact reason to the client.
+ */
+GLOBAL void
+Client_Reject(CLIENT *Client, const char *Reason, bool InformClient)
+{
+ char info[COMMAND_LEN];
+
+ assert(Client != NULL);
+ assert(Reason != NULL);
+
+ if (InformClient)
+ snprintf(info, sizeof(info), "Access denied: %s", Reason);
+ else
+ strcpy(info, "Access denied: Bad password?");
+
+ Log(LOG_ERR,
+ "User \"%s\" rejected (connection %d): %s!",
+ Client_Mask(Client), Client_Conn(Client), Reason);
+ Conn_Close(Client_Conn(Client), Reason, info, true);
+}
+
+
+/**
+ * Introduce a new user or service client in the network.
+ *
+ * @param From Remote server introducing the client or NULL (local).
+ * @param Client New client.
+ * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
+ */
+GLOBAL void
+Client_Introduce(CLIENT *From, CLIENT *Client, int Type)
+{
+ /* Set client type (user or service) */
+ Client_SetType(Client, Type);
+
+ if (From) {
+ if (Conf_NickIsService(Conf_GetServer(Client_Conn(From)),
+ Client_ID(Client)))
+ Client_SetType(Client, CLIENT_SERVICE);
+ LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
+ Client_TypeText(Client), Client_Mask(Client),
+ Client_Modes(Client), Client_ID(From),
+ Client_ID(Client_Introducer(Client)),
+ Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
+ } else {
+ Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
+ Client_TypeText(Client), Client_Mask(Client),
+ Client_Conn(Client));
+ Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
+ Client_ID(Client), Client_User(Client),
+ Client_Hostname(Client),
+ Conn_IPA(Client_Conn(Client)),
+ Client_TypeText(Client));
+ }
+
+ /* Inform other servers */
+ IRC_WriteStrServersPrefixFlag_CB(From,
+ From != NULL ? From : Client_ThisServer(),
+ '\0', cb_introduceClient, (void *)Client);
+} /* Client_Introduce */
+
+
+static unsigned long
+Count( CLIENT_TYPE Type )
+{
+ CLIENT *c;
+ unsigned long cnt = 0;
+
+ c = My_Clients;
+ while( c )
+ {
+ if( c->type == Type ) cnt++;
+ c = (CLIENT *)c->next;
+ }
+ return cnt;
+} /* Count */
+
+
+static unsigned long
+MyCount( CLIENT_TYPE Type )
+{
+ CLIENT *c;
+ unsigned long cnt = 0;
+
+ c = My_Clients;
+ while( c )
+ {
+ if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
+ c = (CLIENT *)c->next;
+ }
+ return cnt;
+} /* MyCount */
+
+
+static CLIENT *
+New_Client_Struct( void )