bridge.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2021 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. "fmt"
  19. "strconv"
  20. "strings"
  21. "text/template"
  22. "go.mau.fi/whatsmeow/types"
  23. "maunium.net/go/mautrix/event"
  24. "maunium.net/go/mautrix/id"
  25. )
  26. type DeferredConfig struct {
  27. StartDaysAgo int `yaml:"start_days_ago"`
  28. MaxBatchEvents int `yaml:"max_batch_events"`
  29. BatchDelay int `yaml:"batch_delay"`
  30. }
  31. type MediaRequestMethod string
  32. const (
  33. MediaRequestMethodImmediate MediaRequestMethod = "immediate"
  34. MediaRequestMethodLocalTime = "local_time"
  35. )
  36. type BridgeConfig struct {
  37. UsernameTemplate string `yaml:"username_template"`
  38. DisplaynameTemplate string `yaml:"displayname_template"`
  39. PersonalFilteringSpaces bool `yaml:"personal_filtering_spaces"`
  40. DeliveryReceipts bool `yaml:"delivery_receipts"`
  41. PortalMessageBuffer int `yaml:"portal_message_buffer"`
  42. CallStartNotices bool `yaml:"call_start_notices"`
  43. IdentityChangeNotices bool `yaml:"identity_change_notices"`
  44. HistorySync struct {
  45. CreatePortals bool `yaml:"create_portals"`
  46. Backfill bool `yaml:"backfill"`
  47. DoublePuppetBackfill bool `yaml:"double_puppet_backfill"`
  48. RequestFullSync bool `yaml:"request_full_sync"`
  49. MaxInitialConversations int `yaml:"max_initial_conversations"`
  50. Immediate struct {
  51. WorkerCount int `yaml:"worker_count"`
  52. MaxEvents int `yaml:"max_events"`
  53. } `yaml:"immediate"`
  54. MediaRequests struct {
  55. AutoRequestMedia bool `yaml:"auto_request_media"`
  56. RequestMethod MediaRequestMethod `yaml:"request_method"`
  57. RequestLocalTime int `yaml:"request_local_time"`
  58. } `yaml:"media_requests"`
  59. Deferred []DeferredConfig `yaml:"deferred"`
  60. } `yaml:"history_sync"`
  61. UserAvatarSync bool `yaml:"user_avatar_sync"`
  62. BridgeMatrixLeave bool `yaml:"bridge_matrix_leave"`
  63. SyncWithCustomPuppets bool `yaml:"sync_with_custom_puppets"`
  64. SyncDirectChatList bool `yaml:"sync_direct_chat_list"`
  65. DefaultBridgeReceipts bool `yaml:"default_bridge_receipts"`
  66. DefaultBridgePresence bool `yaml:"default_bridge_presence"`
  67. SendPresenceOnTyping bool `yaml:"send_presence_on_typing"`
  68. ForceActiveDeliveryReceipts bool `yaml:"force_active_delivery_receipts"`
  69. DoublePuppetServerMap map[string]string `yaml:"double_puppet_server_map"`
  70. DoublePuppetAllowDiscovery bool `yaml:"double_puppet_allow_discovery"`
  71. LoginSharedSecretMap map[string]string `yaml:"login_shared_secret_map"`
  72. PrivateChatPortalMeta bool `yaml:"private_chat_portal_meta"`
  73. BridgeNotices bool `yaml:"bridge_notices"`
  74. ResendBridgeInfo bool `yaml:"resend_bridge_info"`
  75. MuteBridging bool `yaml:"mute_bridging"`
  76. ArchiveTag string `yaml:"archive_tag"`
  77. PinnedTag string `yaml:"pinned_tag"`
  78. TagOnlyOnCreate bool `yaml:"tag_only_on_create"`
  79. MarkReadOnlyOnCreate bool `yaml:"mark_read_only_on_create"`
  80. EnableStatusBroadcast bool `yaml:"enable_status_broadcast"`
  81. MuteStatusBroadcast bool `yaml:"mute_status_broadcast"`
  82. StatusBroadcastTag string `yaml:"status_broadcast_tag"`
  83. WhatsappThumbnail bool `yaml:"whatsapp_thumbnail"`
  84. AllowUserInvite bool `yaml:"allow_user_invite"`
  85. FederateRooms bool `yaml:"federate_rooms"`
  86. URLPreviews bool `yaml:"url_previews"`
  87. DisappearingMessagesInGroups bool `yaml:"disappearing_messages_in_groups"`
  88. DisableBridgeAlerts bool `yaml:"disable_bridge_alerts"`
  89. CommandPrefix string `yaml:"command_prefix"`
  90. ManagementRoomText struct {
  91. Welcome string `yaml:"welcome"`
  92. WelcomeConnected string `yaml:"welcome_connected"`
  93. WelcomeUnconnected string `yaml:"welcome_unconnected"`
  94. AdditionalHelp string `yaml:"additional_help"`
  95. } `yaml:"management_room_text"`
  96. Encryption struct {
  97. Allow bool `yaml:"allow"`
  98. Default bool `yaml:"default"`
  99. KeySharing struct {
  100. Allow bool `yaml:"allow"`
  101. RequireCrossSigning bool `yaml:"require_cross_signing"`
  102. RequireVerification bool `yaml:"require_verification"`
  103. } `yaml:"key_sharing"`
  104. } `yaml:"encryption"`
  105. Permissions PermissionConfig `yaml:"permissions"`
  106. Relay RelaybotConfig `yaml:"relay"`
  107. usernameTemplate *template.Template `yaml:"-"`
  108. displaynameTemplate *template.Template `yaml:"-"`
  109. }
  110. type umBridgeConfig BridgeConfig
  111. func (bc *BridgeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  112. err := unmarshal((*umBridgeConfig)(bc))
  113. if err != nil {
  114. return err
  115. }
  116. bc.usernameTemplate, err = template.New("username").Parse(bc.UsernameTemplate)
  117. if err != nil {
  118. return err
  119. } else if !strings.Contains(bc.FormatUsername("1234567890"), "1234567890") {
  120. return fmt.Errorf("username template is missing user ID placeholder")
  121. }
  122. bc.displaynameTemplate, err = template.New("displayname").Parse(bc.DisplaynameTemplate)
  123. if err != nil {
  124. return err
  125. }
  126. return nil
  127. }
  128. type UsernameTemplateArgs struct {
  129. UserID id.UserID
  130. }
  131. type legacyContactInfo struct {
  132. types.ContactInfo
  133. Phone string
  134. Notify string
  135. VName string
  136. Name string
  137. Short string
  138. JID string
  139. }
  140. func (bc BridgeConfig) FormatDisplayname(jid types.JID, contact types.ContactInfo) (string, int8) {
  141. var buf strings.Builder
  142. _ = bc.displaynameTemplate.Execute(&buf, legacyContactInfo{
  143. ContactInfo: contact,
  144. Notify: contact.PushName,
  145. VName: contact.BusinessName,
  146. Name: contact.FullName,
  147. Short: contact.FirstName,
  148. Phone: "+" + jid.User,
  149. JID: "+" + jid.User,
  150. })
  151. var quality int8
  152. switch {
  153. case len(contact.PushName) > 0 || len(contact.BusinessName) > 0:
  154. quality = 3
  155. case len(contact.FullName) > 0 || len(contact.FirstName) > 0:
  156. quality = 2
  157. default:
  158. quality = 1
  159. }
  160. return buf.String(), quality
  161. }
  162. func (bc BridgeConfig) FormatUsername(username string) string {
  163. var buf strings.Builder
  164. _ = bc.usernameTemplate.Execute(&buf, username)
  165. return buf.String()
  166. }
  167. type PermissionConfig map[string]PermissionLevel
  168. type PermissionLevel int
  169. const (
  170. PermissionLevelDefault PermissionLevel = 0
  171. PermissionLevelRelay PermissionLevel = 5
  172. PermissionLevelUser PermissionLevel = 10
  173. PermissionLevelAdmin PermissionLevel = 100
  174. )
  175. func (pc *PermissionConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  176. rawPC := make(map[string]string)
  177. err := unmarshal(&rawPC)
  178. if err != nil {
  179. return err
  180. }
  181. if *pc == nil {
  182. *pc = make(map[string]PermissionLevel)
  183. }
  184. for key, value := range rawPC {
  185. switch strings.ToLower(value) {
  186. case "relaybot", "relay":
  187. (*pc)[key] = PermissionLevelRelay
  188. case "user":
  189. (*pc)[key] = PermissionLevelUser
  190. case "admin":
  191. (*pc)[key] = PermissionLevelAdmin
  192. default:
  193. val, err := strconv.Atoi(value)
  194. if err != nil {
  195. (*pc)[key] = PermissionLevelDefault
  196. } else {
  197. (*pc)[key] = PermissionLevel(val)
  198. }
  199. }
  200. }
  201. return nil
  202. }
  203. func (pc *PermissionConfig) MarshalYAML() (interface{}, error) {
  204. if *pc == nil {
  205. return nil, nil
  206. }
  207. rawPC := make(map[string]string)
  208. for key, value := range *pc {
  209. switch value {
  210. case PermissionLevelRelay:
  211. rawPC[key] = "relay"
  212. case PermissionLevelUser:
  213. rawPC[key] = "user"
  214. case PermissionLevelAdmin:
  215. rawPC[key] = "admin"
  216. default:
  217. rawPC[key] = strconv.Itoa(int(value))
  218. }
  219. }
  220. return rawPC, nil
  221. }
  222. func (pc PermissionConfig) IsRelayWhitelisted(userID id.UserID) bool {
  223. return pc.GetPermissionLevel(userID) >= PermissionLevelRelay
  224. }
  225. func (pc PermissionConfig) IsWhitelisted(userID id.UserID) bool {
  226. return pc.GetPermissionLevel(userID) >= PermissionLevelUser
  227. }
  228. func (pc PermissionConfig) IsAdmin(userID id.UserID) bool {
  229. return pc.GetPermissionLevel(userID) >= PermissionLevelAdmin
  230. }
  231. func (pc PermissionConfig) GetPermissionLevel(userID id.UserID) PermissionLevel {
  232. permissions, ok := pc[string(userID)]
  233. if ok {
  234. return permissions
  235. }
  236. _, homeserver, _ := userID.Parse()
  237. permissions, ok = pc[homeserver]
  238. if len(homeserver) > 0 && ok {
  239. return permissions
  240. }
  241. permissions, ok = pc["*"]
  242. if ok {
  243. return permissions
  244. }
  245. return PermissionLevelDefault
  246. }
  247. type RelaybotConfig struct {
  248. Enabled bool `yaml:"enabled"`
  249. AdminOnly bool `yaml:"admin_only"`
  250. MessageFormats map[event.MessageType]string `yaml:"message_formats"`
  251. messageTemplates *template.Template `yaml:"-"`
  252. }
  253. type umRelaybotConfig RelaybotConfig
  254. func (rc *RelaybotConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  255. err := unmarshal((*umRelaybotConfig)(rc))
  256. if err != nil {
  257. return err
  258. }
  259. rc.messageTemplates = template.New("messageTemplates")
  260. for key, format := range rc.MessageFormats {
  261. _, err := rc.messageTemplates.New(string(key)).Parse(format)
  262. if err != nil {
  263. return err
  264. }
  265. }
  266. return nil
  267. }
  268. type Sender struct {
  269. UserID string
  270. event.MemberEventContent
  271. }
  272. type formatData struct {
  273. Sender Sender
  274. Message string
  275. Content *event.MessageEventContent
  276. }
  277. func (rc *RelaybotConfig) FormatMessage(content *event.MessageEventContent, sender id.UserID, member event.MemberEventContent) (string, error) {
  278. if len(member.Displayname) == 0 {
  279. member.Displayname = sender.String()
  280. }
  281. member.Displayname = template.HTMLEscapeString(member.Displayname)
  282. var output strings.Builder
  283. err := rc.messageTemplates.ExecuteTemplate(&output, string(content.MsgType), formatData{
  284. Sender: Sender{
  285. UserID: template.HTMLEscapeString(sender.String()),
  286. MemberEventContent: member,
  287. },
  288. Content: content,
  289. Message: content.FormattedBody,
  290. })
  291. return output.String(), err
  292. }