repo: ngircd
action: commit
revision: 
path_from: 
revision_from: 0ad0fe207ab1705a2b042e7f47f1e0d8ce46e2a9:
path_to: 
revision_to: 
git.thebackupbox.net
ngircd
git clone git://git.thebackupbox.net/ngircd
commit 0ad0fe207ab1705a2b042e7f47f1e0d8ce46e2a9
Author: Alexander Barton 
Date:   Tue Feb 5 13:04:11 2013 +0100

    Implement new function Client_SearchServer()

    This function returns the server structure of a client or a given "mask";
    it is useful for implemention handlers for commands like "COMMAND *.net",
    which should work on a server matching "*.net".

    Please note that the local server is always returned when it matches the
    mask, but besides that, the order is completely arbitrary.

diff --git a/src/ngircd/client.c b/src/ngircd/client.c
index d10775a88fad4a39ecb363dc7b6c433b5e497306..
index ..37b168ff8d5dac07cb835b86eb37b366da6fcbe3 100644
--- a/src/ngircd/client.c
+++ b/src/ngircd/client.c
@@ -41,6 +41,7 @@
 #include "hash.h"
 #include "irc-write.h"
 #include "log.h"
+#include "match.h"
 #include "messages.h"

 #include 
@@ -556,13 +557,14 @@ Client_ModeDel( CLIENT *Client, char Mode )
 } /* Client_ModeDel */


+/**
+ * Search CLIENT structure of a given nick name.
+ *
+ * @return Pointer to CLIENT structure or NULL if not found.
+ */
 GLOBAL CLIENT *
 Client_Search( const char *Nick )
 {
-	/* return Client-Structure that has the corresponding Nick.
-	 * If none is found, return NULL.
-	 */
-
 	char search_id[CLIENT_ID_LEN], *ptr;
 	CLIENT *c = NULL;
 	UINT32 search_hash;
@@ -583,7 +585,39 @@ Client_Search( const char *Nick )
 		c = (CLIENT *)c->next;
 	}
 	return NULL;
-} /* Client_Search */
+}
+
+
+/**
+ * Serach first CLIENT structure matching a given mask of a server.
+ *
+ * The order of servers is arbitrary, but this function makes sure that the
+ * local server is always returned if the mask matches it.
+ *
+ * @return Pointer to CLIENT structure or NULL if no server could be found.
+ */
+GLOBAL CLIENT *
+Client_SearchServer(const char *Mask)
+{
+	CLIENT *c;
+
+	assert(Mask != NULL);
+
+	/* First check if mask matches the local server */
+	if (MatchCaseInsensitive(Mask, Client_ID(Client_ThisServer())))
+		return Client_ThisServer();
+
+	c = My_Clients;
+	while (c) {
+		if (Client_Type(c) == CLIENT_SERVER) {
+			/* This is a server: check if Mask matches */
+			if (MatchCaseInsensitive(Mask, c->id))
+				return c;
+		}
+		c = (CLIENT *)c->next;
+	}
+	return NULL;
+}


 /**
diff --git a/src/ngircd/client.h b/src/ngircd/client.h
index ebbd06cba05b0239fd5f42d6c4e8ce4fee706a79..
index ..c248d1ba39756a72a8d7065f6cf1d46349fccdae 100644
--- a/src/ngircd/client.h
+++ b/src/ngircd/client.h
@@ -94,6 +94,7 @@ GLOBAL CLIENT *Client_ThisServer PARAMS(( void ));
 GLOBAL CLIENT *Client_GetFromToken PARAMS(( CLIENT *Client, int Token ));

 GLOBAL CLIENT *Client_Search PARAMS(( const char *ID ));
+GLOBAL CLIENT *Client_SearchServer PARAMS(( const char *ID ));
 GLOBAL CLIENT *Client_First PARAMS(( void ));
 GLOBAL CLIENT *Client_Next PARAMS(( CLIENT *c ));

-----END OF PAGE-----