From 1cba3f74c9607b7b03c85f23969cbdd4163b52d5 Mon Sep 17 00:00:00 2001 From: MikalaiR Date: Thu, 24 Mar 2016 10:22:48 +0000 Subject: Telegram: attempt to implement login git-svn-id: http://svn.miranda-ng.org/main/trunk@16534 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Telegram/src/stdafx.h | 2 + protocols/Telegram/src/t_callback.cpp | 36 +++++++++++++- protocols/Telegram/src/t_messages.cpp | 5 +- protocols/Telegram/src/t_proto.cpp | 52 ++++++++++++++++++-- protocols/Telegram/src/t_proto.h | 9 +++- protocols/Telegram/src/t_utils.cpp | 90 +++++++++++++++++++++++++++++++++++ 6 files changed, 186 insertions(+), 8 deletions(-) create mode 100644 protocols/Telegram/src/t_utils.cpp (limited to 'protocols/Telegram') diff --git a/protocols/Telegram/src/stdafx.h b/protocols/Telegram/src/stdafx.h index 6c0c2016cb..bf44f897c1 100644 --- a/protocols/Telegram/src/stdafx.h +++ b/protocols/Telegram/src/stdafx.h @@ -43,11 +43,13 @@ extern "C" #include "tgl\tgl.h" #include "tgl\tgl-net.h" #include "tgl\tgl-timers.h" + #include "tgl\tgl-binlog.h" } struct MirTLS : public tgl_state, public MZeroedObject { struct CTelegramProto *m_proto; + MirTLS(CTelegramProto *p) : m_proto(p) {} }; #include "version.h" diff --git a/protocols/Telegram/src/t_callback.cpp b/protocols/Telegram/src/t_callback.cpp index 97d7eb0d1a..9045ab2716 100644 --- a/protocols/Telegram/src/t_callback.cpp +++ b/protocols/Telegram/src/t_callback.cpp @@ -5,11 +5,39 @@ static void update_message_handler(tgl_state *TLS, tgl_message *msg) ((MirTLS*)TLS)->m_proto->OnMessage(msg); } +static void user_typing(tgl_state *TLS, tgl_user *U, enum tgl_typing_status status) +{ + ((MirTLS*)TLS)->m_proto->OnUserTyping(U, status); +} + +static void on_login_success(tgl_state *TLS) +{ + +} + +static void on_login_failed(tgl_state *TLS) +{ + +} + +static void on_ready(tgl_state *TLS) +{ + +} + +void request_value(struct tgl_state *TLS, enum tgl_value_type type, const char *prompt, int num_values, + void(*callback) (struct tgl_state *TLS, const char *string[], void *arg), void *arg) +{ + char *result = nullptr; + ((MirTLS*)TLS)->m_proto->TGLGetValue(type, prompt, num_values, &result); + callback(TLS, (const char**)&result, arg); +} + static void logprintf(const char *fmt, ...) { CMStringA str("[Telegram]: "); va_list args; - va_start(fmt, args); + va_start(args, fmt); str.AppendFormatV(fmt, args); va_end(args); CallService(MS_NETLIB_LOG, 0, (LPARAM)str.GetString()); @@ -21,6 +49,12 @@ void CTelegramProto::InitCallbacks() cb.new_msg = update_message_handler; cb.msg_receive = update_message_handler; cb.logprintf = logprintf; + cb.type_notification = user_typing; + cb.logged_in = on_login_success; + cb.started = on_ready; + cb.get_values = request_value; + + tgl_set_callback(TLS, &cb); diff --git a/protocols/Telegram/src/t_messages.cpp b/protocols/Telegram/src/t_messages.cpp index 0ba8cfa0bb..f3aa404f9a 100644 --- a/protocols/Telegram/src/t_messages.cpp +++ b/protocols/Telegram/src/t_messages.cpp @@ -1,6 +1,9 @@ #include "stdafx.h" -void CTelegramProto::OnMessage(tgl_message*) +void CTelegramProto::OnMessage(tgl_message *msg) { +} +void CTelegramProto::OnUserTyping(tgl_user *U, tgl_typing_status status) +{ } \ No newline at end of file diff --git a/protocols/Telegram/src/t_proto.cpp b/protocols/Telegram/src/t_proto.cpp index fd495a7f5f..ee99a0caa9 100644 --- a/protocols/Telegram/src/t_proto.cpp +++ b/protocols/Telegram/src/t_proto.cpp @@ -19,26 +19,56 @@ along with this program. If not, see . CTelegramProto::CTelegramProto(const char* protoName, const TCHAR* userName) : PROTO(protoName, userName) { - TLS = new MirTLS; + TLS = new MirTLS(this); + + tgl_set_verbosity(TLS, 4); InitNetwork(); + InitCallbacks(); + tgl_set_timer_methods(TLS, &tgl_libevent_timers); - tgl_set_rsa_key(TLS, TELEGRAM_PUBLIC_KEY); + tgl_set_rsa_key(TLS, "tgl.pub"); + + + TLS->base_path = Utils_ReplaceVars("%miranda_profilesdir%\\%miranda_profilename%\\TGL_Data\\"); + CreateDirectoryTree(TLS->base_path); + + tgl_set_download_directory(TLS, CMStringA(FORMAT, "%s\\Downloads\\", TLS->base_path)); + CreateDirectoryTree(TLS->downloads_directory); + - tgl_register_app_id(TLS, TELEGRAM_API_ID, TELEGRAM_API_HASH); - tgl_set_app_version(TLS, g_szMirVer); +// tgl_register_app_id(TLS, TELEGRAM_API_ID, TELEGRAM_API_HASH); +// tgl_set_app_version(TLS, g_szMirVer); - tgl_init(TLS); + + ReadState(); + ReadAuth(); + } CTelegramProto::~CTelegramProto() { + SaveState(); } DWORD_PTR CTelegramProto::GetCaps(int type, MCONTACT) { + switch (type) + { + case PFLAGNUM_1: + return PF1_IM | PF1_AUTHREQ | PF1_CHAT | PF1_BASICSEARCH | PF1_MODEMSG | PF1_FILE; + case PFLAGNUM_2: + return PF2_ONLINE; + case PFLAGNUM_3: + return PF2_ONLINE; + case PFLAGNUM_4: + return PF4_FORCEADDED | PF4_NOAUTHDENYREASON | PF4_SUPPORTTYPING | PF4_AVATARS | PF4_IMSENDOFFLINE | PF4_OFFLINEFILES; + case PFLAG_UNIQUEIDTEXT: + case PFLAG_UNIQUEIDSETTING: + return (DWORD_PTR)"ID"; + } return 0; } @@ -90,6 +120,7 @@ int CTelegramProto::SendMsg(MCONTACT hContact, int flags, const char *msg) int CTelegramProto::SetStatus(int iNewStatus) { + if (iNewStatus == ID_STATUS_ONLINE) tgl_login(TLS); return 0; } @@ -105,5 +136,16 @@ int CTelegramProto::OnEvent(PROTOEVENTTYPE iEventType, WPARAM wParam, LPARAM lPa int CTelegramProto::OnPreShutdown(WPARAM, LPARAM) { + SaveState(); return 0; } + + +void CTelegramProto::TGLGetValue(tgl_value_type type, const char *prompt, int num_values, char **result) +{ + switch (type) + { + case tgl_phone_number: + *result = getStringA("ID"); + }; +} \ No newline at end of file diff --git a/protocols/Telegram/src/t_proto.h b/protocols/Telegram/src/t_proto.h index 927718340e..fadf3b179c 100644 --- a/protocols/Telegram/src/t_proto.h +++ b/protocols/Telegram/src/t_proto.h @@ -80,11 +80,13 @@ public: static INT_PTR EventGetIcon(WPARAM wParam, LPARAM lParam); static INT_PTR GetEventText(WPARAM, LPARAM lParam); + void TGLGetValue(tgl_value_type type, const char *prompt, int num_values, char **result); - void OnMessage(tgl_message*); + void OnMessage(tgl_message*); + void OnUserTyping(tgl_user *U, tgl_typing_status status); private: @@ -92,6 +94,11 @@ private: MirTLS *TLS; static mir_cs accountsLock; + void ReadState(); + void SaveState(); + + void ReadAuth(); + void SaveAuth(); //---Accounts static LIST CTelegramProto::Accounts; diff --git a/protocols/Telegram/src/t_utils.cpp b/protocols/Telegram/src/t_utils.cpp new file mode 100644 index 0000000000..16d9e1de4c --- /dev/null +++ b/protocols/Telegram/src/t_utils.cpp @@ -0,0 +1,90 @@ +#include "stdafx.h" + +void CTelegramProto::ReadState() +{ + DBVARIANT dbv = { 0 }; + if (db_get(0, m_szModuleName, "TGL_STATE", &dbv)) + return; + + int *x = (int*)dbv.pbVal; + + bl_do_set_seq(TLS, x[0]); + bl_do_set_pts(TLS, x[1]); + bl_do_set_qts(TLS, x[2]); + bl_do_set_date(TLS, x[3]); + + db_free(&dbv); +} + +void CTelegramProto::SaveState() +{ + int x[4]; + + x[0] = TLS->pts; + x[1] = TLS->qts; + x[2] = TLS->seq; + x[3] = TLS->date; + + db_set_blob(0, m_szModuleName, "TGL_STATE", &x, sizeof(x)); + +} + +void read_dc(tgl_state *TLS, int *&piBlob, int id) { + int port = *piBlob++; + int l = *piBlob++; + assert(l >= 0 && l < 100); + + char ip[100]; + memcpy(ip, piBlob, l); + piBlob += (l / sizeof(int)); + ip[l] = 0; + + long long auth_key_id = *(long long*)piBlob; + piBlob += 2; + static unsigned char auth_key[256]; + memcpy(auth_key, piBlob, 256); + piBlob += (256 / sizeof(int)); + + bl_do_dc_option(TLS, id, "DC", 2, ip, l, port); + bl_do_set_auth_key(TLS, id, auth_key); + bl_do_dc_signed(TLS, id); +} + + +void CTelegramProto::ReadAuth() +{ + DBVARIANT dbv = { 0 }; + if (db_get(0, m_szModuleName, "TGL_AUTH", &dbv)) + { + bl_do_dc_option(TLS, 1, "", 0, TG_SERVER_1, strlen(TG_SERVER_1), 443); + bl_do_dc_option(TLS, 2, "", 0, TG_SERVER_2, strlen(TG_SERVER_2), 443); + bl_do_dc_option(TLS, 3, "", 0, TG_SERVER_3, strlen(TG_SERVER_3), 443); + bl_do_dc_option(TLS, 4, "", 0, TG_SERVER_4, strlen(TG_SERVER_4), 443); + bl_do_dc_option(TLS, 5, "", 0, TG_SERVER_5, strlen(TG_SERVER_5), 443); + bl_do_set_working_dc(TLS, TG_SERVER_DEFAULT); + return; + } + + int *piBlob = (int*)dbv.pbVal; + + size_t x = (size_t)*piBlob++; + int dc_working_num = *piBlob++; + + for (size_t i = 0; i < x; i++) + { + int y = *piBlob++; + if (y) + { + read_dc(TLS, piBlob, i); + } + } + bl_do_set_working_dc(TLS, dc_working_num); + + int our_id = *piBlob++; + + if (our_id) { + bl_do_set_our_id(TLS, TGL_MK_USER(our_id).id); + } + + db_free(&dbv); +} \ No newline at end of file -- cgit v1.2.3