src/config.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Tue, 02 Feb 2010 12:54:44 +0100
changeset 42 cb24a67ec3ec
parent 36 6889aacd038e
child 47 a689b6a8e6ed
permissions -rw-r--r--
This Bot uses bashisms und thus, a bash shell 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)) {
        (void)fgets (buffer, sizeof (buffer), f);
        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;
}