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

114 lines
3.8 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.thoughtcrime.securesms.crypto.MasterCipher;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.whispersystems.libaxolotl.util.guava.Optional;
import org.whispersystems.textsecure.api.messages.TextSecureAttachment;
import org.whispersystems.textsecure.api.messages.TextSecureGroup;
import org.whispersystems.textsecure.push.IncomingPushMessage;
import org.whispersystems.textsecure.push.PushMessageProtos.PushMessageContent;
import org.whispersystems.textsecure.util.Base64;
import java.util.List;
import ws.com.google.android.mms.pdu.CharacterSets;
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 09:26:43 +01:00
private final String groupId;
private final boolean push;
public IncomingMediaMessage(RetrieveConf retreived) {
2014-02-21 03:27:43 +01:00
this.headers = retreived.getPduHeaders();
this.body = retreived.getBody();
this.groupId = null;
this.push = false;
}
public IncomingMediaMessage(MasterSecret masterSecret,
String from,
String to,
long sentTimeMillis,
Optional<String> relay,
Optional<String> body,
Optional<TextSecureGroup> group,
Optional<List<TextSecureAttachment>> attachments)
{
this.headers = new PduHeaders();
this.body = new PduBody();
this.push = true;
if (group.isPresent()) {
this.groupId = GroupUtil.getEncodedId(group.get().getGroupId());
} else {
2014-02-21 03:27:43 +01:00
this.groupId = null;
}
this.headers.setEncodedStringValue(new EncodedStringValue(from), PduHeaders.FROM);
this.headers.appendEncodedStringValue(new EncodedStringValue(to), PduHeaders.TO);
this.headers.setLongInteger(sentTimeMillis / 1000, PduHeaders.DATE);
2014-02-21 03:27:43 +01:00
if (body.isPresent() && !org.whispersystems.textsecure.util.Util.isEmpty(body.get())) {
PduPart text = new PduPart();
text.setData(Util.toUtf8Bytes(body.get()));
text.setContentType(Util.toIsoBytes("text/plain"));
text.setCharset(CharacterSets.UTF_8);
this.body.addPart(text);
}
if (attachments.isPresent()) {
for (TextSecureAttachment attachment : attachments.get()) {
if (attachment.isPointer()) {
PduPart media = new PduPart();
byte[] encryptedKey = new MasterCipher(masterSecret).encryptBytes(attachment.asPointer().getKey());
media.setContentType(Util.toIsoBytes(attachment.getContentType()));
media.setContentLocation(Util.toIsoBytes(String.valueOf(attachment.asPointer().getId())));
media.setContentDisposition(Util.toIsoBytes(Base64.encodeBytes(encryptedKey)));
if (relay.isPresent()) {
media.setName(Util.toIsoBytes(relay.get()));
}
media.setPendingPush(true);
this.body.addPart(media);
}
}
}
}
public PduHeaders getPduHeaders() {
return headers;
}
public PduBody getBody() {
return body;
}
2014-01-14 09:26:43 +01:00
public String getGroupId() {
return groupId;
}
public boolean isPushMessage() {
return push;
}
public boolean isGroupMessage() {
2014-01-14 09:26:43 +01:00
return groupId != null ||
!Util.isEmpty(headers.getEncodedStringValues(PduHeaders.CC)) ||
(headers.getEncodedStringValues(PduHeaders.TO) != null &&
headers.getEncodedStringValues(PduHeaders.TO).length > 1);
}
}