src/nexus.c
author Tero Marttila <terom@fixme.fi>
Fri, 27 Mar 2009 17:22:43 +0200
changeset 83 c8e2dac08207
parent 79 e26f972300e4
child 92 99661e5aac91
permissions -rw-r--r--
add config module and modify irc_log/nexus to use it
53
12d806823775 add irc_client module, plus nexus.h header
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
     1
#include "nexus.h"
48
4841f4398fd2 add irc_net_quit and signal handling
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
     2
#include "signals.h"
4841f4398fd2 add irc_net_quit and signal handling
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
     3
#include "log.h"
0
317e5bc59627 initial code...
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
317e5bc59627 initial code...
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
#include <stdlib.h>
15
9bbeace56269 add some simple command-line options
Tero Marttila <terom@fixme.fi>
parents: 14
diff changeset
     6
#include <stdbool.h>
0
317e5bc59627 initial code...
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
#include <stdio.h>
15
9bbeace56269 add some simple command-line options
Tero Marttila <terom@fixme.fi>
parents: 14
diff changeset
     8
#include <getopt.h>
48
4841f4398fd2 add irc_net_quit and signal handling
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
     9
#include <signal.h>
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    10
#include <string.h>
0
317e5bc59627 initial code...
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
18
dedf137b504f add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
    12
#define DEFAULT_HOST "irc.fixme.fi"
dedf137b504f add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
    13
#define DEFAULT_PORT "6667"
dedf137b504f add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
    14
#define DEFAULT_PORT_SSL "6697"
0
317e5bc59627 initial code...
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    16
/**
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    17
 * Command-line option codes
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    18
 */
23
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents: 22
diff changeset
    19
enum option_code {
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    20
    OPT_HELP            = 'h',
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    21
    OPT_NETWORK         = 'n',
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    22
    OPT_CHANNEL         = 'c',
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    23
    OPT_MODULE          = 'm',
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    24
    OPT_CONFIG          = 'C',
79
e26f972300e4 add --debug option to nexus
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
    25
    OPT_DEBUG           = 'd',
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    26
    
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    27
    /** Options without short names */
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    28
    _OPT_EXT_BEGIN      = 0x00ff,
23
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents: 22
diff changeset
    29
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents: 22
diff changeset
    30
};
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents: 22
diff changeset
    31
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    32
/**
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    33
 * Command-line option definitions
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    34
 */
15
9bbeace56269 add some simple command-line options
Tero Marttila <terom@fixme.fi>
parents: 14
diff changeset
    35
static struct option options[] = {
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    36
    {"help",            0,  NULL,   OPT_HELP        },
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    37
    {"network",         1,  NULL,   OPT_NETWORK     },
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    38
    {"channel",         1,  NULL,   OPT_CHANNEL     },
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    39
    {"module",          1,  NULL,   OPT_MODULE      },
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    40
    {"config",          1,  NULL,   OPT_CONFIG      },
79
e26f972300e4 add --debug option to nexus
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
    41
    {"debug",           0,  NULL,   OPT_DEBUG       },
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    42
    {0,                 0,  0,      0               },
15
9bbeace56269 add some simple command-line options
Tero Marttila <terom@fixme.fi>
parents: 14
diff changeset
    43
};
9bbeace56269 add some simple command-line options
Tero Marttila <terom@fixme.fi>
parents: 14
diff changeset
    44
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    45
/**
83
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    46
 * Display --help output on stdout.
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    47
 *
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    48
 * If nexus is given, --config options for loaded modules are also listed
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    49
 */
83
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    50
static void usage (struct nexus *nexus, const char *exe)
15
9bbeace56269 add some simple command-line options
Tero Marttila <terom@fixme.fi>
parents: 14
diff changeset
    51
{
9bbeace56269 add some simple command-line options
Tero Marttila <terom@fixme.fi>
parents: 14
diff changeset
    52
    printf("Usage: %s [OPTIONS]\n", exe);
9bbeace56269 add some simple command-line options
Tero Marttila <terom@fixme.fi>
parents: 14
diff changeset
    53
    printf("\n");
9bbeace56269 add some simple command-line options
Tero Marttila <terom@fixme.fi>
parents: 14
diff changeset
    54
    printf(" --help / -h            display this message\n");
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    55
    printf(" --network / -n         add an IRC network using '<name>:<hostname>[:<port>[:ssl]]' format\n");
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    56
    printf(" --channel / -c         add an IRC channel using '<network>:<channel>' format\n");
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    57
    printf(" --module / -m          add a module using '<name>:<path>' format\n");
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    58
    printf(" --config / -C          add a module configuration option using '<mod_name>:<name>[:<value>]' format\n");
79
e26f972300e4 add --debug option to nexus
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
    59
    printf(" --debug / -d           set logging level to DEBUG\n");
83
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    60
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    61
    if (nexus && !TAILQ_EMPTY(&nexus->modules->list)) {
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    62
        struct module *module;
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    63
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    64
        printf("\n");
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    65
        printf("Module configuration options\n");
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    66
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    67
        TAILQ_FOREACH(module, &nexus->modules->list, modules_list) {
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    68
            printf("\n");
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    69
            printf("%s:\n", module->info.name);
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    70
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    71
            if (module->funcs->config_options) {
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    72
                const struct config_option *opt;
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    73
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    74
                for (opt = module->funcs->config_options; opt->name && opt->type; opt++) {
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    75
                    printf(" --config %s:%s:%s\t\t%s\n", module->info.name, opt->name, opt->description, opt->help);
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    76
                }
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    77
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    78
            } else {
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    79
                printf("\t???\n");
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    80
            }
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    81
        }
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    82
    }
15
9bbeace56269 add some simple command-line options
Tero Marttila <terom@fixme.fi>
parents: 14
diff changeset
    83
}
9bbeace56269 add some simple command-line options
Tero Marttila <terom@fixme.fi>
parents: 14
diff changeset
    84
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    85
/**
65
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
    86
 * Parse and apply a --network option, adding the given network to our irc_client.
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    87
 */
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    88
static err_t apply_network (struct nexus *nexus, char *opt, struct error_info *err)
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    89
{
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    90
    struct irc_net_info info = {
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    91
        .network            = NULL,
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    92
        .hostname           = NULL,
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    93
        .service            = DEFAULT_PORT,
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    94
        .use_ssl            = false,
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    95
        .register_info      = {
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    96
            .nickname       = "SpBotDev",
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    97
            .username       = "spbot-dev",
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    98
            .realname       = "SpBot (development version)"
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    99
        }
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   100
    };
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   101
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   102
    // parse the required fields
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   103
    if ((info.network = strsep(&opt, ":")) == NULL)
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   104
        RETURN_SET_ERROR_STR(err, ERR_CMD_OPT, "missing <name> field for --network");
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   105
    
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   106
    if ((info.hostname = strsep(&opt, ":")) == NULL)
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   107
        RETURN_SET_ERROR_STR(err, ERR_CMD_OPT, "missing <hostname> field for --network");
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   108
    
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   109
    // parse the optional fields
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   110
    if (opt)
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   111
        info.service = strsep(&opt, ":");
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   112
    
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   113
    // parse any remaining flags
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   114
    while (opt) {
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   115
        char *flag = strsep(&opt, ":");
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   116
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   117
        if (strcmp(flag, "ssl"))
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   118
            info.use_ssl = true;
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   119
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   120
        else
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   121
            RETURN_SET_ERROR_STR(err, ERR_CMD_OPT, "unrecognized flag for --network");
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   122
    }
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   123
66
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   124
    // create the net
65
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   125
    log_info("add network '%s' at '%s:%s'", info.network, info.hostname, info.service);
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   126
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   127
    if (irc_client_add_net(nexus->client, NULL, &info, err))
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   128
        return ERROR_CODE(err);
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   129
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   130
    // ok
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   131
    return SET_ERROR(err, SUCCESS);
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   132
}
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   133
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   134
/**
65
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   135
 * Parse and apply a --channel option, adding the given channel to the given irc_net.
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   136
 */
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   137
static err_t apply_channel (struct nexus *nexus, char *opt, struct error_info *err)
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   138
{
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   139
    const char *network = NULL;
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   140
    struct irc_net *net;
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   141
    struct irc_chan_info info = {
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   142
        .channel            = NULL,
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   143
    };
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   144
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   145
    // parse the required fields
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   146
    if ((network = strsep(&opt, ":")) == NULL)
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   147
        RETURN_SET_ERROR_STR(err, ERR_CMD_OPT, "missing <network> field for --channel");
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   148
    
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   149
    if ((info.channel = strsep(&opt, ":")) == NULL)
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   150
        RETURN_SET_ERROR_STR(err, ERR_CMD_OPT, "missing <channel> field for --channel");
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   151
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   152
    // trailing garbage?
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   153
    if (opt)
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   154
        RETURN_SET_ERROR_STR(err, ERR_CMD_OPT, "trailing values for --channel");
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   155
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   156
    // look up the net
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   157
    if ((net = irc_client_get_net(nexus->client, network)) == NULL)
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   158
        RETURN_SET_ERROR_STR(err, ERR_CMD_OPT, "unknown network for --channel");
65
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   159
    
66
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   160
    // add the channel
65
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   161
    log_info("add channel '%s' on network '%s'", info.channel, net->info.network);
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   162
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   163
    if (irc_net_add_chan(net, NULL, &info, err))
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   164
        return ERROR_CODE(err);
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   165
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   166
    // ok
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   167
    return SUCCESS;
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   168
}
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   169
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   170
/**
65
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   171
 * Parse and apply a --module option, loading the given module.
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   172
 */
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   173
static err_t apply_module (struct nexus *nexus, char *opt, struct error_info *err)
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   174
{
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   175
    struct module_info info = {
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   176
        .name       = NULL,
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   177
        .path       = NULL,
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   178
    };
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   179
    struct module *module;
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   180
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   181
    // parse the required fields
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   182
    if ((info.name = strsep(&opt, ":")) == NULL)
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   183
        RETURN_SET_ERROR_STR(err, ERR_CMD_OPT, "missing <name> field for --module");
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   184
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   185
    if ((info.path = strsep(&opt, ":")) == NULL)
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   186
        RETURN_SET_ERROR_STR(err, ERR_CMD_OPT, "missing <path> field for --module");
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   187
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   188
    // trailing garbage?
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   189
    if (opt)
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   190
        RETURN_SET_ERROR_STR(err, ERR_CMD_OPT, "trailing values for --channel");
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   191
    
66
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   192
    // load it
65
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   193
    log_info("loading module '%s' from path '%s'", info.name, info.path);
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   194
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   195
    if (module_load(nexus->modules, &module, &info, err))
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   196
        return ERROR_CODE(err);
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   197
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   198
    // ok
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   199
    return SUCCESS;
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   200
}
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   201
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   202
/**
66
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   203
 * Parse and apply a --config option, calling the module's conf func.
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   204
 */
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   205
static err_t apply_config (struct nexus *nexus, char *opt, struct error_info *err)
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   206
{
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   207
    struct module *module;
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   208
    const char *module_name, *conf_name;
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   209
    char *conf_value;
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   210
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   211
    // parse the required fields
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   212
    if ((module_name = strsep(&opt, ":")) == NULL)
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   213
        RETURN_SET_ERROR_STR(err, ERR_CMD_OPT, "missing <module> field for --config");
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   214
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   215
    if ((conf_name = strsep(&opt, ":")) == NULL)
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   216
        RETURN_SET_ERROR_STR(err, ERR_CMD_OPT, "missing <name> field for --config");
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   217
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   218
    // value is the rest of the data, might be NULL
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   219
    conf_value = opt;
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   220
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   221
    // lookup the module
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   222
    if ((module = module_get(nexus->modules, module_name)) == NULL)
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   223
        RETURN_SET_ERROR_STR(err, ERR_CMD_OPT, "unknown module for --config");
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   224
    
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   225
    // do the config
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   226
    log_info("applying module '%s' config name '%s' with value: %s", module->info.name, conf_name, conf_value);
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   227
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   228
    if (module_conf(module, conf_name, conf_value, err))
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   229
        return ERROR_CODE(err);
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   230
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   231
    // ok
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   232
    return SUCCESS;
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   233
}
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   234
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   235
/**
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   236
 * Parse arguments and apply them to the given nexus
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   237
 */
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   238
static err_t parse_args (struct nexus *nexus, int argc, char **argv, struct error_info *err)
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   239
{
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   240
    int opt, option_index;
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   241
    
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   242
    // parse options
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   243
    while ((opt = getopt_long(argc, argv, "hn:c:m:C:", options, &option_index)) != -1) {
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   244
        switch (opt) {
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   245
            case OPT_HELP:
83
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   246
                usage(nexus, argv[0]);
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   247
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   248
                // XXX: return instead
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   249
                exit(EXIT_SUCCESS);
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   250
            
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   251
            case OPT_NETWORK:
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   252
                if (apply_network(nexus, optarg, err))
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   253
                    return ERROR_CODE(err);
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   254
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   255
                break;
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   256
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   257
            case OPT_CHANNEL:
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   258
                if (apply_channel(nexus, optarg, err))
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   259
                    return ERROR_CODE(err);
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   260
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   261
                break;
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   262
            
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   263
            case OPT_MODULE:
65
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   264
                if (apply_module(nexus, optarg, err))
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   265
                    return ERROR_CODE(err);
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   266
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   267
                break;
d7508879ad01 add --module support, and tweak irc_net docs
Tero Marttila <terom@fixme.fi>
parents: 63
diff changeset
   268
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   269
            case OPT_CONFIG:
66
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   270
                if (apply_config(nexus, optarg, err))
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   271
                    return ERROR_CODE(err);
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   272
ef8c9d7daf62 all options are now fully implemented
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   273
                break;
79
e26f972300e4 add --debug option to nexus
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   274
            
e26f972300e4 add --debug option to nexus
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   275
            case OPT_DEBUG:
e26f972300e4 add --debug option to nexus
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   276
                set_log_level(LOG_DEBUG);
e26f972300e4 add --debug option to nexus
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   277
                break;
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   278
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   279
            case '?':
83
c8e2dac08207 add config module and modify irc_log/nexus to use it
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   280
                usage(nexus, argv[0]);
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   281
                return SET_ERROR(err, ERR_CMD_OPT);
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   282
        }
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   283
    }
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   284
    
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   285
    // ok
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   286
    return SUCCESS;
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   287
}
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   288
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   289
static void on_sigint (evutil_socket_t sig, short what, void *arg)
48
4841f4398fd2 add irc_net_quit and signal handling
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   290
{
55
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   291
    struct nexus *ctx = arg;
48
4841f4398fd2 add irc_net_quit and signal handling
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   292
4841f4398fd2 add irc_net_quit and signal handling
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   293
    (void) sig;
4841f4398fd2 add irc_net_quit and signal handling
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   294
    (void) what;
4841f4398fd2 add irc_net_quit and signal handling
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   295
    
70
a9a4c5e6aa30 implement module unloading, although module_destroy is currently never called
Tero Marttila <terom@fixme.fi>
parents: 66
diff changeset
   296
    log_info("Quitting...");
48
4841f4398fd2 add irc_net_quit and signal handling
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   297
70
a9a4c5e6aa30 implement module unloading, although module_destroy is currently never called
Tero Marttila <terom@fixme.fi>
parents: 66
diff changeset
   298
    // unload the modules
a9a4c5e6aa30 implement module unloading, although module_destroy is currently never called
Tero Marttila <terom@fixme.fi>
parents: 66
diff changeset
   299
    modules_unload(ctx->modules);
48
4841f4398fd2 add irc_net_quit and signal handling
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   300
70
a9a4c5e6aa30 implement module unloading, although module_destroy is currently never called
Tero Marttila <terom@fixme.fi>
parents: 66
diff changeset
   301
    // quit the irc client
a9a4c5e6aa30 implement module unloading, although module_destroy is currently never called
Tero Marttila <terom@fixme.fi>
parents: 66
diff changeset
   302
    irc_client_quit(ctx->client, "Goodbye, cruel world ;(");
a9a4c5e6aa30 implement module unloading, although module_destroy is currently never called
Tero Marttila <terom@fixme.fi>
parents: 66
diff changeset
   303
    
a9a4c5e6aa30 implement module unloading, although module_destroy is currently never called
Tero Marttila <terom@fixme.fi>
parents: 66
diff changeset
   304
    // remove the signal handlers (ourself)
a9a4c5e6aa30 implement module unloading, although module_destroy is currently never called
Tero Marttila <terom@fixme.fi>
parents: 66
diff changeset
   305
    signals_free(ctx->signals);
71
0a13030f795d implement signal_ignore using sigaction directly, without any libevent in between
Tero Marttila <terom@fixme.fi>
parents: 70
diff changeset
   306
0a13030f795d implement signal_ignore using sigaction directly, without any libevent in between
Tero Marttila <terom@fixme.fi>
parents: 70
diff changeset
   307
    // now event_base_dispatch should return once everythings' shut down...
48
4841f4398fd2 add irc_net_quit and signal handling
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   308
}
4841f4398fd2 add irc_net_quit and signal handling
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   309
15
9bbeace56269 add some simple command-line options
Tero Marttila <terom@fixme.fi>
parents: 14
diff changeset
   310
int main (int argc, char **argv) 
9bbeace56269 add some simple command-line options
Tero Marttila <terom@fixme.fi>
parents: 14
diff changeset
   311
{
70
a9a4c5e6aa30 implement module unloading, although module_destroy is currently never called
Tero Marttila <terom@fixme.fi>
parents: 66
diff changeset
   312
    struct nexus _nexus, *nexus = &_nexus;
21
0911d0b828d4 add basic log.c module
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   313
    struct error_info err;
0
317e5bc59627 initial code...
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   314
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   315
    // zero nexus
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   316
    memset(nexus, 0, sizeof(*nexus));
15
9bbeace56269 add some simple command-line options
Tero Marttila <terom@fixme.fi>
parents: 14
diff changeset
   317
9
4c4c906cc649 add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   318
    // initialize libevent
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   319
    if ((nexus->ev_base = event_base_new()) == NULL)
21
0911d0b828d4 add basic log.c module
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   320
        FATAL("event_base_new");
48
4841f4398fd2 add irc_net_quit and signal handling
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   321
    
4841f4398fd2 add irc_net_quit and signal handling
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   322
    // initialize signal handlers
70
a9a4c5e6aa30 implement module unloading, although module_destroy is currently never called
Tero Marttila <terom@fixme.fi>
parents: 66
diff changeset
   323
    if ((ERROR_CODE(&err) = signals_create(&nexus->signals, nexus->ev_base)))
48
4841f4398fd2 add irc_net_quit and signal handling
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   324
        FATAL("signals_create");
9
4c4c906cc649 add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   325
48
4841f4398fd2 add irc_net_quit and signal handling
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   326
    // add our signal handlers
71
0a13030f795d implement signal_ignore using sigaction directly, without any libevent in between
Tero Marttila <terom@fixme.fi>
parents: 70
diff changeset
   327
    if (signal_ignore(SIGPIPE, &err))
0a13030f795d implement signal_ignore using sigaction directly, without any libevent in between
Tero Marttila <terom@fixme.fi>
parents: 70
diff changeset
   328
        FATAL_ERROR(&err, "signals_ignore(SIGPIPE)");
0a13030f795d implement signal_ignore using sigaction directly, without any libevent in between
Tero Marttila <terom@fixme.fi>
parents: 70
diff changeset
   329
0a13030f795d implement signal_ignore using sigaction directly, without any libevent in between
Tero Marttila <terom@fixme.fi>
parents: 70
diff changeset
   330
    if ((ERROR_CODE(&err) = signals_add(nexus->signals, SIGINT, &on_sigint, nexus)))
0a13030f795d implement signal_ignore using sigaction directly, without any libevent in between
Tero Marttila <terom@fixme.fi>
parents: 70
diff changeset
   331
        FATAL_ERROR(&err, "signals_add(SIGINT)");
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   332
 
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   333
    // initialize sock module
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   334
    if (sock_init(nexus->ev_base, &err))
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   335
        FATAL_ERROR(&err, "sock_init");
55
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   336
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   337
    // modules 
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   338
    if ((ERROR_CODE(&err) = modules_create(&nexus->modules, nexus)))
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   339
        FATAL_ERROR(&err, "modules_create");
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   340
    
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   341
    // the IRC client
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   342
    if (irc_client_create(&nexus->client, &err))
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   343
        FATAL_ERROR(&err, "irc_client_create");
55
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   344
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   345
    // parse args
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   346
    if (parse_args(nexus, argc, argv, &err))
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   347
        FATAL_ERROR(&err, "parse_args");
71
0a13030f795d implement signal_ignore using sigaction directly, without any libevent in between
Tero Marttila <terom@fixme.fi>
parents: 70
diff changeset
   348
    
11
14e79683c48c working event-based operation for sock_tcp
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   349
    // run event loop
71
0a13030f795d implement signal_ignore using sigaction directly, without any libevent in between
Tero Marttila <terom@fixme.fi>
parents: 70
diff changeset
   350
    log_info("entering event loop");
0a13030f795d implement signal_ignore using sigaction directly, without any libevent in between
Tero Marttila <terom@fixme.fi>
parents: 70
diff changeset
   351
63
d399a1d915a3 start reworking option-parsing, but --module/--config is still unimplemented
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
   352
    if (event_base_dispatch(nexus->ev_base))
21
0911d0b828d4 add basic log.c module
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   353
        FATAL("event_base_dispatch");
0
317e5bc59627 initial code...
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   354
    
11
14e79683c48c working event-based operation for sock_tcp
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   355
    // ok, no cleanup
0
317e5bc59627 initial code...
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   356
    return 0;
317e5bc59627 initial code...
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   357
}
317e5bc59627 initial code...
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   358