bridge.go 4.8 KB

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