-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwhitelist.go
More file actions
55 lines (49 loc) · 1.27 KB
/
whitelist.go
File metadata and controls
55 lines (49 loc) · 1.27 KB
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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
)
type WhitelistCommand struct {
Output string `short:"o" long:"output" default:"" description:"Name of file where whitelist will be saved."`
}
var whitelistCommand WhitelistCommand
func (x *WhitelistCommand) Execute(args []string) error {
session := MustNewSession(common.User, common.Device, common.ReportingServer)
count, err := session.CreateWhitelist(x.Output)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Recorded %v IDs to %v.\n", count, x.Output)
return nil
}
func init() {
if _, err := parser.AddCommand("whitelist",
"Create a whitelist of current contacts.",
"Creates a file with the IDs of your current contact list, usually to be used as a whitelist for later purging. If this file exists already, it will be overwritten.",
&whitelistCommand); err != nil {
log.Fatal(err)
}
}
func (s *Session) CreateWhitelist(filename string) (int, error) {
mids, err := s.client.GetAllContactIds()
if err != nil {
return 0, err
}
f, err := os.Create(filename)
defer f.Close()
if err != nil {
return 0, err
}
enc := json.NewEncoder(f)
IDs := make(map[string]string, len(mids))
for _, mid := range mids {
IDs[mid] = ""
}
err = enc.Encode(&IDs)
if err != nil {
return 0, err
}
return len(mids), nil
}