private void start() { messenger = MsnMessengerFactory.createMsnMessenger(email, password); // 创建MsnMessenger messenger.setSupportedProtocol(new MsnProtocol[] {MsnProtocol.MSNP12}); // 设置登录协议 messenger.getOwner().setInitStatus(MsnUserStatus.ONLINE); // 设置用户状态 messenger.addListener(this); // 注册事件 messenger.login(); // 登录 }
public void contactListSyncCompleted(MsnMessenger arg0) { // we don't need both contactListInitCompleted and this firing. // System.out.println("Sync"); // if (haveDispatched) return; // System.out.println("Sync"); // haveDispatched = true; this.dispatchContactList(arg0.getContactList(), false); }
public void contactListInitCompleted(MsnMessenger arg0) { // this is the inital contact list // System.out.println("Init"); // if (haveDispatched) return; // System.out.println("Init"); // haveDispatched = true; this.dispatchContactList(arg0.getContactList(), false); }
@Override protected void messageReceived(MsnSession session) { super.messageReceived(session); MsnSwitchboard switchboard = session.getSwitchboard(); if (switchboard != null) { MsnMessenger messenger = session.getMessenger(); MsnContactImpl contact = (MsnContactImpl) messenger.getContactList().getContactByEmail(getEmail()); if (contact == null) { contact = new MsnContactImpl(messenger.getContactList()); contact.setEmail(getEmail()); } ((AbstractSwitchboard) switchboard).removeContact(contact); ((AbstractMessenger) messenger).fireContactLeaveSwitchboard(switchboard, contact); if (switchboard.getAllContacts().length == 0) { switchboard.close(); } } }
// 收到正常信息的时候发生 @Override public void instantMessageReceived( MsnSwitchboard switchboard, MsnInstantMessage message, MsnContact contact) { msg(contact.getDisplayName() + "对我说:" + message.getContent()); // 如果聊天内容为exit,退出 if (message.getContent().trim().equalsIgnoreCase("exit")) { msg(contact.getDisplayName() + "对我发出退出指令。"); messenger.logout(); System.exit(0); } message.setContent("Hello,我是酷印通机器人!\n现在的时间是:" + new Date()); // 设置要发送消息内容 message.setFontRGBColor((int) (Math.random() * 255 * 255 * 255)); // 设置消息的文本颜色 switchboard.sendMessage(message); // 发送信息 }
public void setStatus(Status theStatus) { MsnUserStatus toChange = null; if (theStatus.getStatus().equals(Status.available) || theStatus.getStatus().equals(Status.superAvailable)) { toChange = MsnUserStatus.ONLINE; } else if (theStatus.getStatus().equals(Status.away)) { toChange = MsnUserStatus.BE_RIGHT_BACK; } else if (theStatus.getStatus().equals(Status.doNotDistrub)) { toChange = MsnUserStatus.BUSY; } else if (theStatus.getStatus().equals(Status.superAway)) { toChange = MsnUserStatus.OUT_TO_LUNCH; } myCon.getOwner().setStatus(toChange); }
public void connect() { myCon = MsnMessengerFactory.createMsnMessenger(theAS.getUsername(), theAS.getPassword()); // turn off logging myCon.setLogIncoming(false); myCon.setLogOutgoing(false); // add ourselves to the listener lists we care about myCon.addMessageListener(this); myCon.addMessengerListener(this); myCon.addContactListListener(this); // log in myCon.login(); }
// 打印联系人 private void listContacts() { MsnContact[] cons = messenger.getContactList().getContactsInList(MsnList.AL); if (cons.length == 0) { msg("空"); } else { msg("你现在有" + cons.length + "个联系人"); } for (int i = 0; i < cons.length; i++) { String personal = ((MsnContactImpl) cons[i]).getPersonalMessage().equals("") ? "空" : ((MsnContactImpl) cons[i]).getPersonalMessage(); msg( cons[i].getDisplayName() + " " + cons[i].getEmail() + " " + cons[i].getStatus() + " " + personal); } }
protected void initMessenger(MsnMessenger messenger) { messenger.addMessageListener( new MsnMessageAdapter() { public void instantMessageReceived( MsnSwitchboard switchboard, MsnInstantMessage message, MsnContact contact) { // text message received switchboard.sendMessage(message); } public void controlMessageReceived( MsnSwitchboard switchboard, MsnControlMessage message, MsnContact contact) { // such as typing message and recording message switchboard.sendMessage(message); } public void datacastMessageReceived( MsnSwitchboard switchboard, MsnDatacastMessage message, MsnContact contact) { // such as Nudge switchboard.sendMessage(message); } }); }
// 登录完成时发生 @Override public void loginCompleted(MsnMessenger messenger) { msg(messenger.getOwner().getDisplayName() + "登录成功!"); messenger.getOwner().setDisplayName("酷印通机器人"); }
public void sendIM(IM theIM) { myCon.sendText(Email.parseStr(theIM.to), theIM.message); }
public void disconnect() { myCon.logout(); isConn = false; }
public void addBuddy(Buddy theBuddy) { myCon.addFriend(Email.parseStr(theBuddy.getScreename()), theBuddy.getAlias()); }