bridge.go 5.1 KB

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