2020-02-09 19:32:14 +01:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
|
|
|
// Copyright (C) 2020 Tulir Asokan
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2021-03-15 17:06:16 +01:00
|
|
|
|
2020-02-09 19:32:14 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-02-09 16:03:34 +01:00
|
|
|
"bufio"
|
2020-02-09 19:32:14 +01:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-02-05 18:26:09 +01:00
|
|
|
"errors"
|
2020-02-09 19:32:14 +01:00
|
|
|
"fmt"
|
2021-02-09 16:03:34 +01:00
|
|
|
"net"
|
2020-02-09 19:32:14 +01:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2021-02-09 16:03:34 +01:00
|
|
|
"time"
|
2020-02-09 19:32:14 +01:00
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
|
2021-02-05 18:26:09 +01:00
|
|
|
"github.com/Rhymen/go-whatsapp"
|
2020-05-28 19:59:36 +02:00
|
|
|
|
2021-02-17 00:21:30 +01:00
|
|
|
log "maunium.net/go/maulogger/v2"
|
2021-05-12 13:54:40 +02:00
|
|
|
|
2021-02-17 00:21:30 +01:00
|
|
|
"maunium.net/go/mautrix/id"
|
2020-02-09 19:32:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProvisioningAPI struct {
|
|
|
|
bridge *Bridge
|
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (prov *ProvisioningAPI) Init() {
|
|
|
|
prov.log = prov.bridge.Log.Sub("Provisioning")
|
|
|
|
prov.log.Debugln("Enabling provisioning API at", prov.bridge.Config.AppService.Provisioning.Prefix)
|
|
|
|
r := prov.bridge.AS.Router.PathPrefix(prov.bridge.Config.AppService.Provisioning.Prefix).Subrouter()
|
|
|
|
r.Use(prov.AuthMiddleware)
|
|
|
|
r.HandleFunc("/ping", prov.Ping).Methods(http.MethodGet)
|
2021-02-05 18:26:09 +01:00
|
|
|
r.HandleFunc("/login", prov.Login).Methods(http.MethodGet)
|
2020-02-09 19:32:14 +01:00
|
|
|
r.HandleFunc("/logout", prov.Logout).Methods(http.MethodPost)
|
|
|
|
r.HandleFunc("/delete_session", prov.DeleteSession).Methods(http.MethodPost)
|
|
|
|
r.HandleFunc("/delete_connection", prov.DeleteConnection).Methods(http.MethodPost)
|
|
|
|
r.HandleFunc("/disconnect", prov.Disconnect).Methods(http.MethodPost)
|
|
|
|
r.HandleFunc("/reconnect", prov.Reconnect).Methods(http.MethodPost)
|
2021-06-01 14:19:47 +02:00
|
|
|
prov.bridge.AS.Router.HandleFunc("/_matrix/app/com.beeper.asmux/ping", prov.BridgeStatePing).Methods(http.MethodPost)
|
|
|
|
prov.bridge.AS.Router.HandleFunc("/_matrix/app/com.beeper.bridge_state", prov.BridgeStatePing).Methods(http.MethodPost)
|
2020-02-09 19:32:14 +01:00
|
|
|
}
|
|
|
|
|
2021-02-09 16:03:34 +01:00
|
|
|
type responseWrap struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
statusCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ http.Hijacker = (*responseWrap)(nil)
|
|
|
|
|
|
|
|
func (rw *responseWrap) WriteHeader(statusCode int) {
|
|
|
|
rw.ResponseWriter.WriteHeader(statusCode)
|
|
|
|
rw.statusCode = statusCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *responseWrap) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
hijacker, ok := rw.ResponseWriter.(http.Hijacker)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, errors.New("response does not implement http.Hijacker")
|
|
|
|
}
|
|
|
|
return hijacker.Hijack()
|
|
|
|
}
|
|
|
|
|
2020-02-09 19:32:14 +01:00
|
|
|
func (prov *ProvisioningAPI) AuthMiddleware(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
auth := r.Header.Get("Authorization")
|
2020-11-24 15:48:29 +01:00
|
|
|
if len(auth) == 0 && strings.HasSuffix(r.URL.Path, "/login") {
|
|
|
|
authParts := strings.Split(r.Header.Get("Sec-WebSocket-Protocol"), ",")
|
|
|
|
for _, part := range authParts {
|
|
|
|
part = strings.TrimSpace(part)
|
|
|
|
if strings.HasPrefix(part, "net.maunium.whatsapp.auth-") {
|
|
|
|
auth = part[len("net.maunium.whatsapp.auth-"):]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if strings.HasPrefix(auth, "Bearer ") {
|
|
|
|
auth = auth[len("Bearer "):]
|
|
|
|
}
|
2020-02-09 19:32:14 +01:00
|
|
|
if auth != prov.bridge.Config.AppService.Provisioning.SharedSecret {
|
|
|
|
jsonResponse(w, http.StatusForbidden, map[string]interface{}{
|
|
|
|
"error": "Invalid auth token",
|
|
|
|
"errcode": "M_FORBIDDEN",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
userID := r.URL.Query().Get("user_id")
|
2020-05-08 21:32:22 +02:00
|
|
|
user := prov.bridge.GetUserByMXID(id.UserID(userID))
|
2021-02-09 16:03:34 +01:00
|
|
|
start := time.Now()
|
|
|
|
wWrap := &responseWrap{w, 200}
|
|
|
|
h.ServeHTTP(wWrap, r.WithContext(context.WithValue(r.Context(), "user", user)))
|
|
|
|
duration := time.Now().Sub(start).Seconds()
|
|
|
|
prov.log.Infofln("%s %s from %s took %.2f seconds and returned status %d", r.Method, r.URL.Path, user.MXID, duration, wWrap.statusCode)
|
2020-02-09 19:32:14 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type Error struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
ErrCode string `json:"errcode"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (prov *ProvisioningAPI) DeleteSession(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user := r.Context().Value("user").(*User)
|
|
|
|
if user.Session == nil && user.Conn == nil {
|
|
|
|
jsonResponse(w, http.StatusNotFound, Error{
|
|
|
|
Error: "Nothing to purge: no session information stored and no active connection.",
|
|
|
|
ErrCode: "no session",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2021-02-18 22:36:14 +01:00
|
|
|
user.DeleteConnection()
|
2020-02-09 19:32:14 +01:00
|
|
|
user.SetSession(nil)
|
|
|
|
jsonResponse(w, http.StatusOK, Response{true, "Session information purged"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (prov *ProvisioningAPI) DeleteConnection(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user := r.Context().Value("user").(*User)
|
|
|
|
if user.Conn == nil {
|
|
|
|
jsonResponse(w, http.StatusNotFound, Error{
|
|
|
|
Error: "You don't have a WhatsApp connection.",
|
|
|
|
ErrCode: "not connected",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2021-02-18 22:36:14 +01:00
|
|
|
user.DeleteConnection()
|
2020-02-09 19:32:14 +01:00
|
|
|
jsonResponse(w, http.StatusOK, Response{true, "Disconnected from WhatsApp and connection deleted"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (prov *ProvisioningAPI) Disconnect(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user := r.Context().Value("user").(*User)
|
|
|
|
if user.Conn == nil {
|
|
|
|
jsonResponse(w, http.StatusNotFound, Error{
|
|
|
|
Error: "You don't have a WhatsApp connection.",
|
|
|
|
ErrCode: "no connection",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2021-02-18 22:36:14 +01:00
|
|
|
err := user.Conn.Disconnect()
|
2020-02-09 19:32:14 +01:00
|
|
|
if err == whatsapp.ErrNotConnected {
|
|
|
|
jsonResponse(w, http.StatusNotFound, Error{
|
|
|
|
Error: "You were not connected",
|
|
|
|
ErrCode: "not connected",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
user.log.Warnln("Error while disconnecting:", err)
|
|
|
|
jsonResponse(w, http.StatusInternalServerError, Error{
|
|
|
|
Error: fmt.Sprintf("Unknown error while disconnecting: %v", err),
|
|
|
|
ErrCode: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2020-09-27 21:30:08 +02:00
|
|
|
user.bridge.Metrics.TrackConnectionState(user.JID, false)
|
2020-02-09 19:32:14 +01:00
|
|
|
jsonResponse(w, http.StatusOK, Response{true, "Disconnected from WhatsApp"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (prov *ProvisioningAPI) Reconnect(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user := r.Context().Value("user").(*User)
|
|
|
|
if user.Conn == nil {
|
|
|
|
if user.Session == nil {
|
|
|
|
jsonResponse(w, http.StatusForbidden, Error{
|
|
|
|
Error: "No existing connection and no session. Please log in first.",
|
|
|
|
ErrCode: "no session",
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
user.Connect(false)
|
|
|
|
jsonResponse(w, http.StatusOK, Response{true, "Created connection to WhatsApp."})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-03-11 17:21:51 +01:00
|
|
|
|
2021-02-09 16:03:34 +01:00
|
|
|
user.log.Debugln("Received /reconnect request, disconnecting")
|
2020-03-11 17:21:51 +01:00
|
|
|
wasConnected := true
|
2021-02-18 22:36:14 +01:00
|
|
|
err := user.Conn.Disconnect()
|
2020-03-11 17:21:51 +01:00
|
|
|
if err == whatsapp.ErrNotConnected {
|
|
|
|
wasConnected = false
|
|
|
|
} else if err != nil {
|
|
|
|
user.log.Warnln("Error while disconnecting:", err)
|
|
|
|
}
|
|
|
|
|
2021-02-09 16:03:34 +01:00
|
|
|
user.log.Debugln("Restoring session for /reconnect")
|
2021-02-18 22:36:14 +01:00
|
|
|
err = user.Conn.Restore(true, r.Context())
|
2021-02-09 16:03:34 +01:00
|
|
|
user.log.Debugfln("Restore session for /reconnect responded with %v", err)
|
2020-02-09 19:32:14 +01:00
|
|
|
if err == whatsapp.ErrInvalidSession {
|
|
|
|
if user.Session != nil {
|
|
|
|
user.log.Debugln("Got invalid session error when reconnecting, but user has session. Retrying using RestoreWithSession()...")
|
2021-02-18 22:36:14 +01:00
|
|
|
user.Conn.SetSession(*user.Session)
|
|
|
|
err = user.Conn.Restore(true, r.Context())
|
2020-02-09 19:32:14 +01:00
|
|
|
} else {
|
|
|
|
jsonResponse(w, http.StatusForbidden, Error{
|
|
|
|
Error: "You're not logged in",
|
|
|
|
ErrCode: "not logged in",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2021-02-18 22:36:14 +01:00
|
|
|
}
|
|
|
|
if err == whatsapp.ErrLoginInProgress {
|
2020-02-09 19:32:14 +01:00
|
|
|
jsonResponse(w, http.StatusConflict, Error{
|
|
|
|
Error: "A login or reconnection is already in progress.",
|
|
|
|
ErrCode: "login in progress",
|
|
|
|
})
|
|
|
|
return
|
2020-03-11 16:09:31 +01:00
|
|
|
} else if err == whatsapp.ErrAlreadyLoggedIn {
|
|
|
|
jsonResponse(w, http.StatusConflict, Error{
|
|
|
|
Error: "You were already connected.",
|
|
|
|
ErrCode: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
2020-02-09 19:32:14 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
user.log.Warnln("Error while reconnecting:", err)
|
2021-02-18 22:36:14 +01:00
|
|
|
jsonResponse(w, http.StatusInternalServerError, Error{
|
|
|
|
Error: fmt.Sprintf("Unknown error while reconnecting: %v", err),
|
|
|
|
ErrCode: err.Error(),
|
|
|
|
})
|
2020-02-09 19:32:14 +01:00
|
|
|
user.log.Debugln("Disconnecting due to failed session restore in reconnect command...")
|
2021-02-18 22:36:14 +01:00
|
|
|
err = user.Conn.Disconnect()
|
2020-02-09 19:32:14 +01:00
|
|
|
if err != nil {
|
|
|
|
user.log.Errorln("Failed to disconnect after failed session restore in reconnect command:", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user.ConnectionErrors = 0
|
|
|
|
user.PostLogin()
|
2020-03-11 17:21:51 +01:00
|
|
|
|
|
|
|
var msg string
|
|
|
|
if wasConnected {
|
|
|
|
msg = "Reconnected successfully."
|
|
|
|
} else {
|
|
|
|
msg = "Connected successfully."
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonResponse(w, http.StatusOK, Response{true, msg})
|
2020-02-09 19:32:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (prov *ProvisioningAPI) Ping(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user := r.Context().Value("user").(*User)
|
|
|
|
wa := map[string]interface{}{
|
|
|
|
"has_session": user.Session != nil,
|
|
|
|
"management_room": user.ManagementRoom,
|
2020-05-28 19:59:36 +02:00
|
|
|
"jid": user.JID,
|
2020-02-09 19:32:14 +01:00
|
|
|
"conn": nil,
|
|
|
|
"ping": nil,
|
|
|
|
}
|
|
|
|
if user.Conn != nil {
|
|
|
|
wa["conn"] = map[string]interface{}{
|
|
|
|
"is_connected": user.Conn.IsConnected(),
|
|
|
|
"is_logged_in": user.Conn.IsLoggedIn(),
|
|
|
|
"is_login_in_progress": user.Conn.IsLoginInProgress(),
|
|
|
|
}
|
2021-02-07 20:48:42 +01:00
|
|
|
user.log.Debugln("Pinging WhatsApp mobile due to /ping API request")
|
2020-06-23 14:36:08 +02:00
|
|
|
err := user.Conn.AdminTest()
|
2021-01-28 21:46:57 +01:00
|
|
|
var errStr string
|
2021-02-23 23:06:11 +01:00
|
|
|
if err == whatsapp.ErrPingFalse {
|
|
|
|
user.log.Debugln("Forwarding ping false error from provisioning API to HandleError")
|
|
|
|
go user.HandleError(err)
|
|
|
|
}
|
2021-01-28 21:46:57 +01:00
|
|
|
if err != nil {
|
|
|
|
errStr = err.Error()
|
|
|
|
}
|
2020-02-09 19:32:14 +01:00
|
|
|
wa["ping"] = map[string]interface{}{
|
2020-06-23 14:36:08 +02:00
|
|
|
"ok": err == nil,
|
2021-01-28 21:46:57 +01:00
|
|
|
"err": errStr,
|
2020-02-09 19:32:14 +01:00
|
|
|
}
|
2021-02-07 20:48:42 +01:00
|
|
|
user.log.Debugfln("Admin test response for /ping: %v (conn: %t, login: %t, in progress: %t)",
|
2021-02-07 16:35:06 +01:00
|
|
|
err, user.Conn.IsConnected(), user.Conn.IsLoggedIn(), user.Conn.IsLoginInProgress())
|
2020-02-09 19:32:14 +01:00
|
|
|
}
|
|
|
|
resp := map[string]interface{}{
|
|
|
|
"mxid": user.MXID,
|
|
|
|
"admin": user.Admin,
|
|
|
|
"whitelisted": user.Whitelisted,
|
|
|
|
"relaybot_whitelisted": user.RelaybotWhitelisted,
|
|
|
|
"whatsapp": wa,
|
|
|
|
}
|
|
|
|
jsonResponse(w, http.StatusOK, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonResponse(w http.ResponseWriter, status int, response interface{}) {
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(status)
|
|
|
|
_ = json.NewEncoder(w).Encode(response)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (prov *ProvisioningAPI) Logout(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user := r.Context().Value("user").(*User)
|
|
|
|
if user.Session == nil {
|
|
|
|
jsonResponse(w, http.StatusNotFound, Error{
|
|
|
|
Error: "You're not logged in",
|
|
|
|
ErrCode: "not logged in",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-19 19:08:27 +01:00
|
|
|
force := strings.ToLower(r.URL.Query().Get("force")) != "false"
|
2020-11-19 18:18:34 +01:00
|
|
|
|
|
|
|
if user.Conn == nil {
|
|
|
|
if !force {
|
|
|
|
jsonResponse(w, http.StatusNotFound, Error{
|
|
|
|
Error: "You're not connected",
|
|
|
|
ErrCode: "not connected",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err := user.Conn.Logout()
|
|
|
|
if err != nil {
|
|
|
|
user.log.Warnln("Error while logging out:", err)
|
|
|
|
if !force {
|
|
|
|
jsonResponse(w, http.StatusInternalServerError, Error{
|
|
|
|
Error: fmt.Sprintf("Unknown error while logging out: %v", err),
|
|
|
|
ErrCode: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-02-18 22:36:14 +01:00
|
|
|
user.DeleteConnection()
|
2020-02-09 19:32:14 +01:00
|
|
|
}
|
2020-11-19 18:18:34 +01:00
|
|
|
|
|
|
|
user.bridge.Metrics.TrackConnectionState(user.JID, false)
|
2021-08-04 15:14:26 +02:00
|
|
|
user.removeFromJIDMap(StateLoggedOut)
|
2020-11-19 18:18:34 +01:00
|
|
|
|
2020-05-21 18:49:01 +02:00
|
|
|
// TODO this causes a foreign key violation, which should be fixed
|
|
|
|
//ce.User.JID = ""
|
2020-02-09 19:32:14 +01:00
|
|
|
user.SetSession(nil)
|
|
|
|
jsonResponse(w, http.StatusOK, Response{true, "Logged out successfully."})
|
|
|
|
}
|
|
|
|
|
2020-11-24 15:48:29 +01:00
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
Subprotocols: []string{"net.maunium.whatsapp.login"},
|
|
|
|
}
|
2020-02-09 19:32:14 +01:00
|
|
|
|
|
|
|
func (prov *ProvisioningAPI) Login(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userID := r.URL.Query().Get("user_id")
|
2020-05-08 21:32:22 +02:00
|
|
|
user := prov.bridge.GetUserByMXID(id.UserID(userID))
|
2020-02-09 19:32:14 +01:00
|
|
|
|
|
|
|
c, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
2021-02-09 16:03:34 +01:00
|
|
|
prov.log.Errorln("Failed to upgrade connection to websocket:", err)
|
2020-02-09 19:32:14 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
if !user.Connect(true) {
|
|
|
|
user.log.Debugln("Connect() returned false, assuming error was logged elsewhere and canceling login.")
|
|
|
|
_ = c.WriteJSON(Error{
|
|
|
|
Error: "Failed to connect to WhatsApp",
|
|
|
|
ErrCode: "connection error",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
qrChan := make(chan string, 3)
|
|
|
|
go func() {
|
|
|
|
for code := range qrChan {
|
|
|
|
if code == "stop" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = c.WriteJSON(map[string]interface{}{
|
|
|
|
"code": code,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}()
|
2021-02-07 21:14:13 +01:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Read everything so SetCloseHandler() works
|
|
|
|
for {
|
|
|
|
_, _, err = c.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
c.SetCloseHandler(func(code int, text string) error {
|
|
|
|
user.log.Debugfln("Login websocket closed (%d), cancelling login", code)
|
|
|
|
cancel()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2021-02-05 18:26:09 +01:00
|
|
|
user.log.Debugln("Starting login via provisioning API")
|
2021-05-12 13:54:40 +02:00
|
|
|
session, jid, err := user.Conn.Login(qrChan, ctx)
|
2020-02-09 19:32:14 +01:00
|
|
|
qrChan <- "stop"
|
|
|
|
if err != nil {
|
|
|
|
var msg string
|
2021-02-07 15:54:09 +01:00
|
|
|
if errors.Is(err, whatsapp.ErrAlreadyLoggedIn) {
|
2020-02-09 19:32:14 +01:00
|
|
|
msg = "You're already logged in"
|
2021-02-07 15:54:09 +01:00
|
|
|
} else if errors.Is(err, whatsapp.ErrLoginInProgress) {
|
2020-02-09 19:32:14 +01:00
|
|
|
msg = "You have a login in progress already."
|
2021-02-07 15:54:09 +01:00
|
|
|
} else if errors.Is(err, whatsapp.ErrLoginTimedOut) {
|
2020-02-09 19:32:14 +01:00
|
|
|
msg = "QR code scan timed out. Please try again."
|
2021-02-07 15:54:09 +01:00
|
|
|
} else if errors.Is(err, whatsapp.ErrInvalidWebsocket) {
|
2021-02-05 18:26:09 +01:00
|
|
|
msg = "WhatsApp connection error. Please try again."
|
2021-02-18 22:36:14 +01:00
|
|
|
// TODO might need to make sure it reconnects?
|
2021-09-23 20:08:15 +02:00
|
|
|
} else if errors.Is(err, whatsapp.ErrMultiDeviceNotSupported) {
|
|
|
|
msg = "WhatsApp multi-device is not currently supported. Please disable it and try again."
|
2020-02-09 19:32:14 +01:00
|
|
|
} else {
|
|
|
|
msg = fmt.Sprintf("Unknown error while logging in: %v", err)
|
|
|
|
}
|
2021-02-05 18:26:09 +01:00
|
|
|
user.log.Warnln("Failed to log in:", err)
|
2020-02-09 19:32:14 +01:00
|
|
|
_ = c.WriteJSON(Error{
|
|
|
|
Error: msg,
|
|
|
|
ErrCode: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2021-02-18 22:36:14 +01:00
|
|
|
user.log.Debugln("Successful login as", jid, "via provisioning API")
|
2020-02-09 19:32:14 +01:00
|
|
|
user.ConnectionErrors = 0
|
2021-02-18 22:36:14 +01:00
|
|
|
user.JID = strings.Replace(jid, whatsapp.OldUserSuffix, whatsapp.NewUserSuffix, 1)
|
2020-05-21 18:49:01 +02:00
|
|
|
user.addToJIDMap()
|
2020-02-09 19:32:14 +01:00
|
|
|
user.SetSession(&session)
|
|
|
|
_ = c.WriteJSON(map[string]interface{}{
|
|
|
|
"success": true,
|
|
|
|
"jid": user.JID,
|
|
|
|
})
|
|
|
|
user.PostLogin()
|
|
|
|
}
|