src/nexus.c
changeset 25 56367df4ce5b
parent 23 542c73d07d3c
child 26 aec062af155d
equal deleted inserted replaced
24:08a26d0b9afd 25:56367df4ce5b
     1 
     1 
     2 #include "log.h"
     2 #include "log.h"
     3 #include "sock.h"
     3 #include "irc_net.h"
     4 #include "irc_conn.h"
       
     5 #include "irc_log.h"
     4 #include "irc_log.h"
     6 
     5 
     7 #include <stdlib.h>
     6 #include <stdlib.h>
     8 #include <stdbool.h>
     7 #include <stdbool.h>
     9 #include <stdio.h>
     8 #include <stdio.h>
    47 
    46 
    48 int main (int argc, char **argv) 
    47 int main (int argc, char **argv) 
    49 {
    48 {
    50     int opt, option_index;
    49     int opt, option_index;
    51     struct event_base *ev_base;
    50     struct event_base *ev_base;
    52     struct sock_stream *sock;
    51     struct irc_net *net;
    53     struct irc_conn *conn;
       
    54     struct error_info err;
    52     struct error_info err;
    55 
    53 
    56     const char *hostname = DEFAULT_HOST, *portname = DEFAULT_PORT;
    54     struct irc_net_info net_info = {
    57     bool ssl = 0;
    55         .network                    = NULL,
    58     struct irc_conn_config conn_config = {
    56         .hostname                   = DEFAULT_HOST,
    59         .nickname       = "SpBotDev",
    57         .service                    = DEFAULT_PORT,
    60         .username       = "spbot-dev",
    58         .use_ssl                    = false,
    61         .realname       = "SpBot (development version)",
    59         .register_info              = {
       
    60             .nickname               = "SpBotDev",
       
    61             .username               = "spbot-dev",
       
    62             .realname               = "SpBot (development version)",
       
    63         }
    62     };
    64     };
    63     const char *log_database = NULL, *log_channel = NULL;
    65     const char *log_database = NULL, *log_channel = NULL;
    64 
    66 
    65     bool port_default = true;
    67     bool port_default = true;
    66     
    68     
    70             case 'h':
    72             case 'h':
    71                 usage(argv[0]);
    73                 usage(argv[0]);
    72                 return EXIT_SUCCESS;
    74                 return EXIT_SUCCESS;
    73 
    75 
    74             case 'H':
    76             case 'H':
    75                 hostname = optarg;
    77                 net_info.hostname = optarg;
    76                 break;
    78                 break;
    77             
    79             
    78             case 'P':
    80             case 'P':
    79                 portname = optarg;
    81                 net_info.service = optarg;
    80                 port_default = false;
    82                 port_default = false;
    81                 break;
    83                 break;
    82 
    84 
    83             case 'S':
    85             case 'S':
    84                 ssl = true;
    86                 net_info.use_ssl = true;
    85 
    87 
    86                 if (port_default)
    88                 if (port_default)
    87                     portname = DEFAULT_PORT_SSL;
    89                     net_info.service = DEFAULT_PORT_SSL;
    88 
    90 
    89                 break;
    91                 break;
    90             
    92             
    91             case OPT_LOG_DATABASE:
    93             case OPT_LOG_DATABASE:
    92                 log_database = optarg;
    94                 log_database = optarg;
   107         FATAL("event_base_new");
   109         FATAL("event_base_new");
   108 
   110 
   109     // initialize sock module
   111     // initialize sock module
   110     if (sock_init(ev_base, &err))
   112     if (sock_init(ev_base, &err))
   111         FATAL_ERROR(&err, "sock_init");
   113         FATAL_ERROR(&err, "sock_init");
   112 
   114     
   113     // over-simplified connect
   115     // the IRC network
   114     if (ssl) {
   116     if (irc_net_create(&net, &net_info, &err))
   115         log_info("SSL connecting to [%s]:%s", hostname, portname);
   117         FATAL_ERROR(&err, "irc_net_create");
   116 
       
   117         if (sock_ssl_connect(&sock, hostname, portname, &err))
       
   118             FATAL_ERROR(&err, "sock_ssl_connect(%s, %s)", hostname, portname);
       
   119 
       
   120     } else {
       
   121         log_info("connecting to [%s]:%s", hostname, portname);
       
   122 
       
   123         if (sock_tcp_connect(&sock, hostname, portname, &err))
       
   124             FATAL_ERROR(&err, "sock_tcp_connect(%s, %s)", hostname, portname);
       
   125 
       
   126     }
       
   127 
       
   128     log_info("connected, registering");
       
   129 
       
   130     // create the irc connection state
       
   131     if (irc_conn_create(&conn, sock, &conn_config, &err))
       
   132         FATAL_ERROR(&err, "irc_conn_create");
       
   133     
   118     
   134     // logging?
   119     // logging?
   135     if (log_database || log_channel) {
   120     if (log_database || log_channel) {
   136         if ((ERROR_CODE(&err) = irc_log_init(ev_base, log_database, conn, log_channel)))
   121         if ((ERROR_CODE(&err) = irc_log_init(ev_base, log_database, net->conn, log_channel)))
   137             FATAL_ERROR(&err, "irc_log_init");
   122             FATAL_ERROR(&err, "irc_log_init");
   138     }
   123     }
   139 
   124 
   140     // run event loop
   125     // run event loop
   141     if (event_base_dispatch(ev_base))
   126     if (event_base_dispatch(ev_base))