]> git.babelmonkeys.de Git - mpdbot.git/blob - src/PEPThread.m
Fetch track information using comand lists
[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         [conn release];
35
36         [super dealloc];
37 }
38
39 - (id)main
40 {
41         OFString *mpd_host;
42         uint16_t mpd_port;
43         OFDictionary *environment = [OFApplication environment];
44         OFString *mpd_port_string;
45         mpd_host = [environment objectForKey: @"MPD_HOST"];
46         if (mpd_host == nil)
47                 mpd_host = @"localhost";
48         mpd_port_string = [environment objectForKey: @"MPD_PORT"];
49         if (mpd_port_string && [mpd_port_string decimalValue] <= UINT16_MAX)
50                 mpd_port = (uint16_t) [mpd_port_string decimalValue];
51         else
52                 mpd_port = 6600;
53         conn = [[MPDConnection alloc] initWithHost: mpd_host
54                                               port: mpd_port];
55         [conn connect];
56         while (1) {
57                 OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
58                 @try {
59                         OFMutableDictionary *response;
60                         OFString *answer;
61                         XMPPIQ *tuneIQ;
62                         OFXMLElement *pubsub, *publish, *item, *tune;
63
64                         tuneIQ = [XMPPIQ IQWithType: @"set"
65                                                  ID: [object generateStanzaID]];
66                         pubsub = [OFXMLElement elementWithName: @"pubsub"
67                                                      namespace: NS_PUBSUB];
68                         [tuneIQ addChild: pubsub];
69                         publish = [OFXMLElement elementWithName: @"publish"
70                                                       namespace: NS_PUBSUB];
71                         [publish addAttributeWithName: @"node"
72                                           stringValue: NS_TUNE];
73                         [pubsub addChild: publish];
74                         item = [OFXMLElement elementWithName: @"item"
75                                                    namespace: NS_PUBSUB];
76                         [publish addChild: item];
77                         tune = [OFXMLElement elementWithName: @"tune"
78                                                    namespace: NS_TUNE];
79                         [item addChild: tune];
80
81                         [conn send: @"command_list_begin"];
82                         [conn send: @"status"];
83                         [conn send: @"currentsong"];
84                         [conn send: @"command_list_end"];
85                         response = [conn response];
86
87                         if ([[response objectForKey: @"state"]
88                             isEqual: @"play"]) {
89                                 if ((answer =
90                                     [response objectForKey: @"Artist"]))
91                                         [tune addChild: [OFXMLElement
92                                             elementWithName: @"artist"
93                                                   namespace: NS_TUNE
94                                                 stringValue: answer]];
95                                 if ((answer = [response objectForKey: @"Time"]))
96                                         [tune addChild: [OFXMLElement
97                                             elementWithName: @"length"
98                                                   namespace: NS_TUNE
99                                                 stringValue: answer]];
100                                 if ((answer =
101                                     [response objectForKey: @"Album"]))
102                                         [tune addChild: [OFXMLElement
103                                             elementWithName: @"source"
104                                                   namespace: NS_TUNE
105                                                 stringValue: answer]];
106                                 if ((answer =
107                                     [response objectForKey: @"Title"]))
108                                         [tune addChild: [OFXMLElement
109                                             elementWithName: @"title"
110                                                   namespace: NS_TUNE
111                                                 stringValue: answer]];
112                                 else if ((answer =
113                                     [response objectForKey: @"file"]))
114                                         [tune addChild: [OFXMLElement
115                                             elementWithName: @"title"
116                                                   namespace: NS_TUNE
117                                                 stringValue: answer]];
118                                 if ((answer =
119                                     [response objectForKey: @"Track"]))
120                                         [tune addChild: [OFXMLElement
121                                             elementWithName: @"track"
122                                                   namespace: NS_TUNE
123                                                 stringValue: answer]];
124                         }
125                         [object sendStanza: tuneIQ];
126                         [conn idle];
127                 } @catch (id e) {
128                         [conn connect];
129                         [e release];
130                 }
131                 [pool release];
132         }
133
134         return nil;
135 }
136 @end