From ce41af2f845523617e8efc7e270a9b50bbdc0df5 Mon Sep 17 00:00:00 2001 From: Wizou Date: Fri, 27 Aug 2021 22:44:43 +0200 Subject: [PATCH] Validation of logged-in user can also be done by its user_id --- README.md | 2 +- src/Client.cs | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 816b39c..7a4c447 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ await client.SendMessageAsync(target, "Hello, World"); An invalid API request can result in a RpcException being raised, reflecting the [error code and status text](https://core.telegram.org/api/errors) of the problem. -The other configuration items that you can override include: **session_pathname, server_address, device_model, system_version, app_version, system_lang_code, lang_pack, lang_code** +The other configuration items that you can override include: **session_pathname, server_address, device_model, system_version, app_version, system_lang_code, lang_pack, lang_code, user_id** Optional API parameters have a default value of `null` when unset. Passing `null` for a required string/array is the same as *empty* (0-length). Required API parameters/fields can sometimes be set to 0 or `null` when unused (check API documentation). diff --git a/src/Client.cs b/src/Client.cs index 9f91ef6..c8b6791 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -604,13 +604,23 @@ namespace WTelegram case MsgsAck msgsAck: break; // we don't do anything with these, for now case BadMsgNotification badMsgNotification: - Helpers.Log(4, $"BadMsgNotification {badMsgNotification.error_code} for msg #{(short)badMsgNotification.bad_msg_id.GetHashCode():X4}"); - goto default; + { + Helpers.Log(4, $"BadMsgNotification {badMsgNotification.error_code} for msg #{(short)badMsgNotification.bad_msg_id.GetHashCode():X4}"); + var (type, tcs) = PullPendingRequest(badMsgNotification.bad_msg_id); + if (tcs != null) + { + if (_bareRequest == badMsgNotification.bad_msg_id) _bareRequest = 0; + _ = Task.Run(() => tcs.SetException(new ApplicationException($"BadMsgNotification {badMsgNotification.error_code}"))); + } + else if (_updateHandler != null) + await _updateHandler?.Invoke(obj); + } + break; default: if (_bareRequest != 0) { var (type, tcs) = PullPendingRequest(_bareRequest); - if (type.IsAssignableFrom(obj.GetType())) + if (type?.IsAssignableFrom(obj.GetType()) == true) { _bareRequest = 0; _ = Task.Run(() => tcs.SetResult(obj)); @@ -670,12 +680,16 @@ namespace WTelegram /// Detail about the logged user public async Task LoginUserIfNeeded(CodeSettings settings = null) { - string phone_number = Config("phone_number"); + string phone_number = null; if (_session.User != null) { try { var prevUser = Schema.Deserialize(_session.User); + var userId = _config("user_id"); // if config prefers to validate current user by its id, use it + if (userId != null && int.TryParse(userId, out int id) && prevUser.id == id) + return prevUser; + phone_number = Config("phone_number"); // otherwise, validation is done by the phone number if (prevUser?.phone == string.Concat(phone_number.Where(char.IsDigit))) return prevUser; } @@ -685,6 +699,7 @@ namespace WTelegram } await Auth_LogOut(); } + phone_number ??= Config("phone_number"); var sentCode = await Auth_SendCode(phone_number, _apiId, _apiHash, settings ?? new()); Helpers.Log(3, $"A verification code has been sent via {sentCode.type.GetType().Name[17..]}"); var verification_code = Config("verification_code");