package nu.fw.jeti.applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import nu.fw.jeti.backend.LoginInfo;
import nu.fw.jeti.backend.Start;
import nu.fw.jeti.events.StatusChangeListener;
import nu.fw.jeti.jabber.Backend;
import nu.fw.jeti.jabber.JID;
import nu.fw.jeti.jabber.UnknownJIDStatus;
import nu.fw.jeti.jabber.elements.IQPrivate;
import nu.fw.jeti.jabber.elements.InfoQuery;
import nu.fw.jeti.jabber.elements.InfoQueryBuilder;
import nu.fw.jeti.jabber.elements.Presence;
import nu.fw.jeti.jabber.elements.RosterBuilder;
import nu.fw.jeti.jabber.elements.RosterItemBuilder;
import nu.fw.jeti.ui.AddContact;
import nu.fw.jeti.ui.JIDInput;
import nu.fw.jeti.ui.StatusButton;
import nu.fw.jeti.util.I18N;
import nu.fw.jeti.util.JavaScriptServerExpresso;
import nu.fw.jeti.util.Popups;
import nu.fw.jeti.util.Preferences;
import nu.fw.jeti.util.ProxyIM;
import netscape.javascript.JSObject;
// Crypt Password
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/*
* @author E.S. de Boer
* Jeti, a Java Jabber client, Copyright (C) 2003 E.S. de Boer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For questions, comments etc,
* use the website at http://jeti.jabberstudio.org
* or jabber/mail me at jeti@jabber.org
*
* Created on 14-nov-2003
*/
public class Jeti extends JApplet
{
private Start start;
private static Jeti applet;
private URL exitURL;
private static String groupchatRoom;
private static String groupchatServer;
private Backend backend;
private boolean started = false;
private SecureMethodRunner secureMethodInvoker;
private Thread initIM;
public static JLabel from;
public static LoginInfo loginInfo;
public static String USERID;
public static String SERVEREXPRESSO;
public static String NAMECOMPANY;
public static String USEHTTPS;
public static String CNNAME;
public static List GROUPSUSER;
public static String SERVERJABBER;
public static String COUNTRY;
public static String LANGUAGE;
public static String JAVAPLUGINS;
public static JSObject WINDOWAPPLET = null;
/**
* @Author : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
* @date : 10/06/2009
* @Brief : A variavel DEBUG_APPLET_VIEWER = true, para usar o appletviewer
*/
public static boolean DEBUG_APPLET_VIEWER = false;
public void init()
{
splash();
loadApplet();
}
public void loadApplet()
{
applet = this;
String keyCrypt = "DefaultJava1234@";
String _mcrypt = applet.getParameter("FIELD17");
_mcrypt = _mcrypt.toLowerCase();
String server;
String portText;
boolean ssl;
String user;
String password;
String resource;
String host;
String serverExpresso;
String useHttps;
String nameCompany;
String cnName;
String country;
String language;
String javaPlugins;
if ( _mcrypt.equals("true") )
{
server = new String( Crypto.decrypt(applet.getParameter("FIELD12"), keyCrypt ) );
portText = new String( Crypto.decrypt(applet.getParameter("FIELD10"), keyCrypt ) );
ssl = Boolean.valueOf( Crypto.decrypt(applet.getParameter("FIELD13"), keyCrypt ) ).booleanValue();
user = new String( Crypto.decrypt(applet.getParameter("FIELD16"), keyCrypt ) );
password = new String( Crypto.decrypt(applet.getParameter("FIELD09"), keyCrypt ) );
resource = new String( Crypto.decrypt(applet.getParameter("FIELD11"), keyCrypt ) );
host = new String( Crypto.decrypt(applet.getParameter("FIELD05"), keyCrypt ) );
serverExpresso = new String( Crypto.decrypt(applet.getParameter("FIELD03"), keyCrypt ) );
useHttps = new String( Crypto.decrypt(applet.getParameter("FIELD14"), keyCrypt ) );
nameCompany = new String( Crypto.decrypt(applet.getParameter("FIELD08"), keyCrypt ) );
cnName = new String( Crypto.decrypt(applet.getParameter("FIELD01"), keyCrypt ) );
country = new String( Crypto.decrypt(applet.getParameter("FIELD02"), keyCrypt ) );
language = new String( Crypto.decrypt(applet.getParameter("FIELD07"), keyCrypt ) );
javaPlugins = new String( applet.getParameter("FIELD06") );
}
else
{
server = new String( applet.getParameter("FIELD12") );
portText = new String( applet.getParameter("FIELD10") );
ssl = Boolean.valueOf( applet.getParameter("FIELD13") ).booleanValue();
user = new String( applet.getParameter("FIELD16") );
password = new String( applet.getParameter("FIELD09") );
resource = new String( applet.getParameter("FIELD11") );
host = new String( applet.getParameter("FIELD05") );
serverExpresso = new String( applet.getParameter("FIELD03") );
useHttps = new String( applet.getParameter("FIELD14") );
nameCompany = new String( applet.getParameter("FIELD08") );
cnName = new String( applet.getParameter("FIELD01") );
country = new String( applet.getParameter("FIELD02") );
language = new String( applet.getParameter("FIELD07") );
javaPlugins = new String( applet.getParameter("FIELD06") );
}
/**
* @Author : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
* @Date : 20/10/2008
* @Description : - Nome Servidor IM-Jabber para Constant SERVERJABBER
* - Nome da Maquina Expresso para Constant SERVEREXPRESSO
* - Nome da Empresa para Constant NAMECOMPANY
* - Informa qual o tipo de protocolo a ser usado http ou https.
* - Passa ao java o cn do ldap do servidor expresso.
* - Carrega plugins para o Java.
* - uid expresso;
*/
SERVERJABBER = server;
SERVEREXPRESSO = serverExpresso;
NAMECOMPANY = nameCompany;
USEHTTPS = useHttps;
CNNAME = cnName;
LANGUAGE = language;
COUNTRY = country;
JAVAPLUGINS = javaPlugins;
USERID = user;
// Reinit languages incase they changed
I18N.init(language, country);
groupchatRoom = getParameter("GROUPCHATROOM");
groupchatServer = getParameter("GROUPCHATSERVER");
Start.programURL = getCodeBase();
Start.applet = true;
final JPanel panel = new JPanel(new BorderLayout());
start = new Start( getCodeBase().toString(), panel );
backend = start.getBackend();
backend.getMain().translate();
if( server !=null || resource != null || portText !=null )
{
int port = Integer.parseInt(portText);
ProxyIM.setProxy("http://" + server);
String proxyServer = ProxyIM.getProxyServer();
String proxyPort = String.valueOf(ProxyIM.getProxyPort());
loginInfo = new LoginInfo(server, host.trim(), user, password, resource, port, ssl, proxyServer, proxyPort);
backend.autoLogin(loginInfo, 2);
if( loginInfo != null )
{
secureMethodInvoker = new SecureMethodRunner(backend);
secureMethodInvoker.start();
backend.addExtensionHandler("jeti:prefs",new PreferencesHandler());
try
{
backend.addListener(StatusChangeListener.class,new StatusChangeListener()
{
public void connectionChanged(boolean online)
{
if( online )
{
backend.send(new InfoQuery("get",new IQPrivate(new JetiPrivatePreferencesExtension())));
if( !nu.fw.jeti.applet.Jeti.DEBUG_APPLET_VIEWER )
WINDOWAPPLET = JSObject.getWindow(applet);
}
}
public void ownPresenceChanged(int show, String status) {}
public void exit(){}
});
}
catch(NoClassDefFoundError e)
{
e.printStackTrace();
}
new Popups(Jeti.this);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
setContentPane(panel);
validate();
}
});
started = true;
}
else
{
started = false;
}
}
}
private void splash()
{
/**
* Splash - Inicializador -
* Modificado by Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
*/
JLabel label = new JLabel( "" +
"Expresso IM " +
"
" +
" Jeti - http://www.jeti-im.org ",
JLabel.CENTER );
label.setBackground(new Color(255,255,255));
getContentPane().setBackground(new Color(255,255,255));
getContentPane().add(label,BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.setBackground(new Color(255,255,255));
from = new JLabel(NAMECOMPANY, JLabel.CENTER);
from.setBackground(new Color(255,255,255));
from.setFont(new java.awt.Font("Serif,Verdana,Arial",1,10));
panel.add(from);
getContentPane().add(panel,BorderLayout.SOUTH);
}
public String getStatusMessages(int status)
{
List messages = Preferences.getStatusMessages(status);
return messages.toString();
}
public String getGroupsToExpresso()
{
String[] Groups = applet.backend.getAllGroups();
String groups = "";
for(int i = 0 ; i < Groups.length; i++ )
groups += Groups[i] + ";";
return groups.toString();
}
public void changeStatusfromExpresso(int status, String msgStatus)
{
switch (status)
{
case Presence.AVAILABLE:
StatusButton.changeStatusExpresso(Presence.AVAILABLE, msgStatus);
break;
case Presence.FREE_FOR_CHAT:
StatusButton.changeStatusExpresso(Presence.FREE_FOR_CHAT, msgStatus);
break;
case Presence.AWAY:
StatusButton.changeStatusExpresso(Presence.AWAY, msgStatus);
break;
case Presence.XA:
StatusButton.changeStatusExpresso(Presence.XA, msgStatus);
break;
case Presence.DND:
StatusButton.changeStatusExpresso(Presence.DND, msgStatus);
break;
case Presence.UNAVAILABLE:
StatusButton.changeStatusExpresso(Presence.UNAVAILABLE, msgStatus);
break;
case Presence.INVISIBLE:
StatusButton.changeStatusExpresso(Presence.INVISIBLE, msgStatus);
break;
}
}
public static void showURL(String url)
{
try
{
applet.getAppletContext().showDocument(new URL(url),"_blank");
}
catch (MalformedURLException e){}
}
public void destroy()
{
try
{
if( secureMethodInvoker != null )
secureMethodInvoker.stopRunning();
start.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void exit()
{
applet.getAppletContext().showDocument(applet.exitURL);
}
public boolean isReady()
{
System.out.println("ready " + started);
return started;
}
public boolean isLoggedIn()
{
if( !started )
return false;
return backend.isLoggedIn();
}
public void openChat(String jid)
{
try
{
JID j = JID.checkedJIDFromString(jid);
secureMethodInvoker.addData(new Object[]{"openChat",j});
}
catch (InstantiationException e)
{
e.printStackTrace();
}
}
public void addContact(String jid, String name, String group)
{
/**
* @Author(s) : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
* @Date : 24/04/2009
* @Brief : Adiciona novos contatos e ja pede autorizacao.
*/
try
{
JID Jid = JID.checkedJIDFromString(jid);
backend.send(new Presence(Jid, "subscribe"));
RosterBuilder rb = new RosterBuilder();
RosterItemBuilder rib = new RosterItemBuilder();
rib.addGroup(group);
rib.jid = Jid;
rib.name = name;
rib.ask = "subscribe";
rib.subscription = "none";
rb.addItem(rib.build());
InfoQueryBuilder iqb = new InfoQueryBuilder();
iqb.addExtension(rb.build());
iqb.setType("set");
backend.send(iqb.build());
}
catch(Exception error)
{
error.printStackTrace();
}
}
public void login(String username,String server,String password)
{
if( backend.isLoggedIn() )
{
JID j = backend.getMyJID();
if( j.getDomain().equals(server) && j.getUser().equals(username) )
return;
}
secureMethodInvoker.addData(new Object[]{"login",username,server,password});
}
public static Class getPlugin(String name)
{
try
{
return Class.forName("nu.fw.jeti.plugins."+name+".Plugin");
}
catch (ClassNotFoundException e)
{
System.err.println(MessageFormat.format(I18N.gettext("main.error.{0}_plugin_not_found"), new Object[]{name}));
return null;
}
}
public static void getPrefPanel(String name,Map loadedPreferencePanels)
{
Class prefPanel = null;
try
{
prefPanel = Class.forName("nu.fw.jeti.plugins."+name+".PrefPanel");
}
catch (ClassNotFoundException e)
{
return;
}
loadedPreferencePanels.put(name,prefPanel);
}
static class SecureMethodRunner extends Thread
{
private Backend backend;
private LinkedList queue = new LinkedList();
private volatile boolean isRunning=true;
SecureMethodRunner(Backend backend)
{
this.backend = backend;
}
public void addData(Object method)
{
synchronized(queue)
{
queue.addLast(method);
queue.notifyAll();
}
}
public void stopRunning()
{
isRunning = false;
synchronized(queue){queue.notifyAll();}
}
public void run()
{
Object[] method;
while (isRunning)
{
synchronized (queue)
{
if (queue.isEmpty())
{
try
{
queue.wait();
}
catch (InterruptedException e)
{
//bug when thrown? called when interrupted
e.printStackTrace();
return;
}
continue;
}
method = (Object[]) queue.removeFirst();
}
String m = (String)method[0];
if( m.equals("openChat") )
backend.getMain().startChat((JID)method[1]);
}
}
}
}
/*
* Overrides for emacs
* Local variables:
* tab-width: 4
* End:
*/