diff --git a/telegram-purple.c b/telegram-purple.c index 3c1aef8..24ffe1c 100644 --- a/telegram-purple.c +++ b/telegram-purple.c @@ -34,6 +34,7 @@ static void update_marked_read (struct tgl_state *TLS, int num, struct tgl_messa static void update_on_logged_in (struct tgl_state *TLS); static void update_on_ready (struct tgl_state *TLS); static void on_user_get_info (struct tgl_state *TLS, void *info_data, int success, struct tgl_user *U); +static void update_on_failed_login (struct tgl_state *TLS); const char *config_dir = "telegram-purple"; const char *user_pk_filename = "server.tglpub"; @@ -56,7 +57,8 @@ struct tgl_update_callback tgp_callback = { .secret_chat_update = update_secret_chat_handler, .type_notification = update_user_typing, .marked_read = update_marked_read, - .create_print_name = tgp_blist_create_print_name + .create_print_name = tgp_blist_create_print_name, + .on_failed_login = update_on_failed_login }; static void _update_buddy (struct tgl_state *TLS, tgl_peer_t *user, unsigned flags) { @@ -521,6 +523,18 @@ static void update_on_ready (struct tgl_state *TLS) { tgl_do_update_contact_list (TLS, 0, 0); } +static void update_on_failed_login (struct tgl_state *TLS) { + info ("update_on_failed_login(): Login to telegram failed."); + + // 401: SESSION_REVOKED is called when the session is cancelled during runtime + // 401: AUTH_KEY_UNREGISTERED is called when logging into a revoked session + if (strstr (TLS->error, "SESSION_REVOKED") || + strstr (TLS->error, "AUTH_KEY_UNREGISTERED")) { + purple_account_set_bool (tls_get_pa (TLS), TGP_KEY_RESET_AUTH, TRUE); + } + purple_connection_error (tls_get_conn (TLS), TLS->error); +} + static void tgprpl_login (PurpleAccount * acct) { info ("tgprpl_login(): Purple is telling the prpl to connect the account"); @@ -597,6 +611,12 @@ static void tgprpl_login (PurpleAccount * acct) { read_auth_file (TLS); read_state_file (TLS); + if (purple_account_get_bool (acct, TGP_KEY_RESET_AUTH, FALSE)) { + info ("last login attempt failed, resetting authorization ..."); + purple_account_set_bool (tls_get_pa (TLS), TGP_KEY_RESET_AUTH, FALSE); + bl_do_reset_authorization (TLS); + } + purple_connection_set_state (conn->gc, PURPLE_CONNECTING); tgl_login (TLS); } diff --git a/telegram-purple.h b/telegram-purple.h index a50b20d..b946aa8 100644 --- a/telegram-purple.h +++ b/telegram-purple.h @@ -87,6 +87,8 @@ #define TGP_DEFAULT_SEND_READ_NOTIFICATIONS TRUE #define TGP_KEY_SEND_READ_NOTIFICATIONS "send-read-notifications" +#define TGP_KEY_RESET_AUTH "reset-authorization" + extern const char *pk_path; extern const char *user_pk_filename; extern const char *config_dir;