]> cgit.babelmonkeys.de Git - mpdbot.git/blobdiff - src/mpdbot.m
Initial command support
[mpdbot.git] / src / mpdbot.m
index 84f864deb653e58f4fa2ef7929dc792d0120cfa8..8ef781b51057eb7dc1a0ccb2df1a30ae30b03701 100644 (file)
@@ -30,8 +30,8 @@
 
 @interface AppDelegate: OFObject <OFApplicationDelegate, XMPPConnectionDelegate>
 {
-OFString *discoID;
-PEPThread *pepper;
+       PEPThread *pepper;
+       MPDConnection *mpdConn;
 }
 @end
 
@@ -41,10 +41,16 @@ 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];
-       conn.delegate = self;
+       [conn addDelegate: self];
 
        if (arguments.count != 3) {
                [of_stdout writeFormat: @"Usage: %@ <server> <user> <passwd>\n",
@@ -57,11 +63,25 @@ 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];
        } @catch (id e) {
-               of_log(@"%@", e);
+               [of_stderr writeFormat: @"%@\n", e];
        }
 }
 
@@ -75,6 +95,7 @@ OF_APPLICATION_DELEGATE(AppDelegate)
 {
        XMPPPresence *pres;
        XMPPIQ *disco;
+       OFString *discoID;
 
        [of_stdout writeFormat: @"Bound to JID: %@\n", [jid fullJID]];
 
@@ -91,40 +112,33 @@ OF_APPLICATION_DELEGATE(AppDelegate)
            elementWithName: @"query"
                  namespace: NS_DISCO_INFO]];
 
-       [conn sendStanza: disco];
+       [conn       sendIQ: disco
+       withCallbackObject: self
+                 selector: @selector(mpdbot_handleDiscoForConnection:withIQ:)];
 }
 
-- (BOOL)connection: (XMPPConnection*)conn
-      didReceiveIQ: (XMPPIQ*)iq
+- (BOOL)mpdbot_handleDiscoForConnection: (XMPPConnection*)conn
+                                withIQ: (XMPPIQ*)iq
 {
        OFXMLElement *query = [iq elementForName: @"query"
                                       namespace: NS_DISCO_INFO];
-       if ([iq.ID isEqual: discoID]) {
-               for (OFXMLElement *identity
-                   in [query elementsForName: @"identity"
-                                   namespace: NS_DISCO_INFO]) {
-                       if ([[[identity attributeForName: @"category"]
-                           stringValue] isEqual: @"pubsub"] &&
-                           [[[identity attributeForName: @"type"] stringValue]
-                           isEqual: @"pep"]) {
-                               pepper = [[PEPThread alloc]
-                                   initWithObject: conn];
-                               [pepper start];
-
-                               return YES;
-                       }
+       for (OFXMLElement *identity in [query elementsForName: @"identity"
+                                                   namespace: NS_DISCO_INFO]) {
+               if ([[[identity attributeForName: @"category"]
+                   stringValue] isEqual: @"pubsub"] &&
+                   [[[identity attributeForName: @"type"] stringValue]
+                   isEqual: @"pep"]) {
+                       pepper = [[PEPThread alloc] initWithObject: conn];
+                       [pepper start];
+
+                       return YES;
                }
        }
+       [of_stderr writeLine: @"Server does NOT support PEP"];
 
        return NO;
 }
 
--  (void)connection: (XMPPConnection*)conn
-  didReceiveMessage: (XMPPMessage*)msg
-{
-       of_log(@"Message: %@", msg);
-}
-
 -   (void)connection: (XMPPConnection*)conn
   didReceivePresence: (XMPPPresence*)pres
 {
@@ -137,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!"];