diff --git a/loop.c b/loop.c index 61b0a5b..566bf2e 100644 --- a/loop.c +++ b/loop.c @@ -556,14 +556,28 @@ char* network_request_registration () } /** - * Verify the phone, by providing the code and the real name + * Request a verification for the given client, by sending + * a code to the current phone number + */ +char* network_request_phone_registration () +{ + return do_send_code (default_username); +} + + +/** + * Verify the phone number by providing the sms_code and the real name * * NOTE: This should be called when the phone number was previously * unknown to the telegram network. */ -int network_verify_phone_registration(char* code, char *firstname, char *lastname) +int network_verify_phone_registration(const char* code, const char *sms_hash, + const char *first ,const char *last) { - if (do_send_code_result_auth (code, firstname, lastname) >= 0) { + logprintf("Registering with code:%s, hash:%s, first:%s, last:%s\n", code, sms_hash, + first, last); + if (do_send_code_result_auth (code, sms_hash, first, last) >= 0) { + logprintf ("Authentication successfull, state = 300\n"); auth_state = 300; return 1; } @@ -575,9 +589,9 @@ int network_verify_phone_registration(char* code, char *firstname, char *lastnam */ int network_verify_registration(const char *code, const char *sms_hash) { - logprintf("telegram: pointer - code: %p, hash: %p\n", code, sms_hash); - logprintf("telegram: string - code: %s, hash: %s\n", code, sms_hash); - if (do_send_code_result (code, sms_hash) >= 0) { + logprintf("Verifying with hash:%s, code:%s\n", code, sms_hash); + int state; + if ((state = do_send_code_result (code, sms_hash)) >= 0) { logprintf ("Authentication successfull, state = 300\n"); auth_state = 300; return 1; diff --git a/purple-plugin/telegram-purple.c b/purple-plugin/telegram-purple.c index ffab3d8..20ac264 100644 --- a/purple-plugin/telegram-purple.c +++ b/purple-plugin/telegram-purple.c @@ -49,6 +49,7 @@ // telegram-purple includes #include "loop.h" +#include "queries.h" #include "telegram-purple.h" static PurplePlugin *_telegram_protocol = NULL; @@ -96,6 +97,36 @@ static void tgprpl_tooltip_text(PurpleBuddy * buddy, PurpleNotifyUserInfo * info purple_debug_info(PLUGIN_ID, "tgprpl_tooltip_text()\n"); } +/** + * Request a verification key, save the returned verification_hash in the account settings + * for later usage and inform the user. + */ +static void login_request_verification(PurpleAccount *acct) +{ + // TODO: we should find a way to request the key + // only once. + purple_debug_info(PLUGIN_ID, "No code provided, requesting new authentication code.\n"); + char *new_hash = network_request_registration(); + purple_debug_info(PLUGIN_ID, "Saving verification_hash: '%s'", new_hash); + purple_account_set_string(acct, "verification_hash", new_hash); + + purple_notify_message(_telegram_protocol, PURPLE_NOTIFY_MSG_INFO, "Please Verify", + "You need to verify this device, please enter the code Telegram has sent to you by SMS.", + NULL, NULL, NULL); +} + +/** + * Handle a failed verification, by removing the invalid sms code and + * notifying the user + */ +static void login_verification_fail(PurpleAccount *acct) +{ + // remove invalid sms code, so we won't try to register again + purple_account_set_string(acct, "verification_key", ""); + purple_notify_message(_telegram_protocol, PURPLE_NOTIFY_MSG_INFO, "Verification Failed", + "Please make sure you entered the correct verification code.", NULL, NULL, NULL); +} + /** * This must be implemented. */ @@ -120,7 +151,7 @@ static void tgprpl_login(PurpleAccount * acct) purple_debug_info(PLUGIN_ID, "running_for_first_time()\n"); running_for_first_time(); - // load all settings: the known network topology, log and configuration file paths + // load all settings: the known network topology, secret keys, logs and configuration file paths purple_debug_info(PLUGIN_ID, "parse_config()\n"); parse_config (); purple_debug_info(PLUGIN_ID, "set_default_username()\n"); @@ -130,34 +161,50 @@ static void tgprpl_login(PurpleAccount * acct) purple_debug_info(PLUGIN_ID, "Connecting to the Telegram network...\n"); network_connect(); - // Login + // Assure phone number registration if (!network_phone_is_registered()) { purple_debug_info(PLUGIN_ID, "Phone is not registered, registering...\n"); + const char *first_name = purple_account_get_string(acct, "first_name", NULL); + const char *last_name = purple_account_get_string(acct, "last_name", NULL); + if (!first_name || !last_name || !strlen(first_name) > 0 || !strlen(last_name) > 0) { + purple_notify_message(_telegram_protocol, PURPLE_NOTIFY_MSG_INFO, "Registration Needed", + "Enter your first and last name to register this phone number with the telegram network.", + NULL, NULL, NULL); + return; + } + if (code && strlen(code) > 0 && hash && strlen(hash) > 0) { + int registered = network_verify_phone_registration(code, hash, first_name, last_name); + if (registered) { + store_config(); + } else { + login_verification_fail(acct); + return; + } + } else { + login_request_verification(acct); + return; + } } + // Assure client registration if (!network_client_is_registered()) { purple_debug_info(PLUGIN_ID, "Client is not registered\n"); - if (code && hash) { + if (code && strlen(code) > 0 && hash && strlen(hash) > 0) { purple_debug_info(PLUGIN_ID, "SMS code provided, trying to verify \n"); - purple_debug_info(PLUGIN_ID, "pointer - code:%p hash:%p\n", code, hash); - purple_debug_info(PLUGIN_ID, "string - code%s hash:%s\n", code, hash); + purple_debug_info(PLUGIN_ID, "strlen - code: %lu hash: %lu\n", strlen(code), strlen(hash)); + purple_debug_info(PLUGIN_ID, "pointer - code: %p hash: %p\n", code, hash); + purple_debug_info(PLUGIN_ID, "string - code: %s hash: %s\n", code, hash); int verified = network_verify_registration(code, hash); if (verified) { store_config(); } else { - purple_connection_set_state(gc, PURPLE_DISCONNECTED); - purple_notify_message(_telegram_protocol, PURPLE_NOTIFY_MSG_INFO, "Verification Failed", - "Please make sure you entered the correct verification code.", NULL, NULL, NULL); + login_verification_fail(acct); return; } } else { - // TODO: we should find a way to request the key - // only once. - purple_debug_info(PLUGIN_ID, "Device not registered, requesting new authentication code.\n"); - char *new_hash = network_request_registration(); - purple_debug_info(PLUGIN_ID, "Saving verification_hash: '%s'", new_hash); - purple_account_set_string(acct, "verification_hash", new_hash); + login_request_verification(acct); + return; } } @@ -581,6 +628,12 @@ static void tgprpl_init(PurplePlugin *plugin) option = purple_account_option_string_new("Verification hash", "verification_hash", NULL); prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); + option = purple_account_option_string_new("First Name", "first_name", NULL); + prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); + + option = purple_account_option_string_new("Last Name", "last_name", NULL); + prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); + // TODO: Path to public key (When you can change the server hostname, // you should also be able to change the public key) diff --git a/queries.c b/queries.c index 181f766..10b450d 100644 --- a/queries.c +++ b/queries.c @@ -644,8 +644,18 @@ int sign_in_on_answer (struct query *q UU) { return 0; } +char lasterror[75]; +const char *get_last_err() +{ + return lasterror; +} + int sign_in_on_error (struct query *q UU, int error_code, int l, char *error) { logprintf ( "error_code = %d, error = %.*s\n", error_code, l, error); + if (sizeof(lasterror) >= strlen(error)) { + int dest = memcmp(lasterror, error, strlen(error)); + logprintf("memcpy-state: %d", dest); + } sign_in_ok = -1; return 0; } @@ -667,11 +677,11 @@ int do_send_code_result (const char *code, const char *sms_hash) { return sign_in_ok; } -int do_send_code_result_auth (const char *code, const char *first_name, const char *last_name) { +int do_send_code_result_auth (const char *code, const char *sms_hash, const char *first_name, const char *last_name) { clear_packet (); out_int (CODE_auth_sign_up); out_string (suser); - out_string (phone_code_hash); + out_string (sms_hash); out_string (code); out_string (first_name); out_string (last_name); diff --git a/queries.h b/queries.h index 189f89d..8456600 100644 --- a/queries.h +++ b/queries.h @@ -98,7 +98,7 @@ void do_load_document_thumb (struct document *video, int next); void do_help_get_config (void); int do_auth_check_phone (const char *user); int do_get_nearest_dc (void); -int do_send_code_result_auth (const char *code, const char *first_name, const char *last_name); +int do_send_code_result_auth (const char *code, const char *sms_hash, const char *first_name, const char *last_name); void do_import_auth (int num); void do_export_auth (int num); void do_add_contact (const char *phone, int phone_len, const char *first_name, int first_name_len, const char *last_name, int last_name_len, int force); @@ -121,3 +121,5 @@ void do_restore_msg (long long id); int get_dh_config_on_answer (struct query *q); void fetch_dc_option (void); #endif + +const char *get_last_err(); diff --git a/tg-cli.h b/tg-cli.h index 128e69c..1989dfd 100755 --- a/tg-cli.h +++ b/tg-cli.h @@ -24,6 +24,12 @@ char* network_request_registration(); */ int network_verify_registration(const char *code, const char *sms_hash); +/** + * Verify the registration with the given registration code + */ +int network_verify_phone_registration(const char *code, const char *sms_hash, + const char *first, const char *last); + /** * Retur if the current phone is registered in the given network. */