From 2e93497d5b19b4fc46f84cb18267bce357e48747 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Sat, 16 Aug 2014 16:02:58 +0000 Subject: Tox: - added netlib logging - minor fixes git-svn-id: http://svn.miranda-ng.org/main/trunk@10203 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tox/src/common.h | 4 +++- protocols/Tox/src/tox_contacts.cpp | 8 +++----- protocols/Tox/src/tox_events.cpp | 33 ++++++++++++++++++++++++++++++--- protocols/Tox/src/tox_netlib.cpp | 22 ++++++++++++++++++++++ protocols/Tox/src/tox_options.cpp | 6 +++--- protocols/Tox/src/tox_proto.cpp | 21 +++++++++++++++++++-- protocols/Tox/src/tox_proto.h | 17 ++++++++++------- protocols/Tox/src/tox_utils.cpp | 13 ++----------- 8 files changed, 92 insertions(+), 32 deletions(-) create mode 100644 protocols/Tox/src/tox_netlib.cpp (limited to 'protocols/Tox/src') diff --git a/protocols/Tox/src/common.h b/protocols/Tox/src/common.h index 8833a1f517..71a16fb1ae 100644 --- a/protocols/Tox/src/common.h +++ b/protocols/Tox/src/common.h @@ -20,6 +20,7 @@ #include #include #include +#include #include "tox\tox.h" @@ -30,6 +31,7 @@ extern HINSTANCE g_hInstance; #define TOX_SETTINGS_ID "ToxID" -#define TOX_SETTINGS_DEFAULT_GROUP "DefaultGroup" +#define TOX_SETTINGS_ADDRES "ToxAddress" +#define TOX_SETTINGS_GROUP "DefaultGroup" #endif //_COMMON_H_ \ No newline at end of file diff --git a/protocols/Tox/src/tox_contacts.cpp b/protocols/Tox/src/tox_contacts.cpp index 6cf642cca6..4bad40f78e 100644 --- a/protocols/Tox/src/tox_contacts.cpp +++ b/protocols/Tox/src/tox_contacts.cpp @@ -77,7 +77,7 @@ MCONTACT CToxProto::AddContact(const char *clientId, bool isTemporary) setString(hContact, TOX_SETTINGS_ID, clientId); DBVARIANT dbv; - if (!getTString(TOX_SETTINGS_DEFAULT_GROUP, &dbv)) + if (!getTString(TOX_SETTINGS_GROUP, &dbv)) { db_set_ts(hContact, "CList", "Group", dbv.ptszVal); db_free(&dbv); @@ -107,11 +107,9 @@ void CToxProto::LoadContactList() tox_get_name(tox, friends[i], &username[0]); std::string nick(username.begin(), username.end()); setString(hContact, "Nick", nick.c_str()); - - /*uint8_t userstatus = tox_get_user_status(tox, friends[i]); - int status = ToxToMirandaStatus((TOX_USERSTATUS)userstatus); - SetContactStatus(hContact, status);*/ } + + //tox_get_last_online } } } \ No newline at end of file diff --git a/protocols/Tox/src/tox_events.cpp b/protocols/Tox/src/tox_events.cpp index d10e146aee..1c32597ce4 100644 --- a/protocols/Tox/src/tox_events.cpp +++ b/protocols/Tox/src/tox_events.cpp @@ -2,11 +2,19 @@ int CToxProto::OnModulesLoaded(WPARAM, LPARAM) { + InitNetlib(); HookEventObj(ME_OPT_INITIALISE, OnOptionsInit, this); return 0; } +int CToxProto::OnPreShutdown(WPARAM, LPARAM) +{ + UninitNetlib(); + + return 0; +} + INT_PTR CToxProto::OnAccountManagerInit(WPARAM, LPARAM lParam) { return (INT_PTR)CreateDialogParam( @@ -124,6 +132,12 @@ void CToxProto::OnStatusMessageChanged(Tox *tox, const int friendnumber, const u void CToxProto::OnUserStatusChanged(Tox *tox, int32_t friendnumber, uint8_t usertatus, void *arg) { + TOX_USERSTATUS userstatus = (TOX_USERSTATUS)usertatus; + if (userstatus == TOX_USERSTATUS::TOX_USERSTATUS_NONE) + { + return; + } + CToxProto *proto = (CToxProto*)arg; std::vector clientId(TOX_CLIENT_ID_SIZE); @@ -133,17 +147,30 @@ void CToxProto::OnUserStatusChanged(Tox *tox, int32_t friendnumber, uint8_t user MCONTACT hContact = proto->FindContact(toxId.c_str()); if (hContact) { - int status = proto->ToxToMirandaStatus((TOX_USERSTATUS)usertatus); + int status = proto->ToxToMirandaStatus(userstatus); proto->SetContactStatus(hContact, status); } } -void CToxProto::OnConnectionStatusChanged(Tox *tox, const int friendId, const uint8_t status, void *arg) +void CToxProto::OnConnectionStatusChanged(Tox *tox, const int friendnumber, const uint8_t status, void *arg) { + CToxProto *proto = (CToxProto*)arg; + + std::vector clientId(TOX_CLIENT_ID_SIZE); + tox_get_client_id(tox, friendnumber, &clientId[0]); + std::string toxId = proto->DataToHexString(clientId); + + MCONTACT hContact = proto->FindContact(toxId.c_str()); + if (hContact) + { + int newStatus = status ? ID_STATUS_ONLINE : ID_STATUS_OFFLINE; + proto->SetContactStatus(hContact, newStatus); + } } -void CToxProto::OnAction(Tox *tox, const int friendId, const uint8_t *message, const uint16_t messageSize, void *arg) +void CToxProto::OnAction(Tox *tox, const int friendnumber, const uint8_t *message, const uint16_t messageSize, void *arg) { + } void CToxProto::OnReadReceipt(Tox *tox, int32_t friendnumber, uint32_t receipt, void *arg) diff --git a/protocols/Tox/src/tox_netlib.cpp b/protocols/Tox/src/tox_netlib.cpp new file mode 100644 index 0000000000..a2fea7a615 --- /dev/null +++ b/protocols/Tox/src/tox_netlib.cpp @@ -0,0 +1,22 @@ +#include "common.h" + +void CToxProto::InitNetlib() +{ + wchar_t name[128]; + mir_sntprintf(name, SIZEOF(name), TranslateT("%s connection"), m_tszUserName); + + NETLIBUSER nlu = {0}; + nlu.cbSize = sizeof(nlu); + nlu.flags = NUF_OUTGOING | NUF_INCOMING | NUF_HTTPCONNS | NUF_UNICODE; + nlu.ptszDescriptiveName = name; + nlu.szSettingsModule = m_szModuleName; + hNetlibUser = (HANDLE)CallService(MS_NETLIB_REGISTERUSER, 0, (LPARAM)&nlu); + + debugLogA("Setting protocol/module name to '%s'", m_szModuleName); +} + +void CToxProto::UninitNetlib() +{ + Netlib_CloseHandle(hNetlibUser); + hNetlibUser = NULL; +} diff --git a/protocols/Tox/src/tox_options.cpp b/protocols/Tox/src/tox_options.cpp index 74463dc454..33d73604c6 100644 --- a/protocols/Tox/src/tox_options.cpp +++ b/protocols/Tox/src/tox_options.cpp @@ -24,7 +24,7 @@ INT_PTR CALLBACK CToxProto::MainOptionsProc(HWND hwnd, UINT uMsg, WPARAM wParam, } SetDlgItemTextA(hwnd, IDC_DATAPATH, dataPath); - ptrW groupName(proto->getTStringA(TOX_SETTINGS_DEFAULT_GROUP)); + ptrW groupName(proto->getTStringA(TOX_SETTINGS_GROUP)); SetDlgItemText(hwnd, IDC_GROUP, groupName); SendDlgItemMessage(hwnd, IDC_GROUP, EM_LIMITTEXT, 64, 0); @@ -121,11 +121,11 @@ INT_PTR CALLBACK CToxProto::MainOptionsProc(HWND hwnd, UINT uMsg, WPARAM wParam, GetDlgItemText(hwnd, IDC_GROUP, groupName, SIZEOF(groupName)); if (lstrlen(groupName) > 0) { - proto->setWString(NULL, TOX_SETTINGS_DEFAULT_GROUP, groupName); + proto->setWString(NULL, TOX_SETTINGS_GROUP, groupName); Clist_CreateGroup(0, groupName); } else - proto->delSetting(NULL, TOX_SETTINGS_DEFAULT_GROUP); + proto->delSetting(NULL, TOX_SETTINGS_GROUP); return TRUE; } diff --git a/protocols/Tox/src/tox_proto.cpp b/protocols/Tox/src/tox_proto.cpp index 11fea96f73..bdf58e51d0 100644 --- a/protocols/Tox/src/tox_proto.cpp +++ b/protocols/Tox/src/tox_proto.cpp @@ -58,7 +58,7 @@ DWORD_PTR __cdecl CToxProto::GetCaps(int type, MCONTACT hContact) MCONTACT __cdecl CToxProto::AddToList(int flags, PROTOSEARCHRESULT* psr) { - return AddContact(_T2A(psr->id)); + return AddContact(_T2A(psr->id), true); } MCONTACT __cdecl CToxProto::AddToListByEvent(int flags, int iContact, HANDLE hDbEvent) { return 0; } @@ -109,6 +109,15 @@ int __cdecl CToxProto::AuthRequest(MCONTACT hContact, const PROTOCHAR* szMessage db_unset(hContact, "CList", "NotOnList"); db_unset(hContact, "CList", "Auth"); + std::vector username(TOX_MAX_NAME_LENGTH); + tox_get_name(tox, friendnumber, &username[0]); + std::string nick(username.begin(), username.end()); + setString(hContact, "Nick", nick.c_str()); + + /*uint8_t userstatus = tox_get_user_status(tox, friends[i]); + int status = ToxToMirandaStatus((TOX_USERSTATUS)userstatus); + SetContactStatus(hContact, status);*/ + return 0; } @@ -158,7 +167,12 @@ int __cdecl CToxProto::SendMsg(MCONTACT hContact, int flags, const char* msg) ULONG messageId = InterlockedIncrement(&hMessageProcess); - tox_send_message_withid(tox, number, messageId, (uint8_t*)msg, strlen(msg)); + int result = tox_send_message_withid(tox, number, messageId, (uint8_t*)msg, strlen(msg)); + if (result < 0) + { + debugLogA("CToxProto::SendMsg: error sending message %i", result); + return 1; + } return messageId; } @@ -232,6 +246,9 @@ int __cdecl CToxProto::OnEvent(PROTOEVENTTYPE iEventType, WPARAM wParam, LPARAM case EV_PROTO_ONCONTACTDELETED: return OnContactDeleted(wParam, lParam); + + case EV_PROTO_ONEXIT: + return OnPreShutdown(wParam, lParam); } return 1; diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index f3c258a22e..d7e566998c 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -76,11 +76,16 @@ private: bool isTerminated; bool isConnected; ULONG hMessageProcess; + HANDLE hNetlibUser; // instances static LIST instanceList; static int CompareProtos(const CToxProto *p1, const CToxProto *p2); + // netlib + void InitNetlib(); + void UninitNetlib(); + // account bool IsOnline(); @@ -91,7 +96,9 @@ private: void __cdecl PollingThread(void*); //events - int OnModulesLoaded(WPARAM, LPARAM); + int __cdecl OnModulesLoaded(WPARAM, LPARAM); + int __cdecl OnPreShutdown(WPARAM, LPARAM); + INT_PTR __cdecl OnAccountManagerInit(WPARAM, LPARAM); static int __cdecl OnOptionsInit(void *obj, WPARAM wParam, LPARAM lParam); @@ -102,8 +109,8 @@ private: static void OnFriendNameChange(Tox *tox, const int friendnumber, const uint8_t *name, const uint16_t nameSize, void *arg); static void OnStatusMessageChanged(Tox *tox, const int friendnumber, const uint8_t* message, const uint16_t messageSize, void *arg); static void OnUserStatusChanged(Tox *tox, int32_t friendnumber, uint8_t usertatus, void *arg); - static void OnConnectionStatusChanged(Tox *tox, const int friendId, const uint8_t status, void *arg); - static void OnAction(Tox *tox, const int friendId, const uint8_t *message, const uint16_t messageSize, void *arg); + static void OnConnectionStatusChanged(Tox *tox, const int friendnumber, const uint8_t status, void *arg); + static void OnAction(Tox *tox, const int friendnumber, const uint8_t *message, const uint16_t messageSize, void *arg); static void OnReadReceipt(Tox *tox, int32_t friendnumber, uint32_t receipt, void *arg); // contacts @@ -117,10 +124,6 @@ private: void LoadContactList(); - //services - - - // utils TOX_USERSTATUS MirandaToToxStatus(int status); int ToxToMirandaStatus(TOX_USERSTATUS userstatus); diff --git a/protocols/Tox/src/tox_utils.cpp b/protocols/Tox/src/tox_utils.cpp index f9e315c9d3..4fb527df27 100644 --- a/protocols/Tox/src/tox_utils.cpp +++ b/protocols/Tox/src/tox_utils.cpp @@ -2,20 +2,14 @@ TOX_USERSTATUS CToxProto::MirandaToToxStatus(int status) { - TOX_USERSTATUS userstatus; + TOX_USERSTATUS userstatus = TOX_USERSTATUS_NONE; switch (status) { - case ID_STATUS_ONLINE: - userstatus = TOX_USERSTATUS_NONE; - break; case ID_STATUS_AWAY: userstatus = TOX_USERSTATUS_AWAY; break; case ID_STATUS_OCCUPIED: userstatus = TOX_USERSTATUS_BUSY; -break; - default: - userstatus = TOX_USERSTATUS_INVALID; break; } return userstatus; @@ -23,7 +17,7 @@ break; int CToxProto::ToxToMirandaStatus(TOX_USERSTATUS userstatus) { - int status; + int status = ID_STATUS_OFFLINE; switch (userstatus) { case TOX_USERSTATUS_NONE: @@ -35,9 +29,6 @@ int CToxProto::ToxToMirandaStatus(TOX_USERSTATUS userstatus) case TOX_USERSTATUS_BUSY: status = ID_STATUS_OCCUPIED; break; - default: - status = ID_STATUS_OFFLINE; - break; } return status; } -- cgit v1.2.3