KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > protocols > aim > JAIMPlugin


1 /*
2     =========================================================================
3     Package aim - Implements the AIM Protocol transport.
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: JAIMPlugin.java,v 1.39 2007/01/28 17:39:21 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.39 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.protocols.aim;
40
41 import java.util.*;
42 import java.io.*;
43 import com.levelonelabs.aim.AIMClient;
44 import com.levelonelabs.aim.AIMBuddy;
45 import com.levelonelabs.aim.AIMListener;
46 import org.planetamessenger.plugin.*;
47
48
49
50 public class JAIMPlugin extends JPlugin implements JMessengerEventListener, AIMListener {
51   
52   AIMClient session = null;
53   int nInitialStatus = JPlugin.STATUS_OFFLINE;
54   boolean bConnected = false;
55   
56   
57
58   /**
59    * Initialize class data.
60    */

61   public JAIMPlugin() {
62     
63   }
64   
65   /**
66    * Implement the onCreate plugin event.
67    * Performs the plugin creation.
68    */

69   public void onCreate() {
70
71     super.onCreate();
72     
73     JPluginCapabilities capabilities = getPluginCapabilities();
74
75     // Update the plugin capabilities
76
capabilities.putBool( JPluginCapabilities.CAPABILITY_PRIVACY_CONTROL_ENABLED, true );
77     capabilities.putBool( JPluginCapabilities.CAPABILITY_BLOCK_CONTACT, true );
78     setPluginCapabilities( capabilities );
79
80     addMessengerEventListener( this );
81     
82     session = null;
83     addStatus( JPlugin.STATUS_ONLINE, new Integer JavaDoc( JPlugin.STATUS_ONLINE ) );
84     addStatus( JPlugin.STATUS_OFFLINE, new Integer JavaDoc( JPlugin.STATUS_OFFLINE ) );
85     addStatus( JPlugin.STATUS_AWAY, new Integer JavaDoc( JPlugin.STATUS_AWAY ) );
86   }
87
88   /**
89    * Implement the onLogin event listener.
90    * @param nPluginStatus The initial plugin status;
91    */

92   public void onLogin( int nPluginStatus ) {
93
94     nInitialStatus = nPluginStatus;
95     session = new AIMClient( getUserId(), getPassword(), "No Info", true );
96     session.addAIMListener( this );
97     session.signOn();
98   }
99   
100   /**
101    * Implement the onLogout event listener.
102    */

103   public void onLogout() {
104
105     if( session != null ) {
106       session.signOff();
107       session = null;
108       bConnected = false;
109       nInitialStatus = JPlugin.STATUS_OFFLINE;
110     }
111   }
112
113   /**
114     * Implement the onAddUserToContactList event listener
115     * @param strNewUserId The User's id that will be added;
116     */

117   public void onAddUserToContactList( java.lang.String JavaDoc strUserId ) {
118     
119     if( session != null ) {
120       AIMBuddy contact = new AIMBuddy( strUserId );
121       session.addBuddy( contact );
122     }
123   }
124   
125   /**
126    * Implement the onRemoveUserFromContactList event
127    * listener;
128    * @param strUserId The User's id that will be removed;
129    */

130   public void onRemoveUserFromContactList( java.lang.String JavaDoc strUserId ) {
131
132     if( session != null ) {
133       AIMBuddy contact = session.getBuddy( strUserId );
134       
135       if( contact != null )
136         session.removeBuddy( contact );
137     }
138   }
139
140   /**
141    * Implement the onSendMessage event listener
142    * @param strToUser The user that we'll send
143    * the message;
144    * @param msg The Message that will be sent;
145    * @param nMsgType The Message type:
146    * MESSAGE_TYPE_ONLINE - Online messages;
147    * MESSAGE_TYPE_OFFLINE - Offline messages;
148    */

149   public void onSendMessage( java.lang.String JavaDoc strToUser, java.lang.String JavaDoc msg, int nMsgType ) {
150     
151     if( session != null ) {
152       AIMBuddy contact = session.getBuddy( strToUser );
153       
154       if( contact != null )
155         session.sendMessage( contact, msg );
156     }
157   }
158   
159   /**
160    * Implement the onRequestAuthorization event
161    * listener.
162    * @param strUserId The user id requesting
163    * the authorization to add him/her to
164    * contact list;
165    * @param strNickName The user nick name;
166    */

167   public void onRequestAuthorization( java.lang.String JavaDoc strUserId, java.lang.String JavaDoc strNickName ) {
168
169   }
170
171   /**
172    * Implement the onUserDetails event listener;
173    * @param strUserId The User id owner of
174    * this detail;
175    */

176   public void onBuddyDetails( java.lang.String JavaDoc strUserId ) {
177
178   }
179   
180   /**
181    * Implement the onAuthorizeBuddy event listener
182    * @param strUserId The user id authorized;
183    */

184   public void onAuthorizeBuddy( java.lang.String JavaDoc strUserId ) {
185     
186   }
187   
188   /**
189    * Implement the onUnauthorizeBuddy event listener
190    * @param strUserId The user id unauthorized;
191    */

192   public void onUnauthorizeBuddy( java.lang.String JavaDoc strUserId ) {
193
194   }
195   
196   /**
197    * Implements the onPluginStatusChanged event.
198    * o nStatus - The new status for this plugin;
199    */

200   public void onPluginStatusChanged( int nStatus ) {
201   
202     if( session != null )
203       switch( nStatus ) {
204
205         case JPlugin.STATUS_AWAY : session.setUnavailable( "Away" ); break;
206
207         case JPlugin.STATUS_ONLINE : session.setAvailable(); break;
208       }
209   }
210   
211   /**
212     * implement the onRegisterExistingUser
213     * event listener;
214     * @param strNickName The NickName of
215     * the user;
216     * @param strNewUserId The User's id
217     * that will be registered;
218     * @param strPasswd The User password;
219     */

220   public void onRegisterExistingUser( java.lang.String JavaDoc strNickName, java.lang.String JavaDoc strNewUserId, java.lang.String JavaDoc strPasswd ) {
221     
222     JUserInfo userInfo = new JUserInfo( strNewUserId, strNickName, strPasswd, JPlugin.STATUS_ONLINE );
223     
224     setUserInfo( userInfo );
225     fireOnLogin();
226   }
227   
228   /**
229    * Implement the onRegisterNewUser event
230    * listener.
231    * @param strNickName The NickName of
232    * the user;
233    * @param strNewUserId The User's id
234    * that will be registered;
235    * @param strPasswd The User password;
236    */

237   public void onRegisterNewUser( java.lang.String JavaDoc strNickName, java.lang.StringBuffer JavaDoc strNewUserId, java.lang.String JavaDoc strPasswd ) {
238     
239     fireOnRegisterUserFailed(); // This plugin doesnt support this feature
240
}
241
242   /**
243    * Implement the onUnregisterUser event
244    * listener.
245    */

246   public void onUnregisterUser() {
247    
248     if( isConnected() ) {
249       fireOnLogout();
250       fireOnUnregisterUserSuccessfull();
251     }
252   }
253   
254   /**
255    * Implement the onChangePassword event
256    * listener
257    * @strNewPasswd The new user's password;
258    */

259   public void onChangePassword( java.lang.String JavaDoc strNewPasswd ) {
260     
261     fireOnPasswordChangedSuccessfull( strNewPasswd );
262   }
263   
264   /**
265    * Implement the onChange privacy event
266    * handler.
267    * Performs a privacy control to a user
268    * for this plugin.
269    * @param strUserId The user that new privacy
270    * will be applied;
271    * @param nPrivacyType The privacy type (@see JPlugin
272    * for available types);
273    */

274   public void onChangePrivacy( java.lang.String JavaDoc strUserId, int nPrivacyType ) {
275
276     AIMBuddy contact = new AIMBuddy( strUserId );
277     
278     switch( nPrivacyType ) {
279           
280       case PRIVACY_BLOCK_USER : session.denyBuddy( contact ); break;
281           
282       case PRIVACY_UNBLOCK_USER : session.permitBuddy( contact ); break;
283     }
284   }
285   
286   /**
287    * Implement the onPrivacyChanged event handler.
288    * This event is generated after all processing
289    * onChangePrivacy events.
290    */

291   public void onPrivacyChanged() {
292
293   }
294   
295   /**
296    * Implement the onPluginPropertiesUpdated event handler.<br>
297    */

298   public void onPluginPropertiesUpdated() {
299     
300   }
301  
302   /**
303    * Returns the plugin connection
304    * status .
305    */

306   public boolean isConnected() {
307     
308     return ( session != null ? bConnected : false );
309   }
310   
311   // Jaimbot listener events
312
/**
313    * Implement the connected event for jaimbot
314    * library;
315    */

316   public void handleConnected() {
317
318     bConnected = true;
319     fireOnLoginAccepted();
320     fireOnRegisterUserSucessfull(); // Called here because this login could be a user registration (onRegisterexistingUser)
321
fireOnPluginStatusChanged( nInitialStatus );
322   }
323
324   /**
325    * Implement the disconnected event for jaimbot
326    * library;
327    */

328   public void handleDisconnected() {
329
330     fireOnLogout();
331     fireOnLoginFailed();
332     fireOnRegisterUserFailed(); // Called here because this login could be a user registration (onRegisterexistingUser)
333
}
334
335   /**
336    * Implement the message handling for jaimbot
337    * library;
338    * @param contact The contact owner of event;
339    * @param msg The message from contact;
340    */

341   public void handleMessage( AIMBuddy contact, String JavaDoc msg ) {
342     
343     fireOnReceiveMessage( contact.getName(), null, msg );
344   }
345
346   /**
347    * Implement the warning handling for jaimbot
348    * library;
349    * @param contact The contact owner of event;
350    * @param amount ??????????
351    */

352   public void handleWarning( AIMBuddy contact, int amount ) {
353
354     System.err.println( "JAIMPlugin.handleWarning() - " + contact.getName() + " amount " + amount );
355   }
356
357   /**
358    * Implement the signon handling for jaimbot
359    * library;
360    * @param contact The contact owner of event;
361    * @param info ?????;
362    */

363   public void handleBuddySignOn( AIMBuddy contact, String JavaDoc info ) {
364
365     System.err.println( "JAIMPlugin.handleBuddySignOn() - " + contact.getName() + " Info " + info );
366     
367     if( !getKernelManager().getContactListManager().isContact( this, contact.getName() ) )
368       getKernelManager().getContactListManager().addToContactList( this, contact.getName(), contact.getName() );
369
370     fireOnBuddyStatusChanged( contact.getName(), JPlugin.STATUS_ONLINE );
371   }
372   
373   /**
374    * Implement the signoff handling for jaimbot
375    * library;
376    * @param contact The contact owner of event;
377    * @param info ?????;
378    */

379   public void handleBuddySignOff( AIMBuddy contact, String JavaDoc info ) {
380     
381     System.err.println( "JAIMPlugin.handleBuddySignOff() - " + contact.getName() + " Info " + info );
382     
383     if( !getKernelManager().getContactListManager().isContact( this, contact.getName() ) )
384       getKernelManager().getContactListManager().addToContactList( this, contact.getName(), contact.getName() );
385
386     fireOnBuddyStatusChanged( contact.getName(), JPlugin.STATUS_OFFLINE );
387   }
388
389   /**
390    * Implement the error handling for jaimbot
391    * library;
392    * @param error The protocol error description;
393    * @param message protocol message;
394    */

395   public void handleError( String JavaDoc error, String JavaDoc message ) {
396     
397     System.err.println( "JAIMPlugin.handleError() - error [" + error + "] message [" + message + "]" );
398   }
399
400   /**
401    * Implement the buddy unavailable event for jaimbot
402    * library;
403    * @param contact The contact owner of message;
404    * @param message The unavailable message for contact;
405    */

406   public void handleBuddyUnavailable( AIMBuddy contact, String JavaDoc message ) {
407     
408     System.err.println( "JAIMPlugin.handleBuddyUnavailable() - " + contact.getName() + " Message " + message );
409     
410     Iterator itItems = session.getBuddyNames();
411     
412     // Load server contact list to local contact list
413
while( itItems.hasNext() ) {
414       String JavaDoc strUserId = itItems.next().toString();
415
416       if( !getKernelManager().getContactListManager().isContact( this, strUserId ) )
417         getKernelManager().getContactListManager().addToContactList( this, strUserId, strUserId );
418     }
419   }
420
421   /**
422    * Implement the buddy available event for jaimbot
423    * library;
424    * @param contact The contact owner of message;
425    * @param message The available message for contact;
426    */

427   public void handleBuddyAvailable( AIMBuddy contact, String JavaDoc message ) {
428
429     System.err.println( "JAIMPlugin.handleBuddyAvailable() - " + contact.getName() + " Message " + message );
430     
431     Iterator itItems = session.getBuddyNames();
432     
433     // Load server contact list to local contact list
434
while( itItems.hasNext() ) {
435       String JavaDoc strUserId = itItems.next().toString();
436
437       if( !getKernelManager().getContactListManager().isContact( this, strUserId ) )
438         getKernelManager().getContactListManager().addToContactList( this, strUserId, strUserId );
439     }
440   }
441 }
442 // JAIMPlugin class
443
Popular Tags