2018-08-18 21:57:08 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
|
|
|
// Copyright (C) 2018 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/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2018-08-26 21:53:13 +02:00
|
|
|
|
2018-12-07 14:42:57 +01:00
|
|
|
"github.com/Rhymen/go-whatsapp"
|
2018-08-18 21:57:08 +02:00
|
|
|
"maunium.net/go/maulogger"
|
2018-08-26 21:53:13 +02:00
|
|
|
"maunium.net/go/mautrix-appservice"
|
2018-12-07 14:42:57 +01:00
|
|
|
"maunium.net/go/mautrix-whatsapp/database"
|
2018-08-26 21:53:13 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/types"
|
2018-12-07 14:42:57 +01:00
|
|
|
"maunium.net/go/mautrix-whatsapp/whatsapp-ext"
|
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
|
|
|
|
Handler *CommandHandler
|
|
|
|
RoomID types.MatrixRoomID
|
|
|
|
User *User
|
|
|
|
Args []string
|
|
|
|
}
|
|
|
|
|
2018-10-16 19:16:13 +02:00
|
|
|
// Reply sends a reply to command as notice
|
2018-08-18 21:57:08 +02:00
|
|
|
func (ce *CommandEvent) Reply(msg string) {
|
2018-12-07 17:02:51 +01:00
|
|
|
_, err := ce.Bot.SendNotice(string(ce.User.ManagementRoom), msg)
|
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
|
2018-08-18 21:57:08 +02:00
|
|
|
func (handler *CommandHandler) Handle(roomID types.MatrixRoomID, user *User, message string) {
|
|
|
|
args := strings.Split(message, " ")
|
|
|
|
cmd := strings.ToLower(args[0])
|
|
|
|
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,
|
|
|
|
Handler: handler,
|
|
|
|
RoomID: roomID,
|
|
|
|
User: user,
|
|
|
|
Args: args[1:],
|
|
|
|
}
|
|
|
|
switch cmd {
|
|
|
|
case "login":
|
|
|
|
handler.CommandLogin(ce)
|
|
|
|
case "logout":
|
|
|
|
handler.CommandLogout(ce)
|
|
|
|
case "help":
|
|
|
|
handler.CommandHelp(ce)
|
2018-12-07 14:42:57 +01:00
|
|
|
case "import":
|
|
|
|
handler.CommandImport(ce)
|
2018-10-16 19:15:38 +02:00
|
|
|
default:
|
|
|
|
ce.Reply("Unknown Command")
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if ce.User.Session != nil {
|
|
|
|
ce.Reply("You're already logged in.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ce.User.Connect(true)
|
|
|
|
ce.User.Login(ce.RoomID)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
err := ce.User.Conn.Logout()
|
|
|
|
if err != nil {
|
|
|
|
ce.User.log.Warnln("Error while logging out:", err)
|
|
|
|
ce.Reply("Error while logging out (see logs for details)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ce.User.Conn = nil
|
|
|
|
ce.User.Session = nil
|
|
|
|
ce.User.Update()
|
|
|
|
ce.Reply("Logged out successfully.")
|
|
|
|
}
|
|
|
|
|
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 := ""
|
|
|
|
if ce.User.ManagementRoom != ce.RoomID {
|
|
|
|
cmdPrefix = handler.bridge.Config.Bridge.CommandPrefix + " "
|
|
|
|
}
|
|
|
|
|
2018-10-16 18:35:39 +02:00
|
|
|
ce.Reply(strings.Join([]string{
|
2018-10-16 19:15:38 +02:00
|
|
|
cmdPrefix + cmdHelpHelp,
|
|
|
|
cmdPrefix + cmdLoginHelp,
|
|
|
|
cmdPrefix + cmdLogoutHelp,
|
2018-12-07 14:42:57 +01:00
|
|
|
cmdPrefix + cmdImportHelp,
|
2018-10-16 18:35:39 +02:00
|
|
|
}, "\n"))
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
2018-12-07 14:42:57 +01:00
|
|
|
|
2018-12-08 00:30:15 +01:00
|
|
|
const cmdImportHelp = `import <jid>|contacts - Open up a room for JID or for each WhatsApp contact`
|
2018-12-07 14:42:57 +01:00
|
|
|
|
|
|
|
// CommandImport handles import command
|
|
|
|
func (handler *CommandHandler) CommandImport(ce *CommandEvent) {
|
|
|
|
// ensure all messages go to the management room
|
|
|
|
ce.RoomID = ce.User.ManagementRoom
|
|
|
|
|
2018-12-08 00:30:15 +01:00
|
|
|
if len(ce.Args) == 0 {
|
|
|
|
ce.Reply("Usage: import <jid>|contacts")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-07 14:42:57 +01:00
|
|
|
user := ce.User
|
|
|
|
|
|
|
|
if ce.Args[0] == "contacts" {
|
|
|
|
handler.log.Debugln("Importing all contacts of", user)
|
|
|
|
_, err := user.Conn.Contacts()
|
|
|
|
if err != nil {
|
|
|
|
handler.log.Errorln("Error on update of contacts of user", user, ":", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for jid, contact := range user.Conn.Store.Contacts {
|
|
|
|
if strings.HasSuffix(jid, whatsappExt.NewUserSuffix) {
|
|
|
|
puppet := user.bridge.GetPuppetByJID(contact.Jid)
|
|
|
|
puppet.Sync(user, contact)
|
|
|
|
} else {
|
|
|
|
portal := user.bridge.GetPortalByJID(database.GroupPortalKey(contact.Jid))
|
|
|
|
portal.Sync(user, contact)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ce.Reply("Importing all contacts done")
|
|
|
|
} else {
|
|
|
|
jid := ce.Args[0] + whatsappExt.NewUserSuffix
|
|
|
|
handler.log.Debugln("Importing", jid, "for", user)
|
|
|
|
puppet := user.bridge.GetPuppetByJID(jid)
|
|
|
|
|
|
|
|
contact := whatsapp.Contact { Jid: jid }
|
|
|
|
puppet.Sync(user, contact)
|
|
|
|
}
|
|
|
|
}
|