KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > protocols > msn > JMSNPlugin


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

38
39 package org.planetamessenger.protocols.msn;
40
41 import org.hn.sleek.jmml.*;
42 import org.planetamessenger.plugin.*;
43 import java.util.*;
44
45 /**
46  * Plugin for the MSN protocol, using the JMML API.
47  *
48  */

49 public class JMSNPlugin extends JPlugin implements MessengerClientListener, JMessengerEventListener {
50
51   MessengerServerManager session = null;
52   Hash hProfileNotification = null;
53   Hash hEmailNotification = null;
54   String JavaDoc strPasswd = null;
55
56
57   /**
58    * Initializes all class data.
59    */

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

68   public void onCreate() {
69
70     super.onCreate();
71     
72     JPluginCapabilities capabilities = getPluginCapabilities();
73
74     // Update the plugin capabilities
75
capabilities.putBool( JPluginCapabilities.CAPABILITY_OFFLINE_MESSAGE, true );
76     capabilities.putBool( JPluginCapabilities.CAPABILITY_PRIVACY_CONTROL_ENABLED, true );
77     capabilities.putBool( JPluginCapabilities.CAPABILITY_BLOCK_CONTACT, true );
78     setPluginCapabilities( capabilities );
79
80     session = MessengerServerManager.newInstance();
81     session.addMessengerClientListener( this );
82     addMessengerEventListener( this );
83     
84     // Status management
85
addStatus( JPlugin.STATUS_ONLINE, ContactStatus.ONLINE );
86     addStatus( JPlugin.STATUS_OFFLINE, ContactStatus.OFFLINE );
87     addStatus( JPlugin.STATUS_INVISIBLE, ContactStatus.APPEAR_OFFLINE );
88     addStatus( JPlugin.STATUS_BUSY, ContactStatus.BUSY );
89     addStatus( JPlugin.STATUS_IDLE, ContactStatus.IDLE );
90     addStatus( JPlugin.STATUS_DND, ContactStatus.BE_RIGHT_BACK );
91     addStatus( JPlugin.STATUS_AWAY, ContactStatus.AWAY );
92     addStatus( JPlugin.STATUS_ON_PHONE, ContactStatus.ON_THE_PHONE );
93     addStatus( JPlugin.STATUS_ON_LUNCH, ContactStatus.OUT_TO_LUNCH );
94   }
95
96   /**
97    * Implement the onLogin event listener.
98    * @param nPluginStatus The initial plugin status;
99    */

100   public void onLogin( int nPluginStatus ) {
101   
102     try {
103       session.signIn( getUserId(), getPassword(), ( String JavaDoc ) translateStatusToPlugin( nPluginStatus ) );
104       this.strPasswd = getPassword();
105     } catch( MSNException e ) {
106       System.err.println( "JMSNPlugin.onLogin() - " + e );
107     }
108   }
109   
110   /**
111    * Implement the onLogout event listener.
112    */

113   public void onLogout() {
114
115     try {
116       session.signOut();
117     } catch( MSNException e ) {
118       System.err.println( "JMSNPlugin.onLogout() - " + e );
119     }
120   }
121   
122   /**
123     * Implement the onAddUserToContactList event listener
124     * @param strUserId The User's id that will be added;
125     */

126   public void onAddUserToContactList( java.lang.String JavaDoc strUserId ) {
127     
128     if( session != null )
129       try {
130         session.addToContactList( new Contact( strUserId, ContactList.FORWARD_LIST ) );
131       } catch( MSNException e ) {
132         System.err.println( "JMSNPlugin.onAddUserToContactList() - " + e );
133       }
134   }
135   
136   /**
137    * Implement the onRemoveUserFromContactList event
138    * listener;
139    * @param strUserId The User's id that will be removed;
140    */

141   public void onRemoveUserFromContactList( java.lang.String JavaDoc strUserId ) {
142
143     if( session != null ) {
144       try {
145         if( session.getContactList().getContact( strUserId ).isContact() ) {
146           Contact contact = new Contact( strUserId, ContactList.FORWARD_LIST );
147           session.removeFromContactList( contact );
148         }
149       } catch( MSNException e ) {
150         System.err.println( "JMSNPlugin.onRemoveUserFromContactList() - " + e );
151       }
152     }
153   }
154
155   /**
156    * Implement the onSendMessage event listener
157    * @param strToUser The user that we'll send
158    * the message;
159    * @param nMsgType The Message type:
160    * MESSAGE_TYPE_ONLINE - Online messages;
161    * MESSAGE_TYPE_OFFLINE - Offline messages;
162    */

163   public void onSendMessage( java.lang.String JavaDoc strToUser, java.lang.String JavaDoc msg, int nMsgType ) {
164
165     try {
166       session.sendMessage( strToUser, msg );
167     } catch( MSNException e ) {
168       System.err.println( "JMSNPlugin.onSendMessage() - " + e );
169     }
170   }
171   
172   /**
173    * Implement the onRequestAuthorization event
174    * listener.
175    * @param strUserId The user id requesting
176    * the authorization to add him/her to
177    * contact list;
178    * @param strNickName The user nick name;
179    */

180   public void onRequestAuthorization( java.lang.String JavaDoc strUserId, java.lang.String JavaDoc strNickName ) {
181     
182   }
183
184   /**
185    * Implement the onUserDetails event listener;
186    * @param strUserId The User id owner of
187    * this detail;
188    */

189   public void onBuddyDetails( java.lang.String JavaDoc strUserId ) {
190
191   }
192   
193   /**
194    * Implement the onAuthorizeBuddy event listener
195    * @param strUserId The user id authorized;
196    */

197   public void onAuthorizeBuddy( java.lang.String JavaDoc strUserId ) {
198     
199     onChangePrivacy( strUserId, PRIVACY_UNBLOCK_USER );
200   }
201   
202   /**
203    * Implement the onUnauthorizeBuddy event listener
204    * @param strUserId The user id unauthorized;
205    */

206   public void onUnauthorizeBuddy( java.lang.String JavaDoc strUserId ) {
207
208     onChangePrivacy( strUserId, PRIVACY_BLOCK_USER );
209   }
210
211   /**
212    * Implements the onPluginStatusChaged event.
213    * o nStatus - The new status for this plugin;
214    */

215   public void onPluginStatusChanged( int nStatus ) {
216
217     try {
218       session.setStatus( ( String JavaDoc ) translateStatusToPlugin( nStatus ) );
219     } catch( MSNException e ) {
220       System.err.println( "JMSNPlugin.onPluginStatusChanged() - " + e );
221     }
222   }
223
224   /**
225     * implement the onRegisterExistingUser
226     * event listener;
227     * @param strNickName The NickName of
228     * the user;
229     * @param strNewUserId The User's id
230     * that will be registered;
231     * @param strPasswd The User password;
232     */

233   public void onRegisterExistingUser( String JavaDoc strNickName, String JavaDoc strNewUserId, String JavaDoc strPasswd ) {
234     
235     JUserInfo userInfo = new JUserInfo( strNewUserId, strNickName, strPasswd, JPlugin.STATUS_ONLINE );
236     
237     setUserInfo( userInfo );
238     fireOnLogin();
239   }
240   
241   /**
242    * Implement the onRegisterNewUser event
243    * listener.
244    * @param strNickName The NickName of
245    * the user;
246    * @param strNewUserId The User's id
247    * that will be registered;
248    * @param strPasswd The User password;
249    */

250   public void onRegisterNewUser( java.lang.String JavaDoc strNickName, java.lang.StringBuffer JavaDoc strNewUserId, java.lang.String JavaDoc strPasswd ) {
251     
252     fireOnRegisterUserFailed(); // This plugin doesnt support this feature
253
}
254
255   /**
256    * Implement the onUnregisterUser event
257    * listener.
258    */

259   public void onUnregisterUser() {
260    
261     if( isConnected() ) {
262       fireOnLogout();
263       fireOnUnregisterUserSuccessfull();
264     }
265   }
266   
267   /**
268    * Implement the onChangePassword event
269    * listener
270    * @strNewPasswd The new user's password;
271    */

272   public void onChangePassword( java.lang.String JavaDoc strNewPasswd ) {
273     
274     fireOnPasswordChangedSuccessfull( strNewPasswd );
275   }
276   
277   /**
278    * Implement the onChange privacy event
279    * handler.
280    * Performs a privacy control to a user
281    * for this plugin.
282    * @param strUserId The user that new privacy
283    * will be applied;
284    * @param nPrivacyType The privacy type (@see JPlugin
285    * for available types);
286    */

287   public void onChangePrivacy( java.lang.String JavaDoc strUserId, int nPrivacyType ) {
288
289     switch( nPrivacyType ) {
290
291       case PRIVACY_BLOCK_USER : {
292                                      try {
293                                        session.blockContact( strUserId );
294                                      } catch( MSNException e ) {
295                                        System.err.println( "JMSNPlugin.onChangePrivacy() - " + e );
296                                      }
297                                    } break;
298
299       case PRIVACY_UNBLOCK_USER : {
300                                      try {
301                                        session.allowContact( strUserId );
302                                      } catch( MSNException e ) {
303                                        System.err.println( "JMSNPlugin.onChangePrivacy() - " + e );
304                                      }
305                                    }
306     }
307   }
308   
309   /**
310    * Implement the onPrivacyChanged event handler.
311    * This event is generated after all processing
312    * onChangePrivacy events.
313    */

314   public void onPrivacyChanged() {
315     
316   }
317   
318   /**
319    * Implement the onPluginPropertiesUpdated event handler.<br>
320    */

321   public void onPluginPropertiesUpdated() {
322     
323     if( isConnected() ) {
324       try {
325         System.err.println( "JMSNPlugin.onPluginPropertiesUpdated() - MSN properties updated by user" );
326         
327         session.setFriendlyName( getAliasName() );
328       } catch( MSNException e ) {
329         System.err.println( "JMSNPlugin.onPluginPropertiesUpdated() - " + e );
330       }
331     }
332   }
333
334   /**
335    * Returns the plugin connection
336    * status .
337    */

338   public boolean isConnected() {
339     
340     return session.isConnected();
341   }
342   
343   // JMML Listener functions
344
/**
345    * Implements the loginAccepted event
346    * listener.
347    */

348   public void loginAccepted() {
349
350     System.err.println( "JMSNPlugin.loginAccepted() - reached" );
351     fireOnLoginAccepted();
352     fireOnRegisterUserSucessfull(); // Called here because this login could be a user registration (onRegisterexistingUser)
353

354     /*
355      * Always set the friendly name with database stored
356      * friendly name.
357      */

358     try {
359       session.setFriendlyName( getAliasName() );
360     } catch( MSNException e ) {
361       System.err.println( "JMSNPlugin.loginAccepted() - " + e );
362     }
363   }
364
365   /**
366    * Implements the loginError event
367    * listener.
368    */

369   public void loginError() {
370     
371     System.err.println( "JMSNPlugin.loginError() - reached" );
372     fireOnLoginFailed();
373     fireOnRegisterUserFailed(); // Called here because this login could be a user registration (onRegisterexistingUser)
374
}
375
376   /**
377    * Implements the jmml onReceive
378    * message function.
379    * @param event The message event object (See JMML);
380    */

381   public void incomingMessage( IncomingMessageEvent event ) {
382         
383     System.err.println( "JMSNPlugin.incomingMessage() - Message from " + event.getUserName() );
384         
385     fireOnReceiveMessage( event.getUserName(), null, event.getMessage() );
386   }
387
388   /**
389    * Implements the jmml Property
390    * event function.
391    * @param event The contact event object (See JMML);
392    */

393   public void contactPropertyChanged( ContactChangeEvent event ) {
394       
395     System.err.println( "JMSNPlugin.contactPropertyChanged() - " + event );
396         
397     // Buddy Status
398
switch( event.getProperty() ) {
399       
400       case Contact.STATUS :
401         fireOnBuddyStatusChanged( event.getUserName(), translateStatusFromPlugin( event.getNewValue().toString() ) );
402         break;
403         
404       case Contact.FRIENDLY_NAME :
405         getKernelManager().getContactListManager().updateScreenName( this, event.getUserName(), event.getNewValue().toString() );
406         break;
407         
408       case Contact.CAPABILITIES :
409         System.err.println( "JMSNPlugin.contactPropertyChanged() - Contact capabilities changed to [" + event.getNewValue().toString() + "]" );
410         break;
411     }
412   }
413
414   /**
415    * Implements the jmml reverseListChanged
416    * function.
417    * @param strUserName The user that generated
418    * this event.
419    */

420   public void reverseListChanged( String JavaDoc strUserName ) {
421       
422     System.err.println( "JMSNPlugin.reverseListChanged() - " + strUserName );
423   }
424
425   /**
426    * Implements the serverDisconnected
427    * method.
428    */

429   public void serverDisconnected() {
430     
431     System.err.println( "JMSNPlugin.serverDisconnected()" );
432     
433     fireOnLogout();
434   }
435
436   /**
437    * Implements the groupReceived
438    * method.
439    * @param nGroupId The group id received by JMML;<br>
440    * @param strGroupName The group name received;<br>
441    */

442   public void groupReceived( int nGroupId, String JavaDoc strGroupName ) {
443     
444   }
445   
446   /**
447    * New Group added.<br>
448    * @param nGroupId The group id received by JMML;<br>
449    * @param strGroupName The group name received;<br>
450    */

451   public void groupAdded( int nGroupId, String JavaDoc strGroupName ) {
452     
453   }
454   
455   /**
456    * Group removed.<br>
457    * @param nGroupId The group id received by JMML;<br>
458    * @param strGroupName The group name received;<br>
459    */

460   public void groupRemoved( int nGroupId, String JavaDoc strGroupName ) {
461     
462   }
463   
464   /**
465    * Group renamed.<br>
466    * @param nGroupId The group id received by JMML;<br>
467    * @param strNewGroupName The group name received;<br>
468    */

469   public void groupRenamed( int nGroupId, String JavaDoc strNewGroupName ) {
470     
471   }
472     
473   /**
474    * Implements the contactReceived
475    * Update the PM database if contact received
476    * not in database contact list.
477    * method.
478    * @param contact The contact received;
479    */

480   public void contactReceived( org.hn.sleek.jmml.Contact contact ) {
481     
482     JContactListItem contactItem = getKernelManager().getContactListManager().getContact( this, contact.getUserName() );
483     
484     
485     if( contactItem == null ) {
486       
487       // Add/update the contact list with server contacts received
488
if( contact.isContact() ) {
489         getKernelManager().getContactListManager().addToContactList( this, contact.getFriendlyName(), contact.getUserName() );
490         contactItem = getKernelManager().getContactListManager().getContact( this, contact.getUserName() );
491       }
492       
493       // New Contact is requesting authorization
494
//if( contact.isReverse() )
495
// fireOnBuddyRequestAuthorization( contact.getUserName(), contact.getFriendlyName() );
496
}
497
498     // Update the blocked user into contact list
499
if( contactItem != null ) {
500       contactItem.setBlocked( contact.isBlocked() );
501       contactItem.setVisible( contact.isAllowed() );
502       getKernelManager().getContactListManager().updatePrivacy( contactItem );
503     }
504   }
505
506   /**
507    * Implements the contactAdded method.
508    * @param contact The contact added to contact list;<br>
509    */

510   public void contactAdded( org.hn.sleek.jmml.Contact contact ) {
511
512     // User has interest in contact you or see your status modifications
513
if( contact.isReverse() )
514       fireOnBuddyRequestAuthorization( contact.getUserName(), contact.getFriendlyName() );
515   }
516   
517   /**
518    * Implements the contactRemoved method.<br>
519    * @param contact The contact removed;<br>
520    */

521   public void contactRemoved( org.hn.sleek.jmml.Contact contact ) {
522     
523   }
524   
525   /**
526    * Error to add contact to contact list.<br>
527    * @param nReason The reason code sent by JMML library.
528    */

529   public void contactAddedFailed( int nReason ) {
530     
531   }
532   
533   /**
534    * Error to remove contact from contact list.
535    * @param nReason The reason code sent by JMML library.
536    */

537   public void contactRemovedFailed( int nReason ) {
538     
539   }
540   
541   /**
542    * Implements the userTyping
543    * method.
544    * @param contact The contact related to event;
545    */

546   public void userTyping( Contact contact ) {
547     
548     System.err.println( "JMSNPlugin.userTyping() - " + contact.getUserName() + " is typing a message" );
549     fireOnUserTyping( contact.getUserName() );
550   }
551   
552   /**
553    * Protocol error report.<br>
554    * @param nErrorCode The error code received by JMML library.
555    */

556   public void errorReceived( int nErrorCode ) {
557     
558     System.err.println( "JMSNPlugin.errorReceived() - Protocol error received" );
559   }
560   
561   /**
562    * Server Notification Messages.
563    * @param nMessageType The kind of message sent by server;
564    * @param hServerData The data received from MSN server.
565    */

566   public void serverNotificationMessage( int nMessageType, Hash hServerData ) {
567     
568     System.err.println( "JMSNPlugin.serverNotificationMessage() - Message type " + nMessageType );
569     hServerData.toString();
570     
571     switch( nMessageType ) {
572       
573       case MSNConstants.MSG_NEW_EMAIL_NOTIFICATION :
574       case MSNConstants.MSG_INITIAL_EMAIL_NOTIFICATION : hEmailNotification = hServerData; break;
575
576       default : if( ( nMessageType >= MSNConstants.MSG_PROFILE_MESSAGES_BEGIN ) &&
577                      ( nMessageType <= MSNConstants.MSG_PROFILE_MESSAGES_END ) ) {
578         System.err.println( "JMSNPlugin.serverNotificationMessage() - Profile received" );
579         hProfileNotification = hServerData;
580         
581         try {
582           session.requestServiceURL( MSNConstants.SERVICE_URL_INBOX );
583         } catch( MSNException e ) {
584           System.err.println( "JMSNPlugin.serverNotificationMessage() - " + e );
585         }
586       }
587         
588     }
589   }
590   
591   /**
592    * Notification for requestServiceURL.
593    * Perform the Hotmail/MSN login process.
594    * @param nURLId The URL Id;
595    * @param strFolderPath The path of requested service;
596    * @param strHttpAddress The Http URL address for requested service;
597    */

598   public void urlReceived( int nURLId, String JavaDoc strFolderPath, String JavaDoc strHttpAddress ) {
599
600     EMailHandler email = new EMailHandler( getUserId(), strPasswd, hProfileNotification, hEmailNotification, nURLId, strFolderPath, strHttpAddress );
601
602     System.err.println( "JMSNPlugin.urlReceived() - " + email.toString() );
603   }
604   
605   /**
606    * Notice event received.
607    * @param strNoticeMessage The received notification message;
608    */

609   public void noticeReceived( String JavaDoc strNotificationMessage ) {
610     
611     System.err.println( "JMSNPlugin.noticeReceived() - " + strNotificationMessage );
612   }
613   
614   /**
615    * Notification for all participants present on existing chat
616    * session;
617    * @param contact The existing contact on chat;
618    * @param nContactNumber The contact number on chat;
619    * @param nNumParticipants Number of participants on chat session;
620    */

621   public void participantOnChat( Contact contact, int nContactNumber, int nNumParticipants ) {
622     
623     System.err.println( "JMSNPlugin.participantOnChat() - " + contact.getFriendlyName() + "(" + contact.getUserName() + ") is contact " + nContactNumber + " of " + nNumParticipants );
624   }
625
626 }
627 // JMSNPlugin class
628
Popular Tags