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() {
if (this.mucOptions == null) {
this.mucOptions = new MucOptions();
this.mucOptions = new MucOptions(this.getAccount());
}
this.mucOptions.setConversation(this);
return this.mucOptions;

View File

@ -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<User> users = new ArrayList<User>();
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");

View File

@ -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);
}

View File

@ -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)

View File

@ -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() {
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 userInputRequried(PendingIntent pi) {
activity.runIntent(pi,
ConversationActivity.REQUEST_ENCRYPT_MESSAGE);
}
@Override
public void success() {
activity.encryptTextMessage();
}
@Override
public void success() {
activity.encryptTextMessage();
}
@Override
public void error(int error) {
@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();
}
}
}

View File

@ -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 {