Add compatibility mode to support SMS authentication on clients that can't prompt the user directly
Read the sms code from settings in case compatibility mode is selected or the purple_request functions do not return a valid code.
This commit is contained in:
parent
2586273f72
commit
ba177d885a
3 changed files with 39 additions and 38 deletions
|
@ -293,25 +293,23 @@ static void request_name_code_entered (PurpleConnection* gc, PurpleRequestFields
|
|||
static void request_code (struct tgl_state *TLS) {
|
||||
debug ("Client is not registered, registering...\n");
|
||||
telegram_conn *conn = TLS->ev_base;
|
||||
|
||||
purple_request_input (
|
||||
conn->gc, // handle (the PurpleAccount)
|
||||
"Telegram Code", // title
|
||||
"Enter Telegram Code", // primary
|
||||
"Telegram wants to verify your identity, please enter the code, that you have received via SMS.", // secondary
|
||||
NULL, // default_value
|
||||
0, // multiline
|
||||
0, // masked
|
||||
"code", // hint
|
||||
"OK", // ok_text
|
||||
G_CALLBACK(request_code_entered),
|
||||
"Cancel", // cancel_text
|
||||
G_CALLBACK(request_code_canceled),
|
||||
conn->pa, // account
|
||||
NULL, // who
|
||||
NULL, // conv
|
||||
TLS // user_data
|
||||
);
|
||||
int compat = purple_account_get_bool (tg_get_acc(TLS), "compat-verification", 0);
|
||||
|
||||
if (compat || ! purple_request_input (conn->gc, "Telegram Code", "Enter Telegram Code",
|
||||
"Telegram wants to verify your identity, please enter the code, that you have received via SMS.",
|
||||
NULL, 0, 0, "code", "OK", G_CALLBACK(request_code_entered), "Cancel",
|
||||
G_CALLBACK(request_code_canceled), conn->pa, NULL, NULL, TLS)) {
|
||||
const char *sms = purple_account_get_string(tg_get_acc(TLS), "code", "");
|
||||
if (*sms == '\0') {
|
||||
const char *error = "Could not prompt for sms code, please set SMS code in account settings and reconnect.";
|
||||
purple_connection_error_reason(conn->gc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, error);
|
||||
purple_notify_error(_telegram_protocol, "Enter SMS code", "Enter SMS code", error);
|
||||
return;
|
||||
}
|
||||
|
||||
request_code_entered (TLS, sms);
|
||||
purple_account_set_string(tg_get_acc(TLS), "code", "");
|
||||
}
|
||||
}
|
||||
|
||||
static void request_name_and_code (struct tgl_state *TLS) {
|
||||
|
@ -334,8 +332,13 @@ static void request_name_and_code (struct tgl_state *TLS) {
|
|||
purple_request_field_group_add_field(group, field);
|
||||
purple_request_fields_add_group(fields, group);
|
||||
|
||||
purple_request_fields(conn->gc, "Register", "Please register your phone number.", NULL, fields, "Ok",
|
||||
G_CALLBACK( request_name_code_entered ), "Cancel", NULL, conn->pa, NULL, NULL, conn->gc);
|
||||
if (!purple_request_fields (conn->gc, "Register", "Please register your phone number.", NULL, fields, "Ok",
|
||||
G_CALLBACK( request_name_code_entered ), "Cancel", NULL, conn->pa, NULL, NULL, conn->gc)) {
|
||||
// purple_request API not available
|
||||
const char *error = "Phone number is not registered, please register your phone on a different client.";
|
||||
purple_connection_error_reason (conn->gc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, error);
|
||||
purple_notify_error(_telegram_protocol, "Not Registered", "Not Registered", error);
|
||||
}
|
||||
}
|
||||
|
||||
static void sign_in_callback (struct tgl_state *TLS, void *extra, int success, int registered, const char *mhash) {
|
||||
|
|
|
@ -58,7 +58,7 @@
|
|||
#include <telegram-purple.h>
|
||||
#include <msglog.h>
|
||||
|
||||
static PurplePlugin *_telegram_protocol = NULL;
|
||||
PurplePlugin *_telegram_protocol = NULL;
|
||||
PurpleGroup *tggroup;
|
||||
const char *config_dir = ".telegram-purple";
|
||||
const char *pk_path = "/etc/telegram-purple/server.pub";
|
||||
|
@ -296,8 +296,9 @@ void on_chat_get_info (struct tgl_state *TLS, void *extra, int success, struct t
|
|||
}
|
||||
|
||||
void on_ready (struct tgl_state *TLS) {
|
||||
debug ("telegram_on_ready().\n");
|
||||
debug ("on_ready().\n");
|
||||
telegram_conn *conn = TLS->ev_base;
|
||||
|
||||
purple_connection_set_state(conn->gc, PURPLE_CONNECTED);
|
||||
purple_connection_set_display_name(conn->gc, purple_account_get_username(conn->pa));
|
||||
purple_blist_add_account(conn->pa);
|
||||
|
@ -341,11 +342,11 @@ static GList *tgprpl_status_types (PurpleAccount * acct) {
|
|||
GList *types = NULL;
|
||||
PurpleStatusType *type;
|
||||
type = purple_status_type_new_with_attrs (PURPLE_STATUS_AVAILABLE, NULL, NULL,
|
||||
1, 1, 0, "message", "Message", purple_value_new (PURPLE_TYPE_STRING), NULL);
|
||||
1, 1, 0, "message", "Message", purple_value_new (PURPLE_TYPE_STRING), NULL);
|
||||
types = g_list_prepend (types, type);
|
||||
|
||||
type = purple_status_type_new_with_attrs (PURPLE_STATUS_MOBILE, NULL, NULL, 1,
|
||||
1, 0, "message", "Message", purple_value_new (PURPLE_TYPE_STRING), NULL);
|
||||
1, 0, "message", "Message", purple_value_new (PURPLE_TYPE_STRING), NULL);
|
||||
types = g_list_prepend (types, type);
|
||||
|
||||
type = purple_status_type_new (PURPLE_STATUS_OFFLINE, NULL, NULL, 1);
|
||||
|
@ -653,19 +654,15 @@ static void tgprpl_init (PurplePlugin *plugin) {
|
|||
PurpleAccountOption *option;
|
||||
GList *verification_values = NULL;
|
||||
|
||||
// Extra Options
|
||||
#define ADD_VALUE(list, desc, v) { \
|
||||
PurpleKeyValuePair *kvp = g_new0(PurpleKeyValuePair, 1); \
|
||||
kvp->key = g_strdup((desc)); \
|
||||
kvp->value = g_strdup((v)); \
|
||||
list = g_list_prepend(list, kvp); \
|
||||
}
|
||||
ADD_VALUE(verification_values, "Phone", TELEGRAM_AUTH_MODE_PHONE);
|
||||
ADD_VALUE(verification_values, "SMS", TELEGRAM_AUTH_MODE_SMS);
|
||||
|
||||
option = purple_account_option_list_new("Verification type", "verification_type", verification_values);
|
||||
prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
|
||||
|
||||
PurpleAccountOption *opt;
|
||||
|
||||
opt = purple_account_option_bool_new("Compatibility Mode (read SMS code from settings)",
|
||||
"compat-verification", 0);
|
||||
prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, opt);
|
||||
|
||||
opt = purple_account_option_string_new("SMS Code", "code", "");
|
||||
prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, opt);
|
||||
|
||||
_telegram_protocol = plugin;
|
||||
}
|
||||
|
||||
|
|
|
@ -55,5 +55,6 @@ void on_chat_get_info (struct tgl_state *TLS, void *extra, int success, struct t
|
|||
void on_ready (struct tgl_state *TLS);
|
||||
extern const char *pk_path;
|
||||
extern const char *config_dir;
|
||||
extern PurplePlugin *_telegram_protocol;
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Reference in a new issue