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