From 900cb3561405d385c70fc57c94e46bc1e4fa178b Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Thu, 28 Aug 2014 18:08:32 +0000 Subject: Tox: actions support (messages started with '/me') git-svn-id: http://svn.miranda-ng.org/main/trunk@10334 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tox/src/common.h | 3 ++ protocols/Tox/src/tox_account.cpp | 1 + protocols/Tox/src/tox_events.cpp | 84 ++++++++++++++++++++++++++++----------- protocols/Tox/src/tox_proto.cpp | 20 +++++++++- protocols/Tox/src/tox_proto.h | 8 ++-- 5 files changed, 88 insertions(+), 28 deletions(-) diff --git a/protocols/Tox/src/common.h b/protocols/Tox/src/common.h index f50d7393d2..545600d409 100644 --- a/protocols/Tox/src/common.h +++ b/protocols/Tox/src/common.h @@ -26,6 +26,7 @@ #include #include #include +#include #include "tox\tox.h" @@ -41,4 +42,6 @@ extern HINSTANCE g_hInstance; #define TOX_SETTINGS_ID "ToxID" #define TOX_SETTINGS_GROUP "DefaultGroup" +#define TOX_DB_EVENT_TYPE_ACTION 10001 + #endif //_COMMON_H_ \ No newline at end of file diff --git a/protocols/Tox/src/tox_account.cpp b/protocols/Tox/src/tox_account.cpp index 8d3b0715f6..7ab6038480 100644 --- a/protocols/Tox/src/tox_account.cpp +++ b/protocols/Tox/src/tox_account.cpp @@ -27,6 +27,7 @@ void CToxProto::InitToxCore() tox = tox_new(&options); tox_callback_friend_request(tox, OnFriendRequest, this); tox_callback_friend_message(tox, OnFriendMessage, this); + tox_callback_friend_action(tox, OnFriendAction, this); tox_callback_typing_change(tox, OnTypingChanged, this); tox_callback_name_change(tox, OnFriendNameChange, this); tox_callback_status_message(tox, OnStatusMessageChanged, this); diff --git a/protocols/Tox/src/tox_events.cpp b/protocols/Tox/src/tox_events.cpp index e55eba54eb..a133b17e35 100644 --- a/protocols/Tox/src/tox_events.cpp +++ b/protocols/Tox/src/tox_events.cpp @@ -2,9 +2,10 @@ int CToxProto::OnAccountLoaded(WPARAM, LPARAM) { - HookEventObj(ME_OPT_INITIALISE, OnOptionsInit, this); - HookEventObj(ME_PROTO_ACCLISTCHANGED, OnAccountListChanged, this); - HookEventObj(ME_DB_CONTACT_SETTINGCHANGED, OnSettingsChanged, this); + HookProtoEvent(ME_OPT_INITIALISE, &CToxProto::OnOptionsInit); + HookProtoEvent(ME_PROTO_ACCLISTCHANGED, &CToxProto::OnAccountListChanged); + HookProtoEvent(ME_DB_CONTACT_SETTINGCHANGED, &CToxProto::OnSettingsChanged); + HookProtoEvent(ME_MSG_PRECREATEEVENT, &CToxProto::OnPreCreateMessage); InitNetlib(); @@ -28,36 +29,33 @@ INT_PTR CToxProto::OnAccountManagerInit(WPARAM, LPARAM lParam) (LPARAM)this); } -int CToxProto::OnAccountListChanged(void *obj, WPARAM wParam, LPARAM lParam) +int CToxProto::OnAccountListChanged(WPARAM wParam, LPARAM lParam) { - CToxProto *proto = (CToxProto*)obj; PROTOACCOUNT* account = (PROTOACCOUNT*)lParam; - if (wParam == PRAC_ADDED && !strcmp(account->szModuleName, proto->m_szModuleName)) + if (wParam == PRAC_ADDED && !strcmp(account->szModuleName, m_szModuleName)) { - proto->UninitToxCore(); + UninitToxCore(); DialogBoxParam( g_hInstance, MAKEINTRESOURCE(IDD_PROFILE_MANAGER), account->hwndAccMgrUI, CToxProto::ToxProfileManagerProc, - (LPARAM)proto); - proto->InitToxCore(); + (LPARAM)this); + InitToxCore(); } return 0; } -int CToxProto::OnOptionsInit(void *obj, WPARAM wParam, LPARAM) +int CToxProto::OnOptionsInit(WPARAM wParam, LPARAM) { - CToxProto *proto = (CToxProto*)obj; - - char *title = mir_t2a(proto->m_tszUserName); + char *title = mir_t2a(m_tszUserName); OPTIONSDIALOGPAGE odp = { sizeof(odp) }; odp.hInstance = g_hInstance; odp.pszTitle = title; - odp.dwInitParam = LPARAM(obj); + odp.dwInitParam = (LPARAM)this; odp.flags = ODPF_BOLDGROUPS; odp.pszGroup = LPGEN("Network"); @@ -90,33 +88,54 @@ int CToxProto::OnContactDeleted(MCONTACT hContact, LPARAM lParam) return 1; } -int CToxProto::OnSettingsChanged(void *obj, WPARAM hContact, LPARAM lParam) +int CToxProto::OnSettingsChanged(WPARAM hContact, LPARAM lParam) { - CToxProto *proto = (CToxProto*)obj; - DBCONTACTWRITESETTING* dbcws = (DBCONTACTWRITESETTING*)lParam; - if (hContact == NULL && !strcmp(dbcws->szModule, proto->m_szModuleName)) + if (hContact == NULL && !strcmp(dbcws->szModule, m_szModuleName)) { if (!strcmp(dbcws->szSetting, "Nick") && dbcws->value.pszVal) { - if (tox_set_name(proto->tox, (uint8_t*)dbcws->value.pszVal, (uint16_t)strlen(dbcws->value.pszVal))) + if (tox_set_name(tox, (uint8_t*)dbcws->value.pszVal, (uint16_t)strlen(dbcws->value.pszVal))) { - proto->SaveToxData(); + SaveToxData(); } } /*if (!strcmp(dbcws->szSetting, "StatusMsg") || !strcmp(dbcws->szSetting, "StatusNote")) { - if (tox_set_status_message(tox, (uint8_t*)(char*)ptrA(mir_utf8encodeW(dbcws->value.ptszVal)), (uint16_t)_tcslen(dbcws->value.ptszVal))) - { - SaveToxData(); - } + if (tox_set_status_message(tox, (uint8_t*)(char*)ptrA(mir_utf8encodeW(dbcws->value.ptszVal)), (uint16_t)_tcslen(dbcws->value.ptszVal))) + { + SaveToxData(); + } }*/ } return 0; } +int CToxProto::OnPreCreateMessage(WPARAM wParam, LPARAM lParam) +{ + MessageWindowEvent *evt = (MessageWindowEvent *)lParam; + if (strcmp(GetContactProto(evt->hContact), m_szModuleName)) + { + return 0; + } + + 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); + 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::OnFriendRequest(Tox *tox, const uint8_t *address, const uint8_t *message, const uint16_t messageSize, void *arg) { CToxProto *proto = (CToxProto*)arg; @@ -146,6 +165,23 @@ void CToxProto::OnFriendMessage(Tox *tox, const int number, const uint8_t *messa } } +void CToxProto::OnFriendAction(Tox *tox, const int number, const uint8_t *action, const uint16_t actionSize, void *arg) +{ + CToxProto *proto = (CToxProto*)arg; + + MCONTACT hContact = proto->FindContact(number); + if (hContact) + { + proto->AddDbEvent( + hContact, + TOX_DB_EVENT_TYPE_ACTION, + time(NULL), + DBEF_UTF, + actionSize, + (BYTE*)action); + } +} + void CToxProto::OnTypingChanged(Tox *tox, const int number, uint8_t isTyping, void *arg) { CToxProto *proto = (CToxProto*)arg; diff --git a/protocols/Tox/src/tox_proto.cpp b/protocols/Tox/src/tox_proto.cpp index 9547b33f90..3693cfc741 100644 --- a/protocols/Tox/src/tox_proto.cpp +++ b/protocols/Tox/src/tox_proto.cpp @@ -30,6 +30,15 @@ CToxProto::CToxProto(const char* protoName, const TCHAR* userName) : sid.ptszDescription = LPGENT("Protocol icon"); sid.iDefaultIndex = -IDI_TOX; Skin_AddIcon(&sid); + + // custom event + DBEVENTTYPEDESCR dbEventType = { sizeof(dbEventType) }; + dbEventType.module = this->m_szModuleName; + dbEventType.flags = DETF_HISTORY | DETF_MSGWINDOW; + + dbEventType.eventType = TOX_DB_EVENT_TYPE_ACTION; + dbEventType.descr = "Tox action"; + CallService(MS_DB_EVENT_REGISTERTYPE, 0, (LPARAM)&dbEventType); } CToxProto::~CToxProto() @@ -235,7 +244,16 @@ int __cdecl CToxProto::SendMsg(MCONTACT hContact, int flags, const char* msg) uint32_t number = tox_get_friend_number(tox, clientId.data()); - int result = tox_send_message(tox, number, (uint8_t*)msg, (uint16_t)strlen(msg)); + 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"); diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index addeec0037..c910317abb 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -103,14 +103,16 @@ private: int __cdecl OnContactDeleted(MCONTACT, LPARAM); int __cdecl OnPreShutdown(WPARAM, LPARAM); - static int __cdecl OnOptionsInit(void *obj, WPARAM wParam, LPARAM lParam); - static int __cdecl OnSettingsChanged(void *obj, WPARAM wParam, LPARAM lParam); - static int __cdecl OnAccountListChanged(void *obj, WPARAM wParam, LPARAM lParam); + int __cdecl OnOptionsInit(WPARAM wParam, LPARAM lParam); + int __cdecl OnSettingsChanged(WPARAM wParam, LPARAM lParam); + int __cdecl OnAccountListChanged(WPARAM wParam, LPARAM lParam); + int __cdecl OnPreCreateMessage(WPARAM wParam, LPARAM lParam); INT_PTR __cdecl OnAccountManagerInit(WPARAM, LPARAM); static void OnFriendRequest(Tox *tox, const uint8_t *address, const uint8_t *message, const uint16_t messageSize, void *arg); 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 OnFriendNameChange(Tox *tox, const int number, const uint8_t *name, const uint16_t nameSize, void *arg); static void OnStatusMessageChanged(Tox *tox, const int number, const uint8_t* message, const uint16_t messageSize, void *arg); -- cgit v1.2.3