⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iminparser.java

📁 java写的qq代码实现qq的部分功能
💻 JAVA
字号:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq.ui.tool;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

import edu.tsinghua.lumaqq.qq.Util;
import edu.tsinghua.lumaqq.utils.FaceUtil;
import edu.tsinghua.lumaqq.widgets.rich.IRichContent;

/**
 * 自定义表情工具类,用来分析一条接收消息,提取出自定义表情的信息
 * 
 * @author luma
 */
public class IMInParser {
    private List offsets;
    private List lengths;
    private List names;
    
    // agents保存表情的中转服务器地址,不包含重复的表情
    private List agents;
    // sessionIds保存session id,不包含重复的表情
    private List sessionIds;
    // 保存表情的文件名,不包含重复的表情
    private List distinctNames;
    
    private String message;
    private byte[] agentKey;
    
    public IMInParser() {
        offsets = new ArrayList();
        lengths = new ArrayList();
        names = new ArrayList();
        agents = new ArrayList();
        sessionIds = new ArrayList();
        distinctNames = new ArrayList();
    }
    
    /**
     * 得到session id
     * 
     * @param index
     * 		索引,不考虑重复的
     * @return
     * 		会话id
     */
    public int getSessionId(int index) {
        if(index < 0 || index >= sessionIds.size())
            return 0;
        return ((Integer)sessionIds.get(index)).intValue();
    }
    
    public InetSocketAddress getAgentAddress(int index) {
        return (InetSocketAddress)agents.get(index);
    }
    
    public int getDistinctFaceCount() {
        return agents.size();
    }
    
    public byte[] getAgentKey() {
        return agentKey;
    }
    
    /**
     * 得到表情文件名,得到的文件名的扩展名是小写的
     * 
     * @param index
     * @return
     */
    public String getFaceFileName(int index) {
        if(index < 0 || index >= distinctNames.size())
            return null;
        return (String)distinctNames.get(index);
    }
    
    /**
     * 分析接收到的信息
     * 
     * @param msg
     */
    public void parseInMessage(String msg) {
        this.message = msg;
        lengths.clear();
        offsets.clear();
        names.clear();
        distinctNames.clear();
        sessionIds.clear();
        agents.clear();
        agentKey = null;
        
        int i = msg.indexOf(IRichContent.CUSTOM_FACE_TAG);
        while(i != -1) {
            offsets.add(new Integer(i));
            int length = Util.getInt(msg.substring(i + 2, i + 5).trim(), 5);
            char existence = msg.charAt(i + 1);
            int shortcutLen = msg.charAt(i + 6) - 'A';
            int index = msg.charAt(i + 5) - 'A';
            switch(existence) {
                case '6':
                    // session id
                    int sessionId = Util.getInt(msg.substring(i + 9, i + 17).trim(), 16, -1);
                    sessionIds.add(new Integer(sessionId));
                    // ip
                    byte[] ip = Util.convertHexStringToByteNoSpace(msg.substring(i + 17, i + 25));
                    byte b = ip[0];
                    ip[0] = ip[3];
                    ip[3] = b;
                    b = ip[1];
                    ip[1] = ip[2];
                    ip[2] = b;
                    String ipStr = Util.convertIpToString(ip);
                    // port
                    int port = Util.getInt(msg.substring(i + 25, i + 33).trim(), 16, 443);
                    // generate address
                    InetSocketAddress address = new InetSocketAddress(ipStr, port);
                    agents.add(address);
                    // get agent key
		            if(agentKey == null)
		                agentKey = msg.substring(i + 33, i + 49).getBytes();
		            // find delimiter 'A',考虑到中文字符的关系,我们不能直接使用长度值
		            int delimitPos = msg.lastIndexOf('A', i + length);
		            int newLength = delimitPos - i + 1;
		            lengths.add(new Integer(newLength));
		            shortcutLen -= length - newLength;
		            length = newLength;
                    // set others
		            String name = msg.substring(i + 49, i + length - shortcutLen - 1);
		            name = FileTool.getNameExcludeExtension(name) + '.' + FileTool.getExtension(name).toLowerCase();
                    names.add(name);
                    distinctNames.add(name);
                    break;
                case '7':
                    names.add(distinctNames.get(index));   
		            // find delimiter 'A',考虑到中文字符的关系,我们不能直接使用长度值
		            delimitPos = msg.lastIndexOf('A', i + length);
		            length = delimitPos - i + 1;
		            lengths.add(new Integer(length));
                    break;
            }
            
            // 查找下一个
            i = msg.indexOf(IRichContent.CUSTOM_FACE_TAG, i + length);
        }
    }
    
    /**
     * @return
     * 		得到表情个数
     */
    public int getFaceCount() {
        return agents.size();
    }
    
    /**
     * 把输入消息转换为能在richbox中显示的格式
     * 
     * @return
     * 		转换后的消息
     */
    public String toDisplayFormat() {
        FaceUtil util = FaceUtil.getInstance();
        StringBuffer sb = new StringBuffer();
        int size = offsets.size();
        int lastOffset = 0;
        for(int i = 0; i < size; i++) {
            int offset = ((Integer)offsets.get(i)).intValue();
            sb.append(message.subSequence(lastOffset, offset));
            lastOffset = offset + ((Integer)lengths.get(i)).intValue();
            
            String md5 = FileTool.getNameExcludeExtension((String)names.get(i));
            char id = (char)util.getFaceId(md5);
            if(id == 0)
                id = (char)util.getReceivedFaceId(md5);
            sb.append(IRichContent.CUSTOM_FACE_TAG).append(id);
        }
        if(lastOffset < message.length())
            sb.append(message.substring(lastOffset));
        return sb.toString();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -