src/irc_nm.c
changeset 36 791d7a5532e2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/irc_nm.c	Thu Mar 12 18:08:27 2009 +0200
@@ -0,0 +1,58 @@
+#include "irc_nm.h"
+
+#include <string.h>
+
+/**
+ * Compare two nicknames
+ */
+int irc_cmp_nick (const char *nick1, const char *nick2)
+{
+    // XXX: just use strcasecmp for now
+    return strcasecmp(nick1, nick2);
+}
+
+int irc_ncmp_nick (const char *nick1, const char *nick2, size_t n)
+{
+    // XXX: just use strncasecmp for now
+    return strncasecmp(nick1, nick2, n);
+}
+
+err_t irc_prefix_parse_nick (const char *prefix, char nick[IRC_NICK_MAX])
+{
+    const char *bang;
+
+    // find the !
+    if ((bang = strchr(prefix, '!')) == NULL)
+        return ERR_INVALID_NM;
+
+    // too long?
+    if (bang - prefix > IRC_NICK_MAX - 1)
+        return ERR_INVALID_NICK_LENGTH;
+    
+    // copy up to the !
+    memcpy(nick, prefix, bang - prefix);
+
+    // terminating NUL
+    nick[bang - prefix] = '\0';
+
+    // ok
+    return SUCCESS;
+}
+
+int irc_prefix_cmp_nick (const char *prefix, const char *nick)
+{
+    const char *bang;
+
+    // find the !
+    if ((bang = strchr(prefix, '!')) == NULL)
+        return -ERR_INVALID_NM;
+
+    // compare up to that
+    if (irc_ncmp_nick(prefix, nick, (bang - prefix)) == 0)
+        // match
+        return 0;
+
+    else
+        // doesn't match
+        return 1;
+}