Skip to content

Commit

Permalink
Add support for logging in by entering code on phone
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jul 18, 2023
1 parent dc00b55 commit a7d9251
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
32 changes: 29 additions & 3 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"html"
"math"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -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() {
Expand All @@ -444,21 +448,41 @@ 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 {
case whatsmeow.QRChannelSuccess.Event:
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:
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit a7d9251

Please sign in to comment.