/*
Plugin of Miranda IM for communicating with users of the MSN Messenger protocol.
Copyright (c) 2012-2014 Miranda NG Team
Copyright (c) 2006-2012 Boris Krasnovskiy.
Copyright (c) 2003-2005 George Hazan.
Copyright (c) 2002-2003 Richard Hughes (original version).
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, see .
*/
#include "stdafx.h"
#include "msn_proto.h"
#include "m_smileyadd.h"
void CMsnProto::Lists_Uninit(void)
{
	Lists_Wipe();
}
void CMsnProto::Lists_Wipe(void)
{
	mir_cslock lck(m_csLists);
	m_arContacts.destroy();
}
bool CMsnProto::Lists_IsInList(int list, const char* email)
{
	mir_cslock lck(m_csLists);
	MsnContact *p = m_arContacts.find((MsnContact*)&email);
	if (p == NULL)
		return false;
	if (list == -1)
		return true;
	return (p->list & list) == list;
}
MsnContact* CMsnProto::Lists_Get(const char* email)
{
	mir_cslock lck(m_csLists);
	return m_arContacts.find((MsnContact*)&email);
}
MsnContact* CMsnProto::Lists_Get(MCONTACT hContact)
{
	mir_cslock lck(m_csLists);
	for (int i = 0; i < m_arContacts.getCount(); ++i)
		if (m_arContacts[i].hContact == hContact)
			return &m_arContacts[i];
	return NULL;
}
MsnPlace* CMsnProto::Lists_GetPlace(const char* wlid)
{
	mir_cslock lck(m_csLists);
	char *szEmail, *szInst;
	parseWLID(NEWSTR_ALLOCA(wlid), NULL, &szEmail, &szInst);
	if (szInst == NULL)
		szInst = (char*)sttVoidUid;
	MsnContact* p = m_arContacts.find((MsnContact*)&szEmail);
	if (p == NULL)
		return NULL;
	return p->places.find((MsnPlace*)&szInst);
}
MsnPlace* CMsnProto::Lists_AddPlace(const char* email, const char* id, unsigned cap1, unsigned cap2)
{
	mir_cslock lck(m_csLists);
	MsnContact *p = m_arContacts.find((MsnContact*)&email);
	if (p == NULL)
		return NULL;
	MsnPlace *pl = p->places.find((MsnPlace*)&id);
	if (pl == NULL) {
		pl = new MsnPlace;
		pl->id = mir_strdup(id);
		pl->cap1 = cap1;
		pl->cap2 = cap2;
		pl->p2pMsgId = 0;
		pl->p2pPktNum = 0;
		p->places.insert(pl);
	}
	return pl;
}
MsnContact* CMsnProto::Lists_GetNext(int &i)
{
	mir_cslock lck(m_csLists);
	MsnContact *p = NULL;
	while (p == NULL && ++i < m_arContacts.getCount())
		if (m_arContacts[i].hContact)
			p = &m_arContacts[i];
	return p;
}
int CMsnProto::Lists_GetMask(const char* email)
{
	mir_cslock lck(m_csLists);
	MsnContact *p = m_arContacts.find((MsnContact*)&email);
	return p ? p->list : 0;
}
int CMsnProto::Lists_GetNetId(const char* email)
{
	if (email[0] == 0) return NETID_UNKNOWN;
	mir_cslock lck(m_csLists);
	MsnContact *p = m_arContacts.find((MsnContact*)&email);
	return p ? p->netId : NETID_UNKNOWN;
}
#ifdef OBSOLETE
unsigned CMsnProto::p2p_getMsgId(const char* wlid, int inc)
{
	mir_cslock lck(m_csLists);
	MsnPlace *p = Lists_GetPlace(wlid);
	unsigned res = p && p->p2pMsgId ? p->p2pMsgId : MSN_GenRandom();
	if (p)
		p->p2pMsgId = res + inc;
	return res;
}
unsigned CMsnProto::p2p_getPktNum(const char* wlid)
{
	mir_cslock lck(m_csLists);
	MsnPlace *p = Lists_GetPlace(wlid);
	return p ? p->p2pPktNum++ : 0;
}
#endif
int CMsnProto::Lists_Add(int list, int netId, const char* email, MCONTACT hContact, const char* nick, const char* invite)
{
	mir_cslock lck(m_csLists);
	MsnContact* p = m_arContacts.find((MsnContact*)&email);
	if (p == NULL) {
		p = new MsnContact;
		p->list = list;
		p->netId = netId;
		p->email = _strlwr(mir_strdup(email));
		p->invite = mir_strdup(invite);
		p->nick = mir_strdup(nick);
		p->hContact = hContact;
		p->p2pMsgId = p->cap1 = p->cap2 = 0;
		m_arContacts.insert(p);
	}
	else {
		p->list |= list;
		if (invite) replaceStr(p->invite, invite);
		if (hContact) p->hContact = hContact;
		if (list & LIST_FL) p->netId = netId;
		if (p->netId == NETID_UNKNOWN && netId != NETID_UNKNOWN)
			p->netId = netId;
	}
	return p->list;
}
void CMsnProto::Lists_Remove(int list, const char* email)
{
	mir_cslock lck(m_csLists);
	int i = m_arContacts.getIndex((MsnContact*)&email);
	if (i != -1) {
		MsnContact &p = m_arContacts[i];
		p.list &= ~list;
		if (list & LIST_PL) { mir_free(p.invite); p.invite = NULL; }
		if (p.list == 0 && p.hContact == NULL)
			m_arContacts.remove(i);
	}
}
void CMsnProto::Lists_Populate(void)
{
	MCONTACT hContact = db_find_first(m_szModuleName);
	while (hContact) {
		MCONTACT hNext = db_find_next(hContact, m_szModuleName);
		char szEmail[MSN_MAX_EMAIL_LEN] = "";
		if (db_get_static(hContact, m_szModuleName, "wlid", szEmail, sizeof(szEmail))) {
			if (db_get_static(hContact, m_szModuleName, "e-mail", szEmail, sizeof(szEmail)) == 0)
				setString(hContact, "wlid", szEmail);
		}
		if (szEmail[0]) {
			bool localList = getByte(hContact, "LocalList", 0) != 0;
			int netId = getWord(hContact, "netId", localList?NETID_MSN:NETID_UNKNOWN);
			if (localList)
				Lists_Add(LIST_LL, netId, szEmail, hContact);
			else
				Lists_Add(0, netId, szEmail, hContact);
		}
		else if (!isChatRoom(hContact)) CallService(MS_DB_CONTACT_DELETE, hContact, 0);
		hContact = hNext;
	}
}
void CMsnProto::MSN_CleanupLists(void)
{
	for (int i = m_arContacts.getCount(); i--;) {
		MsnContact& p = m_arContacts[i];
		if (p.list & LIST_FL)
			MSN_SetContactDb(p.hContact, p.email);
		if (p.list & LIST_PL) {
			if (p.list & (LIST_AL | LIST_BL))
				MSN_AddUser(NULL, p.email, p.netId, LIST_PL + LIST_REMOVE);
			else
				MSN_AddAuthRequest(p.email, p.nick, p.invite);
		}
		if (p.hContact && !(p.list & (LIST_LL | LIST_FL | LIST_PL)) && p.list != LIST_RL) {
			int count = db_event_count(p.hContact);
			if (count) {
				TCHAR text[256];
				TCHAR *sze = mir_a2t(p.email);
				mir_sntprintf(text, TranslateT("Contact %s has been removed from the server.\nWould you like to keep it as \"Local Only\" contact to preserve history?"), sze);
				mir_free(sze);
				TCHAR title[128];
				mir_sntprintf(title, _countof(title), TranslateT("%s protocol"), m_tszUserName);
				if (MessageBox(NULL, text, title, MB_YESNO | MB_ICONQUESTION | MB_SETFOREGROUND) == IDYES) {
					MSN_AddUser(p.hContact, p.email, 0, LIST_LL);
					setByte(p.hContact, "LocalList", 1);
					continue;
				}
			}
			if (!(p.list & (LIST_LL | LIST_FL))) {
				CallService(MS_DB_CONTACT_DELETE, (WPARAM)p.hContact, 0);
				p.hContact = NULL;
			}
		}
		if (p.list & (LIST_LL | LIST_FL) && p.hContact) {
			TCHAR path[MAX_PATH];
			MSN_GetCustomSmileyFileName(p.hContact, path, _countof(path), "", 0);
			if (path[0]) {
				SMADD_CONT cont;
				cont.cbSize = sizeof(SMADD_CONT);
				cont.hContact = p.hContact;
				cont.type = 0;
				cont.path = path;
				CallService(MS_SMILEYADD_LOADCONTACTSMILEYS, 0, (LPARAM)&cont);
			}
		}
	}
}
void CMsnProto::MSN_CreateContList(void)
{
	bool *used = (bool*)mir_calloc(m_arContacts.getCount()*sizeof(bool));
	CMStringA cxml;
	cxml.AppendFormat("", MyOptions.netId == NETID_MSN?1:0);
	{
		mir_cslock lck(m_csLists);
		for (int i = 0; i < m_arContacts.getCount(); i++) {
			if (used[i]) continue;
			const char* lastds = strchr(m_arContacts[i].email, '@');
			bool newdom = true;
			for (int j = 0; j < m_arContacts.getCount(); j++) {
				if (used[j]) continue;
				const MsnContact& C = m_arContacts[j];
				if (C.list == LIST_RL || C.list == LIST_PL || C.list == LIST_LL) {
					used[j] = true;
					continue;
				}
				const char *dom = strchr(C.email, '@');
				if (dom == NULL && lastds == NULL) {
					if (newdom) {
						cxml.Append("");
						newdom = false;
					}
					int list = C.list & ~(LIST_RL | LIST_LL);
					list = LIST_FL | LIST_AL; /* Seems to be always 3 in Skype... */
					cxml.AppendFormat("