Used a GLib loop.
[cascardo/pubsub-bot.git] / status.c
index f4c0d5f..28f9a91 100644 (file)
--- a/status.c
+++ b/status.c
@@ -17,6 +17,7 @@
  */
 
 
+#include <glib.h>
 #include <iksemel.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -134,6 +135,37 @@ pushmood (iksparser *parser, char *node, char *line)
   iks_delete (iq);
 }
 
+iks *
+createtune (char *line)
+{
+  iks *tune;
+  tune = iks_new ("tune");
+  iks_insert_attrib (tune, "xmlns", "http://jabber.org/protocol/tune");
+  iks_insert_cdata (iks_insert (tune, "artist"), line, 0);
+  return tune;
+}
+
+void
+pushtune (iksparser *parser, char *node, char *line)
+{
+  iks *iq;
+  iks *query;
+  iks *publish;
+  iks *item;
+  iks *tune;
+  iq = createiq ("set", pbservice, "pubsub",
+                 "http://jabber.org/protocol/pubsub", &query);
+  publish = iks_insert (query, "publish");
+  iks_insert_attrib (publish, "node", node);
+  item = iks_insert (publish, "item");
+  tune = createtune (line);
+  iks_insert_node (item, tune);
+  printf ("debug: %s\n", iks_string (iks_stack (iq), iq));
+  iks_send (parser, iq);
+  iks_delete (iq);
+}
+
+
 void
 process_mood (iksparser *parser, char *cmdline)
 {
@@ -157,6 +189,12 @@ process_mood (iksparser *parser, char *cmdline)
       node = "http://jabber.org/protocol/mood";
       pushmood (parser, node, cmdline);
     }
+  else if (!strcmp (cmd, "tune"))
+    {
+      char *node;
+      node = "http://jabber.org/protocol/tune";
+      pushtune (parser, node, cmdline);
+    }
   free (orig_cmdline);
 }
 
@@ -369,6 +407,23 @@ hook (void *data, int type, iks *node)
   return IKS_OK;
 }
 
+gboolean
+handler (GIOChannel *channel, GIOCondition cond, gpointer data)
+{
+  iksparser *parser = data;
+  iks_recv (parser, 0);
+  return TRUE;
+}
+
+void
+loop (iksparser *parser)
+{
+  GIOChannel *channel;
+  channel = g_io_channel_unix_new (iks_fd (parser));
+  g_io_add_watch (channel, G_IO_IN, handler, parser);
+  g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE));
+}
+
 int
 main (int argc, char **argv)
 {
@@ -397,7 +452,6 @@ main (int argc, char **argv)
     }
   parser = iks_stream_new ("jabber:client", &parser, hook);
   iks_connect_tcp (parser, server, 5222);
-  while (1)
-    iks_recv (parser, -1);
+  loop (parser);
   return 0;
 }