bridge.go 5.5 KB

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