src/config.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Nov 2010 15:49:44 +0100
changeset 59 e8f15b159e19
parent 58 500a5ea7fcb8
child 62 2d6419d6d4ed
permissions -rw-r--r--
irc_login: csocket holds a valid descriptor or -1 on error committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 *  $Id: config.c 51 2008-01-10 00:19:39Z mbroeker $
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 * $URL: http://localhost/svn/c/mcbot/trunk/src/config.c $
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 *
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
 */
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdio.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <stdlib.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <string.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
13
d3554afaa768 mcbot-0.94-5 Changelog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 11
diff changeset
    11
#include <compat.h>
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
#include <config.h>
33
56571d34d754 safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 18
diff changeset
    13
#include "common.h"
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    15
enum config_map {
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    16
    NICK, PASSWORD, SERVER, PORT, CHANNEL, TOPIC,
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    17
};
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    18
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
const
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
char *CONFIG_OPTIONS[] = {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
    "NICK", "PASSWORD", "SERVER", "PORT",
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    22
    "CHANNEL", "TOPIC", NULL
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
};
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
int config (UC * uc, char *fname)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
{
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
    FILE *f;
33
56571d34d754 safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 18
diff changeset
    28
    char buffer[DEFAULT_BUF_SIZE];
10
311ea5fa60dd Code cleanups and debian changelogs
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 6
diff changeset
    29
    char **line;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
    char *token;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    char *value;
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    32
    enum config_map map = 0;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
    if ((f = fopen (fname, "r")) == NULL)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
        return -1;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    37
    while (CONFIG_OPTIONS[map] != NULL)
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    38
        map++;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
51
5d9aed3948a6 code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 47
diff changeset
    40
    if ((line = calloc ((size_t) (map + 1), sizeof (char *))) == NULL) {
5d9aed3948a6 code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 47
diff changeset
    41
        fclose (f);
5d9aed3948a6 code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 47
diff changeset
    42
        return -1;
5d9aed3948a6 code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 47
diff changeset
    43
    }
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
    /*
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
     * We can easily provide default values ...
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
     */
6
7eb12be31bb5 Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 0
diff changeset
    48
    uc->nick = uc->pass = uc->server = uc->channel = uc->topic = NULL;
58
500a5ea7fcb8 IPv6 Support for mcbot
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 51
diff changeset
    49
    uc->port = "6667";
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    51
    while (!feof (f)) {
47
a689b6a8e6ed fgets may return a NULL pointer...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 36
diff changeset
    52
        if (fgets (buffer, sizeof (buffer), f) == NULL)
a689b6a8e6ed fgets may return a NULL pointer...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 36
diff changeset
    53
            break;
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    54
        token = buffer;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
51
5d9aed3948a6 code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 47
diff changeset
    56
        while (*token == '\t' || *token == ' ') /* Eat trailing tabs and whitespaces */
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
            token++;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
        token = strtok (token, ":");
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
        if (token != NULL) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
            value = strtok (NULL, "\n");
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    63
            map = 0;
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    64
            while (CONFIG_OPTIONS[map] != NULL) {
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    65
                if (!strcmp (CONFIG_OPTIONS[map], token)) {
51
5d9aed3948a6 code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 47
diff changeset
    66
                    if (value) {
5d9aed3948a6 code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 47
diff changeset
    67
                        if (line[map] != '\0')
5d9aed3948a6 code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 47
diff changeset
    68
                            free (line[map]);
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    69
                        line[map] = compat_strdup (value);
51
5d9aed3948a6 code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 47
diff changeset
    70
                    }
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
                }
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    72
                map++;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
            }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
        }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    75
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    76
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    77
    map = 0;
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    78
    while (CONFIG_OPTIONS[map] != NULL) {
51
5d9aed3948a6 code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 47
diff changeset
    79
        if (line[map] != '\0')
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    80
            switch (map) {
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    81
            case NICK:
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    82
                uc->nick = compat_strdup (line[map]);
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    83
                free (line[map]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    84
                break;
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    85
            case PASSWORD:
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    86
                uc->pass = compat_strdup (line[map]);
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    87
                free (line[map]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    88
                break;
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    89
            case SERVER:
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    90
                uc->server = compat_strdup (line[map]);
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    91
                free (line[map]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    92
                break;
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    93
            case PORT:
58
500a5ea7fcb8 IPv6 Support for mcbot
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 51
diff changeset
    94
                uc->port = compat_strdup (line[map]);
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    95
                free (line[map]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    96
                break;
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    97
            case CHANNEL:
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    98
                uc->channel = compat_strdup (line[map]);
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    99
                free (line[map]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   100
                break;
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
   101
            case TOPIC:
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
   102
                uc->topic = compat_strdup (line[map]);
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
   103
                free (line[map]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   104
                break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   105
            }
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
   106
        map++;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   107
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   108
51
5d9aed3948a6 code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 47
diff changeset
   109
    free (line);
5d9aed3948a6 code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 47
diff changeset
   110
5d9aed3948a6 code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 47
diff changeset
   111
    if (fclose (f) != 0)
5d9aed3948a6 code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 47
diff changeset
   112
        return -1;
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
   113
18
4435146391ae LOGIN without Password added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 13
diff changeset
   114
    if (!(uc->nick && uc->server && uc->channel))
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   115
        return -2;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   116
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   117
    return 0;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   118
}