java实现msn机器人 jml
import net.sf.jml.MsnContact;import net.sf.jml.MsnContactPending;import net.sf.jml.MsnGroup;import net.sf.jml.MsnList;import net.sf.jml.MsnMessenger;import net.sf.jml.MsnSwitchboard;import net.sf.jml.event.MsnContactListListener;import net.sf.jml.event.MsnMessageListener;import net.sf.jml.impl.MsnMessengerFactory;import net.sf.jml.message.MsnControlMessage;import net.sf.jml.message.MsnDatacastMessage;import net.sf.jml.message.MsnInstantMessage;import net.sf.jml.message.MsnSystemMessage;import net.sf.jml.message.MsnUnknownMessage;import net.sf.jml.message.p2p.MsnP2PMessage;@SuppressWarnings("unused")public class MSNDaemonOther{private String account="mak0000@msn.com";private String password="*************";private MsnMessenger messenger=null;public MSNDaemonOther(){}private void start(){this.messenger=MsnMessengerFactory.createMsnMessenger(account,password);this.messenger.addMessageListener(new MsnMessageAdapter(this.messenger));this.messenger.setLogIncoming(true); this.messenger.setLogOutgoing(true);this.messenger.login();}public void end(){this.messenger.logout();}public static void main(String[] args){MSNDaemonOther msn=new MSNDaemonOther();msn.start();}}class MsnMessageAdapter implements MsnMessageListener{private MsnMessenger messenger=null;public MsnMessageAdapter(MsnMessenger messenger) {super();this.messenger = messenger;}public void controlMessageReceived(MsnSwitchboard arg0,MsnControlMessage arg1, MsnContact arg2) { System.out.println("正在输入"+arg2.getEmail());}public void datacastMessageReceived(MsnSwitchboard arg0,MsnDatacastMessage arg1, MsnContact arg2) {System.out.println("2");}public void instantMessageReceived(MsnSwitchboard arg0,MsnInstantMessage arg1, MsnContact arg2) {System.out.println("收到消息:"+arg1.getContent());if(arg1.getContent().equals("-h"))this.messenger.sendText(arg2.getEmail(),"1");else if(arg1.getContent().equals("-v"))this.messenger.sendText(arg2.getEmail(),"2");else if(arg1.getContent().equals("-q"))this.messenger.sendText(arg2.getEmail(),"3");else if(arg1.getContent().equals("-x"))this.messenger.sendText(arg2.getEmail(),"4");elsethis.messenger.sendText(arg2.getEmail(),"小孟在线 mengdejun.javaeye.com \r-h帮助 \r-v 验证\r-q 退出\r-x 其他");}public void offlineMessageReceived(String arg0, String arg1, String arg2,MsnContact arg3) {System.out.println("收到在线消息 1:"+arg0+" 2:"+arg2);}public void p2pMessageReceived(MsnSwitchboard arg0, MsnP2PMessage arg1,MsnContact arg2){System.out.println("收到P2P消息:"+arg1.getAppId());}public void systemMessageReceived(MsnMessenger arg0, MsnSystemMessage arg1) {System.out.println("收到系统消息:"+arg1.getContent());}public void unknownMessageReceived(MsnSwitchboard arg0,MsnUnknownMessage arg1, MsnContact arg2) {System.out.println("收到未知消息:"+arg1.getContentAsString());}}class MsnContactListAdapter implements MsnContactListListener{private MsnMessenger messenger=null;public MsnContactListAdapter(MsnMessenger messenger) {super();this.messenger = messenger;}public void contactAddCompleted(MsnMessenger arg0, MsnContact arg1,MsnList arg2) {// TODO Auto-generated method stub}public void contactAddInGroupCompleted(MsnMessenger arg0, MsnContact arg1,MsnGroup arg2) {// TODO Auto-generated method stub}public void contactAddedMe(MsnMessenger arg0, MsnContact arg1) {// TODO Auto-generated method stub}public void contactAddedMe(MsnMessenger arg0, MsnContactPending[] arg1) {// TODO Auto-generated method stub}public void contactListInitCompleted(MsnMessenger arg0) {// TODO Auto-generated method stub}public void contactListSyncCompleted(MsnMessenger arg0) {// TODO Auto-generated method stub}public void contactPersonalMessageChanged(MsnMessenger arg0, MsnContact arg1) {// TODO Auto-generated method stub}public void contactRemoveCompleted(MsnMessenger arg0, MsnContact arg1,MsnList arg2) {// TODO Auto-generated method stub}public void contactRemoveFromGroupCompleted(MsnMessenger arg0,MsnContact arg1, MsnGroup arg2) {// TODO Auto-generated method stub}public void contactRemovedMe(MsnMessenger arg0, MsnContact arg1) {// TODO Auto-generated method stub}public void contactStatusChanged(MsnMessenger arg0, MsnContact arg1) {// TODO Auto-generated method stub}public void groupAddCompleted(MsnMessenger arg0, MsnGroup arg1) {// TODO Auto-generated method stub}public void groupRemoveCompleted(MsnMessenger arg0, MsnGroup arg1) {// TODO Auto-generated method stub}public void ownerDisplayNameChanged(MsnMessenger arg0) {// TODO Auto-generated method stub}public void ownerStatusChanged(MsnMessenger arg0) {// TODO Auto-generated method stub}}
?
package net.sf.jml.example;import net.sf.jml.MsnContact;import net.sf.jml.MsnLogoutReason;import net.sf.jml.MsnMessenger;import net.sf.jml.MsnProtocol;import net.sf.jml.MsnSwitchboard;import net.sf.jml.MsnUserStatus;import net.sf.jml.event.MsnAdapter;import net.sf.jml.event.MsnContactListAdapter;import net.sf.jml.impl.MsnContactImpl;import net.sf.jml.impl.MsnMessengerFactory;import net.sf.jml.message.MsnControlMessage;import net.sf.jml.message.MsnDatacastMessage;import net.sf.jml.message.MsnInstantMessage;import net.sf.jml.message.MsnSystemMessage;import net.sf.jml.message.MsnUnknownMessage;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/*** @author Roger Chen*/public class SimpleMsn {private static final Log log = LogFactory.getLog(SimpleMsn.class);private String email;private String password;private MsnMessenger messenger;public SimpleMsn(String email, String password) { this.email = email; this.password = password;}public void start() { messenger = MsnMessengerFactory.createMsnMessenger(email, password);//创建MsnMessenger messenger.setSupportedProtocol(new MsnProtocol[] { MsnProtocol.MSNP11 });//设置登录协议 messenger.getOwner().setInitStatus(MsnUserStatus.ONLINE);//设置用户状态 messenger.setLogIncoming(true);// messenger.setLogOutgoing(true);// //添加监听 messenger.addListener(new MsnListener()); //登录 messenger.login(); messenger.addContactListListener(new MsnContactListAdapter() { public void contactStatusChanged(MsnMessenger msn, MsnContact con) { System.out.println(con.getDisplayName()); System.out.println(con.getPersonalMessage()); System.out.println(((MsnContactImpl) con).getCurrentMedia()); } public void contactListInitCompleted(MsnMessenger messenger) { listContacts(); } });}private void listContacts() { //取得MSN好友列表 MsnContact[] cons = messenger.getContactList().getContacts(); for (MsnContact con : cons) { System.out.println(con.getDisplayName()); System.out.println(con.getEmail()); System.out.println(con.getStatus()); System.out.println(con.getPersonalMessage()); //发送信息 if(con.getStatus() != MsnUserStatus.OFFLINE) { messenger.sendText(con.getEmail(),"hello"); } }}public static void main(String[] args) throws Exception { String email = www.xiaoshanju; String password = "萧山居"; new SimpleMsn(email, password).start();}private static class MsnListener extends MsnAdapter { //异常时发生 public void exceptionCaught(MsnMessenger messenger, Throwable throwable) { log.error(messenger + throwable.toString(), throwable); try { Thread.sleep(30000); } catch (InterruptedException e) { e.printStackTrace(); } messenger.login(); } //登录完成时发生 public void loginCompleted(MsnMessenger messenger) { log.info(messenger + " login complete "); } //注销时发生 public void logout(MsnMessenger messenger, MsnLogoutReason reason) { log.info(messenger + " logout"); if (reason == MsnLogoutReason.SessionClosed) { messenger.login(); } } //收到正常信息的时候发生 public void instantMessageReceived(MsnSwitchboard switchboard, MsnInstantMessage message, MsnContact friend) { log.info(switchboard + " recv instant message " + message); switchboard.sendMessage(message, false); } //收到系统信息的时候发生,登录时 public void systemMessageReceived(MsnMessenger messenger, MsnSystemMessage message) { log.info(messenger + " recv system message " + message); } //当在联系人聊天窗口获得光标并按下第一个键时发生 public void controlMessageReceived(MsnSwitchboard switchboard, MsnControlMessage message, MsnContact contact) { log.info(switchboard + " recv control message from " + contact.getEmail()); switchboard.sendMessage(message, false); } //收到系统广播信息时发生 public void datacastMessageReceived(MsnSwitchboard switchboard, MsnDatacastMessage message, MsnContact friend) { log.info(switchboard + " recv datacast message " + message); switchboard.sendMessage(message, false); } //收到目前不能处理的信息时发生 public void unknownMessageReceived(MsnSwitchboard switchboard, MsnUnknownMessage message, MsnContact friend) { log.info(switchboard + " recv unknown message " + message); } public void contactListInitCompleted(MsnMessenger messenger) { log.info(messenger + " contact list init completeted"); } //更新好友列表完成时发生 public void contactListSyncCompleted(MsnMessenger messenger) { log.info(messenger + " contact list sync completed"); } public void contactStatusChanged(MsnMessenger messenger, MsnContact friend) { log.info(messenger + " friend " + friend.getEmail() + " status changed from " + friend.getOldStatus() + " to " + friend.getStatus()); } public void ownerStatusChanged(MsnMessenger messenger) { log.info(messenger + " status changed from " + messenger.getOwner().getOldStatus() + " to " + messenger.getOwner().getStatus()); } public void contactAddedMe(MsnMessenger messenger, MsnContact friend) { log.info(friend.getEmail() + " add " + messenger); } public void contactRemovedMe(MsnMessenger messenger, MsnContact friend) { log.info(friend.getEmail() + " remove " + messenger); } //关闭一个聊天窗口时发生 public void switchboardClosed(MsnSwitchboard switchboard) { log.info(switchboard + " closed"); } //打开一个聊天窗口时发生 public void switchboardStarted(MsnSwitchboard switchboard) { log.info(switchboard + " started"); } public void contactJoinSwitchboard(MsnSwitchboard switchboard, MsnContact friend) { log.info(friend.getEmail() + " join " + switchboard); } public void contactLeaveSwitchboard(MsnSwitchboard switchboard, MsnContact friend) { log.info(friend.getEmail() + " leave " + switchboard); }}}
?
jml的全称是"Java MSN Messenger Library (JML)"
官方地址是:http://jml.blathersource.org/
现在的最新版本是1.0
下载地址是:http://sourceforge.net/projects/java-jml/files/