Skip to content

Commit

Permalink
Merge pull request #38 from cseeger-epages/issue/37
Browse files Browse the repository at this point in the history
issue #37 markdown support
  • Loading branch information
cseeger-epages authored Feb 18, 2020
2 parents 5882ad3 + df14703 commit 054e955
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Filter emails from mail accounts and send them to mattermost.

- IMAP(S) support
- Mattermost v4 API support
- HTML 2 Markdown support
- Filter mails by Folder
- Filter mails by From
- Filter mails by To
Expand Down
3 changes: 3 additions & 0 deletions conf/mail2most.conf
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
SubjectOnly = false
# StripHTML will remove all HTML tags bevor sending a msg to mattermost
StripHTML = true
# ConvertToMarkdown will convert html mails to markdown for better readability in mattermost
# if this option is set to true the StripHTML option is ignored
ConvertToMarkdown = true
# HideFrom allows you to hide the From Address in mattermost messages
HideFrom= false
# allow posting mail attachments into mattermost
Expand Down
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ go 1.13
require (
github.com/BurntSushi/toml v0.3.1
github.com/Flaque/filet v0.0.0-20190209224823-fc4d33cfcf93
github.com/PuerkitoBio/goquery v1.5.0
github.com/Skarlso/html-to-markdown v0.0.0-20191210071215-2cf06e949e49
github.com/cseeger-epages/godown v0.0.0-20180312012330-2e9e17e0ea51
github.com/dsnet/compress v0.0.1 // indirect
github.com/emersion/go-imap v1.0.0-rc.2
github.com/emersion/go-message v0.11.0
Expand All @@ -13,8 +16,10 @@ require (
github.com/gopherjs/gopherjs v0.0.0-20191106031601-ce3c9ade29de // indirect
github.com/gorilla/websocket v1.4.1 // indirect
github.com/k3a/html2text v0.0.0-20190714173509-955615037597
github.com/lunny/html2md v0.0.0-20181018071239-7d234de44546
github.com/magefile/mage v1.9.0
github.com/mattermost/mattermost-server v0.0.0-20190626111855-f21a8a370f89
github.com/mattn/godown v0.0.0-20180312012330-2e9e17e0ea51
github.com/mholt/archiver v3.1.1+incompatible
github.com/nwaples/rardecode v1.0.0 // indirect
github.com/onsi/ginkgo v1.10.3 // indirect
Expand All @@ -28,11 +33,10 @@ require (
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/zap v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4 // indirect
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553
golang.org/x/sys v0.0.0-20191210023423-ac6580df4449 // indirect
golang.org/x/text v0.3.2
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.2.7 // indirect
)

replace willnorris.com/go/imageproxy v0.8.1-0.20190326225038-d4246a08fdec => willnorris.com/go/imageproxy v0.8.1-0.20190422234945-d4246a08fdec
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ github.com/Masterminds/squirrel v1.1.0 h1:baP1qLdoQCeTw3ifCdOq2dkYc6vGcmRdaociKL
github.com/Masterminds/squirrel v1.1.0/go.mod h1:yaPeOnPG5ZRwL9oKdTsO/prlkPbXWZlRVMQ/gGlzIuA=
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
github.com/PaulARoy/azurestoragecache v0.0.0-20170906084534-3c249a3ba788/go.mod h1:lY1dZd8HBzJ10eqKERHn3CU59tfhzcAVb2c0ZhIWSOk=
github.com/PuerkitoBio/goquery v1.5.0 h1:uGvmFXOA73IKluu/F84Xd1tt/z07GYm8X49XKHP7EJk=
github.com/PuerkitoBio/goquery v1.5.0/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/Skarlso/html-to-markdown v0.0.0-20191210071215-2cf06e949e49 h1:4EYFpouKjEzdfqWR8A7BxLMysFSr0zG33cq0XDnhbEg=
github.com/Skarlso/html-to-markdown v0.0.0-20191210071215-2cf06e949e49/go.mod h1:rUfIbm4HtabJH+jLcBJWbHDAowSqeMnLekC6jj/ERo0=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o=
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
Expand All @@ -39,6 +45,8 @@ github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8Nz
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd v0.0.0-20181012123002-c6f51f82210d/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/corpix/uarand v0.0.0/go.mod h1:JSm890tOkDN+M1jqN8pUGDKnzJrsVbJwSMHBY4zwz7M=
github.com/cseeger-epages/godown v0.0.0-20180312012330-2e9e17e0ea51 h1:wRkDqYlQratKLUdtdpAeyeRolvJ5y/L/7zeQ3Be8vOw=
github.com/cseeger-epages/godown v0.0.0-20180312012330-2e9e17e0ea51/go.mod h1:YH5dvqBkWk+/05C+IsYsAIsQVkiSRqzgaYp2aogXjIc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -188,6 +196,8 @@ github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhR
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lunny/html2md v0.0.0-20181018071239-7d234de44546 h1:hqxaQP14eTbeZGHZhsDInzj9sJAnEufjVQL4bEA/p+8=
github.com/lunny/html2md v0.0.0-20181018071239-7d234de44546/go.mod h1:lUUaVYlpAQ1Oo6vIZfec6CXQZjOvFZLyqaR8Dl7m+hk=
github.com/magefile/mage v1.9.0 h1:t3AU2wNwehMCW97vuqQLtw6puppWXHO+O2MHo5a50XE=
github.com/magefile/mage v1.9.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
Expand All @@ -208,10 +218,13 @@ github.com/mattermost/rsc v0.0.0-20160330161541-bbaefb05eaa0/go.mod h1:nV5bfVpT/
github.com/mattermost/viper v1.0.4/go.mod h1:uc5hKG9lv4/KRwPOt2c1omOyirS/UnuA2TytiZQSFHM=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/godown v0.0.0-20180312012330-2e9e17e0ea51 h1:MpI7hy3MiCnrggmZI/s8LaPbLVOOWpzDbjA4F+XaXaM=
github.com/mattn/godown v0.0.0-20180312012330-2e9e17e0ea51/go.mod h1:s3KUdOIXJ+jaGM++XHiXA6gikdleaWVATCcQGD4h734=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mholt/archiver v3.1.1+incompatible h1:1dCVxuqs0dJseYEhi5pl7MYPH9zDa1wBi7mF09cbNkU=
github.com/mholt/archiver v3.1.1+incompatible/go.mod h1:Dh2dOXnSdiLxRiPoVfIr/fI1TwETms9B8CTWfeh7ROU=
Expand Down Expand Up @@ -360,6 +373,7 @@ golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTk
golang.org/x/lint v0.0.0-20181217174547-8f45f776aaf1/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
1 change: 1 addition & 0 deletions lib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type mattermost struct {
Broadcast []string
SubjectOnly bool
StripHTML bool
ConvertToMarkdown bool
HideFrom bool
MailAttachments bool
}
Expand Down
29 changes: 23 additions & 6 deletions lib/mattermost.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package mail2most

import (
"bytes"
"encoding/base64"
"fmt"
"strings"

"github.com/cseeger-epages/godown"
"github.com/k3a/html2text"
"github.com/mattermost/mattermost-server/model"
)
Expand Down Expand Up @@ -50,7 +52,14 @@ func (m Mail2Most) PostMattermost(profile int, mail Mail) error {
body = string(bb)
}

if m.Config.Profiles[profile].Mattermost.StripHTML {
if m.Config.Profiles[profile].Mattermost.ConvertToMarkdown {
var b bytes.Buffer
err := godown.Convert(&b, strings.NewReader(body), nil)
if err != nil {
return err
}
body = b.String()
} else if m.Config.Profiles[profile].Mattermost.StripHTML {
body = html2text.HTML2Text(body)
mail.Subject = html2text.HTML2Text(mail.Subject)
mail.From[0].PersonalName = html2text.HTML2Text(mail.From[0].PersonalName)
Expand Down Expand Up @@ -85,11 +94,19 @@ func (m Mail2Most) PostMattermost(profile int, mail Mail) error {
mail.Subject,
)
} else {
msg += fmt.Sprintf(
"\n>_%s_\n\n```\n%s```\n",
mail.Subject,
body,
)
if m.Config.Profiles[profile].Mattermost.ConvertToMarkdown {
msg += fmt.Sprintf(
"\n>_%s_\n\n\n%s\n",
mail.Subject,
body,
)
} else {
msg += fmt.Sprintf(
"\n>_%s_\n\n```\n%s```\n",
mail.Subject,
body,
)
}
}

for _, b := range m.Config.Profiles[profile].Mattermost.Broadcast {
Expand Down

0 comments on commit 054e955

Please sign in to comment.