-
Notifications
You must be signed in to change notification settings - Fork 31
/
httpSettings.go
771 lines (713 loc) · 24.2 KB
/
httpSettings.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
// WebCall Copyright 2023 timur.mobi. All rights reserved.
//
// These methods enable callees to read and modify their
// callee specific settings and contacts.
//
// httpGetSettings() is called via XHR "/rtcsig/getsettings".
// httpSetSettings() is called via XHR "/rtcsig/setsettings".
// httpGetContacts() is called via XHR "/rtcsig/getcontacts".
// httpSetContact() is called via XHR "/rtcsig/setcontact".
// httpDeleteContact() is called via XHR "/rtcsig/deletecontact".
package main
import (
"net/http"
"fmt"
"encoding/json"
"io"
"strconv"
"strings"
)
func httpGetSettings(w http.ResponseWriter, r *http.Request, urlID string, calleeID string, cookie *http.Cookie, remoteAddr string) {
if cookie==nil {
// no settings without a cookie (but not worth logging)
//fmt.Printf("# /getsettings fail calleeID(%s) cookie==nil rip=%s\n", calleeID, remoteAddr)
return
}
if calleeID=="" {
fmt.Printf("# /getsettings fail no calleeID %s\n", remoteAddr)
return
}
// if calleeID!=urlID, that's likely someone trying to run more than one callee in the same browser
if urlID!="" && calleeID!=urlID {
// this happens bc someone with calleeID in the cookie is now trying to use urlID via url
fmt.Printf("# /getsettings urlID(%s) != calleeID(%s) %s ua=%s\n",
urlID, calleeID, remoteAddr, r.UserAgent())
return
}
var dbEntry DbEntry
err := kvMain.Get(dbRegisteredIDs,calleeID,&dbEntry)
if err!=nil {
fmt.Printf("# /getsettings (%s) fail on dbRegisteredIDs %s\n", calleeID, remoteAddr)
return
}
dbUserKey := fmt.Sprintf("%s_%d",calleeID, dbEntry.StartTime)
var dbUser DbUser
err = kvMain.Get(dbUserBucket, dbUserKey, &dbUser)
if err!=nil {
fmt.Printf("# /getsettings (%s) fail on dbUserBucket %s\n", calleeID, remoteAddr)
return
}
if logWantedFor("getsettings") {
fmt.Printf("/getsettings (%s) %s ua=%s\n", calleeID, remoteAddr, r.UserAgent())
}
var reqBody []byte
readConfigLock.RLock() // for vapidPublicKey
reqBody, err = json.Marshal(map[string]string{
"nickname": dbUser.Name,
"mastodonID": dbUser.MastodonID,
"tootOnCall": strconv.FormatBool(dbUser.MastodonSendTootOnCall),
"askCallerBeforeNotify": strconv.FormatBool(dbUser.AskCallerBeforeNotify),
"storeContacts": strconv.FormatBool(dbUser.StoreContacts),
"storeMissedCalls": strconv.FormatBool(dbUser.StoreMissedCalls),
"dialSoundsMuted": strconv.FormatBool(bool(dbUser.Int2&4==4)),
"mainLinkDeactive": strconv.FormatBool(bool(dbUser.Int2&8==8)),
"mastodonLinkDeactive": strconv.FormatBool(bool(dbUser.Int2&16==16)),
// "webPushSubscription1": dbUser.Str2,
// "webPushUA1": dbUser.Str2ua,
// "webPushSubscription2": dbUser.Str3,
// "webPushUA2": dbUser.Str3ua,
// "vapidPublicKey": vapidPublicKey,
"dialSounds": strconv.FormatBool(!(dbUser.Int2&4==4)), // bit4 set for mute (bit4 clear = play dialsounds)
"serverVersion": codetag,
})
readConfigLock.RUnlock()
if err != nil {
fmt.Printf("# /getsettings (%s) fail on json.Marshal %s\n", calleeID, remoteAddr)
return
}
if logWantedFor("getsettings") {
fmt.Printf("/getsettings (%s) done [%s]\n",calleeID,reqBody)
}
fmt.Fprintf(w,string(reqBody))
return
}
func httpSetSettings(w http.ResponseWriter, r *http.Request, urlID string, calleeID string, cookie *http.Cookie, remoteAddr string) {
if calleeID=="" {
fmt.Printf("# /setsettings fail no calleeID %s\n", remoteAddr)
return
}
if cookie==nil {
fmt.Printf("# /setsettings (%s) fail no cookie %s\n", calleeID, remoteAddr)
return
}
// if calleeID!=urlID, could be a user trying to run more than one callee in the same browser
if urlID!="" && calleeID!=urlID {
fmt.Printf("# /setsettings fail calleeID(%s) != urlID(%s) %s\n", calleeID, urlID, remoteAddr)
return
}
// get json response via post to store settings for calleeID (from cookie)
data := ""
postBuf := make([]byte, 2000)
length,_ := io.ReadFull(r.Body, postBuf)
if length>0 {
data = string(postBuf[:length])
}
if data=="" {
fmt.Printf("# /setsettings (%s) failed on io.ReadFull body %s\n",calleeID, remoteAddr)
return
}
var newSettingsMap map[string]string
err := json.Unmarshal([]byte(data), &newSettingsMap)
if err!=nil {
fmt.Printf("# /setsettings (%s) failed on json.Unmarshal (%v) %s err=%v\n",
calleeID, data, remoteAddr, err)
// decoding issue in r.Body: any changes will be lost
return
}
var dbEntry DbEntry
err = kvMain.Get(dbRegisteredIDs,calleeID,&dbEntry)
if err!=nil {
fmt.Printf("# /setsettings (%s) failed on get dbRegisteredIDs dbEntry %s\n", calleeID, remoteAddr)
// any changes will be lost
return
}
dbUserKey := fmt.Sprintf("%s_%d",calleeID, dbEntry.StartTime)
var dbUser DbUser
err = kvMain.Get(dbUserBucket, dbUserKey, &dbUser)
if err!=nil {
fmt.Printf("# /setsettings (%s) failed on get dbUserBucket dbUser key=%s %s\n", calleeID, dbUserKey, remoteAddr)
// any changes will be lost
return
}
if logWantedFor("setsettings") {
fmt.Printf("/setsettings (%s) len=%d rip=%s %v\n", calleeID, len(data), remoteAddr, newSettingsMap)
}
for key,val := range newSettingsMap {
switch(key) {
case "nickname":
if val != dbUser.Name {
if logWantedFor("setsettings") {
fmt.Printf("/setsettings (%s) nickname (%s) (old:%s) %s\n",calleeID,val,dbUser.Name,remoteAddr)
}
dbUser.Name = val
}
case "mastodonID":
if val != dbUser.MastodonID {
if logWantedFor("setsettings") {
fmt.Printf("/setsettings (%s) mastodonID (%s) (old:%s) %s\n",
calleeID,val,dbUser.MastodonID,remoteAddr)
}
dbUser.MastodonID = val
}
case "tootOnCall":
if(val=="true") {
if dbUser.MastodonSendTootOnCall != true {
if logWantedFor("setsettings") {
fmt.Printf("/setsettings (%s) tootOnCall (%s) (old:%v) %s\n",
calleeID, val, dbUser.MastodonSendTootOnCall, remoteAddr)
}
dbUser.MastodonSendTootOnCall = true
}
} else {
if dbUser.MastodonSendTootOnCall != false {
if logWantedFor("setsettings") {
fmt.Printf("/setsettings (%s) tootOnCall (%s) (old:%v) %s\n",
calleeID, val, dbUser.MastodonSendTootOnCall, remoteAddr)
}
dbUser.MastodonSendTootOnCall = false
}
}
case "askCallerBeforeNotify":
if(val=="true") {
if dbUser.AskCallerBeforeNotify != true {
if logWantedFor("setsettings") {
fmt.Printf("/setsettings (%s) askCallerBeforeNotify (%s) (old:%v) %s\n",
calleeID, val, dbUser.AskCallerBeforeNotify, remoteAddr)
}
dbUser.AskCallerBeforeNotify = true
}
} else {
if dbUser.AskCallerBeforeNotify != false {
if logWantedFor("setsettings") {
fmt.Printf("/setsettings (%s) askCallerBeforeNotify (%s) (old:%v) %s\n",
calleeID, val, dbUser.AskCallerBeforeNotify, remoteAddr)
}
dbUser.AskCallerBeforeNotify = false
}
}
case "storeContacts":
if(val=="true") {
if dbUser.StoreContacts != true {
if logWantedFor("setsettings") {
fmt.Printf("/setsettings (%s) storeContacts (%s) (old:%v) %s\n",
calleeID, val, dbUser.StoreContacts, remoteAddr)
}
dbUser.StoreContacts = true
}
} else {
if dbUser.StoreContacts != false {
if logWantedFor("setsettings") {
fmt.Printf("/setsettings (%s) storeContacts (%s) (old:%v) %s\n",
calleeID, val, dbUser.StoreContacts, remoteAddr)
}
dbUser.StoreContacts = false
}
}
case "storeMissedCalls":
if(val=="true") {
if !dbUser.StoreMissedCalls {
if logWantedFor("setsettings") {
fmt.Printf("/setsettings (%s) storeMissedCalls (%s) old:%v\n",
calleeID,val,dbUser.StoreMissedCalls)
}
dbUser.StoreMissedCalls = true
// show missedCalls on callee web client (if avail)
hubMapMutex.RLock()
hub := hubMap[calleeID]
hubMapMutex.RUnlock()
if hub!=nil && hub.CalleeClient!=nil {
var callsWhileInAbsence []CallerInfo
err := kvCalls.Get(dbMissedCalls,calleeID,&callsWhileInAbsence)
if err!=nil {
// "key not found" is here NOT an error
if strings.Index(err.Error(),"key not found")<0 {
fmt.Printf("# /setsettings (%s) storeMissedCalls kvCalls.Get fail err=%v\n",
calleeID, err)
}
} else {
json, err := json.Marshal(callsWhileInAbsence)
if err != nil {
fmt.Printf("# /setsettings (%s) storeMissedCalls json.Marshal fail err=%v\n",
calleeID, err)
} else {
hub.CalleeClient.Write([]byte("missedCalls|"+string(json)))
}
}
}
}
} else {
if dbUser.StoreMissedCalls {
if logWantedFor("setsettings") {
fmt.Printf("/setsettings (%s) storeMissedCalls (%s) old:%v %s\n",
calleeID, val, dbUser.StoreMissedCalls, remoteAddr)
}
dbUser.StoreMissedCalls = false
// hide missedCalls on callee web client
hubMapMutex.RLock()
hub := hubMap[calleeID]
hubMapMutex.RUnlock()
if hub!=nil && hub.CalleeClient!=nil {
hub.CalleeClient.Write([]byte("missedCalls|")) // websocket required
}
}
}
case "dialSoundsMuted":
if val=="true" {
if dbUser.Int2 & 4 == 0 {
// set dialsounds off (muted)
dbUser.Int2 |= 4
if logWantedFor("setsettings") {
fmt.Printf("/setsettings (%s) dialSoundsMuted (%s) %d %s\n",
calleeID, val, dbUser.Int2&4, remoteAddr)
}
}
} else {
if dbUser.Int2 & 4 == 4 {
// set dialsounds on (not muted)
dbUser.Int2 &= ^4
if logWantedFor("setsettings") {
fmt.Printf("/setsettings (%s) dialSoundsMuted (%s) %d %s\n",
calleeID, val, dbUser.Int2&4, remoteAddr)
}
}
}
case "mainLinkDeactive":
if val=="true" {
if dbUser.Int2 & 8 == 0 {
// set mainLink off (deactive)
dbUser.Int2 |= 8
if logWantedFor("setsettings") {
fmt.Printf("/setsettings (%s) mainLinkDeactive (%s) %d %s\n",
calleeID, val, dbUser.Int2&8, remoteAddr)
}
}
} else {
if dbUser.Int2 & 8 == 8 {
// set mainLink on
dbUser.Int2 &= ^8
if logWantedFor("setsettings") {
fmt.Printf("/setsettings (%s) mainLinkDeactive (%s) %d %s\n",
calleeID, val, dbUser.Int2&8, remoteAddr)
}
}
}
case "mastodonLinkDeactive":
if(val=="true") {
if dbUser.Int2 & 16 == 0 {
// set mainLink off (deactive)
dbUser.Int2 |= 16
if logWantedFor("setsettings") {
fmt.Printf("/setsettings (%s) mainLinkDeactive (%s) %d %s\n",
calleeID, val, dbUser.Int2&16, remoteAddr)
}
}
} else {
if dbUser.Int2 & 16 == 16 {
// set mainLink on
dbUser.Int2 &= ^16
if logWantedFor("setsettings") {
fmt.Printf("/setsettings (%s) mainLinkDeactive (%s) %d %s\n",
calleeID, val, dbUser.Int2&16, remoteAddr)
}
}
}
/*
case "webPushSubscription1":
newVal,err := url.QueryUnescape(val)
if err!=nil {
fmt.Printf("# /setsettings (%s) url.QueryUnescape webPushSubscription1 err=%v\n",
calleeID, err)
} else if newVal != dbUser.Str2 {
fmt.Printf("/setsettings (%s) new webPushSubscription1 (%s) (old:%s)\n",
calleeID, newVal, dbUser.Str2)
if dbUser.Str2 != newVal {
dbUser.Str2 = newVal
if newVal!="" {
// send welcome/verification push-msg
msg := "You will from now on receive a WebPush notification for every call"+
" you receive while not being connected to the WebCall server."
err,statusCode := webpushSend(dbUser.Str2,msg,calleeID)
if err!=nil {
fmt.Printf("# setsettings (%s) webpush fail device1 err=%v\n",calleeID,err)
} else if statusCode==201 {
// success
} else if statusCode==410 {
fmt.Printf("# setsettings (%s) webpush fail device1 delete subscr\n",
calleeID)
dbUser.Str2 = ""
} else {
fmt.Printf("# setsettings (%s) webpush fail device1 status=%d\n",
calleeID, statusCode)
}
}
}
}
case "webPushUA1":
newVal,err := url.QueryUnescape(val)
if err!=nil {
fmt.Printf("# /setsettings (%s) url.QueryUnescape webPushUA1 err=%v\n",
calleeID, err)
} else if newVal != dbUser.Str2ua {
fmt.Printf("/setsettings (%s) new webPushUA1 (%s) (old:%s)\n",
calleeID, newVal, dbUser.Str2ua)
dbUser.Str2ua = newVal
}
case "webPushSubscription2":
newVal,err := url.QueryUnescape(val)
if err!=nil {
fmt.Printf("# /setsettings (%s) url.QueryUnescape webPushSubscription2 err=%v\n",
calleeID, err)
} else if newVal != dbUser.Str3 {
fmt.Printf("/setsettings (%s) new webPushSubscription2 (%s) (old:%s)\n",
calleeID, newVal, dbUser.Str3)
if dbUser.Str3 != newVal {
dbUser.Str3 = newVal
if newVal!="" {
// send welcome/verification push-msg
msg := "You will from now on receive a WebPush notification for every call"+
" you receive while not being connected to the WebCall server."
err,statusCode := webpushSend(dbUser.Str3,msg,calleeID)
if err!=nil {
fmt.Printf("# /setsettings (%s) webpush fail device2 err=%v\n",calleeID,err)
} else if statusCode==201 {
// success
} else if statusCode==410 {
fmt.Printf("# /setsettings (%s) webpush fail device2 delete subscr\n",
calleeID)
dbUser.Str3 = ""
} else {
fmt.Printf("# /setsettings (%s) webpush fail device2 status=%d\n",
calleeID, statusCode)
}
}
}
}
case "webPushUA2":
newVal,err := url.QueryUnescape(val)
if err!=nil {
fmt.Printf("# /setsettings (%s) url.QueryUnescape webPushUA2 err=%v\n",
calleeID, err)
} else if newVal != dbUser.Str3ua {
fmt.Printf("/setsettings (%s) new webPushUA2 (%s) (old:%s)\n",
calleeID, newVal, dbUser.Str3ua)
dbUser.Str3ua = newVal
}
*/
}
}
// store data
err = kvMain.Put(dbUserBucket, dbUserKey, dbUser, false)
if err!=nil {
fmt.Printf("# /setsettings (%s) store db=%s bucket=%s %s err=%v\n",
calleeID, dbMainName, dbUserBucket, remoteAddr, err)
} else {
//fmt.Printf("/setsettings (%s) stored db=%s bucket=%s\n", calleeID, dbMainName, dbUserBucket)
}
return
}
func httpGetContacts(w http.ResponseWriter, r *http.Request, urlID string, calleeID string, cookie *http.Cookie, remoteAddr string) {
if calleeID=="" {
fmt.Printf("# /getcontacts calleeID empty urlID=%s %s\n",urlID, remoteAddr)
return
}
if cookie==nil {
fmt.Printf("# /getcontacts (%s) fail no cookie %s\n", calleeID, remoteAddr)
return
}
// if calleeID!=urlID, that's likely someone trying to run more than one callee in the same browser
if urlID!="" && urlID!=calleeID {
fmt.Printf("# /getcontacts urlID=%s != calleeID=%s %s\n",urlID,calleeID, remoteAddr)
return
}
var idNameMap map[string]string // callerID(@host) -> name
err := kvContacts.Get(dbContactsBucket,calleeID,&idNameMap)
if err!=nil {
fmt.Printf("# /getcontacts db get calleeID=%s %s err=%v\n", calleeID, remoteAddr, err)
return
}
jsonStr, err := json.Marshal(idNameMap)
if err != nil {
fmt.Printf("# /getcontacts (%s) failed on json.Marshal %s err=%v\n", calleeID, remoteAddr, err)
return
}
if logWantedFor("contacts") {
fmt.Printf("/getcontacts (%s) send %d elements %s\n", calleeID, len(idNameMap), remoteAddr)
}
fmt.Fprintf(w,string(jsonStr))
return
}
func httpGetContact(w http.ResponseWriter, r *http.Request, urlID string, calleeID string, cookie *http.Cookie, remoteAddr string) {
if calleeID=="" {
fmt.Printf("# /getcontact calleeID empty urlID=%s %s\n",urlID, remoteAddr)
return
}
if cookie==nil {
fmt.Printf("# /getcontact (%s) fail no cookie %s\n", calleeID, remoteAddr)
return
}
// if calleeID!=urlID, that's likely someone trying to run more than one callee in the same browser
if urlID!="" && urlID!=calleeID {
fmt.Printf("# /getcontact urlID=%s != calleeID=%s %s\n",urlID,calleeID, remoteAddr)
return
}
url_arg_array, ok := r.URL.Query()["contactID"]
if ok && len(url_arg_array[0]) >= 1 {
contactID := url_arg_array[0]
// cut off @host from contactID if host starts with hostname of local server
idxAt := strings.Index(contactID,"@"+hostname)
if idxAt >=0 {
contactID = contactID[:idxAt]
}
if contactID=="" {
if logWantedFor("contacts") {
fmt.Printf("/getcontact (%s) empty id=(%s)\n", calleeID, contactID)
}
return
}
var idNameMap map[string]string // callerID(@host) -> name
err := kvContacts.Get(dbContactsBucket,calleeID,&idNameMap)
if err!=nil {
fmt.Printf("# /getcontact (%s) db get %s err=%v\n", calleeID, remoteAddr, err)
return
}
compoundName := idNameMap[contactID]
if compoundName=="" {
//fmt.Printf("/getcontact (%s) id=%s not found rip=%s\n", calleeID, contactID, remoteAddr)
return
}
if logWantedFor("contacts") {
fmt.Printf("/getcontact (%s) id=%s found=%s rip=%s\n", calleeID, contactID, compoundName, remoteAddr)
}
fmt.Fprintf(w,compoundName)
}
return
}
func httpSetContact(w http.ResponseWriter, r *http.Request, urlID string, calleeID string, cookie *http.Cookie, remoteAddr string) {
// store contactID with name into contacts of calleeID
// httpSetContact does not report errors back to the client (only logs them)
if calleeID=="" || calleeID=="undefined" {
//fmt.Printf("# /setcontact urlID empty\n")
return
}
if cookie==nil {
fmt.Printf("# /setcontact (%s) fail no cookie %s\n", calleeID, remoteAddr)
return
}
// if calleeID!=urlID, that's likely someone trying to run more than one callee in the same browser
if urlID!="" && urlID!=calleeID {
fmt.Printf("# /setcontact urlID=%s != calleeID=%s %s\n", urlID, calleeID, remoteAddr)
return
}
if strings.HasPrefix(calleeID,"answie") || strings.HasPrefix(calleeID,"talkback") {
return
}
contactID := "" // may or may not have @host attached
contactName := ""
if r.Method=="POST" {
// TODO implement delivery of contactID and contactName via post body
} else {
url_arg_array, ok := r.URL.Query()["contactID"]
if ok && len(url_arg_array[0]) >= 1 {
contactID = url_arg_array[0]
}
if contactID=="" {
if logWantedFor("contacts") {
fmt.Printf("/setcontact (%s) contactID from client is empty %s\n", calleeID, remoteAddr)
}
return
}
// contactName as format: name|prefCallbackId|ourNickname
url_arg_array, ok = r.URL.Query()["name"]
if ok && len(url_arg_array[0]) >= 1 {
contactName = url_arg_array[0]
}
}
//fmt.Printf("/setcontact (%s) -> setcontact()\n", calleeID)
if !setContact(calleeID, contactID, contactName, remoteAddr, "http") {
// an error has occured
}
}
func setContact(calleeID string, contactID string, compoundName string, remoteAddr string, comment string) bool {
// calleeID = the callee for which to add a contact
// contactID = the userid to be added / changed
// compoundName = contactName+"|"+callerId+"|"+callerName
// contactName must split compoundName
if strings.HasPrefix(calleeID,"answie") || strings.HasPrefix(calleeID,"talkback") {
return true
}
contactName := "";
callerID := "";
callerName := "";
tokenSlice := strings.Split(compoundName, "|")
for idx, tok := range tokenSlice {
switch idx {
case 0: contactName = tok
case 1: callerID = tok
case 2: callerName = tok
}
}
//fmt.Printf("setcontact (%s) compoundName=%s contactName=%s comment=%s\n",
// calleeID, compoundName, contactName, comment)
// if dbUser.StoreContacts==false (not checked), just return true
var dbEntry DbEntry
err := kvMain.Get(dbRegisteredIDs,calleeID,&dbEntry)
if err!=nil {
fmt.Printf("# setcontact (%s) fail on dbRegisteredIDs %s\n", calleeID, remoteAddr)
return false
}
dbUserKey := fmt.Sprintf("%s_%d",calleeID, dbEntry.StartTime)
var dbUser DbUser
err = kvMain.Get(dbUserBucket, dbUserKey, &dbUser)
if err!=nil {
fmt.Printf("# setcontact (%s) fail on dbUserBucket %s\n", calleeID, remoteAddr)
return false
}
if !dbUser.StoreContacts {
if logWantedFor("contacts") {
fmt.Printf("setcontact (%s) !StoreContacts %s\n", calleeID, remoteAddr)
}
return true
}
// cut off @host from contactID if host starts with hostname of local server
idxAt := strings.Index(contactID,"@"+hostname)
if idxAt >=0 {
contactID = contactID[:idxAt]
}
if contactID=="" {
fmt.Printf("# setcontact (%s) abort on empty contactID %s\n", calleeID, remoteAddr)
return false
}
// read the complete contacts for calleeID into idNameMap
var idNameMap map[string]string // calleeID -> contactName
err = kvContacts.Get(dbContactsBucket,calleeID,&idNameMap)
if err!=nil {
if(strings.Index(err.Error(),"key not found")<0) {
fmt.Printf("# setcontact db get calleeID=%s %s err=%v\n", calleeID, remoteAddr, err)
return false
}
// "key not found" is just an empty contacts list
if logWantedFor("contacts") {
fmt.Printf("setcontact creating new contacts map %s\n", remoteAddr)
}
idNameMap = make(map[string]string)
}
// check for contactID
oldCompoundName,ok := idNameMap[contactID]
if !ok || oldCompoundName=="" {
// try lowercase contactID
contactID = strings.ToLower(contactID)
oldCompoundName,ok = idNameMap[contactID]
}
if !ok {
// check for uppercase contactID
toUpperContactID := strings.ToUpper(contactID[0:1])+contactID[1:]
if logWantedFor("contacts") {
fmt.Printf("setcontact (%s->%s) check toUpperContactID=%s\n",
calleeID, contactName, toUpperContactID)
}
oldCompoundName,ok = idNameMap[toUpperContactID]
if ok {
contactID = toUpperContactID
}
}
if ok {
oldName := ""
oldCallerID := "";
oldCallerName := "";
tokenSlice = strings.Split(oldCompoundName, "|")
for idx, tok := range tokenSlice {
switch idx {
case 0: oldName = tok
case 1: oldCallerID = tok
case 2: oldCallerName = tok
}
}
//fmt.Printf("setcontact (%s) oldCompoundName=%s oldName=%s\n", calleeID, oldCompoundName, oldName)
if contactName=="" && oldName!="" {
contactName = oldName
}
if callerID=="" && oldCallerID!="" {
callerID = oldCallerID
}
if callerName=="" && oldCallerName!="" {
callerName = oldCallerName
}
}
if contactName=="" {
contactName = "unknown"
}
newCompoundName := contactName+"|"+callerID+"|"+callerName
if newCompoundName == oldCompoundName {
// contactName for contactID exists and is same as oldName - don't overwrite
if logWantedFor("contacts") {
fmt.Printf("setcontact (%s) contactID=%s already exists, skip (%s) %s\n",
calleeID, contactID, newCompoundName, remoteAddr)
}
return true
}
// we may override the name of a contact edited by callee (oldCompoundName) with the nickname given by caller
if logWantedFor("contacts") {
fmt.Printf("setcontact (%s) store ID=%s from (%s) to (%s) %s %s\n",
calleeID, contactID, oldCompoundName, newCompoundName, remoteAddr, comment)
}
idNameMap[contactID] = newCompoundName
//fmt.Printf("setcontact (%s) idNameMap=%v\n", calleeID, idNameMap[contactID])
err = kvContacts.Put(dbContactsBucket, calleeID, idNameMap, false)
if err!=nil {
fmt.Printf("# setcontact (%s) store contactID=%s %s err=%v\n", calleeID, contactID, remoteAddr, err)
return false
}
return true
}
func httpDeleteContact(w http.ResponseWriter, r *http.Request, urlID string, calleeID string, cookie *http.Cookie, remoteAddr string) {
if calleeID=="" {
fmt.Printf("# /deletecontact calleeID empty %s\n", remoteAddr)
return
}
if(cookie==nil) {
fmt.Printf("# /deletecontact cookie==nil urlID=%s calleeID=%s %s\n", urlID, calleeID, remoteAddr)
return
}
// if calleeID!=urlID, that's likely someone trying to run more than one callee in the same browser
if urlID!=calleeID {
fmt.Printf("# /deletecontact urlID=%s != calleeID=%s %s\n", urlID, calleeID, remoteAddr)
return
}
contactID := ""
url_arg_array, ok := r.URL.Query()["contactID"]
if ok && len(url_arg_array[0]) >= 1 {
contactID = url_arg_array[0]
}
if contactID=="" {
fmt.Printf("# /deletecontact (%s) contactID from client is empty %s\n", calleeID, remoteAddr)
return
}
// delete a single contactID from calleeID's contacts
var idNameMap map[string]string // callerID -> name
err := kvContacts.Get(dbContactsBucket,calleeID,&idNameMap)
if err!=nil {
fmt.Printf("# /deletecontact db get calleeID=%s %s err=%v\n", calleeID, remoteAddr, err)
return
}
_,ok = idNameMap[contactID]
if !ok {
_,ok = idNameMap[strings.ToLower(contactID)]
if !ok {
fmt.Printf("# /deletecontact (%s) idNameMap[%s] does not exist %s\n",
calleeID, contactID, remoteAddr)
return
}
contactID = strings.ToLower(contactID)
}
delete(idNameMap,contactID)
err = kvContacts.Put(dbContactsBucket, calleeID, idNameMap, false)
if err!=nil {
fmt.Printf("# /deletecontact store calleeID=%s %s err=%v\n", calleeID, remoteAddr, err)
return
}
if logWantedFor("contacts") {
fmt.Printf("/deletecontact calleeID=(%s) contactID[%s] %s\n",calleeID, contactID, remoteAddr)
}
fmt.Fprintf(w,"ok")
return
}