From fd4897e1a5a843c8b55b28b00afa6778b345c9a5 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 22 May 2014 15:36:41 +0200 Subject: [PATCH] first tries on crypto muc --- .../conversations/entities/Conversation.java | 2 +- .../conversations/entities/MucOptions.java | 31 ++++++++- .../services/XmppConnectionService.java | 15 +++-- .../ui/ConversationActivity.java | 8 ++- .../ui/ConversationFragment.java | 64 ++++++++++--------- .../conversations/ui/MucDetailsActivity.java | 1 + 6 files changed, 80 insertions(+), 41 deletions(-) diff --git a/src/eu/siacs/conversations/entities/Conversation.java b/src/eu/siacs/conversations/entities/Conversation.java index 37a230df..a9699660 100644 --- a/src/eu/siacs/conversations/entities/Conversation.java +++ b/src/eu/siacs/conversations/entities/Conversation.java @@ -276,7 +276,7 @@ public class Conversation extends AbstractEntity { public synchronized MucOptions getMucOptions() { if (this.mucOptions == null) { - this.mucOptions = new MucOptions(); + this.mucOptions = new MucOptions(this.getAccount()); } this.mucOptions.setConversation(this); return this.mucOptions; diff --git a/src/eu/siacs/conversations/entities/MucOptions.java b/src/eu/siacs/conversations/entities/MucOptions.java index fbca8340..ba2b9bbf 100644 --- a/src/eu/siacs/conversations/entities/MucOptions.java +++ b/src/eu/siacs/conversations/entities/MucOptions.java @@ -3,11 +3,10 @@ package eu.siacs.conversations.entities; import java.util.ArrayList; import java.util.List; -import eu.siacs.conversations.entities.MucOptions.User; +import eu.siacs.conversations.crypto.PgpEngine; import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xmpp.stanzas.PresencePacket; import android.annotation.SuppressLint; -import android.util.Log; @SuppressLint("DefaultLocale") public class MucOptions { @@ -31,6 +30,7 @@ public class MucOptions { private int role; private int affiliation; private String name; + private long pgpKeyId = 0; public String getName() { return name; @@ -70,7 +70,15 @@ public class MucOptions { this.affiliation = AFFILIATION_NONE; } } + public void setPgpKeyId(long id) { + this.pgpKeyId = id; + } + + public long getPgpKeyId() { + return this.pgpKeyId; + } } + private Account account; private ArrayList users = new ArrayList(); private Conversation conversation; private boolean isOnline = false; @@ -80,6 +88,9 @@ public class MucOptions { private User self = new User(); private String subject = null; + public MucOptions(Account account) { + this.account = account; + } public void deleteUser(String name) { for(int i = 0; i < users.size(); ++i) { @@ -100,7 +111,7 @@ public class MucOptions { users.add(user); } - public void processPacket(PresencePacket packet) { + public void processPacket(PresencePacket packet, PgpEngine pgp) { String[] fromParts = packet.getFrom().split("/"); if (fromParts.length>=2) { String name = fromParts[1]; @@ -119,6 +130,20 @@ public class MucOptions { } else { addUser(user); } + if (pgp != null) { + Element x = packet.findChild("x", + "jabber:x:signed"); + if (x != null) { + Element status = packet.findChild("status"); + String msg; + if (status != null) { + msg = status.getContent(); + } else { + msg = ""; + } + user.setPgpKeyId(pgp.fetchKeyId(account,msg, x.getContent())); + } + } } else if (type.equals("unavailable")) { if (name.equals(getNick())) { Element item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item"); diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index e31d28e0..3347ea1f 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -279,7 +279,7 @@ public class XmppConnectionService extends Service { Conversation muc = findMuc( packet.getAttribute("from").split("/")[0], account); if (muc != null) { - muc.getMucOptions().processPacket(packet); + muc.getMucOptions().processPacket(packet,getPgpEngine()); } else { Log.d(LOGTAG, account.getJid() + ": could not find muc for received muc package " @@ -293,7 +293,7 @@ public class XmppConnectionService extends Service { account.getJid() + ": reading muc status packet " + packet.toString()); int error = muc.getMucOptions().getError(); - muc.getMucOptions().processPacket(packet); + muc.getMucOptions().processPacket(packet,getPgpEngine()); if ((muc.getMucOptions().getError() != error) && (convChangedListener != null)) { Log.d(LOGTAG, "muc error status changed"); @@ -873,6 +873,7 @@ public class XmppConnectionService extends Service { packet.setBody(message.getBody()); packet.setTo(message.getCounterpart().split("/")[0]); packet.setFrom(account.getJid()); + packet.addChild("x", "jabber:x:encrypted").setContent("test"); } packet.setId(message.getUuid()); return packet; @@ -1107,6 +1108,7 @@ public class XmppConnectionService extends Service { } public void joinMuc(Conversation conversation) { + Account account = conversation.getAccount(); String[] mucParts = conversation.getContactJid().split("/"); String muc; String nick; @@ -1115,19 +1117,24 @@ public class XmppConnectionService extends Service { nick = mucParts[1]; } else { muc = mucParts[0]; - nick = conversation.getAccount().getUsername(); + nick = account.getUsername(); } PresencePacket packet = new PresencePacket(); packet.setAttribute("to", muc + "/" + nick); Element x = new Element("x"); x.setAttribute("xmlns", "http://jabber.org/protocol/muc"); + String sig = account.getPgpSignature(); + if (sig != null) { + packet.addChild("status").setContent("online"); + packet.addChild("x", "jabber:x:signed").setContent(sig); + } if (conversation.getMessages().size() != 0) { long lastMsgTime = conversation.getLatestMessage().getTimeSent(); long diff = (System.currentTimeMillis() - lastMsgTime) / 1000 - 1; x.addChild("history").setAttribute("seconds", diff + ""); } packet.addChild(x); - conversation.getAccount().getXmppConnection() + account.getXmppConnection() .sendPresencePacket(packet); } diff --git a/src/eu/siacs/conversations/ui/ConversationActivity.java b/src/eu/siacs/conversations/ui/ConversationActivity.java index bd98e979..c696fa45 100644 --- a/src/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/eu/siacs/conversations/ui/ConversationActivity.java @@ -321,7 +321,6 @@ public class ConversationActivity extends XmppActivity { if (this.getSelectedConversation() != null) { if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) { menuContactDetails.setVisible(false); - menuSecure.setVisible(false); menuAttach.setVisible(false); } else { menuMucDetails.setVisible(false); @@ -536,14 +535,17 @@ public class ConversationActivity extends XmppActivity { } }); popup.inflate(R.menu.encryption_choices); + MenuItem otr = popup.getMenu().findItem(R.id.encryption_choice_otr); + if (conversation.getMode() == Conversation.MODE_MULTI) { + otr.setVisible(false); + } switch (conversation.getNextEncryption()) { case Message.ENCRYPTION_NONE: popup.getMenu().findItem(R.id.encryption_choice_none) .setChecked(true); break; case Message.ENCRYPTION_OTR: - popup.getMenu().findItem(R.id.encryption_choice_otr) - .setChecked(true); + otr.setChecked(true); break; case Message.ENCRYPTION_PGP: popup.getMenu().findItem(R.id.encryption_choice_pgp) diff --git a/src/eu/siacs/conversations/ui/ConversationFragment.java b/src/eu/siacs/conversations/ui/ConversationFragment.java index 44d1848f..a3cecfd4 100644 --- a/src/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/eu/siacs/conversations/ui/ConversationFragment.java @@ -678,37 +678,41 @@ public class ConversationFragment extends Fragment { final XmppConnectionService xmppService = activity.xmppConnectionService; final Contact contact = message.getConversation().getContact(); if (activity.hasPgp()) { - if (contact.getPgpKeyId() != 0) { - xmppService.getPgpEngine().hasKey(contact, new UiCallback() { - - @Override - public void userInputRequried(PendingIntent pi) { - activity.runIntent(pi, - ConversationActivity.REQUEST_ENCRYPT_MESSAGE); - } - - @Override - public void success() { - activity.encryptTextMessage(); - } - - @Override - public void error(int error) { - - } - }); - + if (conversation.getMode() == Conversation.MODE_SINGLE) { + if (contact.getPgpKeyId() != 0) { + xmppService.getPgpEngine().hasKey(contact, new UiCallback() { + + @Override + public void userInputRequried(PendingIntent pi) { + activity.runIntent(pi, + ConversationActivity.REQUEST_ENCRYPT_MESSAGE); + } + + @Override + public void success() { + activity.encryptTextMessage(); + } + + @Override + public void error(int error) { + + } + }); + + } else { + showNoPGPKeyDialog(new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + conversation.setNextEncryption(Message.ENCRYPTION_NONE); + message.setEncryption(Message.ENCRYPTION_NONE); + xmppService.sendMessage(message, null); + chatMsg.setText(""); + } + }); + } } else { - showNoPGPKeyDialog(new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - conversation.setNextEncryption(Message.ENCRYPTION_NONE); - message.setEncryption(Message.ENCRYPTION_NONE); - xmppService.sendMessage(message, null); - chatMsg.setText(""); - } - }); + activity.encryptTextMessage(); } } } diff --git a/src/eu/siacs/conversations/ui/MucDetailsActivity.java b/src/eu/siacs/conversations/ui/MucDetailsActivity.java index d1725d41..bee7789f 100644 --- a/src/eu/siacs/conversations/ui/MucDetailsActivity.java +++ b/src/eu/siacs/conversations/ui/MucDetailsActivity.java @@ -181,6 +181,7 @@ public class MucDetailsActivity extends XmppActivity { .findViewById(R.id.contact_photo); imageView.setImageBitmap(UIHelper.getContactPicture(contact.getName(), 48,this.getApplicationContext(), false)); membersView.addView(view); + Log.d(LOGTAG,contact.getName()+" pgp id: "+contact.getPgpKeyId()); } } } else {