/
main.go
347 lines (275 loc) · 8.36 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/Pallinder/go-randomdata"
"github.com/googollee/go-socket.io"
"github.com/patrickmn/go-cache"
"github.com/streamrail/concurrent-map"
)
// Configuration is read from config.json file
type Configuration struct {
PlivoAuthToken string
PlivoAuthID string
PhoneNumbers []string
RandomHashValue string
URL string
Port string
}
// MessageType is any type of message sent by the presenter
type MessageType struct {
Type string `json:"type"`
Data interface{} `json:"data"`
}
// Default params, multi-part texts and photo texts might have other fields...
type plivoSms struct {
To string `json:"to"`
From string `json:"from"`
// TotalRate string
// Units string
Text string `json:"message"`
// TotalAmount string
// type string
MessageUUID string `json:"id"`
SentOn time.Time
}
// Global config object
var configuration Configuration
// Keeping a dictionary of all registered hashtags->socket.Id() and vis-versa
var hashmap cmap.ConcurrentMap
// Global mapping of phone numbers to hashtags
var phoneToHashtagMap *cache.Cache
// Global socket server instance
var sio *socketio.Server
func main() {
// Sets the number of maxium goroutines to the 2*numberCPU + 1
// runtime.GOMAXPROCS()
var err error
file, err := os.Open("config.json")
if err != nil {
log.Fatal(err)
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&configuration)
if err != nil {
log.Fatal("Error loading config file:", err)
}
// Configuring socket.io Server
sio, err = socketio.NewServer(nil)
if err != nil {
log.Fatal(err)
}
hashmap = cmap.New()
// Create a cache with a default expiration time of 5 minutes, and which
// purges expired items every 30 seconds
// This is needed because Plivo doesn't give information about concatenated
// messages so we have to guess
// messageCache := cache.New(5*time.Minute, 30*time.Second)
// People forget to include the hashtag after the first message
// This will help us use hashtags from previous txts
phoneToHashtagMap = cache.New(12*time.Hour, 5*time.Minute)
/*
* There are two types of clients: presenter and viewer
* [presenter] needs to join a room of his socket.io so we can
* alert him when new texts come in.
* [viewer] needs to join a hashtag room so presenter can send
* him messages to display.
*/
sio.On("connection", func(so socketio.Socket) {
// So we can emit to the client like javascript version
so.Join(so.Id())
so.On("disconnection", func() {
clearSocketHashtag(so.Id())
})
// Used by viewer to join a room matching the URL hashtag
so.On("join room", func(room string) {
if onlyAlphanumeric(room) == false {
return
}
so.Join(room + "-guest")
// Tell the presenter a guest joined
so.BroadcastTo(room, "guest joined")
})
// Each presenter can only register a single hashtag (needed before anything else)
so.On("register hashtag", func(hashtag string) string {
// We might want to support unicode later on
// Then again, we only have US carriers and the URL must be ASCII
if onlyAlphanumeric(hashtag) == false {
log.Println("ERROR: onlyAlphanumeric", hashtag)
return "0"
}
if tmp, ok := hashmap.Get(hashtag); ok {
id := tmp.(string)
if id == so.Id() {
return "1"
}
// log.Println("already taken by", id)
return "0"
}
// This socket may or may not have already registered a hashtag
clearSocketHashtag(so.Id())
hashmap.Set(hashtag, so.Id())
hashmap.Set(so.Id(), hashtag) // so we can lookup hashtag on disconnect
// log.Println("registered", hashtag, so.Id())
// log.Println(hashmap.Items())
// Only registered people can pick a room to join
so.Join(hashtag)
// Tell any guests a presenter joined
so.BroadcastTo(hashtag+"-guest", "presenter joined")
return "1"
})
// Presenter publishing a message to viewers
so.On("publish", func(data string) {
log.Println("publish", data)
var msg MessageType
err = json.Unmarshal([]byte(data), &msg)
if err != nil {
log.Println(err, data)
}
if tmp, ok := hashmap.Get(so.Id()); ok {
hashtag := tmp.(string)
// log.Println("broadcastTo", hashtag+"-guest")
// so.BroadcastTo(hashtag+"-guest", "message", data)
so.BroadcastTo(hashtag+"-guest", msg.Type, msg.Data)
return
}
// No registered hashmap yet? Weird...
log.Println("ERROR: no hashtag yet")
})
// Presenter publishing poll to viewers
// so.On("poll", func(data string) {
//
// log.Println("poll", data)
//
// // No registered hashmap yet? Weird...
// if tmp, ok := hashmap.Get(so.Id()); ok {
// hashtag := tmp.(string)
// log.Println("broadcastTo", hashtag+"-guest")
// so.BroadcastTo(hashtag+"-guest", "poll", data)
// return
// }
//
// })
})
sio.On("error", func(so socketio.Socket, err error) {
log.Println("ERROR:", err)
})
// Send a random sample message every X seconds
sendDebugMessages(sio)
// Send a random vote every X seconds
sendDebugVotes(sio)
// Sets up the handlers and listen on port 8080
http.Handle("/socket.io/", sio)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
http.HandleFunc("/about/", serveGuestView)
http.HandleFunc("/start/", serveHostView)
http.HandleFunc("/receivePlivoSMS/", receivePlivoSMS)
http.Handle("/", http.FileServer(http.Dir("./templates/")))
log.Println("listening on", configuration.Port)
err = http.ListenAndServe(configuration.Port, nil)
log.Println(err)
}
/*
* Helper Functions
*/
// On first (correct) text, store the hashtag-to-phone relation
func lookupHashtagFromPhone(phone string) (hashtag string) {
if x, found := phoneToHashtagMap.Get(phone); found {
hashtag = x.(string)
}
return
}
func checkAuth(w http.ResponseWriter, r *http.Request, user string, pass string) bool {
s := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
if len(s) != 2 {
return false
}
b, err := base64.StdEncoding.DecodeString(s[1])
if err != nil {
return false
}
pair := strings.SplitN(string(b), ":", 2)
if len(pair) != 2 {
return false
}
return pair[0] == user && pair[1] == pass
}
/*
* Pretend to send SMS messages
*/
func sendDebugMessages(sio *socketio.Server) {
// Temp Debug Code
ticker := time.NewTicker(5 * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
randID := strings.Replace(strconv.FormatFloat(rand.Float64(), 'f', 15, 64), "0.", "", -1)
randomMessage := "Is it true that " +
randomdata.FullName(randomdata.RandomGender) +
" was born on " +
" " + randomdata.Month() + "/" + fmt.Sprintf("%d", time.Now().Year()-time.Now().Hour()) +
" at " + randomdata.Street() + "?"
// Send a sample message every whatever time
res := map[string]interface{}{
"from": hashPhone("800-222-2222"),
"id": randID,
"message": randomMessage,
// "dateTime": time.Now().UTC().Format(time.RFC3339),
"type": "message",
}
jsonRes, _ := json.Marshal(res)
sio.BroadcastTo("test", "message", string(jsonRes))
sio.BroadcastTo(fmt.Sprintf("test%d", time.Now().Year()), "message", string(jsonRes))
// sio.BroadcastTo("hashtagoneforgood", "message", string(jsonRes))
// log.Println("message")
case <-quit:
ticker.Stop()
return
}
}
}()
}
/*
* Pretend to send SMS messages
*/
func sendDebugVotes(sio *socketio.Server) {
// Temp Debug Code
ticker := time.NewTicker(1 * time.Second)
quit := make(chan struct{})
go func() {
voteOptions := []string{"a", "b", "c"}
for {
select {
case <-ticker.C:
randID := strings.Replace(strconv.FormatFloat(rand.Float64(), 'f', 15, 64), "0.", "", -1)
vote := voteOptions[rand.Intn(len(voteOptions))]
// Send a sample message every whatever time
res := map[string]interface{}{
"from": hashPhone("800-222-2222"),
"id": randID,
"message": vote,
"type": "message",
}
jsonRes, _ := json.Marshal(res)
sio.BroadcastTo("test-guest", "vote", string(jsonRes))
sio.BroadcastTo("test", "vote", string(jsonRes))
y := fmt.Sprintf("%d", time.Now().Year())
sio.BroadcastTo("test"+y+"-guest", "vote", string(jsonRes))
sio.BroadcastTo("test"+y, "vote", string(jsonRes))
case <-quit:
ticker.Stop()
return
}
}
}()
}