forked from coyim/otr3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.go
75 lines (60 loc) · 1.99 KB
/
send.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
package otr3
import (
"bufio"
"bytes"
)
// Send takes a human readable message from the local user, possibly encrypts
// it and returns zero or more messages to send to the peer.
func (c *Conversation) Send(m ValidMessage) ([]ValidMessage, error) {
message := makeCopy(m)
defer wipeBytes(message)
if !c.Policies.isOTREnabled() {
return []ValidMessage{makeCopy(message)}, nil
}
if c.debug && bytes.Index(message, []byte(debugString)) != -1 {
c.dump(bufio.NewWriter(standardErrorOutput))
return nil, nil
}
switch c.msgState {
case plainText:
return c.withInjections(c.sendMessageOnPlaintext(message))
case encrypted:
return c.withInjections(c.sendMessageOnEncrypted(message))
case finished:
c.messageEvent(MessageEventConnectionEnded)
return c.withInjections(nil, newOtrError("cannot send message because secure conversation has finished"))
}
return c.withInjections(nil, newOtrError("cannot send message in current state"))
}
func (c *Conversation) sendMessageOnPlaintext(message ValidMessage) ([]ValidMessage, error) {
if c.Policies.has(requireEncryption) {
c.messageEvent(MessageEventEncryptionRequired)
c.updateLastSent()
c.updateMayRetransmitTo(retransmitExact)
c.lastMessage(MessagePlaintext(makeCopy(message)))
return []ValidMessage{c.QueryMessage()}, nil
}
return []ValidMessage{makeCopy(c.appendWhitespaceTag(message))}, nil
}
func (c *Conversation) sendMessageOnEncrypted(message ValidMessage) ([]ValidMessage, error) {
result, _, err := c.createSerializedDataMessage(message, messageFlagNormal, []tlv{})
if err != nil {
c.messageEvent(MessageEventEncryptionError)
c.generatePotentialErrorMessage(ErrorCodeEncryptionError)
}
return result, err
}
func (c *Conversation) sendDHCommit() (toSend messageWithHeader, err error) {
c.ake.wipe(true)
c.ake = nil
toSend, err = c.dhCommitMessage()
if err != nil {
return
}
toSend, err = c.wrapMessageHeader(msgTypeDHCommit, toSend)
if err != nil {
return nil, err
}
c.ake.state = authStateAwaitingDHKey{}
return
}