respond to unreadable OTR messages with error message. fixed #1021

This commit is contained in:
iNPUTmice 2015-03-21 16:07:17 +01:00
parent 825278971e
commit 99b2ef7e9d
5 changed files with 42 additions and 3 deletions

View File

@ -201,9 +201,21 @@ public class OtrEngine extends OtrCryptoEngineImpl implements OtrEngineHost {
}
@Override
public void messageFromAnotherInstanceReceived(SessionID id) {
Log.d(Config.LOGTAG,
"unreadable message received from " + id.getAccountID());
public void messageFromAnotherInstanceReceived(SessionID session) {
try {
Jid jid = Jid.fromSessionID(session);
Conversation conversation = mXmppConnectionService.find(account, jid);
String id = conversation == null ? null : conversation.getLastReceivedOtrMessageId();
if (id != null) {
MessagePacket packet = mXmppConnectionService.getMessageGenerator().generateOtrError(jid,id);
packet.setFrom(account.getJid());
mXmppConnectionService.sendMessagePacket(account,packet);
Log.d(Config.LOGTAG,packet.toString());
Log.d(Config.LOGTAG,account.getJid().toBareJid().toString()+": unreadable OTR message in "+conversation.getName());
}
} catch (InvalidJidException e) {
return;
}
}
@Override

View File

@ -78,6 +78,7 @@ public class Conversation extends AbstractEntity implements Blockable {
private boolean messagesLeftOnServer = true;
private ChatState mOutgoingChatState = Config.DEFAULT_CHATSTATE;
private ChatState mIncomingChatState = Config.DEFAULT_CHATSTATE;
private String mLastReceivedOtrMessageId = null;
public boolean hasMessagesLeftOnServer() {
return messagesLeftOnServer;
@ -234,6 +235,14 @@ public class Conversation extends AbstractEntity implements Blockable {
return getContact().getBlockedJid();
}
public String getLastReceivedOtrMessageId() {
return this.mLastReceivedOtrMessageId;
}
public void setLastReceivedOtrMessageId(String id) {
this.mLastReceivedOtrMessageId = id;
}
public interface OnMessageFound {
public void onMessageFound(final Message message);

View File

@ -171,4 +171,17 @@ public class MessageGenerator extends AbstractGenerator {
received.setAttribute("id", originalMessage.getId());
return receivedPacket;
}
public MessagePacket generateOtrError(Jid to, String id) {
MessagePacket packet = new MessagePacket();
packet.setType(MessagePacket.TYPE_ERROR);
packet.setAttribute("id",id);
packet.setTo(to);
Element error = packet.addChild("error");
error.setAttribute("code","406");
error.setAttribute("type","modify");
error.addChild("not-acceptable","urn:ietf:params:xml:ns:xmpp-stanzas");
error.addChild("text").setContent("unreadable OTR message received");
return packet;
}
}

View File

@ -118,6 +118,7 @@ public class MessageParser extends AbstractParser implements
}
}
try {
conversation.setLastReceivedOtrMessageId(packet.getId());
Session otrSession = conversation.getOtrSession();
SessionStatus before = otrSession.getSessionStatus();
body = otrSession.transformReceiving(body);
@ -145,6 +146,7 @@ public class MessageParser extends AbstractParser implements
finishedMessage.markable = isMarkable(packet);
finishedMessage.setCounterpart(from);
extractChatState(conversation, packet);
conversation.setLastReceivedOtrMessageId(null);
return finishedMessage;
} catch (Exception e) {
conversation.resetOtrSession();

View File

@ -39,6 +39,9 @@ public class MessagePacket extends AbstractStanza {
break;
case TYPE_NORMAL:
break;
case TYPE_ERROR:
this.setAttribute("type","error");
break;
default:
this.setAttribute("type", "chat");
break;