001/**
002 *
003 * Copyright 2024 Florian Schmaus.
004 *
005 * This file is part of smack-examples.
006 *
007 * smack-examples is free software; you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License as published by
009 * the Free Software Foundation; either version 3 of the License, or
010 * (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with this program; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
020 */
021package org.igniterealtime.smack.examples;
022
023import java.io.IOException;
024
025import org.jivesoftware.smack.SmackException;
026import org.jivesoftware.smack.SmackException.NoResponseException;
027import org.jivesoftware.smack.SmackException.NotConnectedException;
028import org.jivesoftware.smack.XMPPException;
029import org.jivesoftware.smack.XMPPException.XMPPErrorException;
030import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection;
031import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration;
032import org.jivesoftware.smack.debugger.ConsoleDebugger;
033import org.jivesoftware.smack.debugger.SmackDebuggerFactory;
034import org.jivesoftware.smackx.omemo.util.OmemoConstants;
035import org.jivesoftware.smackx.pep.PepManager;
036import org.jivesoftware.smackx.pubsub.PubSubManager;
037
038import org.jxmpp.stringprep.XmppStringprepException;
039
040public class XmppConnectionTool {
041
042    public final ModularXmppClientToServerConnection connection;
043
044    public XmppConnectionTool(ModularXmppClientToServerConnection connection) {
045        this.connection = connection;
046    }
047
048    public boolean purgeOmemoInformation()
049            throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
050        PepManager pepManager = PepManager.getInstanceFor(connection);
051        PubSubManager pepPubSubManager = pepManager.getPepPubSubManager();
052
053        // TODO: Also delete "bundles" nodes.
054        return pepPubSubManager.deleteNode(OmemoConstants.PEP_NODE_DEVICE_LIST);
055    }
056
057    public static XmppConnectionTool of(String jid, String password, boolean debug)
058            throws XMPPException, SmackException, IOException, InterruptedException {
059        ModularXmppClientToServerConnection connection = createConnectionFor(jid, password, debug);
060        connection.connect().login();
061        return new XmppConnectionTool(connection);
062    }
063
064    public static ModularXmppClientToServerConnection createConnectionFor(String jid, String password, boolean debug)
065            throws XmppStringprepException {
066
067        final SmackDebuggerFactory smackDebuggerFactory;
068        if (debug) {
069            smackDebuggerFactory = ConsoleDebugger.Factory.INSTANCE;
070        } else {
071            smackDebuggerFactory = null;
072        }
073
074        ModularXmppClientToServerConnectionConfiguration.Builder configurationBuilder = ModularXmppClientToServerConnectionConfiguration
075                .builder().setXmppAddressAndPassword(jid, password).setDebuggerFactory(smackDebuggerFactory);
076
077        ModularXmppClientToServerConnectionConfiguration configuration = configurationBuilder.build();
078
079        ModularXmppClientToServerConnection connection = new ModularXmppClientToServerConnection(configuration);
080        return connection;
081    }
082}