1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-23 20:42:06 +01:00

Simplify api

This commit is contained in:
Paul Schaub 2018-06-22 15:28:14 +02:00
parent 7b222d8c58
commit 730274f9f8
3 changed files with 58 additions and 37 deletions

View file

@ -16,7 +16,10 @@
*/
package org.jivesoftware.smackx.ox;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
@ -25,10 +28,15 @@ import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.eme.element.ExplicitMessageEncryptionElement;
import org.jivesoftware.smackx.hints.element.StoreHint;
import org.jivesoftware.smackx.ox.chat.OpenPgpContact;
import org.jivesoftware.smackx.ox.element.SigncryptElement;
import org.jivesoftware.smackx.ox.exception.MissingOpenPgpKeyPairException;
import org.jivesoftware.smackx.ox.exception.SmackOpenPgpException;
import org.jivesoftware.smackx.ox.listener.OxMessageListener;
import org.jivesoftware.smackx.ox.listener.internal.SigncryptElementReceivedListener;
@ -116,4 +124,20 @@ public final class OXInstantMessagingManager extends Manager implements Signcryp
listener.newIncomingOxMessage(contact, originalMessage, signcryptElement);
}
}
public void sendOxMessage(OpenPgpContact contact, CharSequence body)
throws InterruptedException, MissingOpenPgpKeyPairException, IOException,
SmackException.NotConnectedException, SmackOpenPgpException {
Message message = new Message(contact.getJid());
List<ExtensionElement> payload = new ArrayList<>();
payload.add(new Message.Body(null, body.toString()));
// Add additional information to the message
message.addExtension(new ExplicitMessageEncryptionElement(
ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0));
StoreHint.set(message);
message.setBody("This message is encrypted using XEP-0374: OpenPGP for XMPP: Instant Messaging.");
contact.send(connection(), message, payload);
}
}

View file

@ -413,34 +413,46 @@ public final class OpenPgpManager extends Manager {
PayloadItem<?> payload = (PayloadItem) items.getItems().get(0);
PublicKeysListElement listElement = (PublicKeysListElement) payload.getPayload();
Map<OpenPgpV4Fingerprint, Date> announcedKeys = new HashMap<>();
for (OpenPgpV4Fingerprint f : listElement.getMetadata().keySet()) {
PublicKeysListElement.PubkeyMetadataElement meta = listElement.getMetadata().get(f);
announcedKeys.put(meta.getV4Fingerprint(), meta.getDate());
}
provider.getStore().setAnnouncedKeysFingerprints(contact, announcedKeys);
Set<OpenPgpV4Fingerprint> missingKeys = listElement.getMetadata().keySet();
try {
missingKeys.removeAll(provider.getStore().getAvailableKeysFingerprints(contact).keySet());
for (OpenPgpV4Fingerprint missing : missingKeys) {
try {
PubkeyElement pubkeyElement = fetchPubkey(connection(), contact, missing);
processPublicKey(pubkeyElement, contact);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Error fetching missing OpenPGP key " + missing.toString(), e);
}
}
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Error processing OpenPGP metadata update from " + contact + ".", e);
}
processPublicKeysListElement(from, listElement);
}
}, "ProcessOXMetadata");
}
}
};
public void requestMetadataUpdate(BareJid contact)
throws InterruptedException, PubSubException.NotALeafNodeException, SmackException.NoResponseException,
SmackException.NotConnectedException, XMPPException.XMPPErrorException,
PubSubException.NotAPubSubNodeException {
PublicKeysListElement metadata = PubSubDelegate.fetchPubkeysList(connection(), contact);
processPublicKeysListElement(contact, metadata);
}
private void processPublicKeysListElement(BareJid contact, PublicKeysListElement listElement) {
Map<OpenPgpV4Fingerprint, Date> announcedKeys = new HashMap<>();
for (OpenPgpV4Fingerprint f : listElement.getMetadata().keySet()) {
PublicKeysListElement.PubkeyMetadataElement meta = listElement.getMetadata().get(f);
announcedKeys.put(meta.getV4Fingerprint(), meta.getDate());
}
provider.getStore().setAnnouncedKeysFingerprints(contact, announcedKeys);
Set<OpenPgpV4Fingerprint> missingKeys = listElement.getMetadata().keySet();
try {
missingKeys.removeAll(provider.getStore().getAvailableKeysFingerprints(contact).keySet());
for (OpenPgpV4Fingerprint missing : missingKeys) {
try {
PubkeyElement pubkeyElement = fetchPubkey(connection(), contact, missing);
processPublicKey(pubkeyElement, contact);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Error fetching missing OpenPGP key " + missing.toString(), e);
}
}
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Error processing OpenPGP metadata update from " + contact + ".", e);
}
}
private final IncomingChatMessageListener incomingOpenPgpMessageListener =
new IncomingChatMessageListener() {
@Override

View file

@ -17,7 +17,6 @@
package org.jivesoftware.smackx.ox.chat;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -138,23 +137,9 @@ public class OpenPgpContact {
// Add encrypted payload to message
message.addExtension(encryptedPayload);
// Add additional information to the message
message.addExtension(new ExplicitMessageEncryptionElement(
ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0));
StoreHint.set(message);
message.setBody("This message is encrypted using XEP-0374: OpenPGP for XMPP: Instant Messaging.");
ChatManager.getInstanceFor(connection).chatWith(getJid().asEntityBareJidIfPossible()).send(message);
}
public void send(XMPPConnection connection, Message message, CharSequence body)
throws MissingOpenPgpKeyPairException, SmackException.NotConnectedException, InterruptedException,
SmackOpenPgpException, IOException {
List<ExtensionElement> payload = new ArrayList<>();
payload.add(new Message.Body(null, body.toString()));
send(connection, message, payload);
}
public OpenPgpContentElement receive(OpenPgpElement element)
throws XmlPullParserException, MissingOpenPgpKeyPairException, SmackOpenPgpException, IOException {
byte[] decoded = Base64.decode(element.getEncryptedBase64MessageContent());