From 56c1f62b71e28b12e83e4ee7999461f73a3367fb Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Tue, 9 Sep 2014 19:52:07 +0000 Subject: Tox: id saves in db as blob at now git-svn-id: http://svn.miranda-ng.org/main/trunk@10412 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tox/src/tox_account.cpp | 3 +- protocols/Tox/src/tox_contacts.cpp | 50 +++++++++++--------- protocols/Tox/src/tox_messages.cpp | 66 ++++++++++++++++++++++++-- protocols/Tox/src/tox_options.cpp | 11 ++++- protocols/Tox/src/tox_proto.cpp | 96 ++++++++++++-------------------------- protocols/Tox/src/tox_proto.h | 4 +- protocols/Tox/src/tox_transfer.cpp | 72 ++++++++++++++++++++-------- 7 files changed, 186 insertions(+), 116 deletions(-) (limited to 'protocols/Tox') diff --git a/protocols/Tox/src/tox_account.cpp b/protocols/Tox/src/tox_account.cpp index e5305a9b2f..2e7a1d9904 100644 --- a/protocols/Tox/src/tox_account.cpp +++ b/protocols/Tox/src/tox_account.cpp @@ -65,8 +65,7 @@ void CToxProto::InitToxCore() std::vector address(TOX_FRIEND_ADDRESS_SIZE); tox_get_address(tox, &address[0]); - std::string toxId = DataToHexString(address); - setString(TOX_SETTINGS_ID, toxId.c_str()); + db_set_blob(NULL, m_szModuleName, TOX_SETTINGS_ID, (uint8_t*)address.data(), TOX_FRIEND_ADDRESS_SIZE); } void CToxProto::UninitToxCore() diff --git a/protocols/Tox/src/tox_contacts.cpp b/protocols/Tox/src/tox_contacts.cpp index 27898916cc..04d7286524 100644 --- a/protocols/Tox/src/tox_contacts.cpp +++ b/protocols/Tox/src/tox_contacts.cpp @@ -46,15 +46,19 @@ MCONTACT CToxProto::GetContactFromAuthEvent(HANDLE hEvent) return DbGetAuthEventContact(&dbei); } -MCONTACT CToxProto::FindContact(const std::string &id) +MCONTACT CToxProto::FindContact(const std::vector &id) { + DBVARIANT dbv; MCONTACT hContact = NULL; for (hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) { - std::string contactId = ToxAddressToId(getStringA(hContact, TOX_SETTINGS_ID)); - if (id == contactId) + if (!db_get(hContact, m_szModuleName, TOX_SETTINGS_ID, &dbv)) { - break; + if (dbv.type == DBVT_BLOB && memcmp(id.data(), dbv.pbVal, TOX_CLIENT_ID_SIZE) == 0) + { + break; + } + db_free(&dbv); } } return hContact; @@ -62,14 +66,13 @@ MCONTACT CToxProto::FindContact(const std::string &id) MCONTACT CToxProto::FindContact(const int friendNumber) { - std::vector clientId(TOX_CLIENT_ID_SIZE); - tox_get_client_id(tox, friendNumber, &clientId[0]); - std::string id = DataToHexString(clientId); + std::vector id(TOX_CLIENT_ID_SIZE); + tox_get_client_id(tox, friendNumber, id.data()); return FindContact(id); } -MCONTACT CToxProto::AddContact(const std::string &id, bool isTemporary) +MCONTACT CToxProto::AddContact(const std::vector &id, bool isTemporary) { MCONTACT hContact = FindContact(id); if (!hContact) @@ -77,7 +80,7 @@ MCONTACT CToxProto::AddContact(const std::string &id, bool isTemporary) hContact = (MCONTACT)CallService(MS_DB_CONTACT_ADD, 0, 0); CallService(MS_PROTO_ADDTOCONTACT, hContact, (LPARAM)m_szModuleName); - setString(hContact, TOX_SETTINGS_ID, id.c_str()); + db_set_blob(hContact, m_szModuleName, TOX_SETTINGS_ID, (uint8_t*)id.data(), TOX_CLIENT_ID_SIZE); setByte(hContact, "Auth", 1); DBVARIANT dbv; @@ -103,12 +106,10 @@ void CToxProto::LoadFriendList() int32_t *friends = (int32_t*)mir_alloc(count * sizeof(int32_t)); tox_get_friendlist(tox, friends, count); - std::vector clientId(TOX_CLIENT_ID_SIZE); + std::vector id(TOX_CLIENT_ID_SIZE); for (uint32_t i = 0; i < count; ++i) { - tox_get_client_id(tox, friends[i], &clientId[0]); - std::string id = DataToHexString(clientId); - + tox_get_client_id(tox, friends[i], id.data()); MCONTACT hContact = AddContact(id); if (hContact) { @@ -125,6 +126,8 @@ void CToxProto::LoadFriendList() } } } + + mir_free(friends); } else { @@ -136,15 +139,20 @@ int CToxProto::OnContactDeleted(MCONTACT hContact, LPARAM lParam) { if (hContact) { - std::string toxId(getStringA(hContact, TOX_SETTINGS_ID)); - std::vector clientId = HexStringToData(toxId); - - uint32_t number = tox_get_friend_number(tox, clientId.data()); - if (tox_del_friend(tox, number) == 0) + DBVARIANT dbv; + std::vector id; + if (!db_get(hContact, m_szModuleName, TOX_SETTINGS_ID, &dbv)) { - SaveToxData(); + memcpy(&id[0], dbv.pbVal, TOX_CLIENT_ID_SIZE); + db_free(&dbv); - return 0; + uint32_t number = tox_get_friend_number(tox, id.data()); + if (tox_del_friend(tox, number) == 0) + { + SaveToxData(); + + return 0; + } } } @@ -159,7 +167,7 @@ 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(id, true); + MCONTACT hContact = proto->AddContact(clientId, true); PROTORECVEVENT pre = { 0 }; pre.flags = PREF_UTF; diff --git a/protocols/Tox/src/tox_messages.cpp b/protocols/Tox/src/tox_messages.cpp index 0c57fbf68d..ee19016c2d 100644 --- a/protocols/Tox/src/tox_messages.cpp +++ b/protocols/Tox/src/tox_messages.cpp @@ -33,15 +33,38 @@ void CToxProto::OnFriendAction(Tox *tox, const int number, const uint8_t *action } } -void CToxProto::OnTypingChanged(Tox *tox, const int number, uint8_t isTyping, void *arg) +int __cdecl CToxProto::SendMsg(MCONTACT hContact, int flags, const char* msg) { - CToxProto *proto = (CToxProto*)arg; + 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); + } - MCONTACT hContact = proto->FindContact(number); - if (hContact) + uint32_t number = tox_get_friend_number(tox, id.data()); + if (number == TOX_ERROR) { - CallService(MS_PROTO_CONTACTISTYPING, hContact, (LPARAM)isTyping); + debugLogA("CToxProto::SendMsg: failed to get friend number"); + return 0; } + + int result = 0; + if (strncmp(msg, "/me ", 4) != 0) + { + result = tox_send_message(tox, number, (uint8_t*)msg, (uint16_t)strlen(msg)); + } + else + { + result = tox_send_action(tox, number, (uint8_t*)&msg[4], (uint16_t)strlen(msg) - 4); + } + if (result == 0) + { + debugLogA("CToxProto::SendMsg: could not to send message"); + } + + return result; } void CToxProto::OnReadReceipt(Tox *tox, int32_t number, uint32_t receipt, void *arg) @@ -79,5 +102,38 @@ int CToxProto::OnPreCreateMessage(WPARAM wParam, LPARAM lParam) evt->dbei->eventType = TOX_DB_EVENT_TYPE_ACTION; } + return 1; +} + +void CToxProto::OnTypingChanged(Tox *tox, const int number, uint8_t isTyping, void *arg) +{ + CToxProto *proto = (CToxProto*)arg; + + MCONTACT hContact = proto->FindContact(number); + if (hContact) + { + CallService(MS_PROTO_CONTACTISTYPING, hContact, (LPARAM)isTyping); + } +} + +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()); + if (number >= 0) + { + tox_set_user_is_typing(tox, number, type); + return 0; + } + } + return 1; } \ No newline at end of file diff --git a/protocols/Tox/src/tox_options.cpp b/protocols/Tox/src/tox_options.cpp index 7df550d585..081da9d7b0 100644 --- a/protocols/Tox/src/tox_options.cpp +++ b/protocols/Tox/src/tox_options.cpp @@ -15,8 +15,15 @@ INT_PTR CToxProto::MainOptionsProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM l ptrW nick(proto->getTStringA("Nick")); SetDlgItemText(hwnd, IDC_NAME, nick); - ptrA toxId(proto->getStringA(TOX_SETTINGS_ID)); - SetDlgItemTextA(hwnd, IDC_TOXID, toxId); + 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); + } 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 4dd008e199..83163a9670 100644 --- a/protocols/Tox/src/tox_proto.cpp +++ b/protocols/Tox/src/tox_proto.cpp @@ -67,16 +67,20 @@ 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::string id = ToxAddressToId(address); - std::string myId = ToxAddressToId(getStringA(TOX_SETTINGS_ID)); - if (myId == id) + std::vector id = HexStringToData(address); + if (!db_get(NULL, m_szModuleName, TOX_SETTINGS_ID, &dbv)) { - debugLogA("CToxProto::AddToList: you cannot add yourself to friend list"); - return NULL; + 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); } - // we set tox address as contact id - return AddContact(address, flags & PALF_TEMPORARY); + // set tox address as contact id + return AddContact(id, flags & PALF_TEMPORARY); } MCONTACT __cdecl CToxProto::AddToListByEvent(int flags, int iContact, HANDLE hDbEvent) { return 0; } @@ -91,14 +95,16 @@ int __cdecl CToxProto::Authorize(HANDLE hDbEvent) return 1; } - std::string toxId = getStringA(hContact, TOX_SETTINGS_ID); - std::vector clientId = HexStringToData(toxId); - - if (tox_add_friend_norequest(tox, &clientId[0]) >= 0) + DBVARIANT dbv; + if (!db_get(NULL, m_szModuleName, TOX_SETTINGS_ID, &dbv)) { - SaveToxData(); - - return 0; + if (tox_add_friend_norequest(tox, (uint8_t*)dbv.pbVal) != TOX_ERROR) + { + SaveToxData(); + db_free(&dbv); + return 0; + } + db_free(&dbv); } } @@ -115,19 +121,23 @@ int __cdecl CToxProto::AuthRecv(MCONTACT, PROTORECVEVENT* pre) int __cdecl CToxProto::AuthRequest(MCONTACT hContact, const PROTOCHAR* szMessage) { - std::string address = getStringA(hContact, TOX_SETTINGS_ID); - std::vector clientId = HexStringToData(address); + std::vector id; + DBVARIANT dbv; + if (!db_get(NULL, m_szModuleName, TOX_SETTINGS_ID, &dbv)) + { + memcpy(&id[0], (uint8_t*)dbv.pbVal, TOX_CLIENT_ID_SIZE); + db_free(&dbv); + } ptrA reason(mir_utf8encodeW(szMessage)); - int32_t number = tox_add_friend(tox, &clientId[0], (uint8_t*)(char*)reason, (uint16_t)strlen(reason)); - if (number >= 0) + int32_t number = tox_add_friend(tox, id.data(), (uint8_t*)(char*)reason, (uint16_t)strlen(reason)); + if (number != TOX_ERROR) { SaveToxData(); // change tox address in contact id by tox id - std::string id = ToxAddressToId(address); - setString(hContact, TOX_SETTINGS_ID, id.c_str()); + db_set_blob(hContact, m_szModuleName, TOX_SETTINGS_ID, (uint8_t*)id.data(), TOX_CLIENT_ID_SIZE); db_unset(hContact, "CList", "NotOnList"); delSetting(hContact, "Auth"); @@ -171,7 +181,7 @@ HWND __cdecl CToxProto::SearchAdvanced(HWND owner) if (std::regex_search(query, match, regex)) { std::string address = match[1]; - std::string id = ToxAddressToId(address); + std::vector id = HexStringToData(address); MCONTACT hContact = FindContact(id); if (!hContact) { @@ -229,31 +239,6 @@ int __cdecl CToxProto::RecvUrl(MCONTACT hContact, PROTORECVEVENT*) { return 0; } int __cdecl CToxProto::SendContacts(MCONTACT hContact, int flags, int nContacts, MCONTACT* hContactsList) { return 0; } -int __cdecl CToxProto::SendMsg(MCONTACT hContact, int flags, const char* msg) -{ - std::string toxId(getStringA(hContact, TOX_SETTINGS_ID)); - std::vector clientId = HexStringToData(toxId); - - uint32_t number = tox_get_friend_number(tox, clientId.data()); - - int result = 0; - if (strncmp(msg, "/me ", 4) != 0) - { - result = tox_send_message(tox, number, (uint8_t*)msg, (uint16_t)strlen(msg)); - } - else - { - result = tox_send_action(tox, number, (uint8_t*)&msg[4], (uint16_t)strlen(msg) - 4); - } - - if (result == 0) - { - debugLogA("CToxProto::SendMsg: could not to send message"); - } - - return result; -} - int __cdecl CToxProto::SendUrl(MCONTACT hContact, int flags, const char* url) { return 0; } int __cdecl CToxProto::SetApparentMode(MCONTACT hContact, int mode) { return 0; } @@ -313,25 +298,6 @@ HANDLE __cdecl CToxProto::GetAwayMsg(MCONTACT hContact) { return 0; } int __cdecl CToxProto::RecvAwayMsg(MCONTACT hContact, int mode, PROTORECVEVENT* evt) { return 0; } int __cdecl CToxProto::SetAwayMsg(int iStatus, const PROTOCHAR* msg) { return 0; } -int __cdecl CToxProto::UserIsTyping(MCONTACT hContact, int type) -{ - if (hContact && IsOnline()) - { - std::string toxId(getStringA(hContact, TOX_SETTINGS_ID)); - std::vector clientId = HexStringToData(toxId); - - uint32_t number = tox_get_friend_number(tox, clientId.data()); - - if (number >= 0) - { - tox_set_user_is_typing(tox, number, type); - return 0; - } - } - - return 1; -} - int __cdecl CToxProto::OnEvent(PROTOEVENTTYPE iEventType, WPARAM wParam, LPARAM lParam) { switch (iEventType) diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index bd1bf4078f..96e0bd662c 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -156,9 +156,9 @@ private: void SetContactStatus(MCONTACT hContact, WORD status); void SetAllContactsStatus(WORD status); - MCONTACT FindContact(const std::string &id); + MCONTACT FindContact(const std::vector &id); MCONTACT FindContact(const int friendNumber); - MCONTACT AddContact(const std::string &id, bool isTemporary = false); + MCONTACT AddContact(const std::vector &id, bool isTemporary = false); MCONTACT GetContactFromAuthEvent(HANDLE hEvent); diff --git a/protocols/Tox/src/tox_transfer.cpp b/protocols/Tox/src/tox_transfer.cpp index f9955c367d..05eedf7bfc 100644 --- a/protocols/Tox/src/tox_transfer.cpp +++ b/protocols/Tox/src/tox_transfer.cpp @@ -56,10 +56,15 @@ 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) { - std::string toxId(getStringA(hContact, TOX_SETTINGS_ID)); - std::vector clientId = HexStringToData(toxId); + DBVARIANT dbv; + std::vector id; + 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, clientId.data()); + uint32_t number = tox_get_friend_number(tox, id.data()); FileTransferParam *transfer = (FileTransferParam*)hTransfer; transfer->pfts.tszWorkingDir = mir_tstrdup(tszPath); @@ -82,10 +87,15 @@ int __cdecl CToxProto::FileResume(HANDLE hTransfer, int* action, const PROTOCHAR { FileTransferParam *transfer = (FileTransferParam*)hTransfer; - std::string toxId(getStringA(transfer->pfts.hContact, TOX_SETTINGS_ID)); - std::vector clientId = HexStringToData(toxId); + DBVARIANT dbv; + std::vector id; + 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, clientId.data()); + uint32_t number = tox_get_friend_number(tox, id.data()); switch (*action) { @@ -145,10 +155,15 @@ 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) { - std::string toxId(getStringA(hContact, TOX_SETTINGS_ID)); - std::vector clientId = HexStringToData(toxId); + DBVARIANT dbv; + std::vector id; + 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, clientId.data()); + uint32_t number = tox_get_friend_number(tox, id.data()); TCHAR *fileName = _tcsrchr(ppszFiles[0], '\\') + 1; @@ -189,10 +204,15 @@ void CToxProto::SendFileAsync(void* arg) { FileTransferParam *transfer = (FileTransferParam*)arg; - std::string toxId(getStringA(transfer->pfts.hContact, TOX_SETTINGS_ID)); - std::vector clientId = HexStringToData(toxId); + DBVARIANT dbv; + std::vector id; + if (!db_get(transfer->pfts.hContact, m_szModuleName, TOX_SETTINGS_ID, &dbv)) + { + memcpy(&id[0], dbv.pbVal, TOX_CLIENT_ID_SIZE); + db_free(&dbv); + } - if (uint32_t number = tox_get_friend_number(tox, clientId.data()) > TOX_ERROR) + if (uint32_t number = tox_get_friend_number(tox, id.data()) > TOX_ERROR) { ProtoBroadcastAck(transfer->pfts.hContact, ACKTYPE_FILE, ACKRESULT_CONNECTED, (HANDLE)transfer, 0); @@ -209,18 +229,27 @@ void CToxProto::SendFileAsync(void* arg) return; } - size_t chunkSize = min(tox_file_data_size(tox, number), fileSize); + size_t chunkSize = min(fileSize, (size_t)tox_file_data_size(tox, number)); uint8_t *data = (uint8_t*)mir_alloc(chunkSize); + data = NULL; while (!feof(hFile) && fileProgress < fileSize) { size_t size = min(chunkSize, fileSize - fileProgress); - if (fread(data, sizeof(uint8_t), size, hFile) == size) + if (data == NULL) { - tox_file_send_data(tox, number, transfer->number, data, size); - transfer->pfts.totalProgress = transfer->pfts.currentFileProgress = fileProgress += size; + fread(data, sizeof(uint8_t), size, hFile);// == size + } + if (tox_file_send_data(tox, number, transfer->number, data, size) != TOX_ERROR) + { + data = NULL; + transfer->pfts.totalProgress = transfer->pfts.currentFileProgress = fileProgress += size; ProtoBroadcastAck(transfer->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DATA, (HANDLE)transfer, (LPARAM)&transfer->pfts); } + else + { + Sleep(50); + } } mir_free(data); @@ -234,10 +263,15 @@ void CToxProto::SendFileAsync(void* arg) // file request is cancelled int __cdecl CToxProto::FileCancel(MCONTACT hContact, HANDLE hTransfer) { - std::string toxId(getStringA(hContact, TOX_SETTINGS_ID)); - std::vector clientId = HexStringToData(toxId); + DBVARIANT dbv; + std::vector id; + 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, clientId.data()); + uint32_t number = tox_get_friend_number(tox, id.data()); FileTransferParam *transfer = (FileTransferParam*)hTransfer; -- cgit v1.2.3