Skip to content

Commit

Permalink
Automatically choose correct way to send QR code
Browse files Browse the repository at this point in the history
* use request_fields API when UI implemented it
* use embedded image if image is available in imgstore + plain-text
  qrcode
* use plain-text qrcode in other cases
  • Loading branch information
vitalyster authored and hoehermann committed Jan 24, 2022
1 parent e94d512 commit 6880aaa
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/c/gowhatsapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void gowhatsapp_login(PurpleAccount *account);
void gowhatsapp_close(PurpleConnection *pc);

// qrcode
void gowhatsapp_handle_qrcode(PurpleAccount *account, const char *challenge, const char *terminal, void *image_data, size_t image_data_len);
void gowhatsapp_handle_qrcode(PurpleConnection *pc, const char *challenge, const char *terminal, void *image_data, size_t image_data_len);
void gowhatsapp_close_qrcode(PurpleAccount *account);

// process_message
Expand Down
7 changes: 0 additions & 7 deletions src/c/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ gowhatsapp_add_account_options(GList *account_options)
);
account_options = g_list_append(account_options, option);

option = purple_account_option_bool_new(
"Plain-text login",
GOWHATSAPP_PLAIN_TEXT_LOGIN_OPTION,
FALSE
);
account_options = g_list_append(account_options, option);

option = purple_account_option_bool_new(
"Display offline contacts as away",
GOWHATSAPP_FAKE_ONLINE_OPTION,
Expand Down
24 changes: 18 additions & 6 deletions src/c/qrcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,25 @@ gowhatsapp_display_qrcode(PurpleAccount *account, const char * challenge, void *
}

void
gowhatsapp_handle_qrcode(PurpleAccount *account, const char *challenge, const char *terminal, void *image_data, size_t image_data_len)
gowhatsapp_handle_qrcode(PurpleConnection *pc, const char *challenge, const char *terminal, void *image_data, size_t image_data_len)
{
if (image_data_len > 0) {
PurpleMessageFlags flags = PURPLE_MESSAGE_RECV;
PurpleRequestUiOps *ui_ops = purple_request_get_ui_ops();
if (!ui_ops->request_fields || image_data_len <= 0) {
// The UI hasn't implemented the func we want, just output as a message instead
gchar *msg_out;
gpointer img_data = g_memdup(image_data, image_data_len);
int img_id = purple_imgstore_add_with_id(img_data, image_data_len, NULL);
if (img_id >= 0) {
msg_out = g_strdup_printf("%s: <img id=\"%u\" alt=\"%s\"/><br />%s", "Please scan this QR code with your phone", img_id, challenge, terminal);
flags |= PURPLE_MESSAGE_IMAGES;
} else {
msg_out = g_strdup_printf("%s: %s<br />%s", "Please scan this QR code with your phone", challenge, terminal);
}
purple_serv_got_im(pc, "Logon QR Code", msg_out, flags, time(NULL));
g_free(msg_out);
} else {
PurpleAccount *account = purple_connection_get_account(pc);
gowhatsapp_display_qrcode(account, challenge, image_data, image_data_len);
}
if (purple_account_get_bool(account, GOWHATSAPP_PLAIN_TEXT_LOGIN_OPTION, FALSE)) {
PurpleConnection *pc = purple_account_get_connection(account);
purple_serv_got_im(pc, "login@whatsmeow", terminal, PURPLE_MESSAGE_RECV, time(NULL));
}
}
1 change: 0 additions & 1 deletion src/go/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ CHARCONSTANT(GOWHATSAPP_QRCODE_SIZE_OPTION, "qrcode-size");
CHARCONSTANT(GOWHATSAPP_SEND_RECEIPT_OPTION, "send-receipt");
CHARCONSTANT(GOWHATSAPP_GET_ICONS_OPTION, "get-icons");
CHARCONSTANT(GOWHATSAPP_SPECTRUM_COMPATIBILITY_OPTION, "spectrum-compatibility");
CHARCONSTANT(GOWHATSAPP_PLAIN_TEXT_LOGIN_OPTION, "plain-text-login");
CHARCONSTANT(GOWHATSAPP_DATABASE_ADDRESS_OPTION, "database-address");

CHARCONSTANT(GOWHATSAPP_DATABASE_ADDRESS_DEFAULT, "file:$purple_user_dir/whatsmeow.db?_foreign_keys=on&_busy_timeout=3000");
Expand Down
2 changes: 1 addition & 1 deletion src/go/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (handler *Handler) connect() {
var png []byte
var b strings.Builder
fmt.Fprintf(&b, "Scan this code to log in:\n%s\n", evt.Code)
qrterminal.Generate(evt.Code, qrterminal.L, &b)
qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, &b)
size := purple_get_int(handler.account, C.GOWHATSAPP_QRCODE_SIZE_OPTION, 256)
if size > 0 {
png, err = qrcode.Encode(evt.Code, qrcode.Medium, size)
Expand Down

0 comments on commit 6880aaa

Please sign in to comment.