src/nexus.c
changeset 23 542c73d07d3c
parent 22 c339c020fd33
child 25 56367df4ce5b
equal deleted inserted replaced
22:c339c020fd33 23:542c73d07d3c
     1 
     1 
     2 #include "log.h"
     2 #include "log.h"
     3 #include "sock.h"
     3 #include "sock.h"
     4 #include "irc_conn.h"
     4 #include "irc_conn.h"
       
     5 #include "irc_log.h"
     5 
     6 
     6 #include <stdlib.h>
     7 #include <stdlib.h>
     7 #include <stdbool.h>
     8 #include <stdbool.h>
     8 #include <stdio.h>
     9 #include <stdio.h>
     9 #include <getopt.h>
    10 #include <getopt.h>
    13 
    14 
    14 #define DEFAULT_HOST "irc.fixme.fi"
    15 #define DEFAULT_HOST "irc.fixme.fi"
    15 #define DEFAULT_PORT "6667"
    16 #define DEFAULT_PORT "6667"
    16 #define DEFAULT_PORT_SSL "6697"
    17 #define DEFAULT_PORT_SSL "6697"
    17 
    18 
       
    19 enum option_code {
       
    20     _OPT_LOG_BEGIN      = 0x00ff,
       
    21 
       
    22     OPT_LOG_DATABASE,
       
    23     OPT_LOG_CHANNEL,
       
    24 };
       
    25 
    18 static struct option options[] = {
    26 static struct option options[] = {
    19     {"help",            0,  NULL,   'h' },
    27     {"help",            0,  NULL,   'h'                 },
    20     {"hostname",        1,  NULL,   'H' },
    28     {"hostname",        1,  NULL,   'H'                 },
    21     {"port",            1,  NULL,   'P' },
    29     {"port",            1,  NULL,   'P'                 },
    22     {"ssl",             0,  NULL,   'S' },
    30     {"ssl",             0,  NULL,   'S'                 },
    23     {0,                 0,  0,      0   },
    31     {"log-database",    1,  NULL,   OPT_LOG_DATABASE    },
       
    32     {"log-channel",     1,  NULL,   OPT_LOG_CHANNEL     },
       
    33     {0,                 0,  0,      0                   },
    24 };
    34 };
    25 
    35 
    26 void usage (const char *exe) 
    36 void usage (const char *exe) 
    27 {
    37 {
    28     printf("Usage: %s [OPTIONS]\n", exe);
    38     printf("Usage: %s [OPTIONS]\n", exe);
    29     printf("\n");
    39     printf("\n");
    30     printf(" --help / -h            display this message\n");
    40     printf(" --help / -h            display this message\n");
    31     printf(" --hostname / -H HOST   set hostname to connect to\n");
    41     printf(" --hostname / -H HOST   set hostname to connect to\n");
    32     printf(" --port / -P PORT       set service port to connect to\n");
    42     printf(" --port / -P PORT       set service port to connect to\n");
    33     printf(" --ssl / -S             use SSL\n");
    43     printf(" --ssl / -S             use SSL\n");
       
    44     printf(" --log-database         database connection string for logging\n");
       
    45     printf(" --log-channel          channel to log\n");
    34 }
    46 }
    35 
    47 
    36 int main (int argc, char **argv) 
    48 int main (int argc, char **argv) 
    37 {
    49 {
    38     int opt, option_index;
    50     int opt, option_index;
    46     struct irc_conn_config conn_config = {
    58     struct irc_conn_config conn_config = {
    47         .nickname       = "SpBotDev",
    59         .nickname       = "SpBotDev",
    48         .username       = "spbot-dev",
    60         .username       = "spbot-dev",
    49         .realname       = "SpBot (development version)",
    61         .realname       = "SpBot (development version)",
    50     };
    62     };
       
    63     const char *log_database = NULL, *log_channel = NULL;
    51 
    64 
    52     bool port_default = true;
    65     bool port_default = true;
    53     
    66     
    54     // parse options
    67     // parse options
    55     while ((opt = getopt_long(argc, argv, "hH:P:S", options, &option_index)) != -1) {
    68     while ((opt = getopt_long(argc, argv, "hH:P:S", options, &option_index)) != -1) {
    71                 ssl = true;
    84                 ssl = true;
    72 
    85 
    73                 if (port_default)
    86                 if (port_default)
    74                     portname = DEFAULT_PORT_SSL;
    87                     portname = DEFAULT_PORT_SSL;
    75 
    88 
       
    89                 break;
       
    90             
       
    91             case OPT_LOG_DATABASE:
       
    92                 log_database = optarg;
       
    93                 break;
       
    94 
       
    95             case OPT_LOG_CHANNEL:
       
    96                 log_channel = optarg;
    76                 break;
    97                 break;
    77 
    98 
    78             case '?':
    99             case '?':
    79                 usage(argv[0]);
   100                 usage(argv[0]);
    80                 return EXIT_FAILURE;
   101                 return EXIT_FAILURE;
   107     log_info("connected, registering");
   128     log_info("connected, registering");
   108 
   129 
   109     // create the irc connection state
   130     // create the irc connection state
   110     if (irc_conn_create(&conn, sock, &conn_config, &err))
   131     if (irc_conn_create(&conn, sock, &conn_config, &err))
   111         FATAL_ERROR(&err, "irc_conn_create");
   132         FATAL_ERROR(&err, "irc_conn_create");
       
   133     
       
   134     // logging?
       
   135     if (log_database || log_channel) {
       
   136         if ((ERROR_CODE(&err) = irc_log_init(ev_base, log_database, conn, log_channel)))
       
   137             FATAL_ERROR(&err, "irc_log_init");
       
   138     }
   112 
   139 
   113     // run event loop
   140     // run event loop
   114     if (event_base_dispatch(ev_base))
   141     if (event_base_dispatch(ev_base))
   115         FATAL("event_base_dispatch");
   142         FATAL("event_base_dispatch");
   116     
   143