bridge.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2023 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. "errors"
  19. "fmt"
  20. "strings"
  21. "text/template"
  22. "time"
  23. "go.mau.fi/whatsmeow/types"
  24. "maunium.net/go/mautrix/bridge/bridgeconfig"
  25. "maunium.net/go/mautrix/event"
  26. "maunium.net/go/mautrix/id"
  27. )
  28. type DeferredConfig struct {
  29. StartDaysAgo int `yaml:"start_days_ago"`
  30. MaxBatchEvents int `yaml:"max_batch_events"`
  31. BatchDelay int `yaml:"batch_delay"`
  32. }
  33. type MediaRequestMethod string
  34. const (
  35. MediaRequestMethodImmediate MediaRequestMethod = "immediate"
  36. MediaRequestMethodLocalTime = "local_time"
  37. )
  38. type BridgeConfig struct {
  39. UsernameTemplate string `yaml:"username_template"`
  40. DisplaynameTemplate string `yaml:"displayname_template"`
  41. PersonalFilteringSpaces bool `yaml:"personal_filtering_spaces"`
  42. DeliveryReceipts bool `yaml:"delivery_receipts"`
  43. MessageStatusEvents bool `yaml:"message_status_events"`
  44. MessageErrorNotices bool `yaml:"message_error_notices"`
  45. PortalMessageBuffer int `yaml:"portal_message_buffer"`
  46. CallStartNotices bool `yaml:"call_start_notices"`
  47. IdentityChangeNotices bool `yaml:"identity_change_notices"`
  48. HistorySync struct {
  49. Backfill bool `yaml:"backfill"`
  50. RequestFullSync bool `yaml:"request_full_sync"`
  51. FullSyncConfig struct {
  52. DaysLimit uint32 `yaml:"days_limit"`
  53. SizeLimit uint32 `yaml:"size_mb_limit"`
  54. StorageQuota uint32 `yaml:"storage_quota_mb"`
  55. }
  56. MaxInitialConversations int `yaml:"max_initial_conversations"`
  57. MessageCount int `yaml:"message_count"`
  58. UnreadHoursThreshold int `yaml:"unread_hours_threshold"`
  59. Immediate struct {
  60. WorkerCount int `yaml:"worker_count"`
  61. MaxEvents int `yaml:"max_events"`
  62. } `yaml:"immediate"`
  63. MediaRequests struct {
  64. AutoRequestMedia bool `yaml:"auto_request_media"`
  65. RequestMethod MediaRequestMethod `yaml:"request_method"`
  66. RequestLocalTime int `yaml:"request_local_time"`
  67. } `yaml:"media_requests"`
  68. Deferred []DeferredConfig `yaml:"deferred"`
  69. } `yaml:"history_sync"`
  70. UserAvatarSync bool `yaml:"user_avatar_sync"`
  71. BridgeMatrixLeave bool `yaml:"bridge_matrix_leave"`
  72. SyncDirectChatList bool `yaml:"sync_direct_chat_list"`
  73. SyncManualMarkedUnread bool `yaml:"sync_manual_marked_unread"`
  74. DefaultBridgePresence bool `yaml:"default_bridge_presence"`
  75. SendPresenceOnTyping bool `yaml:"send_presence_on_typing"`
  76. ForceActiveDeliveryReceipts bool `yaml:"force_active_delivery_receipts"`
  77. DoublePuppetConfig bridgeconfig.DoublePuppetConfig `yaml:",inline"`
  78. PrivateChatPortalMeta string `yaml:"private_chat_portal_meta"`
  79. ParallelMemberSync bool `yaml:"parallel_member_sync"`
  80. BridgeNotices bool `yaml:"bridge_notices"`
  81. ResendBridgeInfo bool `yaml:"resend_bridge_info"`
  82. MuteBridging bool `yaml:"mute_bridging"`
  83. ArchiveTag string `yaml:"archive_tag"`
  84. PinnedTag string `yaml:"pinned_tag"`
  85. TagOnlyOnCreate bool `yaml:"tag_only_on_create"`
  86. MarkReadOnlyOnCreate bool `yaml:"mark_read_only_on_create"`
  87. EnableStatusBroadcast bool `yaml:"enable_status_broadcast"`
  88. MuteStatusBroadcast bool `yaml:"mute_status_broadcast"`
  89. StatusBroadcastTag string `yaml:"status_broadcast_tag"`
  90. WhatsappThumbnail bool `yaml:"whatsapp_thumbnail"`
  91. AllowUserInvite bool `yaml:"allow_user_invite"`
  92. FederateRooms bool `yaml:"federate_rooms"`
  93. URLPreviews bool `yaml:"url_previews"`
  94. CaptionInMessage bool `yaml:"caption_in_message"`
  95. BeeperGalleries bool `yaml:"beeper_galleries"`
  96. ExtEvPolls bool `yaml:"extev_polls"`
  97. CrossRoomReplies bool `yaml:"cross_room_replies"`
  98. DisableReplyFallbacks bool `yaml:"disable_reply_fallbacks"`
  99. MessageHandlingTimeout struct {
  100. ErrorAfterStr string `yaml:"error_after"`
  101. DeadlineStr string `yaml:"deadline"`
  102. ErrorAfter time.Duration `yaml:"-"`
  103. Deadline time.Duration `yaml:"-"`
  104. } `yaml:"message_handling_timeout"`
  105. DisableStatusBroadcastSend bool `yaml:"disable_status_broadcast_send"`
  106. DisableBridgeAlerts bool `yaml:"disable_bridge_alerts"`
  107. CrashOnStreamReplaced bool `yaml:"crash_on_stream_replaced"`
  108. CommandPrefix string `yaml:"command_prefix"`
  109. ManagementRoomText bridgeconfig.ManagementRoomTexts `yaml:"management_room_text"`
  110. Encryption bridgeconfig.EncryptionConfig `yaml:"encryption"`
  111. Provisioning struct {
  112. Prefix string `yaml:"prefix"`
  113. SharedSecret string `yaml:"shared_secret"`
  114. } `yaml:"provisioning"`
  115. Permissions bridgeconfig.PermissionConfig `yaml:"permissions"`
  116. Relay RelaybotConfig `yaml:"relay"`
  117. ParsedUsernameTemplate *template.Template `yaml:"-"`
  118. displaynameTemplate *template.Template `yaml:"-"`
  119. }
  120. func (bc BridgeConfig) GetDoublePuppetConfig() bridgeconfig.DoublePuppetConfig {
  121. return bc.DoublePuppetConfig
  122. }
  123. func (bc BridgeConfig) GetEncryptionConfig() bridgeconfig.EncryptionConfig {
  124. return bc.Encryption
  125. }
  126. func (bc BridgeConfig) EnableMessageStatusEvents() bool {
  127. return bc.MessageStatusEvents
  128. }
  129. func (bc BridgeConfig) EnableMessageErrorNotices() bool {
  130. return bc.MessageErrorNotices
  131. }
  132. func (bc BridgeConfig) GetCommandPrefix() string {
  133. return bc.CommandPrefix
  134. }
  135. func (bc BridgeConfig) GetManagementRoomTexts() bridgeconfig.ManagementRoomTexts {
  136. return bc.ManagementRoomText
  137. }
  138. func (bc BridgeConfig) GetResendBridgeInfo() bool {
  139. return bc.ResendBridgeInfo
  140. }
  141. func boolToInt(val bool) int {
  142. if val {
  143. return 1
  144. }
  145. return 0
  146. }
  147. func (bc BridgeConfig) Validate() error {
  148. _, hasWildcard := bc.Permissions["*"]
  149. _, hasExampleDomain := bc.Permissions["example.com"]
  150. _, hasExampleUser := bc.Permissions["@admin:example.com"]
  151. exampleLen := boolToInt(hasWildcard) + boolToInt(hasExampleUser) + boolToInt(hasExampleDomain)
  152. if len(bc.Permissions) <= exampleLen {
  153. return errors.New("bridge.permissions not configured")
  154. }
  155. return nil
  156. }
  157. type umBridgeConfig BridgeConfig
  158. func (bc *BridgeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  159. err := unmarshal((*umBridgeConfig)(bc))
  160. if err != nil {
  161. return err
  162. }
  163. bc.ParsedUsernameTemplate, err = template.New("username").Parse(bc.UsernameTemplate)
  164. if err != nil {
  165. return err
  166. } else if !strings.Contains(bc.FormatUsername("1234567890"), "1234567890") {
  167. return fmt.Errorf("username template is missing user ID placeholder")
  168. }
  169. bc.displaynameTemplate, err = template.New("displayname").Parse(bc.DisplaynameTemplate)
  170. if err != nil {
  171. return err
  172. }
  173. if bc.MessageHandlingTimeout.ErrorAfterStr != "" {
  174. bc.MessageHandlingTimeout.ErrorAfter, err = time.ParseDuration(bc.MessageHandlingTimeout.ErrorAfterStr)
  175. if err != nil {
  176. return err
  177. }
  178. }
  179. if bc.MessageHandlingTimeout.DeadlineStr != "" {
  180. bc.MessageHandlingTimeout.Deadline, err = time.ParseDuration(bc.MessageHandlingTimeout.DeadlineStr)
  181. if err != nil {
  182. return err
  183. }
  184. }
  185. return nil
  186. }
  187. type UsernameTemplateArgs struct {
  188. UserID id.UserID
  189. }
  190. type legacyContactInfo struct {
  191. types.ContactInfo
  192. Phone string
  193. Notify string
  194. VName string
  195. Name string
  196. Short string
  197. JID string
  198. }
  199. const (
  200. NameQualityPush = 3
  201. NameQualityContact = 2
  202. NameQualityPhone = 1
  203. )
  204. func (bc BridgeConfig) FormatDisplayname(jid types.JID, contact types.ContactInfo) (string, int8) {
  205. var buf strings.Builder
  206. _ = bc.displaynameTemplate.Execute(&buf, legacyContactInfo{
  207. ContactInfo: contact,
  208. Notify: contact.PushName,
  209. VName: contact.BusinessName,
  210. Name: contact.FullName,
  211. Short: contact.FirstName,
  212. Phone: "+" + jid.User,
  213. JID: "+" + jid.User,
  214. })
  215. var quality int8
  216. switch {
  217. case len(contact.PushName) > 0 || len(contact.BusinessName) > 0:
  218. quality = NameQualityPush
  219. case len(contact.FullName) > 0 || len(contact.FirstName) > 0:
  220. quality = NameQualityContact
  221. default:
  222. quality = NameQualityPhone
  223. }
  224. return buf.String(), quality
  225. }
  226. func (bc BridgeConfig) FormatUsername(username string) string {
  227. var buf strings.Builder
  228. _ = bc.ParsedUsernameTemplate.Execute(&buf, username)
  229. return buf.String()
  230. }
  231. type RelaybotConfig struct {
  232. Enabled bool `yaml:"enabled"`
  233. AdminOnly bool `yaml:"admin_only"`
  234. MessageFormats map[event.MessageType]string `yaml:"message_formats"`
  235. messageTemplates *template.Template `yaml:"-"`
  236. }
  237. type umRelaybotConfig RelaybotConfig
  238. func (rc *RelaybotConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  239. err := unmarshal((*umRelaybotConfig)(rc))
  240. if err != nil {
  241. return err
  242. }
  243. rc.messageTemplates = template.New("messageTemplates")
  244. for key, format := range rc.MessageFormats {
  245. _, err := rc.messageTemplates.New(string(key)).Parse(format)
  246. if err != nil {
  247. return err
  248. }
  249. }
  250. return nil
  251. }
  252. type Sender struct {
  253. UserID string
  254. event.MemberEventContent
  255. }
  256. type formatData struct {
  257. Sender Sender
  258. Message string
  259. Content *event.MessageEventContent
  260. }
  261. func (rc *RelaybotConfig) FormatMessage(content *event.MessageEventContent, sender id.UserID, member event.MemberEventContent) (string, error) {
  262. if len(member.Displayname) == 0 {
  263. member.Displayname = sender.String()
  264. }
  265. member.Displayname = template.HTMLEscapeString(member.Displayname)
  266. var output strings.Builder
  267. err := rc.messageTemplates.ExecuteTemplate(&output, string(content.MsgType), formatData{
  268. Sender: Sender{
  269. UserID: template.HTMLEscapeString(sender.String()),
  270. MemberEventContent: member,
  271. },
  272. Content: content,
  273. Message: content.FormattedBody,
  274. })
  275. return output.String(), err
  276. }