bridge.go 6.2 KB

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