From 7d83d20a3b6dd47b1fac2a69fdab43802cb256e4 Mon Sep 17 00:00:00 2001
From: Florian Zeitz <florob@babelmonkeys.de>
Date: Fri, 6 Jul 2012 00:06:14 +0200
Subject: [PATCH] Initial command support

---
 src/PEPThread.h |  2 +-
 src/mpdbot.m    | 33 +++++++++++++++++++++++++++++++--
 2 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/src/PEPThread.h b/src/PEPThread.h
index a752eca..19663ab 100644
--- a/src/PEPThread.h
+++ b/src/PEPThread.h
@@ -24,6 +24,6 @@
 
 @interface PEPThread: OFThread
 {
-MPDConnection *conn;
+	MPDConnection *conn;
 }
 @end
diff --git a/src/mpdbot.m b/src/mpdbot.m
index eb92876..8ef781b 100644
--- a/src/mpdbot.m
+++ b/src/mpdbot.m
@@ -31,6 +31,7 @@
 @interface AppDelegate: OFObject <OFApplicationDelegate, XMPPConnectionDelegate>
 {
 	PEPThread *pepper;
+	MPDConnection *mpdConn;
 }
 @end
 
@@ -40,6 +41,12 @@ OF_APPLICATION_DELEGATE(AppDelegate)
 - (void)applicationDidFinishLaunching
 {
 	XMPPConnection *conn;
+
+	OFString *mpd_host;
+	uint16_t mpd_port;
+	OFString *mpd_port_string;
+
+	OFDictionary *environment = [OFApplication environment];
 	OFArray *arguments = [OFApplication arguments];
 
 	conn = [[XMPPConnection alloc] init];
@@ -56,6 +63,20 @@ OF_APPLICATION_DELEGATE(AppDelegate)
 	conn.password = [arguments objectAtIndex: 2];
 	conn.resource = @"ObjXMPP";
 
+	mpd_host = [environment objectForKey: @"MPD_HOST"];
+	if (mpd_host == nil)
+		mpd_host = @"localhost";
+
+	mpd_port_string = [environment objectForKey: @"MPD_PORT"];
+	if (mpd_port_string && [mpd_port_string decimalValue] <= UINT16_MAX)
+		mpd_port = (uint16_t) [mpd_port_string decimalValue];
+	else
+		mpd_port = 6600;
+
+	mpdConn = [[MPDConnection alloc] initWithHost: mpd_host
+						 port: mpd_port];
+	[mpdConn connect];
+
 	@try {
 		[conn connect];
 		[conn handleConnection];
@@ -107,8 +128,7 @@ OF_APPLICATION_DELEGATE(AppDelegate)
 		    stringValue] isEqual: @"pubsub"] &&
 		    [[[identity attributeForName: @"type"] stringValue]
 		    isEqual: @"pep"]) {
-			pepper = [[PEPThread alloc]
-			    initWithObject: conn];
+			pepper = [[PEPThread alloc] initWithObject: conn];
 			[pepper start];
 
 			return YES;
@@ -131,6 +151,15 @@ OF_APPLICATION_DELEGATE(AppDelegate)
 	}
 }
 
+-  (void)connection: (XMPPConnection*)conn
+  didReceiveMessage: (XMPPMessage*)mesg
+{
+	if ([mesg.body isEqual: @"pause"]) {
+		[mpdConn send: @"pause"];
+		[mpdConn response];
+	}
+}
+
 - (void)connectionWasClosed: (XMPPConnection*)conn
 {
 	[of_stdout writeLine: @"Connection was closed!"];
-- 
2.39.5