Compare commits

...
Sign in to create a new pull request.

8 commits

Author SHA1 Message Date
Daniel Gultsch
3ce5c1f40f use a 20s timeout on socks5 connections 2015-05-18 04:34:34 +02:00
Daniel Gultsch
5480e49fa4 don't offer initiator his own candidates 2015-05-18 03:17:14 +02:00
Daniel Gultsch
64a097f8ed fixed npe in new message parser 2015-05-17 12:32:27 +02:00
Daniel Gultsch
97194f5e5c fixed muc mam. added a few security checks 2015-05-17 12:32:27 +02:00
Daniel Gultsch
3cbd336a51 fixed read/unread markers 2015-05-17 12:32:27 +02:00
Daniel Gultsch
136aa85daa added mam stuff to new message parser 2015-05-17 12:32:27 +02:00
Daniel Gultsch
7fa77091a7 rewrote parser code. mam id and possible other stuff still missing. also massivly untested 2015-05-17 12:32:27 +02:00
Daniel Gultsch
01413b552e provide helper function for getting the content of a child directly 2015-05-17 12:32:27 +02:00
13 changed files with 319 additions and 540 deletions

View file

@ -11,6 +11,7 @@ public final class Config {
public static final int PING_MAX_INTERVAL = 300;
public static final int PING_MIN_INTERVAL = 30;
public static final int PING_TIMEOUT = 10;
public static final int SOCKET_TIMEOUT = 20;
public static final int CONNECT_TIMEOUT = 90;
public static final int CARBON_GRACE_PERIOD = 60;
public static final int MINI_GRACE_PERIOD = 750;

View file

@ -75,12 +75,7 @@ public class Bookmark extends Element implements ListItem {
}
public String getNick() {
Element nick = this.findChild("nick");
if (nick != null) {
return nick.getContent();
} else {
return null;
}
return this.findChildContent("nick");
}
public void setNick(String nick) {
@ -96,12 +91,7 @@ public class Bookmark extends Element implements ListItem {
}
public String getPassword() {
Element password = this.findChild("password");
if (password != null) {
return password.getContent();
} else {
return null;
}
return this.findChildContent("password");
}
public void setPassword(String password) {

View file

@ -328,7 +328,7 @@ public class Message extends AbstractEntity {
return this.remoteMsgId == null
&& this.counterpart.equals(message.getCounterpart())
&& this.body.equals(message.getBody())
&& Math.abs(this.getTimeSent() - message.getTimeSent()) < Config.PING_TIMEOUT * 500;
&& Math.abs(this.getTimeSent() - message.getTimeSent()) < Config.MESSAGE_MERGE_WINDOW * 1000;
}
}

View file

@ -11,6 +11,7 @@ import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
public abstract class AbstractParser {
@ -20,22 +21,23 @@ public abstract class AbstractParser {
this.mXmppConnectionService = service;
}
public static Long getTimestamp(Element element, Long defaultValue) {
Element delay = element.findChild("delay","urn:xmpp:delay");
if (delay != null) {
String stamp = delay.getAttribute("stamp");
if (stamp != null) {
try {
return AbstractParser.parseTimestamp(delay.getAttribute("stamp")).getTime();
} catch (ParseException e) {
return defaultValue;
}
}
}
return defaultValue;
}
protected long getTimestamp(Element packet) {
long now = System.currentTimeMillis();
Element delay = packet.findChild("delay");
if (delay == null) {
return now;
}
String stamp = delay.getAttribute("stamp");
if (stamp == null) {
return now;
}
try {
long time = parseTimestamp(stamp).getTime();
return now < time ? now : time;
} catch (ParseException e) {
return now;
}
return getTimestamp(packet,System.currentTimeMillis());
}
public static Date parseTimestamp(String timestamp) throws ParseException {
@ -46,14 +48,11 @@ public abstract class AbstractParser {
return dateFormat.parse(timestamp);
}
protected void updateLastseen(final Element packet, final Account account,
final boolean presenceOverwrite) {
final Jid from = packet.getAttributeAsJid("from");
updateLastseen(packet, account, from, presenceOverwrite);
protected void updateLastseen(final Element packet, final Account account, final boolean presenceOverwrite) {
updateLastseen(packet, account, packet.getAttributeAsJid("from"), presenceOverwrite);
}
protected void updateLastseen(final Element packet, final Account account, final Jid from,
final boolean presenceOverwrite) {
protected void updateLastseen(final Element packet, final Account account, final Jid from, final boolean presenceOverwrite) {
final String presence = from == null || from.isBareJid() ? "" : from.getResourcepart();
final Contact contact = account.getRoster().getContact(from);
final long timestamp = getTimestamp(packet);
@ -70,10 +69,6 @@ public abstract class AbstractParser {
if (item == null) {
return null;
}
Element data = item.findChild("data", "urn:xmpp:avatar:data");
if (data == null) {
return null;
}
return data.getContent();
return item.findChildContent("data", "urn:xmpp:avatar:data");
}
}

View file

@ -1,8 +1,12 @@
package eu.siacs.conversations.parser;
import android.util.Log;
import android.util.Pair;
import net.java.otr4j.session.Session;
import net.java.otr4j.session.SessionStatus;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation;
@ -25,12 +29,12 @@ public class MessageParser extends AbstractParser implements
super(service);
}
private boolean extractChatState(Conversation conversation, final Element element) {
ChatState state = ChatState.parse(element);
private boolean extractChatState(Conversation conversation, final MessagePacket packet) {
ChatState state = ChatState.parse(packet);
if (state != null && conversation != null) {
final Account account = conversation.getAccount();
Jid from = element.getAttributeAsJid("from");
if (from != null && from.toBareJid().equals(account.getJid().toBareJid())) {
Jid from = packet.getFrom();
if (from.toBareJid().equals(account.getJid().toBareJid())) {
conversation.setOutgoingChatState(state);
return false;
} else {
@ -40,86 +44,27 @@ public class MessageParser extends AbstractParser implements
return false;
}
private Message parseChat(MessagePacket packet, Account account) {
final Jid jid = packet.getFrom();
if (jid == null) {
return null;
}
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, jid.toBareJid(), false);
String pgpBody = getPgpBody(packet);
Message finishedMessage;
if (pgpBody != null) {
finishedMessage = new Message(conversation,
pgpBody, Message.ENCRYPTION_PGP, Message.STATUS_RECEIVED);
} else {
finishedMessage = new Message(conversation,
packet.getBody(), Message.ENCRYPTION_NONE,
Message.STATUS_RECEIVED);
}
finishedMessage.setRemoteMsgId(packet.getId());
finishedMessage.markable = isMarkable(packet);
if (conversation.getMode() == Conversation.MODE_MULTI
&& !jid.isBareJid()) {
final Jid trueCounterpart = conversation.getMucOptions()
.getTrueCounterpart(jid.getResourcepart());
if (trueCounterpart != null) {
updateLastseen(packet, account, trueCounterpart, false);
}
finishedMessage.setType(Message.TYPE_PRIVATE);
finishedMessage.setTrueCounterpart(trueCounterpart);
if (conversation.hasDuplicateMessage(finishedMessage)) {
return null;
}
} else {
updateLastseen(packet, account, true);
}
finishedMessage.setCounterpart(jid);
finishedMessage.setTime(getTimestamp(packet));
extractChatState(conversation,packet);
return finishedMessage;
}
private Message parseOtrChat(MessagePacket packet, Account account) {
final Jid to = packet.getTo();
final Jid from = packet.getFrom();
if (to == null || from == null) {
return null;
}
boolean properlyAddressed = !to.isBareJid() || account.countPresences() == 1;
Conversation conversation = mXmppConnectionService
.findOrCreateConversation(account, from.toBareJid(), false);
private Message parseOtrChat(String body, Jid from, String id, Conversation conversation) {
String presence;
if (from.isBareJid()) {
presence = "";
} else {
presence = from.getResourcepart();
}
extractChatState(conversation, packet);
updateLastseen(packet, account, true);
String body = packet.getBody();
if (body.matches("^\\?OTRv\\d{1,2}\\?.*")) {
conversation.endOtrIfNeeded();
}
if (!conversation.hasValidOtrSession()) {
if (properlyAddressed) {
conversation.startOtrSession(presence,false);
} else {
return null;
}
conversation.startOtrSession(presence,false);
} else {
String foreignPresence = conversation.getOtrSession()
.getSessionID().getUserID();
String foreignPresence = conversation.getOtrSession().getSessionID().getUserID();
if (!foreignPresence.equals(presence)) {
conversation.endOtrIfNeeded();
if (properlyAddressed) {
conversation.startOtrSession(presence, false);
} else {
return null;
}
conversation.startOtrSession(presence, false);
}
}
try {
conversation.setLastReceivedOtrMessageId(packet.getId());
conversation.setLastReceivedOtrMessageId(id);
Session otrSession = conversation.getOtrSession();
SessionStatus before = otrSession.getSessionStatus();
body = otrSession.transformReceiving(body);
@ -140,12 +85,7 @@ public class MessageParser extends AbstractParser implements
conversation.setSymmetricKey(CryptoHelper.hexToBytes(key));
return null;
}
Message finishedMessage = new Message(conversation, body, Message.ENCRYPTION_OTR,
Message.STATUS_RECEIVED);
finishedMessage.setTime(getTimestamp(packet));
finishedMessage.setRemoteMsgId(packet.getId());
finishedMessage.markable = isMarkable(packet);
finishedMessage.setCounterpart(from);
Message finishedMessage = new Message(conversation, body, Message.ENCRYPTION_OTR, Message.STATUS_RECEIVED);
conversation.setLastReceivedOtrMessageId(null);
return finishedMessage;
} catch (Exception e) {
@ -154,293 +94,6 @@ public class MessageParser extends AbstractParser implements
}
}
private Message parseGroupchat(MessagePacket packet, Account account) {
int status;
final Jid from = packet.getFrom();
if (from == null) {
return null;
}
if (mXmppConnectionService.find(account.pendingConferenceLeaves,
account, from.toBareJid()) != null) {
return null;
}
Conversation conversation = mXmppConnectionService
.findOrCreateConversation(account, from.toBareJid(), true);
final Jid trueCounterpart = conversation.getMucOptions().getTrueCounterpart(from.getResourcepart());
if (trueCounterpart != null) {
updateLastseen(packet, account, trueCounterpart, false);
}
if (packet.hasChild("subject")) {
conversation.setHasMessagesLeftOnServer(true);
conversation.getMucOptions().setSubject(packet.findChild("subject").getContent());
mXmppConnectionService.updateConversationUi();
return null;
}
final Element x = packet.findChild("x", "http://jabber.org/protocol/muc#user");
if (from.isBareJid() && (x == null || !x.hasChild("status"))) {
return null;
} else if (from.isBareJid() && x.hasChild("status")) {
for(Element child : x.getChildren()) {
if (child.getName().equals("status")) {
String code = child.getAttribute("code");
if (code.contains(MucOptions.STATUS_CODE_ROOM_CONFIG_CHANGED)) {
mXmppConnectionService.fetchConferenceConfiguration(conversation);
}
}
}
return null;
}
if (from.getResourcepart().equals(conversation.getMucOptions().getActualNick())) {
if (mXmppConnectionService.markMessage(conversation,
packet.getId(), Message.STATUS_SEND_RECEIVED)) {
return null;
} else if (packet.getId() == null) {
Message message = conversation.findSentMessageWithBody(packet.getBody());
if (message != null) {
mXmppConnectionService.markMessage(message,Message.STATUS_SEND_RECEIVED);
return null;
} else {
status = Message.STATUS_SEND;
}
} else {
status = Message.STATUS_SEND;
}
} else {
status = Message.STATUS_RECEIVED;
}
String pgpBody = getPgpBody(packet);
Message finishedMessage;
if (pgpBody == null) {
finishedMessage = new Message(conversation,
packet.getBody(), Message.ENCRYPTION_NONE, status);
} else {
finishedMessage = new Message(conversation, pgpBody,
Message.ENCRYPTION_PGP, status);
}
finishedMessage.setRemoteMsgId(packet.getId());
finishedMessage.markable = isMarkable(packet);
finishedMessage.setCounterpart(from);
if (status == Message.STATUS_RECEIVED) {
finishedMessage.setTrueCounterpart(conversation.getMucOptions()
.getTrueCounterpart(from.getResourcepart()));
}
if (packet.hasChild("delay")
&& conversation.hasDuplicateMessage(finishedMessage)) {
return null;
}
finishedMessage.setTime(getTimestamp(packet));
return finishedMessage;
}
private Message parseCarbonMessage(final MessagePacket packet, final Account account) {
int status;
final Jid fullJid;
Element forwarded;
if (packet.hasChild("received", "urn:xmpp:carbons:2")) {
forwarded = packet.findChild("received", "urn:xmpp:carbons:2")
.findChild("forwarded", "urn:xmpp:forward:0");
status = Message.STATUS_RECEIVED;
} else if (packet.hasChild("sent", "urn:xmpp:carbons:2")) {
forwarded = packet.findChild("sent", "urn:xmpp:carbons:2")
.findChild("forwarded", "urn:xmpp:forward:0");
status = Message.STATUS_SEND;
} else {
return null;
}
if (forwarded == null) {
return null;
}
Element message = forwarded.findChild("message");
if (message == null) {
return null;
}
if (!message.hasChild("body")) {
if (status == Message.STATUS_RECEIVED
&& message.getAttribute("from") != null) {
parseNonMessage(message, account);
} else if (status == Message.STATUS_SEND
&& message.hasChild("displayed", "urn:xmpp:chat-markers:0")) {
final Jid to = message.getAttributeAsJid("to");
if (to != null) {
final Conversation conversation = mXmppConnectionService.find(
mXmppConnectionService.getConversations(), account,
to.toBareJid());
if (conversation != null) {
mXmppConnectionService.markRead(conversation);
}
}
}
return null;
}
if (status == Message.STATUS_RECEIVED) {
fullJid = message.getAttributeAsJid("from");
if (fullJid == null) {
return null;
} else {
updateLastseen(message, account, true);
}
} else {
fullJid = message.getAttributeAsJid("to");
if (fullJid == null) {
return null;
}
}
if (message.hasChild("x","http://jabber.org/protocol/muc#user")
&& "chat".equals(message.getAttribute("type"))) {
return null;
}
Conversation conversation = mXmppConnectionService
.findOrCreateConversation(account, fullJid.toBareJid(), false);
String pgpBody = getPgpBody(message);
Message finishedMessage;
if (pgpBody != null) {
finishedMessage = new Message(conversation, pgpBody,
Message.ENCRYPTION_PGP, status);
} else {
String body = message.findChild("body").getContent();
finishedMessage = new Message(conversation, body,
Message.ENCRYPTION_NONE, status);
}
extractChatState(conversation,message);
finishedMessage.setTime(getTimestamp(message));
finishedMessage.setRemoteMsgId(message.getAttribute("id"));
finishedMessage.markable = isMarkable(message);
finishedMessage.setCounterpart(fullJid);
if (conversation.getMode() == Conversation.MODE_MULTI
&& !fullJid.isBareJid()) {
finishedMessage.setType(Message.TYPE_PRIVATE);
finishedMessage.setTrueCounterpart(conversation.getMucOptions()
.getTrueCounterpart(fullJid.getResourcepart()));
if (conversation.hasDuplicateMessage(finishedMessage)) {
return null;
}
}
return finishedMessage;
}
private Message parseMamMessage(MessagePacket packet, final Account account) {
final Element result = packet.findChild("result","urn:xmpp:mam:0");
if (result == null ) {
return null;
}
final MessageArchiveService.Query query = this.mXmppConnectionService.getMessageArchiveService().findQuery(result.getAttribute("queryid"));
if (query!=null) {
query.incrementTotalCount();
}
final Element forwarded = result.findChild("forwarded","urn:xmpp:forward:0");
if (forwarded == null) {
return null;
}
final Element message = forwarded.findChild("message");
if (message == null) {
return null;
}
final Element body = message.findChild("body");
if (body == null || message.hasChild("private","urn:xmpp:carbons:2") || message.hasChild("no-copy","urn:xmpp:hints")) {
return null;
}
int encryption;
String content = getPgpBody(message);
if (content != null) {
encryption = Message.ENCRYPTION_PGP;
} else {
encryption = Message.ENCRYPTION_NONE;
content = body.getContent();
}
if (content == null) {
return null;
}
final long timestamp = getTimestamp(forwarded);
final Jid to = message.getAttributeAsJid("to");
final Jid from = message.getAttributeAsJid("from");
Jid counterpart;
int status;
Conversation conversation;
if (from!=null && to != null && from.toBareJid().equals(account.getJid().toBareJid())) {
status = Message.STATUS_SEND;
conversation = this.mXmppConnectionService.findOrCreateConversation(account,to.toBareJid(),false,query);
counterpart = to;
} else if (from !=null && to != null) {
status = Message.STATUS_RECEIVED;
conversation = this.mXmppConnectionService.findOrCreateConversation(account,from.toBareJid(),false,query);
counterpart = from;
} else {
return null;
}
Message finishedMessage = new Message(conversation,content,encryption,status);
finishedMessage.setTime(timestamp);
finishedMessage.setCounterpart(counterpart);
finishedMessage.setRemoteMsgId(message.getAttribute("id"));
finishedMessage.setServerMsgId(result.getAttribute("id"));
if (conversation.hasDuplicateMessage(finishedMessage)) {
return null;
}
if (query!=null) {
query.incrementMessageCount();
}
return finishedMessage;
}
private void parseError(final MessagePacket packet, final Account account) {
final Jid from = packet.getFrom();
mXmppConnectionService.markMessage(account, from.toBareJid(),
packet.getId(), Message.STATUS_SEND_FAILED);
}
private void parseNonMessage(Element packet, Account account) {
final Jid from = packet.getAttributeAsJid("from");
if (account.getJid().equals(from)) {
return;
}
if (extractChatState(from == null ? null : mXmppConnectionService.find(account,from), packet)) {
mXmppConnectionService.updateConversationUi();
}
Invite invite = extractInvite(packet);
if (invite != null && invite.jid != null) {
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, invite.jid, true);
if (!conversation.getMucOptions().online()) {
conversation.getMucOptions().setPassword(invite.password);
mXmppConnectionService.databaseBackend.updateConversation(conversation);
mXmppConnectionService.joinMuc(conversation);
mXmppConnectionService.updateConversationUi();
}
}
if (packet.hasChild("event", "http://jabber.org/protocol/pubsub#event")) {
Element event = packet.findChild("event",
"http://jabber.org/protocol/pubsub#event");
parseEvent(event, from, account);
} else if (from != null && packet.hasChild("displayed", "urn:xmpp:chat-markers:0")) {
String id = packet
.findChild("displayed", "urn:xmpp:chat-markers:0")
.getAttribute("id");
updateLastseen(packet, account, true);
final Message displayedMessage = mXmppConnectionService.markMessage(account, from.toBareJid(), id, Message.STATUS_SEND_DISPLAYED);
Message message = displayedMessage == null ? null :displayedMessage.prev();
while (message != null
&& message.getStatus() == Message.STATUS_SEND_RECEIVED
&& message.getTimeSent() < displayedMessage.getTimeSent()) {
mXmppConnectionService.markMessage(message, Message.STATUS_SEND_DISPLAYED);
message = message.prev();
}
} else if (from != null
&& packet.hasChild("received", "urn:xmpp:chat-markers:0")) {
String id = packet.findChild("received", "urn:xmpp:chat-markers:0")
.getAttribute("id");
updateLastseen(packet, account, false);
mXmppConnectionService.markMessage(account, from.toBareJid(),
id, Message.STATUS_SEND_RECEIVED);
} else if (from != null
&& packet.hasChild("received", "urn:xmpp:receipts")) {
String id = packet.findChild("received", "urn:xmpp:receipts")
.getAttribute("id");
updateLastseen(packet, account, false);
mXmppConnectionService.markMessage(account, from.toBareJid(),
id, Message.STATUS_SEND_RECEIVED);
}
}
private class Invite {
Jid jid;
String password;
@ -451,7 +104,7 @@ public class MessageParser extends AbstractParser implements
}
private Invite extractInvite(Element message) {
Element x = message.findChild("x","http://jabber.org/protocol/muc#user");
Element x = message.findChild("x", "http://jabber.org/protocol/muc#user");
if (x != null) {
Element invite = x.findChild("invite");
if (invite != null) {
@ -523,142 +176,230 @@ public class MessageParser extends AbstractParser implements
}
}
private String getPgpBody(Element message) {
Element child = message.findChild("x", "jabber:x:encrypted");
if (child == null) {
return null;
} else {
return child.getContent();
}
}
private boolean isMarkable(Element message) {
return message.hasChild("markable", "urn:xmpp:chat-markers:0");
}
@Override
public void onMessagePacketReceived(Account account, MessagePacket packet) {
Message message = null;
this.parseNick(packet, account);
if ((packet.getType() == MessagePacket.TYPE_CHAT || packet.getType() == MessagePacket.TYPE_NORMAL)) {
if ((packet.getBody() != null)
&& (packet.getBody().startsWith("?OTR"))) {
message = this.parseOtrChat(packet, account);
if (message != null) {
message.markUnread();
}
} else if (packet.hasChild("body") && extractInvite(packet) == null) {
message = this.parseChat(packet, account);
if (message != null) {
message.markUnread();
}
} else if (packet.hasChild("received", "urn:xmpp:carbons:2")
|| (packet.hasChild("sent", "urn:xmpp:carbons:2"))) {
message = this.parseCarbonMessage(packet, account);
if (message != null) {
if (message.getStatus() == Message.STATUS_SEND) {
account.activateGracePeriod();
mXmppConnectionService.markRead(message.getConversation());
} else {
message.markUnread();
}
}
} else if (packet.hasChild("result","urn:xmpp:mam:0")) {
message = parseMamMessage(packet, account);
if (message != null) {
Conversation conversation = message.getConversation();
conversation.add(message);
mXmppConnectionService.databaseBackend.createMessage(message);
}
public void onMessagePacketReceived(Account account, MessagePacket original) {
final MessagePacket packet;
Long timestamp = null;
final boolean isForwarded;
String serverMsgId = null;
final Element fin = original.findChild("fin", "urn:xmpp:mam:0");
if (fin != null) {
mXmppConnectionService.getMessageArchiveService().processFin(fin,original.getFrom());
return;
}
final Element result = original.findChild("result","urn:xmpp:mam:0");
final MessageArchiveService.Query query = result == null ? null : mXmppConnectionService.getMessageArchiveService().findQuery(result.getAttribute("queryid"));
if (query != null && query.validFrom(original.getFrom())) {
Pair<MessagePacket, Long> f = original.getForwardedMessagePacket("result", "urn:xmpp:mam:0");
if (f == null) {
return;
} else if (packet.hasChild("fin","urn:xmpp:mam:0")) {
Element fin = packet.findChild("fin","urn:xmpp:mam:0");
mXmppConnectionService.getMessageArchiveService().processFin(fin);
} else {
parseNonMessage(packet, account);
}
} else if (packet.getType() == MessagePacket.TYPE_GROUPCHAT) {
message = this.parseGroupchat(packet, account);
if (message != null) {
if (message.getStatus() == Message.STATUS_RECEIVED) {
message.markUnread();
timestamp = f.second;
packet = f.first;
isForwarded = true;
serverMsgId = result.getAttribute("id");
query.incrementTotalCount();
} else if (query != null) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": received mam result from invalid sender");
return;
} else if (original.fromServer(account)) {
Pair<MessagePacket, Long> f;
f = original.getForwardedMessagePacket("received", "urn:xmpp:carbons:2");
f = f == null ? original.getForwardedMessagePacket("sent", "urn:xmpp:carbons:2") : f;
packet = f != null ? f.first : original;
timestamp = f != null ? f.second : null;
isForwarded = f != null;
} else {
packet = original;
isForwarded = false;
}
if (timestamp == null) {
timestamp = AbstractParser.getTimestamp(packet, System.currentTimeMillis());
}
final String body = packet.getBody();
final String encrypted = packet.findChildContent("x", "jabber:x:encrypted");
int status;
final Jid counterpart;
final Jid to = packet.getTo();
final Jid from = packet.getFrom();
final String remoteMsgId = packet.getId();
boolean isTypeGroupChat = packet.getType() == MessagePacket.TYPE_GROUPCHAT;
boolean properlyAddressed = !to.isBareJid() || account.countPresences() == 1;
if (packet.fromAccount(account)) {
status = Message.STATUS_SEND;
counterpart = to;
} else {
status = Message.STATUS_RECEIVED;
counterpart = from;
}
if (from == null || to == null) {
Log.d(Config.LOGTAG,"no to or from in: "+packet.toString());
return;
}
Invite invite = extractInvite(packet);
if (invite != null && invite.jid != null) {
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, invite.jid, true,query);
if (!conversation.getMucOptions().online()) {
conversation.getMucOptions().setPassword(invite.password);
mXmppConnectionService.databaseBackend.updateConversation(conversation);
mXmppConnectionService.joinMuc(conversation);
mXmppConnectionService.updateConversationUi();
}
return;
}
if (extractChatState(mXmppConnectionService.find(account,from), packet)) {
mXmppConnectionService.updateConversationUi();
}
if (body != null || encrypted != null) {
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, counterpart.toBareJid(), isTypeGroupChat);
if (isTypeGroupChat) {
if (counterpart.getResourcepart().equals(conversation.getMucOptions().getActualNick())) {
status = Message.STATUS_SEND;
if (mXmppConnectionService.markMessage(conversation, remoteMsgId, Message.STATUS_SEND_RECEIVED)) {
return;
} else if (remoteMsgId == null) {
Message message = conversation.findSentMessageWithBody(packet.getBody());
if (message != null) {
mXmppConnectionService.markMessage(message, Message.STATUS_SEND_RECEIVED);
return;
}
}
} else {
mXmppConnectionService.markRead(message.getConversation());
account.activateGracePeriod();
status = Message.STATUS_RECEIVED;
}
}
} else if (packet.getType() == MessagePacket.TYPE_ERROR) {
this.parseError(packet, account);
return;
} else if (packet.getType() == MessagePacket.TYPE_HEADLINE) {
this.parseHeadline(packet, account);
return;
}
if ((message == null) || (message.getBody() == null)) {
return;
}
if ((mXmppConnectionService.confirmMessages())
&& ((packet.getId() != null))) {
if (packet.hasChild("markable", "urn:xmpp:chat-markers:0")) {
MessagePacket receipt = mXmppConnectionService
.getMessageGenerator().received(account, packet,
"urn:xmpp:chat-markers:0");
mXmppConnectionService.sendMessagePacket(account, receipt);
Message message;
if (body != null && body.startsWith("?OTR")) {
if (!isForwarded && !isTypeGroupChat && properlyAddressed) {
message = parseOtrChat(body, from, remoteMsgId, conversation);
if (message == null) {
return;
}
} else {
message = new Message(conversation, body, Message.ENCRYPTION_NONE, status);
}
} else if (encrypted != null) {
message = new Message(conversation, encrypted, Message.ENCRYPTION_PGP, status);
} else {
message = new Message(conversation, body, Message.ENCRYPTION_NONE, status);
}
if (packet.hasChild("request", "urn:xmpp:receipts")) {
MessagePacket receipt = mXmppConnectionService
.getMessageGenerator().received(account, packet,
"urn:xmpp:receipts");
mXmppConnectionService.sendMessagePacket(account, receipt);
message.setCounterpart(counterpart);
message.setRemoteMsgId(remoteMsgId);
message.setServerMsgId(serverMsgId);
message.setTime(timestamp);
message.markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
if (conversation.getMode() == Conversation.MODE_MULTI) {
message.setTrueCounterpart(conversation.getMucOptions().getTrueCounterpart(counterpart.getResourcepart()));
if (!isTypeGroupChat) {
message.setType(Message.TYPE_PRIVATE);
}
}
}
Conversation conversation = message.getConversation();
conversation.add(message);
if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().advancedStreamFeaturesLoaded()) {
if (conversation.setLastMessageTransmitted(System.currentTimeMillis())) {
mXmppConnectionService.updateConversation(conversation);
updateLastseen(packet,account,true);
boolean checkForDuplicates = serverMsgId != null || (isTypeGroupChat && packet.hasChild("delay","urn:xmpp:delay"));
if (checkForDuplicates && conversation.hasDuplicateMessage(message)) {
Log.d(Config.LOGTAG,"skipping duplicate message from "+message.getCounterpart().toString()+" "+message.getBody());
return;
}
if (query != null) {
query.incrementMessageCount();
}
conversation.add(message);
if (serverMsgId == null) {
if (status == Message.STATUS_SEND) {
mXmppConnectionService.markRead(conversation);
account.activateGracePeriod();
} else {
message.markUnread();
}
mXmppConnectionService.updateConversationUi();
}
}
if (message.getStatus() == Message.STATUS_RECEIVED
&& conversation.getOtrSession() != null
&& !conversation.getOtrSession().getSessionID().getUserID()
.equals(message.getCounterpart().getResourcepart())) {
conversation.endOtrIfNeeded();
}
if (mXmppConnectionService.confirmMessages() && remoteMsgId != null && !isForwarded) {
if (packet.hasChild("markable", "urn:xmpp:chat-markers:0")) {
MessagePacket receipt = mXmppConnectionService
.getMessageGenerator().received(account, packet, "urn:xmpp:chat-markers:0");
mXmppConnectionService.sendMessagePacket(account, receipt);
}
if (packet.hasChild("request", "urn:xmpp:receipts")) {
MessagePacket receipt = mXmppConnectionService
.getMessageGenerator().received(account, packet, "urn:xmpp:receipts");
mXmppConnectionService.sendMessagePacket(account, receipt);
}
}
if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().advancedStreamFeaturesLoaded()) {
if (conversation.setLastMessageTransmitted(System.currentTimeMillis())) {
mXmppConnectionService.updateConversation(conversation);
}
}
if (packet.getType() != MessagePacket.TYPE_ERROR) {
if (message.getEncryption() == Message.ENCRYPTION_NONE
|| mXmppConnectionService.saveEncryptedMessages()) {
if (message.getStatus() == Message.STATUS_RECEIVED
&& conversation.getOtrSession() != null
&& !conversation.getOtrSession().getSessionID().getUserID()
.equals(message.getCounterpart().getResourcepart())) {
conversation.endOtrIfNeeded();
}
if (message.getEncryption() == Message.ENCRYPTION_NONE || mXmppConnectionService.saveEncryptedMessages()) {
mXmppConnectionService.databaseBackend.createMessage(message);
}
}
final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager();
if (message.trusted() && message.bodyContainsDownloadable() && manager.getAutoAcceptFileSize() > 0) {
manager.createNewConnection(message);
} else if (!message.isRead()) {
mXmppConnectionService.getNotificationService().push(message);
}
mXmppConnectionService.updateConversationUi();
}
private void parseHeadline(MessagePacket packet, Account account) {
if (packet.hasChild("event", "http://jabber.org/protocol/pubsub#event")) {
Element event = packet.findChild("event",
"http://jabber.org/protocol/pubsub#event");
parseEvent(event, packet.getFrom(), account);
}
}
private void parseNick(MessagePacket packet, Account account) {
Element nick = packet.findChild("nick",
"http://jabber.org/protocol/nick");
if (nick != null) {
if (packet.getFrom() != null) {
Contact contact = account.getRoster().getContact(
packet.getFrom());
contact.setPresenceName(nick.getContent());
final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager();
if (message.trusted() && message.bodyContainsDownloadable() && manager.getAutoAcceptFileSize() > 0) {
manager.createNewConnection(message);
} else if (!message.isRead()) {
mXmppConnectionService.getNotificationService().push(message);
}
} else { //no body
if (packet.hasChild("subject") && isTypeGroupChat) {
Conversation conversation = mXmppConnectionService.find(account, from.toBareJid());
if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
conversation.setHasMessagesLeftOnServer(true);
conversation.getMucOptions().setSubject(packet.findChildContent("subject"));
mXmppConnectionService.updateConversationUi();
return;
}
}
}
Element received = packet.findChild("received", "urn:xmpp:chat-markers:0");
if (received == null) {
received = packet.findChild("received", "urn:xmpp:receipts");
}
if (received != null && !packet.fromAccount(account)) {
mXmppConnectionService.markMessage(account, from.toBareJid(), received.getAttribute("id"), Message.STATUS_SEND_RECEIVED);
}
Element displayed = packet.findChild("displayed", "urn:xmpp:chat-markers:0");
if (displayed != null) {
if (packet.fromAccount(account)) {
Conversation conversation = mXmppConnectionService.find(account,counterpart.toBareJid());
if (conversation != null) {
mXmppConnectionService.markRead(conversation);
}
} else {
updateLastseen(packet, account, true);
final Message displayedMessage = mXmppConnectionService.markMessage(account, from.toBareJid(), displayed.getAttribute("id"), Message.STATUS_SEND_DISPLAYED);
Message message = displayedMessage == null ? null : displayedMessage.prev();
while (message != null
&& message.getStatus() == Message.STATUS_SEND_RECEIVED
&& message.getTimeSent() < displayedMessage.getTimeSent()) {
mXmppConnectionService.markMessage(message, Message.STATUS_SEND_DISPLAYED);
message = message.prev();
}
}
}
Element event = packet.findChild("event", "http://jabber.org/protocol/pubsub#event");
if (event != null) {
parseEvent(event, from, account);
}
String nick = packet.findChildContent("nick", "http://jabber.org/protocol/nick");
if (nick != null) {
Contact contact = account.getRoster().getContact(from);
contact.setPresenceName(nick);
}
}
}
}

View file

@ -166,12 +166,12 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
}
}
public void processFin(Element fin) {
public void processFin(Element fin, Jid from) {
if (fin == null) {
return;
}
Query query = findQuery(fin.getAttribute("queryid"));
if (query == null) {
if (query == null || !query.validFrom(from)) {
return;
}
boolean complete = fin.getAttributeAsBoolean("complete");
@ -336,6 +336,14 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
return this.messageCount;
}
public boolean validFrom(Jid from) {
if (muc()) {
return getWith().equals(from);
} else {
return (from == null) || account.getJid().toBareJid().equals(from.toBareJid());
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();

View file

@ -1994,8 +1994,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
if (packet.getType() == IqPacket.TYPE.RESULT) {
Element vCard = packet.findChild("vCard","vcard-temp");
Element photo = vCard != null ? vCard.findChild("PHOTO") : null;
Element binval = photo != null ? photo.findChild("BINVAL") : null;
String image = binval != null ? binval.getContent() : null;
String image = photo != null ? photo.findChildContent("BINVAL") : null;
if (image != null) {
avatar.image = image;
if (getFileBackend().save(avatar)) {

View file

@ -57,6 +57,11 @@ public class Element {
return null;
}
public String findChildContent(String name) {
Element element = findChild(name);
return element == null ? null : element.getContent();
}
public Element findChild(String name, String xmlns) {
for (Element child : this.children) {
if (child.getName().equals(name)
@ -67,6 +72,11 @@ public class Element {
return null;
}
public String findChildContent(String name, String xmlns) {
Element element = findChild(name,xmlns);
return element == null ? null : element.getContent();
}
public boolean hasChild(final String name) {
return findChild(name) != null;
}

View file

@ -213,7 +213,7 @@ public class JingleConnection implements Downloadable {
@Override
public void onPrimaryCandidateFound(boolean success,
final JingleCandidate candidate) {
final JingleCandidate candidate) {
if (success) {
final JingleSocks5Transport socksConnection = new JingleSocks5Transport(
JingleConnection.this, candidate);
@ -399,7 +399,9 @@ public class JingleConnection implements Downloadable {
private List<Element> getCandidatesAsElements() {
List<Element> elements = new ArrayList<>();
for (JingleCandidate c : this.candidates) {
elements.add(c.toElement());
if (c.isOurs()) {
elements.add(c.toElement());
}
}
return elements;
}

View file

@ -6,7 +6,9 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@ -56,8 +58,9 @@ public class JingleSocks5Transport extends JingleTransport {
@Override
public void run() {
try {
socket = new Socket(candidate.getHost(),
candidate.getPort());
socket = new Socket();
SocketAddress address = new InetSocketAddress(candidate.getHost(),candidate.getPort());
socket.connect(address,Config.SOCKET_TIMEOUT * 1000);
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
byte[] login = { 0x05, 0x01, 0x00 };

View file

@ -83,8 +83,7 @@ public class Avatar {
}
public static Avatar parsePresence(Element x) {
Element photo = x != null ? x.findChild("photo") : null;
String hash = photo != null ? photo.getContent() : null;
String hash = x == null ? null : x.findChildContent("photo");
if (hash == null) {
return null;
}

View file

@ -51,4 +51,8 @@ public class AbstractStanza extends Element {
|| getTo().equals(account.getJid().toBareJid())
|| getTo().equals(account.getJid());
}
public boolean fromAccount(final Account account) {
return getFrom() != null && getFrom().toBareJid().equals(account.getJid().toBareJid());
}
}

View file

@ -1,5 +1,10 @@
package eu.siacs.conversations.xmpp.stanzas;
import android.util.Pair;
import java.text.ParseException;
import eu.siacs.conversations.parser.AbstractParser;
import eu.siacs.conversations.xml.Element;
public class MessagePacket extends AbstractStanza {
@ -14,12 +19,7 @@ public class MessagePacket extends AbstractStanza {
}
public String getBody() {
Element body = this.findChild("body");
if (body != null) {
return body.getContent();
} else {
return null;
}
return findChildContent("body");
}
public void setBody(String text) {
@ -66,4 +66,31 @@ public class MessagePacket extends AbstractStanza {
return TYPE_NORMAL;
}
}
public Pair<MessagePacket,Long> getForwardedMessagePacket(String name, String namespace) {
Element wrapper = findChild(name, namespace);
if (wrapper == null) {
return null;
}
Element forwarded = wrapper.findChild("forwarded", "urn:xmpp:forward:0");
if (forwarded == null) {
return null;
}
MessagePacket packet = create(forwarded.findChild("message"));
if (packet == null) {
return null;
}
Long timestamp = AbstractParser.getTimestamp(forwarded,null);
return new Pair(packet,timestamp);
}
public static MessagePacket create(Element element) {
if (element == null) {
return null;
}
MessagePacket packet = new MessagePacket();
packet.setAttributes(element.getAttributes());
packet.setChildren(element.getChildren());
return packet;
}
}