1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-12-02 14:02:17 -05:00
k-9/src/com/fsck/k9/crypto/CryptoHelper.java
2014-03-02 16:51:47 +01:00

78 lines
2.2 KiB
Java

package com.fsck.k9.crypto;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.Part;
import com.fsck.k9.mail.internet.MimeUtility;
public class CryptoHelper {
public static Pattern PGP_MESSAGE =
Pattern.compile(".*?(-----BEGIN PGP MESSAGE-----.*?-----END PGP MESSAGE-----).*",
Pattern.DOTALL);
public static Pattern PGP_SIGNED_MESSAGE =
Pattern.compile(
".*?(-----BEGIN PGP SIGNED MESSAGE-----.*?-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----).*",
Pattern.DOTALL);
public CryptoHelper() {
super();
}
/**
* TODO: use new parseMessage() from PgpUtils to actually parse!
* @param message
* @return
*/
public boolean isEncrypted(Message message) {
String data = null;
try {
Part part = MimeUtility.findFirstPartByMimeType(message, "text/plain");
if (part == null) {
part = MimeUtility.findFirstPartByMimeType(message, "text/html");
}
if (part != null) {
data = MimeUtility.getTextFromPart(part);
}
} catch (MessagingException e) {
// guess not...
// TODO: maybe log this?
}
if (data == null) {
return false;
}
Matcher matcher = PGP_MESSAGE.matcher(data);
return matcher.matches();
}
public boolean isSigned(Message message) {
String data = null;
try {
Part part = MimeUtility.findFirstPartByMimeType(message, "text/plain");
if (part == null) {
part = MimeUtility.findFirstPartByMimeType(message, "text/html");
}
if (part != null) {
data = MimeUtility.getTextFromPart(part);
}
} catch (MessagingException e) {
// guess not...
// TODO: maybe log this?
}
if (data == null) {
return false;
}
Matcher matcher = PGP_SIGNED_MESSAGE.matcher(data);
return matcher.matches();
}
}