mirror of
https://github.com/tulir/mautrix-whatsapp
synced 2024-06-14 08:58:22 +02:00
Add support for logging in by entering code on phone
This commit is contained in:
parent
dc00b55dff
commit
a7d9251abd
32
commands.go
32
commands.go
|
@ -23,6 +23,7 @@ import (
|
|||
"fmt"
|
||||
"html"
|
||||
"math"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -431,9 +432,12 @@ var cmdLogin = &commands.FullHandler{
|
|||
Help: commands.HelpMeta{
|
||||
Section: commands.HelpSectionAuth,
|
||||
Description: "Link the bridge to your WhatsApp account as a web client.",
|
||||
Args: "[_phone number_]",
|
||||
},
|
||||
}
|
||||
|
||||
var looksLikeAPhoneRegex = regexp.MustCompile(`^\+[0-9]+$`)
|
||||
|
||||
func fnLogin(ce *WrappedCommandEvent) {
|
||||
if ce.User.Session != nil {
|
||||
if ce.User.IsConnected() {
|
||||
|
@ -444,13 +448,33 @@ func fnLogin(ce *WrappedCommandEvent) {
|
|||
return
|
||||
}
|
||||
|
||||
var phoneNumber string
|
||||
if len(ce.Args) > 0 {
|
||||
phoneNumber = strings.TrimSpace(strings.Join(ce.Args, " "))
|
||||
if !looksLikeAPhoneRegex.MatchString(phoneNumber) {
|
||||
ce.Reply("When specifying a phone number, it must be provided in international format without spaces or other extra characters")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
qrChan, err := ce.User.Login(context.Background())
|
||||
if err != nil {
|
||||
ce.User.log.Errorf("Failed to log in:", err)
|
||||
ce.ZLog.Err(err).Msg("Failed to start login")
|
||||
ce.Reply("Failed to log in: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if phoneNumber != "" {
|
||||
pairingCode, err := ce.User.Client.PairPhone(phoneNumber, true)
|
||||
if err != nil {
|
||||
ce.ZLog.Err(err).Msg("Failed to start phone code login")
|
||||
ce.Reply("Failed to start phone code login: %v", err)
|
||||
go ce.User.DeleteConnection()
|
||||
return
|
||||
}
|
||||
ce.Reply("Scan the code below or enter the following code on your phone to log in: **%s**", pairingCode)
|
||||
}
|
||||
|
||||
var qrEventID id.EventID
|
||||
for item := range qrChan {
|
||||
switch item.Event {
|
||||
|
@ -458,7 +482,7 @@ func fnLogin(ce *WrappedCommandEvent) {
|
|||
jid := ce.User.Client.Store.ID
|
||||
ce.Reply("Successfully logged in as +%s (device #%d)", jid.User, jid.Device)
|
||||
case whatsmeow.QRChannelTimeout.Event:
|
||||
ce.Reply("QR code timed out. Please restart the login.")
|
||||
ce.Reply("Login timed out. Please restart the login.")
|
||||
case whatsmeow.QRChannelErrUnexpectedEvent.Event:
|
||||
ce.Reply("Failed to log in: unexpected connection event from server")
|
||||
case whatsmeow.QRChannelClientOutdated.Event:
|
||||
|
@ -471,7 +495,9 @@ func fnLogin(ce *WrappedCommandEvent) {
|
|||
qrEventID = ce.User.sendQR(ce, item.Code, qrEventID)
|
||||
}
|
||||
}
|
||||
_, _ = ce.Bot.RedactEvent(ce.RoomID, qrEventID)
|
||||
if qrEventID != "" {
|
||||
_, _ = ce.Bot.RedactEvent(ce.RoomID, qrEventID)
|
||||
}
|
||||
}
|
||||
|
||||
func (user *User) sendQR(ce *WrappedCommandEvent, code string, prevEvent id.EventID) id.EventID {
|
||||
|
|
2
go.mod
2
go.mod
|
@ -12,7 +12,7 @@ require (
|
|||
github.com/rs/zerolog v1.29.1
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
||||
github.com/tidwall/gjson v1.14.4
|
||||
go.mau.fi/whatsmeow v0.0.0-20230716092751-4bd753344e7b
|
||||
go.mau.fi/whatsmeow v0.0.0-20230718190209-efef6f1cec8e
|
||||
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1
|
||||
golang.org/x/image v0.9.0
|
||||
golang.org/x/net v0.12.0
|
||||
|
|
4
go.sum
4
go.sum
|
@ -68,8 +68,8 @@ github.com/yuin/goldmark v1.5.4 h1:2uY/xC0roWy8IBEGLgB1ywIoEJFGmRrX21YQcvGZzjU=
|
|||
github.com/yuin/goldmark v1.5.4/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.mau.fi/libsignal v0.1.0 h1:vAKI/nJ5tMhdzke4cTK1fb0idJzz1JuEIpmjprueC+c=
|
||||
go.mau.fi/libsignal v0.1.0/go.mod h1:R8ovrTezxtUNzCQE5PH30StOQWWeBskBsWE55vMfY9I=
|
||||
go.mau.fi/whatsmeow v0.0.0-20230716092751-4bd753344e7b h1:L6nTucSoWAY5le8liGQh+ZOov0NvLjy4nCnYK10lwvE=
|
||||
go.mau.fi/whatsmeow v0.0.0-20230716092751-4bd753344e7b/go.mod h1:+ObGpFE6cbbY4hKc1FmQH9MVfqaemmlXGXSnwDvCOyE=
|
||||
go.mau.fi/whatsmeow v0.0.0-20230718190209-efef6f1cec8e h1:i2atPgn2MRLGxisk+EZM1b1RfPh+4dZxSc8OdyvzutY=
|
||||
go.mau.fi/whatsmeow v0.0.0-20230718190209-efef6f1cec8e/go.mod h1:+ObGpFE6cbbY4hKc1FmQH9MVfqaemmlXGXSnwDvCOyE=
|
||||
go.mau.fi/zeroconfig v0.1.2 h1:DKOydWnhPMn65GbXZOafgkPm11BvFashZWLct0dGFto=
|
||||
go.mau.fi/zeroconfig v0.1.2/go.mod h1:NcSJkf180JT+1IId76PcMuLTNa1CzsFFZ0nBygIQM70=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
|
|
Loading…
Reference in a new issue