<commit_before>/**
 * $Revision: $
 * $Date: $
 *
 * Copyright (C) 2006 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Lesser Public License (LGPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.sparkplugin;

import org.jivesoftware.Spark;
import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.jingle.JingleManager;
import org.jivesoftware.smackx.jingle.JingleSessionRequest;
import org.jivesoftware.smackx.jingle.OutgoingJingleSession;
import org.jivesoftware.smackx.jingle.listeners.JingleSessionRequestListener;
import org.jivesoftware.smackx.jingle.mediaimpl.jmf.JmfMediaManager;
import org.jivesoftware.smackx.jingle.mediaimpl.jspeex.SpeexMediaManager;
import org.jivesoftware.smackx.jingle.mediaimpl.multi.MultiMediaManager;
import org.jivesoftware.smackx.jingle.nat.BridgedTransportManager;
import org.jivesoftware.smackx.jingle.nat.ICETransportManager;
import org.jivesoftware.smackx.jingle.nat.JingleTransportManager;
import org.jivesoftware.smackx.jingle.nat.STUN;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.spark.PresenceManager;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.phone.Phone;
import org.jivesoftware.spark.phone.PhoneManager;
import org.jivesoftware.spark.plugin.Plugin;
import org.jivesoftware.spark.ui.ChatRoom;
import org.jivesoftware.spark.ui.TranscriptWindow;
import org.jivesoftware.spark.util.SwingWorker;
import org.jivesoftware.spark.util.log.Log;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * A simple Jingle Plugin for Spark that uses server Media Proxy for the transport and NAT Traversal
 */
public class JinglePlugin implements Plugin, Phone {

    private JingleManager jingleManager;

    private static final String JINGLE_NAMESPACE = "http://www.xmpp.org/extensions/xep-0166.html#ns";

    private Map<String, Boolean> jingleFeature = new HashMap<String, Boolean>();


    public void initialize() {
        // Add to PhoneManager
        PhoneManager.getInstance().addPhone(this);

        // Adds a tab handler.
        SparkManager.getChatManager().addSparkTabHandler(new JingleTabHandler());

        final SwingWorker jingleLoadingThread = new SwingWorker() {
            public Object construct() {

                String stunServer = "stun.xten.net";
                int stunPort = 3478;

                if (STUN.serviceAvailable(SparkManager.getConnection())) {
                    STUN stun = STUN.getSTUNServer(SparkManager.getConnection());
                    if (stun != null) {
                        List<STUN.StunServerAddress> servers = stun.getServers();
                        if(servers.size()>0){
                        stunServer = servers.get(0).getServer();
                        stunPort = Integer.parseInt(servers.get(0).getPort());
                        }
                    }
                }

                JingleTransportManager transportManager = new ICETransportManager(SparkManager.getConnection(), stunServer, stunPort);

                // if running on Windows use Direct Sound for better performance
                String locator = Spark.isWindows() ? "dsound://" : "javasound://";

                MultiMediaManager jingleMediaManager = new MultiMediaManager();
                jingleMediaManager.addMediaManager(new JmfMediaManager(locator));
                jingleMediaManager.addMediaManager(new SpeexMediaManager());

                if (System.getProperty("codec") != null) {
                    try {
                        int codec = Integer.parseInt(System.getProperty("codec"));
                        jingleMediaManager.setPreferredPayloadType(jingleMediaManager.getPayloads().get(codec));
                    }
                    catch (NumberFormatException e) {
                        // Do Nothing
                    }
                }

                jingleManager = new JingleManager(SparkManager.getConnection(), transportManager, jingleMediaManager);

                if (transportManager instanceof BridgedTransportManager) {
                    jingleManager.addCreationListener((BridgedTransportManager)transportManager);
                }

                // Add Jingle to discovered items list.
                SparkManager.addFeature(JINGLE_NAMESPACE);

                return true;
            }

            public void finished() {
                addListeners();
            }
        };

        jingleLoadingThread.start();
    }


    /**
     * Adds Jingle and ChatRoom listeners.
     */
    private void addListeners() {
        if (jingleManager == null) {
            Log.error("Unable to resolve Jingle Connection");
            return;
        }

        // Listen in for new incoming Jingle requests.
        jingleManager.addJingleSessionRequestListener(new JingleSessionRequestListener() {
            public void sessionRequested(final JingleSessionRequest request) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        incomingJingleSession(request);
                    }
                });
            }
        });
    }


    public Collection<Action> getPhoneActions(final String jid) {
        if (jingleManager == null) {
            return Collections.emptyList();
        }

        Boolean supportsJingle = true;// jingleFeature.get(StringUtils.parseBareAddress(jid));
        if (supportsJingle == null) {
            // Disco for event.
            // Obtain the ServiceDiscoveryManager associated with my XMPPConnection
            ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(SparkManager.getConnection());

            String fullJID = PresenceManager.getFullyQualifiedJID(jid);

            // Get the items of a given XMPP entity
            // This example gets the items associated with online catalog service
            DiscoverInfo discoverInfo = null;
            try {
                discoverInfo = discoManager.discoverInfo(fullJID);
            }
            catch (XMPPException e) {
                Log.error(e);
            }

            // Get the discovered items of the queried XMPP entity
            supportsJingle = discoverInfo.containsFeature(JINGLE_NAMESPACE);
            jingleFeature.put(jid, supportsJingle);
        }

        if (!supportsJingle) {
            return Collections.emptyList();
        }

        final List<Action> actions = new ArrayList<Action>();
        Action action = new AbstractAction() {<commit_msg>Change discovery in jingle plugin.<commit_after>/**
 * $Revision: $
 * $Date: $
 *
 * Copyright (C) 2006 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Lesser Public License (LGPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.sparkplugin;

import org.jivesoftware.Spark;
import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.jingle.JingleManager;
import org.jivesoftware.smackx.jingle.JingleSessionRequest;
import org.jivesoftware.smackx.jingle.OutgoingJingleSession;
import org.jivesoftware.smackx.jingle.listeners.JingleSessionRequestListener;
import org.jivesoftware.smackx.jingle.mediaimpl.jmf.JmfMediaManager;
import org.jivesoftware.smackx.jingle.mediaimpl.jspeex.SpeexMediaManager;
import org.jivesoftware.smackx.jingle.mediaimpl.multi.MultiMediaManager;
import org.jivesoftware.smackx.jingle.nat.BridgedTransportManager;
import org.jivesoftware.smackx.jingle.nat.ICETransportManager;
import org.jivesoftware.smackx.jingle.nat.JingleTransportManager;
import org.jivesoftware.smackx.jingle.nat.STUN;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.spark.PresenceManager;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.phone.Phone;
import org.jivesoftware.spark.phone.PhoneManager;
import org.jivesoftware.spark.plugin.Plugin;
import org.jivesoftware.spark.ui.ChatRoom;
import org.jivesoftware.spark.ui.TranscriptWindow;
import org.jivesoftware.spark.util.SwingWorker;
import org.jivesoftware.spark.util.log.Log;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * A simple Jingle Plugin for Spark that uses server Media Proxy for the transport and NAT Traversal
 */
public class JinglePlugin implements Plugin, Phone {

    private JingleManager jingleManager;

    private static final String JINGLE_NAMESPACE = "http://www.xmpp.org/extensions/xep-0166.html#ns";

    private Map<String, Boolean> jingleFeature = new HashMap<String, Boolean>();


    public void initialize() {
        // Add to PhoneManager
        PhoneManager.getInstance().addPhone(this);

        // Adds a tab handler.
        SparkManager.getChatManager().addSparkTabHandler(new JingleTabHandler());

        final SwingWorker jingleLoadingThread = new SwingWorker() {
            public Object construct() {

                String stunServer = "stun.xten.net";
                int stunPort = 3478;

                if (STUN.serviceAvailable(SparkManager.getConnection())) {
                    STUN stun = STUN.getSTUNServer(SparkManager.getConnection());
                    if (stun != null) {
                        List<STUN.StunServerAddress> servers = stun.getServers();
                        if(servers.size()>0){
                        stunServer = servers.get(0).getServer();
                        stunPort = Integer.parseInt(servers.get(0).getPort());
                        }
                    }
                }

                JingleTransportManager transportManager = new ICETransportManager(SparkManager.getConnection(), stunServer, stunPort);

                // if running on Windows use Direct Sound for better performance
                String locator = Spark.isWindows() ? "dsound://" : "javasound://";

                MultiMediaManager jingleMediaManager = new MultiMediaManager();
                jingleMediaManager.addMediaManager(new JmfMediaManager(locator));
                jingleMediaManager.addMediaManager(new SpeexMediaManager());

                if (System.getProperty("codec") != null) {
                    try {
                        int codec = Integer.parseInt(System.getProperty("codec"));
                        jingleMediaManager.setPreferredPayloadType(jingleMediaManager.getPayloads().get(codec));
                    }
                    catch (NumberFormatException e) {
                        // Do Nothing
                    }
                }

                jingleManager = new JingleManager(SparkManager.getConnection(), transportManager, jingleMediaManager);

                if (transportManager instanceof BridgedTransportManager) {
                    jingleManager.addCreationListener((BridgedTransportManager)transportManager);
                }

                // Add Jingle to discovered items list.
                SparkManager.addFeature(JINGLE_NAMESPACE);

                return true;
            }

            public void finished() {
                addListeners();
            }
        };

        jingleLoadingThread.start();
    }


    /**
     * Adds Jingle and ChatRoom listeners.
     */
    private void addListeners() {
        if (jingleManager == null) {
            Log.error("Unable to resolve Jingle Connection");
            return;
        }

        // Listen in for new incoming Jingle requests.
        jingleManager.addJingleSessionRequestListener(new JingleSessionRequestListener() {
            public void sessionRequested(final JingleSessionRequest request) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        incomingJingleSession(request);
                    }
                });
            }
        });
    }


    public Collection<Action> getPhoneActions(final String jid) {
        if (jingleManager == null) {
            return Collections.emptyList();
        }

        Boolean supportsJingle = jingleFeature.get(StringUtils.parseBareAddress(jid));
        if (supportsJingle == null) {
            // Disco for event.
            // Obtain the ServiceDiscoveryManager associated with my XMPPConnection
            ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(SparkManager.getConnection());

            String fullJID = PresenceManager.getFullyQualifiedJID(jid);

            // Get the items of a given XMPP entity
            // This example gets the items associated with online catalog service
            DiscoverInfo discoverInfo = null;
            try {
                discoverInfo = discoManager.discoverInfo(fullJID);
            }
            catch (XMPPException e) {
                Log.error(e);
            }

            // Get the discovered items of the queried XMPP entity
            supportsJingle = discoverInfo.containsFeature(JINGLE_NAMESPACE);
            jingleFeature.put(jid, supportsJingle);
        }

        if (!supportsJingle) {
            return Collections.emptyList();
        }

        final List<Action> actions = new ArrayList<Action>();
        Action action = new AbstractAction() {
{'fraction_non_alphanumeric': 0.08762490392006149, 'fraction_numerical': 0.002096289567465586, 'mean_word_length': 2.4695757575757575, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5760441', 'n_tokens_mistral': 3931, 'n_tokens_neox': 3770, 'n_words': 779}