1 #import <ObjFW/ObjFW.h>
2 #import <ObjXMPP/ObjXMPP.h>
6 #define NS_PUBSUB @"http://jabber.org/protocol/pubsub"
7 #define NS_TUNE @"http://jabber.org/protocol/tune"
9 @implementation PEPThread: OFThread
23 sock = [[OFTCPSocket alloc] init];
24 [sock connectToHost: @"localhost"
28 of_log(@"Connection failed, retrying in %" PRIi64
30 [OFThread sleepForTimeInterval: pause];
38 - (OFMutableDictionary*)MPD_responseFromSocket: (OFTCPSocket*)sock
41 OFMutableDictionary *response = [OFMutableDictionary dictionary];
42 while ((answer = [sock readLine]) && ![answer hasPrefix: @"OK"]) {
44 index = [answer indexOfFirstOccurrenceOfString: @":"];
45 if (index == OF_INVALID_INDEX)
47 [response setObject: [answer substringFromIndex: index + 2
48 toIndex: answer.length]
49 forKey: [answer substringFromIndex: 0
59 [self MPD_responseFromSocket: sock];
61 OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
63 OFMutableDictionary *response;
66 OFXMLElement *pubsub, *publish, *item, *tune;
68 tuneIQ = [XMPPIQ IQWithType: @"set"
69 ID: [object generateStanzaID]];
70 pubsub = [OFXMLElement elementWithName: @"pubsub"
71 namespace: NS_PUBSUB];
72 [tuneIQ addChild: pubsub];
73 publish = [OFXMLElement elementWithName: @"publish"
74 namespace: NS_PUBSUB];
75 [publish addAttributeWithName: @"node"
76 stringValue: NS_TUNE];
77 [pubsub addChild: publish];
78 item = [OFXMLElement elementWithName: @"item"
79 namespace: NS_PUBSUB];
80 [publish addChild: item];
81 tune = [OFXMLElement elementWithName: @"tune"
83 [item addChild: tune];
84 [sock writeLine: @"status"];
85 response = [self MPD_responseFromSocket: sock];
86 if ([[response objectForKey: @"state"]
88 [sock writeLine: @"currentsong"];
89 response = [self MPD_responseFromSocket: sock];
91 [response objectForKey: @"Artist"]))
92 [tune addChild: [OFXMLElement
93 elementWithName: @"artist"
95 stringValue: answer]];
96 if ((answer = [response objectForKey: @"Time"]))
97 [tune addChild: [OFXMLElement
98 elementWithName: @"length"
100 stringValue: answer]];
102 [response objectForKey: @"Album"]))
103 [tune addChild: [OFXMLElement
104 elementWithName: @"source"
106 stringValue: answer]];
108 [response objectForKey: @"Title"]))
109 [tune addChild: [OFXMLElement
110 elementWithName: @"title"
112 stringValue: answer]];
114 [response objectForKey: @"Track"]))
115 [tune addChild: [OFXMLElement
116 elementWithName: @"track"
118 stringValue: answer]];
120 [object sendStanza: tuneIQ];
121 [sock writeLine: @"idle player"];
122 [self MPD_responseFromSocket: sock];
125 [self MPD_responseFromSocket: sock];