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

📄 qqclient.java

📁 java写的qq代码实现qq的部分功能
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*
* 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;

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.channels.UnresolvedAddressException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.tsinghua.lumaqq.qq.beans.ContactInfo;
import edu.tsinghua.lumaqq.qq.beans.FriendRemark;
import edu.tsinghua.lumaqq.qq.beans.QQUser;
import edu.tsinghua.lumaqq.qq.events.IQQListener;
import edu.tsinghua.lumaqq.qq.events.PacketEvent;
import edu.tsinghua.lumaqq.qq.events.QQEvent;
import edu.tsinghua.lumaqq.qq.net.IPort;
import edu.tsinghua.lumaqq.qq.net.ListenerThread;
import edu.tsinghua.lumaqq.qq.net.PortGate;
import edu.tsinghua.lumaqq.qq.net.Porter;
import edu.tsinghua.lumaqq.qq.net.Resender;
import edu.tsinghua.lumaqq.qq.net.TCPHttpPort;
import edu.tsinghua.lumaqq.qq.net.TCPPort;
import edu.tsinghua.lumaqq.qq.net.TCPSocks5Port;
import edu.tsinghua.lumaqq.qq.net.UDPPort;
import edu.tsinghua.lumaqq.qq.net.UDPSocks5Port;
import edu.tsinghua.lumaqq.qq.packets.BasicInPacket;
import edu.tsinghua.lumaqq.qq.packets.InPacket;
import edu.tsinghua.lumaqq.qq.packets.OutPacket;
import edu.tsinghua.lumaqq.qq.packets.in.ReceiveIMPacket;
import edu.tsinghua.lumaqq.qq.packets.out.AddFriendAuthPacket;
import edu.tsinghua.lumaqq.qq.packets.out.AddFriendPacket;
import edu.tsinghua.lumaqq.qq.packets.out.AdvancedSearchUserPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ChangeStatusPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ClusterActivatePacket;
import edu.tsinghua.lumaqq.qq.packets.out.ClusterAuthPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ClusterCreatePacket;
import edu.tsinghua.lumaqq.qq.packets.out.ClusterCreateTempClusterPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ClusterExitPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ClusterExitTempClusterPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ClusterGetInfoPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ClusterGetMemberInfoPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ClusterGetOnlineMemberPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ClusterGetTempClusterInfoPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ClusterJoinPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ClusterModifyInfoPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ClusterModifyMemberPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ClusterModifyTempClusterMemberPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ClusterSearchPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ClusterSendIMExPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ClusterSendTempClusterIMPacket;
import edu.tsinghua.lumaqq.qq.packets.out.DeleteFriendPacket;
import edu.tsinghua.lumaqq.qq.packets.out.DownloadGroupFriendPacket;
import edu.tsinghua.lumaqq.qq.packets.out.FriendDataOpPacket;
import edu.tsinghua.lumaqq.qq.packets.out.GetFriendListPacket;
import edu.tsinghua.lumaqq.qq.packets.out.GetFriendOnlinePacket;
import edu.tsinghua.lumaqq.qq.packets.out.GetUserInfoPacket;
import edu.tsinghua.lumaqq.qq.packets.out.GroupDataOpPacket;
import edu.tsinghua.lumaqq.qq.packets.out.KeepAlivePacket;
import edu.tsinghua.lumaqq.qq.packets.out.LoginPacket;
import edu.tsinghua.lumaqq.qq.packets.out.LogoutPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ModifyInfoPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ReceiveIMReplyPacket;
import edu.tsinghua.lumaqq.qq.packets.out.RemoveSelfPacket;
import edu.tsinghua.lumaqq.qq.packets.out.RequestKeyPacket;
import edu.tsinghua.lumaqq.qq.packets.out.RequestLoginTokenPacket;
import edu.tsinghua.lumaqq.qq.packets.out.SearchUserPacket;
import edu.tsinghua.lumaqq.qq.packets.out.SendIMPacket;
import edu.tsinghua.lumaqq.qq.packets.out.SendSMSPacket;
import edu.tsinghua.lumaqq.qq.packets.out.UploadGroupFriendPacket;
import edu.tsinghua.lumaqq.qq.packets.out._05.RequestAgentPacket;
import edu.tsinghua.lumaqq.qq.packets.out._05.RequestBeginPacket;
import edu.tsinghua.lumaqq.qq.packets.out._05.RequestFacePacket;
import edu.tsinghua.lumaqq.qq.packets.out._05.TransferPacket;
import edu.tsinghua.lumaqq.qq.robot.IRobot;
import edu.tsinghua.lumaqq.qq.robot.xml.Robot;
import edu.tsinghua.lumaqq.qq.robot.xml.Robots;
import edu.tsinghua.lumaqq.qq.robot.xml.RobotsUnmarshaller;

/**
 * QQ的客户端类,这个类为用户提供一个方便的接口,比如发送消息之类的,只需要调用一个
 * 方法便可以完成了。
 * 
 * LumaQQ网络层通过NIO实现,每个Client分配一个Selector,每个Selector可以监听多个
 * Channel,channel包装在IPort的实现类中,对于一个Client,有一个Main Port,这个
 * Main Port定义为连接QQ登陆服务器的那个Port。系统登陆之初之有一个Port即Main Port,
 * 根据需要可以开启其他Port
 * 
 * @author 马若劼
 */
public class QQClient implements IQQListener {
    /**
     * <pre>
     * 执行清理任务的Runnable类
     * </pre>
     * 
     * @author 马若劼
     */
    private class CleanRunnable implements Runnable {
        private KeepAliveThread thread;
        private PortGate gate;
        private Porter porter;
        
        public CleanRunnable(KeepAliveThread thread, PortGate gate, Porter porter) {
            this.thread = thread;
            this.gate = gate;
            this.porter = porter;
        }

        public void run() {
        	// 终止Keep Alive线程
            if(thread != null) 
                thread.setStop(true); 
            // 关闭所有port 
            if(gate != null)
                gate.disposeAll();
            // 关闭porter
            if(porter != null)             
	            porter.shutdown();
        }
    }
    
    // Log对象
    private static Log log = LogFactory.getLog(QQClient.class);
    // Keep Alive 线程
    private KeepAliveThread kaThread;
    // 重发线程
    private Resender resender;
    // 登陆的服务器IP
    private String loginServer;
    // TCP方式登录时的服务器端口,UDP方式时无用处
    private int tcpLoginPort;
    // 包处理器路由器
    private ProcessorRouter router;
    // QQ监听器
    private List qqListeners, qqListenersBackup;
    // QQ用户
    private QQUser user;
    // 当前是否正在登陆
    private boolean logging;
    // 当前是否在重定向登录
    private boolean loginRedirect;
    // Port管理类
    private PortGate gate;
    // 代理类型
    private int proxyType;
    // 代理服务器地址
    private InetSocketAddress proxyAddress;
    // 代理服务器验证用户名,null表示不需要验证
    private String proxyUsername;
    // 代理服务器验证密码
    private String proxyPassword;
    // 是否打开了机器人功能
    private boolean robotMode;
    // 当前选择的机器人序号
    private int robotIndex;
    // 机器人信息列表
    private List robotInfos;
    // 机器人实例数组
    private IRobot[] robots;
    // 机器人配置文件路径
    private String robotConfig;
    
    // 是否监听器有改变,这是为了处理同步问题而设置的,在实际fireQQEvent时,
    // 不使用qqListeners,而是用backup,这样在事件触发期间如果要修改
    // 监听器也是可以的,不然有可能造成并发修改异常。如果有更好的办法当
    // 然,目前只想出这种办法
    private boolean listenerChanged;    

	/** 接收队列 */
	protected LinkedList receiveQueue;	
	
	/** 包事件监听线程 */
	protected ListenerThread listenerThread;
	
	/** Selector线程 */
	protected Porter porter;
	
	/** 主port名称 */
	public static final String MAIN_PORT = "main";
	
	/** 包处理器数目 */
	protected static final int PROCESSOR_COUNT = 2;

    /**
     * 构造函数
     */
    public QQClient() {
        router = new ProcessorRouter(PROCESSOR_COUNT);
        qqListeners = new Vector();    
        qqListenersBackup = new Vector();
        receiveQueue = new LinkedList();
        logging = false;
        loginRedirect = false;
        robotMode = false;
        listenerChanged = false;
        robotIndex = 0;
        qqListeners.add(this);
        qqListenersBackup.add(this);
        
        router.installProcessor(new BasicFamilyProcessor(this));
        router.installProcessor(new _05FamilyProcessor(this));
    }
    
    /**
     * 得到当前机器人实例,lazy initialization
     * 
     * @return
     * 		IRobot接口
     */
    private IRobot getCurrentRobot() {
    	if(robots == null || robotIndex < 0 || robotIndex >= robots.length)
    		return null;
    	
    	if(robots[robotIndex] == null) {
    		Robot r = (Robot)robotInfos.get(robotIndex);
    		try {
				robots[robotIndex] = (IRobot)Class.forName(r.getImplementation()).newInstance();
			} catch (Exception e) {
				robots[robotIndex] = null;
				return null;
			}
    	}
    	return robots[robotIndex];
    }
    
    /**
     * 载入机器人配置信息
     */
    private void loadRobotInfos() {
    	if(robotConfig == null)
    		return;
    	
    	try {
    		// read robots config file
			Robots allRobots = RobotsUnmarshaller.unmarshal(new File(robotConfig));
			robotInfos = allRobots.getRobotList();
			robots = new IRobot[robotInfos.size()];
		} catch (Exception e) {
			robotInfos = null;
			robots = null;
		}
    }
    
    /**
     * 设置代理类型
     * @param type
     */
    public void setProxyType(String type) {
        if(type.equalsIgnoreCase("None"))
            proxyType = QQ.PROXY_NONE;
        else if(type.equalsIgnoreCase("Socks5"))
            proxyType = QQ.PROXY_SOCKS5;
        else if(type.equalsIgnoreCase("Http"))
            proxyType = QQ.PROXY_HTTP;
        else
            proxyType = QQ.PROXY_NONE;
    }
    
    /**
     * 设置代理服务器地址
     * @param proxyAddress
     */
    public void setProxy(InetSocketAddress proxyAddress) {
        this.proxyAddress = proxyAddress;
    }
    
    /**
     * 发送keep alive包
     */
    public void keepAlive() {
        if(user.isLoggedIn()) {
			KeepAlivePacket packet = new KeepAlivePacket(user);
			gate.send(MAIN_PORT, packet, false);
        }
    }
    
    /**
     * 上传好友备注信息
     * 
     * @param qqNum 
     * 		好友的QQ号
     * @param remark
     * 		备注类
     */
    public void uploadFriendRemark(int qqNum, FriendRemark remark) {
    	if(user.isLoggedIn()) {
    		FriendDataOpPacket packet = new FriendDataOpPacket(user);
    		packet.setRemark(remark);
    		packet.setQQ(qqNum);
    		gate.send(MAIN_PORT, packet, true);	
    	}
    }
    
    /**
     * 下载好友备注信息
     * 
     * @param qqNum 
     * 		好友的QQ号
     */
    public void downloadFriendRemark(int qqNum) {
    	if(user.isLoggedIn()) {
    		FriendDataOpPacket packet = new FriendDataOpPacket(user);
    		packet.setSubCommand(QQ.QQ_DOWNLOAD_FRIEND_REMARK);
    		packet.setQQ(qqNum);
    		gate.send(MAIN_PORT, packet, false);	
    	}
    }
    
    /**
     * 批量下载好友备注
     * 
     * @param page
     * 		页号
     */
    public void batchDownloadFriendRemark(int page) {
    	if(user.isLoggedIn()) {
    		FriendDataOpPacket packet = new FriendDataOpPacket(user);
    		packet.setSubCommand(QQ.QQ_BATCH_DOWNLOAD_FRIEND_REMARK);
    		packet.setPage(page);
    		gate.send(MAIN_PORT, packet, false);	
    	}
    }
    
    /**
     * @param page
     * @param online
     * @param hasCam
     * @param provinceIndex
     * @param cityIndex
     * @param ageIndex
     * @param genderIndex
     * @return
     */
    public char searchUserAdvanced(
            int page,
            boolean online,
            boolean hasCam,
            int provinceIndex,
            int cityIndex,
            int ageIndex,
            int genderIndex) {
        if(user.isLoggedIn()) {
            AdvancedSearchUserPacket packet = new AdvancedSearchUserPacket(user);
            packet.setPage((char)page);
            packet.setSearchOnline(online);
            packet.setHasCam(hasCam);
            packet.setProvinceIndex((char)provinceIndex);
            packet.setCityIndex((char)cityIndex);
            packet.setAgeIndex((byte)ageIndex);
            packet.setGenderIndex((byte)genderIndex);
    		gate.send(MAIN_PORT, packet, false);	
            return packet.getSequence();
        } else
            return 0;
    }
    
    /**
     * 搜索所有的在线用户
     * @param page 页号,从0开始
     * @return
     * 		包序号
     */
    public char searchUser(int page) {
    	if(user.isLoggedIn()) {
    		SearchUserPacket packet = new SearchUserPacket(user);
    		packet.setPage(page);
    		gate.send(MAIN_PORT, packet, false);	
	        return packet.getSequence();
    	} else
    	    return 0;
    }
    
    /**
     * 自定义搜索用户
     * @param page 页号
     * @param qqNum 要搜索的QQ号字符串形式
     * @param nick 要搜索的昵称
     * @param email 要搜索的email
     * @param matchEntire 字符串是否完全匹配
     * @return
     * 		包序号
     */
    public char searchUser(int page, String qqStr, String nick, String email) {
    	if(user.isLoggedIn()) {
    		SearchUserPacket packet = new SearchUserPacket(user);

⌨️ 快捷键说明

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