KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2     =========================================================================
3     Package icq - Implements the 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: JICQPlugin.java,v 1.41 2007/02/03 07:08:04 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.41 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.protocols.icq;
40
41 import org.jcq2k.*;
42 import org.jcq2k.icq2k.*;
43 import org.planetamessenger.plugin.*;
44
45
46 public class JICQPlugin extends JPlugin implements MessagingNetworkListener, JMessengerEventListener {
47
48   ICQ2KMessagingNetwork session = null;
49
50   
51   
52   /**
53    * Initializes all class data.
54    */

55   public JICQPlugin() {
56     
57   }
58
59   /**
60    * Implement the onCreate event listener.
61    * Performs the plugin creation.
62    */

63   public void onCreate() {
64   
65     super.onCreate();
66     
67     JPluginCapabilities capabilities = getPluginCapabilities();
68     
69     // Update the plugin capabilities
70
capabilities.putBool( JPluginCapabilities.CAPABILITY_OFFLINE_MESSAGE, true );
71     capabilities.putBool( JPluginCapabilities.CAPABILITY_UPDATE_SCREEN_NAME, true );
72     setPluginCapabilities( capabilities );
73     
74     addMessengerEventListener( this );
75     session = new ICQ2KMessagingNetwork();
76
77     // Setup status
78
addStatus( JPlugin.STATUS_ONLINE, new Integer JavaDoc( MessagingNetwork.STATUS_ONLINE ) );
79     addStatus( JPlugin.STATUS_OFFLINE, new Integer JavaDoc( MessagingNetwork.STATUS_OFFLINE ) );
80     addStatus( JPlugin.STATUS_AWAY, new Integer JavaDoc( MessagingNetwork.STATUS_AWAY) );
81     addStatus( JPlugin.STATUS_DND, new Integer JavaDoc( MessagingNetwork.STATUS_DND ) );
82     addStatus( JPlugin.STATUS_FREE_CHAT, new Integer JavaDoc( MessagingNetwork.STATUS_FREE_FOR_CHAT ) );
83     addStatus( JPlugin.STATUS_IDLE, new Integer JavaDoc( MessagingNetwork.STATUS_NA ) );
84     addStatus( JPlugin.STATUS_BUSY, new Integer JavaDoc( MessagingNetwork.STATUS_BUSY ) );
85     addStatus( JPlugin.STATUS_INVISIBLE, new Integer JavaDoc( MessagingNetwork.STATUS_INVISIBLE ) );
86     
87     try {
88       session.init();
89       session.addMessagingNetworkListener( this );
90     } catch( MessagingNetworkException e ) {
91       System.err.println( "JICQPlugin.onCreate() - " + e );
92     }
93   }
94
95   /**
96    * Handle the event onDestroy sent
97    * by MOS to this Plugin.
98    */

99   public void onDestroy() {
100     
101     super.onDestroy();
102     session.destroy();
103   }
104   
105   /**
106    * Implement the onLogin event listener.
107    * @param nPluginStatus The initial plugin status;
108    */

109   public void onLogin( int nPluginStatus ) {
110
111     try {
112       int nStatus = ( ( Integer JavaDoc ) translateStatusToPlugin( nPluginStatus ) ).intValue();
113       
114       session.login( getUserId(), getPassword(), null, nStatus );
115       fireOnLoginAccepted();
116       fireOnRegisterUserSucessfull(); // Called here because this login could be a user registration (onRegisterexistingUser)
117
addToLibContactList();
118       
119     } catch( MessagingNetworkException e ) {
120       System.err.println( "JICQPlugin.onLogin(" + getUserId() + ") - Unable to login" );
121       fireOnLoginFailed();
122       fireOnRegisterUserFailed(); // Called here because this login could be a user registration (onRegisterexistingUser)
123
}
124   }
125
126   /**
127    * Implement the onLogout event listener.
128    */

129   public void onLogout() {
130
131     String JavaDoc strUserId = getUserId();
132     
133     
134     try {
135       java.util.Enumeration JavaDoc eContactList = getKernelManager().getContactListManager().getContactList( this ).keys();
136
137       while( eContactList.hasMoreElements() ) {
138         String JavaDoc strBuddyId = eContactList.nextElement().toString();
139
140         session.removeFromContactList( strUserId, strBuddyId );
141       }
142
143       if( isConnected() )
144         session.logout( strUserId );
145
146     } catch( MessagingNetworkException e ) {
147       System.err.println( "JICQPlugin.onLogout(" + strUserId + ") - " + e );
148     }
149   }
150   
151   /**
152     * Implement the onAddUserToContactList event listener
153     * @param strNewUserId The User's id that will be added;
154     */

155   public void onAddUserToContactList( java.lang.String JavaDoc strUserId ) {
156
157     try {
158       Long.valueOf( strUserId );
159     }
160     catch( NumberFormatException JavaDoc e ) {
161       System.err.println( "JICQPlugin.onAddUserToContactList() - Invalid user" );
162       return;
163     }
164
165     try {
166       session.addToContactList( getUserId(), strUserId );
167     } catch( MessagingNetworkException e ) {
168       System.err.println( "JICQPlugin.onAddToContactList(" + strUserId + ") - " + e );
169     }
170   }
171
172   /**
173    * Implement the onRemoveUserFromContactList event
174    * listener;
175    * @param strUserId The User's id that will be removed;
176    */

177   public void onRemoveUserFromContactList( java.lang.String JavaDoc strUserId ) {
178
179     try {
180       session.removeFromContactList( getUserId(), strUserId );
181     } catch( MessagingNetworkException e ) {
182       System.err.println( "JICQPlugin.onRemoveUserFromContactList(" + strUserId + ") - " + e );
183     }
184   }
185  
186   /**
187    * Implement the onSendMessage event listener
188    * @param strToUser The user that we'll send
189    * the message;
190    * @param msg The Message that will be sent;
191    * @param nMsgType The Message type:
192    * MESSAGE_TYPE_ONLINE - Online messages;
193    * MESSAGE_TYPE_OFFLINE - Offline messages;
194    */

195   public void onSendMessage( java.lang.String JavaDoc strToUser, java.lang.String JavaDoc msg, int nMsgType ) {
196
197     try {
198       session.sendMessage( getUserId(), strToUser, msg );
199     } catch( MessagingNetworkException e ) {
200       System.err.println( "JICQPlugin.sendMessage(" + strToUser + ") - Unable to send the message" );
201     }
202   }
203   
204   /**
205    * Implement the onRequestAuthorization event
206    * listener.
207    * @param strUserId The user id requesting
208    * the authorization to add him/her to
209    * contact list;
210    * @param strNickName The user nick name;
211    */

212   public void onRequestAuthorization( java.lang.String JavaDoc strUserId, java.lang.String JavaDoc strNickName ) {
213     
214   }
215
216   /**
217    * Implement the onUserDetails event listener;
218    * @param strUserId The User id owner of
219    * this detail;
220    */

221   public void onBuddyDetails( java.lang.String JavaDoc strUserId ) {
222   
223     try {
224       UserDetails userDetails = session.getUserDetails( getUserId(), strUserId );
225
226       getKernelManager().getContactListManager().updateScreenName( this, strUserId, userDetails.getNick() );
227     } catch( MessagingNetworkException e ) {
228       System.err.println( "JICQPlugin.onBuddyDetails(" + strUserId + ") - " + e );
229     }
230   }
231   
232   /**
233    * Implement the onAuthorizeBuddy event listener
234    * @param strUserId The user id authorized;
235    */

236   public void onAuthorizeBuddy( java.lang.String JavaDoc strUserId ) {
237     
238     onChangePrivacy( strUserId, PRIVACY_UNBLOCK_USER );
239   }
240   
241   /**
242    * Implement the onUnauthorizeBuddy event listener
243    * @param strUserId The user id unauthorized;
244    */

245   public void onUnauthorizeBuddy( java.lang.String JavaDoc strUserId ) {
246
247     onChangePrivacy( strUserId, PRIVACY_BLOCK_USER );
248   }
249   
250   /**
251    * Implements the onPluginStatusChaged event.
252    * o nStatus - The new status for this plugin;
253    */

254   public void onPluginStatusChanged( int nStatus ) {
255
256     try {
257       session.setStatus( getUserId(), ( ( Integer JavaDoc ) translateStatusToPlugin( nStatus ) ).intValue() );
258     } catch( MessagingNetworkException e ) {
259       System.err.println( "JICQPlugin.onPluginStatusChanged(" + getUserId() + ") - Error to set status" );
260     }
261   }
262   
263   /**
264     * implement the onRegisterExistingUser
265     * event listener;
266     * @param strNickName The NickName of
267     * the user;
268     * @param strNewUserId The User's id
269     * that will be registered;
270     * @param strPasswd The User password;
271     */

272   public void onRegisterExistingUser( java.lang.String JavaDoc strNickName, java.lang.String JavaDoc strNewUserId, java.lang.String JavaDoc strPasswd ) {
273     
274     JUserInfo userInfo = new JUserInfo( strNewUserId, strNickName, strPasswd, JPlugin.STATUS_ONLINE );
275
276     setUserInfo( userInfo );
277     fireOnLogin();
278   }
279   
280   /**
281    * Implement the onRegisterNewUser event
282    * listener.
283    * @param strNickName The NickName of
284    * the user;
285    * @param strNewUserId The User's id
286    * that will be registered;
287    * @param strPasswd The User password;
288    */

289   public void onRegisterNewUser( java.lang.String JavaDoc strNickName, java.lang.StringBuffer JavaDoc strNewUserId, java.lang.String JavaDoc strPasswd ) {
290     
291     fireOnRegisterUserFailed(); // This plugin doesnt support this feature
292
}
293
294   /**
295    * Implement the onUnregisterUser event
296    * listener.
297    */

298   public void onUnregisterUser() {
299    
300     if( isConnected() ) {
301       fireOnLogout();
302       fireOnUnregisterUserSuccessfull();
303     }
304   }
305   
306   /**
307    * Implement the onChangePassword event
308    * listener
309    * @strNewPasswd The new user's password;
310    */

311   public void onChangePassword( java.lang.String JavaDoc strNewPasswd ) {
312     
313     fireOnPasswordChangedSuccessfull( strNewPasswd );
314   }
315   
316   /**
317    * Implement the onChange privacy event
318    * handler.
319    * Performs a privacy control to a user
320    * for this plugin.
321    * @param strUserId The user that new privacy
322    * will be applied;
323    * @param nPrivacyType The privacy type (@see JPlugin
324    * for available types);
325    */

326   public void onChangePrivacy( java.lang.String JavaDoc strUserId, int nPrivacyType ) {
327     
328   }
329   
330   /**
331    * Implement the onPrivacyChanged event handler.
332    * This event is generated after all processing
333    * onChangePrivacy events.
334    */

335   public void onPrivacyChanged() {
336     
337   }
338   
339   /**
340    * Implement the onPluginPropertiesUpdated event handler.<br>
341    */

342   public void onPluginPropertiesUpdated() {
343     
344   }
345   
346   /**
347    * Returns the plugin connection
348    * status.
349    */

350   public boolean isConnected() {
351     
352     try {
353       if( session.getStatus( getUserId() ) != MessagingNetwork.STATUS_OFFLINE )
354         return true;
355       else
356         return false;
357     } catch( MessagingNetworkException e ) {
358       System.err.println( "JICQPlugin.isConnected(" + getUserId() + ") - Exception in isConnected()" );
359       return false;
360     }
361   }
362
363   // DJCq2k listeners methods implementation
364
/**
365    * Jcq2k network event listener methods
366    * The methods below will connect the PlanetaMessenger Plugin specification
367    * with the Jcq2k plugin specification.
368    */

369   
370   /**
371    * Event for ReceiveContact for the connected user.
372    * @param see MessagingNetwork interface (jcq2k);
373    */

374   public void contactsReceived( byte networkId, String JavaDoc srcLoginId, String JavaDoc dstLoginId, String JavaDoc[] contactsLoginIds, String JavaDoc[] contactsNicks ) {
375       
376     for( int nCount = 0; nCount < contactsNicks.length; nCount++ ) {
377       System.err.println( "contactsReceived() - " + contactsLoginIds[nCount] + " " + contactsNicks[nCount] );
378     }
379   }
380   
381   /**
382    * Implement the messageReceived Event
383    * @param see MessagingNetwork interface (jcq2k);
384    */

385   public void messageReceived( byte networkId, String JavaDoc srcLoginId, String JavaDoc dstLoginId, String JavaDoc text ) {
386     
387     //java.util.GregorianCalendar msgDate = new java.util.GregorianCalendar();
388
super.fireOnReceiveMessage( srcLoginId, null, text );
389   }
390
391   /**
392    * Implement the stausChanged Event
393    * @param see MessagingNetwork interface (jcq2k);
394    */

395   public void statusChanged( byte networkId, String JavaDoc srcLoginId, String JavaDoc dstLoginId, int status, int reasonCategory, String JavaDoc reasonMessage ) {
396
397     System.err.println( "JICQPlugin.statusChanged() - User [" + dstLoginId + "] changed status to " + status );
398     
399     if( !srcLoginId.equals( dstLoginId ) )
400       fireOnBuddyStatusChanged( dstLoginId, translateStatusFromPlugin( new Integer JavaDoc( status ) ) );
401     else {
402       if( status == MessagingNetwork.STATUS_OFFLINE )
403         fireOnLogout();
404     }
405   }
406
407   /**
408    * Add the MOS contact list to
409    * jcq2k ContactList.
410    */

411   void addToLibContactList() {
412     
413     try {
414       
415       java.util.Enumeration JavaDoc eContactList = getKernelManager().getContactListManager().getContactList( this ).keys();
416
417       while( eContactList.hasMoreElements() ) {
418         String JavaDoc strUserId = eContactList.nextElement().toString();
419       
420         session.addToContactList( getUserId(), strUserId );
421       }
422     } catch( MessagingNetworkException e ) {
423       System.err.println( "JICQPlugin.addToLibContactList(" + getUserId() + ") - " + e );
424     }
425   }
426 }
427
428 // JICQPlugin class
429
Popular Tags