From 6c21b8918e51b66845f2555c8e3e4dc7d7a3b749 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Sat, 20 Sep 2014 18:57:33 +0000 Subject: Tox: - tox id is string again (h8!!) - updated tox core git-svn-id: http://svn.miranda-ng.org/main/trunk@10537 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tox/src/tox_account.cpp | 7 +-- protocols/Tox/src/tox_contacts.cpp | 90 ++++++++++++++++++++------------------ protocols/Tox/src/tox_messages.cpp | 23 +++------- protocols/Tox/src/tox_options.cpp | 11 +---- protocols/Tox/src/tox_proto.cpp | 53 ++++++++-------------- protocols/Tox/src/tox_proto.h | 6 +-- protocols/Tox/src/tox_search.cpp | 42 ++++++++++++------ protocols/Tox/src/tox_transfer.cpp | 60 +++++++------------------ 8 files changed, 123 insertions(+), 169 deletions(-) (limited to 'protocols/Tox/src') diff --git a/protocols/Tox/src/tox_account.cpp b/protocols/Tox/src/tox_account.cpp index 2e7a1d9904..c975276811 100644 --- a/protocols/Tox/src/tox_account.cpp +++ b/protocols/Tox/src/tox_account.cpp @@ -63,9 +63,10 @@ void CToxProto::InitToxCore() std::string nick(username.begin(), username.end()); setWString("Nick", ptrW(Utf8DecodeW(nick.c_str()))); - std::vector address(TOX_FRIEND_ADDRESS_SIZE); - tox_get_address(tox, &address[0]); - db_set_blob(NULL, m_szModuleName, TOX_SETTINGS_ID, (uint8_t*)address.data(), TOX_FRIEND_ADDRESS_SIZE); + std::vector pubKey(TOX_FRIEND_ADDRESS_SIZE); + tox_get_address(tox, &pubKey[0]); + std::string address = DataToHexString(pubKey); + setString(NULL, TOX_SETTINGS_ID, address.c_str()); } void CToxProto::UninitToxCore() diff --git a/protocols/Tox/src/tox_contacts.cpp b/protocols/Tox/src/tox_contacts.cpp index d86ab1ecbc..5abaaeb275 100644 --- a/protocols/Tox/src/tox_contacts.cpp +++ b/protocols/Tox/src/tox_contacts.cpp @@ -41,7 +41,14 @@ MCONTACT CToxProto::GetContactFromAuthEvent(HANDLE hEvent) return DbGetAuthEventContact(&dbei); } -MCONTACT CToxProto::FindContact(const std::vector &id) +bool CToxProto::IsMe(const std::string &id) +{ + std::string ownId = getStringA(NULL, TOX_SETTINGS_ID); + + return strnicmp(id.c_str(), ownId.c_str(), TOX_CLIENT_ID_SIZE) == 0; +} + +MCONTACT CToxProto::FindContact(const std::string &id) { DBVARIANT dbv; MCONTACT hContact = NULL; @@ -49,28 +56,27 @@ MCONTACT CToxProto::FindContact(const std::vector &id) { if (!db_get(hContact, m_szModuleName, TOX_SETTINGS_ID, &dbv)) { + std::string clientId; + // temporary code for contact id conversion - if (dbv.type == DBVT_ASCIIZ) + if (dbv.type == DBVT_BLOB) { - std::vector contactId = HexStringToData(dbv.pszVal); - db_unset(hContact, m_szModuleName, TOX_SETTINGS_ID); - db_set_blob(hContact, m_szModuleName, TOX_SETTINGS_ID, (uint8_t*)contactId.data(), TOX_CLIENT_ID_SIZE); - - if (memcmp(id.data(), contactId.data(), TOX_CLIENT_ID_SIZE) == 0) - { - db_free(&dbv); - break; - } + std::vector pubKey(dbv.cpbVal); + memcpy(&pubKey[0], dbv.pbVal, dbv.cpbVal); + clientId = DataToHexString(pubKey); + delSetting(hContact, TOX_SETTINGS_ID); + setString(hContact, TOX_SETTINGS_ID, id.c_str()); } - else if (dbv.type == DBVT_BLOB) + else if (dbv.type == DBVT_ASCIIZ) { - if (memcmp(id.data(), dbv.pbVal, TOX_CLIENT_ID_SIZE) == 0) - { - db_free(&dbv); - break; - } + clientId = dbv.pszVal; } db_free(&dbv); + + if (_strnicmp(id.c_str(), clientId.c_str(), TOX_CLIENT_ID_SIZE) == 0) + { + break; + } } } return hContact; @@ -78,13 +84,15 @@ MCONTACT CToxProto::FindContact(const std::vector &id) MCONTACT CToxProto::FindContact(const int friendNumber) { - std::vector id(TOX_CLIENT_ID_SIZE); - tox_get_client_id(tox, friendNumber, id.data()); + std::vector clientId(TOX_CLIENT_ID_SIZE); + tox_get_client_id(tox, friendNumber, clientId.data()); + + std::string id = DataToHexString(clientId); return FindContact(id); } -MCONTACT CToxProto::AddContact(const std::vector &id, bool isTemporary) +MCONTACT CToxProto::AddContact(const std::string &id, bool isTemporary) { MCONTACT hContact = FindContact(id); if (!hContact) @@ -92,8 +100,8 @@ MCONTACT CToxProto::AddContact(const std::vector &id, bool isTemporary) hContact = (MCONTACT)CallService(MS_DB_CONTACT_ADD, 0, 0); CallService(MS_PROTO_ADDTOCONTACT, hContact, (LPARAM)m_szModuleName); - db_set_blob(hContact, m_szModuleName, TOX_SETTINGS_ID, (uint8_t*)id.data(), id.size()); - setByte(hContact, "Auth", 1); + setString(hContact, TOX_SETTINGS_ID, id.c_str()); + //setByte(hContact, "Auth", 1); DBVARIANT dbv; if (!getTString(TOX_SETTINGS_GROUP, &dbv)) @@ -122,7 +130,7 @@ void CToxProto::LoadFriendList() for (uint32_t i = 0; i < count; ++i) { tox_get_client_id(tox, friends[i], id.data()); - MCONTACT hContact = AddContact(id); + MCONTACT hContact = AddContact(DataToHexString(id)); if (hContact) { int size = tox_get_name_size(tox, friends[i]); @@ -149,28 +157,18 @@ void CToxProto::LoadFriendList() int CToxProto::OnContactDeleted(MCONTACT hContact, LPARAM lParam) { - DBVARIANT dbv; - std::vector id(TOX_CLIENT_ID_SIZE); - if (!db_get(hContact, m_szModuleName, TOX_SETTINGS_ID, &dbv)) - { - if (dbv.type != DBVT_BLOB) - { - return 0; - } - - memcpy(&id[0], dbv.pbVal, TOX_CLIENT_ID_SIZE); - db_free(&dbv); - - uint32_t number = tox_get_friend_number(tox, id.data()); - if (tox_del_friend(tox, number) == 0) - { - SaveToxData(); + std::string id = getStringA(hContact, TOX_SETTINGS_ID); + std::vector clientId = HexStringToData(id); - return 0; - } + uint32_t number = tox_get_friend_number(tox, clientId.data()); + if (number == TOX_ERROR || tox_del_friend(tox, number) == TOX_ERROR) + { + return 1; } - return 1; + SaveToxData(); + + return 0; } void CToxProto::OnFriendRequest(Tox *tox, const uint8_t *address, const uint8_t *message, const uint16_t messageSize, void *arg) @@ -181,7 +179,13 @@ void CToxProto::OnFriendRequest(Tox *tox, const uint8_t *address, const uint8_t std::vector clientId(address, address + TOX_CLIENT_ID_SIZE); std::string id = proto->DataToHexString(clientId); - MCONTACT hContact = proto->AddContact(clientId); + MCONTACT hContact = proto->AddContact(id); + if (!hContact) + { + return; + } + + proto->setByte(hContact, "Auth", 1); PROTORECVEVENT pre = { 0 }; pre.flags = PREF_UTF; diff --git a/protocols/Tox/src/tox_messages.cpp b/protocols/Tox/src/tox_messages.cpp index ee19016c2d..d22fbc9f2d 100644 --- a/protocols/Tox/src/tox_messages.cpp +++ b/protocols/Tox/src/tox_messages.cpp @@ -35,15 +35,9 @@ void CToxProto::OnFriendAction(Tox *tox, const int number, const uint8_t *action int __cdecl CToxProto::SendMsg(MCONTACT hContact, int flags, const char* msg) { - DBVARIANT dbv; - std::vector id(TOX_CLIENT_ID_SIZE); - if (!db_get(hContact, m_szModuleName, TOX_SETTINGS_ID, &dbv)) - { - memcpy(&id[0], dbv.pbVal, TOX_CLIENT_ID_SIZE); - db_free(&dbv); - } - - uint32_t number = tox_get_friend_number(tox, id.data()); + std::string id = getStringA(hContact, TOX_SETTINGS_ID); + std::vector clientId = HexStringToData(id); + uint32_t number = tox_get_friend_number(tox, clientId.data()); if (number == TOX_ERROR) { debugLogA("CToxProto::SendMsg: failed to get friend number"); @@ -120,14 +114,9 @@ int __cdecl CToxProto::UserIsTyping(MCONTACT hContact, int type) { if (hContact && IsOnline()) { - DBVARIANT dbv; - std::vector id(TOX_CLIENT_ID_SIZE); - if (!db_get(hContact, m_szModuleName, TOX_SETTINGS_ID, &dbv)) - { - memcpy(&id[0], dbv.pbVal, TOX_CLIENT_ID_SIZE); - db_free(&dbv); - } - uint32_t number = tox_get_friend_number(tox, id.data()); + std::string id = getStringA(hContact, TOX_SETTINGS_ID); + std::vector clientId = HexStringToData(id); + uint32_t number = tox_get_friend_number(tox, clientId.data()); if (number >= 0) { tox_set_user_is_typing(tox, number, type); diff --git a/protocols/Tox/src/tox_options.cpp b/protocols/Tox/src/tox_options.cpp index 081da9d7b0..f137c8e738 100644 --- a/protocols/Tox/src/tox_options.cpp +++ b/protocols/Tox/src/tox_options.cpp @@ -15,15 +15,8 @@ INT_PTR CToxProto::MainOptionsProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM l ptrW nick(proto->getTStringA("Nick")); SetDlgItemText(hwnd, IDC_NAME, nick); - DBVARIANT dbv; - ; - if (!db_get(NULL, proto->m_szModuleName, TOX_SETTINGS_ID, &dbv)) - { - std::vector address(dbv.pbVal, dbv.pbVal + TOX_FRIEND_ADDRESS_SIZE); - std::string toxId = proto->DataToHexString(address); - SetDlgItemTextA(hwnd, IDC_TOXID, toxId.c_str()); - db_free(&dbv); - } + std::string address = proto->getStringA(NULL, TOX_SETTINGS_ID); + SetDlgItemTextA(hwnd, IDC_TOXID, address.c_str()); ptrW group(proto->getTStringA(TOX_SETTINGS_GROUP)); SetDlgItemText(hwnd, IDC_GROUP, group); diff --git a/protocols/Tox/src/tox_proto.cpp b/protocols/Tox/src/tox_proto.cpp index ab2cfe61a7..081192322d 100644 --- a/protocols/Tox/src/tox_proto.cpp +++ b/protocols/Tox/src/tox_proto.cpp @@ -71,20 +71,14 @@ DWORD_PTR __cdecl CToxProto::GetCaps(int type, MCONTACT hContact) MCONTACT __cdecl CToxProto::AddToList(int flags, PROTOSEARCHRESULT* psr) { - DBVARIANT dbv; std::string address(mir_t2a(psr->id)); - std::vector id = HexStringToData(address); - if (!db_get(NULL, m_szModuleName, TOX_SETTINGS_ID, &dbv)) + if (IsMe(address)) { - if (memcmp(id.data(), dbv.pbVal, TOX_CLIENT_ID_SIZE) == 0) - { - debugLogA("CToxProto::AddToList: you cannot add yourself to friend list"); - return NULL; - } - db_free(&dbv); + debugLogA("CToxProto::AddToList: you cannot add yourself to friend list"); + return NULL; } // set tox address as contact id - return AddContact(id, flags & PALF_TEMPORARY); + return AddContact(address, flags & PALF_TEMPORARY); } MCONTACT __cdecl CToxProto::AddToListByEvent(int flags, int iContact, HANDLE hDbEvent) @@ -100,23 +94,18 @@ int __cdecl CToxProto::Authorize(HANDLE hDbEvent) return 1; } - DBVARIANT dbv; - if (!db_get(hContact, m_szModuleName, TOX_SETTINGS_ID, &dbv)) + std::string id = getStringA(hContact, TOX_SETTINGS_ID); + std::vector clientId = HexStringToData(id); + if (tox_add_friend_norequest(tox, clientId.data()) != TOX_ERROR) { - std::vector id(TOX_CLIENT_ID_SIZE); - memcpy(&id[0], dbv.pbVal, TOX_CLIENT_ID_SIZE); - if (tox_add_friend_norequest(tox, id.data()) != TOX_ERROR) - { - db_unset(hContact, "CList", "NotOnList"); - delSetting(hContact, "Auth"); - SaveToxData(); - db_free(&dbv); - return 0; - } - db_free(&dbv); + return 1; } - return 1; + db_unset(hContact, "CList", "NotOnList"); + delSetting(hContact, "Auth"); + SaveToxData(); + + return 0; } int __cdecl CToxProto::AuthDeny(HANDLE hDbEvent, const PROTOCHAR* szReason) { return 0; } @@ -124,28 +113,22 @@ int __cdecl CToxProto::AuthDeny(HANDLE hDbEvent, const PROTOCHAR* szReason) { re int __cdecl CToxProto::AuthRecv(MCONTACT, PROTORECVEVENT* pre) { return Proto_AuthRecv(m_szModuleName, pre); - // return 0; } int __cdecl CToxProto::AuthRequest(MCONTACT hContact, const PROTOCHAR* szMessage) { - DBVARIANT dbv; - std::vector address(TOX_FRIEND_ADDRESS_SIZE); - if (!db_get(hContact, m_szModuleName, TOX_SETTINGS_ID, &dbv)) - { - memcpy(&address[0], (uint8_t*)dbv.pbVal, TOX_FRIEND_ADDRESS_SIZE); - db_free(&dbv); - } - ptrA reason(mir_utf8encodeW(szMessage)); - int32_t number = tox_add_friend(tox, address.data(), (uint8_t*)(char*)reason, (uint16_t)strlen(reason)); + std::string id = getStringA(hContact, TOX_SETTINGS_ID); + std::vector pubKey = HexStringToData(id); + int32_t number = tox_add_friend(tox, pubKey.data(), (uint8_t*)(char*)reason, (uint16_t)strlen(reason)); if (number > TOX_ERROR) { SaveToxData(); // change tox address in contact id by tox id - db_set_blob(hContact, m_szModuleName, TOX_SETTINGS_ID, (uint8_t*)address.data(), TOX_CLIENT_ID_SIZE); + id.resize(TOX_CLIENT_ID_SIZE); + setString(hContact, TOX_SETTINGS_ID, id.c_str()); db_unset(hContact, "CList", "NotOnList"); delSetting(hContact, "Auth"); diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index 6d19d708bc..7051e5c381 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -155,10 +155,10 @@ private: WORD GetContactStatus(MCONTACT hContact); void SetContactStatus(MCONTACT hContact, WORD status); void SetAllContactsStatus(WORD status); - - MCONTACT FindContact(const std::vector &id); + bool IsMe(const std::string &id); + MCONTACT FindContact(const std::string &id); MCONTACT FindContact(const int friendNumber); - MCONTACT AddContact(const std::vector &id, bool isTemporary = false); + MCONTACT AddContact(const std::string &id, bool isTemporary = false); MCONTACT GetContactFromAuthEvent(HANDLE hEvent); diff --git a/protocols/Tox/src/tox_search.cpp b/protocols/Tox/src/tox_search.cpp index 88726cadb8..6ceea8d07f 100644 --- a/protocols/Tox/src/tox_search.cpp +++ b/protocols/Tox/src/tox_search.cpp @@ -6,7 +6,7 @@ void CToxProto::SearchFailedAsync(void*) ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_FAILED, (HWND)1, 0); } -void CToxProto::SearchByNameAsync(void* arg) +void CToxProto::SearchByNameAsync(void *arg) { char *query = (char*)arg; char *name = strtok(query, "@"); @@ -45,6 +45,14 @@ void CToxProto::SearchByNameAsync(void* arg) { std::string id = DataToHexString(address); + if (IsMe(id)) + { + ShowNotification(TranslateT("You cannot add yourself to friend list"), 0); + ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_FAILED, (HANDLE)1, 0); + mir_free(arg); + return; + } + PROTOSEARCHRESULT psr = { sizeof(PROTOSEARCHRESULT) }; psr.flags = PSR_TCHAR; psr.id = mir_a2t(id.c_str()); @@ -115,23 +123,29 @@ HWND __cdecl CToxProto::SearchAdvanced(HWND owner) if (std::regex_search(query, match, regex)) { std::string address = match[1]; - std::vector id = HexStringToData(address); - MCONTACT hContact = FindContact(id); - if (!hContact) + if (IsMe(address)) { - PROTOSEARCHRESULT psr = { sizeof(psr) }; - psr.flags = PSR_TCHAR; - psr.id = mir_a2t(query.c_str()); - - ADDCONTACTSTRUCT acs = { HANDLE_SEARCHRESULT }; - acs.szProto = m_szModuleName; - acs.psr = &psr; - - CallService(MS_ADDCONTACT_SHOW, (WPARAM)owner, (LPARAM)&acs); + ShowNotification(TranslateT("You cannot add yourself to friend list"), 0); } else { - ShowNotification(TranslateT("Contact already in your contact list"), 0, hContact); + MCONTACT hContact = FindContact(address); + if (!hContact) + { + PROTOSEARCHRESULT psr = { sizeof(psr) }; + psr.flags = PSR_TCHAR; + psr.id = mir_a2t(query.c_str()); + + ADDCONTACTSTRUCT acs = { HANDLE_SEARCHRESULT }; + acs.szProto = m_szModuleName; + acs.psr = &psr; + + CallService(MS_ADDCONTACT_SHOW, (WPARAM)owner, (LPARAM)&acs); + } + else + { + ShowNotification(TranslateT("Contact already in your contact list"), 0, hContact); + } } ForkThread(&CToxProto::SearchFailedAsync, NULL); } diff --git a/protocols/Tox/src/tox_transfer.cpp b/protocols/Tox/src/tox_transfer.cpp index ac0a145dee..c70ce44b60 100644 --- a/protocols/Tox/src/tox_transfer.cpp +++ b/protocols/Tox/src/tox_transfer.cpp @@ -56,15 +56,9 @@ void CToxProto::OnFriendFile(Tox *tox, int32_t number, uint8_t fileNumber, uint6 // file request is allowed HANDLE __cdecl CToxProto::FileAllow(MCONTACT hContact, HANDLE hTransfer, const PROTOCHAR* tszPath) { - DBVARIANT dbv; - std::vector id(TOX_CLIENT_ID_SIZE); - if (!db_get(hContact, m_szModuleName, TOX_SETTINGS_ID, &dbv)) - { - memcpy(&id[0], dbv.pbVal, TOX_CLIENT_ID_SIZE); - db_free(&dbv); - } - - uint32_t number = tox_get_friend_number(tox, id.data()); + std::string id = getStringA(hContact, TOX_SETTINGS_ID); + std::vector clientId = HexStringToData(id); + uint32_t number = tox_get_friend_number(tox, clientId.data()); FileTransferParam *transfer = (FileTransferParam*)hTransfer; transfer->pfts.tszWorkingDir = mir_tstrdup(tszPath); @@ -87,15 +81,9 @@ int __cdecl CToxProto::FileResume(HANDLE hTransfer, int* action, const PROTOCHAR { FileTransferParam *transfer = (FileTransferParam*)hTransfer; - DBVARIANT dbv; - std::vector id(TOX_CLIENT_ID_SIZE); - if (!db_get(transfer->pfts.hContact, m_szModuleName, TOX_SETTINGS_ID, &dbv)) - { - memcpy(&id[0], dbv.pbVal, TOX_CLIENT_ID_SIZE); - db_free(&dbv); - } - - uint32_t number = tox_get_friend_number(tox, id.data()); + std::string id = getStringA(transfer->pfts.hContact, TOX_SETTINGS_ID); + std::vector clientId = HexStringToData(id); + uint32_t number = tox_get_friend_number(tox, clientId.data()); switch (*action) { @@ -155,15 +143,9 @@ void CToxProto::OnFileData(Tox *tox, int32_t number, uint8_t fileNumber, const u // send request through tox HANDLE __cdecl CToxProto::SendFile(MCONTACT hContact, const PROTOCHAR* szDescription, PROTOCHAR** ppszFiles) { - DBVARIANT dbv; - std::vector id(TOX_CLIENT_ID_SIZE); - if (!db_get(hContact, m_szModuleName, TOX_SETTINGS_ID, &dbv)) - { - memcpy(&id[0], dbv.pbVal, TOX_CLIENT_ID_SIZE); - db_free(&dbv); - } - - uint32_t number = tox_get_friend_number(tox, id.data()); + std::string id = getStringA(hContact, TOX_SETTINGS_ID); + std::vector clientId = HexStringToData(id); + uint32_t number = tox_get_friend_number(tox, clientId.data()); TCHAR *fileName = _tcsrchr(ppszFiles[0], '\\') + 1; @@ -204,15 +186,9 @@ void CToxProto::SendFileAsync(void* arg) { FileTransferParam *transfer = (FileTransferParam*)arg; - DBVARIANT dbv; - std::vector id(TOX_CLIENT_ID_SIZE); - if (!db_get(transfer->pfts.hContact, m_szModuleName, TOX_SETTINGS_ID, &dbv)) - { - memcpy(&id[0], dbv.pbVal, TOX_CLIENT_ID_SIZE); - db_free(&dbv); - } - - int32_t number = tox_get_friend_number(tox, id.data()); + std::string id = getStringA(transfer->pfts.hContact, TOX_SETTINGS_ID); + std::vector clientId = HexStringToData(id); + int32_t number = tox_get_friend_number(tox, clientId.data()); if (number > TOX_ERROR) { ProtoBroadcastAck(transfer->pfts.hContact, ACKTYPE_FILE, ACKRESULT_CONNECTED, (HANDLE)transfer, 0); @@ -262,15 +238,9 @@ void CToxProto::SendFileAsync(void* arg) // file request is cancelled int __cdecl CToxProto::FileCancel(MCONTACT hContact, HANDLE hTransfer) { - DBVARIANT dbv; - std::vector id(TOX_CLIENT_ID_SIZE); - if (!db_get(hContact, m_szModuleName, TOX_SETTINGS_ID, &dbv)) - { - memcpy(&id[0], dbv.pbVal, TOX_CLIENT_ID_SIZE); - db_free(&dbv); - } - - uint32_t number = tox_get_friend_number(tox, id.data()); + std::string id = getStringA(hContact, TOX_SETTINGS_ID); + std::vector clientId = HexStringToData(id); + uint32_t number = tox_get_friend_number(tox, clientId.data()); FileTransferParam *transfer = (FileTransferParam*)hTransfer; -- cgit v1.2.3