Use getpass to get user password.
[cascardo/pubsub-bot.git] / status.c
index 6362b6d..62572fb 100644 (file)
--- a/status.c
+++ b/status.c
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <limits.h>
 #include <dbus/dbus.h>
 #include <dbus/dbus-glib-lowlevel.h>
 
 static char * server = "vespa.holoscopio.com";
 static char * username = "pubsub";
-static char * password = "pubsub";
-static char * pbservice = "pubsub.vespa.holoscopio.com";
+static char * password = NULL;
+static char * pbservice = "pubsub@vespa.holoscopio.com";
 static char * authed_jid = "vespa";
 
 static iks *
@@ -287,6 +288,8 @@ xmpp_tls_hook (iksparser *parser, iks *node)
 static int
 xmpp_sasl_hook (iksparser *parser, iks* node)
 {
+  if (password == NULL)
+    return -1;
   iks_start_sasl (parser, IKS_SASL_DIGEST_MD5, username, password);
   return 0;
 }
@@ -295,6 +298,12 @@ static int
 xmpp_bind_hook (iksparser *parser, iks *node)
 {
   iks *iq;
+  if (password)
+    {
+      memset (password, 0, sysconf (_SC_PASS_MAX));
+      free (password);
+      password = NULL;
+    }
   iq = iks_new ("iq");
   iks_insert_attrib (iq, "type", "set");
   iks_insert_attrib (iq, "id", "bind1");
@@ -417,6 +426,28 @@ handler (GIOChannel *channel, GIOCondition cond, gpointer data)
   return TRUE;
 }
 
+struct
+  { char * key; char * val; } keymaps[] =
+{
+  { "artist", "artist" },
+  { "duration", "length" },
+  { "album", "source" },
+  { "title", "title" },
+  { "track-number", "track" },
+  { "location", "uri" },
+  { NULL, NULL }
+};
+
+static char *
+map_key (char *orig)
+{
+  int i;
+  for (i = 0; keymaps[i].key != NULL; i++)
+    if (strcmp (orig, keymaps[i].key) == 0)
+      return keymaps[i].val;
+  return NULL;
+}
+
 static void
 tune_add_dbus_arg (iks *tune, DBusMessageIter *args)
 {
@@ -440,6 +471,7 @@ tune_add_dbus_arg (iks *tune, DBusMessageIter *args)
     }
   else
     printf ("%c\n", dbus_message_iter_get_arg_type (&entry));
+  strkey = map_key (strkey);
   if (strkey && strval)
     {
       iks_insert_cdata (iks_insert (tune, strkey), strval, 0);
@@ -474,10 +506,19 @@ gettune (DBusConnection *conn, DBusMessage *msg, void *data)
   iks *tune;
   if (dbus_message_is_signal (msg, "org.MetaPlayer.tag", "playing"))
     {
+      printf("publishing tune\n");
       tune = tune_from_dbus (msg);
       pushtune (data, "http://jabber.org/protocol/tune", tune);
       return DBUS_HANDLER_RESULT_HANDLED;
     }
+  else if (dbus_message_is_signal (msg, "org.MetaPlayer.player", "stop"))
+    {
+      printf("tune stopped\n");
+      tune = iks_new ("tune");
+      iks_insert_attrib (tune, "xmlns", "http://jabber.org/protocol/tune");
+      pushtune (data, "http://jabber.org/protocol/tune", tune);
+      return DBUS_HANDLER_RESULT_HANDLED;
+    }
   else
     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
@@ -510,7 +551,9 @@ main (int argc, char **argv)
 {
   iksparser *parser;
   int c;
-  while ((c = getopt (argc, argv, "s:u:p:i:a:")) != -1)
+  int askpasswd = 0;
+  char *passwd = strdup ("pubsub");
+  while ((c = getopt (argc, argv, "s:u:p:i:a:w")) != -1)
     {
       switch (c)
         {
@@ -521,7 +564,8 @@ main (int argc, char **argv)
           username = optarg;
           break;
         case 'p':
-          password = optarg;
+          free (passwd);
+          passwd = strdup (optarg);
           break;
         case 'i':
           pbservice = optarg;
@@ -529,8 +573,21 @@ main (int argc, char **argv)
         case 'a':
           authed_jid = optarg;
           break;
+        case 'w':
+          askpasswd = 1;
+          break;
         }
     }
+  if (askpasswd)
+    passwd = getpass ("Type password: ");
+  password = malloc (sysconf (_SC_PASS_MAX));
+  strcpy (password, passwd);
+  memset (passwd, 0, strlen (passwd));
+  if (!askpasswd)
+    {
+      free (passwd);
+      passwd = NULL;
+    }
   parser = iks_stream_new ("jabber:client", &parser, hook);
   iks_connect_tcp (parser, server, 5222);
   loop (parser);