try to fix idn stuff

This commit is contained in:
Daniel Gultsch 2016-10-20 16:47:46 +02:00
parent c3423d6ffe
commit 9930cd5a47
5 changed files with 28 additions and 11 deletions

View file

@ -196,7 +196,7 @@ public class Contact implements ListItem, Blockable {
values.put(ACCOUNT, accountUuid); values.put(ACCOUNT, accountUuid);
values.put(SYSTEMNAME, systemName); values.put(SYSTEMNAME, systemName);
values.put(SERVERNAME, serverName); values.put(SERVERNAME, serverName);
values.put(JID, jid.toString()); values.put(JID, jid.toPreppedString());
values.put(OPTIONS, subscription); values.put(OPTIONS, subscription);
values.put(SYSTEMACCOUNT, systemAccount); values.put(SYSTEMACCOUNT, systemAccount);
values.put(PHOTOURI, photoUri); values.put(PHOTOURI, photoUri);

View file

@ -506,7 +506,7 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
values.put(NAME, name); values.put(NAME, name);
values.put(CONTACT, contactUuid); values.put(CONTACT, contactUuid);
values.put(ACCOUNT, accountUuid); values.put(ACCOUNT, accountUuid);
values.put(CONTACTJID, contactJid.toString()); values.put(CONTACTJID, contactJid.toPreppedString());
values.put(CREATED, created); values.put(CREATED, created);
values.put(STATUS, status); values.put(STATUS, status);
values.put(MODE, mode); values.put(MODE, mode);

View file

@ -205,12 +205,12 @@ public class Message extends AbstractEntity {
if (counterpart == null) { if (counterpart == null) {
values.putNull(COUNTERPART); values.putNull(COUNTERPART);
} else { } else {
values.put(COUNTERPART, counterpart.toString()); values.put(COUNTERPART, counterpart.toPreppedString());
} }
if (trueCounterpart == null) { if (trueCounterpart == null) {
values.putNull(TRUE_COUNTERPART); values.putNull(TRUE_COUNTERPART);
} else { } else {
values.put(TRUE_COUNTERPART, trueCounterpart.toString()); values.put(TRUE_COUNTERPART, trueCounterpart.toPreppedString());
} }
values.put(BODY, body); values.put(BODY, body);
values.put(TIME_SENT, timeSent); values.put(TIME_SENT, timeSent);

View file

@ -287,7 +287,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
continue; continue;
} }
int ownDeviceId = Integer.valueOf(ownDeviceIdString); int ownDeviceId = Integer.valueOf(ownDeviceIdString);
AxolotlAddress ownAddress = new AxolotlAddress(account.getJid().toBareJid().toString(), ownDeviceId); AxolotlAddress ownAddress = new AxolotlAddress(account.getJid().toBareJid().toPreppedString(), ownDeviceId);
deleteSession(db, account, ownAddress); deleteSession(db, account, ownAddress);
IdentityKeyPair identityKeyPair = loadOwnIdentityKeyPair(db, account); IdentityKeyPair identityKeyPair = loadOwnIdentityKeyPair(db, account);
if (identityKeyPair != null) { if (identityKeyPair != null) {
@ -345,7 +345,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
try { try {
newJid = Jid.fromString( newJid = Jid.fromString(
cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID)) cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID))
).toString(); ).toPreppedString();
} catch (InvalidJidException ignored) { } catch (InvalidJidException ignored) {
Log.e(Config.LOGTAG, "Failed to migrate Conversation CONTACTJID " Log.e(Config.LOGTAG, "Failed to migrate Conversation CONTACTJID "
+ cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID)) + cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID))
@ -578,8 +578,8 @@ public class DatabaseBackend extends SQLiteOpenHelper {
public Conversation findConversation(final Account account, final Jid contactJid) { public Conversation findConversation(final Account account, final Jid contactJid) {
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
String[] selectionArgs = {account.getUuid(), String[] selectionArgs = {account.getUuid(),
contactJid.toBareJid().toString() + "/%", contactJid.toBareJid().toPreppedString() + "/%",
contactJid.toBareJid().toString() contactJid.toBareJid().toPreppedString()
}; };
Cursor cursor = db.query(Conversation.TABLENAME, null, Cursor cursor = db.query(Conversation.TABLENAME, null,
Conversation.ACCOUNT + "=? AND (" + Conversation.CONTACTJID Conversation.ACCOUNT + "=? AND (" + Conversation.CONTACTJID
@ -691,7 +691,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
db.insert(Contact.TABLENAME, null, contact.getContentValues()); db.insert(Contact.TABLENAME, null, contact.getContentValues());
} else { } else {
String where = Contact.ACCOUNT + "=? AND " + Contact.JID + "=?"; String where = Contact.ACCOUNT + "=? AND " + Contact.JID + "=?";
String[] whereArgs = {account.getUuid(), contact.getJid().toString()}; String[] whereArgs = {account.getUuid(), contact.getJid().toPreppedString()};
db.delete(Contact.TABLENAME, where, whereArgs); db.delete(Contact.TABLENAME, where, whereArgs);
} }
} }
@ -1025,7 +1025,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
} }
private IdentityKeyPair loadOwnIdentityKeyPair(SQLiteDatabase db, Account account) { private IdentityKeyPair loadOwnIdentityKeyPair(SQLiteDatabase db, Account account) {
String name = account.getJid().toBareJid().toString(); String name = account.getJid().toBareJid().toPreppedString();
IdentityKeyPair identityKeyPair = null; IdentityKeyPair identityKeyPair = null;
Cursor cursor = getIdentityKeyCursor(db, account, name, true); Cursor cursor = getIdentityKeyCursor(db, account, name, true);
if (cursor.getCount() != 0) { if (cursor.getCount() != 0) {
@ -1181,7 +1181,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
} }
public void storeOwnIdentityKeyPair(Account account, IdentityKeyPair identityKeyPair) { public void storeOwnIdentityKeyPair(Account account, IdentityKeyPair identityKeyPair) {
storeIdentityKey(account, account.getJid().toBareJid().toString(), true, identityKeyPair.getPublicKey().getFingerprint().replaceAll("\\s", ""), Base64.encodeToString(identityKeyPair.serialize(), Base64.DEFAULT), XmppAxolotlSession.Trust.TRUSTED); storeIdentityKey(account, account.getJid().toBareJid().toPreppedString(), true, identityKeyPair.getPublicKey().getFingerprint().replaceAll("\\s", ""), Base64.encodeToString(identityKeyPair.serialize(), Base64.DEFAULT), XmppAxolotlSession.Trust.TRUSTED);
} }

View file

@ -21,6 +21,10 @@ public final class Jid {
private final String domainpart; private final String domainpart;
private final String resourcepart; private final String resourcepart;
// It's much more efficient to store the ful JID as well as the parts instead of figuring them
// all out every time (since some characters are displayed but aren't used for comparisons).
private final String displayjid;
public String getLocalpart() { public String getLocalpart() {
return localpart; return localpart;
} }
@ -69,6 +73,7 @@ public final class Jid {
Jid fromCache = Jid.cache.get(jid); Jid fromCache = Jid.cache.get(jid);
if (fromCache != null) { if (fromCache != null) {
displayjid = fromCache.displayjid;
localpart = fromCache.localpart; localpart = fromCache.localpart;
domainpart = fromCache.domainpart; domainpart = fromCache.domainpart;
resourcepart = fromCache.resourcepart; resourcepart = fromCache.resourcepart;
@ -89,6 +94,8 @@ public final class Jid {
throw new InvalidJidException(InvalidJidException.INVALID_CHARACTER); throw new InvalidJidException(InvalidJidException.INVALID_CHARACTER);
} }
String finaljid;
final int domainpartStart; final int domainpartStart;
final int atLoc = jid.indexOf("@"); final int atLoc = jid.indexOf("@");
final int slashLoc = jid.indexOf("/"); final int slashLoc = jid.indexOf("/");
@ -96,6 +103,7 @@ public final class Jid {
// or there are one or more "@" signs but they're all in the resourcepart (eg. "example.net/@/rp@"): // or there are one or more "@" signs but they're all in the resourcepart (eg. "example.net/@/rp@"):
if (atCount == 0 || (atCount > 0 && slashLoc != -1 && atLoc > slashLoc)) { if (atCount == 0 || (atCount > 0 && slashLoc != -1 && atLoc > slashLoc)) {
localpart = ""; localpart = "";
finaljid = "";
domainpartStart = 0; domainpartStart = 0;
} else { } else {
final String lp = jid.substring(0, atLoc); final String lp = jid.substring(0, atLoc);
@ -108,6 +116,7 @@ public final class Jid {
throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH); throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
} }
domainpartStart = atLoc + 1; domainpartStart = atLoc + 1;
finaljid = lp + "@";
} }
final String dp; final String dp;
@ -126,6 +135,7 @@ public final class Jid {
} catch (final StringprepException e) { } catch (final StringprepException e) {
throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e); throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e);
} }
finaljid = finaljid + dp + "/" + rp;
} else { } else {
resourcepart = ""; resourcepart = "";
try{ try{
@ -133,6 +143,7 @@ public final class Jid {
} catch (final StringprepException e) { } catch (final StringprepException e) {
throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e); throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e);
} }
finaljid = finaljid + dp;
} }
// Remove trailing "." before storing the domain part. // Remove trailing "." before storing the domain part.
@ -156,6 +167,8 @@ public final class Jid {
} }
Jid.cache.put(jid, this); Jid.cache.put(jid, this);
this.displayjid = finaljid;
} }
public Jid toBareJid() { public Jid toBareJid() {
@ -178,6 +191,10 @@ public final class Jid {
@Override @Override
public String toString() { public String toString() {
return displayjid;
}
public String toPreppedString() {
String out; String out;
if (hasLocalpart()) { if (hasLocalpart()) {
out = localpart + '@' + domainpart; out = localpart + '@' + domainpart;