#include "irc_proto.h"
#include <string.h>
err_t irc_nm_parse_buf (struct irc_nm *nm, char *prefix)
{
const char *token;
// parse the first token to determine if this is a nickmask or server name
token = strsep(&prefix, "!");
// did strsep find that "!"?
if (prefix) {
// it must be a full nickmask
nm->nickname = token;
nm->username = strsep(&prefix, "@");
nm->hostname = prefix;
if (!prefix)
// something silly, didn't have an @token
return ERR_INVALID_NM;
} else {
// handle as a server name
nm->nickname = NULL;
nm->username = NULL;
nm->hostname = token;
}
// ok
return SUCCESS;
}
err_t irc_nm_parse (struct irc_nm *nm, char *buf, const char *prefix)
{
// too long?
if (strlen(prefix) >= IRC_PREFIX_MAX)
return ERR_INVALID_NM;
// copy to mutable buffer
strcpy(buf, prefix);
// parse from buf
return irc_nm_parse_buf(nm, buf);
}
/**
* 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;
}
const char* irc_nick_chanflags (const char *nick, char chanflags[IRC_CHANFLAGS_MAX])
{
char *cf = chanflags;
// consume the chanflags, using strchr to look for the char in the set of chanflags...
// XXX: error if nickname is empty...
while (*nick && strchr(IRC_CHANFLAGS, *nick) && (cf < chanflags + IRC_CHANFLAGS_MAX - 1))
*cf++ = *nick++;
// NUL-terminate chanflags
*cf = '\0';
// then return the pointer to the first nickname char
return nick;
}