22package main
33
44import (
5+ "bytes"
56 "fmt"
67 "io"
78 "log"
89 "log/syslog"
10+ "net/mail"
911 "os"
10- "strings"
1112
1213 "github.com/pelletier/go-toml/v2"
1314 "github.com/slack-go/slack"
@@ -21,8 +22,6 @@ type Config struct {
2122}
2223
2324func main () {
24- var subject string
25- var body []string
2625 var config Config
2726
2827 // set a default for syslogtag
@@ -44,6 +43,7 @@ func main() {
4443 // case of being called from crond
4544 sl , err := syslog .New (syslog .LOG_WARNING | syslog .LOG_MAIL , config .SyslogTag )
4645 if err != nil {
46+ _ = sl .Err (fmt .Sprintln ("failed to setup syslog connection " , err ))
4747 log .Fatal ("failed to setup syslog connection " , err )
4848 }
4949
@@ -52,39 +52,48 @@ func main() {
5252 // callers. So far only tested with busybox/Alpine crond
5353 stdin , err := io .ReadAll (os .Stdin )
5454 if err != nil {
55- log .Fatal ("failed to read anything from stdin " , err )
55+ // this really shouldn't fail
56+ log .Fatal ("failed to read stdin" , err )
5657 }
57- lines := strings .Split (string (stdin ), "\n " )
58- for n , line := range lines {
59- if strings .HasPrefix (line , "Subject: " ) {
60- subject = strings .SplitAfterN (line , " " , 2 )[1 ]
61- }
62- if line == "" {
63- body = lines [n :]
64- break
65- }
58+ _ = sl .Debug (string (stdin ))
59+ msg , err := mail .ReadMessage (bytes .NewReader (stdin ))
60+ if err != nil {
61+ _ = sl .Err (fmt .Sprintln ("failed to read stdin email format " , err ))
62+ log .Fatal ("failed to read stdin email format" , err )
63+ }
64+ body , err := io .ReadAll (msg .Body )
65+ if err != nil {
66+ _ = sl .Err (fmt .Sprintln ("failed to read email body " , err ))
67+ log .Fatal ("failed to read email body" , err )
6668 }
6769
6870 // setup slack message
71+ attach := new (slack.Attachment )
72+ attach .Text = string (body )
73+ hostname , _ := os .Hostname ()
74+ subjText := slack .NewTextBlockObject ("mrkdwn" , "*Subject:* " + msg .Header .Get ("Subject" ), false , false )
75+ hostText := slack .NewTextBlockObject ("mrkdwn" , "*Hostname:* " + hostname , false , false )
76+ hdrBlock := make ([]* slack.TextBlockObject , 0 )
77+ hdrBlock = append (hdrBlock , subjText )
78+ hdrBlock = append (hdrBlock , hostText )
6979 smsg := slack .MsgOptionBlocks (
7080 slack .NewSectionBlock (
71- slack .NewTextBlockObject ("mrkdwn" , "*Subject:* " + subject , false , false ), nil , nil ,
72- ),
73- slack .NewDividerBlock (),
74- slack .NewSectionBlock (
75- slack .NewTextBlockObject ("mrkdwn" , strings .Join (body , "\n " ), false , false ), nil , nil ,
81+ nil ,
82+ hdrBlock ,
83+ nil ,
7684 ),
77- slack .NewDividerBlock (),
7885 )
7986
8087 msgchan , msgts , err := api .PostMessage (
8188 config .Channel ,
8289 smsg ,
90+ slack .MsgOptionAttachments (* attach ),
8391 )
8492 if err != nil {
93+ _ = sl .Err (fmt .Sprintln ("failed to post message " , err ))
94+ log .Println ("body" , string (body ))
8595 log .Fatal ("failed to post message " , err )
8696 }
8797
88- sl .Debug (fmt .Sprintf ("channel: %s - ts: %s - argv: %v" , msgchan , msgts , os .Args )) //nolint:errcheck
89- sl .Debug (string (stdin )) //nolint:errcheck
98+ _ = sl .Debug (fmt .Sprintf ("channel: %s - ts: %s - argv: %v" , msgchan , msgts , os .Args )) //nolint:errcheck
9099}
0 commit comments