mautrix-whatsapp/config/bridge.go

310 lines
8.3 KiB
Go
Raw Normal View History

// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
2021-10-22 19:14:34 +02:00
// Copyright (C) 2021 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 config
import (
2021-10-31 12:04:44 +01:00
"fmt"
"strconv"
2018-08-26 21:53:13 +02:00
"strings"
"text/template"
2021-10-22 19:14:34 +02:00
"go.mau.fi/whatsmeow/types"
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"
)
type BridgeConfig struct {
UsernameTemplate string `yaml:"username_template"`
DisplaynameTemplate string `yaml:"displayname_template"`
DeliveryReceipts bool `yaml:"delivery_receipts"`
PortalMessageBuffer int `yaml:"portal_message_buffer"`
2019-05-16 17:08:30 +02:00
CallNotices struct {
Start bool `yaml:"start"`
End bool `yaml:"end"`
} `yaml:"call_notices"`
HistorySync struct {
CreatePortals bool `yaml:"create_portals"`
MaxAge int64 `yaml:"max_age"`
Backfill bool `yaml:"backfill"`
DoublePuppetBackfill bool `yaml:"double_puppet_backfill"`
RequestFullSync bool `yaml:"request_full_sync"`
2021-10-29 21:11:19 +02:00
} `yaml:"history_sync"`
UserAvatarSync bool `yaml:"user_avatar_sync"`
BridgeMatrixLeave bool `yaml:"bridge_matrix_leave"`
2020-05-08 21:32:22 +02:00
SyncWithCustomPuppets bool `yaml:"sync_with_custom_puppets"`
SyncDirectChatList bool `yaml:"sync_direct_chat_list"`
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"`
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"`
PinnedTag string `yaml:"pinned_tag"`
TagOnlyOnCreate bool `yaml:"tag_only_on_create"`
MarkReadOnlyOnCreate bool `yaml:"mark_read_only_on_create"`
EnableStatusBroadcast bool `yaml:"enable_status_broadcast"`
WhatsappThumbnail bool `yaml:"whatsapp_thumbnail"`
AllowUserInvite bool `yaml:"allow_user_invite"`
CommandPrefix string `yaml:"command_prefix"`
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"`
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"`
} `yaml:"encryption"`
Permissions PermissionConfig `yaml:"permissions"`
Relay RelaybotConfig `yaml:"relay"`
2019-11-10 20:22:11 +01:00
usernameTemplate *template.Template `yaml:"-"`
displaynameTemplate *template.Template `yaml:"-"`
}
type umBridgeConfig BridgeConfig
func (bc *BridgeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
err := unmarshal((*umBridgeConfig)(bc))
if err != nil {
return err
}
bc.usernameTemplate, err = template.New("username").Parse(bc.UsernameTemplate)
if err != nil {
return err
2021-10-31 12:04:44 +01:00
} else if !strings.Contains(bc.FormatUsername("1234567890"), "1234567890") {
return fmt.Errorf("username template is missing user ID placeholder")
}
bc.displaynameTemplate, err = template.New("displayname").Parse(bc.DisplaynameTemplate)
if err != nil {
return err
}
return nil
}
type UsernameTemplateArgs struct {
2020-05-08 21:32:22 +02:00
UserID id.UserID
}
2021-10-22 19:14:34 +02:00
type legacyContactInfo struct {
types.ContactInfo
Phone string
Notify string
VName string
Name string
Short string
JID string
}
func (bc BridgeConfig) FormatDisplayname(jid types.JID, contact types.ContactInfo) (string, int8) {
var buf strings.Builder
_ = bc.displaynameTemplate.Execute(&buf, legacyContactInfo{
ContactInfo: contact,
Notify: contact.PushName,
VName: contact.BusinessName,
Name: contact.FullName,
Short: contact.FirstName,
Phone: "+" + jid.User,
JID: "+" + jid.User,
})
var quality int8
switch {
2021-10-22 19:14:34 +02:00
case len(contact.PushName) > 0 || len(contact.BusinessName) > 0:
quality = 3
2021-10-22 19:14:34 +02:00
case len(contact.FullName) > 0 || len(contact.FirstName) > 0:
quality = 2
default:
2021-10-22 19:14:34 +02:00
quality = 1
}
return buf.String(), quality
}
2021-10-22 19:14:34 +02:00
func (bc BridgeConfig) FormatUsername(username string) string {
var buf strings.Builder
_ = bc.usernameTemplate.Execute(&buf, username)
return buf.String()
}
type PermissionConfig map[string]PermissionLevel
type PermissionLevel int
const (
2021-10-28 13:03:55 +02:00
PermissionLevelDefault PermissionLevel = 0
PermissionLevelRelay PermissionLevel = 5
PermissionLevelUser PermissionLevel = 10
PermissionLevelAdmin PermissionLevel = 100
)
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) {
2021-10-28 13:03:55 +02:00
case "relaybot", "relay":
(*pc)[key] = PermissionLevelRelay
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 {
2021-10-28 13:03:55 +02:00
case PermissionLevelRelay:
rawPC[key] = "relay"
case PermissionLevelUser:
rawPC[key] = "user"
case PermissionLevelAdmin:
rawPC[key] = "admin"
default:
rawPC[key] = strconv.Itoa(int(value))
}
}
return rawPC, nil
}
2021-10-28 13:03:55 +02:00
func (pc PermissionConfig) IsRelayWhitelisted(userID id.UserID) bool {
return pc.GetPermissionLevel(userID) >= PermissionLevelRelay
2019-11-10 20:22:11 +01:00
}
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
}
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
}
2020-05-08 21:32:22 +02:00
func (pc PermissionConfig) GetPermissionLevel(userID id.UserID) PermissionLevel {
permissions, ok := pc[string(userID)]
if ok {
return permissions
}
2020-05-08 21:32:22 +02:00
_, homeserver, _ := userID.Parse()
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 {
Enabled bool `yaml:"enabled"`
AdminOnly bool `yaml:"admin_only"`
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
}