bridge.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. AllowUserInvite bool `yaml:"allow_user_invite"`
  43. CommandPrefix string `yaml:"command_prefix"`
  44. Permissions PermissionConfig `yaml:"permissions"`
  45. usernameTemplate *template.Template `yaml:"-"`
  46. displaynameTemplate *template.Template `yaml:"-"`
  47. }
  48. func (bc *BridgeConfig) setDefaults() {
  49. bc.ConnectionTimeout = 20
  50. bc.MaxConnectionAttempts = 3
  51. bc.ConnectionRetryDelay = -1
  52. bc.ReportConnectionRetry = true
  53. bc.ContactWaitDelay = 1
  54. bc.InitialChatSync = 10
  55. bc.InitialHistoryFill = 20
  56. bc.RecoverChatSync = -1
  57. bc.RecoverHistory = true
  58. bc.SyncChatMaxAge = 259200
  59. bc.SyncWithCustomPuppets = true
  60. bc.InviteOwnPuppetForBackfilling = true
  61. bc.PrivateChatPortalMeta = false
  62. }
  63. type umBridgeConfig BridgeConfig
  64. func (bc *BridgeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  65. err := unmarshal((*umBridgeConfig)(bc))
  66. if err != nil {
  67. return err
  68. }
  69. bc.usernameTemplate, err = template.New("username").Parse(bc.UsernameTemplate)
  70. if err != nil {
  71. return err
  72. }
  73. bc.displaynameTemplate, err = template.New("displayname").Parse(bc.DisplaynameTemplate)
  74. return err
  75. }
  76. type UsernameTemplateArgs struct {
  77. UserID string
  78. }
  79. func (bc BridgeConfig) FormatDisplayname(contact whatsapp.Contact) (string, int8) {
  80. var buf bytes.Buffer
  81. if index := strings.IndexRune(contact.Jid, '@'); index > 0 {
  82. contact.Jid = "+" + contact.Jid[:index]
  83. }
  84. bc.displaynameTemplate.Execute(&buf, contact)
  85. var quality int8
  86. switch {
  87. case len(contact.Notify) > 0:
  88. quality = 3
  89. case len(contact.Name) > 0 || len(contact.Short) > 0:
  90. quality = 2
  91. case len(contact.Jid) > 0:
  92. quality = 1
  93. default:
  94. quality = 0
  95. }
  96. return buf.String(), quality
  97. }
  98. func (bc BridgeConfig) FormatUsername(userID types.WhatsAppID) string {
  99. var buf bytes.Buffer
  100. bc.usernameTemplate.Execute(&buf, userID)
  101. return buf.String()
  102. }
  103. type PermissionConfig map[string]PermissionLevel
  104. type PermissionLevel int
  105. const (
  106. PermissionLevelDefault PermissionLevel = 0
  107. PermissionLevelUser PermissionLevel = 10
  108. PermissionLevelAdmin PermissionLevel = 100
  109. )
  110. func (pc *PermissionConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  111. rawPC := make(map[string]string)
  112. err := unmarshal(&rawPC)
  113. if err != nil {
  114. return err
  115. }
  116. if *pc == nil {
  117. *pc = make(map[string]PermissionLevel)
  118. }
  119. for key, value := range rawPC {
  120. switch strings.ToLower(value) {
  121. case "user":
  122. (*pc)[key] = PermissionLevelUser
  123. case "admin":
  124. (*pc)[key] = PermissionLevelAdmin
  125. default:
  126. val, err := strconv.Atoi(value)
  127. if err != nil {
  128. (*pc)[key] = PermissionLevelDefault
  129. } else {
  130. (*pc)[key] = PermissionLevel(val)
  131. }
  132. }
  133. }
  134. return nil
  135. }
  136. func (pc *PermissionConfig) MarshalYAML() (interface{}, error) {
  137. if *pc == nil {
  138. return nil, nil
  139. }
  140. rawPC := make(map[string]string)
  141. for key, value := range *pc {
  142. switch value {
  143. case PermissionLevelUser:
  144. rawPC[key] = "user"
  145. case PermissionLevelAdmin:
  146. rawPC[key] = "admin"
  147. default:
  148. rawPC[key] = strconv.Itoa(int(value))
  149. }
  150. }
  151. return rawPC, nil
  152. }
  153. func (pc PermissionConfig) IsWhitelisted(userID string) bool {
  154. return pc.GetPermissionLevel(userID) >= 10
  155. }
  156. func (pc PermissionConfig) IsAdmin(userID string) bool {
  157. return pc.GetPermissionLevel(userID) >= 100
  158. }
  159. func (pc PermissionConfig) GetPermissionLevel(userID string) PermissionLevel {
  160. permissions, ok := pc[userID]
  161. if ok {
  162. return permissions
  163. }
  164. _, homeserver := appservice.ParseUserID(userID)
  165. permissions, ok = pc[homeserver]
  166. if len(homeserver) > 0 && ok {
  167. return permissions
  168. }
  169. permissions, ok = pc["*"]
  170. if ok {
  171. return permissions
  172. }
  173. return PermissionLevelDefault
  174. }