diff options
-rw-r--r-- | protocols/Tox/src/tox_chatrooms.cpp | 3 | ||||
-rw-r--r-- | protocols/Tox/src/tox_contacts.cpp | 11 | ||||
-rw-r--r-- | protocols/Tox/src/tox_events.cpp | 3 | ||||
-rw-r--r-- | protocols/Tox/src/tox_messages.cpp | 81 | ||||
-rw-r--r-- | protocols/Tox/src/tox_proto.h | 12 | ||||
-rw-r--r-- | protocols/Tox/src/tox_transfer.cpp | 4 |
6 files changed, 56 insertions, 58 deletions
diff --git a/protocols/Tox/src/tox_chatrooms.cpp b/protocols/Tox/src/tox_chatrooms.cpp index f17a5869a5..ce68ee0fb1 100644 --- a/protocols/Tox/src/tox_chatrooms.cpp +++ b/protocols/Tox/src/tox_chatrooms.cpp @@ -136,8 +136,7 @@ INT_PTR CToxProto::OnCreateChatRoom(WPARAM, LPARAM) }
for (std::vector<MCONTACT>::iterator it = param.invitedContacts.begin(); it != param.invitedContacts.end(); ++it)
{
- ToxBinAddress pubKey = ptrA(getStringA(*it, TOX_SETTINGS_ID));
- int32_t friendNumber = tox_get_friend_number(tox, pubKey);
+ int32_t friendNumber = GetToxFriendNumber(*it);
if (friendNumber == TOX_ERROR || tox_invite_friend(tox, friendNumber, groupNumber) == TOX_ERROR)
{
return 1;
diff --git a/protocols/Tox/src/tox_contacts.cpp b/protocols/Tox/src/tox_contacts.cpp index 729cb056f6..4a5f15c7b2 100644 --- a/protocols/Tox/src/tox_contacts.cpp +++ b/protocols/Tox/src/tox_contacts.cpp @@ -97,6 +97,17 @@ MCONTACT CToxProto::AddContact(const char *address, const std::tstring &dnsId, b return hContact;
}
+int32_t CToxProto::GetToxFriendNumber(MCONTACT hContact)
+{
+ ToxBinAddress pubKey = ptrA(getStringA(hContact, TOX_SETTINGS_ID));
+ int32_t friendNumber = tox_get_friend_number(tox, pubKey);
+ if (friendNumber == TOX_ERROR)
+ {
+ debugLogA("CToxProto::SendMsg: failed to get friend number");
+ }
+ return friendNumber;
+}
+
void CToxProto::LoadFriendList(void*)
{
uint32_t count = tox_count_friendlist(tox);
diff --git a/protocols/Tox/src/tox_events.cpp b/protocols/Tox/src/tox_events.cpp index e372b9ee39..87c688c593 100644 --- a/protocols/Tox/src/tox_events.cpp +++ b/protocols/Tox/src/tox_events.cpp @@ -7,10 +7,9 @@ int CToxProto::OnContactDeleted(MCONTACT hContact, LPARAM) return 1;
}
- ToxBinAddress pubKey = ptrA(getStringA(hContact, TOX_SETTINGS_ID));
if (!isChatRoom(hContact))
{
- int32_t friendNumber = tox_get_friend_number(tox, pubKey);
+ int32_t friendNumber = GetToxFriendNumber(hContact);
if (friendNumber == TOX_ERROR || tox_del_friend(tox, friendNumber) == TOX_ERROR)
{
return 1;
diff --git a/protocols/Tox/src/tox_messages.cpp b/protocols/Tox/src/tox_messages.cpp index b03e73e34a..fb7a832983 100644 --- a/protocols/Tox/src/tox_messages.cpp +++ b/protocols/Tox/src/tox_messages.cpp @@ -1,73 +1,62 @@ #include "common.h"
-void CToxProto::OnFriendMessage(Tox *, const int number, const uint8_t *message, const uint16_t, void *arg)
+void CToxProto::RegisterIncomingMessage(const int friendNumber, const uint8_t *message, const uint16_t messageSize)
{
- CToxProto *proto = (CToxProto*)arg;
-
- MCONTACT hContact = proto->GetContact(number);
+ MCONTACT hContact = GetContact(friendNumber);
if (hContact)
{
+ ptrA szMessage((char*)mir_alloc(messageSize + 1));
+ mir_strncpy(szMessage, (const char*)message, messageSize + 1);
+
PROTORECVEVENT recv = { 0 };
recv.flags = PREF_UTF;
recv.timestamp = time(NULL);
- recv.szMessage = (char*)message;
+ recv.szMessage = szMessage;
ProtoChainRecvMsg(hContact, &recv);
}
}
-void CToxProto::OnFriendAction(Tox *, const int number, const uint8_t *action, const uint16_t actionSize, void *arg)
+void CToxProto::OnFriendMessage(Tox*, const int friendNumber, const uint8_t *message, const uint16_t messageSize, void *arg)
{
CToxProto *proto = (CToxProto*)arg;
-
- MCONTACT hContact = proto->GetContact(number);
- if (hContact)
- {
- proto->AddDbEvent(
- hContact,
- TOX_DB_EVENT_TYPE_ACTION,
- time(NULL),
- DBEF_UTF,
- actionSize,
- (BYTE*)action);
- }
+ proto->RegisterIncomingMessage(friendNumber, message, messageSize);
}
-int __cdecl CToxProto::SendMsg(MCONTACT hContact, int, const char* msg)
+void CToxProto::OnFriendAction(Tox*, const int friendNumber, const uint8_t *action, const uint16_t actionSize, void *arg)
{
- ToxBinAddress pubKey = ptrA(getStringA(hContact, TOX_SETTINGS_ID));
- int32_t friendNumber = tox_get_friend_number(tox, pubKey);
- if (friendNumber == TOX_ERROR)
- {
- debugLogA("CToxProto::SendMsg: failed to get friend number");
- return 0;
- }
+ CToxProto *proto = (CToxProto*)arg;
+ proto->RegisterIncomingMessage(friendNumber, action, actionSize);
+}
- int result = 0;
+int CToxProto::SendMsg(MCONTACT hContact, int, const char *msg)
+{
+ int32_t friendNumber = GetToxFriendNumber(hContact);
+ if (friendNumber != TOX_ERROR)
{
+ int receipt = 0;
if (strncmp(msg, "/me ", 4) != 0)
{
- result = tox_send_message(tox, friendNumber, (uint8_t*)msg, mir_strlen(msg));
+ receipt = tox_send_message(tox, friendNumber, (uint8_t*)msg, mir_strlen(msg));
}
else
{
- result = tox_send_action(tox, friendNumber, (uint8_t*)&msg[4], mir_strlen(msg) - 4);
+ 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;
}
-
- if (result == 0)
- {
- debugLogA("CToxProto::SendMsg: failed to send message");
- }
-
- return result;
+ return 0;
}
-void CToxProto::OnReadReceipt(Tox *, int32_t number, uint32_t receipt, void *arg)
+void CToxProto::OnReadReceipt(Tox*, int32_t friendNumber, uint32_t receipt, void *arg)
{
CToxProto *proto = (CToxProto*)arg;
- MCONTACT hContact = proto->GetContact(number);
+ MCONTACT hContact = proto->GetContact(friendNumber);
if (hContact)
{
proto->ProtoBroadcastAck(
@@ -80,7 +69,7 @@ void CToxProto::OnReadReceipt(Tox *, int32_t number, uint32_t receipt, void *arg int CToxProto::OnPreCreateMessage(WPARAM, LPARAM lParam)
{
- MessageWindowEvent *evt = (MessageWindowEvent *)lParam;
+ MessageWindowEvent *evt = (MessageWindowEvent*)lParam;
if (strcmp(GetContactProto(evt->hContact), m_szModuleName))
{
return 0;
@@ -89,19 +78,18 @@ int CToxProto::OnPreCreateMessage(WPARAM, LPARAM lParam) char *message = (char*)evt->dbei->pBlob;
if (strncmp(message, "/me ", 4) == 0)
{
- BYTE *action = (BYTE*)mir_alloc(sizeof(BYTE)* (evt->dbei->cbBlob - 4));
- memcpy(action, (char*)&evt->dbei->pBlob[4], evt->dbei->cbBlob - 4);
+ evt->dbei->cbBlob = evt->dbei->cbBlob - 4;
+ PBYTE action = (PBYTE)mir_alloc(evt->dbei->cbBlob);
+ memcpy(action, &evt->dbei->pBlob[4], evt->dbei->cbBlob);
mir_free(evt->dbei->pBlob);
evt->dbei->pBlob = action;
- evt->dbei->cbBlob -= 4;
-
evt->dbei->eventType = TOX_DB_EVENT_TYPE_ACTION;
}
return 1;
}
-void CToxProto::OnTypingChanged(Tox *, const int number, uint8_t isTyping, void *arg)
+void CToxProto::OnTypingChanged(Tox*, const int number, uint8_t isTyping, void *arg)
{
CToxProto *proto = (CToxProto*)arg;
@@ -112,12 +100,11 @@ void CToxProto::OnTypingChanged(Tox *, const int number, uint8_t isTyping, void }
}
-int __cdecl CToxProto::UserIsTyping(MCONTACT hContact, int type)
+int CToxProto::UserIsTyping(MCONTACT hContact, int type)
{
if (hContact && IsOnline())
{
- ToxBinAddress pubKey = ptrA(getStringA(hContact, TOX_SETTINGS_ID));
- int32_t friendNumber = tox_get_friend_number(tox, pubKey);
+ int32_t friendNumber = GetToxFriendNumber(hContact);
if (friendNumber >= 0)
{
tox_set_user_is_typing(tox, friendNumber, type);
diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index 66225cc626..40b586e0ed 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -143,6 +143,8 @@ private: MCONTACT GetContactFromAuthEvent(MEVENT hEvent);
+ int32_t GetToxFriendNumber(MCONTACT hContact);
+
void __cdecl LoadFriendList(void*);
static void OnFriendRequest(Tox *tox, const uint8_t *pubKey, const uint8_t *message, const uint16_t messageSize, void *arg);
@@ -184,10 +186,12 @@ private: static INT_PTR CALLBACK ChatRoomInviteProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
// messages
- static void OnFriendMessage(Tox *tox, const int number, const uint8_t *message, const uint16_t messageSize, void *arg);
- static void OnFriendAction(Tox *tox, const int number, const uint8_t *action, const uint16_t actionSize, void *arg);
- static void OnTypingChanged(Tox *tox, const int number, uint8_t isTyping, void *arg);
- static void OnReadReceipt(Tox *tox, int32_t number, uint32_t receipt, void *arg);
+ void RegisterIncomingMessage(const int friendNumber, const uint8_t *message, const uint16_t messageSize);
+
+ 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);
+ static void OnTypingChanged(Tox *tox, const int friendNumber, uint8_t isTyping, void *arg);
+ static void OnReadReceipt(Tox *tox, int32_t friendNumber, uint32_t receipt, void *arg);
int __cdecl OnPreCreateMessage(WPARAM wParam, LPARAM lParam);
diff --git a/protocols/Tox/src/tox_transfer.cpp b/protocols/Tox/src/tox_transfer.cpp index 580e3aae75..2f377ab58c 100644 --- a/protocols/Tox/src/tox_transfer.cpp +++ b/protocols/Tox/src/tox_transfer.cpp @@ -153,11 +153,9 @@ void CToxProto::OnFileData(Tox *tox, int32_t friendNumber, uint8_t fileNumber, c // outcoming file flow
HANDLE __cdecl CToxProto::SendFile(MCONTACT hContact, const PROTOCHAR*, PROTOCHAR **ppszFiles)
{
- ToxBinAddress pubKey = ptrA(getStringA(hContact, TOX_SETTINGS_ID));
- int32_t friendNumber = tox_get_friend_number(tox, pubKey);
+ int32_t friendNumber = GetToxFriendNumber(hContact);
if (friendNumber == TOX_ERROR)
{
- debugLogA("CToxProto::SendMsg: failed to get friend number");
return NULL;
}
|