forked from signal-golang/textsecure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.go
130 lines (109 loc) · 3 KB
/
sender.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
package textsecure
import (
"time"
"github.com/golang/protobuf/proto"
"github.com/signal-golang/textsecure/config"
signalservice "github.com/signal-golang/textsecure/protobuf"
"github.com/signal-golang/textsecure/unidentifiedAccess"
log "github.com/sirupsen/logrus"
)
func sendMessage(msg *outgoingMessage) (uint64, error) {
// todo use UnidentifiedSenderMessage
if _, ok := deviceLists[msg.destination]; !ok {
deviceLists[msg.destination] = []uint32{1}
}
dm := createMessage(msg)
content := &signalservice.Content{
DataMessage: dm,
}
b, err := proto.Marshal(content)
if err != nil {
return 0, err
}
resp, err := buildAndSendMessage(msg.destination, padMessage(b), false, dm.Timestamp)
if err != nil {
return 0, err
}
var e164 *string
var uuid *string
if msg.destination[0] == '+' {
e164 = &msg.destination
} else {
uuid = &msg.destination
}
if resp.NeedsSync {
log.Debugf("[textsecure] Needs sync. destination: %s", msg.destination)
sm := &signalservice.SyncMessage{
Sent: &signalservice.SyncMessage_Sent{
DestinationE164: e164,
DestinationUuid: uuid,
Timestamp: dm.Timestamp,
Message: dm,
},
}
_, serr := sendSyncMessage(sm, dm.Timestamp)
if serr != nil {
log.WithFields(log.Fields{
"error": serr,
"destination": msg.destination,
"timestamp": resp.Timestamp,
}).Error("Failed to send sync message")
}
}
return resp.Timestamp, err
}
// TODO switch to uuids
func sendSyncMessage(sm *signalservice.SyncMessage, timestamp *uint64) (uint64, error) {
log.Debugln("[textsecure] sendSyncMessage", timestamp)
user := config.ConfigFile.Tel //TODO: switch tu uuid
if config.ConfigFile.UUID != "" {
user = config.ConfigFile.UUID
}
if _, ok := deviceLists[user]; !ok {
deviceLists[user] = []uint32{1}
}
content := &signalservice.Content{
SyncMessage: sm,
}
b, err := proto.Marshal(content)
if err != nil {
return 0, err
}
resp, err := buildAndSendMessage(user, padMessage(b), true, timestamp)
return resp.Timestamp, err
}
func sendVerifiedMessage(verified *signalservice.Verified, unidentifiedAccess *unidentifiedAccess.UnidentifiedAccess) error {
omsg := &outgoingNullMessage{
destination: verified.GetDestinationUuid(),
msg: &signalservice.NullMessage{
Padding: []byte{},
},
}
_, err := sendNullMessage(omsg)
return err
}
type outgoingNullMessage struct {
destination string
msg *signalservice.NullMessage
}
func sendNullMessage(msg *outgoingNullMessage) (uint64, error) {
if _, ok := deviceLists[msg.destination]; !ok {
deviceLists[msg.destination] = []uint32{1}
}
content := &signalservice.Content{
NullMessage: msg.msg,
}
b, err := proto.Marshal(content)
if err != nil {
return 0, err
}
now := uint64(time.Now().UnixNano() / 1000000)
resp, err := buildAndSendMessage(msg.destination, padMessage(b), false, &now)
if err != nil {
return 0, err
}
if resp.NeedsSync {
log.Debugf("[textsecure] Nullmessage needs sync. destination: %s", msg.destination)
}
return resp.Timestamp, err
}