Initial commit
This commit is contained in:
commit
d572522a96
20 changed files with 2075 additions and 0 deletions
45
matrix/event.go
Normal file
45
matrix/event.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
// maubot - A plugin-based Matrix bot system written in Go.
|
||||
// Copyright (C) 2018 Tulir Asokan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package matrix
|
||||
|
||||
import (
|
||||
"maunium.net/go/gomatrix"
|
||||
)
|
||||
|
||||
type Event struct {
|
||||
*gomatrix.Event
|
||||
Client *Client
|
||||
}
|
||||
|
||||
func (evt *Event) Reply(text string) (string, error) {
|
||||
return evt.SendEvent(
|
||||
SetReply(
|
||||
RenderMarkdown(text),
|
||||
evt.Event))
|
||||
}
|
||||
|
||||
func (evt *Event) SendMessage(text string) (string, error) {
|
||||
return evt.SendEvent(RenderMarkdown(text))
|
||||
}
|
||||
|
||||
func (evt *Event) SendEvent(content map[string]interface{}) (string, error) {
|
||||
resp, err := evt.Client.SendMessageEvent(evt.RoomID, "m.room.message", content)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return resp.EventID, nil
|
||||
}
|
229
matrix/htmltotext.go
Normal file
229
matrix/htmltotext.go
Normal file
|
@ -0,0 +1,229 @@
|
|||
// maubot - A plugin-based Matrix bot system written in Go.
|
||||
// Copyright (C) 2018 Tulir Asokan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package matrix
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var matrixToURL = regexp.MustCompile("^(?:https?://)?(?:www\\.)?matrix\\.to/#/([#@!].*)")
|
||||
|
||||
type htmlParser struct {}
|
||||
|
||||
type taggedString struct {
|
||||
string
|
||||
tag string
|
||||
}
|
||||
|
||||
func (parser *htmlParser) getAttribute(node *html.Node, attribute string) string {
|
||||
for _, attr := range node.Attr {
|
||||
if attr.Key == attribute {
|
||||
return attr.Val
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func digits(num int) int {
|
||||
return int(math.Floor(math.Log10(float64(num))) + 1)
|
||||
}
|
||||
|
||||
func (parser *htmlParser) listToString(node *html.Node, stripLinebreak bool) string {
|
||||
ordered := node.Data == "ol"
|
||||
taggedChildren := parser.nodeToTaggedStrings(node.FirstChild, stripLinebreak)
|
||||
counter := 1
|
||||
indentLength := 0
|
||||
if ordered {
|
||||
start := parser.getAttribute(node, "start")
|
||||
if len(start) > 0 {
|
||||
counter, _ = strconv.Atoi(start)
|
||||
}
|
||||
|
||||
longestIndex := (counter - 1) + len(taggedChildren)
|
||||
indentLength = digits(longestIndex)
|
||||
}
|
||||
indent := strings.Repeat(" ", indentLength+2)
|
||||
var children []string
|
||||
for _, child := range taggedChildren {
|
||||
if child.tag != "li" {
|
||||
continue
|
||||
}
|
||||
var prefix string
|
||||
if ordered {
|
||||
indexPadding := indentLength - digits(counter)
|
||||
prefix = fmt.Sprintf("%d. %s", counter, strings.Repeat(" ", indexPadding))
|
||||
} else {
|
||||
prefix = "● "
|
||||
}
|
||||
str := prefix + child.string
|
||||
counter++
|
||||
parts := strings.Split(str, "\n")
|
||||
for i, part := range parts[1:] {
|
||||
parts[i+1] = indent + part
|
||||
}
|
||||
str = strings.Join(parts, "\n")
|
||||
children = append(children, str)
|
||||
}
|
||||
return strings.Join(children, "\n")
|
||||
}
|
||||
|
||||
func (parser *htmlParser) basicFormatToString(node *html.Node, stripLinebreak bool) string {
|
||||
str := parser.nodeToTagAwareString(node.FirstChild, stripLinebreak)
|
||||
switch node.Data {
|
||||
case "b", "strong":
|
||||
return fmt.Sprintf("**%s**", str)
|
||||
case "i", "em":
|
||||
return fmt.Sprintf("_%s_", str)
|
||||
case "s", "del":
|
||||
return fmt.Sprintf("~~%s~~", str)
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
func (parser *htmlParser) headerToString(node *html.Node, stripLinebreak bool) string {
|
||||
children := parser.nodeToStrings(node.FirstChild, stripLinebreak)
|
||||
length := int(node.Data[1] - '0')
|
||||
prefix := strings.Repeat("#", length) + " "
|
||||
return prefix + strings.Join(children, "")
|
||||
}
|
||||
|
||||
func (parser *htmlParser) blockquoteToString(node *html.Node, stripLinebreak bool) string {
|
||||
str := parser.nodeToTagAwareString(node.FirstChild, stripLinebreak)
|
||||
childrenArr := strings.Split(strings.TrimSpace(str), "\n")
|
||||
for index, child := range childrenArr {
|
||||
childrenArr[index] = "> " + child
|
||||
}
|
||||
return strings.Join(childrenArr, "\n")
|
||||
}
|
||||
|
||||
func (parser *htmlParser) linkToString(node *html.Node, stripLinebreak bool) string {
|
||||
str := parser.nodeToTagAwareString(node.FirstChild, stripLinebreak)
|
||||
href := parser.getAttribute(node, "href")
|
||||
if len(href) == 0 {
|
||||
return str
|
||||
}
|
||||
match := matrixToURL.FindStringSubmatch(href)
|
||||
if len(match) == 2 {
|
||||
// pillTarget := match[1]
|
||||
// if pillTarget[0] == '@' {
|
||||
// if member := parser.room.GetMember(pillTarget); member != nil {
|
||||
// return member.DisplayName
|
||||
// }
|
||||
// }
|
||||
// return pillTarget
|
||||
return str
|
||||
}
|
||||
return fmt.Sprintf("%s (%s)", str, href)
|
||||
}
|
||||
|
||||
func (parser *htmlParser) tagToString(node *html.Node, stripLinebreak bool) string {
|
||||
switch node.Data {
|
||||
case "blockquote":
|
||||
return parser.blockquoteToString(node, stripLinebreak)
|
||||
case "ol", "ul":
|
||||
return parser.listToString(node, stripLinebreak)
|
||||
case "h1", "h2", "h3", "h4", "h5", "h6":
|
||||
return parser.headerToString(node, stripLinebreak)
|
||||
case "br":
|
||||
return "\n"
|
||||
case "b", "strong", "i", "em", "s", "del", "u", "ins":
|
||||
return parser.basicFormatToString(node, stripLinebreak)
|
||||
case "a":
|
||||
return parser.linkToString(node, stripLinebreak)
|
||||
case "p":
|
||||
return parser.nodeToTagAwareString(node.FirstChild, stripLinebreak) + "\n"
|
||||
case "pre":
|
||||
return parser.nodeToString(node.FirstChild, false)
|
||||
default:
|
||||
return parser.nodeToTagAwareString(node.FirstChild, stripLinebreak)
|
||||
}
|
||||
}
|
||||
|
||||
func (parser *htmlParser) singleNodeToString(node *html.Node, stripLinebreak bool) taggedString {
|
||||
switch node.Type {
|
||||
case html.TextNode:
|
||||
if stripLinebreak {
|
||||
node.Data = strings.Replace(node.Data, "\n", "", -1)
|
||||
}
|
||||
return taggedString{node.Data, "text"}
|
||||
case html.ElementNode:
|
||||
return taggedString{parser.tagToString(node, stripLinebreak), node.Data}
|
||||
case html.DocumentNode:
|
||||
return taggedString{parser.nodeToTagAwareString(node.FirstChild, stripLinebreak), "html"}
|
||||
default:
|
||||
return taggedString{"", "unknown"}
|
||||
}
|
||||
}
|
||||
|
||||
func (parser *htmlParser) nodeToTaggedStrings(node *html.Node, stripLinebreak bool) (strs []taggedString) {
|
||||
for ; node != nil; node = node.NextSibling {
|
||||
strs = append(strs, parser.singleNodeToString(node, stripLinebreak))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var BlockTags = []string{"p", "h1", "h2", "h3", "h4", "h5", "h6", "ol", "ul", "pre", "blockquote", "div", "hr", "table"}
|
||||
|
||||
func (parser *htmlParser) isBlockTag(tag string) bool {
|
||||
for _, blockTag := range BlockTags {
|
||||
if tag == blockTag {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (parser *htmlParser) nodeToTagAwareString(node *html.Node, stripLinebreak bool) string {
|
||||
strs := parser.nodeToTaggedStrings(node, stripLinebreak)
|
||||
var output strings.Builder
|
||||
for _, str := range strs {
|
||||
tstr := str.string
|
||||
if parser.isBlockTag(str.tag) {
|
||||
tstr = fmt.Sprintf("\n%s\n", tstr)
|
||||
}
|
||||
output.WriteString(tstr)
|
||||
}
|
||||
return strings.TrimSpace(output.String())
|
||||
}
|
||||
|
||||
func (parser *htmlParser) nodeToStrings(node *html.Node, stripLinebreak bool) (strs []string) {
|
||||
for ; node != nil; node = node.NextSibling {
|
||||
strs = append(strs, parser.singleNodeToString(node, stripLinebreak).string)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (parser *htmlParser) nodeToString(node *html.Node, stripLinebreak bool) string {
|
||||
return strings.Join(parser.nodeToStrings(node, stripLinebreak), "")
|
||||
}
|
||||
|
||||
func (parser *htmlParser) Parse(htmlData string) string {
|
||||
node, _ := html.Parse(strings.NewReader(htmlData))
|
||||
return parser.nodeToTagAwareString(node, true)
|
||||
}
|
||||
|
||||
func HTMLToText(html string) string {
|
||||
html = strings.Replace(html, "\t", " ", -1)
|
||||
str := (&htmlParser{}).Parse(html)
|
||||
return str
|
||||
}
|
52
matrix/htmlutil.go
Normal file
52
matrix/htmlutil.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
// maubot - A plugin-based Matrix bot system written in Go.
|
||||
// Copyright (C) 2018 Tulir Asokan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package matrix
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"gopkg.in/russross/blackfriday.v2"
|
||||
)
|
||||
|
||||
func RenderMarkdown(text string) map[string]interface{} {
|
||||
parser := blackfriday.New(
|
||||
blackfriday.WithExtensions(blackfriday.NoIntraEmphasis |
|
||||
blackfriday.Tables |
|
||||
blackfriday.FencedCode |
|
||||
blackfriday.Strikethrough |
|
||||
blackfriday.SpaceHeadings |
|
||||
blackfriday.DefinitionLists))
|
||||
ast := parser.Parse([]byte(text))
|
||||
|
||||
renderer := blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
|
||||
Flags: blackfriday.UseXHTML,
|
||||
})
|
||||
|
||||
var buf strings.Builder
|
||||
renderer.RenderHeader(&buf, ast)
|
||||
ast.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
|
||||
return renderer.RenderNode(&buf, node, entering)
|
||||
})
|
||||
renderer.RenderFooter(&buf, ast)
|
||||
htmlBody := buf.String()
|
||||
|
||||
return map[string]interface{}{
|
||||
"formatted_body": htmlBody,
|
||||
"format": "org.matrix.custom.html",
|
||||
"msgtype": "m.text",
|
||||
"body": HTMLToText(htmlBody),
|
||||
}
|
||||
}
|
71
matrix/matrix.go
Normal file
71
matrix/matrix.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
// maubot - A plugin-based Matrix bot system written in Go.
|
||||
// Copyright (C) 2018 Tulir Asokan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package matrix
|
||||
|
||||
import (
|
||||
"maubot.xyz/database"
|
||||
"maunium.net/go/gomatrix"
|
||||
log "maunium.net/go/maulogger"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
*gomatrix.Client
|
||||
|
||||
DB *database.MatrixClient
|
||||
}
|
||||
|
||||
func NewClient(db *database.MatrixClient) (*Client, error) {
|
||||
mxClient, err := gomatrix.NewClient(db.Homeserver, db.UserID, db.AccessToken)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client := &Client{
|
||||
Client: mxClient,
|
||||
DB: db,
|
||||
}
|
||||
|
||||
client.AddEventHandler(gomatrix.StateMember, client.onJoin)
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (client *Client) AddEventHandler(evt string, handler gomatrix.OnEventListener) {
|
||||
client.Syncer.(*gomatrix.DefaultSyncer).OnEventType(evt, handler)
|
||||
}
|
||||
|
||||
func (client *Client) onJoin(evt *gomatrix.Event) {
|
||||
if !client.DB.AutoJoinRooms || evt.StateKey == nil || *evt.StateKey != client.DB.UserID {
|
||||
return
|
||||
}
|
||||
if membership, _ := evt.Content["membership"].(string); membership == "invite" {
|
||||
client.JoinRoom(evt.RoomID)
|
||||
}
|
||||
}
|
||||
|
||||
func (client *Client) JoinRoom(roomID string) {
|
||||
client.Client.JoinRoom(roomID, "", nil)
|
||||
}
|
||||
|
||||
func (client *Client) Sync() {
|
||||
go func() {
|
||||
err := client.Client.Sync()
|
||||
if err != nil {
|
||||
log.Errorln("Sync() in client", client.UserID, "errored:", err)
|
||||
}
|
||||
}()
|
||||
}
|
107
matrix/replyutil.go
Normal file
107
matrix/replyutil.go
Normal file
|
@ -0,0 +1,107 @@
|
|||
// maubot - A plugin-based Matrix bot system written in Go.
|
||||
// Copyright (C) 2018 Tulir Asokan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package matrix
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"fmt"
|
||||
"maunium.net/go/gomatrix"
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
var HTMLReplyFallbackRegex = regexp.MustCompile(`^<mx-reply>[\s\S]+?</mx-reply>`)
|
||||
|
||||
func TrimReplyFallbackHTML(html string) string {
|
||||
return HTMLReplyFallbackRegex.ReplaceAllString(html, "")
|
||||
}
|
||||
|
||||
func TrimReplyFallbackText(text string) string {
|
||||
if !strings.HasPrefix(text, "> ") || !strings.Contains(text, "\n") {
|
||||
return text
|
||||
}
|
||||
|
||||
lines := strings.Split(text, "\n")
|
||||
for len(lines) > 0 && strings.HasPrefix(lines[0], "> ") {
|
||||
lines = lines[1:]
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
func RemoveReplyFallback(evt *gomatrix.Event) {
|
||||
if format, ok := evt.Content["format"].(string); ok && format == "org.matrix.custom.html" {
|
||||
htmlBody, _ := evt.Content["formatted_body"].(string)
|
||||
evt.Content["formatted_body"] = TrimReplyFallbackHTML(htmlBody)
|
||||
}
|
||||
plainBody, _ := evt.Content["body"].(string)
|
||||
evt.Content["body"] = TrimReplyFallbackText(plainBody)
|
||||
}
|
||||
|
||||
const ReplyFormat = `<mx-reply><blockquote>
|
||||
<a href="https://matrix.to/#/%s/%s">In reply to</a>
|
||||
<a href="https://matrix.to/#/%s">%s</a>
|
||||
%s
|
||||
</blockquote></mx-reply>
|
||||
`
|
||||
|
||||
func ReplyFallbackHTML(evt *gomatrix.Event) string {
|
||||
body, ok := evt.Content["formatted_body"].(string)
|
||||
if !ok {
|
||||
body, _ = evt.Content["body"].(string)
|
||||
body = html.EscapeString(body)
|
||||
}
|
||||
|
||||
senderDisplayName := evt.Sender
|
||||
|
||||
return fmt.Sprintf(ReplyFormat, evt.RoomID, evt.ID, evt.Sender, senderDisplayName, body)
|
||||
}
|
||||
|
||||
func ReplyFallbackText(evt *gomatrix.Event) string {
|
||||
body, _ := evt.Content["body"].(string)
|
||||
lines := strings.Split(strings.TrimSpace(body), "\n")
|
||||
firstLine, lines := lines[0], lines[1:]
|
||||
|
||||
senderDisplayName := evt.Sender
|
||||
|
||||
var fallbackText strings.Builder
|
||||
fmt.Fprintf(&fallbackText, "> <%s> %s", senderDisplayName, firstLine)
|
||||
for _, line := range lines {
|
||||
fmt.Fprintf(&fallbackText, "\n> %s", line)
|
||||
}
|
||||
fallbackText.WriteString("\n\n")
|
||||
return fallbackText.String()
|
||||
}
|
||||
|
||||
func SetReply(content map[string]interface{}, inReplyTo *gomatrix.Event) map[string]interface{} {
|
||||
content["m.relates_to"] = map[string]interface{}{
|
||||
"m.in_reply_to": map[string]interface{}{
|
||||
"event_id": inReplyTo.ID,
|
||||
"room_id": inReplyTo.RoomID,
|
||||
},
|
||||
}
|
||||
|
||||
body, _ := content["body"].(string)
|
||||
content["body"] = ReplyFallbackText(inReplyTo) + body
|
||||
|
||||
htmlBody, ok := content["formatted_body"].(string)
|
||||
if !ok {
|
||||
htmlBody = html.EscapeString(body)
|
||||
content["format"] = "org.matrix.custom.html"
|
||||
}
|
||||
content["formatted_body"] = ReplyFallbackHTML(inReplyTo) + htmlBody
|
||||
return content
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue