<commit_before>package ru.ifmo.neerc.chat.xmpp;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.KeyManagementException;
import java.util.Calendar;
import java.util.Date;

import org.jivesoftware.smack.AbstractConnectionListener;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.PresenceListener;
import org.jivesoftware.smack.ReconnectionManager;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.StanzaExtensionFilter;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smack.util.TLSUtils;
import org.jivesoftware.smackx.muc.DiscussionHistory;
import org.jivesoftware.smackx.muc.MUCRole;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smackx.muc.MultiUserChatManager;
import org.jivesoftware.smackx.delay.packet.DelayInformation;
import org.jivesoftware.smackx.muc.packet.MUCUser;
import org.jivesoftware.smackx.muc.packet.MUCItem;
import org.jivesoftware.smackx.ping.PingManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.ifmo.neerc.chat.AbstractChat;
import ru.ifmo.neerc.chat.ChatListener;
import ru.ifmo.neerc.chat.ChatMessage;
import ru.ifmo.neerc.chat.user.UserEntry;
import ru.ifmo.neerc.chat.user.UserRegistry;
import ru.ifmo.neerc.chat.utils.DebugUtils;
import ru.ifmo.neerc.chat.xmpp.provider.*;
import ru.ifmo.neerc.chat.xmpp.packet.*;
import ru.ifmo.neerc.task.Task;
import ru.ifmo.neerc.task.TaskStatus;
import ru.ifmo.neerc.task.TaskRegistry;
import ru.ifmo.neerc.utils.XmlUtils;

/**
 * @author <NAME>
 */
public class XmppChat extends AbstractChat {
    private static final Logger LOG = LoggerFactory.getLogger(XmppChat.class);

    private static final String SERVER_HOST = System.getProperty("server.host", "localhost");
    private static final String SERVER_HOSTNAME = System.getProperty("server.hostname", SERVER_HOST);
    private static final int SERVER_PORT = Integer.parseInt(System.getProperty("server.port", "5222"));
    private static final String ROOM_NAME = System.getProperty("room", "neerc");
    private static final String ROOM = ROOM_NAME + "@conference." + SERVER_HOSTNAME;
    private static final String NEERC_SERVICE = ROOM_NAME + "@neerc." + SERVER_HOSTNAME;

    private MultiUserChat muc;
    private AbstractXMPPConnection connection;
    
    private String name;
    private String password = System.getProperty("password", "<PASSWORD>");

    private ConnectionListener connectionListener;
    private Date lastActivity = null;

    public XmppChat(
            String name,
            ConnectionListener connectionListener
    ) {
        this.name = name;
        this.connectionListener = connectionListener;

        NeercTaskPacketExtensionProvider.register();
        NeercClockPacketExtensionProvider.register();
        NeercIQProvider.register();

        ReconnectionManager.setEnabledPerDefault(true);
        PingManager.setDefaultPingInterval(5);
    }

    public synchronized void disconnect() {
        if (connection != null) {
            connection.disconnect();
        }
    }
    
    public synchronized void connect() {
        disconnect();
        LOG.info("connecting to server");

        // Create the configuration for this new connection
        XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder()
            .setUsernameAndPassword(name, password)
            .setServiceName(SERVER_HOSTNAME)
            .setHost(SERVER_HOST)
            .setPort(SERVER_PORT)
            .setResource(ROOM_NAME)
            .setCompressionEnabled(true);

        try {
            TLSUtils.acceptAllCertificates(builder);
            TLSUtils.disableHostnameVerificationForTlsCertificicates(builder);
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            LOG.error("Unable to configure connection", e);
        }

        XMPPTCPConnectionConfiguration config = builder.build();

        connection = new XMPPTCPConnection(config);
        connection.addConnectionListener(new MyConnectionListener());
        connection.addConnectionListener(connectionListener);
        connection.addAsyncStanzaListener(new TaskPacketListener(), new StanzaExtensionFilter(new NeercTaskPacketExtension()));

        muc = MultiUserChatManager.getInstanceFor(connection)
            .getMultiUserChat(ROOM);
        muc.addMessageListener(new MyMessageListener());
        muc.addParticipantListener(new MyPresenceListener());

        // Connect to the server
        try {
            connection.connect();<commit_msg>Generate random resource instead of room name<commit_after>package ru.ifmo.neerc.chat.xmpp;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.KeyManagementException;
import java.util.Calendar;
import java.util.Date;

import org.jivesoftware.smack.AbstractConnectionListener;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.PresenceListener;
import org.jivesoftware.smack.ReconnectionManager;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.StanzaExtensionFilter;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smack.util.TLSUtils;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.muc.DiscussionHistory;
import org.jivesoftware.smackx.muc.MUCRole;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smackx.muc.MultiUserChatManager;
import org.jivesoftware.smackx.delay.packet.DelayInformation;
import org.jivesoftware.smackx.muc.packet.MUCUser;
import org.jivesoftware.smackx.muc.packet.MUCItem;
import org.jivesoftware.smackx.ping.PingManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.ifmo.neerc.chat.AbstractChat;
import ru.ifmo.neerc.chat.ChatListener;
import ru.ifmo.neerc.chat.ChatMessage;
import ru.ifmo.neerc.chat.user.UserEntry;
import ru.ifmo.neerc.chat.user.UserRegistry;
import ru.ifmo.neerc.chat.utils.DebugUtils;
import ru.ifmo.neerc.chat.xmpp.provider.*;
import ru.ifmo.neerc.chat.xmpp.packet.*;
import ru.ifmo.neerc.task.Task;
import ru.ifmo.neerc.task.TaskStatus;
import ru.ifmo.neerc.task.TaskRegistry;
import ru.ifmo.neerc.utils.XmlUtils;

/**
 * @author <NAME>
 */
public class XmppChat extends AbstractChat {
    private static final Logger LOG = LoggerFactory.getLogger(XmppChat.class);

    private static final String SERVER_HOST = System.getProperty("server.host", "localhost");
    private static final String SERVER_HOSTNAME = System.getProperty("server.hostname", SERVER_HOST);
    private static final int SERVER_PORT = Integer.parseInt(System.getProperty("server.port", "5222"));
    private static final String ROOM_NAME = System.getProperty("room", "neerc");
    private static final String ROOM = ROOM_NAME + "@conference." + SERVER_HOSTNAME;
    private static final String NEERC_SERVICE = ROOM_NAME + "@neerc." + SERVER_HOSTNAME;

    private MultiUserChat muc;
    private AbstractXMPPConnection connection;
    
    private String name;
    private String password = System.getProperty("password", "<PASSWORD>");

    private ConnectionListener connectionListener;
    private Date lastActivity = null;

    public XmppChat(
            String name,
            ConnectionListener connectionListener
    ) {
        this.name = name;
        this.connectionListener = connectionListener;

        NeercTaskPacketExtensionProvider.register();
        NeercClockPacketExtensionProvider.register();
        NeercIQProvider.register();

        ReconnectionManager.setEnabledPerDefault(true);
        PingManager.setDefaultPingInterval(5);
    }

    public synchronized void disconnect() {
        if (connection != null) {
            connection.disconnect();
        }
    }
    
    public synchronized void connect() {
        disconnect();
        LOG.info("connecting to server");

        // Create the configuration for this new connection
        XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder()
            .setUsernameAndPassword(name, password)
            .setServiceName(SERVER_HOSTNAME)
            .setHost(SERVER_HOST)
            .setPort(SERVER_PORT)
            .setResource(StringUtils.randomString(10))
            .setCompressionEnabled(true);

        try {
            TLSUtils.acceptAllCertificates(builder);
            TLSUtils.disableHostnameVerificationForTlsCertificicates(builder);
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            LOG.error("Unable to configure connection", e);
        }

        XMPPTCPConnectionConfiguration config = builder.build();

        connection = new XMPPTCPConnection(config);
        connection.addConnectionListener(new MyConnectionListener());
        connection.addConnectionListener(connectionListener);
        connection.addAsyncStanzaListener(new TaskPacketListener(), new StanzaExtensionFilter(new NeercTaskPacketExtension()));

        muc = MultiUserChatManager.getInstanceFor(connection)
            .getMultiUserChat(ROOM);
        muc.addMessageListener(new MyMessageListener());
        muc.addParticipantListener(new MyPresenceListener());

        // Connect to the server
        try {
            connection.connect();
{'fraction_non_alphanumeric': 0.09298765904525017, 'fraction_numerical': 0.001530661054242801, 'mean_word_length': 4.185515873015873, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1689309', 'n_tokens_mistral': 3068, 'n_tokens_neox': 3026, 'n_words': 525}