From 47541e5a57637966a55e957de4dd779e4ebb4702 Mon Sep 17 00:00:00 2001
From: bontric <benjohnwie@gmail.com>
Date: Fri, 28 Sep 2018 20:16:16 +0200
Subject: [PATCH] add contact Id to TAKE request

---
 .../mailbox/protocol/MailboxRequestTake.java  | 37 +++++++++++++++++--
 1 file changed, 33 insertions(+), 4 deletions(-)

diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/protocol/MailboxRequestTake.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/protocol/MailboxRequestTake.java
index 5f7720d7f..7da6925f0 100644
--- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/protocol/MailboxRequestTake.java
+++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/protocol/MailboxRequestTake.java
@@ -4,11 +4,21 @@ import org.briarproject.bramble.api.FormatException;
 import org.briarproject.bramble.api.contact.ContactId;
 import org.briarproject.bramble.api.data.BdfList;
 
+import javax.annotation.Nullable;
+
 public class MailboxRequestTake extends MailboxRequest {
-	private byte[] encryptedSyncStream;
+	private final ContactId contactId;
+	private final byte[] encryptedSyncStream;
 
 	public MailboxRequestTake(byte[] encryptedSyncStream) {
 		super(TYPE.TAKE);
+		this.contactId = null;
+		this.encryptedSyncStream = encryptedSyncStream;
+	}
+
+	public MailboxRequestTake(ContactId contactId, byte[] encryptedSyncStream) {
+		super(TYPE.TAKE);
+		this.contactId = contactId;
 		this.encryptedSyncStream = encryptedSyncStream;
 	}
 
@@ -16,14 +26,26 @@ public class MailboxRequestTake extends MailboxRequest {
 		super(TYPE.TAKE, msg.getLong(1));
 
 		BdfList body = msg.getList(2);
-		if(body.size() != 1)
+		if (body.size() != 2)
 			throw new FormatException();
-		this.encryptedSyncStream = body.getRaw(0);
+
+		Long cId = body.getOptionalLong(0);
+		if (cId != null) {
+			if (cId > Integer.MAX_VALUE || cId < Integer.MIN_VALUE)
+				throw new FormatException();
+			contactId = new ContactId(cId.intValue());
+		} else
+			contactId = null;
+
+		encryptedSyncStream = body.getRaw(1);
 	}
 
 	@Override
 	protected BdfList getRequestBody() {
-		return BdfList.of(encryptedSyncStream);
+		if (contactId == null)
+			return BdfList.of(null, encryptedSyncStream);
+
+		return BdfList.of(contactId.getInt(), encryptedSyncStream);
 	}
 
 	@Override
@@ -35,5 +57,12 @@ public class MailboxRequestTake extends MailboxRequest {
 		return encryptedSyncStream;
 	}
 
+	@Nullable
+	public ContactId getContactId() {
+		return contactId;
+	}
 
+	public boolean hasContactId() {
+		return contactId != null;
+	}
 }
-- 
GitLab