summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/messages.cpp
blob: b051d20903b405657c512bf37d55bd53e2d85528 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "common.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(FMessage* paramFMessage, bool paramBoolean)
{
	bool isChatRoom = !paramFMessage->remote_resource.empty();

	std::string* msg;
	switch (paramFMessage->media_wa_type)
	{
	case FMessage::WA_TYPE_IMAGE:
	case FMessage::WA_TYPE_AUDIO:
	case FMessage::WA_TYPE_VIDEO:
		msg = &paramFMessage->media_url;
		break;
	default:
		msg = &paramFMessage->data;
	}

	if (isChatRoom)
	{
		msg->insert(0, std::string("[").append(paramFMessage->notifyname).append("]: "));
	}

	MCONTACT hContact = this->AddToContactList(paramFMessage->key->remote_jid, 0, false, 
		isChatRoom ? NULL : paramFMessage->notifyname.c_str(), isChatRoom);

	PROTORECVEVENT recv = {0};
	recv.flags = PREF_UTF;
	recv.szMessage = const_cast<char*>(msg->c_str());
	recv.timestamp = paramFMessage->timestamp; //time(NULL);
	ProtoChainRecvMsg(hContact, &recv);

	this->connection->sendMessageReceived(paramFMessage);
}

int WhatsAppProto::SendMsg(MCONTACT hContact, int flags, const char *msg)
{
	debugLogA("");
	int msgId = ++(this->msgId);

	ForkThread( &WhatsAppProto::SendMsgWorker, new send_direct(hContact, msg, (HANDLE) msgId, flags & IS_CHAT));
	return this->msgIdHeader + msgId;
}

void WhatsAppProto::SendMsgWorker(void* p)
{
	debugLogA("");
	if (p == NULL)
		return;

	DBVARIANT dbv;
	send_direct *data = static_cast<send_direct*>(p);

	if (getByte(data->hContact, "SimpleChatRoom", 0) > 0 && getByte(data->hContact, "IsGroupMember", 0) == 0)
	{
		debugLogA("not a group member");
		ProtoBroadcastAck(data->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED,
			(HANDLE) (this->msgIdHeader + this->msgId), (LPARAM) "You cannot send messages to groups if you are not a member.");
	}
	else if (!getString(data->hContact, "ID", &dbv) && this->connection != NULL)
	{
		try
		{
			setDword(data->hContact, WHATSAPP_KEY_LAST_MSG_STATE, 2);
			setDword(data->hContact, WHATSAPP_KEY_LAST_MSG_ID_HEADER, this->msgIdHeader);
			setDword(data->hContact, WHATSAPP_KEY_LAST_MSG_ID, this->msgId);

			std::stringstream ss;
			ss << this->msgIdHeader << "-" << this->msgId;
			Key* key = new Key(Key(dbv.pszVal, true, ss.str())); // deleted by FMessage
			FMessage fmsg(key);
			fmsg.data = data->msg;
			fmsg.timestamp = time(NULL);

			db_free(&dbv);

			this->connection->sendMessage(&fmsg);
		}
		catch (exception &e)
		{
			debugLogA("exception: %s", e.what());
			ProtoBroadcastAck(data->hContact,ACKTYPE_MESSAGE,ACKRESULT_FAILED,
				(HANDLE) (this->msgIdHeader + this->msgId), (LPARAM) e.what());
		}
		catch (...)
		{
			debugLogA("unknown exception");
			ProtoBroadcastAck(data->hContact,ACKTYPE_MESSAGE,ACKRESULT_FAILED,
			  (HANDLE) (this->msgIdHeader + this->msgId), (LPARAM) "Failed sending message");
		}
	}
	else
	{
		debugLogA("No connection");
		ProtoBroadcastAck(data->hContact,ACKTYPE_MESSAGE,ACKRESULT_FAILED,
			(HANDLE) (this->msgIdHeader + this->msgId), (LPARAM) "You cannot send messages when you are offline.");
	}

	delete data;
}

void WhatsAppProto::RecvMsgWorker(void *p)
{
	if (p == NULL)
		return;

	//WAConnection.cpp l1225 - message will be deleted. We cannot send the ack inside a thread!
	//FMessage *fmsg = static_cast<FMessage*>(p);
	//this->connection->sendMessageReceived(fmsg);

	//delete fmsg;
}

void WhatsAppProto::onIsTyping(const std::string& paramString, bool paramBoolean)
{
	MCONTACT hContact = this->AddToContactList(paramString, 0, false);
	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())
		ForkThread(&WhatsAppProto::SendTypingWorker, new send_typing(hContact, type));

	return 0;
}

void WhatsAppProto::SendTypingWorker(void* p)
{
	if(p == NULL)
		return;

	send_typing *typing = static_cast<send_typing*>(p);

	// Don't send typing notifications to contacts which are offline 
	if ( getWord(typing->hContact, "Status", 0) == ID_STATUS_OFFLINE)
		return;

	DBVARIANT dbv;
	if ( !getString(typing->hContact, WHATSAPP_KEY_ID,&dbv) &&
		this->isOnline())
	{
		if (typing->status == PROTOTYPE_SELFTYPING_ON)
			this->connection->sendComposing(dbv.pszVal);
		else
			this->connection->sendPaused(dbv.pszVal);
	}		

	delete typing;
}

void WhatsAppProto::onMessageStatusUpdate(FMessage* fmsg)
{
	debugLogA("");

	MCONTACT hContact = this->ContactIDToHContact(fmsg->key->remote_jid);
	if (hContact == 0)
		return;

	int state = 5 - fmsg->status;
	if (state != 0 && state != 1)
		return;

	int header;
	int id;
	size_t delimPos = fmsg->key->id.find("-");
	
	std::stringstream ss;
	ss << fmsg->key->id.substr(0, delimPos);
	ss >> header;
	
	ss.clear();
	ss << fmsg->key->id.substr(delimPos + 1);
	ss >> id;

	if (state == 1)
		ProtoBroadcastAck(hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, (HANDLE) (header + id),0);

	if (getDword(hContact, WHATSAPP_KEY_LAST_MSG_ID_HEADER, 0) == header && getDword(hContact, WHATSAPP_KEY_LAST_MSG_ID, -1) == id)
	{
		setDword(hContact, WHATSAPP_KEY_LAST_MSG_STATE, state);
		this->UpdateStatusMsg(hContact);
	}
}