src/irc_net.c
changeset 27 e6639132bead
parent 26 aec062af155d
child 28 9c1050bc8709
--- a/src/irc_net.c	Tue Mar 10 01:11:12 2009 +0200
+++ b/src/irc_net.c	Tue Mar 10 01:46:09 2009 +0200
@@ -4,6 +4,29 @@
 #include <stdlib.h>
 #include <string.h>
 
+static void irc_net_conn_registered (struct irc_conn *conn, void *arg)
+{
+    struct irc_net *net = arg;
+    struct irc_chan *chan = NULL;
+    err_t err;
+
+    (void) conn;
+
+    // join our channels
+    TAILQ_FOREACH(chan, &net->channels, node) {
+        if ((err = irc_chan_join(chan)))
+            // XXX: fuck...
+            FATAL_ERR(err, "irc_chan_join failed");
+    }
+}
+
+/**
+ * Our irc_conn_callbacks list
+ */
+struct irc_conn_callbacks _conn_callbacks = {
+    .on_registered  = &irc_net_conn_registered,
+};
+
 err_t irc_net_create (struct irc_net **net_ptr, const struct irc_net_info *info, struct error_info *err)
 {
     struct irc_net *net;
@@ -34,7 +57,11 @@
     log_info("connected, registering");
 
     // create the irc connection state
-    if (irc_conn_create(&net->conn, sock, &info->register_info, err))
+    if (irc_conn_create(&net->conn, sock, &_conn_callbacks, net, err))
+        goto error;
+
+    // register
+    if ((ERROR_CODE(err) = irc_conn_register(net->conn, &info->register_info)))
         goto error;
     
     // ok
@@ -60,11 +87,14 @@
     
     // add to network list
     TAILQ_INSERT_TAIL(&net->channels, chan, node);
-
-    // then join
-    if ((ERROR_CODE(&err) = irc_chan_join(chan)))
-        // XXX
-        return NULL;
+    
+    // currently connected?
+    if (net->conn && net->conn->registered) {
+        // then join
+        if ((ERROR_CODE(&err) = irc_chan_join(chan)))
+            // XXX
+            return NULL;
+    }
 
     // ok, return
     return chan;