From 6be2a08f19f07e9db5da3e644c8d62102fdd27e7 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 19 Mar 2014 16:16:40 +0100 Subject: [PATCH] otr messages are now ignored if conversations isnt 100% sure the message is ours --- res/layout/server_info.xml | 18 +- .../siacs/conversations/entities/Account.java | 17 ++ .../conversations/entities/Presences.java | 22 ++ .../services/XmppConnectionService.java | 241 ++++++++++-------- .../ui/ConversationActivity.java | 31 +-- .../ui/ManageAccountActivity.java | 2 + .../conversations/utils/MessageParser.java | 16 +- .../conversations/xmpp/XmppConnection.java | 2 +- 8 files changed, 216 insertions(+), 133 deletions(-) diff --git a/res/layout/server_info.xml b/res/layout/server_info.xml index 8f19cae13..25e5a4715 100644 --- a/res/layout/server_info.xml +++ b/res/layout/server_info.xml @@ -81,8 +81,24 @@ android:textSize="18sp"/> + + + = PING_TIMEOUT * 1000) { Log.d(LOGTAG, account.getJid() + ": ping timeout"); - this.reconnectAccount(account,true); + this.reconnectAccount(account, true); } else if (SystemClock.elapsedRealtime() - lastReceived >= PING_MIN_INTERVAL * 1000) { account.getXmppConnection().sendPing(); - account.getXmppConnection().lastPingSent = SystemClock.elapsedRealtime(); + account.getXmppConnection().lastPingSent = SystemClock + .elapsedRealtime(); this.scheduleWakeupCall(2, false); } } else if (account.getStatus() == Account.STATUS_OFFLINE) { @@ -440,17 +448,27 @@ public class XmppConnectionService extends Service { account.setXmppConnection(this .createConnection(account)); } - account.getXmppConnection().lastPingSent = SystemClock.elapsedRealtime(); + account.getXmppConnection().lastPingSent = SystemClock + .elapsedRealtime(); new Thread(account.getXmppConnection()).start(); - } else if ((account.getStatus() == Account.STATUS_CONNECTING)&&((SystemClock.elapsedRealtime() - account.getXmppConnection().lastConnect) / 1000 >= CONNECT_TIMEOUT)) { - Log.d(LOGTAG,account.getJid()+": time out during connect reconnecting"); - reconnectAccount(account,true); + } else if ((account.getStatus() == Account.STATUS_CONNECTING) + && ((SystemClock.elapsedRealtime() - account + .getXmppConnection().lastConnect) / 1000 >= CONNECT_TIMEOUT)) { + Log.d(LOGTAG, account.getJid() + + ": time out during connect reconnecting"); + reconnectAccount(account, true); } else { - Log.d(LOGTAG,"seconds since last connect:"+((SystemClock.elapsedRealtime() - account.getXmppConnection().lastConnect) / 1000)); - Log.d(LOGTAG,account.getJid()+": status="+account.getStatus()); - // TODO notify user of ssl cert problem or auth problem or what ever + Log.d(LOGTAG, + "seconds since last connect:" + + ((SystemClock.elapsedRealtime() - account + .getXmppConnection().lastConnect) / 1000)); + Log.d(LOGTAG, + account.getJid() + ": status=" + + account.getStatus()); + // TODO notify user of ssl cert problem or auth problem + // or what ever } - //in any case. reschedule wakup call + // in any case. reschedule wakup call this.scheduleWakeupCall(PING_MAX_INTERVAL, true); } if (accountChangedListener != null) { @@ -490,27 +508,28 @@ public class XmppConnectionService extends Service { Context context = getApplicationContext(); AlarmManager alarmManager = (AlarmManager) context .getSystemService(Context.ALARM_SERVICE); - - - + if (ping) { - if (this.pingIntent==null) { + if (this.pingIntent == null) { this.pingIntent = new Intent(context, EventReceiver.class); this.pingIntent.setAction("ping"); this.pingIntent.putExtra("time", timeToWake); this.pendingPingIntent = PendingIntent.getBroadcast(context, 0, this.pingIntent, 0); - alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,timeToWake, pendingPingIntent); - //Log.d(LOGTAG,"schedule ping in "+seconds+" seconds"); + alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, + timeToWake, pendingPingIntent); + // Log.d(LOGTAG,"schedule ping in "+seconds+" seconds"); } else { long scheduledTime = this.pingIntent.getLongExtra("time", 0); - if (scheduledTime timeToWake)) { + if (scheduledTime < SystemClock.elapsedRealtime() + || (scheduledTime > timeToWake)) { this.pingIntent.putExtra("time", timeToWake); alarmManager.cancel(this.pendingPingIntent); - this.pendingPingIntent = PendingIntent.getBroadcast(context, 0, - this.pingIntent, 0); - alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,timeToWake, pendingPingIntent); - //Log.d(LOGTAG,"reschedule old ping to ping in "+seconds+" seconds"); + this.pendingPingIntent = PendingIntent.getBroadcast( + context, 0, this.pingIntent, 0); + alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, + timeToWake, pendingPingIntent); + // Log.d(LOGTAG,"reschedule old ping to ping in "+seconds+" seconds"); } } } else { @@ -518,7 +537,8 @@ public class XmppConnectionService extends Service { intent.setAction("ping_check"); PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); - alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,timeToWake, alarmIntent); + alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeToWake, + alarmIntent); } } @@ -545,10 +565,11 @@ public class XmppConnectionService extends Service { } }); connection.setOnBindListener(new OnBindListener() { - + @Override public void onBind(Account account) { databaseBackend.clearPresences(account); + account.clearPresences(); // self presences if (account.getXmppConnection().hasFeatureRosterManagment()) { updateRoster(account, null); } @@ -703,7 +724,7 @@ public class XmppConnectionService extends Service { listener.onRosterFetched(contacts); } } - + public List getRoster(Account account) { List contacts = databaseBackend.getContactsByAccount(account); for (int i = 0; i < contacts.size(); ++i) { @@ -834,6 +855,13 @@ public class XmppConnectionService extends Service { conv.setMessages(databaseBackend.getMessages(conv, 50)); } } + Collections.sort(this.conversations, new Comparator() { + @Override + public int compare(Conversation lhs, Conversation rhs) { + return (int) (rhs.getLatestMessage().getTimeSent() - lhs + .getLatestMessage().getTimeSent()); + } + }); return this.conversations; } @@ -870,7 +898,8 @@ public class XmppConnectionService extends Service { } else { conversation.setMode(Conversation.MODE_SINGLE); } - conversation.setMessages(databaseBackend.getMessages(conversation, 50)); + conversation.setMessages(databaseBackend.getMessages(conversation, + 50)); this.databaseBackend.updateConversation(conversation); conversation.setContact(findContact(account, conversation.getContactJid())); @@ -943,7 +972,7 @@ public class XmppConnectionService extends Service { public void updateAccount(Account account) { databaseBackend.updateAccount(account); - reconnectAccount(account,false); + reconnectAccount(account, false); if (accountChangedListener != null) accountChangedListener.onAccountListChangedListener(); } @@ -1065,7 +1094,8 @@ public class XmppConnectionService extends Service { } public void disconnect(Account account, boolean force) { - if ((account.getStatus() == Account.STATUS_ONLINE)||(account.getStatus() == Account.STATUS_DISABLED)) { + if ((account.getStatus() == Account.STATUS_ONLINE) + || (account.getStatus() == Account.STATUS_DISABLED)) { if (!force) { List conversations = getConversations(); for (int i = 0; i < conversations.size(); i++) { @@ -1203,8 +1233,8 @@ public class XmppConnectionService extends Service { this.tlsException = null; } - //TODO dont let thread sleep but schedule wake up - public void reconnectAccount(final Account account,final boolean force) { + // TODO dont let thread sleep but schedule wake up + public void reconnectAccount(final Account account, final boolean force) { new Thread(new Runnable() { @Override @@ -1218,14 +1248,14 @@ public class XmppConnectionService extends Service { } Thread thread = new Thread(account.getXmppConnection()); thread.start(); - scheduleWakeupCall((int) (CONNECT_TIMEOUT * 1.2),false); + scheduleWakeupCall((int) (CONNECT_TIMEOUT * 1.2), false); } } }).start(); } public void updateConversationInGui() { - if (convChangedListener!=null) { + if (convChangedListener != null) { convChangedListener.onConversationListChanged(); } } @@ -1247,7 +1277,7 @@ public class XmppConnectionService extends Service { public void inviteToConference(Conversation conversation, List contacts) { - for(Contact contact : contacts) { + for (Contact contact : contacts) { MessagePacket packet = new MessagePacket(); packet.setTo(conversation.getContactJid().split("/")[0]); packet.setFrom(conversation.getAccount().getFullJid()); @@ -1257,9 +1287,10 @@ public class XmppConnectionService extends Service { invite.setAttribute("to", contact.getJid()); x.addChild(invite); packet.addChild(x); - Log.d(LOGTAG,packet.toString()); - conversation.getAccount().getXmppConnection().sendMessagePacket(packet); + Log.d(LOGTAG, packet.toString()); + conversation.getAccount().getXmppConnection() + .sendMessagePacket(packet); } - + } } \ No newline at end of file diff --git a/src/eu/siacs/conversations/ui/ConversationActivity.java b/src/eu/siacs/conversations/ui/ConversationActivity.java index a08e07273..4acddf8a6 100644 --- a/src/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/eu/siacs/conversations/ui/ConversationActivity.java @@ -1,8 +1,6 @@ package eu.siacs.conversations.ui; import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; import java.util.List; import eu.siacs.conversations.R; @@ -62,9 +60,6 @@ public class ConversationActivity extends XmppActivity { @Override public void onConversationListChanged() { - conversationList.clear(); - conversationList.addAll(xmppConnectionService - .getConversations()); runOnUiThread(new Runnable() { @Override @@ -120,18 +115,6 @@ public class ConversationActivity extends XmppActivity { return paneShouldBeOpen; } - public void updateConversationList() { - if (conversationList.size() >= 1) { - Collections.sort(this.conversationList, new Comparator() { - @Override - public int compare(Conversation lhs, Conversation rhs) { - return (int) (rhs.getLatestMessage().getTimeSent() - lhs.getLatestMessage().getTimeSent()); - } - }); - } - this.listView.invalidateViews(); - } - @Override protected void onCreate(Bundle savedInstanceState) { @@ -229,7 +212,7 @@ public class ConversationActivity extends XmppActivity { if (!getSelectedConversation().isRead()) { getSelectedConversation().markRead(); UIHelper.updateNotification(getApplicationContext(), getConversationList(), null, false); - updateConversationList(); + listView.invalidateViews(); } } } @@ -434,10 +417,7 @@ public class ConversationActivity extends XmppActivity { this.registerListener(); if (conversationList.size()==0) { - conversationList.addAll(xmppConnectionService - .getConversations()); - - this.updateConversationList(); + updateConversationList(); } if ((getIntent().getAction()!=null)&&(getIntent().getAction().equals(Intent.ACTION_VIEW) && (!handledViewIntent))) { @@ -495,4 +475,11 @@ public class ConversationActivity extends XmppActivity { } } } + + public void updateConversationList() { + conversationList.clear(); + conversationList.addAll(xmppConnectionService + .getConversations()); + listView.invalidateViews(); + } } diff --git a/src/eu/siacs/conversations/ui/ManageAccountActivity.java b/src/eu/siacs/conversations/ui/ManageAccountActivity.java index b9ed41025..db6225fac 100644 --- a/src/eu/siacs/conversations/ui/ManageAccountActivity.java +++ b/src/eu/siacs/conversations/ui/ManageAccountActivity.java @@ -316,6 +316,8 @@ public class ManageAccountActivity extends XmppActivity { TextView carbon = (TextView) view.findViewById(R.id.carbon); TextView stream = (TextView) view.findViewById(R.id.stream); TextView roster = (TextView) view.findViewById(R.id.roster); + TextView presences = (TextView) view.findViewById(R.id.number_presences); + presences.setText(selectedAccountForActionMode.countPresences()+""); pcks_received.setText(""+xmpp.getReceivedStanzas()); pcks_sent.setText(""+xmpp.getSentStanzas()); if (connectionAgeHours >= 2) { diff --git a/src/eu/siacs/conversations/utils/MessageParser.java b/src/eu/siacs/conversations/utils/MessageParser.java index d6f86900b..de8f4de88 100644 --- a/src/eu/siacs/conversations/utils/MessageParser.java +++ b/src/eu/siacs/conversations/utils/MessageParser.java @@ -31,18 +31,26 @@ public class MessageParser { } public static Message parseOtrChat(MessagePacket packet, Account account, XmppConnectionService service) { + boolean properlyAddressed = (packet.getTo().split("/").length == 2) || (account.countPresences() == 1); String[] fromParts = packet.getFrom().split("/"); Conversation conversation = service.findOrCreateConversation(account, fromParts[0],false); String body = packet.getBody(); if (!conversation.hasValidOtrSession()) { - conversation.startOtrSession(service.getApplicationContext(), fromParts[1]); + if (properlyAddressed) { + conversation.startOtrSession(service.getApplicationContext(), fromParts[1]); + } else { + Log.d("xmppService",account.getJid()+": ignoring otr session with "+fromParts[0]); + return null; + } } else { String foreignPresence = conversation.getOtrSession().getSessionID().getUserID(); if (!foreignPresence.equals(fromParts[1])) { - Log.d(LOGTAG,"new otr during existing otr session requested. ending old one"); conversation.resetOtrSession(); - Log.d("xmppService","starting new one with "+fromParts[1]); - conversation.startOtrSession(service.getApplicationContext(), fromParts[1]); + if (properlyAddressed) { + conversation.startOtrSession(service.getApplicationContext(), fromParts[1]); + } else { + return null; + } } } try { diff --git a/src/eu/siacs/conversations/xmpp/XmppConnection.java b/src/eu/siacs/conversations/xmpp/XmppConnection.java index fd8242c06..49aca167b 100644 --- a/src/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/eu/siacs/conversations/xmpp/XmppConnection.java @@ -554,7 +554,7 @@ public class XmppConnection implements Runnable { if (bindListener !=null) { bindListener.onBind(account); } - account.setStatus(Account.STATUS_ONLINE); + changeStatus(Account.STATUS_ONLINE); } }); }