toys/sprayandpray.go (view raw)
1package main
2
3import (
4 "flag"
5 "fmt"
6 "io/ioutil"
7 "log"
8 "net/http"
9 "net/url"
10 "os"
11 "strings"
12)
13
14
15func sendmsg(server, token, msg, rcpt string) {
16 form := make(url.Values)
17 form.Add("token", token)
18 form.Add("action", "sendactivity")
19 form.Add("msg", msg)
20 form.Add("rcpt", rcpt)
21 apiurl := fmt.Sprintf("https://%s/api", server)
22 req, err := http.NewRequest("POST", apiurl, strings.NewReader(form.Encode()))
23 if err != nil {
24 log.Fatal(err)
25 }
26 req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
27 resp, err := http.DefaultClient.Do(req)
28 if err != nil {
29 log.Fatal(err)
30 }
31 defer resp.Body.Close()
32 answer, err := ioutil.ReadAll(resp.Body)
33 if err != nil {
34 log.Fatal(err)
35 }
36 if resp.StatusCode != 200 {
37 log.Fatalf("status: %d: %s", resp.StatusCode, answer)
38 }
39}
40
41func main() {
42 var server, token, msgfile, rcpt string
43 flag.StringVar(&server, "server", server, "server to connnect")
44 flag.StringVar(&token, "token", token, "auth token to use")
45 flag.StringVar(&msgfile, "msgfile", token, "file with message to send")
46 flag.StringVar(&rcpt, "rcpt", rcpt, "rcpt to send it to")
47 flag.Parse()
48
49 if server == "" || token == "" || msgfile == "" || rcpt == "" {
50 flag.Usage()
51 os.Exit(1)
52 }
53 msg, err := ioutil.ReadFile(msgfile)
54 if err != nil {
55 log.Fatal(err)
56 }
57
58 sendmsg(server, token, string(msg), rcpt)
59}