123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
- // 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 (
- "strconv"
- "strings"
- "text/template"
- "go.mau.fi/whatsmeow/types"
- "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"`
- MaxConnectionAttempts int `yaml:"max_connection_attempts"`
- ConnectionRetryDelay int `yaml:"connection_retry_delay"`
- ReportConnectionRetry bool `yaml:"report_connection_retry"`
- PortalMessageBuffer int `yaml:"portal_message_buffer"`
- CallNotices struct {
- Start bool `yaml:"start"`
- End bool `yaml:"end"`
- } `yaml:"call_notices"`
- HistorySync struct {
- CreatePortals bool `yaml:"create_portals"`
- Backfill bool `yaml:"backfill"`
- DoublePuppetBackfill bool `yaml:"double_puppet_backfill"`
- }
- UserAvatarSync bool `yaml:"user_avatar_sync"`
- BridgeMatrixLeave bool `yaml:"bridge_matrix_leave"`
- 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"`
- 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"`
- Encryption struct {
- Allow bool `yaml:"allow"`
- Default bool `yaml:"default"`
- 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"`
- usernameTemplate *template.Template `yaml:"-"`
- displaynameTemplate *template.Template `yaml:"-"`
- }
- func (bc *BridgeConfig) setDefaults() {
- bc.MaxConnectionAttempts = 3
- bc.ConnectionRetryDelay = -1
- bc.ReportConnectionRetry = true
- bc.PortalMessageBuffer = 128
- bc.CallNotices.Start = true
- bc.CallNotices.End = true
- bc.HistorySync.CreatePortals = true
- bc.UserAvatarSync = true
- bc.BridgeMatrixLeave = true
- bc.SyncWithCustomPuppets = true
- bc.DefaultBridgePresence = true
- bc.DefaultBridgeReceipts = true
- bc.BridgeNotices = true
- bc.EnableStatusBroadcast = true
- bc.Relay.AdminOnly = true
- }
- 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
- }
- bc.displaynameTemplate, err = template.New("displayname").Parse(bc.DisplaynameTemplate)
- if err != nil {
- return err
- }
- return nil
- }
- type UsernameTemplateArgs struct {
- UserID id.UserID
- }
- 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 {
- case len(contact.PushName) > 0 || len(contact.BusinessName) > 0:
- quality = 3
- case len(contact.FullName) > 0 || len(contact.FirstName) > 0:
- quality = 2
- default:
- quality = 1
- }
- return buf.String(), quality
- }
- 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 (
- PermissionLevelDefault PermissionLevel = 0
- PermissionLevelRelaybot 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) {
- case "relaybot":
- (*pc)[key] = PermissionLevelRelaybot
- 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 {
- case PermissionLevelRelaybot:
- rawPC[key] = "relaybot"
- case PermissionLevelUser:
- rawPC[key] = "user"
- case PermissionLevelAdmin:
- rawPC[key] = "admin"
- default:
- rawPC[key] = strconv.Itoa(int(value))
- }
- }
- return rawPC, nil
- }
- func (pc PermissionConfig) IsRelaybotWhitelisted(userID id.UserID) bool {
- return pc.GetPermissionLevel(userID) >= PermissionLevelRelaybot
- }
- func (pc PermissionConfig) IsWhitelisted(userID id.UserID) bool {
- return pc.GetPermissionLevel(userID) >= PermissionLevelUser
- }
- func (pc PermissionConfig) IsAdmin(userID id.UserID) bool {
- return pc.GetPermissionLevel(userID) >= PermissionLevelAdmin
- }
- func (pc PermissionConfig) GetPermissionLevel(userID id.UserID) PermissionLevel {
- permissions, ok := pc[string(userID)]
- if ok {
- return permissions
- }
- _, homeserver, _ := userID.Parse()
- permissions, ok = pc[homeserver]
- if len(homeserver) > 0 && ok {
- return permissions
- }
- permissions, ok = pc["*"]
- if ok {
- return permissions
- }
- return PermissionLevelDefault
- }
- type RelaybotConfig struct {
- Enabled bool `yaml:"enabled"`
- AdminOnly bool `yaml:"admin_only"`
- MessageFormats map[event.MessageType]string `yaml:"message_formats"`
- messageTemplates *template.Template `yaml:"-"`
- }
- 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 {
- UserID id.UserID
- *event.MemberEventContent
- }
- type formatData struct {
- Sender Sender
- Message string
- Content *event.MessageEventContent
- }
- func (rc *RelaybotConfig) FormatMessage(content *event.MessageEventContent, sender id.UserID, member *event.MemberEventContent) (string, error) {
- var output strings.Builder
- err := rc.messageTemplates.ExecuteTemplate(&output, string(content.MsgType), formatData{
- Sender: Sender{
- UserID: sender,
- MemberEventContent: member,
- },
- Content: content,
- Message: content.FormattedBody,
- })
- return output.String(), err
- }
|