bridge.go 6.3 KB

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