/
main.go
326 lines (269 loc) · 7.88 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package main
import (
"bufio"
"fmt"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"strconv"
"strings"
"time"
"golang.org/x/crypto/ssh/terminal"
"github.com/howeyc/gopass"
"github.com/PuerkitoBio/goquery"
"github.com/matryer/try"
)
const retryInterval = 60
const retryLimit = 6
type person struct {
Name string
ID string
Joined time.Time
}
func getCredentials() (string, string) {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter Username: ")
username, _ := reader.ReadString('\n')
fmt.Print("Enter Password: ")
password := gopass.GetPasswd()
fmt.Println("")
return strings.TrimSpace(username), strings.TrimSpace(string(password))
}
func promptPeople(limit int) []interface{} {
fmt.Println("Enter one or more users to delete, if you know the ID of a user that doesn't appear on the above list you can enter it by prefixing a hash(#)")
fmt.Print("Select users [eg: 1,3,5..12,#12345672]: ")
reader := bufio.NewReader(os.Stdin)
stringList, _ := reader.ReadString('\n')
list := make([]interface{}, 0)
for _, entry := range strings.Split(stringList, ",") {
if strings.HasPrefix(entry, "#") {
list = append(list, strings.TrimSpace(strings.TrimPrefix(entry, "#")))
} else if strings.Contains(entry, "..") {
indexRange := strings.Split(entry, "..")
if len(indexRange) != 2 {
log.Fatal("Wrong number of arguments in range")
}
min, err := strconv.Atoi(strings.TrimSpace(indexRange[0]))
if err != nil {
log.Fatal(err)
}
max, err := strconv.Atoi(strings.TrimSpace(indexRange[1]))
for i := min; i <= max; i++ {
if i > 0 && i <= limit {
list = append(list, i-1)
}
}
} else {
val, err := strconv.Atoi(strings.TrimSpace(entry))
if err != nil {
log.Fatal(err)
}
if val > 0 && val <= limit {
list = append(list, val-1)
}
}
}
return list
}
func extractProfileMetadata(gq *goquery.Document) (string, time.Time, error) {
uid, ok := gq.Find("meta[name=uid]").Attr("content")
if !ok {
log.Println("Can't find uid, there might be something wrong")
}
var err error
var created time.Time
screated, ok := gq.Find("meta[name=ucreated]").Attr("content")
if !ok {
return "", nullTime, fmt.Errorf("Error reading account creation date")
} else {
created, err = time.Parse("2006-01-02 15:04:05", screated)
if err != nil {
return "", nullTime, err
}
}
return uid, created, nil
}
func main() {
user, pass := getCredentials()
cookieJar, _ := cookiejar.New(nil)
client := &http.Client{Jar: cookieJar}
log.Println("Grabbing XSRF Token")
resp, err := client.Get("https://www.hipchat.com/sign_in")
if err != nil {
log.Fatal(err)
}
gq := mustParseResponse(resp)
xsrf, exists := gq.Find("input[name=xsrf_token]").Attr("value")
if !exists {
log.Fatal("Can't find xsrf_token")
}
log.Println("Logging in")
resp, err = client.PostForm("https://www.hipchat.com/sign_in", url.Values{
"xsrf_token": {xsrf},
"email": {user},
"password": {pass},
"stay_signed_in": {"1"},
"signin": {"log in"},
})
if err != nil {
log.Fatal(err)
}
gq = mustParseResponse(resp)
if gq.Find("div.aui-page-header-main h1").Size() != 1 {
log.Fatal("Couldn't find welcome header")
}
uid, created, err := extractProfileMetadata(gq)
if err != nil {
log.Fatal(err)
}
log.Printf("Found user %s created %s", uid, created)
domain := resp.Request.URL.Host
log.Println("Listing people")
// Get first page
resp, err = client.Get("https://" + domain + "/people")
if err != nil {
log.Fatal(err)
}
gq = mustParseResponse(resp)
people := make([]person, 0)
// Figure out how many pages there are (the next/prev are links in aui-nav, but page 1 is not a link so just subtract one)
pages := gq.Find("ol.aui-nav a").Size() - 1
longestName := 0
for page := 1; page <= pages; page++ {
gq.Find("a.name").Each(func(i int, s *goquery.Selection) {
attr, exists := s.Attr("href")
name := strings.TrimSpace(s.Text())
signup := parseSignupDate(strings.TrimSpace(s.Parent().Parent().SiblingsFiltered("[headers=date-joined]").Text()))
if !exists {
log.Println("Can't find the href for this item")
return
}
longestName = maxInt(longestName, len(name))
people = append(people, person{
Name: name,
ID: strings.TrimPrefix(attr, "/people/show/"),
Joined: signup,
})
})
if page < pages {
log.Printf("Getting people page %d", page+1)
resp, err = client.Get("https://" + domain + "/people?p=" + strconv.Itoa(page+1))
if err != nil {
log.Fatal(err)
}
gq = mustParseResponse(resp)
}
}
// Doesn't seem to on windows
width, _, _ := terminal.GetSize(0)
peopleDigits := len(strconv.Itoa(len(people)))
columns := width / (longestName + peopleDigits + 6)
if columns < 1 {
columns = 1
}
peopleDigitsString := strconv.Itoa(peopleDigits)
for i, person := range people {
index := i + 1 // offset by 1 for input
padding := strings.Repeat(".", longestName-len(person.Name))
fmt.Printf("%"+peopleDigitsString+"d:%s%s ", index, padding, person.Name)
if index%columns == 0 {
fmt.Println("")
}
}
// extra newline for when the columns don't line up
if len(people)%columns != 0 {
fmt.Println("")
}
historiesToDelete := promptPeople(len(people))
for _, index := range historiesToDelete {
var currentPerson person
switch t := index.(type) {
case int:
currentPerson = people[t]
case string:
log.Printf("Resolving %s to a user", t)
err = func() error {
resp, err = client.Get("https://" + domain + "/people/show/" + t)
if err != nil {
return fmt.Errorf("Can't lookup %s: %s", t, err)
}
gq, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
return fmt.Errorf("Can't parse %s: %s", t, err)
}
name := gq.Find("div.aui-item h2").Text()
if name == "" {
return fmt.Errorf("Can't find name for %s", t)
}
_, joined, err := extractProfileMetadata(gq)
if err != nil {
return err
}
currentPerson = person{
Name: name,
Joined: joined,
ID: t,
}
return nil
}()
if err != nil {
log.Printf("ERR: %s", err)
continue
}
}
endDate := created.Add(-1 * 24 * time.Hour)
if endDate.Before(currentPerson.Joined) {
endDate = currentPerson.Joined
}
for working := time.Now(); working.After(endDate); working = working.Add(-1 * 24 * time.Hour) {
log.Printf("Checking %s @ %s", currentPerson.Name, working.Format("2006-01-02"))
err := try.Do(func(attempt int) (bool, error) {
if attempt > 1 {
time.Sleep(retryInterval * time.Duration(attempt-1) * time.Second)
}
resp, err = client.Get("https://" + domain + "/history/member/" + currentPerson.ID + working.Format("/2006/01/02"))
if err != nil {
return attempt < retryLimit, err
}
doc, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
return attempt < retryLimit, err
}
forms := doc.Find("div.delete form")
if forms.Size() > 0 {
log.Printf("Found %d entries, deleting...", forms.Size())
}
forms.Each(func(i int, form *goquery.Selection) {
delurl, exists := form.Attr("action")
if !exists {
log.Fatal("Can't find action for the delete form")
}
values := url.Values{}
form.Find("input").Each(func(i int, input *goquery.Selection) {
name, nameExists := input.Attr("name")
value, valueExists := input.Attr("value")
if nameExists && valueExists {
values.Set(name, value)
}
})
err := try.Do(func(attempt int) (bool, error) {
if attempt > 1 {
time.Sleep(retryInterval * time.Duration(attempt-1) * time.Second)
}
_, err := client.PostForm(delurl, values)
return attempt < retryLimit, err
})
if err != nil {
log.Fatalln("Tried", retryLimit, "times to delete post:", err)
}
})
return attempt < retryLimit, err
})
if err != nil {
log.Fatalln("Tried", retryLimit, "times to pull day:", err)
}
}
}
}