1 | package nu.fw.jeti.applet; |
---|
2 | |
---|
3 | import java.awt.BorderLayout; |
---|
4 | import java.awt.Color; |
---|
5 | import java.net.MalformedURLException; |
---|
6 | import java.net.URL; |
---|
7 | import java.text.MessageFormat; |
---|
8 | import java.util.LinkedList; |
---|
9 | import java.util.List; |
---|
10 | import java.util.Map; |
---|
11 | |
---|
12 | import javax.swing.JApplet; |
---|
13 | import javax.swing.JLabel; |
---|
14 | import javax.swing.JPanel; |
---|
15 | import javax.swing.SwingUtilities; |
---|
16 | import nu.fw.jeti.backend.LoginInfo; |
---|
17 | import nu.fw.jeti.backend.Start; |
---|
18 | import nu.fw.jeti.events.StatusChangeListener; |
---|
19 | import nu.fw.jeti.jabber.Backend; |
---|
20 | import nu.fw.jeti.jabber.JID; |
---|
21 | import nu.fw.jeti.jabber.UnknownJIDStatus; |
---|
22 | import nu.fw.jeti.jabber.elements.IQPrivate; |
---|
23 | import nu.fw.jeti.jabber.elements.InfoQuery; |
---|
24 | import nu.fw.jeti.jabber.elements.InfoQueryBuilder; |
---|
25 | import nu.fw.jeti.jabber.elements.Presence; |
---|
26 | import nu.fw.jeti.jabber.elements.RosterBuilder; |
---|
27 | import nu.fw.jeti.jabber.elements.RosterItemBuilder; |
---|
28 | import nu.fw.jeti.ui.AddContact; |
---|
29 | import nu.fw.jeti.ui.JIDInput; |
---|
30 | import nu.fw.jeti.ui.StatusButton; |
---|
31 | import nu.fw.jeti.util.I18N; |
---|
32 | import nu.fw.jeti.util.JavaScriptServerExpresso; |
---|
33 | import nu.fw.jeti.util.Popups; |
---|
34 | import nu.fw.jeti.util.Preferences; |
---|
35 | import nu.fw.jeti.util.ProxyIM; |
---|
36 | |
---|
37 | import netscape.javascript.JSObject; |
---|
38 | |
---|
39 | // Crypt Password |
---|
40 | import javax.crypto.Cipher; |
---|
41 | import javax.crypto.spec.SecretKeySpec; |
---|
42 | |
---|
43 | /* |
---|
44 | * @author E.S. de Boer |
---|
45 | * Jeti, a Java Jabber client, Copyright (C) 2003 E.S. de Boer |
---|
46 | * |
---|
47 | * This program is free software; you can redistribute it and/or modify |
---|
48 | * it under the terms of the GNU General Public License as published by |
---|
49 | * the Free Software Foundation; either version 2 of the License, or |
---|
50 | * (at your option) any later version. |
---|
51 | * |
---|
52 | * This program is distributed in the hope that it will be useful, |
---|
53 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
54 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
55 | * GNU General Public License for more details. |
---|
56 | * |
---|
57 | * You should have received a copy of the GNU General Public License |
---|
58 | * along with this program; if not, write to the Free Software |
---|
59 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
60 | * |
---|
61 | * For questions, comments etc, |
---|
62 | * use the website at http://jeti.jabberstudio.org |
---|
63 | * or jabber/mail me at jeti@jabber.org |
---|
64 | * |
---|
65 | * Created on 14-nov-2003 |
---|
66 | */ |
---|
67 | |
---|
68 | public class Jeti extends JApplet |
---|
69 | { |
---|
70 | private Start start; |
---|
71 | private static Jeti applet; |
---|
72 | private URL exitURL; |
---|
73 | private static String groupchatRoom; |
---|
74 | private static String groupchatServer; |
---|
75 | private Backend backend; |
---|
76 | private boolean started = false; |
---|
77 | private SecureMethodRunner secureMethodInvoker; |
---|
78 | private Thread initIM; |
---|
79 | public static JLabel from; |
---|
80 | public static LoginInfo loginInfo; |
---|
81 | public static String USERID; |
---|
82 | public static String SERVEREXPRESSO; |
---|
83 | public static String NAMECOMPANY; |
---|
84 | public static String USEHTTPS; |
---|
85 | public static String CNNAME; |
---|
86 | public static List GROUPSUSER; |
---|
87 | public static String SERVERJABBER; |
---|
88 | public static String COUNTRY; |
---|
89 | public static String LANGUAGE; |
---|
90 | public static String JAVAPLUGINS; |
---|
91 | |
---|
92 | public static JSObject WINDOWAPPLET = null; |
---|
93 | |
---|
94 | /** |
---|
95 | * @Author : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br |
---|
96 | * @date : 10/06/2009 |
---|
97 | * @Brief : A variavel DEBUG_APPLET_VIEWER = true, para usar o appletviewer |
---|
98 | */ |
---|
99 | public static boolean DEBUG_APPLET_VIEWER = false; |
---|
100 | |
---|
101 | public void init() |
---|
102 | { |
---|
103 | splash(); |
---|
104 | loadApplet(); |
---|
105 | } |
---|
106 | |
---|
107 | public void loadApplet() |
---|
108 | { |
---|
109 | applet = this; |
---|
110 | String keyCrypt = "DefaultJava1234@"; |
---|
111 | |
---|
112 | String _mcrypt = applet.getParameter("FIELD17"); |
---|
113 | _mcrypt = _mcrypt.toLowerCase(); |
---|
114 | |
---|
115 | String server; |
---|
116 | String portText; |
---|
117 | boolean ssl; |
---|
118 | String user; |
---|
119 | String password; |
---|
120 | String resource; |
---|
121 | String host; |
---|
122 | String serverExpresso; |
---|
123 | String useHttps; |
---|
124 | String nameCompany; |
---|
125 | String cnName; |
---|
126 | String country; |
---|
127 | String language; |
---|
128 | String javaPlugins; |
---|
129 | |
---|
130 | if ( _mcrypt.equals("true") ) |
---|
131 | { |
---|
132 | server = new String( Crypto.decrypt(applet.getParameter("FIELD12"), keyCrypt ) ); |
---|
133 | portText = new String( Crypto.decrypt(applet.getParameter("FIELD10"), keyCrypt ) ); |
---|
134 | ssl = Boolean.valueOf( Crypto.decrypt(applet.getParameter("FIELD13"), keyCrypt ) ).booleanValue(); |
---|
135 | user = new String( Crypto.decrypt(applet.getParameter("FIELD16"), keyCrypt ) ); |
---|
136 | password = new String( Crypto.decrypt(applet.getParameter("FIELD09"), keyCrypt ) ); |
---|
137 | resource = new String( Crypto.decrypt(applet.getParameter("FIELD11"), keyCrypt ) ); |
---|
138 | host = new String( Crypto.decrypt(applet.getParameter("FIELD05"), keyCrypt ) ); |
---|
139 | serverExpresso = new String( Crypto.decrypt(applet.getParameter("FIELD03"), keyCrypt ) ); |
---|
140 | useHttps = new String( Crypto.decrypt(applet.getParameter("FIELD14"), keyCrypt ) ); |
---|
141 | nameCompany = new String( Crypto.decrypt(applet.getParameter("FIELD08"), keyCrypt ) ); |
---|
142 | cnName = new String( Crypto.decrypt(applet.getParameter("FIELD01"), keyCrypt ) ); |
---|
143 | country = new String( Crypto.decrypt(applet.getParameter("FIELD02"), keyCrypt ) ); |
---|
144 | language = new String( Crypto.decrypt(applet.getParameter("FIELD07"), keyCrypt ) ); |
---|
145 | javaPlugins = new String( applet.getParameter("FIELD06") ); |
---|
146 | } |
---|
147 | else |
---|
148 | { |
---|
149 | server = new String( applet.getParameter("FIELD12") ); |
---|
150 | portText = new String( applet.getParameter("FIELD10") ); |
---|
151 | ssl = Boolean.valueOf( applet.getParameter("FIELD13") ).booleanValue(); |
---|
152 | user = new String( applet.getParameter("FIELD16") ); |
---|
153 | password = new String( applet.getParameter("FIELD09") ); |
---|
154 | resource = new String( applet.getParameter("FIELD11") ); |
---|
155 | host = new String( applet.getParameter("FIELD05") ); |
---|
156 | serverExpresso = new String( applet.getParameter("FIELD03") ); |
---|
157 | useHttps = new String( applet.getParameter("FIELD14") ); |
---|
158 | nameCompany = new String( applet.getParameter("FIELD08") ); |
---|
159 | cnName = new String( applet.getParameter("FIELD01") ); |
---|
160 | country = new String( applet.getParameter("FIELD02") ); |
---|
161 | language = new String( applet.getParameter("FIELD07") ); |
---|
162 | javaPlugins = new String( applet.getParameter("FIELD06") ); |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * @Author : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br |
---|
167 | * @Date : 20/10/2008 |
---|
168 | * @Description : - Nome Servidor IM-Jabber para Constant SERVERJABBER |
---|
169 | * - Nome da Maquina Expresso para Constant SERVEREXPRESSO |
---|
170 | * - Nome da Empresa para Constant NAMECOMPANY |
---|
171 | * - Informa qual o tipo de protocolo a ser usado http ou https. |
---|
172 | * - Passa ao java o cn do ldap do servidor expresso. |
---|
173 | * - Carrega plugins para o Java. |
---|
174 | * - uid expresso; |
---|
175 | */ |
---|
176 | |
---|
177 | SERVERJABBER = server; |
---|
178 | SERVEREXPRESSO = serverExpresso; |
---|
179 | NAMECOMPANY = nameCompany; |
---|
180 | USEHTTPS = useHttps; |
---|
181 | CNNAME = cnName; |
---|
182 | LANGUAGE = language; |
---|
183 | COUNTRY = country; |
---|
184 | JAVAPLUGINS = javaPlugins; |
---|
185 | USERID = user; |
---|
186 | |
---|
187 | // Reinit languages incase they changed |
---|
188 | I18N.init(language, country); |
---|
189 | |
---|
190 | groupchatRoom = getParameter("GROUPCHATROOM"); |
---|
191 | groupchatServer = getParameter("GROUPCHATSERVER"); |
---|
192 | |
---|
193 | Start.programURL = getCodeBase(); |
---|
194 | Start.applet = true; |
---|
195 | final JPanel panel = new JPanel(new BorderLayout()); |
---|
196 | start = new Start( getCodeBase().toString(), panel ); |
---|
197 | |
---|
198 | backend = start.getBackend(); |
---|
199 | backend.getMain().translate(); |
---|
200 | |
---|
201 | if( server !=null || resource != null || portText !=null ) |
---|
202 | { |
---|
203 | int port = Integer.parseInt(portText); |
---|
204 | |
---|
205 | ProxyIM.setProxy("http://" + server); |
---|
206 | String proxyServer = ProxyIM.getProxyServer(); |
---|
207 | String proxyPort = String.valueOf(ProxyIM.getProxyPort()); |
---|
208 | |
---|
209 | loginInfo = new LoginInfo(server, host.trim(), user, password, resource, port, ssl, proxyServer, proxyPort); |
---|
210 | backend.autoLogin(loginInfo, 2); |
---|
211 | |
---|
212 | if( loginInfo != null ) |
---|
213 | { |
---|
214 | secureMethodInvoker = new SecureMethodRunner(backend); |
---|
215 | secureMethodInvoker.start(); |
---|
216 | |
---|
217 | backend.addExtensionHandler("jeti:prefs",new PreferencesHandler()); |
---|
218 | try |
---|
219 | { |
---|
220 | backend.addListener(StatusChangeListener.class,new StatusChangeListener() |
---|
221 | { |
---|
222 | public void connectionChanged(boolean online) |
---|
223 | { |
---|
224 | if( online ) |
---|
225 | { |
---|
226 | backend.send(new InfoQuery("get",new IQPrivate(new JetiPrivatePreferencesExtension()))); |
---|
227 | |
---|
228 | if( !nu.fw.jeti.applet.Jeti.DEBUG_APPLET_VIEWER ) |
---|
229 | WINDOWAPPLET = JSObject.getWindow(applet); |
---|
230 | } |
---|
231 | } |
---|
232 | public void ownPresenceChanged(int show, String status) {} |
---|
233 | public void exit(){} |
---|
234 | }); |
---|
235 | } |
---|
236 | catch(NoClassDefFoundError e) |
---|
237 | { |
---|
238 | e.printStackTrace(); |
---|
239 | } |
---|
240 | |
---|
241 | new Popups(Jeti.this); |
---|
242 | SwingUtilities.invokeLater(new Runnable() |
---|
243 | { |
---|
244 | public void run() |
---|
245 | { |
---|
246 | setContentPane(panel); |
---|
247 | validate(); |
---|
248 | } |
---|
249 | }); |
---|
250 | started = true; |
---|
251 | } |
---|
252 | else |
---|
253 | { |
---|
254 | started = false; |
---|
255 | } |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | private void splash() |
---|
260 | { |
---|
261 | /** |
---|
262 | * Splash - Inicializador - |
---|
263 | * Modificado by Alexandre Correia - alexandrecorreia@celepar.pr.gov.br |
---|
264 | */ |
---|
265 | |
---|
266 | JLabel label = new JLabel( "<html>" + |
---|
267 | "<span style='margin-left:10px;font:20pt Verdana,Arial,serif'>Expresso IM </span>" + |
---|
268 | "<br/>" + |
---|
269 | "<span style='font:9pt Verdana,Arial,serif'> Jeti - http://www.jeti-im.org </span></html>", |
---|
270 | JLabel.CENTER ); |
---|
271 | |
---|
272 | label.setBackground(new Color(255,255,255)); |
---|
273 | getContentPane().setBackground(new Color(255,255,255)); |
---|
274 | getContentPane().add(label,BorderLayout.CENTER); |
---|
275 | |
---|
276 | JPanel panel = new JPanel(); |
---|
277 | panel.setBackground(new Color(255,255,255)); |
---|
278 | from = new JLabel(NAMECOMPANY, JLabel.CENTER); |
---|
279 | from.setBackground(new Color(255,255,255)); |
---|
280 | from.setFont(new java.awt.Font("Serif,Verdana,Arial",1,10)); |
---|
281 | panel.add(from); |
---|
282 | getContentPane().add(panel,BorderLayout.SOUTH); |
---|
283 | } |
---|
284 | |
---|
285 | public String getStatusMessages(int status) |
---|
286 | { |
---|
287 | List messages = Preferences.getStatusMessages(status); |
---|
288 | return messages.toString(); |
---|
289 | } |
---|
290 | |
---|
291 | public String getGroupsToExpresso() |
---|
292 | { |
---|
293 | String[] Groups = applet.backend.getAllGroups(); |
---|
294 | String groups = ""; |
---|
295 | |
---|
296 | for(int i = 0 ; i < Groups.length; i++ ) |
---|
297 | groups += Groups[i] + ";"; |
---|
298 | |
---|
299 | return groups.toString(); |
---|
300 | } |
---|
301 | |
---|
302 | public void changeStatusfromExpresso(int status, String msgStatus) |
---|
303 | { |
---|
304 | switch (status) |
---|
305 | { |
---|
306 | case Presence.AVAILABLE: |
---|
307 | StatusButton.changeStatusExpresso(Presence.AVAILABLE, msgStatus); |
---|
308 | break; |
---|
309 | case Presence.FREE_FOR_CHAT: |
---|
310 | StatusButton.changeStatusExpresso(Presence.FREE_FOR_CHAT, msgStatus); |
---|
311 | break; |
---|
312 | case Presence.AWAY: |
---|
313 | StatusButton.changeStatusExpresso(Presence.AWAY, msgStatus); |
---|
314 | break; |
---|
315 | case Presence.XA: |
---|
316 | StatusButton.changeStatusExpresso(Presence.XA, msgStatus); |
---|
317 | break; |
---|
318 | case Presence.DND: |
---|
319 | StatusButton.changeStatusExpresso(Presence.DND, msgStatus); |
---|
320 | break; |
---|
321 | case Presence.UNAVAILABLE: |
---|
322 | StatusButton.changeStatusExpresso(Presence.UNAVAILABLE, msgStatus); |
---|
323 | break; |
---|
324 | case Presence.INVISIBLE: |
---|
325 | StatusButton.changeStatusExpresso(Presence.INVISIBLE, msgStatus); |
---|
326 | break; |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | public static void showURL(String url) |
---|
331 | { |
---|
332 | try |
---|
333 | { |
---|
334 | applet.getAppletContext().showDocument(new URL(url),"_blank"); |
---|
335 | } |
---|
336 | catch (MalformedURLException e){} |
---|
337 | } |
---|
338 | |
---|
339 | public void destroy() |
---|
340 | { |
---|
341 | try |
---|
342 | { |
---|
343 | if( secureMethodInvoker != null ) |
---|
344 | secureMethodInvoker.stopRunning(); |
---|
345 | start.close(); |
---|
346 | } |
---|
347 | catch (Exception e) |
---|
348 | { |
---|
349 | e.printStackTrace(); |
---|
350 | } |
---|
351 | } |
---|
352 | |
---|
353 | public static void exit() |
---|
354 | { |
---|
355 | applet.getAppletContext().showDocument(applet.exitURL); |
---|
356 | } |
---|
357 | |
---|
358 | public boolean isReady() |
---|
359 | { |
---|
360 | System.out.println("ready " + started); |
---|
361 | return started; |
---|
362 | } |
---|
363 | |
---|
364 | public boolean isLoggedIn() |
---|
365 | { |
---|
366 | if( !started ) |
---|
367 | return false; |
---|
368 | |
---|
369 | return backend.isLoggedIn(); |
---|
370 | } |
---|
371 | |
---|
372 | public void openChat(String jid) |
---|
373 | { |
---|
374 | try |
---|
375 | { |
---|
376 | JID j = JID.checkedJIDFromString(jid); |
---|
377 | secureMethodInvoker.addData(new Object[]{"openChat",j}); |
---|
378 | } |
---|
379 | catch (InstantiationException e) |
---|
380 | { |
---|
381 | e.printStackTrace(); |
---|
382 | } |
---|
383 | |
---|
384 | } |
---|
385 | |
---|
386 | public void addContact(String jid, String name, String group) |
---|
387 | { |
---|
388 | /** |
---|
389 | * @Author(s) : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br |
---|
390 | * @Date : 24/04/2009 |
---|
391 | * @Brief : Adiciona novos contatos e ja pede autorizacao. |
---|
392 | */ |
---|
393 | try |
---|
394 | { |
---|
395 | JID Jid = JID.checkedJIDFromString(jid); |
---|
396 | backend.send(new Presence(Jid, "subscribe")); |
---|
397 | RosterBuilder rb = new RosterBuilder(); |
---|
398 | RosterItemBuilder rib = new RosterItemBuilder(); |
---|
399 | rib.addGroup(group); |
---|
400 | rib.jid = Jid; |
---|
401 | rib.name = name; |
---|
402 | rib.ask = "subscribe"; |
---|
403 | rib.subscription = "none"; |
---|
404 | rb.addItem(rib.build()); |
---|
405 | InfoQueryBuilder iqb = new InfoQueryBuilder(); |
---|
406 | iqb.addExtension(rb.build()); |
---|
407 | iqb.setType("set"); |
---|
408 | backend.send(iqb.build()); |
---|
409 | } |
---|
410 | catch(Exception error) |
---|
411 | { |
---|
412 | error.printStackTrace(); |
---|
413 | } |
---|
414 | } |
---|
415 | |
---|
416 | public void login(String username,String server,String password) |
---|
417 | { |
---|
418 | if( backend.isLoggedIn() ) |
---|
419 | { |
---|
420 | JID j = backend.getMyJID(); |
---|
421 | |
---|
422 | if( j.getDomain().equals(server) && j.getUser().equals(username) ) |
---|
423 | return; |
---|
424 | } |
---|
425 | secureMethodInvoker.addData(new Object[]{"login",username,server,password}); |
---|
426 | } |
---|
427 | |
---|
428 | public static Class getPlugin(String name) |
---|
429 | { |
---|
430 | try |
---|
431 | { |
---|
432 | return Class.forName("nu.fw.jeti.plugins."+name+".Plugin"); |
---|
433 | } |
---|
434 | catch (ClassNotFoundException e) |
---|
435 | { |
---|
436 | System.err.println(MessageFormat.format(I18N.gettext("main.error.{0}_plugin_not_found"), new Object[]{name})); |
---|
437 | return null; |
---|
438 | } |
---|
439 | } |
---|
440 | |
---|
441 | public static void getPrefPanel(String name,Map loadedPreferencePanels) |
---|
442 | { |
---|
443 | Class prefPanel = null; |
---|
444 | try |
---|
445 | { |
---|
446 | prefPanel = Class.forName("nu.fw.jeti.plugins."+name+".PrefPanel"); |
---|
447 | } |
---|
448 | catch (ClassNotFoundException e) |
---|
449 | { |
---|
450 | return; |
---|
451 | } |
---|
452 | loadedPreferencePanels.put(name,prefPanel); |
---|
453 | } |
---|
454 | |
---|
455 | static class SecureMethodRunner extends Thread |
---|
456 | { |
---|
457 | private Backend backend; |
---|
458 | private LinkedList queue = new LinkedList(); |
---|
459 | private volatile boolean isRunning=true; |
---|
460 | |
---|
461 | SecureMethodRunner(Backend backend) |
---|
462 | { |
---|
463 | this.backend = backend; |
---|
464 | } |
---|
465 | |
---|
466 | public void addData(Object method) |
---|
467 | { |
---|
468 | synchronized(queue) |
---|
469 | { |
---|
470 | queue.addLast(method); |
---|
471 | queue.notifyAll(); |
---|
472 | } |
---|
473 | } |
---|
474 | |
---|
475 | public void stopRunning() |
---|
476 | { |
---|
477 | isRunning = false; |
---|
478 | synchronized(queue){queue.notifyAll();} |
---|
479 | } |
---|
480 | |
---|
481 | public void run() |
---|
482 | { |
---|
483 | Object[] method; |
---|
484 | while (isRunning) |
---|
485 | { |
---|
486 | synchronized (queue) |
---|
487 | { |
---|
488 | if (queue.isEmpty()) |
---|
489 | { |
---|
490 | try |
---|
491 | { |
---|
492 | queue.wait(); |
---|
493 | } |
---|
494 | catch (InterruptedException e) |
---|
495 | { |
---|
496 | //bug when thrown? called when interrupted |
---|
497 | e.printStackTrace(); |
---|
498 | return; |
---|
499 | } |
---|
500 | continue; |
---|
501 | } |
---|
502 | method = (Object[]) queue.removeFirst(); |
---|
503 | } |
---|
504 | |
---|
505 | String m = (String)method[0]; |
---|
506 | |
---|
507 | if( m.equals("openChat") ) |
---|
508 | backend.getMain().startChat((JID)method[1]); |
---|
509 | } |
---|
510 | } |
---|
511 | } |
---|
512 | } |
---|
513 | |
---|
514 | /* |
---|
515 | * Overrides for emacs |
---|
516 | * Local variables: |
---|
517 | * tab-width: 4 |
---|
518 | * End: |
---|
519 | */ |
---|