1 29 38 39 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 HOSTNAME = "login.icq.com"; 58 final int PORT = 5190; 59 final int LOGIN_TIMEOUT = 60000; 60 61 javax.swing.Timer loginTimer; 62 OscarInterface oscarInterface; 63 OscarConnection session; 64 int nInitialStatus; 65 StatusListener statusObject; 66 67 68 71 public JOscarLibPlugin() { 72 73 } 74 75 79 public void onCreate() { 80 81 super.onCreate(); 82 83 JPluginCapabilities capabilities = getPluginCapabilities(); 84 85 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 ( LOGIN_TIMEOUT, this ); 93 session = null; 94 95 statusObject = new StatusListener() { 96 97 104 public void onIncomingUser( JOscarLib.Integration.Event.IncomingUserEvent incomingUserEvent ) { 105 106 fireOnBuddyStatusChanged( incomingUserEvent.getIncomingUserId(), translateStatusFromPlugin( new Integer ( incomingUserEvent.getStatusMode().getMode() ) ) ); 107 } 108 109 116 public void onOffgoingUser( JOscarLib.Integration.Event.OffgoingUserEvent offgoingUserEvent ) { 117 118 fireOnBuddyStatusChanged( offgoingUserEvent.getOffgoingUserId(), JPlugin.STATUS_OFFLINE ); 119 } 120 121 126 public void onStatusChange( StatusEvent e ) { 127 128 } 129 130 134 public void onLogout() { 135 136 fireOnLogout(); 137 } 138 }; 139 140 addStatus( JPlugin.STATUS_ONLINE, new Integer ( StatusModeEnum.ONLINE ) ); 142 addStatus( JPlugin.STATUS_OFFLINE, new Integer ( StatusModeEnum.OFFLINE ) ); 143 addStatus( JPlugin.STATUS_BUSY, new Integer ( StatusModeEnum.OCCUPIED ) ); 144 addStatus( JPlugin.STATUS_AWAY, new Integer ( StatusModeEnum.AWAY ) ); 145 addStatus( JPlugin.STATUS_DND, new Integer ( StatusModeEnum.DND ) ); 146 addStatus( JPlugin.STATUS_FREE_CHAT, new Integer ( StatusModeEnum.FREE_FOR_CHAT ) ); 147 addStatus( JPlugin.STATUS_INVISIBLE, new Integer ( StatusModeEnum.INVISIBLE ) ); 148 addStatus( JPlugin.STATUS_IDLE, new Integer ( StatusModeEnum.NA ) ); 149 } 150 151 155 public void onDestroy() { 156 157 super.onDestroy(); 158 159 session = null; 160 oscarInterface = null; 161 } 162 163 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 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 e ) { 197 System.err.println( "JOscarLibPlugin.logout() - " + e ); 198 } 199 200 session = null; 201 } 202 } 203 204 208 public void onAddUserToContactList( java.lang.String strUserId ) { 209 210 if( session != null ) 211 oscarInterface.addUser( session, strUserId ); 212 } 213 214 219 public void onRemoveUserFromContactList( java.lang.String strUserId ) { 220 221 } 222 223 232 public void onSendMessage( java.lang.String strToUser, java.lang.String msg, int nMsgType ) { 233 234 oscarInterface.sendExtendedMessage( session, strToUser, msg ); 235 } 236 237 245 public void onRequestAuthorization( java.lang.String strUserId, java.lang.String strNickName ) { 246 247 } 248 249 254 public void onBuddyDetails( String strUserId ) { 255 256 try { 257 258 try { 259 Long.parseLong( strUserId ); session.sendFlap( ClientMeta.searchByUinTlv( session, strUserId ) ); 261 } catch( NumberFormatException e ) { 262 System.err.println( "JOscarLibPlugin.onBuddyDetails() - Invalid UIN (Are you using AIM user instead ICQ user ??)" ); 263 } 264 } catch( Exception e ) { 265 System.err.println( "JOscarLibPlugin.onBuddyDetails() - " + e ); 266 } 267 } 268 269 273 public void onAuthorizeBuddy( java.lang.String strUserId ) { 274 275 } 276 277 281 public void onUnauthorizeBuddy( java.lang.String strUserId ) { 282 283 } 284 285 289 public void onPluginStatusChanged( int nStatus ) { 290 291 if( session != null ) 292 oscarInterface.changeStatus( session, new StatusModeEnum( ( ( Integer ) translateStatusToPlugin( nStatus ) ).intValue() ) ); 293 } 294 295 304 public void onRegisterExistingUser( java.lang.String strNickName, java.lang.String strNewUserId, java.lang.String strPasswd ) { 305 306 JUserInfo userInfo = new JUserInfo( strNewUserId, strNickName, strPasswd, JPlugin.STATUS_ONLINE ); 307 308 setUserInfo( userInfo ); 309 fireOnLogin(); 310 } 311 312 321 public void onRegisterNewUser( java.lang.String strNickName, java.lang.StringBuffer strNewUserId, java.lang.String strPasswd ) { 322 323 fireOnRegisterUserFailed(); } 325 326 330 public void onUnregisterUser() { 331 332 if( isConnected() ) { 333 fireOnLogout(); 334 fireOnUnregisterUserSuccessfull(); 335 } 336 } 337 338 343 public void onChangePassword( java.lang.String strNewPasswd ) { 344 345 fireOnPasswordChangedSuccessfull( strNewPasswd ); 346 } 347 348 358 public void onChangePrivacy( java.lang.String strUserId, int nPrivacyType ) { 359 360 } 361 362 367 public void onPrivacyChanged() { 368 369 } 370 371 374 public void onPluginPropertiesUpdated() { 375 376 } 377 378 382 public boolean isConnected() { 383 384 return ( session != null ? session.isLogged() : false ); 385 } 386 387 393 public void update( java.util.Observable observable, Object obj ) { 394 395 if( session.isLogged() ) { 396 StatusModeEnum status = new StatusModeEnum( ( ( Integer ) translateStatusToPlugin( nInitialStatus ) ).intValue() ); 397 Hashtable hContactList = getKernelManager().getContactListManager().getContactList( this ); 398 Enumeration eContactList = hContactList.keys(); 399 String [] oscarContactList = new String [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 try { 415 session.sendFlap( ClientMeta.requestOfflineMessages( session ) ); 416 } catch( Exception e ) { 417 System.err.println( "JOscarLibPlugin.update()" + e ); 418 } 419 420 fireOnPluginStatusChanged( status.getMode() ); 421 fireOnLoginAccepted(); 422 fireOnRegisterUserSucessfull(); } 424 } 425 426 434 public void onIncomingMessage( JOscarLib.Integration.Event.IncomingMessageEvent incomingMessageEvent ) { 435 436 fireOnReceiveMessage( incomingMessageEvent.getSenderID(), null, incomingMessageEvent.getMessage() ); 437 } 438 439 446 public void onOfflineMessage( JOscarLib.Integration.Event.OfflineMessageEvent offlineMessageEvent ) { 447 448 fireOnReceiveMessage( offlineMessageEvent.getSenderUin(), null, offlineMessageEvent.getMessage() ); 449 } 450 451 458 public void onIncomingUrl( JOscarLib.Integration.Event.IncomingUrlEvent incomingUrlEvent ) { 459 460 } 461 462 466 public void onMessageAck( final MessageAckEvent e ) { 467 468 } 469 470 474 public void onMessageError( MessageErrorEvent e ) { 475 476 System.err.println( "JOscarLibPlugin.onMessageError() - " + e ); 477 } 478 479 484 public void updateContactList( ContactListEvent event ) { 485 486 System.err.println( "JOscarLibPlugin.updateContactList()" ); 487 488 ContactListItem[] groups = event.getRoot().getContainedItems(); 489 490 491 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 if( !getKernelManager().getContactListManager().isContact( this, items[nCountItem].getId() ) ) { 500 getKernelManager().getContactListManager().addToContactList( this, items[nCountItem].getId(), items[nCountItem].getId() ); 501 onBuddyDetails( items[nCountItem].getId() ); 503 } 504 } 505 } 506 } 507 508 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 526 public void actionPerformed( java.awt.event.ActionEvent actionEvent ) { 527 528 System.err.println( "JOscarLibPlugin.actionPerformed() - Login timeout reached." ); 529 530 loginTimer.stop(); 531 session = null; 532 fireOnLoginFailed(); 533 fireOnRegisterUserFailed(); } 535 } 536 537 | Popular Tags |