2 * Copyright (c) 2010, 2011, Jonathan Schleifer <js@webkeks.org>
3 * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
5 * http://cgit.babelmonkeys.de/cgit.cgi/mpdbot/
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.
24 #import <ObjFW/ObjFW.h>
25 #import <ObjXMPP/ObjXMPP.h>
29 #define NS_DISCO_INFO @"http://jabber.org/protocol/disco#info"
31 @interface AppDelegate: OFObject
32 #ifdef OF_HAVE_OPTIONAL_PROTOCOLS
33 <OFApplicationDelegate, XMPPConnectionDelegate>
40 OF_APPLICATION_DELEGATE(AppDelegate)
42 @implementation AppDelegate
43 - (void)applicationDidFinishLaunching
46 OFArray *arguments = [OFApplication arguments];
48 conn = [[XMPPConnection alloc] init];
49 [conn setDelegate: self];
51 if ([arguments count] != 3) {
52 of_log(@"Invalid count of command line arguments!");
53 [OFApplication terminateWithStatus: 1];
56 [conn setDomain: [arguments objectAtIndex: 0]];
57 [conn setUsername: [arguments objectAtIndex: 1]];
58 [conn setPassword: [arguments objectAtIndex: 2]];
59 [conn setResource: @"ObjXMPP"];
63 [conn handleConnection];
69 - (void)connectionWasAuthenticated: (XMPPConnection*)conn
71 of_log(@"Auth successful");
74 - (void)connection: (XMPPConnection*)conn
75 wasBoundToJID: (XMPPJID*)jid
80 of_log(@"Bound to JID: %@", [jid fullJID]);
82 pres = [XMPPPresence presence];
83 [pres addPriority: 0];
84 [pres addStatus: @"Hello I'm MPDbot!"];
85 [conn sendStanza: pres];
87 discoID = [[conn generateStanzaID] retain];
88 disco = [XMPPIQ IQWithType: @"get"
90 disco.to = [XMPPJID JIDWithString: [[conn JID] bareJID]];
91 [disco addChild: [OFXMLElement
92 elementWithName: @"query"
93 namespace: NS_DISCO_INFO]];
95 [conn sendStanza: disco];
98 - (BOOL)connection: (XMPPConnection*)conn
99 didReceiveIQ: (XMPPIQ*)iq
101 OFXMLElement *query = [iq elementForName: @"query"
102 namespace: NS_DISCO_INFO];
103 if ([[iq ID] isEqual: discoID]) {
104 for (OFXMLElement *identity
105 in [query elementsForName: @"identity"
106 namespace: NS_DISCO_INFO]) {
107 if ([[[identity attributeForName: @"category"]
108 stringValue] isEqual: @"pubsub"] &&
109 [[[identity attributeForName: @"type"] stringValue]
111 pepper = [[PEPThread alloc]
112 initWithObject: conn];
122 - (void)connection: (XMPPConnection*)conn
123 didReceiveMessage: (XMPPMessage*)msg
125 of_log(@"Message: %@", msg);
128 - (void)connection: (XMPPConnection*)conn
129 didReceivePresence: (XMPPPresence*)pres
131 of_log(@"Presence: %@", pres);
134 - (void)connectionWasClosed: (XMPPConnection*)conn
136 of_log(@"Connection was closed!");