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-13 00:00:23 +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 config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-08-16 23:11:28 +02:00
|
|
|
"strconv"
|
2018-08-26 21:53:13 +02:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
2018-08-18 21:57:08 +02:00
|
|
|
"github.com/Rhymen/go-whatsapp"
|
2019-01-11 20:17:31 +01:00
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
"maunium.net/go/mautrix/event"
|
|
|
|
"maunium.net/go/mautrix/id"
|
2018-08-13 00:00:23 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type BridgeConfig struct {
|
2018-08-16 23:11:28 +02:00
|
|
|
UsernameTemplate string `yaml:"username_template"`
|
|
|
|
DisplaynameTemplate string `yaml:"displayname_template"`
|
2019-08-10 14:24:53 +02:00
|
|
|
CommunityTemplate string `yaml:"community_template"`
|
2018-08-16 23:11:28 +02:00
|
|
|
|
2019-05-17 22:53:57 +02:00
|
|
|
ConnectionTimeout int `yaml:"connection_timeout"`
|
2020-05-24 16:28:30 +02:00
|
|
|
FetchMessageOnTimeout bool `yaml:"fetch_message_on_timeout"`
|
2020-05-25 10:17:47 +02:00
|
|
|
DeliveryReceipts bool `yaml:"delivery_receipts"`
|
2019-05-17 22:53:57 +02:00
|
|
|
MaxConnectionAttempts int `yaml:"max_connection_attempts"`
|
2019-05-28 13:09:49 +02:00
|
|
|
ConnectionRetryDelay int `yaml:"connection_retry_delay"`
|
2019-05-17 22:53:57 +02:00
|
|
|
ReportConnectionRetry bool `yaml:"report_connection_retry"`
|
2021-02-05 18:26:26 +01:00
|
|
|
AggressiveReconnect bool `yaml:"aggressive_reconnect"`
|
2019-08-30 19:57:08 +02:00
|
|
|
ChatListWait int `yaml:"chat_list_wait"`
|
|
|
|
PortalSyncWait int `yaml:"portal_sync_wait"`
|
2020-07-23 19:10:21 +02:00
|
|
|
UserMessageBuffer int `yaml:"user_message_buffer"`
|
|
|
|
PortalMessageBuffer int `yaml:"portal_message_buffer"`
|
2019-05-16 17:08:30 +02:00
|
|
|
|
2019-08-24 21:42:03 +02:00
|
|
|
CallNotices struct {
|
|
|
|
Start bool `yaml:"start"`
|
|
|
|
End bool `yaml:"end"`
|
|
|
|
} `yaml:"call_notices"`
|
|
|
|
|
2021-04-19 21:14:32 +02:00
|
|
|
InitialChatSync int `yaml:"initial_chat_sync_count"`
|
|
|
|
InitialHistoryFill int `yaml:"initial_history_fill_count"`
|
|
|
|
HistoryDisableNotifs bool `yaml:"initial_history_disable_notifications"`
|
|
|
|
RecoverChatSync int `yaml:"recovery_chat_sync_count"`
|
|
|
|
RecoverHistory bool `yaml:"recovery_history_backfill"`
|
|
|
|
ChatMetaSync bool `yaml:"chat_meta_sync"`
|
|
|
|
UserAvatarSync bool `yaml:"user_avatar_sync"`
|
|
|
|
BridgeMatrixLeave bool `yaml:"bridge_matrix_leave"`
|
|
|
|
SyncChatMaxAge int64 `yaml:"sync_max_chat_age"`
|
2019-05-22 15:46:18 +02:00
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
SyncWithCustomPuppets bool `yaml:"sync_with_custom_puppets"`
|
2020-08-22 12:07:55 +02:00
|
|
|
SyncDirectChatList bool `yaml:"sync_direct_chat_list"`
|
2020-07-10 15:26:55 +02:00
|
|
|
DefaultBridgeReceipts bool `yaml:"default_bridge_receipts"`
|
|
|
|
DefaultBridgePresence bool `yaml:"default_bridge_presence"`
|
2020-05-08 21:32:22 +02:00
|
|
|
LoginSharedSecret string `yaml:"login_shared_secret"`
|
2019-05-24 01:33:26 +02:00
|
|
|
|
2021-04-19 21:14:32 +02:00
|
|
|
InviteOwnPuppetForBackfilling bool `yaml:"invite_own_puppet_for_backfilling"`
|
|
|
|
PrivateChatPortalMeta bool `yaml:"private_chat_portal_meta"`
|
|
|
|
BridgeNotices bool `yaml:"bridge_notices"`
|
|
|
|
ResendBridgeInfo bool `yaml:"resend_bridge_info"`
|
|
|
|
MuteBridging bool `yaml:"mute_bridging"`
|
|
|
|
ArchiveTag string `yaml:"archive_tag"`
|
2021-04-20 15:32:23 +02:00
|
|
|
PinnedTag string `yaml:"pinned_tag"`
|
2021-04-29 10:57:05 +02:00
|
|
|
TagOnlyOnCreate bool `yaml:"tag_only_on_create"`
|
2021-05-18 13:16:35 +02:00
|
|
|
MarkReadOnlyOnCreate bool `yaml:"mark_read_only_on_create"`
|
2021-06-01 14:28:15 +02:00
|
|
|
EnableStatusBroadcast bool `yaml:"enable_status_broadcast"`
|
2019-06-01 19:03:29 +02:00
|
|
|
|
2020-06-10 14:26:14 +02:00
|
|
|
WhatsappThumbnail bool `yaml:"whatsapp_thumbnail"`
|
|
|
|
|
2019-08-10 14:24:53 +02:00
|
|
|
AllowUserInvite bool `yaml:"allow_user_invite"`
|
2019-07-16 11:16:17 +02:00
|
|
|
|
2018-08-16 23:11:28 +02:00
|
|
|
CommandPrefix string `yaml:"command_prefix"`
|
|
|
|
|
2021-10-28 12:58:20 +02:00
|
|
|
ManagementRoomText struct {
|
|
|
|
Welcome string `yaml:"welcome"`
|
|
|
|
WelcomeConnected string `yaml:"welcome_connected"`
|
|
|
|
WelcomeUnconnected string `yaml:"welcome_unconnected"`
|
|
|
|
AdditionalHelp string `yaml:"additional_help"`
|
|
|
|
} `yaml:"management_room_text"`
|
|
|
|
|
2020-05-09 01:03:59 +02:00
|
|
|
Encryption struct {
|
|
|
|
Allow bool `yaml:"allow"`
|
|
|
|
Default bool `yaml:"default"`
|
2020-08-05 13:58:46 +02:00
|
|
|
|
|
|
|
KeySharing struct {
|
|
|
|
Allow bool `yaml:"allow"`
|
|
|
|
RequireCrossSigning bool `yaml:"require_cross_signing"`
|
|
|
|
RequireVerification bool `yaml:"require_verification"`
|
|
|
|
} `yaml:"key_sharing"`
|
2020-05-09 01:03:59 +02:00
|
|
|
} `yaml:"encryption"`
|
|
|
|
|
2018-08-16 23:11:28 +02:00
|
|
|
Permissions PermissionConfig `yaml:"permissions"`
|
|
|
|
|
2019-11-10 20:22:11 +01:00
|
|
|
Relaybot RelaybotConfig `yaml:"relaybot"`
|
|
|
|
|
2018-08-16 14:59:18 +02:00
|
|
|
usernameTemplate *template.Template `yaml:"-"`
|
|
|
|
displaynameTemplate *template.Template `yaml:"-"`
|
2019-08-10 14:24:53 +02:00
|
|
|
communityTemplate *template.Template `yaml:"-"`
|
2018-08-13 00:00:23 +02:00
|
|
|
}
|
|
|
|
|
2019-05-17 22:53:57 +02:00
|
|
|
func (bc *BridgeConfig) setDefaults() {
|
|
|
|
bc.ConnectionTimeout = 20
|
2020-05-25 10:17:47 +02:00
|
|
|
bc.FetchMessageOnTimeout = false
|
|
|
|
bc.DeliveryReceipts = false
|
2019-05-17 22:53:57 +02:00
|
|
|
bc.MaxConnectionAttempts = 3
|
2019-05-28 13:09:49 +02:00
|
|
|
bc.ConnectionRetryDelay = -1
|
2019-05-17 22:53:57 +02:00
|
|
|
bc.ReportConnectionRetry = true
|
2019-08-30 19:57:08 +02:00
|
|
|
bc.ChatListWait = 30
|
|
|
|
bc.PortalSyncWait = 600
|
2020-07-23 19:10:21 +02:00
|
|
|
bc.UserMessageBuffer = 1024
|
|
|
|
bc.PortalMessageBuffer = 128
|
2019-05-22 15:46:18 +02:00
|
|
|
|
2019-08-24 21:42:03 +02:00
|
|
|
bc.CallNotices.Start = true
|
|
|
|
bc.CallNotices.End = true
|
|
|
|
|
2019-05-22 15:46:18 +02:00
|
|
|
bc.InitialChatSync = 10
|
|
|
|
bc.InitialHistoryFill = 20
|
|
|
|
bc.RecoverChatSync = -1
|
|
|
|
bc.RecoverHistory = true
|
2021-02-09 22:41:14 +01:00
|
|
|
bc.ChatMetaSync = true
|
|
|
|
bc.UserAvatarSync = true
|
2021-02-10 21:15:23 +01:00
|
|
|
bc.BridgeMatrixLeave = true
|
2019-05-23 19:25:46 +02:00
|
|
|
bc.SyncChatMaxAge = 259200
|
2019-05-24 01:33:26 +02:00
|
|
|
|
|
|
|
bc.SyncWithCustomPuppets = true
|
2020-07-10 15:26:55 +02:00
|
|
|
bc.DefaultBridgePresence = true
|
|
|
|
bc.DefaultBridgeReceipts = true
|
2019-12-30 19:21:04 +01:00
|
|
|
bc.LoginSharedSecret = ""
|
2019-06-01 19:03:29 +02:00
|
|
|
|
|
|
|
bc.InviteOwnPuppetForBackfilling = true
|
|
|
|
bc.PrivateChatPortalMeta = false
|
2021-02-26 15:10:57 +01:00
|
|
|
bc.BridgeNotices = true
|
2021-06-01 14:28:15 +02:00
|
|
|
bc.EnableStatusBroadcast = true
|
2021-10-28 12:58:20 +02:00
|
|
|
|
|
|
|
bc.ManagementRoomText.Welcome = "Hello, I'm a WhatsApp bridge bot."
|
|
|
|
bc.ManagementRoomText.WelcomeConnected = "Use `help` for help."
|
|
|
|
bc.ManagementRoomText.WelcomeUnconnected = "Use `help` for help or `login` to log in."
|
2019-05-17 22:53:57 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 14:59:18 +02:00
|
|
|
type umBridgeConfig BridgeConfig
|
|
|
|
|
|
|
|
func (bc *BridgeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
err := unmarshal((*umBridgeConfig)(bc))
|
2018-08-13 00:00:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:59:18 +02:00
|
|
|
bc.usernameTemplate, err = template.New("username").Parse(bc.UsernameTemplate)
|
2018-08-13 00:00:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:59:18 +02:00
|
|
|
bc.displaynameTemplate, err = template.New("displayname").Parse(bc.DisplaynameTemplate)
|
2019-08-10 14:24:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bc.CommunityTemplate) > 0 {
|
|
|
|
bc.communityTemplate, err = template.New("community").Parse(bc.CommunityTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-08-13 00:00:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type UsernameTemplateArgs struct {
|
2020-05-08 21:32:22 +02:00
|
|
|
UserID id.UserID
|
2018-08-13 00:00:23 +02:00
|
|
|
}
|
|
|
|
|
2018-09-01 22:38:03 +02:00
|
|
|
func (bc BridgeConfig) FormatDisplayname(contact whatsapp.Contact) (string, int8) {
|
2018-08-13 00:00:23 +02:00
|
|
|
var buf bytes.Buffer
|
2021-02-18 22:36:14 +01:00
|
|
|
if index := strings.IndexRune(contact.JID, '@'); index > 0 {
|
|
|
|
contact.JID = "+" + contact.JID[:index]
|
2018-08-26 21:50:28 +02:00
|
|
|
}
|
2018-08-18 21:57:08 +02:00
|
|
|
bc.displaynameTemplate.Execute(&buf, contact)
|
2018-09-01 22:38:03 +02:00
|
|
|
var quality int8
|
|
|
|
switch {
|
2021-09-01 14:49:19 +02:00
|
|
|
case len(contact.Notify) > 0 || len(contact.VName) > 0:
|
2018-09-01 22:38:03 +02:00
|
|
|
quality = 3
|
|
|
|
case len(contact.Name) > 0 || len(contact.Short) > 0:
|
|
|
|
quality = 2
|
2021-02-18 22:36:14 +01:00
|
|
|
case len(contact.JID) > 0:
|
2018-09-01 22:38:03 +02:00
|
|
|
quality = 1
|
|
|
|
default:
|
|
|
|
quality = 0
|
|
|
|
}
|
|
|
|
return buf.String(), quality
|
2018-08-13 00:00:23 +02:00
|
|
|
}
|
|
|
|
|
2021-02-17 00:21:30 +01:00
|
|
|
func (bc BridgeConfig) FormatUsername(userID whatsapp.JID) string {
|
2018-08-13 00:00:23 +02:00
|
|
|
var buf bytes.Buffer
|
2018-08-28 23:40:54 +02:00
|
|
|
bc.usernameTemplate.Execute(&buf, userID)
|
2018-08-13 00:00:23 +02:00
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2019-08-10 14:24:53 +02:00
|
|
|
type CommunityTemplateArgs struct {
|
|
|
|
Localpart string
|
|
|
|
Server string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc BridgeConfig) EnableCommunities() bool {
|
|
|
|
return bc.communityTemplate != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc BridgeConfig) FormatCommunity(localpart, server string) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
bc.communityTemplate.Execute(&buf, CommunityTemplateArgs{localpart, server})
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2018-08-16 23:11:28 +02:00
|
|
|
type PermissionConfig map[string]PermissionLevel
|
|
|
|
|
|
|
|
type PermissionLevel int
|
|
|
|
|
|
|
|
const (
|
2019-11-10 20:22:11 +01:00
|
|
|
PermissionLevelDefault PermissionLevel = 0
|
|
|
|
PermissionLevelRelaybot PermissionLevel = 5
|
|
|
|
PermissionLevelUser PermissionLevel = 10
|
|
|
|
PermissionLevelAdmin PermissionLevel = 100
|
2018-08-16 23:11:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (pc *PermissionConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
rawPC := make(map[string]string)
|
|
|
|
err := unmarshal(&rawPC)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if *pc == nil {
|
|
|
|
*pc = make(map[string]PermissionLevel)
|
|
|
|
}
|
|
|
|
for key, value := range rawPC {
|
|
|
|
switch strings.ToLower(value) {
|
2019-11-10 20:22:11 +01:00
|
|
|
case "relaybot":
|
|
|
|
(*pc)[key] = PermissionLevelRelaybot
|
2018-08-16 23:11:28 +02:00
|
|
|
case "user":
|
|
|
|
(*pc)[key] = PermissionLevelUser
|
|
|
|
case "admin":
|
|
|
|
(*pc)[key] = PermissionLevelAdmin
|
|
|
|
default:
|
|
|
|
val, err := strconv.Atoi(value)
|
|
|
|
if err != nil {
|
|
|
|
(*pc)[key] = PermissionLevelDefault
|
|
|
|
} else {
|
|
|
|
(*pc)[key] = PermissionLevel(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *PermissionConfig) MarshalYAML() (interface{}, error) {
|
|
|
|
if *pc == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
rawPC := make(map[string]string)
|
|
|
|
for key, value := range *pc {
|
|
|
|
switch value {
|
2019-11-10 20:22:11 +01:00
|
|
|
case PermissionLevelRelaybot:
|
|
|
|
rawPC[key] = "relaybot"
|
2018-08-16 23:11:28 +02:00
|
|
|
case PermissionLevelUser:
|
|
|
|
rawPC[key] = "user"
|
|
|
|
case PermissionLevelAdmin:
|
|
|
|
rawPC[key] = "admin"
|
|
|
|
default:
|
|
|
|
rawPC[key] = strconv.Itoa(int(value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rawPC, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
func (pc PermissionConfig) IsRelaybotWhitelisted(userID id.UserID) bool {
|
2019-11-10 20:22:11 +01:00
|
|
|
return pc.GetPermissionLevel(userID) >= PermissionLevelRelaybot
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
func (pc PermissionConfig) IsWhitelisted(userID id.UserID) bool {
|
2019-11-10 20:22:11 +01:00
|
|
|
return pc.GetPermissionLevel(userID) >= PermissionLevelUser
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
func (pc PermissionConfig) IsAdmin(userID id.UserID) bool {
|
2019-11-10 20:22:11 +01:00
|
|
|
return pc.GetPermissionLevel(userID) >= PermissionLevelAdmin
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
func (pc PermissionConfig) GetPermissionLevel(userID id.UserID) PermissionLevel {
|
|
|
|
permissions, ok := pc[string(userID)]
|
2018-08-16 23:11:28 +02:00
|
|
|
if ok {
|
|
|
|
return permissions
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
_, homeserver, _ := userID.Parse()
|
2018-08-16 23:11:28 +02:00
|
|
|
permissions, ok = pc[homeserver]
|
|
|
|
if len(homeserver) > 0 && ok {
|
|
|
|
return permissions
|
|
|
|
}
|
|
|
|
|
|
|
|
permissions, ok = pc["*"]
|
|
|
|
if ok {
|
|
|
|
return permissions
|
|
|
|
}
|
|
|
|
|
|
|
|
return PermissionLevelDefault
|
|
|
|
}
|
2019-11-10 20:22:11 +01:00
|
|
|
|
|
|
|
type RelaybotConfig struct {
|
2020-05-08 21:32:22 +02:00
|
|
|
Enabled bool `yaml:"enabled"`
|
|
|
|
ManagementRoom id.RoomID `yaml:"management"`
|
|
|
|
InviteUsers []id.UserID `yaml:"invites"`
|
2019-11-10 20:22:11 +01:00
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
MessageFormats map[event.MessageType]string `yaml:"message_formats"`
|
|
|
|
messageTemplates *template.Template `yaml:"-"`
|
2019-11-10 20:22:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type umRelaybotConfig RelaybotConfig
|
|
|
|
|
|
|
|
func (rc *RelaybotConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
err := unmarshal((*umRelaybotConfig)(rc))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rc.messageTemplates = template.New("messageTemplates")
|
|
|
|
for key, format := range rc.MessageFormats {
|
|
|
|
_, err := rc.messageTemplates.New(string(key)).Parse(format)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Sender struct {
|
2020-05-08 21:32:22 +02:00
|
|
|
UserID id.UserID
|
|
|
|
*event.MemberEventContent
|
2019-11-10 20:22:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type formatData struct {
|
|
|
|
Sender Sender
|
|
|
|
Message string
|
2020-05-08 21:32:22 +02:00
|
|
|
Content *event.MessageEventContent
|
2019-11-10 20:22:11 +01:00
|
|
|
}
|
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
func (rc *RelaybotConfig) FormatMessage(content *event.MessageEventContent, sender id.UserID, member *event.MemberEventContent) (string, error) {
|
2019-11-10 20:22:11 +01:00
|
|
|
var output strings.Builder
|
2020-05-08 21:32:22 +02:00
|
|
|
err := rc.messageTemplates.ExecuteTemplate(&output, string(content.MsgType), formatData{
|
2019-11-10 20:22:11 +01:00
|
|
|
Sender: Sender{
|
2020-05-08 21:32:22 +02:00
|
|
|
UserID: sender,
|
|
|
|
MemberEventContent: member,
|
2019-11-10 20:22:11 +01:00
|
|
|
},
|
2020-05-08 21:32:22 +02:00
|
|
|
Content: content,
|
|
|
|
Message: content.FormattedBody,
|
2019-11-10 20:22:11 +01:00
|
|
|
})
|
|
|
|
return output.String(), err
|
|
|
|
}
|