MessageAdapter: code reformat

This commit is contained in:
Daniel Gultsch 2017-11-21 13:26:45 +01:00
parent 702d6cf2a5
commit 79ea0713bb
1 changed files with 37 additions and 38 deletions

View File

@ -45,7 +45,7 @@ import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
public class MessageParser extends AbstractParser implements OnMessagePacketReceived { public class MessageParser extends AbstractParser implements OnMessagePacketReceived {
private static final List<String> CLIENTS_SENDING_HTML_IN_OTR = Arrays.asList("Pidgin","Adium","Trillian"); private static final List<String> CLIENTS_SENDING_HTML_IN_OTR = Arrays.asList("Pidgin", "Adium", "Trillian");
public MessageParser(XmppConnectionService service) { public MessageParser(XmppConnectionService service) {
super(service); super(service);
@ -90,7 +90,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
conversation.endOtrIfNeeded(); conversation.endOtrIfNeeded();
} }
if (!conversation.hasValidOtrSession()) { if (!conversation.hasValidOtrSession()) {
conversation.startOtrSession(presence,false); conversation.startOtrSession(presence, false);
} else { } else {
String foreignPresence = conversation.getOtrSession().getSessionID().getUserID(); String foreignPresence = conversation.getOtrSession().getSessionID().getUserID();
if (!foreignPresence.equals(presence)) { if (!foreignPresence.equals(presence)) {
@ -119,9 +119,9 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
return null; return null;
} }
if (clientMightSendHtml(conversation.getAccount(), from)) { if (clientMightSendHtml(conversation.getAccount(), from)) {
Log.d(Config.LOGTAG,conversation.getAccount().getJid().toBareJid()+": received OTR message from bad behaving client. escaping HTML…"); Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid() + ": received OTR message from bad behaving client. escaping HTML…");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
body = Html.fromHtml(body,Html.FROM_HTML_MODE_LEGACY).toString(); body = Html.fromHtml(body, Html.FROM_HTML_MODE_LEGACY).toString();
} else { } else {
body = Html.fromHtml(body).toString(); body = Html.fromHtml(body).toString();
} }
@ -153,7 +153,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
} }
private static boolean hasIdentityKnowForSendingHtml(List<ServiceDiscoveryResult.Identity> identities) { private static boolean hasIdentityKnowForSendingHtml(List<ServiceDiscoveryResult.Identity> identities) {
for(ServiceDiscoveryResult.Identity identity : identities) { for (ServiceDiscoveryResult.Identity identity : identities) {
if (identity.getName() != null) { if (identity.getName() != null) {
if (CLIENTS_SENDING_HTML_IN_OTR.contains(identity.getName())) { if (CLIENTS_SENDING_HTML_IN_OTR.contains(identity.getName())) {
return true; return true;
@ -163,20 +163,20 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
return false; return false;
} }
private Message parseAxolotlChat(Element axolotlMessage, Jid from, Conversation conversation, int status) { private Message parseAxolotlChat(Element axolotlMessage, Jid from, Conversation conversation, int status) {
AxolotlService service = conversation.getAccount().getAxolotlService(); AxolotlService service = conversation.getAccount().getAxolotlService();
XmppAxolotlMessage xmppAxolotlMessage; XmppAxolotlMessage xmppAxolotlMessage;
try { try {
xmppAxolotlMessage = XmppAxolotlMessage.fromElement(axolotlMessage, from.toBareJid()); xmppAxolotlMessage = XmppAxolotlMessage.fromElement(axolotlMessage, from.toBareJid());
} catch (Exception e) { } catch (Exception e) {
Log.d(Config.LOGTAG,conversation.getAccount().getJid().toBareJid()+": invalid omemo message received "+e.getMessage()); Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid() + ": invalid omemo message received " + e.getMessage());
return null; return null;
} }
XmppAxolotlMessage.XmppAxolotlPlaintextMessage plaintextMessage = service.processReceivingPayloadMessage(xmppAxolotlMessage); XmppAxolotlMessage.XmppAxolotlPlaintextMessage plaintextMessage = service.processReceivingPayloadMessage(xmppAxolotlMessage);
if(plaintextMessage != null) { if (plaintextMessage != null) {
Message finishedMessage = new Message(conversation, plaintextMessage.getPlaintext(), Message.ENCRYPTION_AXOLOTL, status); Message finishedMessage = new Message(conversation, plaintextMessage.getPlaintext(), Message.ENCRYPTION_AXOLOTL, status);
finishedMessage.setFingerprint(plaintextMessage.getFingerprint()); finishedMessage.setFingerprint(plaintextMessage.getFingerprint());
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(finishedMessage.getConversation().getAccount())+" Received Message with session fingerprint: "+plaintextMessage.getFingerprint()); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(finishedMessage.getConversation().getAccount()) + " Received Message with session fingerprint: " + plaintextMessage.getFingerprint());
return finishedMessage; return finishedMessage;
} else { } else {
return null; return null;
@ -187,6 +187,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
final Jid jid; final Jid jid;
final String password; final String password;
final Contact inviter; final Contact inviter;
Invite(Jid jid, String password, Contact inviter) { Invite(Jid jid, String password, Contact inviter) {
this.jid = jid; this.jid = jid;
this.password = password; this.password = password;
@ -216,14 +217,14 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
Element pw = x.findChild("password"); Element pw = x.findChild("password");
Jid from = invite.getAttributeAsJid("from"); Jid from = invite.getAttributeAsJid("from");
Contact contact = from == null ? null : account.getRoster().getContact(from); Contact contact = from == null ? null : account.getRoster().getContact(from);
return new Invite(message.getAttributeAsJid("from"), pw != null ? pw.getContent(): null, contact); return new Invite(message.getAttributeAsJid("from"), pw != null ? pw.getContent() : null, contact);
} }
} else { } else {
x = message.findChild("x","jabber:x:conference"); x = message.findChild("x", "jabber:x:conference");
if (x != null) { if (x != null) {
Jid from = message.getAttributeAsJid("from"); Jid from = message.getAttributeAsJid("from");
Contact contact = from == null ? null : account.getRoster().getContact(from); Contact contact = from == null ? null : account.getRoster().getContact(from);
return new Invite(x.getAttributeAsJid("jid"),x.getAttribute("password"),contact); return new Invite(x.getAttributeAsJid("jid"), x.getAttribute("password"), contact);
} }
} }
return null; return null;
@ -244,7 +245,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
} }
private static String extractStanzaId(Element packet, Jid by) { private static String extractStanzaId(Element packet, Jid by) {
for(Element child : packet.getChildren()) { for (Element child : packet.getChildren()) {
if (child.getName().equals("stanza-id") if (child.getName().equals("stanza-id")
&& Namespace.STANZA_IDS.equals(child.getNamespace()) && Namespace.STANZA_IDS.equals(child.getNamespace())
&& by.equals(child.getAttributeAsJid("by"))) { && by.equals(child.getAttributeAsJid("by"))) {
@ -295,7 +296,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
Element item = items.findChild("item"); Element item = items.findChild("item");
Set<Integer> deviceIds = mXmppConnectionService.getIqParser().deviceIds(item); Set<Integer> deviceIds = mXmppConnectionService.getIqParser().deviceIds(item);
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Received PEP device list ("+deviceIds+") update from "+ from + ", processing..."); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received PEP device list (" + deviceIds + ") update from " + from + ", processing...");
AxolotlService axolotlService = account.getAxolotlService(); AxolotlService axolotlService = account.getAxolotlService();
axolotlService.registerDevices(from, deviceIds); axolotlService.registerDevices(from, deviceIds);
mXmppConnectionService.updateAccountUi(); mXmppConnectionService.updateAccountUi();
@ -334,11 +335,11 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
String serverMsgId = null; String serverMsgId = null;
final Element fin = original.findChild("fin", Namespace.MAM_LEGACY); final Element fin = original.findChild("fin", Namespace.MAM_LEGACY);
if (fin != null) { if (fin != null) {
mXmppConnectionService.getMessageArchiveService().processFinLegacy(fin,original.getFrom()); mXmppConnectionService.getMessageArchiveService().processFinLegacy(fin, original.getFrom());
return; return;
} }
final boolean mamLegacy = original.hasChild("result", Namespace.MAM_LEGACY); final boolean mamLegacy = original.hasChild("result", Namespace.MAM_LEGACY);
final Element result = original.findChild("result",mamLegacy ? Namespace.MAM_LEGACY : Namespace.MAM); final Element result = original.findChild("result", mamLegacy ? Namespace.MAM_LEGACY : Namespace.MAM);
final MessageArchiveService.Query query = result == null ? null : mXmppConnectionService.getMessageArchiveService().findQuery(result.getAttribute("queryid")); final MessageArchiveService.Query query = result == null ? null : mXmppConnectionService.getMessageArchiveService().findQuery(result.getAttribute("queryid"));
if (query != null && query.validFrom(original.getFrom())) { if (query != null && query.validFrom(original.getFrom())) {
Pair<MessagePacket, Long> f = original.getForwardedMessagePacket("result", mamLegacy ? Namespace.MAM_LEGACY : Namespace.MAM); Pair<MessagePacket, Long> f = original.getForwardedMessagePacket("result", mamLegacy ? Namespace.MAM_LEGACY : Namespace.MAM);
@ -351,7 +352,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
serverMsgId = result.getAttribute("id"); serverMsgId = result.getAttribute("id");
query.incrementMessageCount(); query.incrementMessageCount();
} else if (query != null) { } else if (query != null) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": received mam result from invalid sender"); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": received mam result from invalid sender");
return; return;
} else if (original.fromServer(account)) { } else if (original.fromServer(account)) {
Pair<MessagePacket, Long> f; Pair<MessagePacket, Long> f;
@ -370,7 +371,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
} }
if (timestamp == null) { if (timestamp == null) {
timestamp = AbstractParser.parseTimestamp(original,AbstractParser.parseTimestamp(packet)); timestamp = AbstractParser.parseTimestamp(original, AbstractParser.parseTimestamp(packet));
} }
final String body = packet.getBody(); final String body = packet.getBody();
final Element mucUserElement = packet.findChild("x", "http://jabber.org/protocol/muc#user"); final Element mucUserElement = packet.findChild("x", "http://jabber.org/protocol/muc#user");
@ -384,7 +385,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
final Jid counterpart; final Jid counterpart;
final Jid to = packet.getTo(); final Jid to = packet.getTo();
final Jid from = packet.getFrom(); final Jid from = packet.getFrom();
final Element originId = packet.findChild("origin-id",Namespace.STANZA_IDS); final Element originId = packet.findChild("origin-id", Namespace.STANZA_IDS);
final String remoteMsgId; final String remoteMsgId;
if (originId != null && originId.getAttribute("id") != null) { if (originId != null && originId.getAttribute("id") != null) {
remoteMsgId = originId.getAttribute("id"); remoteMsgId = originId.getAttribute("id");
@ -394,12 +395,12 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
boolean notify = false; boolean notify = false;
if (from == null) { if (from == null) {
Log.d(Config.LOGTAG,"no from in: "+packet.toString()); Log.d(Config.LOGTAG, "no from in: " + packet.toString());
return; return;
} }
boolean isTypeGroupChat = packet.getType() == MessagePacket.TYPE_GROUPCHAT; boolean isTypeGroupChat = packet.getType() == MessagePacket.TYPE_GROUPCHAT;
boolean isProperlyAddressed = (to != null ) && (!to.isBareJid() || account.countPresences() == 0); boolean isProperlyAddressed = (to != null) && (!to.isBareJid() || account.countPresences() == 0);
boolean isMucStatusMessage = from.isBareJid() && mucUserElement != null && mucUserElement.hasChild("status"); boolean isMucStatusMessage = from.isBareJid() && mucUserElement != null && mucUserElement.hasChild("status");
if (packet.fromAccount(account)) { if (packet.fromAccount(account)) {
status = Message.STATUS_SEND; status = Message.STATUS_SEND;
@ -451,7 +452,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
return; return;
} }
} else { } else {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": ignoring OTR message from "+from+" isForwarded="+Boolean.toString(isForwarded)+", isProperlyAddressed="+Boolean.valueOf(isProperlyAddressed)); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": ignoring OTR message from " + from + " isForwarded=" + Boolean.toString(isForwarded) + ", isProperlyAddressed=" + Boolean.valueOf(isProperlyAddressed));
message = new Message(conversation, body, Message.ENCRYPTION_NONE, status); message = new Message(conversation, body, Message.ENCRYPTION_NONE, status);
} }
} else if (pgpEncrypted != null && Config.supportOpenPgp()) { } else if (pgpEncrypted != null && Config.supportOpenPgp()) {
@ -558,22 +559,22 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
} }
return; return;
} else { } else {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": received message correction but verification didn't check out"); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": received message correction but verification didn't check out");
} }
} }
} }
long deletionDate = mXmppConnectionService.getAutomaticMessageDeletionDate(); long deletionDate = mXmppConnectionService.getAutomaticMessageDeletionDate();
if (deletionDate != 0 && message.getTimeSent() < deletionDate) { if (deletionDate != 0 && message.getTimeSent() < deletionDate) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": skipping message from "+message.getCounterpart().toString()+" because it was sent prior to our deletion date"); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": skipping message from " + message.getCounterpart().toString() + " because it was sent prior to our deletion date");
return; return;
} }
boolean checkForDuplicates = (isTypeGroupChat && packet.hasChild("delay","urn:xmpp:delay")) boolean checkForDuplicates = (isTypeGroupChat && packet.hasChild("delay", "urn:xmpp:delay"))
|| message.getType() == Message.TYPE_PRIVATE || message.getType() == Message.TYPE_PRIVATE
|| message.getServerMsgId() != null; || message.getServerMsgId() != null;
if (checkForDuplicates && conversation.hasDuplicateMessage(message)) { if (checkForDuplicates && conversation.hasDuplicateMessage(message)) {
Log.d(Config.LOGTAG,"skipping duplicate message from "+message.getCounterpart().toString()+" "+message.getBody()); Log.d(Config.LOGTAG, "skipping duplicate message from " + message.getCounterpart().toString() + " " + message.getBody());
return; return;
} }
@ -632,7 +633,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
mXmppConnectionService.getNotificationService().push(message); mXmppConnectionService.getNotificationService().push(message);
} }
} }
} else if (!packet.hasChild("body")){ //no body } else if (!packet.hasChild("body")) { //no body
final Conversation conversation = mXmppConnectionService.find(account, from.toBareJid()); final Conversation conversation = mXmppConnectionService.find(account, from.toBareJid());
if (isTypeGroupChat) { if (isTypeGroupChat) {
if (packet.hasChild("subject")) { if (packet.hasChild("subject")) {
@ -664,12 +665,12 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
//ignored //ignored
} }
} else if ("item".equals(child.getName())) { } else if ("item".equals(child.getName())) {
MucOptions.User user = AbstractParser.parseItem(conversation,child); MucOptions.User user = AbstractParser.parseItem(conversation, child);
Log.d(Config.LOGTAG,account.getJid()+": changing affiliation for " Log.d(Config.LOGTAG, account.getJid() + ": changing affiliation for "
+user.getRealJid()+" to "+user.getAffiliation()+" in " + user.getRealJid() + " to " + user.getAffiliation() + " in "
+conversation.getJid().toBareJid()); + conversation.getJid().toBareJid());
if (!user.realJidMatchesAccount()) { if (!user.realJidMatchesAccount()) {
boolean isNew =conversation.getMucOptions().updateUser(user); boolean isNew = conversation.getMucOptions().updateUser(user);
mXmppConnectionService.getAvatarService().clear(conversation); mXmppConnectionService.getAvatarService().clear(conversation);
mXmppConnectionService.updateMucRosterUi(); mXmppConnectionService.updateMucRosterUi();
mXmppConnectionService.updateConversationUi(); mXmppConnectionService.updateConversationUi();
@ -677,7 +678,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
Jid jid = user.getRealJid(); Jid jid = user.getRealJid();
List<Jid> cryptoTargets = conversation.getAcceptedCryptoTargets(); List<Jid> cryptoTargets = conversation.getAcceptedCryptoTargets();
if (cryptoTargets.remove(user.getRealJid())) { if (cryptoTargets.remove(user.getRealJid())) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": removed "+jid+" from crypto targets of "+conversation.getName()); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": removed " + jid + " from crypto targets of " + conversation.getName());
conversation.setAcceptedCryptoTargets(cryptoTargets); conversation.setAcceptedCryptoTargets(cryptoTargets);
mXmppConnectionService.updateConversation(conversation); mXmppConnectionService.updateConversation(conversation);
} }
@ -690,8 +691,6 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
} }
} }
Element received = packet.findChild("received", "urn:xmpp:chat-markers:0"); Element received = packet.findChild("received", "urn:xmpp:chat-markers:0");
if (received == null) { if (received == null) {
received = packet.findChild("received", "urn:xmpp:receipts"); received = packet.findChild("received", "urn:xmpp:receipts");
@ -779,8 +778,8 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH); private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH);
private void activateGracePeriod(Account account) { private void activateGracePeriod(Account account) {
long duration = mXmppConnectionService.getLongPreference("grace_period_length",R.integer.grace_period) * 1000; long duration = mXmppConnectionService.getLongPreference("grace_period_length", R.integer.grace_period) * 1000;
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": activating grace period till "+TIME_FORMAT.format(new Date(System.currentTimeMillis() + duration))); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": activating grace period till " + TIME_FORMAT.format(new Date(System.currentTimeMillis() + duration)));
account.activateGracePeriod(duration); account.activateGracePeriod(duration);
} }
} }