From db8c860f22d93d04fbe35bcb50e812c269cb2ea7 Mon Sep 17 00:00:00 2001
From: Alexander Lantsev <aunsane@gmail.com>
Date: Wed, 25 Feb 2015 22:07:05 +0000
Subject: Tox: messages refactoring pt 2

git-svn-id: http://svn.miranda-ng.org/main/trunk@12268 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
---
 protocols/Tox/src/tox_chatrooms.cpp |   3 +
 protocols/Tox/src/tox_contacts.cpp  |   2 +-
 protocols/Tox/src/tox_messages.cpp  | 111 +++++++++++++++++++++++++++---------
 protocols/Tox/src/tox_proto.cpp     |  11 ++--
 protocols/Tox/src/tox_proto.h       |   5 +-
 protocols/Tox/src/tox_utils.cpp     |  13 -----
 6 files changed, 95 insertions(+), 50 deletions(-)

(limited to 'protocols/Tox')

diff --git a/protocols/Tox/src/tox_chatrooms.cpp b/protocols/Tox/src/tox_chatrooms.cpp
index ce68ee0fb1..d4b134faed 100644
--- a/protocols/Tox/src/tox_chatrooms.cpp
+++ b/protocols/Tox/src/tox_chatrooms.cpp
@@ -163,6 +163,9 @@ void CToxProto::InitGroupChatModule()
 
 	HookProtoEvent(ME_GC_EVENT, &CToxProto::OnGroupChatEventHook);
 	HookProtoEvent(ME_GC_BUILDMENU, &CToxProto::OnGroupChatMenuHook);
+
+	CreateProtoService(PS_JOINCHAT, &CToxProto::OnJoinChatRoom);
+	CreateProtoService(PS_LEAVECHAT, &CToxProto::OnLeaveChatRoom);
 }
 
 void CToxProto::CloseAllChatChatSessions()
diff --git a/protocols/Tox/src/tox_contacts.cpp b/protocols/Tox/src/tox_contacts.cpp
index 4a5f15c7b2..d4173a041a 100644
--- a/protocols/Tox/src/tox_contacts.cpp
+++ b/protocols/Tox/src/tox_contacts.cpp
@@ -103,7 +103,7 @@ int32_t CToxProto::GetToxFriendNumber(MCONTACT hContact)
 	int32_t friendNumber = tox_get_friend_number(tox, pubKey);
 	if (friendNumber == TOX_ERROR)
 	{
-		debugLogA("CToxProto::SendMsg: failed to get friend number");
+		debugLogA("CToxProto::GetToxFriendNumber: failed to get friend number");
 	}
 	return friendNumber;
 }
diff --git a/protocols/Tox/src/tox_messages.cpp b/protocols/Tox/src/tox_messages.cpp
index fb7a832983..2e3e0b4a71 100644
--- a/protocols/Tox/src/tox_messages.cpp
+++ b/protocols/Tox/src/tox_messages.cpp
@@ -1,8 +1,13 @@
 #include "common.h"
 
-void CToxProto::RegisterIncomingMessage(const int friendNumber, const uint8_t *message, const uint16_t messageSize)
+/* MESSAGE RECEIVING */
+
+// incoming message flow
+void CToxProto::OnFriendMessage(Tox*, const int friendNumber, const uint8_t *message, const uint16_t messageSize, void *arg)
 {
-	MCONTACT hContact = GetContact(friendNumber);
+	CToxProto *proto = (CToxProto*)arg;
+
+	MCONTACT hContact = proto->GetContact(friendNumber);
 	if (hContact)
 	{
 		ptrA szMessage((char*)mir_alloc(messageSize + 1));
@@ -12,46 +17,96 @@ void CToxProto::RegisterIncomingMessage(const int friendNumber, const uint8_t *m
 		recv.flags = PREF_UTF;
 		recv.timestamp = time(NULL);
 		recv.szMessage = szMessage;
+		recv.lParam = EVENTTYPE_MESSAGE;
 
 		ProtoChainRecvMsg(hContact, &recv);
 	}
 }
 
-void CToxProto::OnFriendMessage(Tox*, const int friendNumber, const uint8_t *message, const uint16_t messageSize, void *arg)
+// incoming action flow
+void CToxProto::OnFriendAction(Tox*, const int friendNumber, const uint8_t *action, const uint16_t actionSize, void *arg)
 {
 	CToxProto *proto = (CToxProto*)arg;
-	proto->RegisterIncomingMessage(friendNumber, message, messageSize);
+
+	MCONTACT hContact = proto->GetContact(friendNumber);
+	if (hContact)
+	{
+		ptrA szMessage((char*)mir_alloc(actionSize + 1));
+		mir_strncpy(szMessage, (const char*)action, actionSize + 1);
+
+		PROTORECVEVENT recv = { 0 };
+		recv.flags = PREF_UTF;
+		recv.timestamp = time(NULL);
+		recv.szMessage = szMessage;
+		recv.lParam = TOX_DB_EVENT_TYPE_ACTION;
+
+		ProtoChainRecvMsg(hContact, &recv);
+	}
 }
 
-void CToxProto::OnFriendAction(Tox*, const int friendNumber, const uint8_t *action, const uint16_t actionSize, void *arg)
+// writing message/even into db
+int CToxProto::OnReceiveMessage(MCONTACT hContact, PROTORECVEVENT *pre)
 {
-	CToxProto *proto = (CToxProto*)arg;
-	proto->RegisterIncomingMessage(friendNumber, action, actionSize);
+	//return Proto_RecvMessage(hContact, pre);
+	if (pre->szMessage == NULL)
+		return NULL;
+
+	ptrA pszTemp;
+	mir_ptr<BYTE> pszBlob;
+
+	DBEVENTINFO dbei = { sizeof(dbei) };
+	dbei.szModule = GetContactProto(hContact);
+	dbei.timestamp = pre->timestamp;
+	dbei.flags = DBEF_UTF;
+	dbei.eventType = pre->lParam;
+	dbei.cbBlob = (DWORD)strlen(pre->szMessage) + 1;
+	dbei.pBlob = (PBYTE)pre->szMessage;
+
+	return (INT_PTR)db_event_add(hContact, &dbei);
 }
 
-int CToxProto::SendMsg(MCONTACT hContact, int, const char *msg)
+/* MESSAGE SENDING */
+
+// outcoming message flow
+int CToxProto::OnSendMessage(MCONTACT hContact, int flags, const char *szMessage)
 {
 	int32_t friendNumber = GetToxFriendNumber(hContact);
-	if (friendNumber != TOX_ERROR)
+	if (friendNumber == TOX_ERROR)
 	{
-		int receipt = 0;
-		if (strncmp(msg, "/me ", 4) != 0)
-		{
-			receipt = tox_send_message(tox, friendNumber, (uint8_t*)msg, mir_strlen(msg));
-		}
-		else
-		{
-			receipt = tox_send_action(tox, friendNumber, (uint8_t*)&msg[4], mir_strlen(msg) - 4);
-		}
-		if (receipt == TOX_ERROR)
-		{
-			debugLogA("CToxProto::SendMsg: failed to send message");
-		}
-		return receipt;
+		return 0;
+	}
+
+	ptrA message;
+	if (flags & PREF_UNICODE)
+	{
+		message = mir_utf8encodeW((wchar_t*)&szMessage[mir_strlen(szMessage) + 1]);
 	}
-	return 0;
+	else if (flags & PREF_UTF)
+	{
+		message = mir_strdup(szMessage);
+	}
+	else
+	{
+		message = mir_utf8encode(szMessage);
+	}
+
+	int receipt = 0;
+	if (strncmp(message, "/me ", 4) != 0)
+	{
+		receipt = tox_send_message(tox, friendNumber, (uint8_t*)(char*)message, mir_strlen(message));
+	}
+	else
+	{
+		receipt = tox_send_action(tox, friendNumber, (uint8_t*)&message[4], mir_strlen(message) - 4);
+	}
+	if (receipt == TOX_ERROR)
+	{
+		debugLogA("CToxProto::OnSendMessage: failed to send message");
+	}
+	return receipt;
 }
 
+// message is received by the other side
 void CToxProto::OnReadReceipt(Tox*, int32_t friendNumber, uint32_t receipt, void *arg)
 {
 	CToxProto *proto = (CToxProto*)arg;
@@ -60,13 +115,11 @@ void CToxProto::OnReadReceipt(Tox*, int32_t friendNumber, uint32_t receipt, void
 	if (hContact)
 	{
 		proto->ProtoBroadcastAck(
-			hContact,
-			ACKTYPE_MESSAGE,
-			ACKRESULT_SUCCESS,
-			(HANDLE)receipt, 0);
+			hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, (HANDLE)receipt, 0);
 	}
 }
 
+// preparing message/action to writing into db
 int CToxProto::OnPreCreateMessage(WPARAM, LPARAM lParam)
 {
 	MessageWindowEvent *evt = (MessageWindowEvent*)lParam;
@@ -89,6 +142,8 @@ int CToxProto::OnPreCreateMessage(WPARAM, LPARAM lParam)
 	return 1;
 }
 
+/* TYPING */
+
 void CToxProto::OnTypingChanged(Tox*, const int number, uint8_t isTyping, void *arg)
 {
 	CToxProto *proto = (CToxProto*)arg;
diff --git a/protocols/Tox/src/tox_proto.cpp b/protocols/Tox/src/tox_proto.cpp
index 1a22017a6a..a3dac57a20 100644
--- a/protocols/Tox/src/tox_proto.cpp
+++ b/protocols/Tox/src/tox_proto.cpp
@@ -48,10 +48,6 @@ CToxProto::CToxProto(const char* protoName, const TCHAR* userName) :
 
 	CreateProtoService(PS_SETMYNICKNAME, &CToxProto::SetMyNickname);
 
-	// chat rooms
-	//CreateProtoService(PS_JOINCHAT, &CToxProto::OnJoinChatRoom);
-	//CreateProtoService(PS_LEAVECHAT, &CToxProto::OnLeaveChatRoom);
-
 	// transfers
 	transfers = new CTransferList();
 }
@@ -176,13 +172,18 @@ int __cdecl CToxProto::RecvFile(MCONTACT hContact, PROTOFILEEVENT *pre)
 
 int __cdecl CToxProto::RecvMsg(MCONTACT hContact, PROTORECVEVENT *pre)
 {
-	return Proto_RecvMessage(hContact, pre);
+	return OnReceiveMessage(hContact, pre);
 }
 
 int __cdecl CToxProto::RecvUrl(MCONTACT, PROTORECVEVENT*) { return 0; }
 
 int __cdecl CToxProto::SendContacts(MCONTACT, int, int, MCONTACT*) { return 0; }
 
+int CToxProto::SendMsg(MCONTACT hContact, int flags, const char *msg)
+{
+	return OnSendMessage(hContact, flags, msg);
+}
+
 int __cdecl CToxProto::SendUrl(MCONTACT, int, const char*) { return 0; }
 
 int __cdecl CToxProto::SetApparentMode(MCONTACT, int) { return 0; }
diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h
index 40b586e0ed..8ea108a01f 100644
--- a/protocols/Tox/src/tox_proto.h
+++ b/protocols/Tox/src/tox_proto.h
@@ -186,7 +186,8 @@ private:
 	static INT_PTR CALLBACK ChatRoomInviteProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
 
 	// messages
-	void RegisterIncomingMessage(const int friendNumber, const uint8_t *message, const uint16_t messageSize);
+	int OnReceiveMessage(MCONTACT hContact, PROTORECVEVENT *pre);
+	int OnSendMessage(MCONTACT hContact, int flags, const char *message);
 
 	static void OnFriendMessage(Tox *tox, const int friendNumber, const uint8_t *message, const uint16_t messageSize, void *arg);
 	static void OnFriendAction(Tox *tox, const int friendNumber, const uint8_t *action, const uint16_t actionSize, void *arg);
@@ -224,8 +225,6 @@ private:
 	static void ShowNotification(const TCHAR *message, int flags = 0, MCONTACT hContact = NULL);
 	static void ShowNotification(const TCHAR *caption, const TCHAR *message, int flags = 0, MCONTACT hContact = NULL);
 
-	MEVENT AddDbEvent(MCONTACT hContact, WORD type, DWORD timestamp, DWORD flags, DWORD cbBlob, PBYTE pBlob);
-
 	static bool IsFileExists(std::tstring path);
 };
 
diff --git a/protocols/Tox/src/tox_utils.cpp b/protocols/Tox/src/tox_utils.cpp
index 14caa84026..ccbb41effd 100644
--- a/protocols/Tox/src/tox_utils.cpp
+++ b/protocols/Tox/src/tox_utils.cpp
@@ -60,19 +60,6 @@ void CToxProto::ShowNotification(const TCHAR *message, int flags, MCONTACT hCont
 	ShowNotification(_T(MODULE), message, flags, hContact);
 }
 
-MEVENT CToxProto::AddDbEvent(MCONTACT hContact, WORD type, DWORD timestamp, DWORD flags, DWORD cbBlob, PBYTE pBlob)
-{
-	DBEVENTINFO dbei = { sizeof(dbei) };
-	dbei.szModule = m_szModuleName;
-	dbei.timestamp = timestamp;
-	dbei.eventType = type;
-	dbei.cbBlob = cbBlob;
-	dbei.pBlob = pBlob;
-	dbei.flags = flags;
-
-	return db_event_add(hContact, &dbei);
-}
-
 bool CToxProto::IsFileExists(std::tstring path)
 {
 	//return ::GetFileAttributes(fileName) != DWORD(-1)
-- 
cgit v1.2.3