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>
37 OF_APPLICATION_DELEGATE(AppDelegate)
39 @implementation AppDelegate
40 - (void)applicationDidFinishLaunching
43 OFArray *arguments = [OFApplication arguments];
45 conn = [[XMPPConnection alloc] init];
46 [conn addDelegate: self];
48 if (arguments.count != 3) {
49 [of_stdout writeFormat: @"Usage: %@ <server> <user> <passwd>\n",
50 [OFApplication programName]];
51 [OFApplication terminateWithStatus: 1];
54 conn.domain = [arguments objectAtIndex: 0];
55 conn.username = [arguments objectAtIndex: 1];
56 conn.password = [arguments objectAtIndex: 2];
57 conn.resource = @"ObjXMPP";
61 [conn handleConnection];
63 [of_stderr writeFormat: @"%@\n", e];
67 - (void)connectionWasAuthenticated: (XMPPConnection*)conn
69 [of_stdout writeLine: @"Auth successful"];
72 - (void)connection: (XMPPConnection*)conn
73 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]];
95 withCallbackObject: self
96 selector: @selector(mpdbot_handleDiscoForConnection:withIQ:)];
99 - (BOOL)mpdbot_handleDiscoForConnection: (XMPPConnection*)conn
102 OFXMLElement *query = [iq elementForName: @"query"
103 namespace: NS_DISCO_INFO];
104 for (OFXMLElement *identity 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];
117 [of_stderr writeLine: @"Server does NOT support PEP"];
122 - (void)connection: (XMPPConnection*)conn
123 didReceivePresence: (XMPPPresence*)pres
125 if ([pres.type isEqual: @"subscribe"]) {
126 XMPPPresence *answer;
127 answer = [XMPPPresence presenceWithType: @"subscribed"
129 answer.to = [XMPPJID JIDWithString: [pres.from bareJID]];
130 [conn sendStanza: answer];
134 - (void)connectionWasClosed: (XMPPConnection*)conn
136 [of_stdout writeLine: @"Connection was closed!"];