-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan2mail.go
73 lines (62 loc) · 1.62 KB
/
scan2mail.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
package scan2mail
import (
"bufio"
"bytes"
"log"
"os"
"path/filepath"
"strings"
"github.com/nlopes/slack"
)
func Run() {
// procmailはメール本文をstdinに投げる
reader := bufio.NewReader(os.Stdin)
// 設定ファイル読み込み。同一ディレクトリに存在すると仮定(まずそう)
exe, err := os.Executable()
if err != nil {
log.Fatal(err)
}
exeDir := filepath.Dir(exe)
configPath := filepath.Join(exeDir, "config.toml")
config, err := LoadToml(configPath, "ap-northeast-1")
if err != nil {
log.Fatal(err)
}
mail, err := NewMail(reader)
if err != nil {
log.Fatal(err)
}
address := mail.GetAddress()
log.Print("address: " + address)
// 送信先ドメインがfolio-sec.comじゃなければ無視
var user string
for _, domain := range config.Domain {
if strings.LastIndex(address[strings.Index(address, "@"):], domain) != -1 {
user = address[:strings.Index(address, "@")]
break
}
}
if len(user) < 1 {
log.Fatal("address is not valid dommain: " + address)
}
api := slack.New(config.BotToken)
// email から slackの user_idを得る
userResponse, err := api.GetUserByEmail(address)
if err != nil {
log.Fatal("user not found" + user + "\n" + err.Error())
}
userID := userResponse.ID
// 添付ファイルをslackへ投げる
attachments := *mail.GetAttachments()
for _, attachment := range attachments {
file, err := api.UploadFile(slack.FileUploadParameters{
Filename: attachment.Filename,
Channels: []string{ userID },
Reader: bytes.NewReader(*attachment.Content),
})
if err != nil {
log.Fatal(err)
}
log.Print("[DEBUG] ", file)
}
}