`
waterdh
  • 浏览: 96634 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

MSN机器人-头像显示

阅读更多
这里只描述下如何设置机器人的头像,并且传输给用户。
1.使用CHG指令告诉MSN服务器你更换了头像
 
  例如: CHG 15 NLN 1610612736 [ MSNObjectStr ]
  CHG指令格式不介绍了,[msnobjectStr]字符串的生成,这里给出代码,最终需要urlencode下:
 public class MSNObject {

	private String creator;
	private String type;
	private String sha1d;
	private String sha1c;
	private String size;
	private String location;
	private String friendly;

	public static MSNObject parse(String msnObjStr) {
		if (msnObjStr == null)
			return null;
		MSNObject obj = new MSNObject();
		String regex = "\\s+([^\\=]+)\\=\"([^\"]+)\"";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(msnObjStr);
		while (m.find()) {
			try {
				obj.getClass().getMethod("set" + toLowerUp1Case(m.group(1)), new Class[] { String.class }).invoke(obj, m.group(2));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return obj;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}


	public String getSha1d() {
		return sha1d;
	}

	public void setSha1d(String sha1d) {
		this.sha1d = sha1d;
	}

	public String getFriendly() {
		return friendly;
	}

	public void setFriendly(String friendly) {
		this.friendly = friendly;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public static String toLowerUp1Case(String str){
		StringBuffer sb = new StringBuffer(str);
		sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
		for (int i = 1; i < sb.length(); i++) {
			Character temp  = sb.charAt(i);
			sb.setCharAt(i, Character.toLowerCase(temp));
		}
		return sb.toString();
	}
	
	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append("<msnobj Creator=\"");
		sb.append(creator);	
		sb.append("\" Size=\"");
		sb.append(size);
		sb.append("\" Type=\"");
		sb.append(type);		
		sb.append("\" Location=\"");
		sb.append(location);
		sb.append("\" Friendly=\"");
		sb.append(friendly);
		sb.append("\" SHA1D=\"");
		sb.append(sha1d);
		StringBuilder sha1c = new StringBuilder();
		sha1c.append("Creator");
		sha1c.append(creator);
		sha1c.append("Size");
		sha1c.append(size);
		sha1c.append("Type");
		sha1c.append(type);
		sha1c.append("Location");
		sha1c.append(location);
		sha1c.append("Friendly");
		sha1c.append(friendly);
		sha1c.append("SHA1D");
		sha1c.append(sha1d);
		try{
		sb.append("\" SHA1C=\""+StringUtil.sha1(sha1c.toString().getBytes("utf-8")));
		}catch(Exception e){}
		sb.append("\"/>");
	    return sb.toString().replaceAll("\\n","");
	}
			
	public String getSha1c() {
		return sha1c;
	}

	public void setSha1c(String sha1c) {
		this.sha1c = sha1c;
	}
 

下面是SHA-1摘要的base64加密字符串方法:
 public static String sha1(byte[] data) {		
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-1");
		    md.update(data);			
			byte[] b = md.digest();
			return new BASE64().encode(b);
		} catch (Exception e) {			
			e.printStackTrace();
			return null;
		}		
	} 


2.当对方和你的机器人通讯的时候,对方msn发出一个p2p请求。
3.应答请求invite acknowledge,并且回复200 OK p2p消息
4.对方回复200 OK acknowledge
5.你告诉对方Data prepare
6.对方响应Data prepare acknowledge
7.发送p2p data数据
8.对方回复p2p data acknowledge
9.对方回复bye p2p 消息
10.你响应bye acknowlege

这里面需要理解binaryheader中的各字段的含义:
每次p2p数据的解析和生成都至关重要,一个字段错误就不能进入下个环节。



2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics