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

📄 jicq.java

📁 用java做的icq,不错的啊!大家看看啊!
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
			if(index>=canShow||index<0)return;
			Graphics g=f.getGraphics();
			Toolkit t=f.getToolkit();
			int imgh=0,imgw=0;
			imgw=imgh=bottom-top-4;

			int first=1,second=0;
			if(isOnline)second=1;else second=0;
			first=Integer.parseInt(picNo);

			headImg=jICQ.head[first][second];
			newHeadImg=jICQ.alphaHead[first][second];

			g.drawString(getNick(),left+imgw+10,top+(bottom-top)/2+4);
			g.drawImage(headImg,left+5,top+2,imgw,imgh,f);
			if(!isAlive())start();
		}catch(Exception e){System.out.println("   IcqID.draw() error:"+e);}
	}
	Image getImg(Toolkit t)
	{
		int first=1,second=0;
		if(isOnline)second=1;else second=0;
		first=Integer.parseInt(picNo);
		return jICQ.head[first][second];
	}
	void mouseEnter(Frame f)
	{
		//System.out.println("  mosueEnter()");
		if(!mouseEntered){
			int imgw,imgh;
			imgw=imgh=bottom-top-4;
			f.getGraphics().drawImage(newHeadImg,left+5,top+2,imgw,imgh,f);
			mouseEntered=true;
		}
	}
	void mouseExit(Frame f)
	{
		if(mouseEntered){
			int imgw,imgh;
			imgw=imgh=bottom-top-4;
			f.getGraphics().drawImage(headImg,left+5,top+2,imgw,imgh,f);
			mouseEntered=false;
		}
	}

	String getNick()
	{
		if(!nick.equals(""))checked=true;
		String n=null;
		if(nick.equals(""))n=icqNo;else n=nick;
		if(isStranger)n+="(陌生人)";
		//System.out.println("  icqNo,isStranger="+icqNo+","+isStranger);
		return n;
	}
	public void sendOut(String s)
	{
		try{
			String sMsgs=null;
			sMsgs="MSG from:"+MainFrame.myIcqID.icqNo+" to:"+icqNo+" msg:"+s;
			sMsgs=replace(sMsgs,"\n","\r");
			String randomId=" id:"+new Long(new Date().getTime()).toString();
			if(sMsgs.length()>CONTMAX-randomId.length())sMsgs=sMsgs.substring(0,CONTMAX-randomId.length());
			sMsgs+=randomId;		//add a random number to identfy the same message received tow times.
			addOutMsg(sMsgs);
		}catch(Exception e){System.out.println("  SMsg.sendOut() error:"+e);}
	}

	void addOutMsg(String s)
	{
		if(!isOnline||ip==null||rport==null){
			Sender sender=new Sender("FORWARD from:"+MainFrame.myIcqID.icqNo+" to:"+icqNo+" "+s);
			sender.start();
			Result r=sender.getReturn();
			if (r.getCode()==200)System.out.println("  通过服务器转发成功。");
		}else {udpSend(s,CONTMAX);outMsg.addElement(s);}
		if(!isAlive())start();
		s=replace(s,"\r","\n");
		addrec(s,"我");
	}
	void addInMsg(String ins)
	{
		//System.out.println("  ins:"+ins);
		try{
			String s=null;
			long thisTime=new Date().getTime();
			for(int i=0;i<inMsg.size();i++){
				s=(String)inMsg.elementAt(i);
				if(getMsgTime(s)==getMsgTime(ins))return;
				if(thisTime>getMsgTime(s)+60*1000){      //if timeout then forward
					inMsg.removeElementAt(i);
				}
			}
			ins=replace(ins,"\r","\n");
			addrec(ins,getNick());
			if(smsg!=null)
				if (smsg.isShowing()){newMsg=false;smsg.toFront();}
				else newMsg=true;
			else newMsg=true;
			jICQ.audioPlay(1);
			inMsg.addElement(ins);

			String from=null;
			from=ins.substring(ins.indexOf("from:")+5);
			from=from.substring(0,from.indexOf(' '));
			String id=ins.substring(ins.lastIndexOf("id:"));
			udpSend("UDPOK from:"+MainFrame.myIcqID.icqNo+" to:"+icqNo+" "+id,CONTMAX);
			if(MainFrame.sysinfo.autoRe)sendOut(MainFrame.sysinfo.autoReText); //this is autoReturn 
			if(smsg!=null)if(smsg.isShowing())smsg.refrush();        //if there are two mssage into and not opened,then refrush smsg.logta
		}catch(Exception e){System.out.println("  IcqID.addInMsg() error:"+e);}
	}
	void addrec(String s,String who)
	{
		try{
			String msg=null;
			if( (s.indexOf("msg:")!=-1) && (s.indexOf("id:")!=-1) )
			{
				msg=s.substring(s.indexOf("msg:")+4);
				msg=msg.substring(0,msg.lastIndexOf("id:"));
			}
			//Date d=new Date(getMsgTime(s));
			String time=new Integer(Calendar.getInstance().get(Calendar.HOUR_OF_DAY)).toString();
			time+=":"+new Integer(Calendar.getInstance().get(Calendar.MINUTE)).toString();
			String date=new Integer(Calendar.getInstance().get(Calendar.MONTH)+1).toString();
			date+="-"+new Integer(Calendar.getInstance().get(Calendar.DAY_OF_MONTH)).toString();
			chatrec="\n"+who+"("+date+" "+time+"):\n"+msg+"\n"+chatrec;
		}catch(Exception e){e.printStackTrace();}
	}
	void clearNew(){newMsg=false;}
	void setStranger(String myIcqNo)
	{
		if(!isStranger)return;
		if(!updated){
			Sender s=new Sender("STRANGER icqNo:"+myIcqNo+" stranger:"+icqNo);
			s.start();
			Result r=s.getReturn();
			if(r.getCode()==200)updated=true;
		}
	}
	String replace(String s1,String s2,String s3)
	{
		if(s1==null||s2==null||s3==null)return s1;
		try{while(-1!=s1.indexOf(s2)){s1=s1.substring(0,s1.indexOf(s2))+s3+s1.substring(s1.indexOf(s2)+s2.length());}}catch(Exception e){System.out.println("  replace() error:"+s1);}
		return s1;
	}
	public void udpSend(String sMsgs,int CONTMAX)
	{
		DatagramSocket udpsocket=null;
		DatagramPacket dp=null;

		for(int i=0;udpsocket==null&&i<3;i++)//build udpSocket
			try{udpsocket=new DatagramSocket();}
			catch(Exception e){}
		if(udpsocket==null){System.out.println("   建立UDP发送端口时出错。");return;}

		//System.out.println("  the SMsg() sMsgs,length():"+sMsgs+","+sMsgs.length());
		byte[] sMsg=new byte[CONTMAX*2];
		try{
			for(int i=0;i<sMsgs.length();i++){sMsg[i*2]=(byte)(sMsgs.charAt(i)>>8);sMsg[i*2+1]=(byte)sMsgs.charAt(i);}
			//convert the String to byte and the 8bit
			int rPort=Integer.parseInt(rport);
			dp=new DatagramPacket(sMsg,sMsg.length,InetAddress.getByName(ip),rPort);
			udpsocket.send(dp);
			//System.out.println("  dp.getLength()="+dp.getLength());

		}catch(Exception e){System.out.println("  UDP udpSend() error:"+e);}
		System.out.println("   UDP send to "+ip+":"+rport+" "+sMsgs);
	}
}

class Sender extends Thread
{
	Socket socket=null;
	int TCPserverPort=7802;
	String URLstring=null;
	IcqID myIcqID=null;
	String outs=null;
	Result result=null;
	BufferedOutputStream bos=null;

	Sender(String cmd,IcqID i)
	{
		outs=cmd;
		result=new Result();
		URLstring=jICQ.URLstring;
		myIcqID=i;
	}

	Sender(String cmd){
		outs=cmd;
		result=new Result();
		URLstring=jICQ.URLstring;
	}

	public void run(){

		DataInputStream instream=null;
		PrintStream outstream=null;
		for(int i=0;(i<3)&&(socket==null);i++)
			try{
				socket=new Socket(URLstring,TCPserverPort);
				//System.out.println("socket is:"+socket);
			}catch(Exception e){result.setResult(401,"连接到jICQ服务器"+URLstring+"时出错。");System.out.println("  Sender build socket error");return;}
		try{
			bos=new BufferedOutputStream(socket.getOutputStream());
			outstream=new PrintStream(bos);
			instream=new DataInputStream(new BufferedInputStream(socket.getInputStream()));
		}catch(Exception e){result.setResult(401,"建立网络I/O错误!");return;}

		//System.out.println("  Send out:"+outs);
		switch(request(outs))
		{
		case 0:join(outs,instream,outstream);break;
		case 1:logon(outs,instream,outstream);break;
		case 2:seek(outs,instream,outstream);break;
		case 3:registy(outs,instream,outstream);break;
		case 4:onlineMon(outs,instream,outstream);break;
		case 7:logout(outs,instream,outstream);break;
		case 8:getme(outs,instream,outstream);break;
		case 9:forward(outs,instream,outstream);break;
		case 10:getID(outs,instream,outstream);break;
		case 11:delFriend(outs,instream,outstream);break;
		case 12:modify(outs,instream,outstream);break;
		case 13:join(outs,instream,outstream);break;
		case 14:stranger(outs,instream,outstream);break;  //it like logon()
		default:
				result.setResult(501,"未知命令。");
				try{sleep(2000);}catch(Exception e){}
				System.out.println("  received unknow command.");
		}

		try{socket.close();}
		catch(Exception e){System.out.println("  close socket error:"+e);}
		//System.out.println("completed:"+socket.getInetAddress()+" Port"+socket.getPort());
	}
	int request(String s)
	{
		try{
			if(s.length()>0){
				String cmd=s.substring(0,s.indexOf(' '));
				if(cmd.equalsIgnoreCase("JOIN")){return 0;}
				if(cmd.equalsIgnoreCase("LOGON")){return 1;}
				if(cmd.equalsIgnoreCase("SEEK")){return 2;}
				if(cmd.equalsIgnoreCase("REGISTY")){return 3;}
				if(cmd.equalsIgnoreCase("ONLINEMON")){return 4;}
				if(cmd.equalsIgnoreCase("LOGOUT")){return 7;}
				if(cmd.equalsIgnoreCase("GETME")){return 8;}
				if(cmd.equalsIgnoreCase("FORWARD")){return 9;}
				if(cmd.equalsIgnoreCase("GETID")){return 10;}
				if(cmd.equalsIgnoreCase("DELFRIEND")){return 11;}
				if(cmd.equalsIgnoreCase("MODIFY")){return 12;}
				if(cmd.equalsIgnoreCase("JOINED")){return 13;}
				if(cmd.equalsIgnoreCase("STRANGER")){return 14;}
			}
		}catch(Exception e){result.setResult(303,"在识别命令时出错!");}
		return -1;
	}

	public Result getReturn()
	{
		int timeout=0;  //timeout
		while(result.getCode()==-1&&timeout<600){try{Thread.sleep(100);timeout++;}catch(Exception e){}}
		if(timeout==300)result.setResult(301,"操作超时!");
		return result;
	}
	public Result getReturn(int timeoutSecond)
	{
		int outmax=timeoutSecond*10;
		int timeout=0;
		while(result.getCode()==-1&&timeout<outmax){try{Thread.sleep(100);timeout++;}catch(Exception e){}}
		if(timeout==300)result.setResult(301,"操作超时!");
		return result;
	}
	void sendString(PrintStream ps,String s)
	{
		ps.println(s.length());
		int len=s.length()*2;
		byte buf[]=new byte[len];
		try{
			for(int i=0;i<s.length();i++){buf[i*2]=(byte)(s.charAt(i)>>8);buf[i*2+1]=(byte)s.charAt(i);}
			ps.write(buf,0,len);
			ps.flush();
			bos.flush();
		}
		catch(Exception e){result.setResult(403,"发送失败!");System.out.println("   sendString() error:"+e);}
		//System.out.println("   has sent.");
	}
	String receivString(DataInputStream in)
	{
		String inline="";
		int len=0;
		try{
			inline=in.readLine();
			for(int i=0;inline.equals("");i++){inline=in.readLine();Thread.sleep(100);if(i==300)return "";}
			len=new Integer(inline).intValue();
		}catch(Exception e){result.setResult(401,"接收失败!");System.out.println("   receivString() error:"+e);}
		char c[]=new char[len];
		byte buf[]=new byte[len*2];
		try{in.readFully(buf);}
		catch(IOException e){System.out.println("   receivString() error:"+e);}
		for(int i=0;i<len;i++)
		{c[i]=(char)(buf[i*2]<<8);c[i]=(char)((c[i]&0xff00)|(0x00ff&buf[i*2+1]));}
		String s=null;
		s=new String(c);
		return s;
	}
	String replace(String s1,String s2,String s3)
	{
		if(s1==null||s2==null||s3==null)return s1;
		try{while(-1!=s1.indexOf(s2)){s1=s1.substring(0,s1.indexOf(s2))+s3+s1.substring(s1.indexOf(s2)+s2.length());}}catch(Exception e){System.out.println("  replace() error:"+e);}
		return s1;
	}

	void seek(String outs,DataInputStream instream,PrintStream outstream)
	{
		sendString(outstream,outs);
		String inline=receivString(instream);
		if(inline.equals("OK")){
			inline=receivString(instream);
			result.setResult(200,inline);
		}
		else result.setResult(501,inline);
	}
	void getID(String outs,DataInputStream instream,PrintStream outstream)
	{
		sendString(outstream,outs);
		String icqids="";
		String inline=receivString(instream);
		if(inline.equals("OK")){
			do{
				inline=receivString(instream);
				//System.out.println("  getID() received:"+inline);
				icqids+=inline+"\n";
				try{inline=inline.substring(0,inline.indexOf(' '));}catch(Exception e){System.out.println("  Sender getID() error:"+e);}
			}while(!inline.equalsIgnoreCase("end"));
			result.setResult(200,icqids);
		}
		else {result.setResult(402,inline);}
	}
	void join(String outs,DataInputStream instream,PrintStream outstream)
	{
		sendString(outstream,outs);
		String inline=receivString(instream);
		if(inline.equals("OK"))	result.setResult(200,inline);
		else result.setResult(402,inline);
	}
	void delFriend(String outs,DataInputStream instream,PrintStream outstream)
	{
		sendString(outstream,outs);
		String inline=receivString(instream);
		if(inline.equals("OK"))	result.setResult(200,inline);
		else result.setResult(402,inline);
	}
	void stranger(String outs,DataInputStream instream,PrintStream outstream)
	{
		sendString(outstream,outs);
		String inline=receivString(instream);
		if(inline.equals("OK"))	result.setResult(200,inline);
		else result.setResult(402,inline);
	}

	void getme(String outs,DataInputStream instream,PrintStream outstream)
	{
		sendString(outstream,outs);
		String inline=receivString(instream);
		if(inline.equals("OK")){
			MainFrame.friends=receivString(instream);
			MainFrame.onlineFriends=receivString(

⌨️ 快捷键说明

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