src/irc_user.c
author Tero Marttila <terom@fixme.fi>
Thu, 28 May 2009 01:17:36 +0300
branchnew-lib-errors
changeset 219 cefec18b8268
parent 78 941bb8379d3d
permissions -rw-r--r--
some of the lib/transport stuff compiles
#include "irc_user.h"
#include "log.h"

#include <stdlib.h>
#include <string.h>
#include <assert.h>

// XXX: prototype of function from irc_net
void irc_net_remove_user (struct irc_net *net, struct irc_user *user);

err_t irc_user_create (struct irc_user **user_ptr, struct irc_net *net, const char *nickname)
{
    struct irc_user *user;

    (void) net;
    
    // allocate
    if ((user = calloc(1, sizeof(*user))) == NULL)
        return ERR_CALLOC;

    // init
    if ((user->nickname = strdup(nickname)) == NULL)
        return ERR_STRDUP;
    
    user->refcount = 0;

    // ok
    *user_ptr = user;

    return SUCCESS;
}

err_t irc_user_rename (struct irc_user *user, const char *new_nickname)
{
    // release old name
    free(user->nickname);
    
    // copy new one
    if ((user->nickname = strdup(new_nickname)) == NULL)
        return ERR_STRDUP;

    // ok
    return SUCCESS;
}

void irc_user_destroy (struct irc_user *user)
{
    if (user->refcount > 0)
        log_warn("refcount=%zu", user->refcount);

    free(user->nickname);
    free(user);
}