2018-08-13 22:24:44 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2021-10-22 19:14:34 +02:00
|
|
|
// Copyright (C) 2021 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 (
|
2022-06-21 20:56:02 +02:00
|
|
|
"errors"
|
2021-10-31 12:04:44 +01:00
|
|
|
"fmt"
|
2018-08-26 21:53:13 +02:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
2022-06-29 19:05:55 +02:00
|
|
|
"time"
|
2018-08-26 21:53:13 +02:00
|
|
|
|
2021-10-22 19:14:34 +02:00
|
|
|
"go.mau.fi/whatsmeow/types"
|
2019-01-11 20:17:31 +01:00
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
"maunium.net/go/mautrix/bridge/bridgeconfig"
|
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
|
|
|
)
|
|
|
|
|
2022-03-24 21:18:32 +01:00
|
|
|
type DeferredConfig struct {
|
|
|
|
StartDaysAgo int `yaml:"start_days_ago"`
|
|
|
|
MaxBatchEvents int `yaml:"max_batch_events"`
|
|
|
|
BatchDelay int `yaml:"batch_delay"`
|
|
|
|
}
|
|
|
|
|
2022-05-10 20:18:01 +02:00
|
|
|
type MediaRequestMethod string
|
|
|
|
|
|
|
|
const (
|
|
|
|
MediaRequestMethodImmediate MediaRequestMethod = "immediate"
|
|
|
|
MediaRequestMethodLocalTime = "local_time"
|
|
|
|
)
|
|
|
|
|
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"`
|
|
|
|
|
2021-12-29 20:40:08 +01:00
|
|
|
PersonalFilteringSpaces bool `yaml:"personal_filtering_spaces"`
|
|
|
|
|
2021-11-09 21:57:36 +01:00
|
|
|
DeliveryReceipts bool `yaml:"delivery_receipts"`
|
2022-05-31 16:28:58 +02:00
|
|
|
MessageStatusEvents bool `yaml:"message_status_events"`
|
|
|
|
MessageErrorNotices bool `yaml:"message_error_notices"`
|
2021-11-09 21:57:36 +01:00
|
|
|
PortalMessageBuffer int `yaml:"portal_message_buffer"`
|
|
|
|
CallStartNotices bool `yaml:"call_start_notices"`
|
|
|
|
IdentityChangeNotices bool `yaml:"identity_change_notices"`
|
2019-08-24 21:42:03 +02:00
|
|
|
|
2021-10-28 11:59:22 +02:00
|
|
|
HistorySync struct {
|
2022-04-19 04:50:21 +02:00
|
|
|
CreatePortals bool `yaml:"create_portals"`
|
2022-05-02 14:00:57 +02:00
|
|
|
Backfill bool `yaml:"backfill"`
|
2022-04-19 04:50:21 +02:00
|
|
|
|
2022-03-24 21:18:32 +01:00
|
|
|
DoublePuppetBackfill bool `yaml:"double_puppet_backfill"`
|
|
|
|
RequestFullSync bool `yaml:"request_full_sync"`
|
|
|
|
MaxInitialConversations int `yaml:"max_initial_conversations"`
|
2022-10-12 21:32:48 +02:00
|
|
|
UnreadHoursThreshold int `yaml:"unread_hours_threshold"`
|
2022-03-24 21:18:32 +01:00
|
|
|
|
|
|
|
Immediate struct {
|
|
|
|
WorkerCount int `yaml:"worker_count"`
|
|
|
|
MaxEvents int `yaml:"max_events"`
|
|
|
|
} `yaml:"immediate"`
|
|
|
|
|
2022-05-10 20:18:01 +02:00
|
|
|
MediaRequests struct {
|
|
|
|
AutoRequestMedia bool `yaml:"auto_request_media"`
|
|
|
|
RequestMethod MediaRequestMethod `yaml:"request_method"`
|
|
|
|
RequestLocalTime int `yaml:"request_local_time"`
|
|
|
|
} `yaml:"media_requests"`
|
|
|
|
|
2022-03-24 21:18:32 +01:00
|
|
|
Deferred []DeferredConfig `yaml:"deferred"`
|
2021-10-29 21:11:19 +02:00
|
|
|
} `yaml:"history_sync"`
|
2021-10-28 11:59:22 +02:00
|
|
|
UserAvatarSync bool `yaml:"user_avatar_sync"`
|
|
|
|
BridgeMatrixLeave bool `yaml:"bridge_matrix_leave"`
|
2019-05-22 15:46:18 +02:00
|
|
|
|
2022-05-18 18:40:29 +02:00
|
|
|
SyncWithCustomPuppets bool `yaml:"sync_with_custom_puppets"`
|
|
|
|
SyncDirectChatList bool `yaml:"sync_direct_chat_list"`
|
|
|
|
SyncManualMarkedUnread bool `yaml:"sync_manual_marked_unread"`
|
|
|
|
DefaultBridgeReceipts bool `yaml:"default_bridge_receipts"`
|
|
|
|
DefaultBridgePresence bool `yaml:"default_bridge_presence"`
|
|
|
|
SendPresenceOnTyping bool `yaml:"send_presence_on_typing"`
|
2021-11-06 12:57:35 +01:00
|
|
|
|
2022-02-25 00:27:24 +01:00
|
|
|
ForceActiveDeliveryReceipts bool `yaml:"force_active_delivery_receipts"`
|
|
|
|
|
2021-11-06 12:57:35 +01:00
|
|
|
DoublePuppetServerMap map[string]string `yaml:"double_puppet_server_map"`
|
|
|
|
DoublePuppetAllowDiscovery bool `yaml:"double_puppet_allow_discovery"`
|
|
|
|
LoginSharedSecretMap map[string]string `yaml:"login_shared_secret_map"`
|
2019-05-24 01:33:26 +02:00
|
|
|
|
2021-10-26 16:01:10 +02:00
|
|
|
PrivateChatPortalMeta bool `yaml:"private_chat_portal_meta"`
|
2022-09-29 13:39:58 +02:00
|
|
|
ParallelMemberSync bool `yaml:"parallel_member_sync"`
|
2021-10-26 16:01:10 +02:00
|
|
|
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"`
|
2021-11-15 13:06:31 +01:00
|
|
|
MuteStatusBroadcast bool `yaml:"mute_status_broadcast"`
|
2022-04-27 11:45:11 +02:00
|
|
|
StatusBroadcastTag string `yaml:"status_broadcast_tag"`
|
2021-11-07 21:31:22 +01:00
|
|
|
WhatsappThumbnail bool `yaml:"whatsapp_thumbnail"`
|
|
|
|
AllowUserInvite bool `yaml:"allow_user_invite"`
|
|
|
|
FederateRooms bool `yaml:"federate_rooms"`
|
2022-02-15 15:28:20 +01:00
|
|
|
URLPreviews bool `yaml:"url_previews"`
|
2022-06-17 10:34:24 +02:00
|
|
|
CaptionInMessage bool `yaml:"caption_in_message"`
|
2022-11-26 17:53:08 +01:00
|
|
|
ExtEvPolls bool `yaml:"extev_polls"`
|
2022-10-08 16:46:42 +02:00
|
|
|
SendWhatsAppEdits bool `yaml:"send_whatsapp_edits"`
|
2021-11-01 10:17:44 +01:00
|
|
|
|
2022-06-30 13:41:37 +02:00
|
|
|
MessageHandlingTimeout struct {
|
|
|
|
ErrorAfterStr string `yaml:"error_after"`
|
|
|
|
DeadlineStr string `yaml:"deadline"`
|
|
|
|
|
|
|
|
ErrorAfter time.Duration `yaml:"-"`
|
|
|
|
Deadline time.Duration `yaml:"-"`
|
|
|
|
} `yaml:"message_handling_timeout"`
|
2022-06-29 19:05:55 +02:00
|
|
|
|
2022-06-13 19:01:39 +02:00
|
|
|
DisableStatusBroadcastSend bool `yaml:"disable_status_broadcast_send"`
|
2022-01-07 13:32:00 +01:00
|
|
|
DisappearingMessagesInGroups bool `yaml:"disappearing_messages_in_groups"`
|
|
|
|
|
2022-09-06 21:38:47 +02:00
|
|
|
DisableBridgeAlerts bool `yaml:"disable_bridge_alerts"`
|
|
|
|
CrashOnStreamReplaced bool `yaml:"crash_on_stream_replaced"`
|
2022-01-25 13:26:24 +01:00
|
|
|
|
2018-08-16 23:11:28 +02:00
|
|
|
CommandPrefix string `yaml:"command_prefix"`
|
|
|
|
|
2022-05-22 15:15:54 +02:00
|
|
|
ManagementRoomText bridgeconfig.ManagementRoomTexts `yaml:"management_room_text"`
|
2021-10-28 12:58:20 +02:00
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
Encryption bridgeconfig.EncryptionConfig `yaml:"encryption"`
|
2020-08-05 13:58:46 +02:00
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
Provisioning struct {
|
|
|
|
Prefix string `yaml:"prefix"`
|
|
|
|
SharedSecret string `yaml:"shared_secret"`
|
|
|
|
} `yaml:"provisioning"`
|
2020-05-09 01:03:59 +02:00
|
|
|
|
2022-05-22 17:21:12 +02:00
|
|
|
Permissions bridgeconfig.PermissionConfig `yaml:"permissions"`
|
2018-08-16 23:11:28 +02:00
|
|
|
|
2021-10-28 12:57:15 +02:00
|
|
|
Relay RelaybotConfig `yaml:"relay"`
|
2019-11-10 20:22:11 +01:00
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
ParsedUsernameTemplate *template.Template `yaml:"-"`
|
|
|
|
displaynameTemplate *template.Template `yaml:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc BridgeConfig) GetEncryptionConfig() bridgeconfig.EncryptionConfig {
|
|
|
|
return bc.Encryption
|
2018-08-13 00:00:23 +02:00
|
|
|
}
|
|
|
|
|
2022-06-22 19:09:21 +02:00
|
|
|
func (bc BridgeConfig) EnableMessageStatusEvents() bool {
|
|
|
|
return bc.MessageStatusEvents
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc BridgeConfig) EnableMessageErrorNotices() bool {
|
|
|
|
return bc.MessageErrorNotices
|
|
|
|
}
|
|
|
|
|
2022-05-22 15:15:54 +02:00
|
|
|
func (bc BridgeConfig) GetCommandPrefix() string {
|
|
|
|
return bc.CommandPrefix
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc BridgeConfig) GetManagementRoomTexts() bridgeconfig.ManagementRoomTexts {
|
|
|
|
return bc.ManagementRoomText
|
|
|
|
}
|
|
|
|
|
2022-06-27 10:46:30 +02:00
|
|
|
func (bc BridgeConfig) GetResendBridgeInfo() bool {
|
|
|
|
return bc.ResendBridgeInfo
|
|
|
|
}
|
|
|
|
|
2022-06-21 20:56:02 +02:00
|
|
|
func boolToInt(val bool) int {
|
|
|
|
if val {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc BridgeConfig) Validate() error {
|
|
|
|
_, hasWildcard := bc.Permissions["*"]
|
|
|
|
_, hasExampleDomain := bc.Permissions["example.com"]
|
|
|
|
_, hasExampleUser := bc.Permissions["@admin:example.com"]
|
|
|
|
exampleLen := boolToInt(hasWildcard) + boolToInt(hasExampleUser) + boolToInt(hasExampleDomain)
|
|
|
|
if len(bc.Permissions) <= exampleLen {
|
|
|
|
return errors.New("bridge.permissions not configured")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
bc.ParsedUsernameTemplate, err = template.New("username").Parse(bc.UsernameTemplate)
|
2018-08-13 00:00:23 +02:00
|
|
|
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")
|
2018-08-13 00:00:23 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-06-30 13:41:37 +02:00
|
|
|
if bc.MessageHandlingTimeout.ErrorAfterStr != "" {
|
|
|
|
bc.MessageHandlingTimeout.ErrorAfter, err = time.ParseDuration(bc.MessageHandlingTimeout.ErrorAfterStr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if bc.MessageHandlingTimeout.DeadlineStr != "" {
|
|
|
|
bc.MessageHandlingTimeout.Deadline, err = time.ParseDuration(bc.MessageHandlingTimeout.DeadlineStr)
|
2022-06-29 19:05:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-10 14:24:53 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-05-13 10:34:45 +02:00
|
|
|
const (
|
|
|
|
NameQualityPush = 3
|
|
|
|
NameQualityContact = 2
|
|
|
|
NameQualityPhone = 1
|
|
|
|
)
|
|
|
|
|
2021-10-22 19:14:34 +02:00
|
|
|
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,
|
|
|
|
})
|
2018-09-01 22:38:03 +02:00
|
|
|
var quality int8
|
|
|
|
switch {
|
2021-10-22 19:14:34 +02:00
|
|
|
case len(contact.PushName) > 0 || len(contact.BusinessName) > 0:
|
2022-05-13 10:34:45 +02:00
|
|
|
quality = NameQualityPush
|
2021-10-22 19:14:34 +02:00
|
|
|
case len(contact.FullName) > 0 || len(contact.FirstName) > 0:
|
2022-05-13 10:34:45 +02:00
|
|
|
quality = NameQualityContact
|
2018-09-01 22:38:03 +02:00
|
|
|
default:
|
2022-05-13 10:34:45 +02:00
|
|
|
quality = NameQualityPhone
|
2018-09-01 22:38:03 +02:00
|
|
|
}
|
|
|
|
return buf.String(), quality
|
2018-08-13 00:00:23 +02:00
|
|
|
}
|
|
|
|
|
2021-10-22 19:14:34 +02:00
|
|
|
func (bc BridgeConfig) FormatUsername(username string) string {
|
|
|
|
var buf strings.Builder
|
2022-05-22 00:06:30 +02:00
|
|
|
_ = bc.ParsedUsernameTemplate.Execute(&buf, username)
|
2019-08-10 14:24:53 +02:00
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2019-11-10 20:22:11 +01:00
|
|
|
type RelaybotConfig struct {
|
2021-10-28 12:57:15 +02:00
|
|
|
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 {
|
2021-11-05 19:08:49 +01:00
|
|
|
UserID string
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-05 19:08:49 +01:00
|
|
|
func (rc *RelaybotConfig) FormatMessage(content *event.MessageEventContent, sender id.UserID, member event.MemberEventContent) (string, error) {
|
|
|
|
if len(member.Displayname) == 0 {
|
|
|
|
member.Displayname = sender.String()
|
|
|
|
}
|
|
|
|
member.Displayname = template.HTMLEscapeString(member.Displayname)
|
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{
|
2021-11-05 19:08:49 +01:00
|
|
|
UserID: template.HTMLEscapeString(sender.String()),
|
2020-05-08 21:32:22 +02:00
|
|
|
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
|
|
|
|
}
|