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 <OFApplicationDelegate, XMPPConnectionDelegate>
36 OF_APPLICATION_DELEGATE(AppDelegate)
38 @implementation AppDelegate
39 - (void)applicationDidFinishLaunching
42 OFArray *arguments = [OFApplication arguments];
44 conn = [[XMPPConnection alloc] init];
47 if (arguments.count != 3) {
48 [of_stdout writeFormat: @"Usage: %@ <server> <user> <passwd>\n",
49 [OFApplication programName]];
50 [OFApplication terminateWithStatus: 1];
53 conn.domain = [arguments objectAtIndex: 0];
54 conn.username = [arguments objectAtIndex: 1];
55 conn.password = [arguments objectAtIndex: 2];
56 conn.resource = @"ObjXMPP";
60 [conn handleConnection];
66 - (void)connectionWasAuthenticated: (XMPPConnection*)conn
68 [of_stdout writeLine: @"Auth successful"];
71 - (void)connection: (XMPPConnection*)conn
72 wasBoundToJID: (XMPPJID*)jid
77 [of_stdout writeFormat: @"Bound to JID: %@\n", [jid fullJID]];
79 pres = [XMPPPresence presence];
80 [pres addPriority: 0];
81 [pres addStatus: @"Hello I'm MPDbot!"];
82 [conn sendStanza: pres];
84 discoID = [[conn generateStanzaID] retain];
85 disco = [XMPPIQ IQWithType: @"get"
87 disco.to = [XMPPJID JIDWithString: [[conn JID] bareJID]];
88 [disco addChild: [OFXMLElement
89 elementWithName: @"query"
90 namespace: NS_DISCO_INFO]];
92 [conn sendStanza: disco];
95 - (BOOL)connection: (XMPPConnection*)conn
96 didReceiveIQ: (XMPPIQ*)iq
98 OFXMLElement *query = [iq elementForName: @"query"
99 namespace: NS_DISCO_INFO];
100 if ([iq.ID isEqual: discoID]) {
101 for (OFXMLElement *identity
102 in [query elementsForName: @"identity"
103 namespace: NS_DISCO_INFO]) {
104 if ([[[identity attributeForName: @"category"]
105 stringValue] isEqual: @"pubsub"] &&
106 [[[identity attributeForName: @"type"] stringValue]
108 pepper = [[PEPThread alloc]
109 initWithObject: conn];
120 - (void)connection: (XMPPConnection*)conn
121 didReceiveMessage: (XMPPMessage*)msg
123 of_log(@"Message: %@", msg);
126 - (void)connection: (XMPPConnection*)conn
127 didReceivePresence: (XMPPPresence*)pres
129 if ([pres.type isEqual: @"subscribe"]) {
130 XMPPPresence *answer;
131 answer = [XMPPPresence presenceWithType: @"subscribed"
133 answer.to = [XMPPJID JIDWithString: [pres.from bareJID]];
134 [conn sendStanza: answer];
138 - (void)connectionWasClosed: (XMPPConnection*)conn
140 [of_stdout writeLine: @"Connection was closed!"];