File: send.go

package info (click to toggle)
golang-github-twstrike-otr3 0.0~git20161015.0.744856d-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 1,080 kB
  • sloc: ansic: 127; makefile: 76
file content (75 lines) | stat: -rw-r--r-- 2,113 bytes parent folder | download | duplicates (3)
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, trace ...interface{}) ([]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, trace...))
	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, trace ...interface{}) ([]ValidMessage, error) {
	if c.Policies.has(requireEncryption) {
		c.messageEvent(MessageEventEncryptionRequired, trace...)
		c.updateLastSent()
		c.updateMayRetransmitTo(retransmitExact)
		c.lastMessage(MessagePlaintext(makeCopy(message)), trace...)
		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
}