src/irc_client.c
author Tero Marttila <terom@fixme.fi>
Sun, 15 Mar 2009 01:17:22 +0200
branchmodules
changeset 55 6f7f6ae729d0
parent 53 12d806823775
child 63 d399a1d915a3
permissions -rw-r--r--
'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
#include "irc_client.h"
#include "log.h"

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

err_t irc_client_create (struct irc_client **client_ptr, struct error_info *err)
{
    struct irc_client *client;

    // allocate
    if ((client = calloc(1, sizeof(*client))) == NULL)
        return SET_ERROR(err, ERR_CALLOC);

    // init
    TAILQ_INIT(&client->networks);

    // ok
    *client_ptr = client;

    return SUCCESS;
}

void irc_client_destroy (struct irc_client *client)
{
    struct irc_net *next = TAILQ_FIRST(&client->networks), *net;

    // our networks
    while (next) {
        net = next;
        next = TAILQ_NEXT(net, client_networks);

        irc_net_destroy(net);
    }

    // ourselves
    free(client);
}

err_t irc_client_add_net (struct irc_client *client, struct irc_net **net_ptr, struct irc_net_info *net_info)
{
    struct irc_net *net;
    struct error_info err;

    // create the new irc_chan struct
    if (irc_net_create(&net, net_info, &err))
        return ERROR_CODE(&err);
    
    // add to network list
    TAILQ_INSERT_TAIL(&client->networks, net, client_networks);
    
    // ok
    *net_ptr = net;

    return SUCCESS;
}

struct irc_net* irc_client_get_net (struct irc_client *client, const char *network)
{
    struct irc_net *net = NULL;

    // look for it...
    TAILQ_FOREACH(net, &client->networks, client_networks) {
        if (strcasecmp(net->info.network, network) == 0)
            // found it
            return net;
    }

    // no such network
    return NULL;
}

struct irc_chan* irc_client_get_chan (struct irc_client *client, const char *network, const char *channel)
{
    struct irc_net *net;
    
    // lookup network
    if ((net = irc_client_get_net(client, network)) == NULL)
        return NULL;
    
    // and then return channel lookup
    return irc_net_get_chan(net, channel);
}

err_t irc_client_quit (struct irc_client *client, const char *message)
{
    struct irc_net *net;
    err_t err;

    // quit each network
    TAILQ_FOREACH(net, &client->networks, client_networks) {
        if ((err = irc_net_quit(net, message))) {
            log_err(err, "irc_net_quit: %s [%s]", net->info.network, message);
            
            // XXX: destroy it?
        }
    }

    // state
    client->quitting = true;

    // ok
    return SUCCESS;
}