2 * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
4 * http://cgit.babelmonkeys.de/cgit.cgi/mpdbot/
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice is present in all copies.
10 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
11 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
12 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
13 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
14 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
15 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
16 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
17 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
18 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20 * POSSIBILITY OF SUCH DAMAGE.
23 #import <ObjFW/ObjFW.h>
24 #import <ObjXMPP/ObjXMPP.h>
28 #define NS_PUBSUB @"http://jabber.org/protocol/pubsub"
29 #define NS_TUNE @"http://jabber.org/protocol/tune"
31 @implementation PEPThread: OFThread
45 sock = [[OFTCPSocket alloc] init];
46 [sock connectToHost: @"localhost"
50 [of_stderr writeFormat: @"Connection failed, retrying"
51 @" in %" PRIi64 @" seconds\n", pause];
52 [OFThread sleepForTimeInterval: pause];
60 - (OFMutableDictionary*)MPD_responseFromSocket: (OFTCPSocket*)sock_
63 OFMutableDictionary *response = [OFMutableDictionary dictionary];
64 while ((answer = [sock_ readLine]) && ![answer hasPrefix: @"OK"]) {
66 index = [answer indexOfFirstOccurrenceOfString: @":"];
67 if (index == OF_INVALID_INDEX)
69 [response setObject: [answer substringFromIndex: index + 2
70 toIndex: answer.length]
71 forKey: [answer substringFromIndex: 0
81 [self MPD_responseFromSocket: sock];
83 OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
85 OFMutableDictionary *response;
88 OFXMLElement *pubsub, *publish, *item, *tune;
90 tuneIQ = [XMPPIQ IQWithType: @"set"
91 ID: [object generateStanzaID]];
92 pubsub = [OFXMLElement elementWithName: @"pubsub"
93 namespace: NS_PUBSUB];
94 [tuneIQ addChild: pubsub];
95 publish = [OFXMLElement elementWithName: @"publish"
96 namespace: NS_PUBSUB];
97 [publish addAttributeWithName: @"node"
98 stringValue: NS_TUNE];
99 [pubsub addChild: publish];
100 item = [OFXMLElement elementWithName: @"item"
101 namespace: NS_PUBSUB];
102 [publish addChild: item];
103 tune = [OFXMLElement elementWithName: @"tune"
105 [item addChild: tune];
106 [sock writeLine: @"status"];
107 response = [self MPD_responseFromSocket: sock];
108 if ([[response objectForKey: @"state"]
110 [sock writeLine: @"currentsong"];
111 response = [self MPD_responseFromSocket: sock];
113 [response objectForKey: @"Artist"]))
114 [tune addChild: [OFXMLElement
115 elementWithName: @"artist"
117 stringValue: answer]];
118 if ((answer = [response objectForKey: @"Time"]))
119 [tune addChild: [OFXMLElement
120 elementWithName: @"length"
122 stringValue: answer]];
124 [response objectForKey: @"Album"]))
125 [tune addChild: [OFXMLElement
126 elementWithName: @"source"
128 stringValue: answer]];
130 [response objectForKey: @"Title"]))
131 [tune addChild: [OFXMLElement
132 elementWithName: @"title"
134 stringValue: answer]];
136 [response objectForKey: @"Track"]))
137 [tune addChild: [OFXMLElement
138 elementWithName: @"track"
140 stringValue: answer]];
142 [object sendStanza: tuneIQ];
143 [sock writeLine: @"idle player"];
144 [self MPD_responseFromSocket: sock];
147 [self MPD_responseFromSocket: sock];