-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.go
126 lines (98 loc) · 3.17 KB
/
slack.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
package main
import (
"fmt"
"io/ioutil"
"net"
"strings"
"github.com/ipinfo/go/v2/ipinfo"
"github.com/kmc-jp/SlackAuthNotifier/slack_webhook"
)
type MessageType int
const (
MessageTypeAccept = iota
MessageTypeFailed
MessageTypeCaution
)
type SlackMessageHandler struct {
ipinfoClient *ipinfo.Client
}
type SlackUserSetting struct {
CustomName string
SlackID string
}
func NewSlackMessageHandler(ipinfoClient *ipinfo.Client) *SlackMessageHandler {
return &SlackMessageHandler{
ipinfoClient: ipinfoClient,
}
}
func (s SlackMessageHandler) NewMessage(addr, username, LastLine string, messageType MessageType) slack_webhook.Message {
var message slack_webhook.Message
var section = slack_webhook.SectionBlock()
if messageType == MessageTypeAccept {
section.Text = slack_webhook.MrkdwnElement(fmt.Sprintf("*%s*", LastLine), false)
} else {
section.Text = slack_webhook.MrkdwnElement(LastLine, false)
}
var blocks = make([]slack_webhook.BlockBase, 0)
blocks = append(blocks, section)
section = slack_webhook.SectionBlock()
b, err := ioutil.ReadFile(fmt.Sprintf("/home/%s/.slack_notifier", username))
if err == nil {
var settings = s.readSettingsFile(string(b))
if settings.CustomName != "" {
username = settings.CustomName
}
var text = fmt.Sprintf("User: %s", username)
if settings.SlackID != "" {
text += fmt.Sprintf(" (<@%s>)", settings.SlackID)
}
section.Text = slack_webhook.MrkdwnElement(text, false)
} else {
section.Text = slack_webhook.MrkdwnElement(fmt.Sprintf("User: %s", username), false)
}
blocks = append(blocks, section)
var content string
// get DN for the address
addrs, err := net.LookupAddr(addr)
if err == nil && len(addrs) > 0 {
section = slack_webhook.SectionBlock()
section.Text = slack_webhook.MrkdwnElement(fmt.Sprintf("addr: %s", strings.Join(addrs, " ")), true)
blocks = append(blocks, section)
content = fmt.Sprintf("*%s*\naddr: %s", LastLine, strings.Join(addrs, " "))
} else {
content = fmt.Sprintf("*%s*", LastLine)
}
var IP = net.ParseIP(addr)
if IP != nil && !IP.IsPrivate() && !IP.IsLoopback() {
core, err := s.ipinfoClient.GetIPInfo(IP)
if err == nil && core != nil {
section = slack_webhook.SectionBlock()
section.Text = slack_webhook.MrkdwnElement(fmt.Sprintf("Country: %s\nCity: %s\nOrg: %s", core.Country, core.City, core.Org), true)
blocks = append(blocks, section)
}
}
message.Blocks = blocks
message.Text = content
return message
}
func (s SlackMessageHandler) readSettingsFile(File string) SlackUserSetting {
var lines = strings.Split(File, "\n")
var settings = SlackUserSetting{}
for _, line := range lines {
sep := strings.Split(line, "=")
var value = strings.TrimSpace(strings.Join(sep[1:], "="))
value = strings.TrimSuffix(value, "\"")
value = strings.TrimSuffix(value, "'")
value = strings.TrimPrefix(value, "\"")
value = strings.TrimPrefix(value, "'")
switch strings.TrimSpace(sep[0]) {
case "CustomName":
settings.CustomName = value
case "SlackID":
settings.SlackID = value
default:
continue
}
}
return settings
}