first tries on crypto muc

This commit is contained in:
Daniel Gultsch 2014-05-22 15:36:41 +02:00
parent f4ce5082b9
commit fd4897e1a5
6 changed files with 80 additions and 41 deletions

View File

@ -276,7 +276,7 @@ public class Conversation extends AbstractEntity {
public synchronized MucOptions getMucOptions() { public synchronized MucOptions getMucOptions() {
if (this.mucOptions == null) { if (this.mucOptions == null) {
this.mucOptions = new MucOptions(); this.mucOptions = new MucOptions(this.getAccount());
} }
this.mucOptions.setConversation(this); this.mucOptions.setConversation(this);
return this.mucOptions; return this.mucOptions;

View File

@ -3,11 +3,10 @@ package eu.siacs.conversations.entities;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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.xml.Element;
import eu.siacs.conversations.xmpp.stanzas.PresencePacket; import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.util.Log;
@SuppressLint("DefaultLocale") @SuppressLint("DefaultLocale")
public class MucOptions { public class MucOptions {
@ -31,6 +30,7 @@ public class MucOptions {
private int role; private int role;
private int affiliation; private int affiliation;
private String name; private String name;
private long pgpKeyId = 0;
public String getName() { public String getName() {
return name; return name;
@ -70,7 +70,15 @@ public class MucOptions {
this.affiliation = AFFILIATION_NONE; this.affiliation = AFFILIATION_NONE;
} }
} }
public void setPgpKeyId(long id) {
this.pgpKeyId = id;
}
public long getPgpKeyId() {
return this.pgpKeyId;
}
} }
private Account account;
private ArrayList<User> users = new ArrayList<User>(); private ArrayList<User> users = new ArrayList<User>();
private Conversation conversation; private Conversation conversation;
private boolean isOnline = false; private boolean isOnline = false;
@ -80,6 +88,9 @@ public class MucOptions {
private User self = new User(); private User self = new User();
private String subject = null; private String subject = null;
public MucOptions(Account account) {
this.account = account;
}
public void deleteUser(String name) { public void deleteUser(String name) {
for(int i = 0; i < users.size(); ++i) { for(int i = 0; i < users.size(); ++i) {
@ -100,7 +111,7 @@ public class MucOptions {
users.add(user); users.add(user);
} }
public void processPacket(PresencePacket packet) { public void processPacket(PresencePacket packet, PgpEngine pgp) {
String[] fromParts = packet.getFrom().split("/"); String[] fromParts = packet.getFrom().split("/");
if (fromParts.length>=2) { if (fromParts.length>=2) {
String name = fromParts[1]; String name = fromParts[1];
@ -119,6 +130,20 @@ public class MucOptions {
} else { } else {
addUser(user); 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")) { } else if (type.equals("unavailable")) {
if (name.equals(getNick())) { if (name.equals(getNick())) {
Element item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item"); Element item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");

View File

@ -279,7 +279,7 @@ public class XmppConnectionService extends Service {
Conversation muc = findMuc( Conversation muc = findMuc(
packet.getAttribute("from").split("/")[0], account); packet.getAttribute("from").split("/")[0], account);
if (muc != null) { if (muc != null) {
muc.getMucOptions().processPacket(packet); muc.getMucOptions().processPacket(packet,getPgpEngine());
} else { } else {
Log.d(LOGTAG, account.getJid() Log.d(LOGTAG, account.getJid()
+ ": could not find muc for received muc package " + ": could not find muc for received muc package "
@ -293,7 +293,7 @@ public class XmppConnectionService extends Service {
account.getJid() + ": reading muc status packet " account.getJid() + ": reading muc status packet "
+ packet.toString()); + packet.toString());
int error = muc.getMucOptions().getError(); int error = muc.getMucOptions().getError();
muc.getMucOptions().processPacket(packet); muc.getMucOptions().processPacket(packet,getPgpEngine());
if ((muc.getMucOptions().getError() != error) if ((muc.getMucOptions().getError() != error)
&& (convChangedListener != null)) { && (convChangedListener != null)) {
Log.d(LOGTAG, "muc error status changed"); Log.d(LOGTAG, "muc error status changed");
@ -873,6 +873,7 @@ public class XmppConnectionService extends Service {
packet.setBody(message.getBody()); packet.setBody(message.getBody());
packet.setTo(message.getCounterpart().split("/")[0]); packet.setTo(message.getCounterpart().split("/")[0]);
packet.setFrom(account.getJid()); packet.setFrom(account.getJid());
packet.addChild("x", "jabber:x:encrypted").setContent("test");
} }
packet.setId(message.getUuid()); packet.setId(message.getUuid());
return packet; return packet;
@ -1107,6 +1108,7 @@ public class XmppConnectionService extends Service {
} }
public void joinMuc(Conversation conversation) { public void joinMuc(Conversation conversation) {
Account account = conversation.getAccount();
String[] mucParts = conversation.getContactJid().split("/"); String[] mucParts = conversation.getContactJid().split("/");
String muc; String muc;
String nick; String nick;
@ -1115,19 +1117,24 @@ public class XmppConnectionService extends Service {
nick = mucParts[1]; nick = mucParts[1];
} else { } else {
muc = mucParts[0]; muc = mucParts[0];
nick = conversation.getAccount().getUsername(); nick = account.getUsername();
} }
PresencePacket packet = new PresencePacket(); PresencePacket packet = new PresencePacket();
packet.setAttribute("to", muc + "/" + nick); packet.setAttribute("to", muc + "/" + nick);
Element x = new Element("x"); Element x = new Element("x");
x.setAttribute("xmlns", "http://jabber.org/protocol/muc"); 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) { if (conversation.getMessages().size() != 0) {
long lastMsgTime = conversation.getLatestMessage().getTimeSent(); long lastMsgTime = conversation.getLatestMessage().getTimeSent();
long diff = (System.currentTimeMillis() - lastMsgTime) / 1000 - 1; long diff = (System.currentTimeMillis() - lastMsgTime) / 1000 - 1;
x.addChild("history").setAttribute("seconds", diff + ""); x.addChild("history").setAttribute("seconds", diff + "");
} }
packet.addChild(x); packet.addChild(x);
conversation.getAccount().getXmppConnection() account.getXmppConnection()
.sendPresencePacket(packet); .sendPresencePacket(packet);
} }

View File

@ -321,7 +321,6 @@ public class ConversationActivity extends XmppActivity {
if (this.getSelectedConversation() != null) { if (this.getSelectedConversation() != null) {
if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) { if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
menuContactDetails.setVisible(false); menuContactDetails.setVisible(false);
menuSecure.setVisible(false);
menuAttach.setVisible(false); menuAttach.setVisible(false);
} else { } else {
menuMucDetails.setVisible(false); menuMucDetails.setVisible(false);
@ -536,14 +535,17 @@ public class ConversationActivity extends XmppActivity {
} }
}); });
popup.inflate(R.menu.encryption_choices); 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()) { switch (conversation.getNextEncryption()) {
case Message.ENCRYPTION_NONE: case Message.ENCRYPTION_NONE:
popup.getMenu().findItem(R.id.encryption_choice_none) popup.getMenu().findItem(R.id.encryption_choice_none)
.setChecked(true); .setChecked(true);
break; break;
case Message.ENCRYPTION_OTR: case Message.ENCRYPTION_OTR:
popup.getMenu().findItem(R.id.encryption_choice_otr) otr.setChecked(true);
.setChecked(true);
break; break;
case Message.ENCRYPTION_PGP: case Message.ENCRYPTION_PGP:
popup.getMenu().findItem(R.id.encryption_choice_pgp) popup.getMenu().findItem(R.id.encryption_choice_pgp)

View File

@ -678,37 +678,41 @@ public class ConversationFragment extends Fragment {
final XmppConnectionService xmppService = activity.xmppConnectionService; final XmppConnectionService xmppService = activity.xmppConnectionService;
final Contact contact = message.getConversation().getContact(); final Contact contact = message.getConversation().getContact();
if (activity.hasPgp()) { if (activity.hasPgp()) {
if (contact.getPgpKeyId() != 0) { if (conversation.getMode() == Conversation.MODE_SINGLE) {
xmppService.getPgpEngine().hasKey(contact, new UiCallback() { if (contact.getPgpKeyId() != 0) {
xmppService.getPgpEngine().hasKey(contact, new UiCallback() {
@Override @Override
public void userInputRequried(PendingIntent pi) { public void userInputRequried(PendingIntent pi) {
activity.runIntent(pi, activity.runIntent(pi,
ConversationActivity.REQUEST_ENCRYPT_MESSAGE); ConversationActivity.REQUEST_ENCRYPT_MESSAGE);
} }
@Override @Override
public void success() { public void success() {
activity.encryptTextMessage(); activity.encryptTextMessage();
} }
@Override @Override
public void error(int error) { 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 { } else {
showNoPGPKeyDialog(new DialogInterface.OnClickListener() { activity.encryptTextMessage();
@Override
public void onClick(DialogInterface dialog, int which) {
conversation.setNextEncryption(Message.ENCRYPTION_NONE);
message.setEncryption(Message.ENCRYPTION_NONE);
xmppService.sendMessage(message, null);
chatMsg.setText("");
}
});
} }
} }
} }

View File

@ -181,6 +181,7 @@ public class MucDetailsActivity extends XmppActivity {
.findViewById(R.id.contact_photo); .findViewById(R.id.contact_photo);
imageView.setImageBitmap(UIHelper.getContactPicture(contact.getName(), 48,this.getApplicationContext(), false)); imageView.setImageBitmap(UIHelper.getContactPicture(contact.getName(), 48,this.getApplicationContext(), false));
membersView.addView(view); membersView.addView(view);
Log.d(LOGTAG,contact.getName()+" pgp id: "+contact.getPgpKeyId());
} }
} }
} else { } else {