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>
38 OF_APPLICATION_DELEGATE(AppDelegate)
40 @implementation AppDelegate
41 - (void)applicationDidFinishLaunching
44 OFArray *arguments = [OFApplication arguments];
46 conn = [[XMPPConnection alloc] init];
49 if (arguments.count != 3) {
50 [of_stdout writeFormat: @"Usage: %@ <server> <user> <passwd>\n",
51 [OFApplication programName]];
52 [OFApplication terminateWithStatus: 1];
55 conn.domain = [arguments objectAtIndex: 0];
56 conn.username = [arguments objectAtIndex: 1];
57 conn.password = [arguments objectAtIndex: 2];
58 conn.resource = @"ObjXMPP";
62 [conn handleConnection];
68 - (void)connectionWasAuthenticated: (XMPPConnection*)conn
70 [of_stdout writeLine: @"Auth successful"];
73 - (void)connection: (XMPPConnection*)conn
74 wasBoundToJID: (XMPPJID*)jid
79 [of_stdout writeFormat: @"Bound to JID: %@\n", [jid fullJID]];
81 pres = [XMPPPresence presence];
82 [pres addPriority: 0];
83 [pres addStatus: @"Hello I'm MPDbot!"];
84 [conn sendStanza: pres];
86 discoID = [[conn generateStanzaID] retain];
87 disco = [XMPPIQ IQWithType: @"get"
89 disco.to = [XMPPJID JIDWithString: [[conn JID] bareJID]];
90 [disco addChild: [OFXMLElement
91 elementWithName: @"query"
92 namespace: NS_DISCO_INFO]];
94 [conn sendStanza: disco];
97 - (BOOL)connection: (XMPPConnection*)conn
98 didReceiveIQ: (XMPPIQ*)iq
100 OFXMLElement *query = [iq elementForName: @"query"
101 namespace: NS_DISCO_INFO];
102 if ([iq.ID isEqual: discoID]) {
103 for (OFXMLElement *identity
104 in [query elementsForName: @"identity"
105 namespace: NS_DISCO_INFO]) {
106 if ([[[identity attributeForName: @"category"]
107 stringValue] isEqual: @"pubsub"] &&
108 [[[identity attributeForName: @"type"] stringValue]
110 pepper = [[PEPThread alloc]
111 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 if ([pres.type isEqual: @"subscribe"]) {
132 XMPPPresence *answer;
133 answer = [XMPPPresence presenceWithType: @"subscribed"
135 answer.to = [XMPPJID JIDWithString: [pres.from bareJID]];
136 [conn sendStanza: answer];
140 - (void)connectionWasClosed: (XMPPConnection*)conn
142 [of_stdout writeLine: @"Connection was closed!"];