summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/message.cpp
blob: 806fbf1198d01cc1d523608a9d85098294f27e56 (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
/*

WhatsApp plugin for Miranda NG
Copyright © 2019-22 George Hazan

*/

#include "stdafx.h"

static const proto::Message& getBody(const proto::Message &message)
{
	if (message.has_ephemeralmessage()) {
		auto &pMsg = message.ephemeralmessage().message();
		return (pMsg.has_viewoncemessage()) ? pMsg.viewoncemessage().message() : pMsg;
	}

	if (message.has_viewoncemessage())
		return message.viewoncemessage().message();

	return message;
}

void WhatsAppProto::ProcessMessage(WAMSG type, const proto::WebMessageInfo &msg)
{
	auto &key = msg.key();
	auto &body = getBody(msg.message());
	bool bFromMe = key.fromme();

	debugLogA("Got a message: %s", msg.Utf8DebugString().c_str());

	uint32_t timestamp = msg.messagetimestamp();
	auto &participant = key.participant();
	auto &chatId = key.remotejid();
	auto &msgId = key.id();
	
	WAUser *pUser = FindUser(chatId.c_str());
	if (pUser == nullptr) {
		if (type.bPrivateChat)
			pUser = AddUser(chatId.c_str(), false, false);
		else if (type.bGroupChat)
			pUser = AddUser(chatId.c_str(), false, true);
	}

	if (!bFromMe && msg.has_pushname() && pUser && !pUser->bIsGroupChat)
		setUString(pUser->hContact, "Nick", msg.pushname().c_str());

	// try to extract some text 
	if (body.has_conversation()) {
		auto &conversation = body.conversation();
		if (!conversation.empty()) {
			PROTORECVEVENT pre = {};
			pre.timestamp = timestamp;
			pre.szMessage = (char*)conversation.c_str();
			pre.szMsgId = msgId.c_str();
			if (type.bOffline)
				pre.flags |= PREF_CREATEREAD;
			if (bFromMe)
				pre.flags |= PREF_SENT;
			ProtoChainRecvMsg(pUser->hContact, &pre);
		}
	}

	if (body.has_protocolmessage()) {
		auto &protoMsg = body.protocolmessage();
		switch (protoMsg.type()) {
		case proto::Message_ProtocolMessage_Type_APP_STATE_SYNC_KEY_SHARE:
			if (protoMsg.appstatesynckeyshare().keys_size()) {
				for (auto &it : protoMsg.appstatesynckeyshare().keys()) {
					auto &keyid = it.keyid().keyid();
					auto &keydata = it.keydata().keydata();

					CMStringA szSetting(FORMAT, "AppSyncKey%d", decodeBigEndian(keyid));
					db_set_blob(0, m_szModuleName, szSetting, keydata.c_str(), (unsigned)keydata.size());
				}
			}
			break;

		case proto::Message_ProtocolMessage_Type_HISTORY_SYNC_NOTIFICATION:
			debugLogA("History sync notification");
			m_impl.m_resyncApp.Stop();
			m_impl.m_resyncApp.Start(10000);
			break;

		case proto::Message_ProtocolMessage_Type_REVOKE:
			break;

		case proto::Message_ProtocolMessage_Type_EPHEMERAL_SETTING:
			if (pUser) {
				setDword(pUser->hContact, DBKEY_EPHEMERAL_TS, timestamp);
				setDword(pUser->hContact, DBKEY_EPHEMERAL_EXPIRE, protoMsg.ephemeralexpiration());
			}
			break;
		}
	}
	else if (body.has_reactionmessage()) {
		debugLogA("Got a reaction to a message");
	}
	else if (msg.has_messagestubtype()) {
		switch (msg.messagestubtype()) {
		case proto::WebMessageInfo::GROUP_PARTICIPANT_LEAVE:
		case proto::WebMessageInfo::GROUP_PARTICIPANT_REMOVE:
			debugLogA("Participant %s removed from chat", participant.c_str());
			break;

		case proto::WebMessageInfo::GROUP_PARTICIPANT_ADD:
		case proto::WebMessageInfo::GROUP_PARTICIPANT_INVITE:
		case proto::WebMessageInfo::GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
			debugLogA("Participant %s added to chat", participant.c_str());
			break;

		case proto::WebMessageInfo::GROUP_PARTICIPANT_DEMOTE:
			debugLogA("Participant %s demoted", participant.c_str());
			break;

		case proto::WebMessageInfo::GROUP_PARTICIPANT_PROMOTE:
			debugLogA("Participant %s promoted", participant.c_str());
			break;

		case proto::WebMessageInfo::GROUP_CHANGE_ANNOUNCE:
			debugLogA("Groupchat announce", participant.c_str());
			break;

		case proto::WebMessageInfo::GROUP_CHANGE_RESTRICT:
			debugLogA("Groupchat restriction", participant.c_str());
			break;

		case proto::WebMessageInfo::GROUP_CHANGE_SUBJECT:
			debugLogA("Groupchat subject was changed", participant.c_str());
			break;
		}
	}
}