src/config.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 27 Feb 2010 18:58:24 +0100
changeset 50 412ac658eb97
parent 47 a689b6a8e6ed
child 51 5d9aed3948a6
permissions -rw-r--r--
No such username(402) status code updated committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 *  $Id: config.c 51 2008-01-10 00:19:39Z mbroeker $
 * $URL: http://localhost/svn/c/mcbot/trunk/src/config.c $
 *
 */

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

#include <compat.h>
#include <config.h>
#include "common.h"

enum config_map {
    NICK, PASSWORD, SERVER, PORT, CHANNEL, TOPIC,
};

const
char *CONFIG_OPTIONS[] = {
    "NICK", "PASSWORD", "SERVER", "PORT",
    "CHANNEL", "TOPIC", NULL
};

int config (UC * uc, char *fname)
{
    FILE *f;
    char buffer[DEFAULT_BUF_SIZE];
    char **line;
    char *token;
    char *value;
    enum config_map map = 0;

    if ((f = fopen (fname, "r")) == NULL)
        return -1;

    while (CONFIG_OPTIONS[map] != NULL)
        map++;

    line = calloc ((size_t) (map + 1), sizeof (char *));

    /*
     * We can easily provide default values ...
     */
    uc->nick = uc->pass = uc->server = uc->channel = uc->topic = NULL;
    uc->port = 6667;

    while (!feof (f)) {
        if (fgets (buffer, sizeof (buffer), f) == NULL)
            break;
        token = buffer;

        while (*token == '\t')  /* Eat trailing tabs */
            token++;
        while (*token == ' ')   /* Eat trailing whitespaces */
            token++;

        token = strtok (token, ":");

        if (token != NULL) {
            value = strtok (NULL, "\n");
            map = 0;
            while (CONFIG_OPTIONS[map] != NULL) {
                if (!strcmp (CONFIG_OPTIONS[map], token)) {
                    if (value)
                        line[map] = compat_strdup (value);
                }
                map++;
            }
        }
    }

    if (fclose (f) != 0)
        return -1;

    map = 0;
    while (CONFIG_OPTIONS[map] != NULL) {
        if (line[map] != NULL)
            switch (map) {
            case NICK:
                uc->nick = compat_strdup (line[map]);
                free (line[map]);
                break;
            case PASSWORD:
                uc->pass = compat_strdup (line[map]);
                free (line[map]);
                break;
            case SERVER:
                uc->server = compat_strdup (line[map]);
                free (line[map]);
                break;
            case PORT:
                uc->port = atoi (line[map]);
                free (line[map]);
                break;
            case CHANNEL:
                uc->channel = compat_strdup (line[map]);
                free (line[map]);
                break;
            case TOPIC:
                uc->topic = compat_strdup (line[map]);
                free (line[map]);
                break;
            }
        map++;
    }

    if (line != NULL)
        free (line);

    if (!(uc->nick && uc->server && uc->channel))
        return -2;

    return 0;
}