bridge.go 4.2 KB

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