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_stdout writeFormat: @"Usage: %@ <server> <user> <passwd>\n",
53 [OFApplication programName]];
54 [OFApplication terminateWithStatus: 1];
57 [conn setDomain: [arguments objectAtIndex: 0]];
58 [conn setUsername: [arguments objectAtIndex: 1]];
59 [conn setPassword: [arguments objectAtIndex: 2]];
60 [conn setResource: @"ObjXMPP"];
64 [conn handleConnection];
70 - (void)connectionWasAuthenticated: (XMPPConnection*)conn
72 [of_stdout writeLine: @"Auth successful"];
75 - (void)connection: (XMPPConnection*)conn
76 wasBoundToJID: (XMPPJID*)jid
81 [of_stdout writeFormat: @"Bound to JID: %@\n", [jid fullJID]];
83 pres = [XMPPPresence presence];
84 [pres addPriority: 0];
85 [pres addStatus: @"Hello I'm MPDbot!"];
86 [conn sendStanza: pres];
88 discoID = [[conn generateStanzaID] retain];
89 disco = [XMPPIQ IQWithType: @"get"
91 disco.to = [XMPPJID JIDWithString: [[conn JID] bareJID]];
92 [disco addChild: [OFXMLElement
93 elementWithName: @"query"
94 namespace: NS_DISCO_INFO]];
96 [conn sendStanza: disco];
99 - (BOOL)connection: (XMPPConnection*)conn
100 didReceiveIQ: (XMPPIQ*)iq
102 OFXMLElement *query = [iq elementForName: @"query"
103 namespace: NS_DISCO_INFO];
104 if ([[iq ID] isEqual: discoID]) {
105 for (OFXMLElement *identity
106 in [query elementsForName: @"identity"
107 namespace: NS_DISCO_INFO]) {
108 if ([[[identity attributeForName: @"category"]
109 stringValue] isEqual: @"pubsub"] &&
110 [[[identity attributeForName: @"type"] stringValue]
112 pepper = [[PEPThread alloc]
113 initWithObject: conn];
123 - (void)connection: (XMPPConnection*)conn
124 didReceiveMessage: (XMPPMessage*)msg
126 of_log(@"Message: %@", msg);
129 - (void)connection: (XMPPConnection*)conn
130 didReceivePresence: (XMPPPresence*)pres
132 if ([pres.type isEqual: @"subscribe"]) {
133 XMPPPresence *answer;
134 answer = [XMPPPresence presenceWithType: @"subscribed"
136 answer.to = [XMPPJID JIDWithString: [pres.from bareJID]];
137 [conn sendStanza: answer];
141 - (void)connectionWasClosed: (XMPPConnection*)conn
143 [of_stdout writeLine: @"Connection was closed!"];