formatting.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2020 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 main
  17. import (
  18. "fmt"
  19. "html"
  20. "regexp"
  21. "strings"
  22. "maunium.net/go/mautrix/event"
  23. "maunium.net/go/mautrix/format"
  24. "maunium.net/go/mautrix/id"
  25. "maunium.net/go/mautrix-whatsapp/types"
  26. "maunium.net/go/mautrix-whatsapp/whatsapp-ext"
  27. )
  28. var italicRegex = regexp.MustCompile("([\\s>~*]|^)_(.+?)_([^a-zA-Z\\d]|$)")
  29. var boldRegex = regexp.MustCompile("([\\s>_~]|^)\\*(.+?)\\*([^a-zA-Z\\d]|$)")
  30. var strikethroughRegex = regexp.MustCompile("([\\s>_*]|^)~(.+?)~([^a-zA-Z\\d]|$)")
  31. var codeBlockRegex = regexp.MustCompile("```(?:.|\n)+?```")
  32. var mentionRegex = regexp.MustCompile("@[0-9]+")
  33. type Formatter struct {
  34. bridge *Bridge
  35. matrixHTMLParser *format.HTMLParser
  36. waReplString map[*regexp.Regexp]string
  37. waReplFunc map[*regexp.Regexp]func(string) string
  38. waReplFuncText map[*regexp.Regexp]func(string) string
  39. }
  40. func NewFormatter(bridge *Bridge) *Formatter {
  41. formatter := &Formatter{
  42. bridge: bridge,
  43. matrixHTMLParser: &format.HTMLParser{
  44. TabsToSpaces: 4,
  45. Newline: "\n",
  46. PillConverter: func(mxid, eventID string) string {
  47. if mxid[0] == '@' {
  48. puppet := bridge.GetPuppetByMXID(id.UserID(mxid))
  49. if puppet != nil {
  50. return "@" + puppet.PhoneNumber()
  51. }
  52. }
  53. return mxid
  54. },
  55. BoldConverter: func(text string) string {
  56. return fmt.Sprintf("*%s*", text)
  57. },
  58. ItalicConverter: func(text string) string {
  59. return fmt.Sprintf("_%s_", text)
  60. },
  61. StrikethroughConverter: func(text string) string {
  62. return fmt.Sprintf("~%s~", text)
  63. },
  64. MonospaceConverter: func(text string) string {
  65. return fmt.Sprintf("```%s```", text)
  66. },
  67. MonospaceBlockConverter: func(text, language string) string {
  68. return fmt.Sprintf("```%s```", text)
  69. },
  70. },
  71. waReplString: map[*regexp.Regexp]string{
  72. italicRegex: "$1<em>$2</em>$3",
  73. boldRegex: "$1<strong>$2</strong>$3",
  74. strikethroughRegex: "$1<del>$2</del>$3",
  75. },
  76. }
  77. formatter.waReplFunc = map[*regexp.Regexp]func(string) string{
  78. codeBlockRegex: func(str string) string {
  79. str = str[3 : len(str)-3]
  80. if strings.ContainsRune(str, '\n') {
  81. return fmt.Sprintf("<pre><code>%s</code></pre>", str)
  82. }
  83. return fmt.Sprintf("<code>%s</code>", str)
  84. },
  85. mentionRegex: func(str string) string {
  86. mxid, displayname := formatter.getMatrixInfoByJID(str[1:] + whatsappExt.NewUserSuffix)
  87. return fmt.Sprintf(`<a href="https://matrix.to/#/%s">%s</a>`, mxid, displayname)
  88. },
  89. }
  90. formatter.waReplFuncText = map[*regexp.Regexp]func(string) string{
  91. mentionRegex: func(str string) string {
  92. _, displayname := formatter.getMatrixInfoByJID(str[1:] + whatsappExt.NewUserSuffix)
  93. return displayname
  94. },
  95. }
  96. return formatter
  97. }
  98. func (formatter *Formatter) getMatrixInfoByJID(jid types.WhatsAppID) (mxid id.UserID, displayname string) {
  99. if user := formatter.bridge.GetUserByJID(jid); user != nil {
  100. mxid = user.MXID
  101. displayname = string(user.MXID)
  102. } else if puppet := formatter.bridge.GetPuppetByJID(jid); puppet != nil {
  103. mxid = puppet.MXID
  104. displayname = puppet.Displayname
  105. }
  106. return
  107. }
  108. func (formatter *Formatter) ParseWhatsApp(content *event.MessageEventContent) {
  109. output := html.EscapeString(content.Body)
  110. for regex, replacement := range formatter.waReplString {
  111. output = regex.ReplaceAllString(output, replacement)
  112. }
  113. for regex, replacer := range formatter.waReplFunc {
  114. output = regex.ReplaceAllStringFunc(output, replacer)
  115. }
  116. if output != content.Body {
  117. output = strings.Replace(output, "\n", "<br/>", -1)
  118. content.FormattedBody = output
  119. content.Format = event.FormatHTML
  120. for regex, replacer := range formatter.waReplFuncText {
  121. content.Body = regex.ReplaceAllStringFunc(content.Body, replacer)
  122. }
  123. }
  124. }
  125. func (formatter *Formatter) ParseMatrix(html string) string {
  126. return formatter.matrixHTMLParser.Parse(html)
  127. }