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 @Override
public void messageFromAnotherInstanceReceived(SessionID id) { public void messageFromAnotherInstanceReceived(SessionID session) {
Log.d(Config.LOGTAG, try {
"unreadable message received from " + id.getAccountID()); 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 @Override

View file

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

View file

@ -171,4 +171,17 @@ public class MessageGenerator extends AbstractGenerator {
received.setAttribute("id", originalMessage.getId()); received.setAttribute("id", originalMessage.getId());
return receivedPacket; 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 { try {
conversation.setLastReceivedOtrMessageId(packet.getId());
Session otrSession = conversation.getOtrSession(); Session otrSession = conversation.getOtrSession();
SessionStatus before = otrSession.getSessionStatus(); SessionStatus before = otrSession.getSessionStatus();
body = otrSession.transformReceiving(body); body = otrSession.transformReceiving(body);
@ -145,6 +146,7 @@ public class MessageParser extends AbstractParser implements
finishedMessage.markable = isMarkable(packet); finishedMessage.markable = isMarkable(packet);
finishedMessage.setCounterpart(from); finishedMessage.setCounterpart(from);
extractChatState(conversation, packet); extractChatState(conversation, packet);
conversation.setLastReceivedOtrMessageId(null);
return finishedMessage; return finishedMessage;
} catch (Exception e) { } catch (Exception e) {
conversation.resetOtrSession(); conversation.resetOtrSession();

View file

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