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

📄 clustersendimpacket.java

📁 很多人都在开发的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.qq.packets.out;

import java.nio.ByteBuffer;

import edu.tsinghua.lumaqq.qq.PacketParseException;
import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.Utils;

/**
 * <pre>
 * 发送群消息的包,格式为:
 * 1. 头部
 * 2. 命令类型,1字节,发送群消息是0x0A
 * 3. 群内部ID,4字节
 * 4. 后面的数据的总长度,2字节
 * 5. 以空格和0x0结束的消息
 * 6. 消息的尾部,包含一些消息的参数,比如字体颜色啦,等等等等,顺序是
 *     1. 字体修饰属性,bold,italic之类的,1字节,具体的设置是
 *         i.   bit0-bit4用来表示字体大小,所以最大是32
 *         ii.  bit5表示是否bold
 *         iii. bit6表示是否italic
 *         iv.  bit7表示是否underline
 *     2. 颜色Red,1字节
 *     3. 颜色Green,1字节
 *     4. 颜色Blue,1字节
 *     5. 1个未知字节,置0先
 *     6. 消息编码,2字节,0x8602为GB,0x0000为EN,其他未知,好像可以自定义,因为服务器好像不干涉
 *     7. 可变长度的一段信息,字体名后面跟一个回车符,比如0xcb, 0xce, 0xcc, 0xe5, 0x0d,表示宋体
 * 7. 尾部 
 * </pre>
 * 
 * @author 马若劼
 */
public class ClusterSendIMPacket extends ClusterCommandPacket {
	// 下面为发送消息需要设置的变量,但是目前没有什么作用,设置了也白设
	//     等到ChatBox的功能有大幅度提高时可用
    private byte red, green, blue;
    private String fontName;
    private boolean bold, italic, underline;
    private byte fontSize;
    private byte fontFlag; // 用来表示bold, italic, underline, fontSize的组合结果
    private char encoding;
    // 消息内容
    private String message;
    
    /** 字体属性 */
    private static final byte NONE = 0x00;
    private static final byte BOLD = 0x20;
    private static final byte ITALIC = 0x40;
    private static final byte UNDERLINE = (byte)0x80;
    
	/**
	 * 构造函数
	 */
	public ClusterSendIMPacket() {
		super();		
		subCommand = QQ.QQ_CLUSTER_CMD_SEND_IM;
        encoding = QQ.QQ_IM_ENCODING_GB;
        fontName = "宋体";
        red = green = blue = 0;
        bold = italic = underline = false;
        fontSize = 0x9;
        fontFlag = 0x9;
        message = "";
	}

    /**
     * @param buf
     * @param length
     * @throws PacketParseException
     */
    public ClusterSendIMPacket(ByteBuffer buf, int length)
            throws PacketParseException {
        super(buf, length);
    }
    
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.OutPacket#putBody(java.nio.ByteBuffer)
     */
    protected void putBody(ByteBuffer buf) {
		// 命令类型
		buf.put(subCommand);
		// 群内部ID
		buf.putInt(clusterId);
	    // 后面数据的长度,这个长度需要根据消息长度和字体名称长度计算才能知道,
		//     所以先来产生消息和字体名称字节数组,先占个位置		
		int pos = buf.position();
		buf.putChar((char)0);
	    // 以0结束的消息,首先我们要根据用户设置的message,解析出一个网络可发送的格式
	    //    这一步比较麻烦,暂时想不到好的办法
		byte[] msgBytes = null;
	    int j, i = 0;
	    while((j = message.indexOf((char)QQ.QQ_DEFAULT_FACE_TAG, i)) != -1) {
	    	String sub = message.substring(i, j);
	    	if(!sub.equals("")) {
	    	    msgBytes = Utils.getBytes(sub, QQ.QQ_CHARSET_DEFAULT);
			    buf.put(msgBytes);
	    	}
		    buf.put(QQ.QQ_DEFAULT_FACE_TAG);
		    buf.put((byte)(message.charAt(j + 1) & 0xFF));
		    i = j + 2;
	    }
	    if(i < message.length()) {
	    	String sub = message.substring(i);
	    	msgBytes = Utils.getBytes(sub, QQ.QQ_CHARSET_DEFAULT);		
		    buf.put(msgBytes);	    	
	    }
	    // 得到字体名称字节数组
	    byte[] fontBytes = fontName.getBytes();
	    // 写入长度
	    int cur = buf.position();
	    buf.position(pos);
	    buf.putChar((char)(cur - pos - 2 + fontBytes.length + 10));
	    buf.position(cur);
	    // 写入消息正文字节数组
	    buf.put((byte)0x20); // 空格,不要也没事,但是能尽量和QQ一致就一致
	    buf.put((byte)0);    // 0x0
	    // 消息尾部,字体修饰属性
	    buf.put(fontFlag);
	    // 字体颜色红绿篮
	    buf.put(red);
	    buf.put(green);
	    buf.put(blue);
	    // 一个未知字节
	    buf.put((byte)0);
	    // 消息编码
	    buf.putChar(encoding);
	    // 字体
	    buf.put(fontBytes);
	    // 最后一个回车符
	    buf.put((byte)0x0D);  
    }
    
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.OutPacket#parseBody(java.nio.ByteBuffer)
     */
    protected void parseBody(ByteBuffer buf) throws PacketParseException {
        subCommand = buf.get();
        clusterId = buf.getInt();
        buf.getChar();
        message = Utils.getString(buf, 0x0);
        fontFlag = buf.get();
        red = buf.get();
        green = buf.get();
        blue = buf.get();
        buf.get();
        encoding = buf.getChar();
        fontName = Utils.getString(buf, 0x0D);
        
        bold = (fontFlag & BOLD) != 0;
        italic = (fontFlag & ITALIC) != 0;
        underline = (fontFlag & UNDERLINE) != 0;
        fontSize = (byte)(fontFlag & 0x1F);
    }

    /**
     * @return Returns the blue.
     */
    public byte getBlue() {
        return blue;
    }
    
    /**
     * @param blue The blue to set.
     */
    public void setBlue(byte blue) {
        this.blue = blue;
    }
    
    /**
     * @return Returns the bold.
     */
    public boolean isBold() {
        return bold;
    }
    
    /**
     * @param bold The bold to set.
     */
    public void setBold(boolean bold) {
        this.bold = bold;
        fontFlag &= 0xDF;
        fontFlag |= bold ? BOLD : NONE;
    }
    
    /**
     * @return Returns the encoding.
     */
    public char getEncoding() {
        return encoding;
    }
    
    /**
     * @param encoding The encoding to set.
     */
    public void setEncoding(char encoding) {
        this.encoding = encoding;
    }
    
    /**
     * @return Returns the fontName.
     */
    public String getFontName() {
        return fontName;
    }
    
    /**
     * @param fontName The fontName to set.
     */
    public void setFontName(String fontName) {
        this.fontName = fontName;
    }
    
    /**
     * @return Returns the green.
     */
    public byte getGreen() {
        return green;
    }
    
    /**
     * @param green The green to set.
     */
    public void setGreen(byte green) {
        this.green = green;
    }
    
    /**
     * @return Returns the italic.
     */
    public boolean isItalic() {
        return italic;
    }
    
    /**
     * @param italic The italic to set.
     */
    public void setItalic(boolean italic) {
        this.italic = italic;
        fontFlag &= 0xBF;
        fontFlag |= italic ? ITALIC : NONE;
    }
    
    /**
     * @return Returns the red.
     */
    public byte getRed() {
        return red;
    }
    
    /**
     * @param red The red to set.
     */
    public void setRed(byte red) {
        this.red = red;
    }
    
    /**
     * @return Returns the underline.
     */
    public boolean isUnderline() {
        return underline;
    }
    
    /**
     * @param underline The underline to set.
     */
    public void setUnderline(boolean underline) {
        this.underline = underline;
        fontFlag &= 0x7F;
        fontFlag |= underline ? UNDERLINE : NONE;
    }
    
    /**
     * @return Returns the message.
     */
    public String getMessage() {
        return message;
    }
    
    /**
     * @param message The message to set.
     */
    public void setMessage(String message) {
        this.message = message;
    }
	
    /**
     * @return Returns the fontSize.
     */
    public byte getFontSize() {
        return fontSize;
    }
    
    /**
     * @param fontSize The fontSize to set.
     */
    public void setFontSize(byte fontSize) {
        this.fontSize = fontSize;
        // 清空fontSize高3位,保证他不大于32
        fontSize &= 0x1F;
        // 清空fontFlag低5位
        fontFlag &= 0xE0;
        // 组合得到最终的font字节
        fontFlag |= fontSize;
    }
}

⌨️ 快捷键说明

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