src/nexus.c
changeset 21 0911d0b828d4
parent 18 dedf137b504f
child 22 c339c020fd33
equal deleted inserted replaced
20:d9c4c2980a0d 21:0911d0b828d4
       
     1 
       
     2 #include "log.h"
       
     3 #include "sock.h"
       
     4 #include "irc_conn.h"
     1 
     5 
     2 #include <stdlib.h>
     6 #include <stdlib.h>
     3 #include <stdbool.h>
     7 #include <stdbool.h>
     4 #include <err.h>
       
     5 #include <stdio.h>
     8 #include <stdio.h>
     6 #include <getopt.h>
     9 #include <getopt.h>
     7 
    10 
     8 #include <event2/event.h>
    11 #include <event2/event.h>
     9 
    12 
    10 #include "sock.h"
       
    11 #include "irc_conn.h"
       
    12 
    13 
    13 #define DEFAULT_HOST "irc.fixme.fi"
    14 #define DEFAULT_HOST "irc.fixme.fi"
    14 #define DEFAULT_PORT "6667"
    15 #define DEFAULT_PORT "6667"
    15 #define DEFAULT_PORT_SSL "6697"
    16 #define DEFAULT_PORT_SSL "6697"
    16 
    17 
    36 {
    37 {
    37     int opt, option_index;
    38     int opt, option_index;
    38     struct event_base *ev_base;
    39     struct event_base *ev_base;
    39     struct sock_stream *sock;
    40     struct sock_stream *sock;
    40     struct irc_conn *conn;
    41     struct irc_conn *conn;
    41     struct error_info _err;
    42     struct error_info err;
    42 
    43 
    43     const char *hostname = DEFAULT_HOST, *portname = DEFAULT_PORT;
    44     const char *hostname = DEFAULT_HOST, *portname = DEFAULT_PORT;
    44     bool ssl = 0;
    45     bool ssl = 0;
    45     struct irc_conn_config conn_config = {
    46     struct irc_conn_config conn_config = {
    46         .nickname       = "SpBotDev",
    47         .nickname       = "SpBotDev",
    80         }
    81         }
    81     }
    82     }
    82 
    83 
    83     // initialize libevent
    84     // initialize libevent
    84     if ((ev_base = event_base_new()) == NULL)
    85     if ((ev_base = event_base_new()) == NULL)
    85         err(1, "event_base_new");
    86         FATAL("event_base_new");
    86 
    87 
    87     // initialize sock module
    88     // initialize sock module
    88     if (sock_init(ev_base, &_err))
    89     if (sock_init(ev_base, &err))
    89         errx(1, "sock_init: %s", error_msg(&_err));
    90         FATAL_ERROR(&err, "sock_init");
    90 
    91 
    91     // over-simplified connect
    92     // over-simplified connect
    92     if (ssl) {
    93     if (ssl) {
    93         if (sock_ssl_connect(&sock, hostname, portname, &_err))
    94         if (sock_ssl_connect(&sock, hostname, portname, &err))
    94             errx(1, "sock_ssl_connect: %s", error_msg(&_err));
    95             FATAL_ERROR(&err, "sock_ssl_connect(%s, %s)", hostname, portname);
    95 
    96 
    96     } else {
    97     } else {
    97         if (sock_tcp_connect(&sock, hostname, portname, &_err))
    98         if (sock_tcp_connect(&sock, hostname, portname, &err))
    98             errx(1, "sock_tcp_connect: %s", error_msg(&_err));
    99             FATAL_ERROR(&err, "sock_tcp_connect(%s, %s)", hostname, portname);
    99 
   100 
   100     }
   101     }
   101 
   102 
   102     // create the irc connection state
   103     // create the irc connection state
   103     if (irc_conn_create(&conn, sock, &conn_config, &_err))
   104     if (irc_conn_create(&conn, sock, &conn_config, &err))
   104         errx(1, "irc_conn_create: %s", error_msg(&_err));
   105         FATAL_ERROR(&err, "irc_conn_create");
   105 
   106 
   106     // run event loop
   107     // run event loop
   107     if (event_base_dispatch(ev_base))
   108     if (event_base_dispatch(ev_base))
   108         errx(1, "event_base_dispatch");
   109         FATAL("event_base_dispatch");
   109     
   110     
   110     // ok, no cleanup
   111     // ok, no cleanup
   111     return 0;
   112     return 0;
   112 }
   113 }
   113 
   114