Skip to content

Commit

Permalink
Fix enc attrs and simplify getting message content
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Apr 6, 2023
1 parent 091a74f commit 1ee2ff1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (cli *Client) handleRetryReceipt(receipt *events.Receipt, node *waBinary.No
err = cli.sendNode(waBinary.Node{
Tag: "message",
Attrs: attrs,
Content: append([]waBinary.Node{*encrypted}, cli.getExtraMessageContent(msg, attrs, includeDeviceIdentity)...),
Content: cli.getMessageContent(*encrypted, msg, attrs, includeDeviceIdentity),
})
if err != nil {
return fmt.Errorf("failed to send retry message: %w", err)
Expand Down
33 changes: 20 additions & 13 deletions send.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,8 +641,8 @@ func (cli *Client) preparePeerMessageNode(to types.JID, id types.MessageID, mess
}, nil
}

func (cli *Client) getExtraMessageContent(message *waProto.Message, msgAttrs waBinary.Attrs, includeIdentity bool) []waBinary.Node {
var content []waBinary.Node
func (cli *Client) getMessageContent(baseNode waBinary.Node, message *waProto.Message, msgAttrs waBinary.Attrs, includeIdentity bool) []waBinary.Node {
content := []waBinary.Node{baseNode}
if includeIdentity {
content = append(content, cli.makeDeviceIdentityNode())
}
Expand Down Expand Up @@ -700,13 +700,14 @@ func (cli *Client) prepareMessageNode(ctx context.Context, to, ownID types.JID,
start = time.Now()
participantNodes, includeIdentity := cli.encryptMessageForDevices(ctx, allDevices, ownID, id, plaintext, dsmPlaintext, encAttrs)
timings.PeerEncrypt = time.Since(start)
participantNode := waBinary.Node{
Tag: "participants",
Content: participantNodes,
}
return &waBinary.Node{
Tag: "message",
Attrs: attrs,
Content: append([]waBinary.Node{{
Tag: "participants",
Content: participantNodes,
}}, cli.getExtraMessageContent(message, attrs, includeIdentity)...),
Tag: "message",
Attrs: attrs,
Content: cli.getMessageContent(participantNode, message, attrs, includeIdentity),
}, allDevices, nil
}

Expand Down Expand Up @@ -811,7 +812,13 @@ func (cli *Client) encryptMessageForDeviceAndWrap(plaintext []byte, to types.JID
}, includeDeviceIdentity, nil
}

func (cli *Client) encryptMessageForDevice(plaintext []byte, to types.JID, bundle *prekey.Bundle, encAttrs waBinary.Attrs) (*waBinary.Node, bool, error) {
func copyAttrs(from, to waBinary.Attrs) {
for k, v := range from {
to[k] = v
}
}

func (cli *Client) encryptMessageForDevice(plaintext []byte, to types.JID, bundle *prekey.Bundle, extraAttrs waBinary.Attrs) (*waBinary.Node, bool, error) {
builder := session.NewBuilderFromSignal(cli.Store, to.SignalAddress(), pbSerializer)
if bundle != nil {
cli.Log.Debugf("Processing prekey bundle for %s", to)
Expand All @@ -833,14 +840,14 @@ func (cli *Client) encryptMessageForDevice(plaintext []byte, to types.JID, bundl
return nil, false, fmt.Errorf("cipher encryption failed: %w", err)
}

if encAttrs == nil {
encAttrs = make(waBinary.Attrs, 2)
encAttrs := waBinary.Attrs{
"v": "2",
"type": "msg",
}
encAttrs["v"] = "2"
encAttrs["type"] = "msg"
if ciphertext.Type() == protocol.PREKEY_TYPE {
encAttrs["type"] = "pkmsg"
}
copyAttrs(extraAttrs, encAttrs)

return &waBinary.Node{
Tag: "enc",
Expand Down

0 comments on commit 1ee2ff1

Please sign in to comment.