formatting.go 4.3 KB

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