src/nexus.c
changeset 18 dedf137b504f
parent 17 5001564ac5fc
child 21 0911d0b828d4
equal deleted inserted replaced
17:5001564ac5fc 18:dedf137b504f
     6 #include <getopt.h>
     6 #include <getopt.h>
     7 
     7 
     8 #include <event2/event.h>
     8 #include <event2/event.h>
     9 
     9 
    10 #include "sock.h"
    10 #include "sock.h"
    11 #include "line_proto.h"
    11 #include "irc_conn.h"
    12 #include "irc_line.h"
       
    13 
    12 
    14 #define CONNECT_HOST "irc.fixme.fi"
    13 #define DEFAULT_HOST "irc.fixme.fi"
    15 #define CONNECT_SERV "6697"
    14 #define DEFAULT_PORT "6667"
    16 #define LINE_LENGTH 512
    15 #define DEFAULT_PORT_SSL "6697"
    17 
    16 
    18 static struct option options[] = {
    17 static struct option options[] = {
    19     {"help",            0,  NULL,   'h' },
    18     {"help",            0,  NULL,   'h' },
    20     {"hostname",        1,  NULL,   'H' },
    19     {"hostname",        1,  NULL,   'H' },
    21     {"port",            1,  NULL,   'P' },
    20     {"port",            1,  NULL,   'P' },
    31     printf(" --hostname / -H HOST   set hostname to connect to\n");
    30     printf(" --hostname / -H HOST   set hostname to connect to\n");
    32     printf(" --port / -P PORT       set service port to connect to\n");
    31     printf(" --port / -P PORT       set service port to connect to\n");
    33     printf(" --ssl / -S             use SSL\n");
    32     printf(" --ssl / -S             use SSL\n");
    34 }
    33 }
    35 
    34 
    36 void on_line (char *line_buf, void *arg) 
       
    37 {
       
    38     struct irc_line line;
       
    39     int err;
       
    40 
       
    41     // parse
       
    42     if ((err = irc_line_parse(&line, line_buf)))
       
    43         printf("!!! Invalid line: %s: %s\n", line_buf, error_name(err));
       
    44 
       
    45     else
       
    46         printf("<<< prefix=%s, command=%s, args={%s, %s, %s, ...}\n",
       
    47                 line.prefix, line.command, line.args[0], line.args[1], line.args[2]
       
    48         );
       
    49 }
       
    50 
       
    51 int main (int argc, char **argv) 
    35 int main (int argc, char **argv) 
    52 {
    36 {
    53     int opt, option_index;
    37     int opt, option_index;
    54     struct event_base *ev_base;
    38     struct event_base *ev_base;
    55     struct sock_stream *sock;
    39     struct sock_stream *sock;
    56     struct line_proto *lp;
    40     struct irc_conn *conn;
    57     struct error_info _err;
    41     struct error_info _err;
    58 
    42 
    59     const char *hostname = CONNECT_HOST, *portname = CONNECT_SERV;
    43     const char *hostname = DEFAULT_HOST, *portname = DEFAULT_PORT;
    60     bool ssl = 0;
    44     bool ssl = 0;
       
    45     struct irc_conn_config conn_config = {
       
    46         .nickname       = "SpBotDev",
       
    47         .username       = "spbot-dev",
       
    48         .realname       = "SpBot (development version)",
       
    49     };
       
    50 
       
    51     bool port_default = true;
    61     
    52     
    62     // parse options
    53     // parse options
    63     while ((opt = getopt_long(argc, argv, "hH:P:S", options, &option_index)) != -1) {
    54     while ((opt = getopt_long(argc, argv, "hH:P:S", options, &option_index)) != -1) {
    64         switch (opt) {
    55         switch (opt) {
    65             case 'h':
    56             case 'h':
    70                 hostname = optarg;
    61                 hostname = optarg;
    71                 break;
    62                 break;
    72             
    63             
    73             case 'P':
    64             case 'P':
    74                 portname = optarg;
    65                 portname = optarg;
       
    66                 port_default = false;
    75                 break;
    67                 break;
    76 
    68 
    77             case 'S':
    69             case 'S':
    78                 ssl = true;
    70                 ssl = true;
       
    71 
       
    72                 if (port_default)
       
    73                     portname = DEFAULT_PORT_SSL;
       
    74 
    79                 break;
    75                 break;
    80 
    76 
    81             case '?':
    77             case '?':
    82                 usage(argv[0]);
    78                 usage(argv[0]);
    83                 return EXIT_FAILURE;
    79                 return EXIT_FAILURE;
   101         if (sock_tcp_connect(&sock, hostname, portname, &_err))
    97         if (sock_tcp_connect(&sock, hostname, portname, &_err))
   102             errx(1, "sock_tcp_connect: %s", error_msg(&_err));
    98             errx(1, "sock_tcp_connect: %s", error_msg(&_err));
   103 
    99 
   104     }
   100     }
   105 
   101 
   106     // line protocol, with safety margin for buffer
   102     // create the irc connection state
   107     if (line_proto_create(&lp, sock, LINE_LENGTH * 2, on_line, NULL, &_err))
   103     if (irc_conn_create(&conn, sock, &conn_config, &_err))
   108         errx(1, "line_proto_create: %s", error_msg(&_err));
   104         errx(1, "irc_conn_create: %s", error_msg(&_err));
   109 
   105 
   110     // run event loop
   106     // run event loop
   111     if (event_base_dispatch(ev_base))
   107     if (event_base_dispatch(ev_base))
   112         errx(1, "event_base_dispatch");
   108         errx(1, "event_base_dispatch");
   113     
   109