Support for SASL PLAIN authentication
[cascardo/chat.git] / xmpp.c
diff --git a/xmpp.c b/xmpp.c
index f42c55c..ca9501e 100644 (file)
--- a/xmpp.c
+++ b/xmpp.c
 
 
 #include <stdlib.h>
+#include <string.h>
+#include <gsasl.h>
 #include "xmpp.h"
 #include "xmpp_internal.h"
+#include "iksemel_extra.h"
+#include "tcp_connect.h"
 
 hc_xmpp_t *
-hc_xmpp_new (void)
+hc_xmpp_new (iksStreamHook *hook, char *server, char *user, char *pass)
 {
   hc_xmpp_t *xmpp = malloc (sizeof (hc_xmpp_t));
+  xmpp->server = strdup (server);
+  xmpp->user = strdup (user);
+  xmpp->password = strdup (pass);
+  xmpp->parser = iks_extra_stream_new (xmpp, hook);
+  gsasl_init (&xmpp->sasl_ctx);
+  xmpp->fd = hc_tcp_connect (server, "xmpp-client");
   xmpp->tls = NONE;
   xmpp->sasl = NONE;
+  xmpp->status = HC_XMPP_NONE;
   return xmpp;
 }
 
@@ -78,3 +89,39 @@ hc_xmpp_is_sasl_enabled (hc_xmpp_t *xmpp)
   return xmpp->sasl & ENABLED;
 }
 
+char *
+hc_xmpp_server (hc_xmpp_t *xmpp)
+{
+  return xmpp->server;
+}
+
+void
+hc_xmpp_send_buffer (hc_xmpp_t *xmpp, char *buffer, size_t len)
+{
+  if (len == 0)
+    len = strlen (buffer);
+  write (xmpp->fd, buffer, len);
+}
+
+void
+hc_xmpp_send_iks (hc_xmpp_t *xmpp, iks *x)
+{
+  char *str;
+  str = iks_string (iks_stack (x), x);
+  write (xmpp->fd, str, strlen (str));
+}
+
+void
+hc_xmpp_read_and_parse (hc_xmpp_t *xmpp)
+{
+  char buffer[4096];
+  int r;
+  r = read (xmpp->fd, buffer, sizeof (buffer));
+  iks_parse (xmpp->parser, buffer, r, 0);
+}
+
+int
+hc_xmpp_status (hc_xmpp_t *xmpp)
+{
+  return xmpp->status;
+}