void CSkypeProto::OnGetServerHistory(const NETLIBHTTPREQUEST *response)
{
	if (response == NULL)
		return;

	JSONNode root = JSONNode::parse(response->pData);
	if (!root)
		return;

	const JSONNode &metadata = root["_metadata"];
	const JSONNode &conversations = root["messages"].as_array();

	int totalCount = metadata["totalCount"].as_int();
	std::string syncState = metadata["syncState"].as_string();

	bool markAllAsUnread = getBool("MarkMesUnread", true);

	if (totalCount >= 99 || conversations.size() >= 99)
		PushRequest(new GetHistoryOnUrlRequest(syncState.c_str(), li), &CSkypeProto::OnGetServerHistory);

	for (int i = (int)conversations.size(); i >= 0; i--)
	{
		const JSONNode &message = conversations.at(i);

		CMStringA szMessageId = message["clientmessageid"] ? message["clientmessageid"].as_string().c_str() : message["skypeeditedid"].as_string().c_str();

		std::string messageType = message["messagetype"].as_string();
		std::string from = message["from"].as_string();
		std::string content = message["content"].as_string();
		std::string conversationLink = message["conversationLink"].as_string();
		int emoteOffset = message["skypeemoteoffset"].as_int();
		time_t timestamp = IsoToUnixTime(message["composetime"].as_string().c_str());
		CMStringA skypename(UrlToSkypename(from.c_str()));

		bool isEdited = message["skypeeditedid"];

		MCONTACT hContact = FindContact(UrlToSkypename(conversationLink.c_str()));
			  
		if (timestamp > db_get_dw(hContact, m_szModuleName, "LastMsgTime", 0))
			db_set_dw(hContact, m_szModuleName, "LastMsgTime", (DWORD)timestamp);

		int iFlags = DBEF_UTF;

		if (!markAllAsUnread)
			iFlags |= DBEF_READ;

		if (IsMe(skypename))
			iFlags |= DBEF_SENT;

		if (strstr(conversationLink.c_str(), "/8:"))
		{
			if (messageType == "Text" || messageType == "RichText")
			{
				ptrA szMessage(messageType == "RichText" ? RemoveHtml(content.c_str()) : mir_strdup(content.c_str()));
				MEVENT dbevent = GetMessageFromDb(hContact, szMessageId);

				if (isEdited && dbevent != NULL)
				{
					AppendDBEvent(hContact, dbevent, szMessage, szMessageId, timestamp);
				}
				else AddDbEvent(emoteOffset == 0 ? EVENTTYPE_MESSAGE : SKYPE_DB_EVENT_TYPE_ACTION, hContact, timestamp, iFlags, &szMessage[emoteOffset], szMessageId);
			}
			else if (messageType == "Event/Call")
			{
				AddDbEvent(SKYPE_DB_EVENT_TYPE_CALL_INFO, hContact, timestamp, iFlags, content.c_str(), szMessageId);
			}
			else if (messageType == "RichText/Files")
			{
				AddDbEvent(SKYPE_DB_EVENT_TYPE_FILETRANSFER_INFO, hContact, timestamp, iFlags, content.c_str(), szMessageId);
			}
			else if (messageType == "RichText/UriObject")
			{
				AddDbEvent(SKYPE_DB_EVENT_TYPE_URIOBJ, hContact, timestamp, iFlags, content.c_str(), szMessageId);
			}
			else if (messageType == "RichText/Contacts")
			{
				ProcessContactRecv(hContact, timestamp, content.c_str(), szMessageId);
			}
			else
			{
				AddDbEvent(SKYPE_DB_EVENT_TYPE_UNKNOWN, hContact, timestamp, iFlags, content.c_str(), szMessageId);
			}
		}
		else if (conversationLink.find("/19:") != -1)
		{
			CMStringA chatname(UrlToSkypename(conversationLink.c_str()));
			if (messageType == "Text" || messageType == "RichText")
			{
				AddMessageToChat(_A2T(chatname), _A2T(skypename), content.c_str(), emoteOffset != NULL, emoteOffset, timestamp, true);
			}
		}
	}
}
Exemple #2
0
void CSkypeProto::OnChatEvent(const JSONNode &node)
{
	//std::string clientMsgId = node["clientmessageid"].as_string();
	//std::string skypeEditedId = node["skypeeditedid"].as_string();

	std::string fromLink = node["from"].as_string();
	CMStringA from(ContactUrlToName(fromLink.c_str()));

	time_t timestamp = IsoToUnixTime(node["composetime"].as_string().c_str());

	std::string content = node["content"].as_string();
	int emoteOffset = node["skypeemoteoffset"].as_int();

	std::string conversationLink = node["conversationLink"].as_string();
	CMStringA chatname(ChatUrlToName(conversationLink.c_str()));

	CMString topic(node["threadtopic"].as_mstring());
	if (FindChatRoom(chatname) == NULL)
		SendRequest(new GetChatInfoRequest(m_szRegToken, chatname, m_szServer), &CSkypeProto::OnGetChatInfo, topic.Detach());

	std::string messageType = node["messagetype"].as_string();
	if (!mir_strcmpi(messageType.c_str(), "Text") || !mir_strcmpi(messageType.c_str(), "RichText"))
	{
		AddMessageToChat(_A2T(chatname), _A2T(from), content.c_str(), emoteOffset != NULL, emoteOffset, timestamp);
	}
	else if (!mir_strcmpi(messageType.c_str(), "ThreadActivity/AddMember"))
	{
		ptrA xinitiator, xtarget, initiator;
		//content = <addmember><eventtime>1429186229164</eventtime><initiator>8:initiator</initiator><target>8:user</target></addmember>

		HXML xml = xmlParseString(ptrT(mir_a2t(content.c_str())), 0, _T("addmember"));
		if (xml == NULL)
			return;

		for (int i = 0; i < xmlGetChildCount(xml); i++)
		{
			HXML xmlNode = xmlGetNthChild(xml, L"target", i);
			if (xmlNode == NULL)
				break;

			xtarget = mir_t2a(xmlGetText(xmlNode));

			CMStringA target = ParseUrl(xtarget, "8:");
			AddChatContact(_A2T(chatname), target, target, L"User");
		}
		xmlDestroyNode(xml);
	}
	else if (!mir_strcmpi(messageType.c_str(), "ThreadActivity/DeleteMember"))
	{
		ptrA xinitiator, xtarget;
		//content = <addmember><eventtime>1429186229164</eventtime><initiator>8:initiator</initiator><target>8:user</target></addmember>

		HXML xml = xmlParseString(ptrT(mir_a2t(content.c_str())), 0, _T("deletemember"));
		if (xml != NULL) {
			HXML xmlNode = xmlGetChildByPath(xml, _T("initiator"), 0);
			xinitiator = node != NULL ? mir_t2a(xmlGetText(xmlNode)) : NULL;

			xmlNode = xmlGetChildByPath(xml, _T("target"), 0);
			xtarget = xmlNode != NULL ? mir_t2a(xmlGetText(xmlNode)) : NULL;

			xmlDestroyNode(xml);
		}
		if (xtarget == NULL)
			return;

		CMStringA target = ParseUrl(xtarget, "8:");
		CMStringA initiator = ParseUrl(xinitiator, "8:");
		RemoveChatContact(_A2T(chatname), target, target, true, initiator);

	}
	else if (!mir_strcmpi(messageType.c_str(), "ThreadActivity/TopicUpdate"))
	{
		//content=<topicupdate><eventtime>1429532702130</eventtime><initiator>8:user</initiator><value>test topic</value></topicupdate>
		ptrA xinitiator, value;
		HXML xml = xmlParseString(ptrT(mir_a2t(content.c_str())), 0, _T("topicupdate"));
		if (xml != NULL) {
			HXML xmlNode = xmlGetChildByPath(xml, _T("initiator"), 0);
			xinitiator = xmlNode != NULL ? mir_t2a(xmlGetText(xmlNode)) : NULL;

			xmlNode = xmlGetChildByPath(xml, _T("value"), 0);
			value = xmlNode != NULL ? mir_t2a(xmlGetText(xmlNode)) : NULL;

			xmlDestroyNode(xml);
		}

		CMStringA initiator = ParseUrl(xinitiator, "8:");
		RenameChat(chatname, value);
		ChangeChatTopic(chatname, value, initiator);
	}
	else if (!mir_strcmpi(messageType.c_str(), "ThreadActivity/RoleUpdate"))
	{
		//content=<roleupdate><eventtime>1429551258363</eventtime><initiator>8:user</initiator><target><id>8:user1</id><role>admin</role></target></roleupdate>
		ptrA xinitiator, xId, xRole;
		HXML xml = xmlParseString(ptrT(mir_a2t(content.c_str())), 0, _T("roleupdate"));
		if (xml != NULL) {
			HXML xmlNode = xmlGetChildByPath(xml, _T("initiator"), 0);
			xinitiator = xmlNode != NULL ? mir_t2a(xmlGetText(xmlNode)) : NULL;

			xmlNode = xmlGetChildByPath(xml, _T("target"), 0);
			if (xmlNode != NULL)
			{
				HXML xmlId = xmlGetChildByPath(xmlNode, _T("id"), 0);
				HXML xmlRole = xmlGetChildByPath(xmlNode, _T("role"), 0);
				xId = xmlId != NULL ? mir_t2a(xmlGetText(xmlId)) : NULL;
				xRole = xmlRole != NULL ? mir_t2a(xmlGetText(xmlRole)) : NULL;
			}
			xmlDestroyNode(xml);
			
			CMStringA initiator = ParseUrl(xinitiator, "8:");
			CMStringA id = ParseUrl(xId, "8:");

			GCDEST gcd = { m_szModuleName, _A2T(chatname), !mir_strcmpi(xRole, "Admin") ? GC_EVENT_ADDSTATUS : GC_EVENT_REMOVESTATUS };
			GCEVENT gce = { sizeof(gce), &gcd };
			ptrT tszId(mir_a2t(id));
			ptrT tszRole(mir_a2t(xRole));
			ptrT tszInitiator(mir_a2t(initiator));
			gce.pDest = &gcd;
			gce.dwFlags = GCEF_ADDTOLOG;
			gce.ptszNick = tszId;
			gce.ptszUID = tszId;
			gce.ptszText = tszInitiator;
			gce.time = time(NULL);
			gce.bIsMe = IsMe(id);
			gce.ptszStatus = TranslateT("Admin");
			CallServiceSync(MS_GC_EVENT, 0, (LPARAM)&gce);
		}
	}
}