-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
127 lines (106 loc) · 2.72 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"github.com/BrianLeishman/go-imap"
"github.com/bwmarrin/lit"
"github.com/hhhapz/noncer/announcements"
)
func main() {
if err := run(context.Background()); err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err)
os.Exit(1)
}
}
var (
verbose = flag.Bool("v", false, "verbose imap output")
user = flag.String("user", "", "imap username")
pass = flag.String("pass", "", "imap password")
host = flag.String("host", "imap.transip.email", "imap host")
port = flag.Int("port", 993, "imap port")
period = flag.Int("period", 60, "email fetch period")
webhook = flag.String("webhook", "", "webhook url")
allowList allowListFlags
)
type allowListFlags []string
func (i *allowListFlags) String() string {
return strings.Join(*i, ",")
}
func (i *allowListFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
func run(ctx context.Context) error {
flag.Var(&allowList, "allowlist", "allow list for source email addresses")
flag.Parse()
announcements.AllowedDomains = allowList
if *webhook == "" {
return fmt.Errorf("webhook url must be provided")
}
// Always Log info level entries
lit.LogLevel = 4
imap.Verbose = *verbose
imap.RetryCount = 3
im, err := imap.New(*user, *pass, *host, *port)
if err != nil {
return fmt.Errorf("could not connect to imap: %w", err)
}
ctx, cancel := context.WithCancel(context.Background())
anns := make(chan announcements.Announcement)
// push announcements to webhook url
go func() {
for a := range anns {
if err := sendWebhook(ctx, a); err != nil {
lit.Error("sending webhook: %v", err)
}
}
}()
// listen for announcements
go func() {
announcements.Listen(ctx, im, anns, *period)
}()
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
cancel()
return nil
}
func sendWebhook(ctx context.Context, a announcements.Announcement) error {
buf := new(bytes.Buffer)
for i := range a.Contents {
buf.Reset()
msg := Webhook{a.Contents[i]}
if i == 0 {
msg = Webhook{fmt.Sprintf("**%s**\n\n%s", a.Subject, a.Contents[i])}
}
json.NewEncoder(buf).Encode(msg)
req, err := http.NewRequestWithContext(ctx, "POST", *webhook, buf)
req.Header.Set("Content-Type", "application/json")
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 204 {
buf.Reset()
io.Copy(buf, resp.Body)
return fmt.Errorf("unexpected status code %d:\n%v", resp.StatusCode, buf.String())
}
}
return nil
}
type Webhook struct {
Content string `json:"content"`
}