2018-08-18 21:57:08 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2020-05-08 21:32:22 +02:00
|
|
|
// Copyright (C) 2020 Tulir Asokan
|
2018-08-18 21:57:08 +02:00
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-02-18 22:36:14 +01:00
|
|
|
"context"
|
2020-10-24 15:51:23 +02:00
|
|
|
"errors"
|
2019-02-20 13:39:44 +01:00
|
|
|
"fmt"
|
2020-05-23 22:17:43 +02:00
|
|
|
"math"
|
|
|
|
"sort"
|
2020-05-09 01:03:59 +02:00
|
|
|
"strconv"
|
2019-05-24 01:33:26 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Rhymen/go-whatsapp"
|
2019-01-11 20:17:31 +01:00
|
|
|
|
|
|
|
"maunium.net/go/maulogger/v2"
|
2019-11-10 20:22:11 +01:00
|
|
|
|
|
|
|
"maunium.net/go/mautrix"
|
2020-05-09 13:31:06 +02:00
|
|
|
"maunium.net/go/mautrix/appservice"
|
2020-05-08 21:32:22 +02:00
|
|
|
"maunium.net/go/mautrix/event"
|
2019-11-10 20:22:11 +01:00
|
|
|
"maunium.net/go/mautrix/format"
|
2020-05-08 21:32:22 +02:00
|
|
|
"maunium.net/go/mautrix/id"
|
2019-01-11 20:17:31 +01:00
|
|
|
|
2018-12-07 14:42:57 +01:00
|
|
|
"maunium.net/go/mautrix-whatsapp/database"
|
2018-08-18 21:57:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type CommandHandler struct {
|
|
|
|
bridge *Bridge
|
|
|
|
log maulogger.Logger
|
|
|
|
}
|
|
|
|
|
2018-10-16 19:16:13 +02:00
|
|
|
// NewCommandHandler creates a CommandHandler
|
2018-08-18 21:57:08 +02:00
|
|
|
func NewCommandHandler(bridge *Bridge) *CommandHandler {
|
|
|
|
return &CommandHandler{
|
|
|
|
bridge: bridge,
|
|
|
|
log: bridge.Log.Sub("Command handler"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 19:16:13 +02:00
|
|
|
// CommandEvent stores all data which might be used to handle commands
|
2018-08-18 21:57:08 +02:00
|
|
|
type CommandEvent struct {
|
|
|
|
Bot *appservice.IntentAPI
|
|
|
|
Bridge *Bridge
|
2020-06-25 21:42:52 +02:00
|
|
|
Portal *Portal
|
2018-08-18 21:57:08 +02:00
|
|
|
Handler *CommandHandler
|
2020-05-08 21:32:22 +02:00
|
|
|
RoomID id.RoomID
|
2018-08-18 21:57:08 +02:00
|
|
|
User *User
|
2019-11-10 20:22:11 +01:00
|
|
|
Command string
|
2018-08-18 21:57:08 +02:00
|
|
|
Args []string
|
|
|
|
}
|
|
|
|
|
2018-10-16 19:16:13 +02:00
|
|
|
// Reply sends a reply to command as notice
|
2019-05-15 22:04:09 +02:00
|
|
|
func (ce *CommandEvent) Reply(msg string, args ...interface{}) {
|
2020-05-08 21:32:22 +02:00
|
|
|
content := format.RenderMarkdown(fmt.Sprintf(msg, args...), true, false)
|
|
|
|
content.MsgType = event.MsgNotice
|
2020-06-25 21:42:52 +02:00
|
|
|
intent := ce.Bot
|
|
|
|
if ce.Portal != nil && ce.Portal.IsPrivateChat() {
|
|
|
|
intent = ce.Portal.MainIntent()
|
|
|
|
}
|
|
|
|
_, err := intent.SendMessageEvent(ce.RoomID, event.EventMessage, content)
|
2018-08-18 21:57:08 +02:00
|
|
|
if err != nil {
|
2018-08-28 23:40:54 +02:00
|
|
|
ce.Handler.log.Warnfln("Failed to reply to command from %s: %v", ce.User.MXID, err)
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 19:16:13 +02:00
|
|
|
// Handle handles messages to the bridge
|
2020-05-08 21:32:22 +02:00
|
|
|
func (handler *CommandHandler) Handle(roomID id.RoomID, user *User, message string) {
|
2020-05-24 18:03:57 +02:00
|
|
|
args := strings.Fields(message)
|
2020-12-27 23:21:17 +01:00
|
|
|
if len(args) == 0 {
|
|
|
|
args = []string{"unknown-command"}
|
|
|
|
}
|
2018-08-18 21:57:08 +02:00
|
|
|
ce := &CommandEvent{
|
2018-08-30 00:10:26 +02:00
|
|
|
Bot: handler.bridge.Bot,
|
2018-08-18 21:57:08 +02:00
|
|
|
Bridge: handler.bridge,
|
2020-06-25 21:42:52 +02:00
|
|
|
Portal: handler.bridge.GetPortalByMXID(roomID),
|
2018-08-18 21:57:08 +02:00
|
|
|
Handler: handler,
|
|
|
|
RoomID: roomID,
|
|
|
|
User: user,
|
2019-11-10 20:22:11 +01:00
|
|
|
Command: strings.ToLower(args[0]),
|
2018-08-18 21:57:08 +02:00
|
|
|
Args: args[1:],
|
|
|
|
}
|
2019-05-23 19:09:13 +02:00
|
|
|
handler.log.Debugfln("%s sent '%s' in %s", user.MXID, message, roomID)
|
2019-11-10 20:22:11 +01:00
|
|
|
if roomID == handler.bridge.Config.Bridge.Relaybot.ManagementRoom {
|
|
|
|
handler.CommandRelaybot(ce)
|
|
|
|
} else {
|
|
|
|
handler.CommandMux(ce)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *CommandHandler) CommandMux(ce *CommandEvent) {
|
|
|
|
switch ce.Command {
|
|
|
|
case "relaybot":
|
|
|
|
handler.CommandRelaybot(ce)
|
2018-08-18 21:57:08 +02:00
|
|
|
case "login":
|
|
|
|
handler.CommandLogin(ce)
|
2019-05-24 01:33:26 +02:00
|
|
|
case "logout-matrix":
|
|
|
|
handler.CommandLogoutMatrix(ce)
|
2018-08-18 21:57:08 +02:00
|
|
|
case "help":
|
|
|
|
handler.CommandHelp(ce)
|
2020-06-03 19:32:53 +02:00
|
|
|
case "version":
|
|
|
|
handler.CommandVersion(ce)
|
2020-06-15 18:00:29 +02:00
|
|
|
case "reconnect", "connect":
|
2019-05-15 23:18:43 +02:00
|
|
|
handler.CommandReconnect(ce)
|
2019-05-23 19:09:13 +02:00
|
|
|
case "disconnect":
|
|
|
|
handler.CommandDisconnect(ce)
|
2019-08-24 21:39:12 +02:00
|
|
|
case "ping":
|
|
|
|
handler.CommandPing(ce)
|
2019-05-23 19:09:13 +02:00
|
|
|
case "delete-connection":
|
|
|
|
handler.CommandDeleteConnection(ce)
|
2019-05-16 17:00:46 +02:00
|
|
|
case "delete-session":
|
|
|
|
handler.CommandDeleteSession(ce)
|
2019-05-21 20:06:27 +02:00
|
|
|
case "delete-portal":
|
|
|
|
handler.CommandDeletePortal(ce)
|
2019-05-31 18:33:18 +02:00
|
|
|
case "delete-all-portals":
|
|
|
|
handler.CommandDeleteAllPortals(ce)
|
2020-10-05 21:32:15 +02:00
|
|
|
case "discard-megolm-session", "discard-session":
|
|
|
|
handler.CommandDiscardMegolmSession(ce)
|
2019-08-24 21:39:12 +02:00
|
|
|
case "dev-test":
|
|
|
|
handler.CommandDevTest(ce)
|
2020-05-09 01:03:59 +02:00
|
|
|
case "set-pl":
|
|
|
|
handler.CommandSetPowerLevel(ce)
|
2020-05-21 19:50:54 +02:00
|
|
|
case "logout":
|
|
|
|
handler.CommandLogout(ce)
|
2020-07-10 15:26:55 +02:00
|
|
|
case "toggle":
|
|
|
|
handler.CommandToggle(ce)
|
2020-07-10 14:23:32 +02:00
|
|
|
case "login-matrix", "sync", "list", "open", "pm", "invite-link", "join", "create":
|
2019-08-24 23:25:29 +02:00
|
|
|
if !ce.User.HasSession() {
|
2019-05-15 23:18:43 +02:00
|
|
|
ce.Reply("You are not logged in. Use the `login` command to log into WhatsApp.")
|
|
|
|
return
|
2019-08-24 21:39:12 +02:00
|
|
|
} else if !ce.User.IsConnected() {
|
2019-05-15 23:18:43 +02:00
|
|
|
ce.Reply("You are not connected to WhatsApp. Use the `reconnect` command to reconnect.")
|
2019-03-15 15:45:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-10 20:22:11 +01:00
|
|
|
switch ce.Command {
|
2019-05-24 01:33:26 +02:00
|
|
|
case "login-matrix":
|
|
|
|
handler.CommandLoginMatrix(ce)
|
2019-03-15 15:45:27 +01:00
|
|
|
case "sync":
|
|
|
|
handler.CommandSync(ce)
|
|
|
|
case "list":
|
|
|
|
handler.CommandList(ce)
|
|
|
|
case "open":
|
|
|
|
handler.CommandOpen(ce)
|
|
|
|
case "pm":
|
|
|
|
handler.CommandPM(ce)
|
2020-06-25 21:40:34 +02:00
|
|
|
case "invite-link":
|
|
|
|
handler.CommandInviteLink(ce)
|
2020-06-25 22:29:16 +02:00
|
|
|
case "join":
|
|
|
|
handler.CommandJoin(ce)
|
2020-07-10 14:23:32 +02:00
|
|
|
case "create":
|
|
|
|
handler.CommandCreate(ce)
|
2019-03-15 15:45:27 +01:00
|
|
|
}
|
2018-10-16 19:15:38 +02:00
|
|
|
default:
|
2020-12-27 23:21:17 +01:00
|
|
|
ce.Reply("Unknown command, use the `help` command for help.")
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-05 21:32:15 +02:00
|
|
|
|
|
|
|
func (handler *CommandHandler) CommandDiscardMegolmSession(ce *CommandEvent) {
|
|
|
|
if handler.bridge.Crypto == nil {
|
|
|
|
ce.Reply("This bridge instance doesn't have end-to-bridge encryption enabled")
|
|
|
|
} else if !ce.User.Admin {
|
|
|
|
ce.Reply("Only the bridge admin can reset Megolm sessions")
|
|
|
|
} else {
|
|
|
|
handler.bridge.Crypto.ResetSession(ce.RoomID)
|
|
|
|
ce.Reply("Successfully reset Megolm session in this room. New decryption keys will be shared the next time a message is sent from WhatsApp.")
|
|
|
|
}
|
|
|
|
}
|
2018-08-18 21:57:08 +02:00
|
|
|
|
2019-11-10 20:22:11 +01:00
|
|
|
func (handler *CommandHandler) CommandRelaybot(ce *CommandEvent) {
|
|
|
|
if handler.bridge.Relaybot == nil {
|
|
|
|
ce.Reply("The relaybot is disabled")
|
|
|
|
} else if !ce.User.Admin {
|
|
|
|
ce.Reply("Only admins can manage the relaybot")
|
|
|
|
} else {
|
|
|
|
if ce.Command == "relaybot" {
|
2019-11-11 21:30:59 +01:00
|
|
|
if len(ce.Args) == 0 {
|
|
|
|
ce.Reply("**Usage:** `relaybot <command>`")
|
|
|
|
return
|
|
|
|
}
|
2019-11-10 20:22:11 +01:00
|
|
|
ce.Command = strings.ToLower(ce.Args[0])
|
|
|
|
ce.Args = ce.Args[1:]
|
|
|
|
}
|
|
|
|
ce.User = handler.bridge.Relaybot
|
|
|
|
handler.CommandMux(ce)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 22:25:22 +02:00
|
|
|
func (handler *CommandHandler) CommandDevTest(_ *CommandEvent) {
|
2019-08-24 21:39:12 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-03 19:32:53 +02:00
|
|
|
const cmdVersionHelp = `version - View the bridge version`
|
|
|
|
|
|
|
|
func (handler *CommandHandler) CommandVersion(ce *CommandEvent) {
|
2021-04-29 13:00:26 +02:00
|
|
|
linkifiedVersion := fmt.Sprintf("v%s", Version)
|
2020-06-03 19:32:53 +02:00
|
|
|
if Tag == Version {
|
2021-04-29 13:00:26 +02:00
|
|
|
linkifiedVersion = fmt.Sprintf("[v%s](%s/releases/v%s)", Version, URL, Tag)
|
2020-06-03 19:32:53 +02:00
|
|
|
} else if len(Commit) > 8 {
|
2021-04-29 13:00:26 +02:00
|
|
|
linkifiedVersion = strings.Replace(linkifiedVersion, Commit[:8], fmt.Sprintf("[%s](%s/commit/%s)", Commit[:8], URL, Commit), 1)
|
2020-06-03 19:32:53 +02:00
|
|
|
}
|
2021-04-29 13:00:26 +02:00
|
|
|
ce.Reply(fmt.Sprintf("[%s](%s) %s (%s)", Name, URL, linkifiedVersion, BuildTime))
|
2020-06-03 19:32:53 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 21:40:34 +02:00
|
|
|
const cmdInviteLinkHelp = `invite-link - Get an invite link to the current group chat.`
|
|
|
|
|
|
|
|
func (handler *CommandHandler) CommandInviteLink(ce *CommandEvent) {
|
2020-06-25 21:42:52 +02:00
|
|
|
if ce.Portal == nil {
|
2020-06-25 21:40:34 +02:00
|
|
|
ce.Reply("Not a portal room")
|
|
|
|
return
|
2020-06-25 21:42:52 +02:00
|
|
|
} else if ce.Portal.IsPrivateChat() {
|
2020-06-25 21:40:34 +02:00
|
|
|
ce.Reply("Can't get invite link to private chat")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 21:42:52 +02:00
|
|
|
link, err := ce.User.Conn.GroupInviteLink(ce.Portal.Key.JID)
|
2020-06-25 21:40:34 +02:00
|
|
|
if err != nil {
|
|
|
|
ce.Reply("Failed to get invite link: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2020-06-25 22:29:16 +02:00
|
|
|
ce.Reply("%s%s", inviteLinkPrefix, link)
|
|
|
|
}
|
|
|
|
|
|
|
|
const cmdJoinHelp = `join <invite link> - Join a group chat with an invite link.`
|
|
|
|
const inviteLinkPrefix = "https://chat.whatsapp.com/"
|
|
|
|
|
|
|
|
func (handler *CommandHandler) CommandJoin(ce *CommandEvent) {
|
|
|
|
if len(ce.Args) == 0 {
|
|
|
|
ce.Reply("**Usage:** `join <invite link>`")
|
|
|
|
return
|
|
|
|
} else if len(ce.Args[0]) <= len(inviteLinkPrefix) || ce.Args[0][:len(inviteLinkPrefix)] != inviteLinkPrefix {
|
|
|
|
ce.Reply("That doesn't look like a WhatsApp invite link")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jid, err := ce.User.Conn.GroupAcceptInviteCode(ce.Args[0][len(inviteLinkPrefix):])
|
|
|
|
if err != nil {
|
|
|
|
ce.Reply("Failed to join group: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.log.Debugln("%s successfully joined group %s", ce.User.MXID, jid)
|
|
|
|
portal := handler.bridge.GetPortalByJID(database.GroupPortalKey(jid))
|
|
|
|
if len(portal.MXID) > 0 {
|
2021-02-18 22:36:14 +01:00
|
|
|
portal.Sync(ce.User, whatsapp.Contact{JID: portal.Key.JID})
|
2020-06-25 22:58:35 +02:00
|
|
|
ce.Reply("Successfully joined group \"%s\" and synced portal room: [%s](https://matrix.to/#/%s)", portal.Name, portal.Name, portal.MXID)
|
2020-06-25 22:29:16 +02:00
|
|
|
} else {
|
|
|
|
err = portal.CreateMatrixRoom(ce.User)
|
|
|
|
if err != nil {
|
|
|
|
ce.Reply("Failed to create portal room: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 22:58:35 +02:00
|
|
|
ce.Reply("Successfully joined group \"%s\" and created portal room: [%s](https://matrix.to/#/%s)", portal.Name, portal.Name, portal.MXID)
|
2020-06-25 22:29:16 +02:00
|
|
|
}
|
2020-06-25 21:40:34 +02:00
|
|
|
}
|
|
|
|
|
2020-07-10 14:23:32 +02:00
|
|
|
const cmdCreateHelp = `create - Create a group chat.`
|
|
|
|
|
|
|
|
func (handler *CommandHandler) CommandCreate(ce *CommandEvent) {
|
|
|
|
if ce.Portal != nil {
|
|
|
|
ce.Reply("This is already a portal room")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
members, err := ce.Bot.JoinedMembers(ce.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
ce.Reply("Failed to get room members: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var roomNameEvent event.RoomNameEventContent
|
|
|
|
err = ce.Bot.StateEvent(ce.RoomID, event.StateRoomName, "", &roomNameEvent)
|
2020-10-24 15:51:23 +02:00
|
|
|
if err != nil && !errors.Is(err, mautrix.MNotFound) {
|
2020-07-10 14:23:32 +02:00
|
|
|
ce.Reply("Failed to get room name")
|
|
|
|
return
|
|
|
|
} else if len(roomNameEvent.Name) == 0 {
|
|
|
|
ce.Reply("Please set a name for the room first")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var encryptionEvent event.EncryptionEventContent
|
|
|
|
err = ce.Bot.StateEvent(ce.RoomID, event.StateEncryption, "", &encryptionEvent)
|
2020-10-24 15:51:23 +02:00
|
|
|
if err != nil && !errors.Is(err, mautrix.MNotFound) {
|
2020-07-10 14:23:32 +02:00
|
|
|
ce.Reply("Failed to get room encryption status")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
participants := []string{ce.User.JID}
|
|
|
|
for userID := range members.Joined {
|
|
|
|
jid, ok := handler.bridge.ParsePuppetMXID(userID)
|
|
|
|
if ok && jid != ce.User.JID {
|
|
|
|
participants = append(participants, jid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := ce.User.Conn.CreateGroup(roomNameEvent.Name, participants)
|
|
|
|
if err != nil {
|
|
|
|
ce.Reply("Failed to create group: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
portal := handler.bridge.GetPortalByJID(database.GroupPortalKey(resp.GroupID))
|
|
|
|
portal.roomCreateLock.Lock()
|
|
|
|
defer portal.roomCreateLock.Unlock()
|
|
|
|
if len(portal.MXID) != 0 {
|
|
|
|
portal.log.Warnln("Detected race condition in room creation")
|
|
|
|
// TODO race condition, clean up the old room
|
|
|
|
}
|
|
|
|
portal.MXID = ce.RoomID
|
|
|
|
portal.Name = roomNameEvent.Name
|
|
|
|
portal.Encrypted = encryptionEvent.Algorithm == id.AlgorithmMegolmV1
|
|
|
|
if !portal.Encrypted && handler.bridge.Config.Bridge.Encryption.Default {
|
|
|
|
_, err = portal.MainIntent().SendStateEvent(portal.MXID, event.StateEncryption, "", &event.EncryptionEventContent{Algorithm: id.AlgorithmMegolmV1})
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnln("Failed to enable e2be:", err)
|
|
|
|
}
|
|
|
|
portal.Encrypted = true
|
|
|
|
}
|
|
|
|
|
|
|
|
portal.Update()
|
|
|
|
portal.UpdateBridgeInfo()
|
|
|
|
|
|
|
|
ce.Reply("Successfully created WhatsApp group %s", portal.Key.JID)
|
2021-02-26 15:09:24 +01:00
|
|
|
inCommunity := ce.User.addPortalToCommunity(portal)
|
|
|
|
ce.User.CreateUserPortal(database.PortalKeyWithMeta{PortalKey: portal.Key, InCommunity: inCommunity})
|
2020-07-10 14:23:32 +02:00
|
|
|
}
|
|
|
|
|
2020-05-23 22:25:22 +02:00
|
|
|
const cmdSetPowerLevelHelp = `set-pl [user ID] <power level> - Change the power level in a portal room. Only for bridge admins.`
|
|
|
|
|
2020-05-09 01:03:59 +02:00
|
|
|
func (handler *CommandHandler) CommandSetPowerLevel(ce *CommandEvent) {
|
2020-06-25 21:42:52 +02:00
|
|
|
if ce.Portal == nil {
|
2020-05-09 01:03:59 +02:00
|
|
|
ce.Reply("Not a portal room")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var level int
|
|
|
|
var userID id.UserID
|
|
|
|
var err error
|
|
|
|
if len(ce.Args) == 1 {
|
|
|
|
level, err = strconv.Atoi(ce.Args[0])
|
|
|
|
if err != nil {
|
|
|
|
ce.Reply("Invalid power level \"%s\"", ce.Args[0])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
userID = ce.User.MXID
|
|
|
|
} else if len(ce.Args) == 2 {
|
|
|
|
userID = id.UserID(ce.Args[0])
|
|
|
|
_, _, err := userID.Parse()
|
|
|
|
if err != nil {
|
|
|
|
ce.Reply("Invalid user ID \"%s\"", ce.Args[0])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
level, err = strconv.Atoi(ce.Args[1])
|
|
|
|
if err != nil {
|
|
|
|
ce.Reply("Invalid power level \"%s\"", ce.Args[1])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ce.Reply("**Usage:** `set-pl [user] <level>`")
|
|
|
|
return
|
|
|
|
}
|
2020-06-25 21:42:52 +02:00
|
|
|
intent := ce.Portal.MainIntent()
|
2020-05-09 01:03:59 +02:00
|
|
|
_, err = intent.SetPowerLevel(ce.RoomID, userID, level)
|
|
|
|
if err != nil {
|
|
|
|
ce.Reply("Failed to set power levels: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 19:15:38 +02:00
|
|
|
const cmdLoginHelp = `login - Authenticate this Bridge as WhatsApp Web Client`
|
2018-10-16 18:35:39 +02:00
|
|
|
|
2018-10-16 19:16:13 +02:00
|
|
|
// CommandLogin handles login command
|
2018-08-18 21:57:08 +02:00
|
|
|
func (handler *CommandHandler) CommandLogin(ce *CommandEvent) {
|
2019-08-30 19:57:08 +02:00
|
|
|
if !ce.User.Connect(true) {
|
|
|
|
ce.User.log.Debugln("Connect() returned false, assuming error was logged elsewhere and canceling login.")
|
|
|
|
return
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
2019-05-15 22:17:09 +02:00
|
|
|
ce.User.Login(ce)
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 19:15:38 +02:00
|
|
|
const cmdLogoutHelp = `logout - Logout from WhatsApp`
|
2018-10-16 18:35:39 +02:00
|
|
|
|
2018-10-16 19:16:13 +02:00
|
|
|
// CommandLogout handles !logout command
|
2018-08-18 21:57:08 +02:00
|
|
|
func (handler *CommandHandler) CommandLogout(ce *CommandEvent) {
|
|
|
|
if ce.User.Session == nil {
|
|
|
|
ce.Reply("You're not logged in.")
|
|
|
|
return
|
2020-05-21 19:50:54 +02:00
|
|
|
} else if !ce.User.IsConnected() {
|
|
|
|
ce.Reply("You are not connected to WhatsApp. Use the `reconnect` command to reconnect, or `delete-session` to forget all login information.")
|
|
|
|
return
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
2020-05-21 18:49:01 +02:00
|
|
|
puppet := handler.bridge.GetPuppetByJID(ce.User.JID)
|
|
|
|
if puppet.CustomMXID != "" {
|
|
|
|
err := puppet.SwitchCustomMXID("", "")
|
|
|
|
if err != nil {
|
|
|
|
ce.User.log.Warnln("Failed to logout-matrix while logging out of WhatsApp:", err)
|
|
|
|
}
|
|
|
|
}
|
2018-08-18 21:57:08 +02:00
|
|
|
err := ce.User.Conn.Logout()
|
|
|
|
if err != nil {
|
|
|
|
ce.User.log.Warnln("Error while logging out:", err)
|
2019-05-15 23:18:43 +02:00
|
|
|
ce.Reply("Unknown error while logging out: %v", err)
|
2018-08-18 21:57:08 +02:00
|
|
|
return
|
|
|
|
}
|
2020-05-21 18:49:01 +02:00
|
|
|
ce.User.removeFromJIDMap()
|
|
|
|
// TODO this causes a foreign key violation, which should be fixed
|
|
|
|
//ce.User.JID = ""
|
2019-05-16 17:00:46 +02:00
|
|
|
ce.User.SetSession(nil)
|
2021-02-18 22:36:14 +01:00
|
|
|
ce.User.DeleteConnection()
|
2018-08-18 21:57:08 +02:00
|
|
|
ce.Reply("Logged out successfully.")
|
|
|
|
}
|
|
|
|
|
2021-04-19 18:25:40 +02:00
|
|
|
const cmdToggleHelp = `toggle <presence|receipts|all> - Toggle bridging of presence or read receipts`
|
2020-07-10 13:53:18 +02:00
|
|
|
|
2020-07-10 15:26:55 +02:00
|
|
|
func (handler *CommandHandler) CommandToggle(ce *CommandEvent) {
|
2021-03-02 18:06:16 +01:00
|
|
|
if len(ce.Args) == 0 || (ce.Args[0] != "presence" && ce.Args[0] != "receipts" && ce.Args[0] != "all") {
|
|
|
|
ce.Reply("**Usage:** `toggle <presence|receipts|all>`")
|
2020-07-10 15:26:55 +02:00
|
|
|
return
|
|
|
|
}
|
2020-07-10 13:53:18 +02:00
|
|
|
if ce.User.Session == nil {
|
|
|
|
ce.Reply("You're not logged in.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
customPuppet := handler.bridge.GetPuppetByCustomMXID(ce.User.MXID)
|
|
|
|
if customPuppet == nil {
|
|
|
|
ce.Reply("You're not logged in with your Matrix account.")
|
|
|
|
return
|
|
|
|
}
|
2021-03-02 18:06:16 +01:00
|
|
|
if ce.Args[0] == "presence" || ce.Args[0] == "all" {
|
2020-07-10 15:26:55 +02:00
|
|
|
customPuppet.EnablePresence = !customPuppet.EnablePresence
|
|
|
|
var newPresence whatsapp.Presence
|
|
|
|
if customPuppet.EnablePresence {
|
|
|
|
newPresence = whatsapp.PresenceAvailable
|
|
|
|
ce.Reply("Enabled presence bridging")
|
|
|
|
} else {
|
|
|
|
newPresence = whatsapp.PresenceUnavailable
|
|
|
|
ce.Reply("Disabled presence bridging")
|
|
|
|
}
|
|
|
|
if ce.User.IsConnected() {
|
|
|
|
_, err := ce.User.Conn.Presence("", newPresence)
|
|
|
|
if err != nil {
|
|
|
|
ce.User.log.Warnln("Failed to set presence:", err)
|
|
|
|
}
|
|
|
|
}
|
2021-04-19 18:25:40 +02:00
|
|
|
}
|
|
|
|
if ce.Args[0] == "receipts" || ce.Args[0] == "all" {
|
2020-07-10 15:26:55 +02:00
|
|
|
customPuppet.EnableReceipts = !customPuppet.EnableReceipts
|
|
|
|
if customPuppet.EnableReceipts {
|
|
|
|
ce.Reply("Enabled read receipt bridging")
|
|
|
|
} else {
|
|
|
|
ce.Reply("Disabled read receipt bridging")
|
2020-07-10 13:53:18 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-10 15:26:55 +02:00
|
|
|
customPuppet.Update()
|
2020-07-10 13:53:18 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 17:00:46 +02:00
|
|
|
const cmdDeleteSessionHelp = `delete-session - Delete session information and disconnect from WhatsApp without sending a logout request`
|
|
|
|
|
|
|
|
func (handler *CommandHandler) CommandDeleteSession(ce *CommandEvent) {
|
2019-08-24 21:39:12 +02:00
|
|
|
if ce.User.Session == nil && ce.User.Conn == nil {
|
2019-05-16 17:00:46 +02:00
|
|
|
ce.Reply("Nothing to purge: no session information stored and no active connection.")
|
|
|
|
return
|
|
|
|
}
|
2021-02-18 22:36:14 +01:00
|
|
|
//ce.User.JID = ""
|
2020-10-04 12:55:09 +02:00
|
|
|
ce.User.removeFromJIDMap()
|
2019-05-16 17:00:46 +02:00
|
|
|
ce.User.SetSession(nil)
|
2021-02-18 22:36:14 +01:00
|
|
|
ce.User.DeleteConnection()
|
2019-05-16 17:00:46 +02:00
|
|
|
ce.Reply("Session information purged")
|
|
|
|
}
|
|
|
|
|
2019-05-15 22:04:09 +02:00
|
|
|
const cmdReconnectHelp = `reconnect - Reconnect to WhatsApp`
|
|
|
|
|
|
|
|
func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) {
|
2019-05-23 19:09:13 +02:00
|
|
|
if ce.User.Conn == nil {
|
2019-05-23 19:11:55 +02:00
|
|
|
if ce.User.Session == nil {
|
|
|
|
ce.Reply("No existing connection and no session. Did you mean `login`?")
|
|
|
|
} else {
|
|
|
|
ce.Reply("No existing connection, creating one...")
|
|
|
|
ce.User.Connect(false)
|
|
|
|
}
|
2019-05-23 19:09:13 +02:00
|
|
|
return
|
|
|
|
}
|
2020-03-11 17:21:51 +01:00
|
|
|
|
|
|
|
wasConnected := true
|
2021-02-18 22:36:14 +01:00
|
|
|
err := ce.User.Conn.Disconnect()
|
2020-03-11 17:21:51 +01:00
|
|
|
if err == whatsapp.ErrNotConnected {
|
|
|
|
wasConnected = false
|
|
|
|
} else if err != nil {
|
|
|
|
ce.User.log.Warnln("Error while disconnecting:", err)
|
|
|
|
}
|
|
|
|
|
2021-02-18 22:36:14 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
err = ce.User.Conn.Restore(true, ctx)
|
2019-05-15 23:18:43 +02:00
|
|
|
if err == whatsapp.ErrInvalidSession {
|
|
|
|
if ce.User.Session != nil {
|
|
|
|
ce.User.log.Debugln("Got invalid session error when reconnecting, but user has session. Retrying using RestoreWithSession()...")
|
2021-02-18 22:36:14 +01:00
|
|
|
ce.User.Conn.SetSession(*ce.User.Session)
|
|
|
|
err = ce.User.Conn.Restore(true, ctx)
|
2019-05-15 22:04:09 +02:00
|
|
|
} else {
|
2019-05-15 23:18:43 +02:00
|
|
|
ce.Reply("You are not logged in.")
|
|
|
|
return
|
2019-05-15 22:04:09 +02:00
|
|
|
}
|
2019-05-17 22:53:57 +02:00
|
|
|
} else if err == whatsapp.ErrLoginInProgress {
|
|
|
|
ce.Reply("A login or reconnection is already in progress.")
|
|
|
|
return
|
2020-03-11 16:09:31 +01:00
|
|
|
} else if err == whatsapp.ErrAlreadyLoggedIn {
|
|
|
|
ce.Reply("You were already connected.")
|
|
|
|
return
|
2019-05-15 23:18:43 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
2019-05-15 22:04:09 +02:00
|
|
|
ce.User.log.Warnln("Error while reconnecting:", err)
|
2021-02-18 22:36:14 +01:00
|
|
|
ce.Reply("Unknown error while reconnecting: %v", err)
|
2019-08-30 21:04:57 +02:00
|
|
|
ce.User.log.Debugln("Disconnecting due to failed session restore in reconnect command...")
|
2021-02-18 22:36:14 +01:00
|
|
|
err = ce.User.Conn.Disconnect()
|
2019-08-30 21:04:57 +02:00
|
|
|
if err != nil {
|
|
|
|
ce.User.log.Errorln("Failed to disconnect after failed session restore in reconnect command:", err)
|
|
|
|
}
|
2019-05-15 22:04:09 +02:00
|
|
|
return
|
|
|
|
}
|
2019-05-17 22:53:57 +02:00
|
|
|
ce.User.ConnectionErrors = 0
|
2020-03-11 17:21:51 +01:00
|
|
|
|
|
|
|
var msg string
|
|
|
|
if wasConnected {
|
|
|
|
msg = "Reconnected successfully."
|
|
|
|
} else {
|
|
|
|
msg = "Connected successfully."
|
|
|
|
}
|
|
|
|
ce.Reply(msg)
|
2019-05-30 16:48:22 +02:00
|
|
|
ce.User.PostLogin()
|
2019-05-15 22:04:09 +02:00
|
|
|
}
|
|
|
|
|
2020-05-23 22:25:22 +02:00
|
|
|
const cmdDeleteConnectionHelp = `delete-connection - Disconnect ignoring errors and delete internal connection state.`
|
|
|
|
|
2019-05-23 19:09:13 +02:00
|
|
|
func (handler *CommandHandler) CommandDeleteConnection(ce *CommandEvent) {
|
|
|
|
if ce.User.Conn == nil {
|
|
|
|
ce.Reply("You don't have a WhatsApp connection.")
|
|
|
|
return
|
|
|
|
}
|
2021-02-18 22:36:14 +01:00
|
|
|
ce.User.DeleteConnection()
|
2019-05-23 19:09:13 +02:00
|
|
|
ce.Reply("Successfully disconnected. Use the `reconnect` command to reconnect.")
|
|
|
|
}
|
|
|
|
|
2019-05-15 22:04:09 +02:00
|
|
|
const cmdDisconnectHelp = `disconnect - Disconnect from WhatsApp (without logging out)`
|
|
|
|
|
|
|
|
func (handler *CommandHandler) CommandDisconnect(ce *CommandEvent) {
|
2019-05-23 19:09:13 +02:00
|
|
|
if ce.User.Conn == nil {
|
|
|
|
ce.Reply("You don't have a WhatsApp connection.")
|
|
|
|
return
|
|
|
|
}
|
2021-02-18 22:36:14 +01:00
|
|
|
err := ce.User.Conn.Disconnect()
|
2019-05-15 22:04:09 +02:00
|
|
|
if err == whatsapp.ErrNotConnected {
|
|
|
|
ce.Reply("You were not connected.")
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
ce.User.log.Warnln("Error while disconnecting:", err)
|
2019-05-15 23:18:43 +02:00
|
|
|
ce.Reply("Unknown error while disconnecting: %v", err)
|
2019-05-15 22:04:09 +02:00
|
|
|
return
|
|
|
|
}
|
2020-09-27 21:30:08 +02:00
|
|
|
ce.User.bridge.Metrics.TrackConnectionState(ce.User.JID, false)
|
2021-03-15 17:06:16 +01:00
|
|
|
ce.User.sendBridgeStatus(AsmuxPong{Error: AsmuxWANotConnected})
|
2019-05-15 22:04:09 +02:00
|
|
|
ce.Reply("Successfully disconnected. Use the `reconnect` command to reconnect.")
|
|
|
|
}
|
|
|
|
|
2019-08-24 21:39:12 +02:00
|
|
|
const cmdPingHelp = `ping - Check your connection to WhatsApp.`
|
|
|
|
|
|
|
|
func (handler *CommandHandler) CommandPing(ce *CommandEvent) {
|
2019-08-30 20:32:47 +02:00
|
|
|
if ce.User.Session == nil {
|
2019-08-30 21:04:57 +02:00
|
|
|
if ce.User.IsLoginInProgress() {
|
|
|
|
ce.Reply("You're not logged into WhatsApp, but there's a login in progress.")
|
|
|
|
} else {
|
|
|
|
ce.Reply("You're not logged into WhatsApp.")
|
|
|
|
}
|
2019-08-30 20:32:47 +02:00
|
|
|
} else if ce.User.Conn == nil {
|
2019-08-24 21:39:12 +02:00
|
|
|
ce.Reply("You don't have a WhatsApp connection.")
|
2020-06-23 14:36:08 +02:00
|
|
|
} else if err := ce.User.Conn.AdminTest(); err != nil {
|
2019-08-30 21:04:57 +02:00
|
|
|
if ce.User.IsLoginInProgress() {
|
|
|
|
ce.Reply("Connection not OK: %v, but login in progress", err)
|
|
|
|
} else {
|
|
|
|
ce.Reply("Connection not OK: %v", err)
|
|
|
|
}
|
2020-06-25 21:42:52 +02:00
|
|
|
} else {
|
2019-08-24 21:39:12 +02:00
|
|
|
ce.Reply("Connection to WhatsApp OK")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 19:15:38 +02:00
|
|
|
const cmdHelpHelp = `help - Prints this help`
|
|
|
|
|
2018-10-16 19:16:13 +02:00
|
|
|
// CommandHelp handles help command
|
2018-08-18 21:57:08 +02:00
|
|
|
func (handler *CommandHandler) CommandHelp(ce *CommandEvent) {
|
2018-12-07 23:31:15 +01:00
|
|
|
cmdPrefix := ""
|
2019-11-10 20:22:11 +01:00
|
|
|
if ce.User.ManagementRoom != ce.RoomID || ce.User.IsRelaybot {
|
2018-12-07 23:31:15 +01:00
|
|
|
cmdPrefix = handler.bridge.Config.Bridge.CommandPrefix + " "
|
|
|
|
}
|
|
|
|
|
2019-02-20 13:39:44 +01:00
|
|
|
ce.Reply("* " + strings.Join([]string{
|
2018-10-16 19:15:38 +02:00
|
|
|
cmdPrefix + cmdHelpHelp,
|
2021-04-29 13:00:26 +02:00
|
|
|
cmdPrefix + cmdVersionHelp,
|
2018-10-16 19:15:38 +02:00
|
|
|
cmdPrefix + cmdLoginHelp,
|
|
|
|
cmdPrefix + cmdLogoutHelp,
|
2019-05-16 17:00:46 +02:00
|
|
|
cmdPrefix + cmdDeleteSessionHelp,
|
2019-05-15 22:04:09 +02:00
|
|
|
cmdPrefix + cmdReconnectHelp,
|
|
|
|
cmdPrefix + cmdDisconnectHelp,
|
2020-05-23 22:25:22 +02:00
|
|
|
cmdPrefix + cmdDeleteConnectionHelp,
|
2019-08-24 21:39:12 +02:00
|
|
|
cmdPrefix + cmdPingHelp,
|
|
|
|
cmdPrefix + cmdLoginMatrixHelp,
|
|
|
|
cmdPrefix + cmdLogoutMatrixHelp,
|
2020-07-10 15:26:55 +02:00
|
|
|
cmdPrefix + cmdToggleHelp,
|
2019-02-20 13:39:44 +01:00
|
|
|
cmdPrefix + cmdSyncHelp,
|
|
|
|
cmdPrefix + cmdListHelp,
|
|
|
|
cmdPrefix + cmdOpenHelp,
|
|
|
|
cmdPrefix + cmdPMHelp,
|
2020-06-25 21:40:34 +02:00
|
|
|
cmdPrefix + cmdInviteLinkHelp,
|
2020-06-25 22:29:16 +02:00
|
|
|
cmdPrefix + cmdJoinHelp,
|
2020-07-10 14:23:32 +02:00
|
|
|
cmdPrefix + cmdCreateHelp,
|
2020-05-23 22:25:22 +02:00
|
|
|
cmdPrefix + cmdSetPowerLevelHelp,
|
|
|
|
cmdPrefix + cmdDeletePortalHelp,
|
|
|
|
cmdPrefix + cmdDeleteAllPortalsHelp,
|
2019-02-20 13:39:44 +01:00
|
|
|
}, "\n* "))
|
|
|
|
}
|
|
|
|
|
2019-05-31 18:33:18 +02:00
|
|
|
const cmdSyncHelp = `sync [--create-all] - Synchronize contacts from phone and optionally create portals for group chats.`
|
2019-02-20 13:39:44 +01:00
|
|
|
|
|
|
|
// CommandSync handles sync command
|
|
|
|
func (handler *CommandHandler) CommandSync(ce *CommandEvent) {
|
|
|
|
user := ce.User
|
2019-05-31 18:33:18 +02:00
|
|
|
create := len(ce.Args) > 0 && ce.Args[0] == "--create-all"
|
2019-02-20 13:39:44 +01:00
|
|
|
|
2019-05-31 18:33:18 +02:00
|
|
|
ce.Reply("Updating contact and chat list...")
|
|
|
|
handler.log.Debugln("Importing contacts of", user.MXID)
|
2019-02-20 13:39:44 +01:00
|
|
|
_, err := user.Conn.Contacts()
|
|
|
|
if err != nil {
|
2019-05-23 21:57:52 +02:00
|
|
|
user.log.Errorln("Error updating contacts:", err)
|
|
|
|
ce.Reply("Failed to sync contact list (see logs for details)")
|
2019-02-20 13:39:44 +01:00
|
|
|
return
|
|
|
|
}
|
2019-05-31 18:33:18 +02:00
|
|
|
handler.log.Debugln("Importing chats of", user.MXID)
|
2019-05-23 21:57:52 +02:00
|
|
|
_, err = user.Conn.Chats()
|
|
|
|
if err != nil {
|
|
|
|
user.log.Errorln("Error updating chats:", err)
|
|
|
|
ce.Reply("Failed to sync chat list (see logs for details)")
|
|
|
|
return
|
2019-02-20 13:39:44 +01:00
|
|
|
}
|
|
|
|
|
2019-05-31 18:33:18 +02:00
|
|
|
ce.Reply("Syncing contacts...")
|
2019-08-24 23:25:29 +02:00
|
|
|
user.syncPuppets(nil)
|
2019-05-31 18:33:18 +02:00
|
|
|
ce.Reply("Syncing chats...")
|
2019-08-24 23:25:29 +02:00
|
|
|
user.syncPortals(nil, create)
|
2019-05-23 21:57:52 +02:00
|
|
|
|
2019-05-31 18:33:18 +02:00
|
|
|
ce.Reply("Sync complete.")
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
2018-12-07 14:42:57 +01:00
|
|
|
|
2020-05-23 22:25:22 +02:00
|
|
|
const cmdDeletePortalHelp = `delete-portal - Delete the current portal. If the portal is used by other people, this is limited to bridge admins.`
|
2019-05-21 20:06:27 +02:00
|
|
|
|
2020-05-23 22:25:22 +02:00
|
|
|
func (handler *CommandHandler) CommandDeletePortal(ce *CommandEvent) {
|
2020-06-25 21:42:52 +02:00
|
|
|
if ce.Portal == nil {
|
2019-05-21 20:06:27 +02:00
|
|
|
ce.Reply("You must be in a portal room to use that command")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 22:25:22 +02:00
|
|
|
if !ce.User.Admin {
|
2020-06-25 21:42:52 +02:00
|
|
|
users := ce.Portal.GetUserIDs()
|
2020-05-23 22:25:22 +02:00
|
|
|
if len(users) > 1 || (len(users) == 1 && users[0] != ce.User.MXID) {
|
|
|
|
ce.Reply("Only bridge admins can delete portals with other Matrix users")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 21:42:52 +02:00
|
|
|
ce.Portal.log.Infoln(ce.User.MXID, "requested deletion of portal.")
|
|
|
|
ce.Portal.Delete()
|
|
|
|
ce.Portal.Cleanup(false)
|
2019-05-21 20:06:27 +02:00
|
|
|
}
|
|
|
|
|
2020-05-23 22:25:22 +02:00
|
|
|
const cmdDeleteAllPortalsHelp = `delete-all-portals - Delete all your portals that aren't used by any other user.'`
|
|
|
|
|
2019-05-31 18:33:18 +02:00
|
|
|
func (handler *CommandHandler) CommandDeleteAllPortals(ce *CommandEvent) {
|
|
|
|
portals := ce.User.GetPortals()
|
|
|
|
portalsToDelete := make([]*Portal, 0, len(portals))
|
|
|
|
for _, portal := range portals {
|
|
|
|
users := portal.GetUserIDs()
|
|
|
|
if len(users) == 1 && users[0] == ce.User.MXID {
|
|
|
|
portalsToDelete = append(portalsToDelete, portal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
leave := func(portal *Portal) {
|
|
|
|
if len(portal.MXID) > 0 {
|
|
|
|
_, _ = portal.MainIntent().KickUser(portal.MXID, &mautrix.ReqKickUser{
|
|
|
|
Reason: "Deleting portal",
|
|
|
|
UserID: ce.User.MXID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
customPuppet := handler.bridge.GetPuppetByCustomMXID(ce.User.MXID)
|
2019-05-31 19:51:16 +02:00
|
|
|
if customPuppet != nil && customPuppet.CustomIntent() != nil {
|
2019-05-31 18:33:18 +02:00
|
|
|
intent := customPuppet.CustomIntent()
|
|
|
|
leave = func(portal *Portal) {
|
|
|
|
if len(portal.MXID) > 0 {
|
|
|
|
_, _ = intent.LeaveRoom(portal.MXID)
|
|
|
|
_, _ = intent.ForgetRoom(portal.MXID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ce.Reply("Found %d portals with no other users, deleting...", len(portalsToDelete))
|
|
|
|
for _, portal := range portalsToDelete {
|
|
|
|
portal.Delete()
|
|
|
|
leave(portal)
|
|
|
|
}
|
|
|
|
ce.Reply("Finished deleting portal info. Now cleaning up rooms in background. " +
|
|
|
|
"You may already continue using the bridge. Use `sync` to recreate portals.")
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for _, portal := range portalsToDelete {
|
|
|
|
portal.Cleanup(false)
|
|
|
|
}
|
|
|
|
ce.Reply("Finished background cleanup of deleted portal rooms.")
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-05-23 22:25:22 +02:00
|
|
|
const cmdListHelp = `list <contacts|groups> [page] [items per page] - Get a list of all contacts and groups.`
|
2019-02-20 13:39:44 +01:00
|
|
|
|
2020-05-23 23:11:56 +02:00
|
|
|
func formatContacts(contacts bool, input map[string]whatsapp.Contact) (result []string) {
|
2020-05-23 22:17:43 +02:00
|
|
|
for jid, contact := range input {
|
2021-02-17 00:21:30 +01:00
|
|
|
if strings.HasSuffix(jid, whatsapp.NewUserSuffix) != contacts {
|
2020-05-23 22:17:43 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-03-15 15:45:27 +01:00
|
|
|
|
2020-05-23 22:17:43 +02:00
|
|
|
if contacts {
|
2021-02-18 22:36:14 +01:00
|
|
|
result = append(result, fmt.Sprintf("* %s / %s - `%s`", contact.Name, contact.Notify, contact.JID[:len(contact.JID)-len(whatsapp.NewUserSuffix)]))
|
2019-02-20 13:39:44 +01:00
|
|
|
} else {
|
2021-02-18 22:36:14 +01:00
|
|
|
result = append(result, fmt.Sprintf("* %s - `%s`", contact.Name, contact.JID))
|
2019-02-20 13:39:44 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-23 22:17:43 +02:00
|
|
|
sort.Sort(sort.StringSlice(result))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *CommandHandler) CommandList(ce *CommandEvent) {
|
|
|
|
if len(ce.Args) == 0 {
|
|
|
|
ce.Reply("**Usage:** `list <contacts|groups> [page] [items per page]`")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mode := strings.ToLower(ce.Args[0])
|
|
|
|
if mode[0] != 'g' && mode[0] != 'c' {
|
|
|
|
ce.Reply("**Usage:** `list <contacts|groups> [page] [items per page]`")
|
2020-05-23 23:11:56 +02:00
|
|
|
return
|
2020-05-23 22:17:43 +02:00
|
|
|
}
|
|
|
|
var err error
|
2020-05-23 22:52:05 +02:00
|
|
|
page := 1
|
2020-05-23 22:17:43 +02:00
|
|
|
max := 100
|
|
|
|
if len(ce.Args) > 1 {
|
|
|
|
page, err = strconv.Atoi(ce.Args[1])
|
|
|
|
if err != nil || page <= 0 {
|
|
|
|
ce.Reply("\"%s\" isn't a valid page number", ce.Args[1])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(ce.Args) > 2 {
|
|
|
|
max, err = strconv.Atoi(ce.Args[2])
|
|
|
|
if err != nil || max <= 0 {
|
|
|
|
ce.Reply("\"%s\" isn't a valid number of items per page", ce.Args[2])
|
|
|
|
return
|
|
|
|
} else if max > 400 {
|
|
|
|
ce.Reply("Warning: a high number of items per page may fail to send a reply")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contacts := mode[0] == 'c'
|
|
|
|
typeName := "Groups"
|
|
|
|
if contacts {
|
|
|
|
typeName = "Contacts"
|
|
|
|
}
|
2020-05-23 23:11:56 +02:00
|
|
|
result := formatContacts(contacts, ce.User.Conn.Store.Contacts)
|
|
|
|
if len(result) == 0 {
|
|
|
|
ce.Reply("No %s found", strings.ToLower(typeName))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pages := int(math.Ceil(float64(len(result)) / float64(max)))
|
2020-05-24 16:03:50 +02:00
|
|
|
if (page-1)*max >= len(result) {
|
2020-05-23 23:11:56 +02:00
|
|
|
if pages == 1 {
|
|
|
|
ce.Reply("There is only 1 page of %s", strings.ToLower(typeName))
|
|
|
|
} else {
|
|
|
|
ce.Reply("There are only %d pages of %s", pages, strings.ToLower(typeName))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-05-24 16:03:50 +02:00
|
|
|
lastIndex := page * max
|
2020-05-24 14:44:17 +02:00
|
|
|
if lastIndex > len(result) {
|
|
|
|
lastIndex = len(result)
|
|
|
|
}
|
|
|
|
result = result[(page-1)*max : lastIndex]
|
2020-05-23 22:17:43 +02:00
|
|
|
ce.Reply("### %s (page %d of %d)\n\n%s", typeName, page, pages, strings.Join(result, "\n"))
|
2019-02-20 13:39:44 +01:00
|
|
|
}
|
2018-12-07 14:42:57 +01:00
|
|
|
|
2019-02-20 13:39:44 +01:00
|
|
|
const cmdOpenHelp = `open <_group JID_> - Open a group chat portal.`
|
2018-12-07 14:42:57 +01:00
|
|
|
|
2019-02-20 13:39:44 +01:00
|
|
|
func (handler *CommandHandler) CommandOpen(ce *CommandEvent) {
|
2018-12-08 00:30:15 +01:00
|
|
|
if len(ce.Args) == 0 {
|
2019-02-20 13:39:44 +01:00
|
|
|
ce.Reply("**Usage:** `open <group JID>`")
|
2019-03-15 15:45:27 +01:00
|
|
|
return
|
2019-02-20 13:39:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
user := ce.User
|
|
|
|
jid := ce.Args[0]
|
|
|
|
|
2021-02-17 00:21:30 +01:00
|
|
|
if strings.HasSuffix(jid, whatsapp.NewUserSuffix) {
|
|
|
|
ce.Reply("That looks like a user JID. Did you mean `pm %s`?", jid[:len(jid)-len(whatsapp.NewUserSuffix)])
|
2018-12-08 00:30:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-20 13:39:44 +01:00
|
|
|
contact, ok := user.Conn.Store.Contacts[jid]
|
|
|
|
if !ok {
|
|
|
|
ce.Reply("Group JID not found in contacts. Try syncing contacts with `sync` first.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
handler.log.Debugln("Importing", jid, "for", user)
|
|
|
|
portal := user.bridge.GetPortalByJID(database.GroupPortalKey(jid))
|
|
|
|
if len(portal.MXID) > 0 {
|
|
|
|
portal.Sync(user, contact)
|
|
|
|
ce.Reply("Portal room synced.")
|
|
|
|
} else {
|
|
|
|
portal.Sync(user, contact)
|
|
|
|
ce.Reply("Portal room created.")
|
|
|
|
}
|
2020-05-12 19:30:38 +02:00
|
|
|
_, _ = portal.MainIntent().InviteUser(portal.MXID, &mautrix.ReqInviteUser{UserID: user.MXID})
|
2019-02-20 13:39:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const cmdPMHelp = `pm [--force] <_international phone number_> - Open a private chat with the given phone number.`
|
|
|
|
|
|
|
|
func (handler *CommandHandler) CommandPM(ce *CommandEvent) {
|
|
|
|
if len(ce.Args) == 0 {
|
|
|
|
ce.Reply("**Usage:** `pm [--force] <international phone number>`")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
force := ce.Args[0] == "--force"
|
|
|
|
if force {
|
|
|
|
ce.Args = ce.Args[1:]
|
|
|
|
}
|
|
|
|
|
2018-12-07 14:42:57 +01:00
|
|
|
user := ce.User
|
|
|
|
|
2019-02-20 13:39:44 +01:00
|
|
|
number := strings.Join(ce.Args, "")
|
|
|
|
if number[0] == '+' {
|
|
|
|
number = number[1:]
|
|
|
|
}
|
|
|
|
for _, char := range number {
|
|
|
|
if char < '0' || char > '9' {
|
|
|
|
ce.Reply("Invalid phone number.")
|
2018-12-07 14:42:57 +01:00
|
|
|
return
|
|
|
|
}
|
2019-02-20 13:39:44 +01:00
|
|
|
}
|
2021-02-17 00:21:30 +01:00
|
|
|
jid := number + whatsapp.NewUserSuffix
|
2018-12-07 14:42:57 +01:00
|
|
|
|
2019-02-20 13:39:44 +01:00
|
|
|
handler.log.Debugln("Importing", jid, "for", user)
|
2018-12-07 14:42:57 +01:00
|
|
|
|
2019-02-20 13:39:44 +01:00
|
|
|
contact, ok := user.Conn.Store.Contacts[jid]
|
|
|
|
if !ok {
|
|
|
|
if !force {
|
|
|
|
ce.Reply("Phone number not found in contacts. Try syncing contacts with `sync` first. " +
|
|
|
|
"To create a portal anyway, use `pm --force <number>`.")
|
|
|
|
return
|
|
|
|
}
|
2021-02-18 22:36:14 +01:00
|
|
|
contact = whatsapp.Contact{JID: jid}
|
2019-02-20 13:39:44 +01:00
|
|
|
}
|
2021-02-18 22:36:14 +01:00
|
|
|
puppet := user.bridge.GetPuppetByJID(contact.JID)
|
2019-02-20 13:39:44 +01:00
|
|
|
puppet.Sync(user, contact)
|
2021-02-18 22:36:14 +01:00
|
|
|
portal := user.bridge.GetPortalByJID(database.NewPortalKey(contact.JID, user.JID))
|
2019-02-20 13:39:44 +01:00
|
|
|
if len(portal.MXID) > 0 {
|
2021-03-08 00:51:37 +01:00
|
|
|
var err error
|
|
|
|
if !user.IsRelaybot {
|
|
|
|
err = portal.MainIntent().EnsureInvited(portal.MXID, user.MXID)
|
|
|
|
}
|
2019-02-20 13:39:44 +01:00
|
|
|
if err != nil {
|
2020-07-05 18:27:40 +02:00
|
|
|
portal.log.Warnfln("Failed to invite %s to portal: %v. Creating new portal", user.MXID, err)
|
2021-03-08 00:51:37 +01:00
|
|
|
portal.MXID = ""
|
2019-02-20 13:39:44 +01:00
|
|
|
} else {
|
2020-07-05 18:41:28 +02:00
|
|
|
ce.Reply("You already have a private chat portal with that user at [%s](https://matrix.to/#/%s)", puppet.Displayname, portal.MXID)
|
2020-07-05 18:27:40 +02:00
|
|
|
return
|
2019-02-20 13:39:44 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-13 23:38:11 +01:00
|
|
|
err := portal.CreateMatrixRoom(user)
|
2019-02-20 13:39:44 +01:00
|
|
|
if err != nil {
|
2019-05-15 22:04:09 +02:00
|
|
|
ce.Reply("Failed to create portal room: %v", err)
|
2019-02-20 13:39:44 +01:00
|
|
|
return
|
2018-12-07 14:42:57 +01:00
|
|
|
}
|
2019-02-20 13:39:44 +01:00
|
|
|
ce.Reply("Created portal room and invited you to it.")
|
2018-12-07 14:42:57 +01:00
|
|
|
}
|
2019-05-24 01:33:26 +02:00
|
|
|
|
|
|
|
const cmdLoginMatrixHelp = `login-matrix <_access token_> - Replace your WhatsApp account's Matrix puppet with your real Matrix account.'`
|
|
|
|
|
|
|
|
func (handler *CommandHandler) CommandLoginMatrix(ce *CommandEvent) {
|
|
|
|
if len(ce.Args) == 0 {
|
|
|
|
ce.Reply("**Usage:** `login-matrix <access token>`")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
puppet := handler.bridge.GetPuppetByJID(ce.User.JID)
|
|
|
|
err := puppet.SwitchCustomMXID(ce.Args[0], ce.User.MXID)
|
|
|
|
if err != nil {
|
|
|
|
ce.Reply("Failed to switch puppet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ce.Reply("Successfully switched puppet")
|
|
|
|
}
|
|
|
|
|
|
|
|
const cmdLogoutMatrixHelp = `logout-matrix - Switch your WhatsApp account's Matrix puppet back to the default one.`
|
|
|
|
|
|
|
|
func (handler *CommandHandler) CommandLogoutMatrix(ce *CommandEvent) {
|
2019-05-31 22:02:00 +02:00
|
|
|
puppet := handler.bridge.GetPuppetByJID(ce.User.JID)
|
|
|
|
if len(puppet.CustomMXID) == 0 {
|
|
|
|
ce.Reply("You had not changed your WhatsApp account's Matrix puppet.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := puppet.SwitchCustomMXID("", "")
|
|
|
|
if err != nil {
|
|
|
|
ce.Reply("Failed to remove custom puppet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ce.Reply("Successfully removed custom puppet")
|
2019-05-24 01:33:26 +02:00
|
|
|
}
|