/*
 * Copyright (c) 2010, 2011, Jonathan Schleifer <js@webkeks.org>
 * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
 *
 * http://cgit.babelmonkeys.de/cgit.cgi/mpdbot/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#import <ObjFW/ObjFW.h>
#import <ObjXMPP/ObjXMPP.h>

#import "PEPThread.h"

#define NS_DISCO_INFO @"http://jabber.org/protocol/disco#info"

@interface AppDelegate: OFObject <OFApplicationDelegate, XMPPConnectionDelegate>
{
OFString *discoID;
PEPThread *pepper;
}
@end

OF_APPLICATION_DELEGATE(AppDelegate)

@implementation AppDelegate
- (void)applicationDidFinishLaunching
{
	XMPPConnection *conn;
	OFArray *arguments = [OFApplication arguments];

	conn = [[XMPPConnection alloc] init];
	[conn addDelegate: self];

	if (arguments.count != 3) {
		[of_stdout writeFormat: @"Usage: %@ <server> <user> <passwd>\n",
		    [OFApplication programName]];
		[OFApplication terminateWithStatus: 1];
	}

	conn.domain = [arguments objectAtIndex: 0];
	conn.username = [arguments objectAtIndex: 1];
	conn.password = [arguments objectAtIndex: 2];
	conn.resource = @"ObjXMPP";

	@try {
		[conn connect];
		[conn handleConnection];
	} @catch (id e) {
		[of_stderr writeFormat: @"%@\n", e];
	}
}

- (void)connectionWasAuthenticated: (XMPPConnection*)conn
{
	[of_stdout writeLine: @"Auth successful"];
}

- (void)connection: (XMPPConnection*)conn
     wasBoundToJID: (XMPPJID*)jid
{
	XMPPPresence *pres;
	XMPPIQ *disco;

	[of_stdout writeFormat: @"Bound to JID: %@\n", [jid fullJID]];

	pres = [XMPPPresence presence];
	[pres addPriority: 0];
	[pres addStatus: @"Hello I'm MPDbot!"];
	[conn sendStanza: pres];

	discoID = [[conn generateStanzaID] retain];
	disco = [XMPPIQ IQWithType: @"get"
				ID: discoID];
	disco.to = [XMPPJID JIDWithString: [[conn JID] bareJID]];
	[disco addChild: [OFXMLElement
	    elementWithName: @"query"
		  namespace: NS_DISCO_INFO]];

	[conn sendStanza: disco];
}

- (BOOL)connection: (XMPPConnection*)conn
      didReceiveIQ: (XMPPIQ*)iq
{
	OFXMLElement *query = [iq elementForName: @"query"
				       namespace: NS_DISCO_INFO];
	if ([iq.ID isEqual: discoID]) {
		for (OFXMLElement *identity
		    in [query elementsForName: @"identity"
				    namespace: NS_DISCO_INFO]) {
			if ([[[identity attributeForName: @"category"]
			    stringValue] isEqual: @"pubsub"] &&
			    [[[identity attributeForName: @"type"] stringValue]
			    isEqual: @"pep"]) {
				pepper = [[PEPThread alloc]
				    initWithObject: conn];
				[pepper start];

				return YES;
			}
		}
		[of_stderr writeLine: @"Server does NOT support PEP"];
	}

	return NO;
}

-   (void)connection: (XMPPConnection*)conn
  didReceivePresence: (XMPPPresence*)pres
{
	if ([pres.type isEqual: @"subscribe"]) {
		XMPPPresence *answer;
		answer = [XMPPPresence presenceWithType: @"subscribed"
						     ID: pres.ID];
		answer.to = [XMPPJID JIDWithString: [pres.from bareJID]];
		[conn sendStanza: answer];
	}
}

- (void)connectionWasClosed: (XMPPConnection*)conn
{
	[of_stdout writeLine: @"Connection was closed!"];
}
@end
