KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > protocols > icq > JOscarLibPlugin


1 /*
2     =========================================================================
3     Package icq - Implements the AIM/ICQ Protocol.
4
5     This module is developed and maintained by PlanetaMessenger.org.
6     Specs, New and updated versions can be found in
7     http://www.planetamessenger.org
8     If you want contact the Team please send a email to Project Manager
9     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
10
11     Copyright (C) since 2001 by PlanetaMessenger.org
12     
13     This library is free software; you can redistribute it and/or
14     modify it under the terms of the GNU Lesser General Public
15     License as published by the Free Software Foundation; either
16     version 2.1 of the License, or (at your option) any later version.
17
18     This library is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21     Lesser General Public License for more details.
22
23     You should have received a copy of the GNU Lesser General Public
24     License along with this library; if not, write to the Free Software
25     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27     =========================================================================
28 */

29 /**
30  *
31  * $Id: JOscarLibPlugin.java,v 1.34 2007/02/17 01:54:36 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.34 $
35  * $State: Exp $
36  *
37  */

38
39 // TODO: Add Login failed event.
40

41 package org.planetamessenger.protocols.icq;
42
43 import java.util.*;
44 import java.awt.event.*;
45 import JOscarLib.*;
46 import JOscarLib.Core.*;
47 import JOscarLib.Tool.*;
48 import JOscarLib.Management.*;
49 import JOscarLib.Setting.Enum.*;
50 import JOscarLib.Integration.Event.*;
51 import JOscarLib.Packet.Sent.Icq.*;
52 import org.planetamessenger.plugin.*;
53
54
55 public class JOscarLibPlugin extends JPlugin implements Observer, MessagingListener, ContactListListener, MetaInfoListener, JMessengerEventListener, ActionListener {
56
57   final String JavaDoc HOSTNAME = "login.icq.com";
58   final int PORT = 5190;
59   final int LOGIN_TIMEOUT = 60000;
60
61   javax.swing.Timer JavaDoc loginTimer;
62   OscarInterface oscarInterface;
63   OscarConnection session;
64   int nInitialStatus;
65   StatusListener statusObject;
66    
67
68   /**
69    * Initializes all class data.
70    */

71   public JOscarLibPlugin() {
72     
73   }
74
75   /**
76    * Implement the onCreate plugin event.
77    * Performs the plugin creation.
78    */

79   public void onCreate() {
80
81     super.onCreate();
82     
83     JPluginCapabilities capabilities = getPluginCapabilities();
84     
85     // Update the plugin capabilities
86
capabilities.putBool( JPluginCapabilities.CAPABILITY_OFFLINE_MESSAGE, true );
87     capabilities.putBool( JPluginCapabilities.CAPABILITY_UPDATE_SCREEN_NAME, true );
88     setPluginCapabilities( capabilities );
89
90     addMessengerEventListener( this );
91     oscarInterface = new OscarInterface();
92     loginTimer = new javax.swing.Timer JavaDoc( LOGIN_TIMEOUT, this );
93     session = null;
94     
95     statusObject = new StatusListener() {
96
97       /**
98        * Implements the onOffgoingUser
99        * translating this event to PlanetaMessenger.org
100        * event.
101        * This event is response to user status update.
102        * @param see JOSCARLib docs;
103        */

104       public void onIncomingUser( JOscarLib.Integration.Event.IncomingUserEvent incomingUserEvent ) {
105
106         fireOnBuddyStatusChanged( incomingUserEvent.getIncomingUserId(), translateStatusFromPlugin( new Integer JavaDoc( incomingUserEvent.getStatusMode().getMode() ) ) );
107       }
108
109       /**
110        * Implements the onOffgoingUser
111        * translating this event to PlanetaMessenger.org
112        * event.
113        * This event is response to user status update.
114        * @param see JOSCARLib docs;
115        */

116       public void onOffgoingUser( JOscarLib.Integration.Event.OffgoingUserEvent offgoingUserEvent ) {
117
118         fireOnBuddyStatusChanged( offgoingUserEvent.getOffgoingUserId(), JPlugin.STATUS_OFFLINE );
119       }
120
121       /**
122        * Implements the onStatusChanged translating this event to
123        * PlanetaMessenger.org event.
124        * @param see JOSCARLib docs;
125        */

126       public void onStatusChange( StatusEvent e ) {
127
128       }
129       
130       /**
131        * Implements the onLogout method for StatusListener
132        * interface object;
133        */

134       public void onLogout() {
135
136         fireOnLogout();
137       }
138     };
139
140     // Setup contactlist
141
addStatus( JPlugin.STATUS_ONLINE, new Integer JavaDoc( StatusModeEnum.ONLINE ) );
142     addStatus( JPlugin.STATUS_OFFLINE, new Integer JavaDoc( StatusModeEnum.OFFLINE ) );
143     addStatus( JPlugin.STATUS_BUSY, new Integer JavaDoc( StatusModeEnum.OCCUPIED ) );
144     addStatus( JPlugin.STATUS_AWAY, new Integer JavaDoc( StatusModeEnum.AWAY ) );
145     addStatus( JPlugin.STATUS_DND, new Integer JavaDoc( StatusModeEnum.DND ) );
146     addStatus( JPlugin.STATUS_FREE_CHAT, new Integer JavaDoc( StatusModeEnum.FREE_FOR_CHAT ) );
147     addStatus( JPlugin.STATUS_INVISIBLE, new Integer JavaDoc( StatusModeEnum.INVISIBLE ) );
148     addStatus( JPlugin.STATUS_IDLE, new Integer JavaDoc( StatusModeEnum.NA ) );
149   }
150
151   /**
152    * Handle the event onDestroy sent
153    * by MOS to this Plugin.
154    */

155   public void onDestroy() {
156     
157     super.onDestroy();
158
159     session = null;
160     oscarInterface = null;
161   }
162   
163   /**
164    * Implement the onLogin event listener.
165    * @param nPluginStatus The initial plugin status;
166    */

167   public void onLogin( int nPluginStatus ) {
168
169     if( nPluginStatus != JPlugin.STATUS_OFFLINE ) {
170       nInitialStatus = nPluginStatus;
171       session = new OscarConnection( HOSTNAME, PORT, getUserId(), getPassword() );
172       session.getPacketAnalyser().getJarClassLoader().setPath( getKernelManager().getDependeciesPath() + "joscarlib.jar" );
173       session.getPacketAnalyser().setDebug( true );
174       session.addMessagingListener( this );
175       session.addStatusListener( statusObject );
176       session.addContactListListener( this );
177       session.addMetaInfoListener( this );
178       session.addObserver( this );
179       loginTimer.start();
180     }
181   }
182   
183   /**
184    * Implement the onLogout event listener.
185    */

186   public void onLogout() {
187
188     if( session != null ) {
189       try {
190         session.deleteObservers();
191         session.close();
192         session.removeMessagingListener( this );
193         session.removeStatusListener( statusObject );
194         session.removeContactListListener( this );
195         session.removeMetaInfoListener( this );
196       } catch( java.io.IOException JavaDoc e ) {
197         System.err.println( "JOscarLibPlugin.logout() - " + e );
198       }
199       
200       session = null;
201     }
202   }
203   
204   /**
205     * Implement the onAddUserToContactList event listener
206     * @param strNewUserId The User's id that will be added;
207     */

208   public void onAddUserToContactList( java.lang.String JavaDoc strUserId ) {
209     
210     if( session != null )
211       oscarInterface.addUser( session, strUserId );
212   }
213   
214   /**
215    * Implement the onRemoveUserFromContactList event
216    * listener;
217    * @param strUserId The User's id that will be removed;
218    */

219   public void onRemoveUserFromContactList( java.lang.String JavaDoc strUserId ) {
220
221   }
222
223   /**
224    * Implement the onSendMessage event listener
225    * @param strToUser The user that we'll send
226    * the message;
227    * @param msg The Message that will be sent;
228    * @param nMsgType The Message type:
229    * MESSAGE_TYPE_ONLINE - Online messages;
230    * MESSAGE_TYPE_OFFLINE - Offline messages;
231    */

232   public void onSendMessage( java.lang.String JavaDoc strToUser, java.lang.String JavaDoc msg, int nMsgType ) {
233     
234     oscarInterface.sendExtendedMessage( session, strToUser, msg );
235   }
236
237   /**
238    * Implement the onRequestAuthorization event
239    * listener.
240    * @param strUserId The user id requesting
241    * the authorization to add him/her to
242    * contact list;
243    * @param strNickName The user nick name;
244    */

245   public void onRequestAuthorization( java.lang.String JavaDoc strUserId, java.lang.String JavaDoc strNickName ) {
246     
247   }
248
249   /**
250    * Implement the onUserDetails event listener;
251    * @param strUserId The User id owner of
252    * this detail;
253    */

254   public void onBuddyDetails( String JavaDoc strUserId ) {
255
256     try {
257       
258       try {
259         Long.parseLong( strUserId ); // Just to test if UIN is a valid number
260
session.sendFlap( ClientMeta.searchByUinTlv( session, strUserId ) );
261       } catch( NumberFormatException JavaDoc e ) {
262         System.err.println( "JOscarLibPlugin.onBuddyDetails() - Invalid UIN (Are you using AIM user instead ICQ user ??)" );
263       }
264     } catch( Exception JavaDoc e ) {
265       System.err.println( "JOscarLibPlugin.onBuddyDetails() - " + e );
266     }
267   }
268   
269   /**
270    * Implement the onAuthorizeBuddy event listener
271    * @param strUserId The user id authorized;
272    */

273   public void onAuthorizeBuddy( java.lang.String JavaDoc strUserId ) {
274     
275   }
276   
277   /**
278    * Implement the onUnauthorizeBuddy event listener
279    * @param strUserId The user id unauthorized;
280    */

281   public void onUnauthorizeBuddy( java.lang.String JavaDoc strUserId ) {
282
283   }
284   
285   /**
286    * Implements the onPluginStatusChanged event.
287    * o nStatus - The new status for this plugin;
288    */

289   public void onPluginStatusChanged( int nStatus ) {
290
291     if( session != null )
292       oscarInterface.changeStatus( session, new StatusModeEnum( ( ( Integer JavaDoc ) translateStatusToPlugin( nStatus ) ).intValue() ) );
293   }
294   
295   /**
296     * implement the onRegisterExistingUser
297     * event listener;
298     * @param strNickName The NickName of
299     * the user;
300     * @param strNewUserId The User's id
301     * that will be registered;
302     * @param strPasswd The User password;
303     */

304   public void onRegisterExistingUser( java.lang.String JavaDoc strNickName, java.lang.String JavaDoc strNewUserId, java.lang.String JavaDoc strPasswd ) {
305     
306     JUserInfo userInfo = new JUserInfo( strNewUserId, strNickName, strPasswd, JPlugin.STATUS_ONLINE );
307     
308     setUserInfo( userInfo );
309     fireOnLogin();
310   }
311   
312   /**
313    * Implement the onRegisterNewUser event
314    * listener.
315    * @param strNickName The NickName of
316    * the user;
317    * @param strNewUserId The User's id
318    * that will be registered;
319    * @param strPasswd The User password;
320    */

321   public void onRegisterNewUser( java.lang.String JavaDoc strNickName, java.lang.StringBuffer JavaDoc strNewUserId, java.lang.String JavaDoc strPasswd ) {
322     
323     fireOnRegisterUserFailed(); // This plugin doesnt support this feature
324
}
325
326   /**
327    * Implement the onUnregisterUser event
328    * listener.
329    */

330   public void onUnregisterUser() {
331    
332     if( isConnected() ) {
333       fireOnLogout();
334       fireOnUnregisterUserSuccessfull();
335     }
336   }
337   
338   /**
339    * Implement the onChangePassword event
340    * listener
341    * @strNewPasswd The new user's password;
342    */

343   public void onChangePassword( java.lang.String JavaDoc strNewPasswd ) {
344     
345     fireOnPasswordChangedSuccessfull( strNewPasswd );
346   }
347   
348   /**
349    * Implement the onChange privacy event
350    * handler.
351    * Performs a privacy control to a user
352    * for this plugin.
353    * @param strUserId The user that new privacy
354    * will be applied;
355    * @param nPrivacyType The privacy type (@see JPlugin
356    * for available types);
357    */

358   public void onChangePrivacy( java.lang.String JavaDoc strUserId, int nPrivacyType ) {
359     
360   }
361   
362   /**
363    * Implement the onPrivacyChanged event handler.
364    * This event is generated after all processing
365    * onChangePrivacy events.
366    */

367   public void onPrivacyChanged() {
368     
369   }
370   
371   /**
372    * Implement the onPluginPropertiesUpdated event handler.<br>
373    */

374   public void onPluginPropertiesUpdated() {
375     
376   }
377
378   /**
379    * Returns the plugin connection
380    * status .
381    */

382   public boolean isConnected() {
383     
384     return ( session != null ? session.isLogged() : false );
385   }
386   
387   // Observer interface implementation
388
/**
389    * Event handler to JOSCARLib connection
390    * successfull event.
391    * @param see JOSCARLib docs;
392    */

393   public void update( java.util.Observable JavaDoc observable, Object JavaDoc obj ) {
394
395     if( session.isLogged() ) {
396       StatusModeEnum status = new StatusModeEnum( ( ( Integer JavaDoc ) translateStatusToPlugin( nInitialStatus ) ).intValue() );
397       Hashtable hContactList = getKernelManager().getContactListManager().getContactList( this );
398       Enumeration eContactList = hContactList.keys();
399       String JavaDoc[] oscarContactList = new String JavaDoc[hContactList.size()];
400       int nCount = 0;
401
402
403       loginTimer.stop();
404
405       while( eContactList.hasMoreElements() ) {
406         oscarContactList[nCount] = eContactList.nextElement().toString();
407         nCount++;
408       }
409
410       oscarInterface.changeStatus( session, status );
411       oscarInterface.addUsers( session, oscarContactList );
412       oscarInterface.checkRoster( session );
413       // Get offline messages if any
414
try {
415         session.sendFlap( ClientMeta.requestOfflineMessages( session ) );
416       } catch( Exception JavaDoc e ) {
417         System.err.println( "JOscarLibPlugin.update()" + e );
418       }
419
420       fireOnPluginStatusChanged( status.getMode() );
421       fireOnLoginAccepted();
422       fireOnRegisterUserSucessfull(); // Called here because this login could be a user registration (onRegisterexistingUser)
423
}
424   }
425   
426   // MessagingListener events
427
/**
428    * Implements the onIncomingMessage
429    * translating this event to PlanetaMessenger.org
430    * event.
431    * This event is response to online messages.
432    * @param see JOSCARLib docs;
433    */

434   public void onIncomingMessage( JOscarLib.Integration.Event.IncomingMessageEvent incomingMessageEvent ) {
435     
436     fireOnReceiveMessage( incomingMessageEvent.getSenderID(), null, incomingMessageEvent.getMessage() );
437   }
438
439   /**
440    * Implements the onOfflineMessage
441    * translating this event to PlanetaMessenger.org
442    * event.
443    * This event is response to offline messages.
444    * @param see JOSCARLib docs;
445    */

446   public void onOfflineMessage( JOscarLib.Integration.Event.OfflineMessageEvent offlineMessageEvent ) {
447     
448     fireOnReceiveMessage( offlineMessageEvent.getSenderUin(), null, offlineMessageEvent.getMessage() );
449   }
450
451   /**
452    * Implements the onIncomingUrl
453    * translating this event to PlanetaMessenger.org
454    * event.
455    * This event is response to URLs received.
456    * @param see JOSCARLib docs;
457    */

458   public void onIncomingUrl( JOscarLib.Integration.Event.IncomingUrlEvent incomingUrlEvent ) {
459     
460   }
461   
462   /**
463    * Implements the onMessageAck event of JOscarLib library.
464    * @param see JOscarLib docs;
465    */

466   public void onMessageAck( final MessageAckEvent e ) {
467     
468   }
469   
470   /**
471    * Implements the onMessageError event of JOscarLib library.
472    * @param see JOscarLib docs;
473    */

474   public void onMessageError( MessageErrorEvent e ) {
475     
476     System.err.println( "JOscarLibPlugin.onMessageError() - " + e );
477   }
478
479   // ContactListListener events
480
/**
481    * Response to contact list management events.
482    * @param event The event generated for event;
483    */

484   public void updateContactList( ContactListEvent event ) {
485     
486     System.err.println( "JOscarLibPlugin.updateContactList()" );
487     
488     ContactListItem[] groups = event.getRoot().getContainedItems();
489     
490     
491     // Add to contact list
492
for( int nCountGrp = 0; nCountGrp < groups.length; nCountGrp++ ) {
493
494       ContactListItem[] items = ( ( Group ) groups[nCountGrp] ).getContainedItems();
495
496       for( int nCountItem = 0; nCountItem < items.length; nCountItem++ ) {
497         
498         // Is in contact list ????
499
if( !getKernelManager().getContactListManager().isContact( this, items[nCountItem].getId() ) ) {
500           getKernelManager().getContactListManager().addToContactList( this, items[nCountItem].getId(), items[nCountItem].getId() );
501           // Request contact information
502
onBuddyDetails( items[nCountItem].getId() );
503         }
504       }
505     }
506   }
507   
508   // MetaInfoListener events
509
/**
510    * Response to contact meta info events.
511    * @param event The event generated for event;
512    */

513   public void updateContactMetaInfo( MetaInfoEvent event ) {
514
515     System.err.println( "JOscarLibPlugin.updateContactMetaInfo()" );
516     
517     if( getKernelManager().getContactListManager().isContact( this, event.getUin() ) )
518       getKernelManager().getContactListManager().updateScreenName( this, event.getUin(), event.getNickName() );
519   }
520
521   // ActionListener events
522
/**
523    * Implement the timer event for loginTimer object.<br>
524    * @param actionEvent The event sent by timer;<br>
525    */

526   public void actionPerformed( java.awt.event.ActionEvent JavaDoc actionEvent ) {
527     
528     System.err.println( "JOscarLibPlugin.actionPerformed() - Login timeout reached." );
529     
530     loginTimer.stop();
531     session = null;
532     fireOnLoginFailed();
533     fireOnRegisterUserFailed(); // Called here because this login could be a user registration (onRegisterexistingUser)
534
}
535 }
536
537 // JOscarLibPlugin class
538
Popular Tags