summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/chat.cpp
blob: bef230ecfd6e5574f117d706fb169131054b6ae6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "common.h"

// #TODO Remove, as we are not using the chat-module for groups anymore

INT_PTR WhatsAppProto::OnJoinChat(WPARAM,LPARAM)
{
	return 0;
}

INT_PTR WhatsAppProto::OnLeaveChat(WPARAM,LPARAM)
{
	return 0;
}

int WhatsAppProto::OnChatOutgoing(WPARAM wParam, LPARAM lParam)
{
	GCHOOK *hook = reinterpret_cast<GCHOOK*>(lParam);
	char *text;
	char *id;

	if (strcmp(hook->pDest->pszModule,m_szModuleName))
		return 0;

	switch(hook->pDest->iType)
	{
	case GC_USER_MESSAGE:
	{
		text = mir_t2a_cp(hook->ptszText,CP_UTF8);
		std::string msg = text;

		id = mir_t2a_cp(hook->pDest->ptszID,CP_UTF8);
		std::string chat_id = id;

		mir_free(text);
		mir_free(id);
	
		if (isOnline()) {
			MCONTACT hContact = this->ContactIDToHContact(chat_id);
			if (hContact)
			{
				debugLogA("**Chat - Outgoing message: %s", text);
				this->SendMsg(hContact, IS_CHAT, msg.c_str());
				
				// #TODO Move to SendMsgWorker, otherwise all messages are "acknowledged" by Miranda

				GCDEST gcd = { m_szModuleName, hook->pDest->ptszID, GC_EVENT_MESSAGE };
				GCEVENT gce = { sizeof(gce), &gcd };
				gce.dwFlags = GCEF_ADDTOLOG;
				gce.ptszNick = mir_a2t(this->nick.c_str());
				gce.ptszUID = mir_a2t(this->jid.c_str());
				gce.time = time(NULL);
				gce.ptszText = hook->ptszText;
				gce.bIsMe = TRUE;
				CallServiceSync(MS_GC_EVENT, 0, (LPARAM)&gce);

				mir_free((void*)gce.ptszUID);
				mir_free((void*)gce.ptszNick);
			}
		}
	
		break;
	}

	case GC_USER_LEAVE:
	case GC_SESSION_TERMINATE:
	{
		break;
	}
	}

	return 0;
}