bridge.go 4.4 KB

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