bridge.go 5.0 KB

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