]> cgit.babelmonkeys.de Git - mpdbot.git/blobdiff - src/mpdbot.m
Use new callback API of objXMPP
[mpdbot.git] / src / mpdbot.m
index 1c7ab7d4ac18cdcee606e4230ada7da49953cd92..eb92876880af712e30f7ee333ce276ed681dea54 100644 (file)
 
 #define NS_DISCO_INFO @"http://jabber.org/protocol/disco#info"
 
-@interface AppDelegate: OFObject
-#ifdef OF_HAVE_OPTIONAL_PROTOCOLS
-    <OFApplicationDelegate, XMPPConnectionDelegate>
-#endif
-
-OFString *discoID;
-PEPThread *pepper;
+@interface AppDelegate: OFObject <OFApplicationDelegate, XMPPConnectionDelegate>
+{
+       PEPThread *pepper;
+}
 @end
 
 OF_APPLICATION_DELEGATE(AppDelegate)
@@ -46,24 +43,24 @@ OF_APPLICATION_DELEGATE(AppDelegate)
        OFArray *arguments = [OFApplication arguments];
 
        conn = [[XMPPConnection alloc] init];
-       [conn setDelegate: self];
+       [conn addDelegate: self];
 
-       if ([arguments count] != 3) {
+       if (arguments.count != 3) {
                [of_stdout writeFormat: @"Usage: %@ <server> <user> <passwd>\n",
                    [OFApplication programName]];
                [OFApplication terminateWithStatus: 1];
        }
 
-       [conn setDomain: [arguments objectAtIndex: 0]];
-       [conn setUsername: [arguments objectAtIndex: 1]];
-       [conn setPassword: [arguments objectAtIndex: 2]];
-       [conn setResource: @"ObjXMPP"];
+       conn.domain = [arguments objectAtIndex: 0];
+       conn.username = [arguments objectAtIndex: 1];
+       conn.password = [arguments objectAtIndex: 2];
+       conn.resource = @"ObjXMPP";
 
        @try {
                [conn connect];
                [conn handleConnection];
        } @catch (id e) {
-               of_log(@"%@", e);
+               [of_stderr writeFormat: @"%@\n", e];
        }
 }
 
@@ -77,6 +74,7 @@ OF_APPLICATION_DELEGATE(AppDelegate)
 {
        XMPPPresence *pres;
        XMPPIQ *disco;
+       OFString *discoID;
 
        [of_stdout writeFormat: @"Bound to JID: %@\n", [jid fullJID]];
 
@@ -90,42 +88,37 @@ OF_APPLICATION_DELEGATE(AppDelegate)
                                ID: discoID];
        disco.to = [XMPPJID JIDWithString: [[conn JID] bareJID]];
        [disco addChild: [OFXMLElement
-       elementWithName: @"query"
-             namespace: NS_DISCO_INFO]];
+           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
 {