2018-08-13 22:24:44 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2018-08-12 21:26:05 +02:00
|
|
|
// 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
|
|
|
|
|
2018-08-13 00:00:23 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-08-13 22:24:44 +02:00
|
|
|
"os/signal"
|
2018-08-28 23:40:54 +02:00
|
|
|
"sync"
|
2018-08-13 22:24:44 +02:00
|
|
|
"syscall"
|
2018-08-26 21:53:13 +02:00
|
|
|
|
|
|
|
flag "maunium.net/go/mauflag"
|
2019-01-11 20:17:31 +01:00
|
|
|
log "maunium.net/go/maulogger/v2"
|
2018-08-26 21:53:13 +02:00
|
|
|
"maunium.net/go/mautrix-appservice"
|
2019-01-11 20:17:31 +01:00
|
|
|
|
2018-08-26 21:53:13 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/config"
|
2018-08-13 22:24:44 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/database"
|
2018-08-16 23:11:28 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/types"
|
2018-08-13 00:00:23 +02:00
|
|
|
)
|
|
|
|
|
2018-08-13 22:24:44 +02:00
|
|
|
var configPath = flag.MakeFull("c", "config", "The path to your config file.", "config.yaml").String()
|
2018-08-28 23:40:54 +02:00
|
|
|
var baseConfigPath = flag.MakeFull("b", "base-config", "The path to the example config file.", "example-config.yaml").String()
|
2018-08-13 22:24:44 +02:00
|
|
|
var registrationPath = flag.MakeFull("r", "registration", "The path where to save the appservice registration.", "registration.yaml").String()
|
|
|
|
var generateRegistration = flag.MakeFull("g", "generate-registration", "Generate registration and quit.", "false").Bool()
|
|
|
|
var wantHelp, _ = flag.MakeHelpFlag()
|
|
|
|
|
|
|
|
func (bridge *Bridge) GenerateRegistration() {
|
|
|
|
reg, err := bridge.Config.NewRegistration()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Failed to generate registration:", err)
|
|
|
|
os.Exit(20)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = reg.Save(*registrationPath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Failed to save registration:", err)
|
|
|
|
os.Exit(21)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bridge.Config.Save(*configPath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Failed to save config:", err)
|
|
|
|
os.Exit(22)
|
|
|
|
}
|
|
|
|
fmt.Println("Registration generated. Add the path to the registration to your Synapse config restart it, then start the bridge.")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Bridge struct {
|
2018-08-28 23:40:54 +02:00
|
|
|
AS *appservice.AppService
|
2018-08-16 23:11:28 +02:00
|
|
|
EventProcessor *appservice.EventProcessor
|
2018-08-18 21:57:08 +02:00
|
|
|
MatrixHandler *MatrixHandler
|
2018-08-16 23:11:28 +02:00
|
|
|
Config *config.Config
|
|
|
|
DB *database.Database
|
|
|
|
Log log.Logger
|
2018-08-28 23:40:54 +02:00
|
|
|
StateStore *AutosavingStateStore
|
|
|
|
Bot *appservice.IntentAPI
|
|
|
|
Formatter *Formatter
|
|
|
|
|
|
|
|
usersByMXID map[types.MatrixUserID]*User
|
|
|
|
usersByJID map[types.WhatsAppID]*User
|
|
|
|
usersLock sync.Mutex
|
|
|
|
managementRooms map[types.MatrixRoomID]*User
|
|
|
|
managementRoomsLock sync.Mutex
|
|
|
|
portalsByMXID map[types.MatrixRoomID]*Portal
|
|
|
|
portalsByJID map[database.PortalKey]*Portal
|
|
|
|
portalsLock sync.Mutex
|
|
|
|
puppets map[types.WhatsAppID]*Puppet
|
|
|
|
puppetsLock sync.Mutex
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBridge() *Bridge {
|
2018-08-16 23:11:28 +02:00
|
|
|
bridge := &Bridge{
|
2018-08-28 23:40:54 +02:00
|
|
|
usersByMXID: make(map[types.MatrixUserID]*User),
|
|
|
|
usersByJID: make(map[types.WhatsAppID]*User),
|
2018-08-18 21:57:08 +02:00
|
|
|
managementRooms: make(map[types.MatrixRoomID]*User),
|
2018-08-28 23:40:54 +02:00
|
|
|
portalsByMXID: make(map[types.MatrixRoomID]*Portal),
|
|
|
|
portalsByJID: make(map[database.PortalKey]*Portal),
|
|
|
|
puppets: make(map[types.WhatsAppID]*Puppet),
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
|
2018-08-29 22:48:15 +02:00
|
|
|
var err error
|
2018-08-13 22:24:44 +02:00
|
|
|
bridge.Config, err = config.Load(*configPath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Failed to load config:", err)
|
2018-08-29 22:48:15 +02:00
|
|
|
os.Exit(10)
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
return bridge
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bridge *Bridge) Init() {
|
|
|
|
var err error
|
2018-08-16 18:20:07 +02:00
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
bridge.AS, err = bridge.Config.MakeAppService()
|
2018-08-13 22:24:44 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Failed to initialize AppService:", err)
|
2018-08-29 22:48:15 +02:00
|
|
|
os.Exit(11)
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
bridge.AS.Init()
|
|
|
|
bridge.Bot = bridge.AS.BotIntent()
|
|
|
|
|
|
|
|
bridge.Log = log.Create()
|
|
|
|
bridge.Config.Logging.Configure(bridge.Log)
|
2018-08-16 18:20:07 +02:00
|
|
|
log.DefaultLogger = bridge.Log.(*log.BasicLogger)
|
2018-08-28 23:40:54 +02:00
|
|
|
err = log.OpenFile()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Failed to open log file:", err)
|
2018-08-29 22:48:15 +02:00
|
|
|
os.Exit(12)
|
2018-08-28 23:40:54 +02:00
|
|
|
}
|
|
|
|
bridge.AS.Log = log.Sub("Matrix")
|
2018-08-16 18:20:07 +02:00
|
|
|
|
2018-08-16 23:11:28 +02:00
|
|
|
bridge.Log.Debugln("Initializing state store")
|
|
|
|
bridge.StateStore = NewAutosavingStateStore(bridge.Config.AppService.StateStore)
|
2018-08-16 18:20:07 +02:00
|
|
|
err = bridge.StateStore.Load()
|
|
|
|
if err != nil {
|
|
|
|
bridge.Log.Fatalln("Failed to load state store:", err)
|
2018-08-29 22:48:15 +02:00
|
|
|
os.Exit(13)
|
2018-08-16 18:20:07 +02:00
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
bridge.AS.StateStore = bridge.StateStore
|
2018-08-13 22:24:44 +02:00
|
|
|
|
2018-08-16 23:11:28 +02:00
|
|
|
bridge.Log.Debugln("Initializing database")
|
2018-08-13 22:24:44 +02:00
|
|
|
bridge.DB, err = database.New(bridge.Config.AppService.Database.URI)
|
|
|
|
if err != nil {
|
|
|
|
bridge.Log.Fatalln("Failed to initialize database:", err)
|
2018-08-29 22:48:15 +02:00
|
|
|
os.Exit(14)
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
2018-08-18 21:57:08 +02:00
|
|
|
bridge.Log.Debugln("Initializing Matrix event processor")
|
2018-08-28 23:40:54 +02:00
|
|
|
bridge.EventProcessor = appservice.NewEventProcessor(bridge.AS)
|
2018-08-18 21:57:08 +02:00
|
|
|
bridge.Log.Debugln("Initializing Matrix event handler")
|
|
|
|
bridge.MatrixHandler = NewMatrixHandler(bridge)
|
2018-08-28 23:40:54 +02:00
|
|
|
bridge.Formatter = NewFormatter(bridge)
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bridge *Bridge) Start() {
|
2018-08-18 21:57:08 +02:00
|
|
|
err := bridge.DB.CreateTables()
|
|
|
|
if err != nil {
|
|
|
|
bridge.Log.Fatalln("Failed to create database tables:", err)
|
2018-08-29 22:48:15 +02:00
|
|
|
os.Exit(15)
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
2018-08-16 23:11:28 +02:00
|
|
|
bridge.Log.Debugln("Starting application service HTTP server")
|
2018-08-28 23:40:54 +02:00
|
|
|
go bridge.AS.Start()
|
2018-08-16 23:11:28 +02:00
|
|
|
bridge.Log.Debugln("Starting event processor")
|
|
|
|
go bridge.EventProcessor.Start()
|
2018-08-16 18:20:07 +02:00
|
|
|
go bridge.UpdateBotProfile()
|
2018-08-18 21:57:08 +02:00
|
|
|
go bridge.StartUsers()
|
2018-08-16 18:20:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bridge *Bridge) UpdateBotProfile() {
|
2018-08-18 21:57:08 +02:00
|
|
|
bridge.Log.Debugln("Updating bot profile")
|
2018-08-16 18:20:07 +02:00
|
|
|
botConfig := bridge.Config.AppService.Bot
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if botConfig.Avatar == "remove" {
|
2018-08-30 00:10:26 +02:00
|
|
|
err = bridge.Bot.SetAvatarURL("")
|
2018-08-16 18:20:07 +02:00
|
|
|
} else if len(botConfig.Avatar) > 0 {
|
2018-08-30 00:10:26 +02:00
|
|
|
err = bridge.Bot.SetAvatarURL(botConfig.Avatar)
|
2018-08-16 18:20:07 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
bridge.Log.Warnln("Failed to update bot avatar:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if botConfig.Displayname == "remove" {
|
2018-08-30 00:10:26 +02:00
|
|
|
err = bridge.Bot.SetDisplayName("")
|
2018-08-16 18:20:07 +02:00
|
|
|
} else if len(botConfig.Avatar) > 0 {
|
2018-08-30 00:10:26 +02:00
|
|
|
err = bridge.Bot.SetDisplayName(botConfig.Displayname)
|
2018-08-16 18:20:07 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
bridge.Log.Warnln("Failed to update bot displayname:", err)
|
|
|
|
}
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
2018-08-18 21:57:08 +02:00
|
|
|
func (bridge *Bridge) StartUsers() {
|
|
|
|
for _, user := range bridge.GetAllUsers() {
|
2018-12-07 14:42:57 +01:00
|
|
|
go user.Connect(false)
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 22:24:44 +02:00
|
|
|
func (bridge *Bridge) Stop() {
|
2018-08-28 23:40:54 +02:00
|
|
|
bridge.AS.Stop()
|
2018-08-16 23:11:28 +02:00
|
|
|
bridge.EventProcessor.Stop()
|
2018-08-16 18:20:07 +02:00
|
|
|
err := bridge.StateStore.Save()
|
|
|
|
if err != nil {
|
|
|
|
bridge.Log.Warnln("Failed to save state store:", err)
|
|
|
|
}
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bridge *Bridge) Main() {
|
|
|
|
if *generateRegistration {
|
|
|
|
bridge.GenerateRegistration()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bridge.Init()
|
|
|
|
bridge.Log.Infoln("Bridge initialization complete, starting...")
|
|
|
|
bridge.Start()
|
|
|
|
bridge.Log.Infoln("Bridge started!")
|
|
|
|
|
|
|
|
c := make(chan os.Signal)
|
|
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
|
|
|
<-c
|
|
|
|
|
|
|
|
bridge.Log.Infoln("Interrupt received, stopping...")
|
|
|
|
bridge.Stop()
|
|
|
|
bridge.Log.Infoln("Bridge stopped.")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2018-08-12 21:26:05 +02:00
|
|
|
func main() {
|
2018-08-16 14:59:18 +02:00
|
|
|
flag.SetHelpTitles(
|
|
|
|
"mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.",
|
|
|
|
"mautrix-whatsapp [-h] [-c <path>] [-r <path>] [-g]")
|
2018-08-13 22:24:44 +02:00
|
|
|
err := flag.Parse()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
flag.PrintHelp()
|
|
|
|
os.Exit(1)
|
|
|
|
} else if *wantHelp {
|
|
|
|
flag.PrintHelp()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
NewBridge().Main()
|
|
|
|
}
|