2018-08-13 22:24:44 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2020-05-08 21:32:22 +02:00
|
|
|
// Copyright (C) 2020 Tulir Asokan
|
2018-08-12 21:26:05 +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
|
|
|
|
|
2018-08-13 00:00:23 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-08-13 22:24:44 +02:00
|
|
|
"os/signal"
|
2020-06-03 19:32:53 +02:00
|
|
|
"strings"
|
2018-08-28 23:40:54 +02:00
|
|
|
"sync"
|
2018-08-13 22:24:44 +02:00
|
|
|
"syscall"
|
2019-11-10 20:22:11 +01:00
|
|
|
"time"
|
2018-08-26 21:53:13 +02:00
|
|
|
|
2021-02-17 00:21:30 +01:00
|
|
|
"github.com/Rhymen/go-whatsapp"
|
|
|
|
|
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"
|
2019-05-28 20:29:43 +02:00
|
|
|
|
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"
|
|
|
|
"maunium.net/go/mautrix/event"
|
2020-05-08 21:32:22 +02:00
|
|
|
"maunium.net/go/mautrix/id"
|
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"
|
2019-11-10 20:22:11 +01:00
|
|
|
"maunium.net/go/mautrix-whatsapp/database/upgrades"
|
2018-08-13 00:00:23 +02:00
|
|
|
)
|
|
|
|
|
2020-06-03 19:32:53 +02:00
|
|
|
var (
|
|
|
|
// These are static
|
|
|
|
Name = "mautrix-whatsapp"
|
2020-06-03 19:59:44 +02:00
|
|
|
URL = "https://github.com/tulir/mautrix-whatsapp"
|
2020-06-03 19:32:53 +02:00
|
|
|
// This is changed when making a release
|
2020-12-28 17:57:30 +01:00
|
|
|
Version = "0.1.5"
|
2020-06-17 16:50:06 +02:00
|
|
|
// This is filled by init()
|
2020-06-03 19:59:44 +02:00
|
|
|
WAVersion = ""
|
2020-06-03 19:32:53 +02:00
|
|
|
// These are filled at build time with the -X linker flag
|
2020-06-03 19:59:44 +02:00
|
|
|
Tag = "unknown"
|
|
|
|
Commit = "unknown"
|
2020-06-03 19:32:53 +02:00
|
|
|
BuildTime = "unknown"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if len(Tag) > 0 && Tag[0] == 'v' {
|
|
|
|
Tag = Tag[1:]
|
|
|
|
}
|
2020-06-03 19:44:52 +02:00
|
|
|
if Tag != Version && !strings.HasSuffix(Version, "+dev") {
|
|
|
|
Version += "+dev"
|
|
|
|
}
|
2020-06-03 19:59:44 +02:00
|
|
|
WAVersion = strings.FieldsFunc(Version, func(r rune) bool { return r == '-' || r == '+' })[0]
|
2020-06-03 19:32:53 +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()
|
2019-08-24 23:34:58 +02:00
|
|
|
|
2019-01-21 22:55:16 +01: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()
|
2020-06-03 19:32:53 +02:00
|
|
|
var version = flag.MakeFull("v", "version", "View bridge version and quit.", "false").Bool()
|
2019-05-28 20:29:43 +02:00
|
|
|
var ignoreUnsupportedDatabase = flag.Make().LongKey("ignore-unsupported-database").Usage("Run even if database is too new").Default("false").Bool()
|
2019-08-25 18:26:04 +02:00
|
|
|
var migrateFrom = flag.Make().LongKey("migrate-db").Usage("Source database type and URI to migrate from.").Bool()
|
2018-08-13 22:24:44 +02:00
|
|
|
var wantHelp, _ = flag.MakeHelpFlag()
|
|
|
|
|
|
|
|
func (bridge *Bridge) GenerateRegistration() {
|
|
|
|
reg, err := bridge.Config.NewRegistration()
|
|
|
|
if err != nil {
|
2020-06-17 16:50:06 +02:00
|
|
|
_, _ = fmt.Fprintln(os.Stderr, "Failed to generate registration:", err)
|
2018-08-13 22:24:44 +02:00
|
|
|
os.Exit(20)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = reg.Save(*registrationPath)
|
|
|
|
if err != nil {
|
2020-06-17 16:50:06 +02:00
|
|
|
_, _ = fmt.Fprintln(os.Stderr, "Failed to save registration:", err)
|
2018-08-13 22:24:44 +02:00
|
|
|
os.Exit(21)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bridge.Config.Save(*configPath)
|
|
|
|
if err != nil {
|
2020-06-17 16:50:06 +02:00
|
|
|
_, _ = fmt.Fprintln(os.Stderr, "Failed to save config:", err)
|
2018-08-13 22:24:44 +02:00
|
|
|
os.Exit(22)
|
|
|
|
}
|
2019-04-06 21:41:15 +02:00
|
|
|
fmt.Println("Registration generated. Add the path to the registration to your Synapse config, restart it, then start the bridge.")
|
2018-08-13 22:24:44 +02:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2019-08-25 18:26:04 +02:00
|
|
|
func (bridge *Bridge) MigrateDatabase() {
|
2021-01-25 20:01:54 +01:00
|
|
|
oldDB, err := database.New(flag.Arg(0), flag.Arg(1), bridge.Log)
|
2019-08-25 18:26:04 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to open old database:", err)
|
|
|
|
os.Exit(30)
|
|
|
|
}
|
|
|
|
err = oldDB.Init()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to upgrade old database:", err)
|
|
|
|
os.Exit(31)
|
|
|
|
}
|
|
|
|
|
2021-01-25 20:01:54 +01:00
|
|
|
newDB, err := database.New(bridge.Config.AppService.Database.Type, bridge.Config.AppService.Database.URI, bridge.Log)
|
2019-08-25 18:26:04 +02:00
|
|
|
if err != nil {
|
2020-07-30 20:41:04 +02:00
|
|
|
fmt.Println("Failed to open new database:", err)
|
2019-08-25 18:26:04 +02:00
|
|
|
os.Exit(32)
|
|
|
|
}
|
|
|
|
err = newDB.Init()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to upgrade new database:", err)
|
|
|
|
os.Exit(33)
|
|
|
|
}
|
|
|
|
|
|
|
|
database.Migrate(oldDB, newDB)
|
|
|
|
}
|
|
|
|
|
2018-08-13 22:24:44 +02:00
|
|
|
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
|
2019-08-25 15:29:35 +02:00
|
|
|
StateStore *database.SQLStateStore
|
2020-02-09 19:32:14 +01:00
|
|
|
Provisioning *ProvisioningAPI
|
2018-08-28 23:40:54 +02:00
|
|
|
Bot *appservice.IntentAPI
|
|
|
|
Formatter *Formatter
|
2019-11-10 20:22:11 +01:00
|
|
|
Relaybot *User
|
2020-05-09 01:03:59 +02:00
|
|
|
Crypto Crypto
|
2020-09-24 14:25:36 +02:00
|
|
|
Metrics *MetricsHandler
|
2018-08-28 23:40:54 +02:00
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
usersByMXID map[id.UserID]*User
|
2021-02-17 00:21:30 +01:00
|
|
|
usersByJID map[whatsapp.JID]*User
|
2018-08-28 23:40:54 +02:00
|
|
|
usersLock sync.Mutex
|
2020-05-08 21:32:22 +02:00
|
|
|
managementRooms map[id.RoomID]*User
|
2018-08-28 23:40:54 +02:00
|
|
|
managementRoomsLock sync.Mutex
|
2020-05-08 21:32:22 +02:00
|
|
|
portalsByMXID map[id.RoomID]*Portal
|
2018-08-28 23:40:54 +02:00
|
|
|
portalsByJID map[database.PortalKey]*Portal
|
|
|
|
portalsLock sync.Mutex
|
2021-02-17 00:21:30 +01:00
|
|
|
puppets map[whatsapp.JID]*Puppet
|
2020-05-08 21:32:22 +02:00
|
|
|
puppetsByCustomMXID map[id.UserID]*Puppet
|
2018-08-28 23:40:54 +02:00
|
|
|
puppetsLock sync.Mutex
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 01:03:59 +02:00
|
|
|
type Crypto interface {
|
|
|
|
HandleMemberEvent(*event.Event)
|
|
|
|
Decrypt(*event.Event) (*event.Event, error)
|
|
|
|
Encrypt(id.RoomID, event.Type, event.Content) (*event.EncryptedEventContent, error)
|
2020-09-24 14:25:36 +02:00
|
|
|
WaitForSession(id.RoomID, id.SenderKey, id.SessionID, time.Duration) bool
|
2020-10-05 21:32:15 +02:00
|
|
|
ResetSession(id.RoomID)
|
2020-05-09 19:07:21 +02:00
|
|
|
Init() error
|
2020-05-09 01:03:59 +02:00
|
|
|
Start()
|
|
|
|
Stop()
|
|
|
|
}
|
|
|
|
|
2018-08-13 22:24:44 +02:00
|
|
|
func NewBridge() *Bridge {
|
2018-08-16 23:11:28 +02:00
|
|
|
bridge := &Bridge{
|
2020-05-08 21:32:22 +02:00
|
|
|
usersByMXID: make(map[id.UserID]*User),
|
2021-02-17 00:21:30 +01:00
|
|
|
usersByJID: make(map[whatsapp.JID]*User),
|
2020-05-08 21:32:22 +02:00
|
|
|
managementRooms: make(map[id.RoomID]*User),
|
|
|
|
portalsByMXID: make(map[id.RoomID]*Portal),
|
2019-05-24 01:33:26 +02:00
|
|
|
portalsByJID: make(map[database.PortalKey]*Portal),
|
2021-02-17 00:21:30 +01:00
|
|
|
puppets: make(map[whatsapp.JID]*Puppet),
|
2020-05-08 21:32:22 +02:00
|
|
|
puppetsByCustomMXID: make(map[id.UserID]*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 {
|
2020-06-17 16:50:06 +02:00
|
|
|
_, _ = 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
|
|
|
|
}
|
|
|
|
|
2019-08-24 23:34:58 +02:00
|
|
|
func (bridge *Bridge) ensureConnection() {
|
|
|
|
for {
|
2020-05-08 21:32:22 +02:00
|
|
|
resp, err := bridge.Bot.Whoami()
|
2019-08-24 23:34:58 +02:00
|
|
|
if err != nil {
|
|
|
|
if httpErr, ok := err.(mautrix.HTTPError); ok && httpErr.RespError != nil && httpErr.RespError.ErrCode == "M_UNKNOWN_ACCESS_TOKEN" {
|
|
|
|
bridge.Log.Fatalln("Access token invalid. Is the registration installed in your homeserver correctly?")
|
|
|
|
os.Exit(16)
|
|
|
|
}
|
|
|
|
bridge.Log.Errorfln("Failed to connect to homeserver: %v. Retrying in 10 seconds...", err)
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
} else if resp.UserID != bridge.Bot.UserID {
|
|
|
|
bridge.Log.Fatalln("Unexpected user ID in whoami call: got %s, expected %s", resp.UserID, bridge.Bot.UserID)
|
|
|
|
os.Exit(17)
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 22:24:44 +02:00
|
|
|
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 {
|
2019-08-24 21:45:16 +02:00
|
|
|
_, _ = 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
|
|
|
}
|
2019-08-24 21:45:16 +02:00
|
|
|
_, _ = bridge.AS.Init()
|
2018-08-28 23:40:54 +02:00
|
|
|
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)
|
2019-08-24 21:45:16 +02:00
|
|
|
if len(bridge.Config.Logging.FileNameFormat) > 0 {
|
|
|
|
err = log.OpenFile()
|
|
|
|
if err != nil {
|
|
|
|
_, _ = fmt.Fprintln(os.Stderr, "Failed to open log file:", err)
|
|
|
|
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
|
|
|
|
2020-11-02 15:16:36 +01:00
|
|
|
bridge.Log.Debugln("Initializing database connection")
|
2021-01-25 20:01:54 +01:00
|
|
|
bridge.DB, err = database.New(bridge.Config.AppService.Database.Type, bridge.Config.AppService.Database.URI, bridge.Log)
|
2020-11-02 15:16:36 +01:00
|
|
|
if err != nil {
|
|
|
|
bridge.Log.Fatalln("Failed to initialize database connection:", err)
|
2018-08-29 22:48:15 +02:00
|
|
|
os.Exit(14)
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
2019-08-25 16:25:19 +02:00
|
|
|
if len(bridge.Config.AppService.StateStore) > 0 && bridge.Config.AppService.StateStore != "./mx-state.json" {
|
|
|
|
version, err := upgrades.GetVersion(bridge.DB.DB)
|
|
|
|
if version < 0 && err == nil {
|
|
|
|
bridge.Log.Fatalln("Non-standard state store path. Please move the state store to ./mx-state.json " +
|
|
|
|
"and update the config. The state store will be migrated into the db on the next launch.")
|
|
|
|
os.Exit(18)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 15:29:35 +02:00
|
|
|
bridge.Log.Debugln("Initializing state store")
|
|
|
|
bridge.StateStore = database.NewSQLStateStore(bridge.DB)
|
|
|
|
bridge.AS.StateStore = bridge.StateStore
|
|
|
|
|
2019-05-17 22:33:27 +02:00
|
|
|
bridge.DB.SetMaxOpenConns(bridge.Config.AppService.Database.MaxOpenConns)
|
|
|
|
bridge.DB.SetMaxIdleConns(bridge.Config.AppService.Database.MaxIdleConns)
|
|
|
|
|
2020-02-09 19:32:14 +01:00
|
|
|
ss := bridge.Config.AppService.Provisioning.SharedSecret
|
|
|
|
if len(ss) > 0 && ss != "disable" {
|
|
|
|
bridge.Provisioning = &ProvisioningAPI{bridge: bridge}
|
|
|
|
}
|
|
|
|
|
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)
|
2020-05-09 19:07:21 +02:00
|
|
|
bridge.Crypto = NewCryptoHelper(bridge)
|
2020-06-17 16:50:06 +02:00
|
|
|
bridge.Metrics = NewMetricsHandler(bridge.Config.Metrics.Listen, bridge.Log.Sub("Metrics"), bridge.DB)
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bridge *Bridge) Start() {
|
2020-11-02 15:16:36 +01:00
|
|
|
bridge.Log.Debugln("Running database upgrades")
|
2019-08-25 15:29:35 +02:00
|
|
|
err := bridge.DB.Init()
|
2020-11-02 15:16:36 +01:00
|
|
|
if err != nil && (err != upgrades.UnsupportedDatabaseVersion || !*ignoreUnsupportedDatabase) {
|
2019-05-16 19:14:32 +02:00
|
|
|
bridge.Log.Fatalln("Failed to initialize database:", err)
|
2018-08-29 22:48:15 +02:00
|
|
|
os.Exit(15)
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
2020-07-23 18:18:24 +02:00
|
|
|
bridge.Log.Debugln("Checking connection to homeserver")
|
|
|
|
bridge.ensureConnection()
|
2020-05-09 19:07:21 +02:00
|
|
|
if bridge.Crypto != nil {
|
|
|
|
err := bridge.Crypto.Init()
|
|
|
|
if err != nil {
|
|
|
|
bridge.Log.Fatalln("Error initializing end-to-bridge encryption:", err)
|
|
|
|
os.Exit(19)
|
|
|
|
}
|
|
|
|
}
|
2020-02-09 19:32:14 +01:00
|
|
|
if bridge.Provisioning != nil {
|
|
|
|
bridge.Log.Debugln("Initializing provisioning API")
|
|
|
|
bridge.Provisioning.Init()
|
|
|
|
}
|
2019-11-10 20:22:11 +01:00
|
|
|
bridge.LoadRelaybot()
|
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()
|
2020-05-21 20:56:46 +02:00
|
|
|
if bridge.Crypto != nil {
|
|
|
|
go bridge.Crypto.Start()
|
|
|
|
}
|
2018-08-18 21:57:08 +02:00
|
|
|
go bridge.StartUsers()
|
2020-06-17 16:50:06 +02:00
|
|
|
if bridge.Config.Metrics.Enabled {
|
|
|
|
go bridge.Metrics.Start()
|
|
|
|
}
|
2020-06-15 19:28:04 +02:00
|
|
|
|
|
|
|
if bridge.Config.Bridge.ResendBridgeInfo {
|
|
|
|
go bridge.ResendBridgeInfo()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bridge *Bridge) ResendBridgeInfo() {
|
|
|
|
bridge.Config.Bridge.ResendBridgeInfo = false
|
|
|
|
err := bridge.Config.Save(*configPath)
|
|
|
|
if err != nil {
|
|
|
|
bridge.Log.Errorln("Failed to save config after setting resend_bridge_info to false:", err)
|
|
|
|
}
|
|
|
|
bridge.Log.Infoln("Re-sending bridge info state event to all portals")
|
|
|
|
for _, portal := range bridge.GetAllPortals() {
|
|
|
|
portal.UpdateBridgeInfo()
|
|
|
|
}
|
|
|
|
bridge.Log.Infoln("Finished re-sending bridge info state events")
|
2018-08-16 18:20:07 +02:00
|
|
|
}
|
|
|
|
|
2019-11-10 20:22:11 +01:00
|
|
|
func (bridge *Bridge) LoadRelaybot() {
|
|
|
|
if !bridge.Config.Bridge.Relaybot.Enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bridge.Relaybot = bridge.GetUserByMXID("relaybot")
|
|
|
|
if bridge.Relaybot.HasSession() {
|
|
|
|
bridge.Log.Debugln("Relaybot is enabled")
|
|
|
|
} else {
|
|
|
|
bridge.Log.Debugln("Relaybot is enabled, but not logged in")
|
|
|
|
}
|
|
|
|
bridge.Relaybot.ManagementRoom = bridge.Config.Bridge.Relaybot.ManagementRoom
|
|
|
|
bridge.Relaybot.IsRelaybot = true
|
|
|
|
bridge.Relaybot.Connect(false)
|
|
|
|
}
|
|
|
|
|
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
|
2020-05-08 21:32:22 +02:00
|
|
|
var mxc id.ContentURI
|
2018-08-16 18:20:07 +02:00
|
|
|
if botConfig.Avatar == "remove" {
|
2020-05-08 21:32:22 +02:00
|
|
|
err = bridge.Bot.SetAvatarURL(mxc)
|
2018-08-16 18:20:07 +02:00
|
|
|
} else if len(botConfig.Avatar) > 0 {
|
2020-05-08 21:32:22 +02:00
|
|
|
mxc, err = id.ParseContentURI(botConfig.Avatar)
|
|
|
|
if err == nil {
|
|
|
|
err = bridge.Bot.SetAvatarURL(mxc)
|
|
|
|
}
|
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() {
|
2019-03-06 16:33:42 +01:00
|
|
|
bridge.Log.Debugln("Starting users")
|
2018-08-18 21:57:08 +02:00
|
|
|
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
|
|
|
}
|
2019-05-24 01:33:26 +02:00
|
|
|
bridge.Log.Debugln("Starting custom puppets")
|
2019-05-31 22:07:33 +02:00
|
|
|
for _, loopuppet := range bridge.GetAllPuppetsWithCustomMXID() {
|
|
|
|
go func(puppet *Puppet) {
|
2019-05-24 01:33:26 +02:00
|
|
|
puppet.log.Debugln("Starting custom puppet", puppet.CustomMXID)
|
2021-02-11 19:47:10 +01:00
|
|
|
err := puppet.StartCustomMXID(true)
|
2019-05-24 01:33:26 +02:00
|
|
|
if err != nil {
|
|
|
|
puppet.log.Errorln("Failed to start custom puppet:", err)
|
|
|
|
}
|
2019-05-31 22:07:33 +02:00
|
|
|
}(loopuppet)
|
2019-05-24 01:33:26 +02:00
|
|
|
}
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 22:24:44 +02:00
|
|
|
func (bridge *Bridge) Stop() {
|
2020-05-21 20:56:46 +02:00
|
|
|
if bridge.Crypto != nil {
|
|
|
|
bridge.Crypto.Stop()
|
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
bridge.AS.Stop()
|
2020-06-17 16:50:06 +02:00
|
|
|
bridge.Metrics.Stop()
|
2018-08-16 23:11:28 +02:00
|
|
|
bridge.EventProcessor.Stop()
|
2019-05-23 18:16:29 +02:00
|
|
|
for _, user := range bridge.usersByJID {
|
2019-05-23 21:57:19 +02:00
|
|
|
if user.Conn == nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-05-23 18:16:29 +02:00
|
|
|
bridge.Log.Debugln("Disconnecting", user.MXID)
|
2021-02-18 22:36:14 +01:00
|
|
|
err := user.Conn.Disconnect()
|
2019-05-23 18:16:29 +02:00
|
|
|
if err != nil {
|
|
|
|
bridge.Log.Errorfln("Error while disconnecting %s: %v", user.MXID, err)
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bridge *Bridge) Main() {
|
|
|
|
if *generateRegistration {
|
|
|
|
bridge.GenerateRegistration()
|
|
|
|
return
|
2019-08-25 18:26:04 +02:00
|
|
|
} else if *migrateFrom {
|
|
|
|
bridge.MigrateDatabase()
|
|
|
|
return
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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.",
|
2019-08-25 18:26:04 +02:00
|
|
|
"mautrix-whatsapp [-h] [-c <path>] [-r <path>] [-g] [--migrate-db <source type> <source uri>]")
|
2018-08-13 22:24:44 +02:00
|
|
|
err := flag.Parse()
|
|
|
|
if err != nil {
|
2020-06-17 16:50:06 +02:00
|
|
|
_, _ = fmt.Fprintln(os.Stderr, err)
|
2018-08-13 22:24:44 +02:00
|
|
|
flag.PrintHelp()
|
|
|
|
os.Exit(1)
|
|
|
|
} else if *wantHelp {
|
|
|
|
flag.PrintHelp()
|
|
|
|
os.Exit(0)
|
2020-06-03 19:47:13 +02:00
|
|
|
} else if *version {
|
|
|
|
if Tag == Version {
|
|
|
|
fmt.Printf("%s %s (%s)\n", Name, Tag, BuildTime)
|
|
|
|
} else if len(Commit) > 8 {
|
|
|
|
fmt.Printf("%s %s.%s (%s)\n", Name, Version, Commit[:8], BuildTime)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%s %s.unknown\n", Name, Version)
|
|
|
|
}
|
|
|
|
return
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NewBridge().Main()
|
|
|
|
}
|