-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
177 lines (137 loc) · 3.78 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"fmt"
"log"
"net/http"
"github.com/julienschmidt/httprouter"
"gopkg.in/jmcvetta/napping.v1"
)
type oauthAccess struct {
AccessToken string `json:"access_token"`
Scope string `json:"scope"`
Ok bool `json:"ok"`
}
type authTest struct {
UserID string `json:"user_id"`
}
type member struct {
ID string `json:"id"`
Name string `json:"name"`
RealName string `json:"real_name"`
Deleted bool
IsBot bool `json:"is_bot"`
}
type userList struct {
Ok bool `json:"ok"`
Members []member `json:"members"`
}
func main() {
loadConfig()
openSQLConnection()
router := httprouter.New()
router.GET("/", index)
router.POST("/slash", slashCommand)
router.GET("/oauth", getOauth)
log.Print("Listening on port ", config.Port, "...")
log.Fatal(http.ListenAndServe(fmt.Sprint(":", config.Port), router))
}
func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
r.Header.Set("Content-Type", "text/html")
fmt.Fprint(w, "Nothing to see here...")
}
func listMembers(w http.ResponseWriter, r *http.Request) {
userID := r.PostFormValue("user_id")
text := r.PostFormValue("text")
if userID == "" {
fmt.Fprint(w, "No user provided")
return
}
token := dbGetUserToken(userID)
missingToken := len(token) == 0
if missingToken {
fmt.Fprint(w, "You are not registered. Type /flushme register")
return
}
params := &napping.Params{"token": token}
test := userList{}
var err interface{}
napping.Get("https://slack.com/api/users.list", params, &test, err)
if err != nil {
fmt.Fprint(w, "Could not access user list using access token")
return
}
for _, member := range test.Members {
if member.Deleted && text != "list all" {
continue
}
if member.IsBot && text != "list all" {
continue
}
lastNiom := dbGetLastNiom(member.ID)
if lastNiom != "" {
fmt.Fprint(w, member.Name+": "+lastNiom+"; ")
} else {
fmt.Fprint(w, member.Name+": never :-1:; ")
}
}
}
func slashCommand(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
userID := r.PostFormValue("user_id")
text := r.PostFormValue("text")
userName := r.PostFormValue("user_name")
if userID == "" {
fmt.Fprint(w, "No user provided")
return
}
token := dbGetUserToken(userID)
missingToken := len(token) == 0
if text == "register" {
if missingToken {
fmt.Fprint(w, "Click on this link: <https://slack.com/oauth/authorize?client_id=", config.ClientID, "&scope=identify,read,post&state=1>")
return
}
fmt.Fprint(w, "You are already registered")
return
}
if missingToken {
fmt.Fprint(w, "You are not registered. Type /flushme register")
return
}
if text == "list" || text == "list all" {
listMembers(w, r)
return
}
if text == "all" {
fmt.Fprint(w, "Flushing public files...")
go eatPublicFiles(token)
return
}
dbSaveNiom(userID)
fmt.Fprint(w, "Monster is starting to eat your files...")
go eatUserFiles(userID, token, userName)
}
func getOauth(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
error := r.URL.Query().Get("error")
code := r.URL.Query().Get("code")
if len(error) > 0 {
fmt.Fprint(w, "Please accept authorisation request.")
return
}
params := &napping.Params{"client_id": config.ClientID, "client_secret": config.ClientSecret, "code": code}
access := oauthAccess{}
var err interface{}
napping.Get("https://slack.com/api/oauth.access", params, &access, err)
if err != nil || !access.Ok {
fmt.Fprint(w, "Invalid access token")
return
}
params = &napping.Params{"token": access.AccessToken}
test := authTest{}
napping.Get("https://slack.com/api/auth.test", params, &test, err)
if err != nil {
fmt.Fprint(w, "Could not access user using access token")
return
}
dbSaveUserToken(test.UserID, access.AccessToken)
fmt.Fprint(w, "You may now close this window.")
}