session-android/src/org/thoughtcrime/securesms/mms/IncomingMediaMessage.java

101 lines
3.4 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.mms;
import org.thoughtcrime.securesms.util.GroupUtil;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.textsecure.crypto.MasterCipher;
import org.whispersystems.textsecure.crypto.MasterSecret;
import org.whispersystems.textsecure.push.IncomingPushMessage;
import org.whispersystems.textsecure.push.PushMessageProtos.PushMessageContent;
import org.whispersystems.textsecure.util.Base64;
import org.whispersystems.textsecure.util.Hex;
import ws.com.google.android.mms.pdu.EncodedStringValue;
import ws.com.google.android.mms.pdu.PduBody;
import ws.com.google.android.mms.pdu.PduHeaders;
import ws.com.google.android.mms.pdu.PduPart;
import ws.com.google.android.mms.pdu.RetrieveConf;
public class IncomingMediaMessage {
private final PduHeaders headers;
private final PduBody body;
2014-01-14 08:26:43 +00:00
private final String groupId;
private final boolean push;
public IncomingMediaMessage(RetrieveConf retreived) {
2014-02-21 02:27:43 +00:00
this.headers = retreived.getPduHeaders();
this.body = retreived.getBody();
this.groupId = null;
this.push = false;
}
public IncomingMediaMessage(MasterSecret masterSecret, String localNumber,
IncomingPushMessage message,
PushMessageContent messageContent)
{
this.headers = new PduHeaders();
this.body = new PduBody();
this.push = true;
if (messageContent.hasGroup()) {
2014-02-21 02:27:43 +00:00
this.groupId = GroupUtil.getEncodedId(messageContent.getGroup().getId().toByteArray());
} else {
2014-02-21 02:27:43 +00:00
this.groupId = null;
}
this.headers.setEncodedStringValue(new EncodedStringValue(message.getSource()), PduHeaders.FROM);
this.headers.appendEncodedStringValue(new EncodedStringValue(localNumber), PduHeaders.TO);
this.headers.setLongInteger(message.getTimestampMillis() / 1000, PduHeaders.DATE);
2014-02-21 02:27:43 +00:00
2014-01-14 08:26:43 +00:00
if (!org.whispersystems.textsecure.util.Util.isEmpty(messageContent.getBody())) {
PduPart text = new PduPart();
text.setData(Util.toIsoBytes(messageContent.getBody()));
text.setContentType(Util.toIsoBytes("text/plain"));
body.addPart(text);
}
if (messageContent.getAttachmentsCount() > 0) {
for (PushMessageContent.AttachmentPointer attachment : messageContent.getAttachmentsList()) {
PduPart media = new PduPart();
byte[] encryptedKey = new MasterCipher(masterSecret).encryptBytes(attachment.getKey().toByteArray());
media.setContentType(Util.toIsoBytes(attachment.getContentType()));
media.setContentLocation(Util.toIsoBytes(String.valueOf(attachment.getId())));
media.setContentDisposition(Util.toIsoBytes(Base64.encodeBytes(encryptedKey)));
if (message.getRelay() != null) {
media.setName(Util.toIsoBytes(message.getRelay()));
}
media.setPendingPush(true);
body.addPart(media);
}
}
}
public PduHeaders getPduHeaders() {
return headers;
}
public PduBody getBody() {
return body;
}
2014-01-14 08:26:43 +00:00
public String getGroupId() {
return groupId;
}
public boolean isPushMessage() {
return push;
}
public boolean isGroupMessage() {
2014-01-14 08:26:43 +00:00
return groupId != null ||
!Util.isEmpty(headers.getEncodedStringValues(PduHeaders.CC)) ||
(headers.getEncodedStringValues(PduHeaders.TO) != null &&
headers.getEncodedStringValues(PduHeaders.TO).length > 1);
}
}