src/irc_proto.c
author Tero Marttila <terom@fixme.fi>
Thu, 12 Mar 2009 22:50:08 +0200
changeset 45 71e65564afd2
parent 39 a4891d71aca9
child 72 43084f103c2a
permissions -rw-r--r--
remove irc_chan.state, modify irc_chan_callbacks.on_msg to take a irc_nm, improve error handling a bit further (up to irc_net now)
#include "irc_proto.h"

#include <string.h>

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);

    // mangle tokens
    nm->nickname = strsep(&buf, "!");
    nm->username = strsep(&buf, "@");

    // did we find the ! and @ tokens?
    if (!buf)
        // probably a server name instead
        return ERR_INVALID_NM;
    
    // the hostname is then the rest of the prefix
    nm->hostname = buf;

    // ok
    return SUCCESS;
}

/**
 * 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;
}