diff xmpp.js @ 0:363600705376 default tip

Initial commit.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 23 Aug 2011 18:43:15 -0700
parents
children
line wrap: on
line diff
new file mode 100755
--- /dev/null
+++ b/xmpp.js
@@ -0,0 +1,48 @@
+#!/usr/bin/env node
+
+var MPD = require('./lib');
+var xmpp = require('node-xmpp');
+var config = require('./config');
+
+var mpd = new MPD();
+var conn = new xmpp.Client(config);
+var id = 0;
+var NS = 'http://jabber.org/protocol/tune';
+
+var publish = function() {
+  mpd.send('status', function(s) {
+    mpd.send('currentsong', function(cs) {
+      var tune = new xmpp.Element('tune', {xmlns: NS});
+     
+      if (cs.file && s.state === 'play') {
+        // Don't publish "rating" and "uri".
+        if (cs.Artist)
+          tune.c('artist').t(cs.Artist);
+        if (cs.Time)
+          tune.c('length').t(cs.Time);
+        if (cs.Album)
+          tune.c('source').t(cs.Album);
+        if (cs.Title)
+          tune.c('title').t(cs.Title);
+        if (cs.Track)
+          tune.c('track').t(cs.Track);
+      }
+     
+      conn.send(new xmpp.Element('iq', {type: 'set', id: 'mpd-'+(++id)})
+        .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
+          .c('publish', {node: NS})
+            .c('item')
+              .cnode(tune).up()
+            .up()
+          .up()
+        .up());
+     
+      mpd.send('idle player', publish);
+    });
+  });
+};
+
+// We suppose that the XMPP connection will be open after the MPD one.
+conn.on('online', publish);
+
+// vim: sts=2 et sw=2