bridge.go 4.9 KB

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