| 1 | /* |
| 2 | variables available |
| 3 | read-only: |
| 4 | msgUserID, msgAccount, msgChannel, msgProtocol |
| 5 | |
| 6 | read-write: |
| 7 | msgText, msgUsername |
| 8 | */ |
| 9 | |
| 10 | fmt := import("fmt") |
| 11 | text := import("text") |
| 12 | |
| 13 | doQuote_ := func(reply, nick, ctx) { |
| 14 | return reply + " (in reply to " + nick + ": " + ctx + ")" |
| 15 | } |
| 16 | |
| 17 | stripQuote := func(ctx) { |
| 18 | re := text.re_compile(`(?m)^> (?:-# )?↪ .+\n(?:> .*\n)+(.*)`) |
| 19 | if re.match(ctx) { return re.replace(ctx, "$1") } |
| 20 | re = text.re_compile(`(?s)^(.*?) \(in reply to .*?: .*\)$`) |
| 21 | if re.match(ctx) { return re.replace(ctx, "$1") } |
| 22 | return ctx |
| 23 | } |
| 24 | |
| 25 | doQuote := func(...reStrs) { |
| 26 | for reStr in reStrs { |
| 27 | re := text.re_compile(reStr) |
| 28 | if re.match(msgText) { |
| 29 | reply := re.replace(msgText, "$1") |
| 30 | nick := re.replace(msgText, "$2") |
| 31 | ctx := re.replace(msgText, "$3") |
| 32 | ctx = stripQuote(ctx) |
| 33 | msgText = doQuote_(reply, nick, ctx) |
| 34 | break |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | stripEmbed := func() { |
| 40 | if !text.contains(msgText, " embed: ") { return } |
| 41 | |
| 42 | re := text.re_compile(`^([^ ]+) embed: ([^ ]+)$`) |
| 43 | if !re.match(msgText) { return } |
| 44 | |
| 45 | a := re.replace(msgText, "$1") |
| 46 | b := re.replace(msgText, "$2") |
| 47 | |
| 48 | if a == b { |
| 49 | msgText = a |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if msgText != "" { |
| 54 | if msgProtocol == "discord" { |
| 55 | doQuote(`(?s)(.*?) \(in reply to (.*?): (.*?)\)$`) |
| 56 | stripEmbed() |
| 57 | } else if msgProtocol == "xmpp" { |
| 58 | doQuote( |
| 59 | `(?s)(.*?) \(in reply to (.*?) \[b]: (.*?)\)$`, |
| 60 | `(?s)(.*?) \(in reply to (.*?): (.*?)\)$` |
| 61 | ) |
| 62 | } else if msgProtocol == "telegram" { |
| 63 | doQuote( |
| 64 | `(?s)(.*?) \(in reply to lulbridge: <([^>]+)> (.*?)\)$`, |
| 65 | `(?s)(.*?) \(in reply to (.*?): (.*?)\)$` |
| 66 | ) |
| 67 | } |
| 68 | } |
| 69 | |