/*
Tlen Protocol Plugin for Miranda NG
Copyright (C) 2004-2007  Piotr Piastucki
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#include "stdafx.h"
#include "tlen_list.h"
#include "tlen_iq.h"
#include "resource.h"
#include "tlen_muc.h"
static int TlenMUCHandleEvent(void *ptr, WPARAM wParam, LPARAM lParam);
static int TlenMUCQueryContacts(TlenProtocol *proto, const char *roomId);
static int TlenMUCSendInvitation(TlenProtocol *proto, const char *roomID, const char *user);
static int TlenMUCSendPresence(TlenProtocol *proto, const char *roomID, const char *nick, int desiredStatus);
static int TlenMUCSendQuery(TlenProtocol *proto, int type, const char *parent, int page);
static int isSelf(TlenProtocol *proto, const char *roomID, const char *nick)
{
	TLEN_LIST_ITEM *item;
	int result;
	result=0;
	item = TlenListGetItemPtr(proto, LIST_CHATROOM, roomID);
	if (item != NULL) {
		if (item->nick == NULL) {
			if (!mir_strcmp(nick, proto->threadData->username)) result = 1;
		} else if (nick[0] == '~') {
			if (!mir_strcmp(nick+1, item->nick)) {
				result = 1;
			}
		}
	}
	return result;
}
static int stringToHex(const char *str)
{
	int i, val;
	val = 0;
	for (i=0;i<2;i++) {
		val <<= 4;
		if (str[i] >= 'A' && str[i] <= 'F') {
			val += 10 + str[i]-'A';
		} else if (str[i] >= '0' && str[i] <= '9') {
			val += str[i]-'0';
		}
	}
	return val;
}
static char *getDisplayName(TlenProtocol *proto, const char *id)
{
	char jid[256];
	MCONTACT hContact;
	DBVARIANT dbv;
	if (!db_get(NULL, proto->m_szModuleName, "LoginServer", &dbv)) {
		mir_snprintf(jid, "%s@%s", id, dbv.pszVal);
		db_free(&dbv);
		if (((hContact = TlenHContactFromJID(proto, jid)) != NULL) || !mir_strcmp(id, proto->threadData->username)) {
			ptrW szName(Contact_GetInfo(CNF_DISPLAY, hContact, proto->m_szModuleName));
			if (szName)
				return mir_u2a(szName);
		}
	}
	return mir_strdup(id);
}
int TlenMUCRecvInvitation(TlenProtocol *proto, const char *roomId, const char*, const char *from, const char*)
{
	int	 ignore, ask, groupChatPolicy;
	if (roomId == NULL) return 1;
	groupChatPolicy = db_get_w(NULL, proto->m_szModuleName, "GroupChatPolicy", 0);
	ask = TRUE;
	ignore = FALSE;
	if (groupChatPolicy == TLEN_MUC_ASK) {
		ignore = FALSE;
		ask = TRUE;
	} else if (groupChatPolicy == TLEN_MUC_IGNORE_ALL) {
		ignore = TRUE;
	} else if (groupChatPolicy == TLEN_MUC_IGNORE_NIR) {
		char jid[256];
		DBVARIANT dbv;
		if (!db_get(NULL, proto->m_szModuleName, "LoginServer", &dbv)) {
			mir_snprintf(jid, "%s@%s", from, dbv.pszVal);
			db_free(&dbv);
		} else {
			mir_strcpy(jid, from);
		}
		ignore = !IsAuthorized(proto, jid);
		ask = TRUE;
	} else if (groupChatPolicy == TLEN_MUC_ACCEPT_IR) {
		char jid[256];
		TLEN_LIST_ITEM *item;
		DBVARIANT dbv;
		if (!db_get(NULL, proto->m_szModuleName, "LoginServer", &dbv)) {
			mir_snprintf(jid, "%s@%s", from, dbv.pszVal);
			db_free(&dbv);
		} else {
			mir_strcpy(jid, from);
		}
		item = TlenListGetItemPtr(proto, LIST_ROSTER, jid);
		ask = !IsAuthorized(proto, jid);
		ignore = FALSE;
	} else if (groupChatPolicy == TLEN_MUC_ACCEPT_ALL) {
		ask = FALSE;
		ignore = FALSE;
	}
	return 0;
}
static int TlenMUCSendInvitation(TlenProtocol *proto, const char *roomID, const char *user)
{
	if (!proto->isOnline) {
		return 1;
	}
	TlenSend(proto, "
unavailable