2 * Copyright (c) 2010, 2011, Jonathan Schleifer <js@webkeks.org>
3 * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
5 * https://webkeks.org/hg/objxmpp/
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice is present in all copies.
11 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
12 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
15 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
16 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
17 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
18 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
19 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21 * POSSIBILITY OF SUCH DAMAGE.
26 #import <ObjFW/ObjFW.h>
27 #import <ObjXMPP/ObjXMPP.h>
31 #define NS_DISCO_INFO @"http://jabber.org/protocol/disco#info"
33 @interface AppDelegate: OFObject
34 #ifdef OF_HAVE_OPTIONAL_PROTOCOLS
35 <OFApplicationDelegate, XMPPConnectionDelegate>
42 OF_APPLICATION_DELEGATE(AppDelegate)
44 @implementation AppDelegate
45 - (void)applicationDidFinishLaunching
48 OFArray *arguments = [OFApplication arguments];
50 conn = [[XMPPConnection alloc] init];
51 [conn setDelegate: self];
53 if ([arguments count] != 3) {
54 of_log(@"Invalid count of command line arguments!");
55 [OFApplication terminateWithStatus: 1];
58 [conn setDomain: [arguments objectAtIndex: 0]];
59 [conn setUsername: [arguments objectAtIndex: 1]];
60 [conn setPassword: [arguments objectAtIndex: 2]];
61 [conn setResource: @"ObjXMPP"];
65 [conn handleConnection];
71 - (void)connectionWasAuthenticated: (XMPPConnection*)conn
73 of_log(@"Auth successful");
76 - (void)connection: (XMPPConnection*)conn
77 wasBoundToJID: (XMPPJID*)jid
82 of_log(@"Bound to JID: %@", [jid fullJID]);
84 pres = [XMPPPresence presence];
85 [pres addPriority: 0];
86 [pres addStatus: @"Hello I'm MPDbot!"];
87 [conn sendStanza: pres];
89 discoID = [[conn generateStanzaID] retain];
90 disco = [XMPPIQ IQWithType: @"get"
92 disco.to = [XMPPJID JIDWithString: [[conn JID] bareJID]];
93 [disco addChild: [OFXMLElement
94 elementWithName: @"query"
95 namespace: NS_DISCO_INFO]];
97 [conn sendStanza: disco];
100 - (BOOL)connection: (XMPPConnection*)conn
101 didReceiveIQ: (XMPPIQ*)iq
103 OFXMLElement *query = [iq elementForName: @"query"
104 namespace: NS_DISCO_INFO];
105 if ([[iq ID] isEqual: discoID]) {
106 for (OFXMLElement *identity
107 in [query elementsForName: @"identity"
108 namespace: NS_DISCO_INFO]) {
109 if ([[[identity attributeForName: @"category"]
110 stringValue] isEqual: @"pubsub"] &&
111 [[[identity attributeForName: @"type"] stringValue]
113 pepper = [[PEPThread alloc]
114 initWithObject: conn];
124 - (void)connection: (XMPPConnection*)conn
125 didReceiveMessage: (XMPPMessage*)msg
127 of_log(@"Message: %@", msg);
130 - (void)connection: (XMPPConnection*)conn
131 didReceivePresence: (XMPPPresence*)pres
133 of_log(@"Presence: %@", pres);
136 - (void)connectionWasClosed: (XMPPConnection*)conn
138 of_log(@"Connection was closed!");