bridge.go 4.7 KB

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