bridge.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. CommunityTemplate string `yaml:"community_template"`
  30. ConnectionTimeout int `yaml:"connection_timeout"`
  31. LoginQRRegenCount int `yaml:"login_qr_regen_count"`
  32. MaxConnectionAttempts int `yaml:"max_connection_attempts"`
  33. ConnectionRetryDelay int `yaml:"connection_retry_delay"`
  34. ReportConnectionRetry bool `yaml:"report_connection_retry"`
  35. ContactWaitDelay int `yaml:"contact_wait_delay"`
  36. InitialChatSync int `yaml:"initial_chat_sync_count"`
  37. InitialHistoryFill int `yaml:"initial_history_fill_count"`
  38. RecoverChatSync int `yaml:"recovery_chat_sync_count"`
  39. RecoverHistory bool `yaml:"recovery_history_backfill"`
  40. SyncChatMaxAge uint64 `yaml:"sync_max_chat_age"`
  41. SyncWithCustomPuppets bool `yaml:"sync_with_custom_puppets"`
  42. InviteOwnPuppetForBackfilling bool `yaml:"invite_own_puppet_for_backfilling"`
  43. PrivateChatPortalMeta bool `yaml:"private_chat_portal_meta"`
  44. AllowUserInvite bool `yaml:"allow_user_invite"`
  45. CommandPrefix string `yaml:"command_prefix"`
  46. Permissions PermissionConfig `yaml:"permissions"`
  47. usernameTemplate *template.Template `yaml:"-"`
  48. displaynameTemplate *template.Template `yaml:"-"`
  49. communityTemplate *template.Template `yaml:"-"`
  50. }
  51. func (bc *BridgeConfig) setDefaults() {
  52. bc.ConnectionTimeout = 20
  53. bc.LoginQRRegenCount = 2
  54. bc.MaxConnectionAttempts = 3
  55. bc.ConnectionRetryDelay = -1
  56. bc.ReportConnectionRetry = true
  57. bc.ContactWaitDelay = 1
  58. bc.InitialChatSync = 10
  59. bc.InitialHistoryFill = 20
  60. bc.RecoverChatSync = -1
  61. bc.RecoverHistory = true
  62. bc.SyncChatMaxAge = 259200
  63. bc.SyncWithCustomPuppets = true
  64. bc.InviteOwnPuppetForBackfilling = true
  65. bc.PrivateChatPortalMeta = false
  66. }
  67. type umBridgeConfig BridgeConfig
  68. func (bc *BridgeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  69. err := unmarshal((*umBridgeConfig)(bc))
  70. if err != nil {
  71. return err
  72. }
  73. bc.usernameTemplate, err = template.New("username").Parse(bc.UsernameTemplate)
  74. if err != nil {
  75. return err
  76. }
  77. bc.displaynameTemplate, err = template.New("displayname").Parse(bc.DisplaynameTemplate)
  78. if err != nil {
  79. return err
  80. }
  81. if len(bc.CommunityTemplate) > 0 {
  82. bc.communityTemplate, err = template.New("community").Parse(bc.CommunityTemplate)
  83. if err != nil {
  84. return err
  85. }
  86. }
  87. return nil
  88. }
  89. type UsernameTemplateArgs struct {
  90. UserID string
  91. }
  92. func (bc BridgeConfig) FormatDisplayname(contact whatsapp.Contact) (string, int8) {
  93. var buf bytes.Buffer
  94. if index := strings.IndexRune(contact.Jid, '@'); index > 0 {
  95. contact.Jid = "+" + contact.Jid[:index]
  96. }
  97. bc.displaynameTemplate.Execute(&buf, contact)
  98. var quality int8
  99. switch {
  100. case len(contact.Notify) > 0:
  101. quality = 3
  102. case len(contact.Name) > 0 || len(contact.Short) > 0:
  103. quality = 2
  104. case len(contact.Jid) > 0:
  105. quality = 1
  106. default:
  107. quality = 0
  108. }
  109. return buf.String(), quality
  110. }
  111. func (bc BridgeConfig) FormatUsername(userID types.WhatsAppID) string {
  112. var buf bytes.Buffer
  113. bc.usernameTemplate.Execute(&buf, userID)
  114. return buf.String()
  115. }
  116. type CommunityTemplateArgs struct {
  117. Localpart string
  118. Server string
  119. }
  120. func (bc BridgeConfig) EnableCommunities() bool {
  121. return bc.communityTemplate != nil
  122. }
  123. func (bc BridgeConfig) FormatCommunity(localpart, server string) string {
  124. var buf bytes.Buffer
  125. bc.communityTemplate.Execute(&buf, CommunityTemplateArgs{localpart, server})
  126. return buf.String()
  127. }
  128. type PermissionConfig map[string]PermissionLevel
  129. type PermissionLevel int
  130. const (
  131. PermissionLevelDefault PermissionLevel = 0
  132. PermissionLevelUser PermissionLevel = 10
  133. PermissionLevelAdmin PermissionLevel = 100
  134. )
  135. func (pc *PermissionConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  136. rawPC := make(map[string]string)
  137. err := unmarshal(&rawPC)
  138. if err != nil {
  139. return err
  140. }
  141. if *pc == nil {
  142. *pc = make(map[string]PermissionLevel)
  143. }
  144. for key, value := range rawPC {
  145. switch strings.ToLower(value) {
  146. case "user":
  147. (*pc)[key] = PermissionLevelUser
  148. case "admin":
  149. (*pc)[key] = PermissionLevelAdmin
  150. default:
  151. val, err := strconv.Atoi(value)
  152. if err != nil {
  153. (*pc)[key] = PermissionLevelDefault
  154. } else {
  155. (*pc)[key] = PermissionLevel(val)
  156. }
  157. }
  158. }
  159. return nil
  160. }
  161. func (pc *PermissionConfig) MarshalYAML() (interface{}, error) {
  162. if *pc == nil {
  163. return nil, nil
  164. }
  165. rawPC := make(map[string]string)
  166. for key, value := range *pc {
  167. switch value {
  168. case PermissionLevelUser:
  169. rawPC[key] = "user"
  170. case PermissionLevelAdmin:
  171. rawPC[key] = "admin"
  172. default:
  173. rawPC[key] = strconv.Itoa(int(value))
  174. }
  175. }
  176. return rawPC, nil
  177. }
  178. func (pc PermissionConfig) IsWhitelisted(userID string) bool {
  179. return pc.GetPermissionLevel(userID) >= 10
  180. }
  181. func (pc PermissionConfig) IsAdmin(userID string) bool {
  182. return pc.GetPermissionLevel(userID) >= 100
  183. }
  184. func (pc PermissionConfig) GetPermissionLevel(userID string) PermissionLevel {
  185. permissions, ok := pc[userID]
  186. if ok {
  187. return permissions
  188. }
  189. _, homeserver := appservice.ParseUserID(userID)
  190. permissions, ok = pc[homeserver]
  191. if len(homeserver) > 0 && ok {
  192. return permissions
  193. }
  194. permissions, ok = pc["*"]
  195. if ok {
  196. return permissions
  197. }
  198. return PermissionLevelDefault
  199. }