]> git.babelmonkeys.de Git - mpdbot.git/blob - src/PEPThread.m
e00db9d8597aebcfc6ada79fa3c27af2b49e3742
[mpdbot.git] / src / PEPThread.m
1 /*
2  * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
3  *
4  * http://cgit.babelmonkeys.de/cgit.cgi/mpdbot/
5  *
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.
9  *
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.
21  */
22
23 #import <ObjFW/ObjFW.h>
24 #import <ObjXMPP/ObjXMPP.h>
25
26 #import "PEPThread.h"
27
28 #define NS_PUBSUB @"http://jabber.org/protocol/pubsub"
29 #define NS_TUNE @"http://jabber.org/protocol/tune"
30
31 @implementation PEPThread: OFThread
32 - (void)dealloc
33 {
34         [sock release];
35
36         [super dealloc];
37 }
38
39 - (void)MPD_connect
40 {
41         int64_t pause = 1;
42         while(1) {
43                 @try {
44                         [sock release];
45                         sock = [[OFTCPSocket alloc] init];
46                         [sock connectToHost: @"localhost"
47                                        port: 6600];
48                         return;
49                 } @catch (id e) {
50                         [of_stderr writeFormat: @"Connection failed, retrying"
51                                 " in %" PRIi64 " seconds\n", pause];
52                         [OFThread sleepForTimeInterval: pause];
53                         if (pause < 120)
54                                 pause *= 2;
55                         [e release];
56                 }
57         }
58 }
59
60 - (OFMutableDictionary*)MPD_responseFromSocket: (OFTCPSocket*)sock
61 {
62         OFString *answer;
63         OFMutableDictionary *response = [OFMutableDictionary dictionary];
64         while ((answer = [sock readLine]) && ![answer hasPrefix: @"OK"]) {
65                 size_t index;
66                 index = [answer indexOfFirstOccurrenceOfString: @":"];
67                 if (index == OF_INVALID_INDEX)
68                         continue;
69                 [response setObject: [answer substringFromIndex: index + 2
70                                                         toIndex: answer.length]
71                              forKey: [answer substringFromIndex: 0
72                                                         toIndex: index]];
73         }
74
75         return response;
76 }
77
78 - (id)main
79 {
80         [self MPD_connect];
81         [self MPD_responseFromSocket: sock];
82         while (1) {
83                 OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
84                 @try {
85                         OFMutableDictionary *response;
86                         OFString *answer;
87                         XMPPIQ *tuneIQ;
88                         OFXMLElement *pubsub, *publish, *item, *tune;
89
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"
104                                                    namespace: NS_TUNE];
105                         [item addChild: tune];
106                         [sock writeLine: @"status"];
107                         response = [self MPD_responseFromSocket: sock];
108                         if ([[response objectForKey: @"state"]
109                             isEqual: @"play"]) {
110                                 [sock writeLine: @"currentsong"];
111                                 response = [self MPD_responseFromSocket: sock];
112                                 if ((answer =
113                                     [response objectForKey: @"Artist"]))
114                                         [tune addChild: [OFXMLElement
115                                             elementWithName: @"artist"
116                                                   namespace: NS_TUNE
117                                                 stringValue: answer]];
118                                 if ((answer = [response objectForKey: @"Time"]))
119                                         [tune addChild: [OFXMLElement
120                                             elementWithName: @"length"
121                                                   namespace: NS_TUNE
122                                                 stringValue: answer]];
123                                 if ((answer =
124                                     [response objectForKey: @"Album"]))
125                                         [tune addChild: [OFXMLElement
126                                             elementWithName: @"source"
127                                                   namespace: NS_TUNE
128                                                 stringValue: answer]];
129                                 if ((answer =
130                                     [response objectForKey: @"Title"]))
131                                         [tune addChild: [OFXMLElement
132                                             elementWithName: @"title"
133                                                   namespace: NS_TUNE
134                                                 stringValue: answer]];
135                                 if ((answer =
136                                     [response objectForKey: @"Track"]))
137                                         [tune addChild: [OFXMLElement
138                                             elementWithName: @"track"
139                                                   namespace: NS_TUNE
140                                                 stringValue: answer]];
141                         }
142                         [object sendStanza: tuneIQ];
143                         [sock writeLine: @"idle player"];
144                         [self MPD_responseFromSocket: sock];
145                 } @catch (id e) {
146                         [self MPD_connect];
147                         [self MPD_responseFromSocket: sock];
148                         [e release];
149                 }
150                 [pool release];
151         }
152
153         return nil;
154 }
155 @end