From 89fb3fa619409cd5c5768383481225ff43eb0fc8 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Sat, 29 May 2021 21:05:12 +0100 Subject: [PATCH] Matrix: Notifications --- email.go | 9 ++++++- matrix.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/email.go b/email.go index 8d4399b..c6f6880 100644 --- a/email.go +++ b/email.go @@ -25,6 +25,8 @@ import ( "github.com/mailgun/mailgun-go/v4" ) +var renderer = html.NewRenderer(html.RendererOptions{Flags: html.Smartypants}) + // implements email sending, right now via smtp or mailgun. type EmailClient interface { Send(fromName, fromAddr string, message *Message, address ...string) error @@ -337,7 +339,6 @@ func (emailer *Emailer) constructConfirmation(code, username, key string, app *a func (emailer *Emailer) constructTemplate(subject, md string, app *appContext) (*Message, error) { email := &Message{Subject: subject} - renderer := html.NewRenderer(html.RendererOptions{Flags: html.Smartypants}) html := markdown.ToHTML([]byte(md), nil, renderer) text := stripMarkdown(md) message := app.config.Section("messages").Key("message").String() @@ -817,6 +818,12 @@ func (app *appContext) sendByID(email *Message, ID ...string) error { return err } } + if mxChat, ok := app.storage.matrix[id]; ok && mxChat.Contact && matrixEnabled { + err = app.matrix.Send(email, mxChat.RoomID) + if err != nil { + return err + } + } if address, ok := app.storage.emails[id]; ok && address.Contact && emailEnabled { err = app.email.send(email, address.Addr) if err != nil { diff --git a/matrix.go b/matrix.go index 1d004b5..3360abc 100644 --- a/matrix.go +++ b/matrix.go @@ -2,7 +2,10 @@ package main import ( "encoding/json" + "fmt" + "strings" + "github.com/gomarkdown/markdown" "github.com/matrix-org/gomatrix" ) @@ -93,7 +96,54 @@ func (d *MatrixDaemon) Shutdown() { close(d.ShutdownChannel) } -func (d *MatrixDaemon) handleMessage(event *gomatrix.Event) { return } +func (d *MatrixDaemon) handleMessage(event *gomatrix.Event) { + if event.Sender == d.userID { + return + } + lang := "en-us" + if l, ok := d.languages[event.RoomID]; ok { + if _, ok := d.app.storage.lang.Telegram[l]; ok { + lang = l + } + } + sects := strings.Split(event.Content["body"].(string), " ") + switch sects[0] { + case "!lang": + if len(sects) == 2 { + d.commandLang(event, sects[1], lang) + } else { + d.commandLang(event, "", lang) + } + } +} + +func (d *MatrixDaemon) commandLang(event *gomatrix.Event, code, lang string) { + if code == "" { + list := "!lang \n" + for c := range d.app.storage.lang.Telegram { + list += fmt.Sprintf("%s: %s\n", c, d.app.storage.lang.Telegram[c].Meta.Name) + } + _, err := d.bot.SendText( + event.RoomID, + list, + ) + if err != nil { + d.app.err.Printf("Matrix: Failed to send message to \"%s\": %v", event.Sender, err) + } + return + } + if _, ok := d.app.storage.lang.Telegram[code]; !ok { + return + } + d.languages[event.RoomID] = code + if u, ok := d.app.storage.matrix[event.RoomID]; ok { + u.Lang = code + d.app.storage.matrix[event.RoomID] = u + if err := d.app.storage.storeMatrixUsers(); err != nil { + d.app.err.Printf("Matrix: Failed to store Matrix users: %v", err) + } + } +} func (d *MatrixDaemon) SendStart(userID string) (ok bool) { room, err := d.bot.CreateRoom(&gomatrix.ReqCreateRoom{ @@ -128,6 +178,25 @@ func (d *MatrixDaemon) SendStart(userID string) (ok bool) { return } +func (d *MatrixDaemon) Send(message *Message, roomID ...string) (err error) { + md := "" + if message.Markdown != "" { + // Convert images to links + md = string(markdown.ToHTML([]byte(strings.ReplaceAll(message.Markdown, "![", "[")), nil, renderer)) + } + for _, id := range roomID { + if md != "" { + _, err = d.bot.SendFormattedText(id, message.Text, md) + } else { + _, err = d.bot.SendText(id, message.Text) + } + if err != nil { + return + } + } + return +} + // User enters ID on sign-up, a PIN is sent to them. They enter it on sign-up. // Message the user first, to avoid E2EE by default