all repos — honk @ b602d33e4b4c03dc9a1a172ec4a850152d4bc11b

my fork of honk

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