bridge.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. "text/template"
  20. "maunium.net/go/mautrix-appservice"
  21. "strings"
  22. "strconv"
  23. "github.com/Rhymen/go-whatsapp"
  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 DisplaynameTemplateArgs struct {
  48. Displayname string
  49. }
  50. type UsernameTemplateArgs struct {
  51. Receiver string
  52. UserID string
  53. }
  54. func (bc BridgeConfig) FormatDisplayname(contact whatsapp.Contact) string {
  55. var buf bytes.Buffer
  56. bc.displaynameTemplate.Execute(&buf, contact)
  57. return buf.String()
  58. }
  59. func (bc BridgeConfig) FormatUsername(receiver types.MatrixUserID, userID types.WhatsAppID) string {
  60. var buf bytes.Buffer
  61. receiver = strings.Replace(receiver, "@", "=40", 1)
  62. receiver = strings.Replace(receiver, ":", "=3", 1)
  63. bc.usernameTemplate.Execute(&buf, UsernameTemplateArgs{
  64. Receiver: receiver,
  65. UserID: userID,
  66. })
  67. return buf.String()
  68. }
  69. func (bc BridgeConfig) MarshalYAML() (interface{}, error) {
  70. bc.DisplaynameTemplate = bc.FormatDisplayname(whatsapp.Contact{
  71. Jid: "{{.Jid}}",
  72. Notify: "{{.Notify}}",
  73. Name: "{{.Name}}",
  74. Short: "{{.Short}}",
  75. })
  76. bc.UsernameTemplate = bc.FormatUsername("{{.Receiver}}", "{{.UserID}}")
  77. return bc, nil
  78. }
  79. type PermissionConfig map[string]PermissionLevel
  80. type PermissionLevel int
  81. const (
  82. PermissionLevelDefault PermissionLevel = 0
  83. PermissionLevelUser PermissionLevel = 10
  84. PermissionLevelAdmin PermissionLevel = 100
  85. )
  86. func (pc *PermissionConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  87. rawPC := make(map[string]string)
  88. err := unmarshal(&rawPC)
  89. if err != nil {
  90. return err
  91. }
  92. if *pc == nil {
  93. *pc = make(map[string]PermissionLevel)
  94. }
  95. for key, value := range rawPC {
  96. switch strings.ToLower(value) {
  97. case "user":
  98. (*pc)[key] = PermissionLevelUser
  99. case "admin":
  100. (*pc)[key] = PermissionLevelAdmin
  101. default:
  102. val, err := strconv.Atoi(value)
  103. if err != nil {
  104. (*pc)[key] = PermissionLevelDefault
  105. } else {
  106. (*pc)[key] = PermissionLevel(val)
  107. }
  108. }
  109. }
  110. return nil
  111. }
  112. func (pc *PermissionConfig) MarshalYAML() (interface{}, error) {
  113. if *pc == nil {
  114. return nil, nil
  115. }
  116. rawPC := make(map[string]string)
  117. for key, value := range *pc {
  118. switch value {
  119. case PermissionLevelUser:
  120. rawPC[key] = "user"
  121. case PermissionLevelAdmin:
  122. rawPC[key] = "admin"
  123. default:
  124. rawPC[key] = strconv.Itoa(int(value))
  125. }
  126. }
  127. return rawPC, nil
  128. }
  129. func (pc PermissionConfig) IsWhitelisted(userID string) bool {
  130. return pc.GetPermissionLevel(userID) >= 10
  131. }
  132. func (pc PermissionConfig) IsAdmin(userID string) bool {
  133. return pc.GetPermissionLevel(userID) >= 100
  134. }
  135. func (pc PermissionConfig) GetPermissionLevel(userID string) PermissionLevel {
  136. permissions, ok := pc[userID]
  137. if ok {
  138. return permissions
  139. }
  140. _, homeserver := appservice.ParseUserID(userID)
  141. permissions, ok = pc[homeserver]
  142. if len(homeserver) > 0 && ok {
  143. return permissions
  144. }
  145. permissions, ok = pc["*"]
  146. if ok {
  147. return permissions
  148. }
  149. return PermissionLevelDefault
  150. }