toys/wonkawonk.go (view raw)
1package main
2
3import (
4 "crypto/rand"
5 "flag"
6 "fmt"
7 "io/ioutil"
8 "log"
9 "math/big"
10 "net/http"
11 "net/url"
12 "os"
13 "strings"
14)
15
16var debugMode = false
17
18func honkahonk(server, token, wonk, wonkles string) {
19 form := make(url.Values)
20 form.Add("token", token)
21 form.Add("action", "honk")
22 form.Add("noise", wonk)
23 form.Add("wonkles", wonkles)
24 apiurl := fmt.Sprintf("https://%s/api", server)
25 req, err := http.NewRequest("POST", apiurl, strings.NewReader(form.Encode()))
26 if err != nil {
27 log.Fatal(err)
28 }
29 req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
30 client := http.DefaultClient
31 if debugMode {
32 client = debugClient
33 }
34
35 resp, err := client.Do(req)
36 if err != nil {
37 log.Fatal(err)
38 }
39 defer resp.Body.Close()
40 answer, err := ioutil.ReadAll(resp.Body)
41 if err != nil {
42 log.Fatal(err)
43 }
44 if resp.StatusCode != 200 {
45 log.Fatalf("status: %d: %s", resp.StatusCode, answer)
46 }
47}
48
49func main() {
50 server := ""
51 token := ""
52 wonkles := ""
53 flag.StringVar(&server, "server", server, "server to connnect")
54 flag.StringVar(&token, "token", token, "auth token to use")
55 flag.StringVar(&wonkles, "wonkles", wonkles, "wordlist to use")
56 flag.BoolVar(&debugMode, "debug", debugMode, "debug mode")
57 flag.Parse()
58
59 if server == "" || token == "" || wonkles == "" {
60 flag.Usage()
61 os.Exit(1)
62 }
63
64 wordlist, err := fetchsome(wonkles)
65 if err != nil {
66 log.Printf("error fetching wonkles: %s", err)
67 }
68 var words []string
69 for _, w := range strings.Split(string(wordlist), "\n") {
70 words = append(words, w)
71 }
72 max := big.NewInt(int64(len(words)))
73 i, _ := rand.Int(rand.Reader, max)
74 wonk := words[i.Int64()]
75
76 log.Printf("picking: %s", wonk)
77
78 honkahonk(server, token, wonk, wonkles)
79}