add irc_net module, and fix Makefile CFLAGS, add -Wextra
authorTero Marttila <terom@fixme.fi>
Mon, 09 Mar 2009 16:30:59 +0200
changeset 25 56367df4ce5b
parent 24 08a26d0b9afd
child 26 aec062af155d
add irc_net module, and fix Makefile CFLAGS, add -Wextra
Makefile
src/irc_conn.h
src/irc_net.c
src/irc_net.h
src/nexus.c
--- a/Makefile	Mon Mar 09 16:30:30 2009 +0200
+++ b/Makefile	Mon Mar 09 16:30:59 2009 +0200
@@ -13,7 +13,7 @@
 MODE_CFLAGS = -DINFO_DISABLED -O2
 endif
 
-FIXED_CFLAGS = -Wall -std=gnu99
+FIXED_CFLAGS = -Wall -Wextra -std=gnu99
 
 # libevent
 LIBEVENT_CFLAGS = -I/home/terom/opt/include
@@ -38,7 +38,7 @@
 SOCK_OBJS = obj/sock.o obj/sock_tcp.o
 SOCK_GNUTLS_OBJS = obj/sock_gnutls.o
 LINEPROTO_OBJS = obj/line_proto.o
-IRC_OBJS = obj/irc_line.o obj/irc_conn.o
+IRC_OBJS = obj/irc_line.o obj/irc_conn.o obj/irc_net.o
 IRC_LOG_OBJS = obj/irc_log.o
 
 # XXX: not yet there
@@ -83,8 +83,10 @@
 
 include $(SRC_NAMES:%.c=build/deps/%.d)
 
+
+# XXX: removed $(CPPFLAGS) 
 obj/%.o : src/%.c
-	$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@
+	$(CC) -c $(CFLAGS) $< -o $@
 
 bin/% : obj/%.o
 	$(CC) $(LDFLAGS) $+ $(LOADLIBES) $(LDLIBS) -o $@
--- a/src/irc_conn.h	Mon Mar 09 16:30:30 2009 +0200
+++ b/src/irc_conn.h	Mon Mar 09 16:30:59 2009 +0200
@@ -33,6 +33,12 @@
 };
 
 // XXX: this should probably be slightly reworked
+
+/**
+ * The configuration info for an IRC connection.
+ *
+ * XXX: this should probably be reworked, maybe as a separate irc_conn_register function?
+ */
 struct irc_conn_config {
     /* Nickname to use on that server */
     const char *nickname;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/irc_net.c	Mon Mar 09 16:30:59 2009 +0200
@@ -0,0 +1,46 @@
+#include "irc_net.h"
+#include "log.h"
+
+#include <stdlib.h>
+
+err_t irc_net_create (struct irc_net **net_ptr, const struct irc_net_info *info, struct error_info *err)
+{
+    struct irc_net *net;
+    struct sock_stream *sock;
+    
+    // allocate
+    if ((net = calloc(1, sizeof(*net))) == NULL)
+        return SET_ERROR(err, ERR_CALLOC);
+
+    // XXX: over-simplified blocking connect
+    if (info->use_ssl) {
+        log_info("connecting to [%s]:%s using SSL", info->hostname, info->service);
+
+        if (sock_ssl_connect(&sock, info->hostname, info->service, err))
+            goto error;
+
+    } else {
+        log_info("connecting to [%s]:%s", info->hostname, info->service);
+
+        if (sock_tcp_connect(&sock, info->hostname, info->service, err))
+            goto error;
+
+    }
+
+    log_info("connected, registering");
+
+    // create the irc connection state
+    if (irc_conn_create(&net->conn, sock, &info->register_info, err))
+        goto error;
+    
+    // ok
+    *net_ptr = net;
+
+    return SUCCESS;
+
+error:
+    // XXX: cleanup
+
+    return ERROR_CODE(err);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/irc_net.h	Mon Mar 09 16:30:59 2009 +0200
@@ -0,0 +1,50 @@
+#ifndef IRC_NET_H
+#define IRC_NET_H
+
+/**
+ * @file
+ *
+ * Support for IRC networks. This is similar to an IRC connection, but we keep track of channel state, and handle
+ * reconnects.
+ */
+#include "error.h"
+#include "irc_conn.h"
+
+/**
+ * Configuration info for an IRC network
+ */
+struct irc_net_info {
+    /** The name of the network */
+    const char *network;
+
+    /** The hostname to connect to */
+    const char *hostname;
+
+    /** Service name (port) */
+    const char *service;
+
+    /** SSL? */
+    bool use_ssl;
+
+    /** Protocol registration info */
+    struct irc_conn_config register_info;
+};
+
+/**
+ * IRC Network state
+ */
+struct irc_net {
+    /* The current connection */
+    struct irc_conn *conn;
+
+
+};
+
+/**
+ * Create a new IRC network state, using the given network info to connect/register.
+ *
+ * Errors are returned via *err, also returning the error code.
+ */
+err_t irc_net_create (struct irc_net **net, const struct irc_net_info *info, struct error_info *err);
+
+#endif
--- a/src/nexus.c	Mon Mar 09 16:30:30 2009 +0200
+++ b/src/nexus.c	Mon Mar 09 16:30:59 2009 +0200
@@ -1,7 +1,6 @@
 
 #include "log.h"
-#include "sock.h"
-#include "irc_conn.h"
+#include "irc_net.h"
 #include "irc_log.h"
 
 #include <stdlib.h>
@@ -49,16 +48,19 @@
 {
     int opt, option_index;
     struct event_base *ev_base;
-    struct sock_stream *sock;
-    struct irc_conn *conn;
+    struct irc_net *net;
     struct error_info err;
 
-    const char *hostname = DEFAULT_HOST, *portname = DEFAULT_PORT;
-    bool ssl = 0;
-    struct irc_conn_config conn_config = {
-        .nickname       = "SpBotDev",
-        .username       = "spbot-dev",
-        .realname       = "SpBot (development version)",
+    struct irc_net_info net_info = {
+        .network                    = NULL,
+        .hostname                   = DEFAULT_HOST,
+        .service                    = DEFAULT_PORT,
+        .use_ssl                    = false,
+        .register_info              = {
+            .nickname               = "SpBotDev",
+            .username               = "spbot-dev",
+            .realname               = "SpBot (development version)",
+        }
     };
     const char *log_database = NULL, *log_channel = NULL;
 
@@ -72,19 +74,19 @@
                 return EXIT_SUCCESS;
 
             case 'H':
-                hostname = optarg;
+                net_info.hostname = optarg;
                 break;
             
             case 'P':
-                portname = optarg;
+                net_info.service = optarg;
                 port_default = false;
                 break;
 
             case 'S':
-                ssl = true;
+                net_info.use_ssl = true;
 
                 if (port_default)
-                    portname = DEFAULT_PORT_SSL;
+                    net_info.service = DEFAULT_PORT_SSL;
 
                 break;
             
@@ -109,31 +111,14 @@
     // initialize sock module
     if (sock_init(ev_base, &err))
         FATAL_ERROR(&err, "sock_init");
-
-    // over-simplified connect
-    if (ssl) {
-        log_info("SSL connecting to [%s]:%s", hostname, portname);
-
-        if (sock_ssl_connect(&sock, hostname, portname, &err))
-            FATAL_ERROR(&err, "sock_ssl_connect(%s, %s)", hostname, portname);
-
-    } else {
-        log_info("connecting to [%s]:%s", hostname, portname);
-
-        if (sock_tcp_connect(&sock, hostname, portname, &err))
-            FATAL_ERROR(&err, "sock_tcp_connect(%s, %s)", hostname, portname);
-
-    }
-
-    log_info("connected, registering");
-
-    // create the irc connection state
-    if (irc_conn_create(&conn, sock, &conn_config, &err))
-        FATAL_ERROR(&err, "irc_conn_create");
+    
+    // the IRC network
+    if (irc_net_create(&net, &net_info, &err))
+        FATAL_ERROR(&err, "irc_net_create");
     
     // logging?
     if (log_database || log_channel) {
-        if ((ERROR_CODE(&err) = irc_log_init(ev_base, log_database, conn, log_channel)))
+        if ((ERROR_CODE(&err) = irc_log_init(ev_base, log_database, net->conn, log_channel)))
             FATAL_ERROR(&err, "irc_log_init");
     }