summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/messages.cpp
blob: 388b12f3091251b817ee5c07a6f18074eea13ccf (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "stdafx.h"

int WhatsAppProto::RecvMsg(MCONTACT hContact, PROTORECVEVENT *pre)
{
	CallService(MS_PROTO_CONTACTISTYPING, (WPARAM)hContact, (LPARAM)PROTOTYPE_CONTACTTYPING_OFF);

	return Proto_RecvMessage(hContact, pre);
}

void WhatsAppProto::onMessageForMe(const FMessage &pMsg)
{
	// someone sent us a contact. launch contact addition dialog
	if (pMsg.media_wa_type == FMessage::WA_TYPE_CONTACT) {
		MCONTACT hContact = AddToContactList(pMsg.media_url, pMsg.media_name.c_str());

		ADDCONTACTSTRUCT acs = { 0 };
		acs.handleType = HANDLE_CONTACT;
		acs.hContact = hContact;
		acs.szProto = m_szModuleName;
		CallServiceSync(MS_ADDCONTACT_SHOW, 0, (LPARAM)&acs);
	}
	else {
		std::string msg(pMsg.data);
		if (!pMsg.media_url.empty()) {
			if (!msg.empty())
				msg.append("\n");
			msg += pMsg.media_url;
		}

		MCONTACT hContact = this->AddToContactList(pMsg.key.remote_jid, pMsg.notifyname.c_str());

		PROTORECVEVENT recv = { 0 };
		recv.szMessage = const_cast<char*>(msg.c_str());
		if (getByte(WHATSAPP_KEY_USE_REMOTE_TIME, 0))
			recv.timestamp = pMsg.timestamp;
		else
			recv.timestamp = time(NULL);

		ProtoChainRecvMsg(hContact, &recv);
	}

	if (isOnline())
		m_pConnection->sendMessageReceived(pMsg);
}

int WhatsAppProto::SendMsg(MCONTACT hContact, int, const char *msg)
{
	ptrA jid(getStringA(hContact, "ID"));
	if (jid == NULL)
		return 0;

	if (m_pConnection == NULL) {
		debugLogA("No connection");
		return 0;
	}

	int msgId = GetSerial();
	try {
		time_t now = time(NULL);
		std::string id = Utilities::intToStr(now) + "-" + Utilities::intToStr(msgId);
		FMessage fmsg(std::string(jid), true, id);
		fmsg.timestamp = now;
		fmsg.data = msg;

		m_pConnection->sendMessage(&fmsg);
		utils::setStatusMessage(hContact, NULL);
	}
	catch (exception &e) {
		debugLogA("exception: %s", e.what());
		ProtoBroadcastAck(hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, (HANDLE)msgId, (LPARAM)e.what());
	}
	catch (...) {
		debugLogA("unknown exception");
		ProtoBroadcastAck(hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, (HANDLE)msgId, (LPARAM)"Failed sending message");
	}

	return msgId;
}

void WhatsAppProto::onIsTyping(const std::string &paramString, bool paramBoolean)
{
	MCONTACT hContact = this->AddToContactList(paramString);
	if (hContact != NULL) {
		CallService(MS_PROTO_CONTACTISTYPING, (WPARAM)hContact, (LPARAM)
			paramBoolean ? PROTOTYPE_CONTACTTYPING_INFINITE : PROTOTYPE_CONTACTTYPING_OFF);
	}
}

int WhatsAppProto::UserIsTyping(MCONTACT hContact, int type)
{
	if (hContact && isOnline()) {
		ptrA jid(getStringA(hContact, WHATSAPP_KEY_ID));
		if (jid && isOnline()) {
			if (type == PROTOTYPE_SELFTYPING_ON)
				m_pConnection->sendComposing((char*)jid);
			else
				m_pConnection->sendPaused((char*)jid);
		}
	}

	return 0;
}

void WhatsAppProto::onMessageStatusUpdate(const FMessage &fmsg)
{
	MCONTACT hContact = this->ContactIDToHContact(fmsg.key.remote_jid);
	if (hContact == 0)
		return;

	if (isChatRoom(hContact)) {
		onGroupMessageReceived(fmsg);
		return;
	}

	const wchar_t *ptszBy;
	switch (fmsg.status) {
	case FMessage::STATUS_RECEIVED_BY_SERVER: ptszBy = TranslateT("server"); break;
	case FMessage::STATUS_RECEIVED_BY_TARGET: ptszBy = pcli->pfnGetContactDisplayName(hContact, 0);  break;
	default:
		return;
	}

	size_t delim = fmsg.key.id.find('-');
	if (delim == string::npos)
		return;

	int msgId = atoi(fmsg.key.id.substr(delim+1).c_str());

	if (fmsg.status == FMessage::STATUS_RECEIVED_BY_SERVER)
		ProtoBroadcastAck(hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, (HANDLE)msgId, 0);

	time_t ts = atol(fmsg.key.id.substr(0, delim).c_str());

	wchar_t ttime[64];
	wcsftime(ttime, _countof(ttime), L"%X", localtime(&ts));
	utils::setStatusMessage(hContact, CMString(FORMAT, TranslateT("Message received: %s by %s"), ttime, ptszBy));
}