bridge.go 4.4 KB

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