From 0d83860a9019d93acd5ea3204e049b647331833b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hermann=20H=C3=B6hne?= Date: Sun, 16 Jun 2024 16:40:19 +0200 Subject: [PATCH] Pairing with character code halfway done. Only text mode has been tried and that has double text. --- src/go/login.go | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/src/go/login.go b/src/go/login.go index e82cb4d..85512e9 100644 --- a/src/go/login.go +++ b/src/go/login.go @@ -165,29 +165,52 @@ func login(account *PurpleAccount, purple_user_dir string, username string, cred err = handler.client.Connect() if err != nil { purple_error(handler.account, fmt.Sprintf("%#v", err), ERROR_TRANSIENT) + return } } +/* + * Generates an pairing 8-character pairing code. + */ +func (handler *Handler) generate_pairing_code() (string, error) { + own_jid, err := parseJID(handler.username) + if err != nil { + return "", fmt.Errorf("ā€ž%sā€œ is not a valid WhatsApp JID.", handler.username) + } + phone := own_jid.ToNonAD().User + showPushNotification := true + // cannot use store.DeviceProps.Os here, since "only common browsers/OSes are allowed", + // see https://pkg.go.dev/go.mau.fi/whatsmeow#Client.PairPhone + // Firefox on Linux chosen arbitrarily + clientDisplayName := "Firefox (Linux)" + clientType := whatsmeow.PairClientFirefox + pairing_code, err := handler.client.PairPhone(phone, showPushNotification, clientType, clientDisplayName) + return pairing_code, err +} + /* * After calling client.Connect() with a pristine device ID, WhatsApp servers * send a list of codes which can be turned into QR codes for scanning with the offical app. */ -func (handler *Handler) handle_qrcode(codes []string) { - code := codes[0] // use only first code for now +func (handler *Handler) handle_qrcode(qrcodes []string) { + var stringBuilder strings.Builder + pairing_code, err := handler.generate_pairing_code() + if err != nil { + purple_error(handler.account, fmt.Sprintf("%#v", err), ERROR_FATAL) + } + qrcode_data := qrcodes[0] // use only first code for now // TODO: emit events to destroy and update the code in the ui - var png []byte - var err error - var b strings.Builder - fmt.Fprintf(&b, "Scan this code to log in:\n%s\n", code) - qrterminal.GenerateHalfBlock(code, qrterminal.L, &b) + fmt.Fprintf(&stringBuilder, "Enter pairing code %s or scan this code to log in:\n%s\n", pairing_code, qrcode_data) + qrterminal.GenerateHalfBlock(qrcode_data, qrterminal.L, &stringBuilder) size := purple_get_int(handler.account, C.GOWHATSAPP_QRCODE_SIZE_OPTION, 256) + var png []byte if size > 0 { - png, err = qrcode.Encode(code, qrcode.Medium, size) + png, err = qrcode.Encode(qrcode_data, qrcode.Medium, size) if err != nil { purple_error(handler.account, fmt.Sprintf("%#v", err), ERROR_FATAL) } } - purple_display_qrcode(handler.account, b.String(), code, png) + purple_display_qrcode(handler.account, stringBuilder.String(), qrcode_data, png) } /*