1 29 38 39 package org.planetamessenger.protocols.yahoo; 40 41 import ymsg.network.*; 42 import ymsg.network.event.*; 43 import java.awt.*; 44 import java.awt.event.*; 45 import java.util.*; 46 import java.io.IOException ; 47 import org.planetamessenger.plugin.*; 48 49 50 51 public class JYahooPlugin extends JPlugin implements SessionListener, JMessengerEventListener { 52 53 Session session = null; 54 55 56 59 public JYahooPlugin() { 60 61 } 64 65 69 public void onCreate() { 70 71 super.onCreate(); 72 73 JPluginCapabilities capabilities = getPluginCapabilities(); 74 75 capabilities.putBool( JPluginCapabilities.CAPABILITY_OFFLINE_MESSAGE, true ); 77 capabilities.putBool( JPluginCapabilities.CAPABILITY_PRIVACY_CONTROL_ENABLED, true ); 78 capabilities.putBool( JPluginCapabilities.CAPABILITY_BLOCK_CONTACT, true ); 79 setPluginCapabilities( capabilities ); 80 81 addMessengerEventListener( this ); 82 83 addStatus( JPlugin.STATUS_ONLINE, new Long ( StatusConstants.STATUS_AVAILABLE ) ); 85 addStatus( JPlugin.STATUS_CUSTOM, new Long ( StatusConstants.STATUS_CUSTOM ) ); 86 addStatus( JPlugin.STATUS_OFFLINE, new Long ( StatusConstants.STATUS_OFFLINE ) ); 87 addStatus( JPlugin.STATUS_INVISIBLE, new Long ( StatusConstants.STATUS_INVISIBLE ) ); 88 addStatus( JPlugin.STATUS_BUSY, new Long ( StatusConstants.STATUS_BUSY ) ); 89 addStatus( JPlugin.STATUS_IDLE, new Long ( StatusConstants.STATUS_IDLE ) ); 90 addStatus( JPlugin.STATUS_DND, new Long ( StatusConstants.STATUS_BRB ) ); 91 addStatus( JPlugin.STATUS_ON_PHONE, new Long ( StatusConstants.STATUS_ONPHONE ) ); 92 addStatus( JPlugin.STATUS_ON_LUNCH, new Long ( StatusConstants.STATUS_OUTTOLUNCH ) ); 93 addStatus( JPlugin.STATUS_NOT_AT_HOME, new Long ( StatusConstants.STATUS_NOTATHOME ) ); 94 addStatus( JPlugin.STATUS_NOT_IN_OFFICE, new Long ( StatusConstants.STATUS_NOTINOFFICE ) ); 95 addStatus( JPlugin.STATUS_NOT_AT_DESK, new Long ( StatusConstants.STATUS_NOTATDESK ) ); 96 addStatus( JPlugin.STATUS_ON_VACATION, new Long ( StatusConstants.STATUS_ONVACATION ) ); 97 addStatus( JPlugin.STATUS_AWAY, new Long ( StatusConstants.STATUS_STEPPEDOUT ) ); 98 } 99 100 104 public void onLogin( int nPluginStatus ) { 105 106 110 session = new Session( new DirectConnectionHandler() ); 111 session.addSessionListener( this ); 112 113 try { 114 session.login( getUserId(), getPassword() ); 115 } catch( AccountLockedException e ) { 116 System.err.println( "JYahooPlugin.onLogin() - Your account is locked" ); 117 fireOnLoginFailed(); 118 fireOnRegisterUserFailed(); 120 if( e.getWebPage() != null ) 121 System.err.println( "JYahooPlugin.onLogin() - Please visit: " + e.getWebPage().toString() ); 122 123 session = null; 124 return; 125 } catch( LoginRefusedException e ) { 126 System.err.println( "JYahooPlugin.onLogin() - Yahoo refused our connection. Username/password incorrect?" ); 127 fireOnLoginFailed(); 128 fireOnRegisterUserFailed(); session = null; 130 return; 131 } catch( java.io.IOException e ) { 132 System.err.println( "JYahooPlugin.onLogin() - " + e ); 133 fireOnLoginFailed(); 134 fireOnRegisterUserFailed(); session = null; 136 return; 137 } 138 139 if( session.getSessionStatus() == StatusConstants.MESSAGING ) { 141 System.err.println( "JYahooPlugin.onLogin() - " + session.getConnectionHandler().toString() ); 142 fireOnLoginAccepted(); 143 fireOnRegisterUserSucessfull(); fireOnPluginStatusChanged( nPluginStatus ); 145 146 try { 147 session.refreshFriends(); 148 } catch( java.io.IOException e ) { 149 System.err.println( "JYahooPlugin.onLogin() - " + e ); 150 } 151 } 152 else { 153 System.err.println( "JYahooPlugin.onLogin() - Sorry, there was a problem connecting" ); 154 fireOnLoginFailed(); 155 fireOnRegisterUserFailed(); session = null; 157 } 158 } 159 160 163 public void onLogout() { 164 165 if( isConnected() != false ) { 166 try { 167 session.logout(); 168 session.removeSessionListener( this ); 169 session.reset(); 170 } catch( IllegalStateException e ) { 171 System.err.println( "JYahooPlugin.onLogin() - " + e ) ; 172 } catch( IOException e ) { 173 System.err.println( "JYahooPlugin.onLogin() - " + e ) ; 174 } 175 176 session = null; 177 } 178 } 179 180 184 public void onAddUserToContactList( java.lang.String strUserId ) { 185 186 if( session != null ) { 187 try { 188 session.addFriend( strUserId, "planetamessenger" ); 189 } catch( IllegalStateException e ) { 190 System.err.println( "JYahooPlugin.onAddUserToContactList() - " + e ); 191 } catch( java.io.IOException e ) { 192 System.err.println( "JYahooPlugin.onAddUserToContactList() - " + e ); 193 } 194 } 195 } 196 197 202 public void onRemoveUserFromContactList( java.lang.String strUserId ) { 203 204 if( session != null ) { 205 try { 206 session.removeFriend( strUserId, "planetamessenger" ); 207 } catch( IllegalStateException e ) { 208 System.err.println( "JYahooPlugin.onRemoveUserFromContactList() - " + e ); 209 } catch( java.io.IOException e ) { 210 System.err.println( "JYahooPlugin.onRemoveUserFromContactList() - " + e ); 211 } 212 } 213 } 214 215 224 public void onSendMessage( java.lang.String strToUser, java.lang.String msg, int nMsgType ) { 225 226 if( session != null ) { 227 try { 228 session.sendMessage( strToUser, msg ); 229 } catch( IllegalStateException e ) { 230 System.err.println( "JYahooPlugin.onSendMessage() - " + e ); 231 } catch( java.io.IOException e ) { 232 System.err.println( "JYahooPlugin.onSendMessage() - " + e ); 233 } 234 } 235 } 236 237 245 public void onRequestAuthorization( java.lang.String strUserId, java.lang.String strNickName ) { 246 247 } 248 249 254 public void onBuddyDetails( java.lang.String strUserId ) { 255 256 } 257 258 262 public void onAuthorizeBuddy( java.lang.String strUserId ) { 263 264 onChangePrivacy( strUserId, PRIVACY_UNBLOCK_USER ); 265 } 266 267 271 public void onUnauthorizeBuddy( java.lang.String strUserId ) { 272 273 onChangePrivacy( strUserId, PRIVACY_BLOCK_USER ); 274 } 275 276 280 public void onPluginStatusChanged( int nStatus ) { 281 282 if( session != null ) { 283 try { 284 session.setStatus( ( ( Long ) translateStatusToPlugin( nStatus ) ).longValue() ); 285 } catch( IllegalArgumentException e ) { 286 System.err.println( "JYahooPlugin.onSendMessage() - " + e ); 287 } catch( java.io.IOException e ) { 288 System.err.println( "JYahooPlugin.onSendMessage() - " + e ); 289 } 290 } 291 } 292 293 302 public void onRegisterExistingUser( String strNickName, String strNewUserId, String strPasswd ) { 303 304 JUserInfo userInfo = new JUserInfo( strNewUserId, strNickName, strPasswd, JPlugin.STATUS_ONLINE ); 305 306 setUserInfo( userInfo ); 307 fireOnLogin(); 308 } 309 310 319 public void onRegisterNewUser( java.lang.String strNickName, java.lang.StringBuffer strNewUserId, java.lang.String strPasswd ) { 320 321 fireOnRegisterUserFailed(); } 323 324 328 public void onUnregisterUser() { 329 330 if( isConnected() ) { 331 fireOnLogout(); 332 fireOnUnregisterUserSuccessfull(); 333 } 334 } 335 336 341 public void onChangePassword( java.lang.String strNewPasswd ) { 342 343 fireOnPasswordChangedSuccessfull( strNewPasswd ); 344 } 345 346 356 public void onChangePrivacy( java.lang.String strUserId, int nPrivacyType ) { 357 358 try { 359 switch( nPrivacyType ) { 360 361 case PRIVACY_BLOCK_USER : { 362 368 session.removeFriend( strUserId, "planetamessenger" ); 369 System.err.println( "JYahooPlugin.onChangePrivacy() - User [" + strUserId + "] now is denied." ); 370 } break; 371 372 case PRIVACY_UNBLOCK_USER : { 373 376 session.ignoreContact( strUserId, false ); 377 session.addFriend( strUserId, "planetamessenger" ); 378 System.err.println( "JYahooPlugin.onChangePrivacy() - User [" + strUserId + "] now is granted." ); 379 } break; 380 } 381 } catch( java.lang.IllegalStateException ie ) { 382 System.err.println( "JYahooPlugin.onChangePrivacy() - " + ie ); 383 } catch( java.io.IOException e ) { 384 System.err.println( "JYahooPlugin.onChangePrivacy() - " + e ); 385 } 386 } 387 388 393 public void onPrivacyChanged() { 394 395 } 396 397 400 public void onPluginPropertiesUpdated() { 401 402 } 403 404 408 public boolean isConnected() { 409 410 return ( ( session != null ? ( session.getSessionStatus() == StatusConstants.MESSAGING ) : false ) ); 411 } 412 413 418 public void messageReceived( ymsg.network.event.SessionEvent sessionEvent ) { 419 420 System.err.println( "JYahooPlugin.messageReceived() - Message from " + sessionEvent.getFrom() ); 421 422 fireOnReceiveMessage( sessionEvent.getFrom(), null, sessionEvent.getMessage() ); 423 } 424 425 429 public void offlineMessageReceived( ymsg.network.event.SessionEvent sessionEvent ) { 430 431 fireOnReceiveMessage( sessionEvent.getFrom(), null, sessionEvent.getMessage() ); 432 } 433 434 438 public void notifyReceived( ymsg.network.event.SessionNotifyEvent sessionNotifyEvent ) { 439 440 System.err.println( "JYahooPlugin.notifyReceived() - Message from " + sessionNotifyEvent.getFrom() ); 441 } 442 443 447 public void logoffReceived( ymsg.network.event.SessionEvent sessionEvent ) { 448 449 System.err.println( "JYahooPlugin.logoffReceived() - Message from " + sessionEvent.getFrom() ); 450 451 fireOnLogout(); 452 } 453 454 458 public void listReceived( ymsg.network.event.SessionEvent sessionEvent ) { 459 460 System.err.println( "JYahooPlugin.listReceived()" ); 461 462 Iterator<YahooUser> itUsers = ( session.getUsers() == null ? null : session.getUsers().values().iterator() ); 463 464 while( itUsers.hasNext() ) { 465 466 YahooUser user = itUsers.next(); 467 468 if( !getKernelManager().getContactListManager().isContact( this, user.getId() ) ) { 469 470 JContactListItem contactItem; 471 472 getKernelManager().getContactListManager().addToContactList( this, user.getId(), user.getId() ); 473 contactItem = ( JContactListItem ) getKernelManager().getContactListManager().getContact( this, user.getId() ); 474 contactItem.setBlocked( user.isIgnored() ); 475 contactItem.setVisible( user.isFriend() ); 476 getKernelManager().getContactListManager().updatePrivacy( contactItem ); 477 } 478 } 479 } 480 481 485 public void friendsUpdateReceived( ymsg.network.event.SessionFriendEvent sessionFriendEvent ) { 486 487 System.err.println( "JYahooPlugin.friendsUpdateReceived()" ); 488 489 YahooUser users[] = sessionFriendEvent.getFriends(); 490 491 for( int nCount = 0; nCount < users.length; nCount++ ) { 492 493 if( !getKernelManager().getContactListManager().isContact( this, users[nCount].getId() ) ) { 495 JContactListItem contactItem; 496 497 getKernelManager().getContactListManager().addToContactList( this, users[nCount].getId(), users[nCount].getId() ); 498 contactItem = ( JContactListItem ) getKernelManager().getContactListManager().getContact( this, users[nCount].getId() ); 499 contactItem.setBlocked( users[nCount].isIgnored() ); 500 contactItem.setVisible( users[nCount].isFriend() ); 501 getKernelManager().getContactListManager().updatePrivacy( contactItem ); 502 } 503 504 if( users[nCount].getStatus() != StatusConstants.STATUS_OFFLINE ) fireOnBuddyStatusChanged( users[nCount].getId(), translateStatusFromPlugin( new Long ( users[nCount].getStatus() ) ) ); 506 else 507 fireOnBuddyStatusChanged( users[nCount].getId(), JPlugin.STATUS_OFFLINE ); 508 } 509 } 510 511 515 public void friendRemovedReceived( ymsg.network.event.SessionFriendEvent sessionFriendEvent ) { 516 517 System.err.println( "JYahooPlugin.friendRemovedReceived() - " + sessionFriendEvent.getFriend().getId() ); 518 519 String strUserId = sessionFriendEvent.getFriend().getId(); 520 JContactListItem contact = getKernelManager().getContactListManager().getContact( this, strUserId ); 521 522 523 if( contact.getBlocked() ) 524 try { 525 session.ignoreContact( strUserId, true ); 526 } catch( java.lang.IllegalStateException e ) { 527 System.err.println( "JYahooPlugin.friendRemovedReceived() - " + e ); 528 } catch( java.io.IOException ioe ) { 529 System.err.println( "JYahooPlugin.friendRemovedReceived() - " + ioe ); 530 } 531 532 getKernelManager().getContactListManager().setItemStatus( this, strUserId, JPlugin.STATUS_OFFLINE ); 533 } 534 535 539 public void friendAddedReceived( ymsg.network.event.SessionFriendEvent sessionFriendEvent ) { 540 541 } 542 543 547 public void conferenceInviteReceived( ymsg.network.event.SessionConferenceEvent sessionConferenceEvent ) { 548 549 } 550 551 555 public void conferenceInviteDeclinedReceived( ymsg.network.event.SessionConferenceEvent sessionConferenceEvent ) { 556 557 } 558 559 563 public void conferenceLogoffReceived( ymsg.network.event.SessionConferenceEvent sessionConferenceEvent ) { 564 565 } 566 567 571 public void conferenceLogonReceived( ymsg.network.event.SessionConferenceEvent sessionConferenceEvent ) { 572 573 574 } 575 576 580 public void conferenceMessageReceived( ymsg.network.event.SessionConferenceEvent sessionConferenceEvent ) { 581 582 } 583 584 588 public void contactRejectionReceived( ymsg.network.event.SessionEvent sessionEvent ) { 589 590 } 591 592 596 public void contactRequestReceived( ymsg.network.event.SessionEvent sessionEvent ) { 597 598 } 599 600 604 public void errorPacketReceived( ymsg.network.event.SessionErrorEvent sessionErrorEvent ) { 605 606 } 607 608 612 public void fileTransferReceived( ymsg.network.event.SessionFileTransferEvent sessionFileTransferEvent ) { 613 614 } 615 616 620 public void inputExceptionThrown( ymsg.network.event.SessionExceptionEvent sessionExceptionEvent ) { 621 622 } 623 624 628 public void newMailReceived( ymsg.network.event.SessionNewMailEvent sessionNewMailEvent ) { 629 630 } 631 632 636 public void buzzReceived( ymsg.network.event.SessionEvent sessionEvent ) { 637 638 } 639 640 644 public void chatLogoffReceived( ymsg.network.event.SessionChatEvent sessionChatEvent ) { 645 646 } 647 648 652 public void chatLogonReceived( ymsg.network.event.SessionChatEvent sessionChatEvent ) { 653 654 } 655 656 660 public void chatMessageReceived( ymsg.network.event.SessionChatEvent sessionChatEvent ) { 661 662 } 663 664 668 public void chatConnectionClosed( ymsg.network.event.SessionEvent sessionEvent ) { 669 670 } 671 672 676 public void chatUserUpdateReceived( ymsg.network.event.SessionChatEvent sessionChatEvent ) { 677 678 } 679 680 684 public void connectionClosed( ymsg.network.event.SessionEvent sessionEvent ) { 685 686 } 687 688 689 } 690 | Popular Tags |