formatting.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2018 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"
  23. "maunium.net/go/mautrix/format"
  24. "maunium.net/go/mautrix-whatsapp/types"
  25. "maunium.net/go/mautrix-whatsapp/whatsapp-ext"
  26. )
  27. var italicRegex = regexp.MustCompile("([\\s>~*]|^)_(.+?)_([^a-zA-Z\\d]|$)")
  28. var boldRegex = regexp.MustCompile("([\\s>_~]|^)\\*(.+?)\\*([^a-zA-Z\\d]|$)")
  29. var strikethroughRegex = regexp.MustCompile("([\\s>_*]|^)~(.+?)~([^a-zA-Z\\d]|$)")
  30. var codeBlockRegex = regexp.MustCompile("```(?:.|\n)+?```")
  31. var mentionRegex = regexp.MustCompile("@[0-9]+")
  32. type Formatter struct {
  33. bridge *Bridge
  34. matrixHTMLParser *format.HTMLParser
  35. waReplString map[*regexp.Regexp]string
  36. waReplFunc map[*regexp.Regexp]func(string) string
  37. waReplFuncText map[*regexp.Regexp]func(string) string
  38. }
  39. func NewFormatter(bridge *Bridge) *Formatter {
  40. formatter := &Formatter{
  41. bridge: bridge,
  42. matrixHTMLParser: &format.HTMLParser{
  43. TabsToSpaces: 4,
  44. Newline: "\n",
  45. PillConverter: func(mxid, eventID string) string {
  46. if mxid[0] == '@' {
  47. puppet := bridge.GetPuppetByMXID(mxid)
  48. fmt.Println(mxid, puppet)
  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 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, displayname string) {
  99. if user := formatter.bridge.GetUserByJID(jid); user != nil {
  100. mxid = user.MXID
  101. displayname = 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 *mautrix.Content) {
  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. content.FormattedBody = output
  118. content.Format = mautrix.FormatHTML
  119. for regex, replacer := range formatter.waReplFuncText {
  120. content.Body = regex.ReplaceAllStringFunc(content.Body, replacer)
  121. }
  122. }
  123. }
  124. func (formatter *Formatter) ParseMatrix(html string) string {
  125. return formatter.matrixHTMLParser.Parse(html)
  126. }