forked from thatoddmailbox/slack-status-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
management.go
180 lines (154 loc) · 5.48 KB
/
management.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
178
179
180
package main
import (
"fmt"
"log"
"html"
"net"
"net/http"
"strconv"
"github.com/hydrogen18/stoppableListener"
)
var Management_Socket *stoppableListener.StoppableListener
func Management_Handler_StopManagement(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if !Bot_CanActivate() {
fmt.Fprintf(w, "You haven't signed in to every service yet; the bot cannot start.")
fmt.Fprintf(w, "<br /> <a href='/'>Go back</a>")
} else {
fmt.Fprintf(w, "Management UI stopped.")
Management_Socket.Stop()
Management_Socket.TCPListener.Close()
}
}
/*
* Schedule
*/
func Management_Handler_Schedule_SignIn(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
username := r.FormValue("username")
password := r.FormValue("password")
if username == "" || password == "" {
fmt.Fprintf(w, "Username and password are required.<br /><a href='/schedule_signin'>Go back</a>")
return
}
message := API_Schedules_SignIn(username, password)
if message != "" {
fmt.Fprintf(w, message)
fmt.Fprintf(w, "<br /><a href='/schedule_signin'>Go back</a>")
return
}
err := API_Schedules_FetchAndSave()
if err != nil {
log.Println(err)
fmt.Fprintf(w, "There was an error fetching your schedule.<br /><a href='/schedule_signin'>Go back</a>")
return
}
fmt.Fprintf(w, "Your schedule has been downloaded.<br /><a href='/'>Go back</a>")
}
func Management_Handler_Schedule_SignInPage(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "<form action='/schedule_signin_h' method='POST'>")
fmt.Fprintf(w, "<input type='text' name='username' placeholder='Username' /><br />")
fmt.Fprintf(w, "<input type='password' name='password' placeholder='Password' /><br />")
fmt.Fprintf(w, "<input type='submit' />")
fmt.Fprintf(w, "</form>")
}
/*
* MyHomeworkSpace
*/
func Management_Handler_MHS_Callback(w http.ResponseWriter, r *http.Request) {
token := r.URL.Query()["token"][0]
if token == "" {
fmt.Fprintf(w, "No token parameter.")
return
}
Storage_Set("mhs-auth", token)
API_MHS_Init()
http.Redirect(w, r, "/", http.StatusFound)
}
func Management_Handler_MHS_SignOut(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if API_MHS_Connected {
API_MHS_SignOut()
fmt.Fprintf(w, "Signed out successfully. <br /> <a href='/'>Go back</a>")
} else {
fmt.Fprintf(w, "You aren't signed in to MyHomeworkSpace!")
}
}
func Management_Handler_MHS_SignIn(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, API_MHS_GetRedirectPath(), http.StatusFound)
}
/*
* Slack
*/
func Management_Handler_Slack_Callback(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query()["code"][0]
if code == "" {
fmt.Fprintf(w, "No code parameter.")
return
}
token, err := API_Slack_GetTokenFromCode(code)
if err != nil {
log.Println(err)
fmt.Fprintf(w, "Error getting Slack access token.")
return
}
Storage_Set("slack-auth", token)
API_Slack_Init()
http.Redirect(w, r, "/", http.StatusFound)
}
func Management_Handler_Slack_SignOut(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if API_Slack_Connected {
API_Slack_SignOut()
fmt.Fprintf(w, "Signed out successfully. <br /> <a href='/'>Go back</a>")
} else {
fmt.Fprintf(w, "You aren't signed in to Slack!")
}
}
func Management_Handler_Slack_SignIn(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, API_Slack_GetRedirectPath(), http.StatusFound)
}
func Management_Handler_Index(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "<h2>slack-status-bot</h2>")
fmt.Fprintf(w, "Schedule: <a href='/schedule_signin'>Fetch schedule</a><br />")
fmt.Fprintf(w, "MyHomeworkSpace: ")
if API_MHS_Connected {
fmt.Fprintf(w, "Signed in as user <strong>%s</strong> <a href='/mhs_signout'>Sign out</a>", html.EscapeString(API_MHS_Me["name"].(string)))
} else {
fmt.Fprintf(w, "Not signed in! <a href='/mhs_signin'>Sign in</a>")
}
fmt.Fprintf(w, "<br />")
fmt.Fprintf(w, "Slack: ")
if API_Slack_Connected {
fmt.Fprintf(w, "Signed in as user <strong>%s</strong> <a href='/slack_signout'>Sign out</a>", html.EscapeString(API_Slack_Me["user"].(string)))
} else {
fmt.Fprintf(w, "Not signed in! <a href='/slack_signin'>Sign in</a>")
}
fmt.Fprintf(w, "<br />")
fmt.Fprintf(w, "<br /><a href='/stop_management'>Stop management and start bot</a>")
}
func Management_Init(port int) {
http.HandleFunc("/stop_management", Management_Handler_StopManagement)
http.HandleFunc("/schedule_signin_h", Management_Handler_Schedule_SignIn)
http.HandleFunc("/schedule_signin", Management_Handler_Schedule_SignInPage)
http.HandleFunc("/mhs_cb", Management_Handler_MHS_Callback)
http.HandleFunc("/mhs_signout", Management_Handler_MHS_SignOut)
http.HandleFunc("/mhs_signin", Management_Handler_MHS_SignIn)
http.HandleFunc("/slack_cb", Management_Handler_Slack_Callback)
http.HandleFunc("/slack_signout", Management_Handler_Slack_SignOut)
http.HandleFunc("/slack_signin", Management_Handler_Slack_SignIn)
http.HandleFunc("/", Management_Handler_Index)
log.Println("Management UI listening on port", port)
socket, err := net.Listen("tcp", ":" + strconv.Itoa(port))
if err != nil {
panic(err)
}
Management_Socket, err = stoppableListener.New(socket)
if err != nil {
panic(err)
}
http.Serve(Management_Socket, nil)
Management_Socket.TCPListener.Close()
}