+/**
+ * Register client in My_Whowas structure for further recall by WHOWAS.
+ * Note: Only clients that have been connected at least 30 seconds will be
+ * registered to prevent automated IRC bots to "destroy" a nice server
+ * history database.
+ */
+GLOBAL void
+Client_RegisterWhowas( CLIENT *Client )
+{
+ int slot;
+ time_t now;
+
+ assert( Client != NULL );
+
+ /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
+ if (Conf_MorePrivacy)
+ return;
+
+ now = time(NULL);
+ /* Don't register clients that were connected less than 30 seconds. */
+ if( now - Client->starttime < 30 )
+ return;
+
+ slot = Last_Whowas + 1;
+ if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
+
+#ifdef DEBUG
+ Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
+#endif
+
+ My_Whowas[slot].time = now;
+ strlcpy( My_Whowas[slot].id, Client_ID( Client ),
+ sizeof( My_Whowas[slot].id ));
+ strlcpy( My_Whowas[slot].user, Client_User( Client ),
+ sizeof( My_Whowas[slot].user ));
+ strlcpy( My_Whowas[slot].host, Client_HostnameDisplayed( Client ),
+ sizeof( My_Whowas[slot].host ));
+ strlcpy( My_Whowas[slot].info, Client_Info( Client ),
+ sizeof( My_Whowas[slot].info ));
+ strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
+ sizeof( My_Whowas[slot].server ));
+
+ Last_Whowas = slot;
+} /* Client_RegisterWhowas */
+
+
+GLOBAL const char *
+Client_TypeText(CLIENT *Client)
+{
+ assert(Client != NULL);
+ switch (Client_Type(Client)) {
+ case CLIENT_USER:
+ return "User";
+ break;
+ case CLIENT_SERVICE:
+ return "Service";
+ break;
+ case CLIENT_SERVER:
+ return "Server";
+ break;
+ default:
+ return "Client";
+ }
+} /* Client_TypeText */
+
+
+/**
+ * Destroy user or service client.
+ */
+static void
+Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
+{
+ if(Client->conn_id != NONE) {
+ /* Local (directly connected) client */
+ Log(LOG_NOTICE,
+ "%s \"%s\" unregistered (connection %d): %s",
+ Client_TypeText(Client), Client_Mask(Client),
+ Client->conn_id, Txt);
+ Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
+ Client_ID(Client), Client_User(Client),
+ Client_Hostname(Client), Txt);
+
+ if (SendQuit) {
+ /* Inforam all the other servers */
+ if (FwdMsg)
+ IRC_WriteStrServersPrefix(NULL,
+ Client, "QUIT :%s", FwdMsg );
+ else
+ IRC_WriteStrServersPrefix(NULL,
+ Client, "QUIT :");
+ }
+ } else {
+ /* Remote client */
+ LogDebug("%s \"%s\" unregistered: %s",
+ Client_TypeText(Client), Client_Mask(Client), Txt);
+
+ if(SendQuit) {
+ /* Inform all the other servers, but the ones in the
+ * direction we got the QUIT from */
+ if(FwdMsg)
+ IRC_WriteStrServersPrefix(Client_NextHop(Client),
+ Client, "QUIT :%s", FwdMsg );
+ else
+ IRC_WriteStrServersPrefix(Client_NextHop(Client),
+ Client, "QUIT :" );
+ }
+ }
+
+ /* Unregister client from channels */
+ Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
+
+ /* Register client in My_Whowas structure */
+ Client_RegisterWhowas(Client);
+} /* Destroy_UserOrService */
+
+
+/**
+ * Introduce a new user or service client to a remote server.
+ *
+ * This function differentiates between RFC1459 and RFC2813 server links and
+ * generates the appropriate commands to register the new user or service.
+ *
+ * @param To The remote server to inform.
+ * @param Prefix Prefix for the generated commands.
+ * @param data CLIENT structure of the new client.
+ */
+static void
+cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
+{
+ CLIENT *c = (CLIENT *)data;
+ CONN_ID conn;
+ char *modes, *user, *host;
+
+ modes = Client_Modes(c);
+ user = Client_User(c) ? Client_User(c) : "-";
+ host = Client_Hostname(c) ? Client_Hostname(c) : "-";
+
+ conn = Client_Conn(To);
+ if (Conn_Options(conn) & CONN_RFC1459) {
+ /* RFC 1459 mode: separate NICK and USER commands */
+ Conn_WriteStr(conn, "NICK %s :%d", Client_ID(c),
+ Client_Hops(c) + 1);
+ Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
+ Client_ID(c), user, host,
+ Client_ID(Client_Introducer(c)), Client_Info(c));
+ if (modes[0])
+ Conn_WriteStr(conn, ":%s MODE %s +%s",
+ Client_ID(c), Client_ID(c), modes);
+ } else {
+ /* RFC 2813 mode: one combined NICK or SERVICE command */
+ if (Client_Type(c) == CLIENT_SERVICE
+ && strchr(Client_Flags(To), 'S'))
+ IRC_WriteStrClientPrefix(To, Prefix,
+ "SERVICE %s %d * +%s %d :%s",
+ Client_Mask(c),
+ Client_MyToken(Client_Introducer(c)),
+ Client_Modes(c), Client_Hops(c) + 1,
+ Client_Info(c));
+ else
+ IRC_WriteStrClientPrefix(To, Prefix,
+ "NICK %s %d %s %s %d +%s :%s",
+ Client_ID(c), Client_Hops(c) + 1,
+ user, host,
+ Client_MyToken(Client_Introducer(c)),
+ modes, Client_Info(c));
+ }
+} /* cb_introduceClient */
+
+
+#ifdef DEBUG
+
+GLOBAL void
+Client_DebugDump(void)
+{
+ CLIENT *c;
+
+ Log(LOG_DEBUG, "Client status:");
+ c = My_Clients;
+ while (c) {
+ Log(LOG_DEBUG,
+ " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
+ Client_ID(c), Client_Type(c), Client_Hostname(c),
+ Client_User(c), Client_Conn(c), Client_StartTime(c),
+ Client_Flags(c));
+ c = (CLIENT *)c->next;
+ }
+} /* Client_DumpClients */
+
+#endif
+
+