bridge.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2019 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package config
  17. import (
  18. "bytes"
  19. "strconv"
  20. "strings"
  21. "text/template"
  22. "github.com/Rhymen/go-whatsapp"
  23. "maunium.net/go/mautrix-appservice"
  24. "maunium.net/go/mautrix-whatsapp/types"
  25. )
  26. type BridgeConfig struct {
  27. UsernameTemplate string `yaml:"username_template"`
  28. DisplaynameTemplate string `yaml:"displayname_template"`
  29. ConnectionTimeout int `yaml:"connection_timeout"`
  30. MaxConnectionAttempts int `yaml:"max_connection_attempts"`
  31. ConnectionRetryDelay int `yaml:"connection_retry_delay"`
  32. ReportConnectionRetry bool `yaml:"report_connection_retry"`
  33. ContactWaitDelay int `yaml:"contact_wait_delay"`
  34. InitialChatSync int `yaml:"initial_chat_sync_count"`
  35. InitialHistoryFill int `yaml:"initial_history_fill_count"`
  36. RecoverChatSync int `yaml:"recovery_chat_sync_count"`
  37. RecoverHistory bool `yaml:"recovery_history_backfill"`
  38. SyncChatMaxAge uint64 `yaml:"sync_max_chat_age"`
  39. SyncWithCustomPuppets bool `yaml:"sync_with_custom_puppets"`
  40. InviteOwnPuppetForBackfilling bool `yaml:"invite_own_puppet_for_backfilling"`
  41. PrivateChatPortalMeta bool `yaml:"private_chat_portal_meta"`
  42. CommandPrefix string `yaml:"command_prefix"`
  43. Permissions PermissionConfig `yaml:"permissions"`
  44. usernameTemplate *template.Template `yaml:"-"`
  45. displaynameTemplate *template.Template `yaml:"-"`
  46. }
  47. func (bc *BridgeConfig) setDefaults() {
  48. bc.ConnectionTimeout = 20
  49. bc.MaxConnectionAttempts = 3
  50. bc.ConnectionRetryDelay = -1
  51. bc.ReportConnectionRetry = true
  52. bc.ContactWaitDelay = 1
  53. bc.InitialChatSync = 10
  54. bc.InitialHistoryFill = 20
  55. bc.RecoverChatSync = -1
  56. bc.RecoverHistory = true
  57. bc.SyncChatMaxAge = 259200
  58. bc.SyncWithCustomPuppets = true
  59. bc.InviteOwnPuppetForBackfilling = true
  60. bc.PrivateChatPortalMeta = false
  61. }
  62. type umBridgeConfig BridgeConfig
  63. func (bc *BridgeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  64. err := unmarshal((*umBridgeConfig)(bc))
  65. if err != nil {
  66. return err
  67. }
  68. bc.usernameTemplate, err = template.New("username").Parse(bc.UsernameTemplate)
  69. if err != nil {
  70. return err
  71. }
  72. bc.displaynameTemplate, err = template.New("displayname").Parse(bc.DisplaynameTemplate)
  73. return err
  74. }
  75. type UsernameTemplateArgs struct {
  76. UserID string
  77. }
  78. func (bc BridgeConfig) FormatDisplayname(contact whatsapp.Contact) (string, int8) {
  79. var buf bytes.Buffer
  80. if index := strings.IndexRune(contact.Jid, '@'); index > 0 {
  81. contact.Jid = "+" + contact.Jid[:index]
  82. }
  83. bc.displaynameTemplate.Execute(&buf, contact)
  84. var quality int8
  85. switch {
  86. case len(contact.Notify) > 0:
  87. quality = 3
  88. case len(contact.Name) > 0 || len(contact.Short) > 0:
  89. quality = 2
  90. case len(contact.Jid) > 0:
  91. quality = 1
  92. default:
  93. quality = 0
  94. }
  95. return buf.String(), quality
  96. }
  97. func (bc BridgeConfig) FormatUsername(userID types.WhatsAppID) string {
  98. var buf bytes.Buffer
  99. bc.usernameTemplate.Execute(&buf, userID)
  100. return buf.String()
  101. }
  102. type PermissionConfig map[string]PermissionLevel
  103. type PermissionLevel int
  104. const (
  105. PermissionLevelDefault PermissionLevel = 0
  106. PermissionLevelUser PermissionLevel = 10
  107. PermissionLevelAdmin PermissionLevel = 100
  108. )
  109. func (pc *PermissionConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  110. rawPC := make(map[string]string)
  111. err := unmarshal(&rawPC)
  112. if err != nil {
  113. return err
  114. }
  115. if *pc == nil {
  116. *pc = make(map[string]PermissionLevel)
  117. }
  118. for key, value := range rawPC {
  119. switch strings.ToLower(value) {
  120. case "user":
  121. (*pc)[key] = PermissionLevelUser
  122. case "admin":
  123. (*pc)[key] = PermissionLevelAdmin
  124. default:
  125. val, err := strconv.Atoi(value)
  126. if err != nil {
  127. (*pc)[key] = PermissionLevelDefault
  128. } else {
  129. (*pc)[key] = PermissionLevel(val)
  130. }
  131. }
  132. }
  133. return nil
  134. }
  135. func (pc *PermissionConfig) MarshalYAML() (interface{}, error) {
  136. if *pc == nil {
  137. return nil, nil
  138. }
  139. rawPC := make(map[string]string)
  140. for key, value := range *pc {
  141. switch value {
  142. case PermissionLevelUser:
  143. rawPC[key] = "user"
  144. case PermissionLevelAdmin:
  145. rawPC[key] = "admin"
  146. default:
  147. rawPC[key] = strconv.Itoa(int(value))
  148. }
  149. }
  150. return rawPC, nil
  151. }
  152. func (pc PermissionConfig) IsWhitelisted(userID string) bool {
  153. return pc.GetPermissionLevel(userID) >= 10
  154. }
  155. func (pc PermissionConfig) IsAdmin(userID string) bool {
  156. return pc.GetPermissionLevel(userID) >= 100
  157. }
  158. func (pc PermissionConfig) GetPermissionLevel(userID string) PermissionLevel {
  159. permissions, ok := pc[userID]
  160. if ok {
  161. return permissions
  162. }
  163. _, homeserver := appservice.ParseUserID(userID)
  164. permissions, ok = pc[homeserver]
  165. if len(homeserver) > 0 && ok {
  166. return permissions
  167. }
  168. permissions, ok = pc["*"]
  169. if ok {
  170. return permissions
  171. }
  172. return PermissionLevelDefault
  173. }