forked from angelajt/sibyl-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (88 loc) · 2.13 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"time"
"github.com/sacOO7/gowebsocket"
"github.com/synacor/sibyl/server"
)
func main() {
topic := flag.String("t", "", "new room topic sentence")
room := flag.String("r", "", "room name")
deck := flag.String("d", "", "deck name")
flag.Parse()
if len(*room) == 0 {
log.Fatal("no room given")
}
hostport := os.Getenv("SIBYL_HOST")
if len(hostport) == 0 {
log.Fatal("no host given. set SIBYL_HOST to host:port")
}
geturl := fmt.Sprintf("http://%s/r/%s", hostport, *room)
resp, err := http.Get(geturl)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
// extract token from html
re := regexp.MustCompile("Token:\\s+\"(.+?)\"")
token := re.FindStringSubmatch(string(body))[1]
if len(*topic) != 0 {
setTopic(hostport, *room, token, *topic)
}
if len(*deck) != 0 {
setDeck(hostport, *room, token, *deck)
}
}
func setDeck(hostport, room, token, deck string) {
req := server.WsRequest{
Room: room,
Action: server.WsRequestActionDeck,
Deck: deck,
Token: token,
}
set(req, hostport)
}
func setTopic(hostport, room, token, topic string) {
req := server.WsRequest{
Room: room,
Action: server.WsRequestActionTopic,
Value: topic,
Token: token,
}
set(req, hostport)
}
func set(req server.WsRequest, hostport string) {
buf, err := json.Marshal(req)
if err != nil {
log.Fatal(err)
}
txt := string(buf)
wsurl := fmt.Sprintf("ws://%s/ws", hostport)
username := "sibylcli"
url := fmt.Sprintf("%s?room=%s&token=%s&username=%s", wsurl, req.Room, req.Token, username)
socket := gowebsocket.New(url)
socket.Connect()
defer socket.Close()
socket.SendText(txt)
// prevent connection from closing before server processes message
// XXX instead of sleeping, we should wait for the server to send a message before we return
time.Sleep(3 * time.Second)
}
/* in case we ever want to create rooms
data := url.Values{
"room": {room},
"deck": {"hewwo"},
}
resp, err := http.PostForm("http://%s/create", hostport, data)
if err != nil {
log.Fatal(err)
}
*/