`
mengdejun
  • 浏览: 408106 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

java实现msn机器人 jml

    博客分类:
  • Jav
阅读更多
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");
		else
			this.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/

分享到:
评论

相关推荐

    JML 简单的MSN 机器人

    2. **JML(Java Messenger Library)**:JML是基于Java语言实现的MSN Messenger客户端库,它提供了对MSN Messenger服务的接口,使得开发者能够用Java编程语言来创建自定义的MSNMessenger客户端,例如机器人。...

    java msn 机器人 带界面

    JavaMSN机器人是一个基于Java语言开发的应用程序,它利用JML(Java Messenger Library)库来实现与MSN即时通讯服务的交互。这个系统具有图形用户界面,能够实现自动化聊天功能,为用户提供了一个便捷的方式来模拟...

    Java MSN 实现msn发信息

    java msn 实现接口jar包 Java MSN 实现msn发信息

    msn.rar_jml ms_msn_msn robot ja_robot java_邀请

    java实现的msn机器人,可以自动应答,主动邀请,主动接受邀请。函数库实现msn所有功能

    用JAVA实现MSN Messenger的功能.doc

    本文将探讨如何使用Java语言实现MSN Messenger的功能,主要涉及的知识点包括Java编程、即时通讯协议(IM)、JML库以及相关的第三方库。 **JML库介绍** JML(Java Messenger Library)是专为Java开发者设计的一个库...

    用JML实现的一个MSN客户端一

    JML的源码通常包括多个Java类和接口,这些类和接口实现了MSN协议的不同部分,如登录、发送消息、接收消息、添加联系人等功能。开发者可以通过阅读和理解这些源码,学习如何使用Java来处理网络通信和解析XML数据(MSN...

    Jml插件获取MSN好友列表

    总结来说,Jml插件是一个帮助开发者获取和管理MSN好友列表的工具,它基于Java语言,利用MSN的通讯协议进行工作。通过学习和使用Jml,开发者不仅可以掌握网络编程和协议解析的基本技能,还能了解到如何通过开源项目...

    Java Modeling Language (JML)-开源

    在提供的压缩包中,`openjml.jar`是OpenJML工具的实现,它是一个JML的编译器插件,可以与标准Java编译器配合使用,进行契约检查。 5. **文档生成**:JML的规格可以用作自动生成文档的源,这对于理解和维护代码非常...

    jml-src-1.0b4

    "jml-src-1.0b4" 是一个开源项目,其主要目的是提供Java语言对MSN Messenger服务的接口和操作支持。这个项目的源代码库包含了多个目录,每个目录都有特定的功能和用途,使得开发者能够更好地理解和使用这个库来构建...

    使用java导入某个msn帐号的好友列表并发送消息

    - **JML (Java Msn Manager Library)**:这是一个专注于MSN通讯管理的Java库。相较于Openfire,JML对MSN的支持更为优化,且使用起来更为简便。 本篇文档将详细介绍如何使用JML来实现导入指定MSN账号的好友列表,并...

    jml-0.9.3.zip

    首先,"jml-0.9.3.zip"这个压缩包中包含的JML全称为Java Message Library,它是一个旨在简化JMS(Java Message Service)消息路由和转换的库。JMS是Java平台上的一个标准接口,用于在分布式环境中进行异步通信。通过...

    java实现朴素贝叶斯算法

    在Java中实现朴素贝叶斯算法,可以自定义类来存储和操作这些概率,或者使用现有的库,如Weka、Apache Commons Math或Java Machine Learning Library (jML)。这些库提供了现成的接口和类,使得开发者可以轻松地构建和...

    面向对象JML系列第三次代码作业指导书1

    JML(Java Modeling Language)是一种形式化语言,用于描述软件系统的规格,确保代码实现与需求一致。在这个作业中,学生需要理解JML规格,并以此为基础,使用Java实现具体的类。 首先,作业要求学生不使用官方包中...

    java web msn-开源

    Roy Tsang开发的基于Java Msn Lib(JML)的Web msn客户端。 您可以通过gmail dot com的roy dot cstsang与我联系。

    面向对象JML系列第一次指导书1

    本篇指南将探讨如何在Java中应用JML来实现Path和PathContainer类。 首先,我们来看JML如何定义Path类。Path代表一个节点序列,通常用于表示图形或数据结构中的路径。在示例中,`MyPath`实现了`Path`接口,它有一个...

    java计算器实现

    java实现计算器 import java.awt.*; import javax.swing.*; public class jisuanqi extends JFrame { JMenuBar jmb; //菜单栏组件 JMenu jm1,jm2,jm3; //菜单组件 JMenuItem jml1, jml2,jml3,jml4,jml5,jml6; //...

Global site tag (gtag.js) - Google Analytics