summaryrefslogtreecommitdiff
path: root/protocols/Tox/src/tox_messages.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'protocols/Tox/src/tox_messages.cpp')
-rw-r--r--protocols/Tox/src/tox_messages.cpp111
1 files changed, 83 insertions, 28 deletions
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;