📄 icqserver.java
字号:
connDB();
//String destIP=client.getInetAddress().toString();
//int destport=client.getPort();
//ICQserver.errmsg("client:"+client.toString());
BufferedOutputStream bos=new BufferedOutputStream(client.getOutputStream());
PrintStream outstream=new PrintStream(bos);
BufferedInputStream bis=new BufferedInputStream(client.getInputStream());
DataInputStream instream=new DataInputStream(bis);
String in="";
in=receivString(instream);
//ICQserver.errmsg(" Received:\""+in+"\".");
offline=false;
switch(request(in)){
case 0:join(in,instream,outstream);break;
case 1:logon(in,instream,outstream);break;
case 2:seek(in,instream,outstream);break;
case 3:registy(in,instream,outstream);break;
case 4:serverMon(in,instream,outstream);break;
case 7:logout(in,instream,outstream);break;
case 8:getme(in,instream,outstream);break;
case 9:forward(in,instream,outstream);break;
case 10:getID(in,instream,outstream);break;
case 11:delFriend(in,instream,outstream);break;
case 12:modify(in,instream,outstream);break;
case 13:joined(in,instream,outstream);break;
case 14:stranger(in,instream,outstream);break;
default:sendString(outstream,"FAIL 未知命令。");/*ICQserver.errmsg(" received unknow cmd:\""+in+"\",from:"+client.getInetAddress());*/
}//end switcch
closeDB();
//ICQserver.errmsg("completed:"+client.getInetAddress()+" Port"+client.getPort());
client.close();
}
catch(Exception e){ICQserver.errmsg(" Mission.run() error:"+e);}
}
int request(String s)
{
if(s==null)return -1;
if(s.indexOf(' ')==-1)s+=" ";
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;}
}
return -1;
}
void connDB()
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(sqlURL);
stmt = con.createStatement();
}
catch(ClassNotFoundException ex) {System.err.println("ClassNotFoundException : " + ex.getMessage());}
catch(SQLException ex){System.err.println("SQLException: " + ex.getMessage());}
}
void closeDB()
{
try {
stmt.close();
con.close();
}
catch(SQLException ex){System.err.println("SQLException: " + ex.getMessage());}
}
void sendString(PrintStream ps,String s)
{
try{
ps.println(s.length());
int len=s.length()*2;
byte buf[]=new byte[len];
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();
}
catch(Exception e){offline=true;ICQserver.errmsg(" sendString() error:"+e);}
//ICQserver.errmsg(" has sent.");
}
String receivString(DataInputStream in)
{
String inline="";
String s=null;
int len=0;
try{inline=in.readLine();}
catch(SocketException e){offline=true;/*ICQserver.errmsg(" receivString() error:"+e);*/}
catch(Exception e){}
try{
for(int i=0;inline.equals("");i++){inline=in.readLine();Thread.sleep(100);if(i==300)return "";}
len=new Integer(inline).intValue();
char c[]=new char[len];
byte buf[]=new byte[len*2];
in.readFully(buf);
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]));}
s=new String(c);
}
catch(Exception e){}
return s;
}
void logon(String inline,DataInputStream instream,PrintStream outstream)
{
try{
String icqNo=null,password=null;
try{
icqNo=inline.substring(inline.indexOf("icqNo:")+6);
icqNo=icqNo.substring(0,icqNo.indexOf(' '));
int ino=Integer.parseInt(icqNo);//parseInt();
password=inline.substring(inline.indexOf("password:")+9);
}catch(Exception e){sendString(outstream,"FAIL ICQ号码错误:"+e);return;}
String sql="select icqNo,password from icqNo where icqNo="+icqNo;
rs = stmt.executeQuery(sql);
//ICQserver.errmsg(" logon() icqNo,password,sql:"+icqNo+","+password+","+sql);
String friendNo=null;
if(rs.next()){
String dbicqNo = rs.getString("icqNo");
String dbpassword = rs.getString("password");
//ICQserver.errmsg(" dbicqNo="+dbicqNo+" dbpassword:"+dbpassword);
if(dbpassword.equals(password)){
sendString(outstream,"OK");
ICQserver.errmsg(" "+icqNo+" login,at IP:"+client.getInetAddress().toString());
}
else{sendString(outstream,"PWDERR icqNo:"+icqNo+" 密码错误!");}
}
else{
sendString(outstream,"NOTFOUND icqNo:"+icqNo+" 不存在的号码!");
}
}catch(Exception e){sendString(outstream,"服务器操作出错:"+e);ICQserver.errmsg(" logon() error:"+e);}
}
void getme(String inline,DataInputStream instream,PrintStream outstream)
{
String icqNo=null,ip=null,port=null,sql=null;
Mission m=null;
try
{
icqNo=inline.substring(inline.indexOf("icqNo:")+6);
icqNo=icqNo.substring(0,icqNo.indexOf(' '));
port=inline.substring(inline.indexOf("port:")+5);
String tmps=client.getInetAddress().toString();
ip=tmps.substring(tmps.indexOf('/')+1)+":"+port;
String friendNo="",forward=null,friendOnline="",deletedNo=null;
sql="select * from friend where icqNo="+icqNo;
rs = stmt.executeQuery(sql);
while(rs.next())friendNo+=","+rs.getString("friendNo");
friendNo+=",";
//getFriend,deletedNo and forword
//System.out.println(" getme() friendNo="+friendNo);
sql="select forward from icqNo where icqNo="+icqNo;
rs = stmt.executeQuery(sql);
if(rs.next()){
forward=rs.getString("forward");if(forward==null)forward="";
//getOnlineFriend
String friendNoArr=" (-1"+friendNo+"-1)"; //becase the friendNo=,8217,2345,
sql="select icqNo,ip from icqNo where statu=1 and icqNo in"+friendNoArr;
rs = stmt.executeQuery(sql);
while(rs.next()){
friendOnline+=rs.getString("icqNo")+" "+rs.getString("ip")+"\n";
}
//setPort
String time=new Integer(Calendar.getInstance().get(Calendar.HOUR_OF_DAY)).toString();
time+=":"+new Integer(Calendar.getInstance().get(Calendar.MINUTE)).toString();
String date=null;
date=new Integer( Calendar.getInstance().get(Calendar.YEAR)).toString();
date+="-"+new Integer(Calendar.getInstance().get(Calendar.MONTH)+1).toString();
date+="-"+new Integer( Calendar.getInstance().get(Calendar.DAY_OF_MONTH)).toString();
sql="update icqNo set statu=1,ip='"+ip+"',forward='',lastlogon='"+date+" "+time+"' where icqNo="+icqNo;
stmt.executeUpdate(sql);
//ICQserver.errmsg(" getme() ok.");
}else{
sendString(outstream,"FAIL icqNo:"+icqNo+" 不存在的号码!");
return;
}
//tell friend I'm logon.
String friendNo_deletedNo=" (-1"+friendNo+"-1"+deletedNo+"-1)";
sql="select icqNo from friend where friendNo="+icqNo;
rs = stmt.executeQuery(sql);
String sMsgs=null,fIcqNo=null;
sMsgs="LOGON icqNo:"+icqNo+" ip:"+ip;
while(rs.next()){
fIcqNo=rs.getString("icqNo");
Mission fm=ICQserver.get(fIcqNo);
if(fm!=null)fm.addCmd(sMsgs);
}
sendString(outstream,"OK");
sendString(outstream,friendNo);
sendString(outstream,friendOnline);
//send out the forward message
m=ICQserver.get(icqNo);if(m!=null)m.addCmd("SYSMSG \n"+forward);
}catch(Exception e){sendString(outstream,"FAIL "+e);ICQserver.errmsg(" getme() error:"+e);}
}
void logout(String inline,DataInputStream instream,PrintStream outstream)
{
try
{
String icqNo=inline.substring(inline.indexOf("icqNo:")+6);
//ICQserver.errmsg(" No:"+icqNo+" logout.");
//ICQserver.errmsg(" ip:prot="+ip);
Mission m=ICQserver.get(icqNo);
if(m!=null){m.offline=true;}
sendString(outstream,"OK");
}catch(Exception e){sendString(outstream,"FAIL");ICQserver.errmsg(" logout() error:"+e);}
}
void getID(String inline,DataInputStream instream,PrintStream outstream)
{
try
{
String icqNo=inline.substring(inline.indexOf("icqNo:")+6);
String sql="select * from icqNo where icqNo="+icqNo;
rs=stmt.executeQuery(sql);
if (rs.next())
{
sendString(outstream,"OK");
sendString(outstream,"nick "+rs.getString("nick"));
//sendString(outstream,"pass "+rs.getString("password"));
sendString(outstream,"sex "+rs.getString("sex"));
String info=rs.getString("info");
info=replace(info,"\n","\r");
sendString(outstream,"info "+info);
sendString(outstream,"tel "+rs.getString("tel"));
sendString(outstream,"addr "+rs.getString("addr"));
sendString(outstream,"http "+rs.getString("http"));
sendString(outstream,"mobile "+rs.getString("mobile"));
sendString(outstream,"email "+rs.getString("email"));
sendString(outstream,"picNo "+rs.getString("picNo"));
sendString(outstream,"age "+rs.getString("age"));
sendString(outstream,"check "+rs.getString("check"));
sendString(outstream,"ip "+rs.getString("ip"));
sendString(outstream,"statu "+rs.getString("statu"));
sendString(outstream,"integral "+rs.getString("integral"));
sendString(outstream,"end ID");
}else sendString(outstream,"FAIL 没找到这个人。");
//ICQserver.errmsg(" getID() end.");
}catch(Exception e){sendString(outstream,"FAIL 操作时出错。");ICQserver.errmsg(" getID() error:"+e);}
}
void registy(String inline,DataInputStream instream,PrintStream outstream)
{
String sql=null;
String icqNo=genNo();
String nick=null,sex=null,picNo="1",info=null,tel=null,addr=null,age="20",check="否";
String http=null,mobile=null,email=null,pass=null;
if(icqNo!=null)
try
{
do{
inline=receivString(instream);
//ICQserver.errmsg(" registy() received="+inline);
if(inline.substring(0,inline.indexOf(' ')).equalsIgnoreCase("nick")){nick=inline.substring(inline.indexOf(' ')+1);nick=replace(nick,"'","''");}
if(inline.substring(0,inline.indexOf(' ')).equalsIgnoreCase("sex")){sex=inline.substring(inline.indexOf(' ')+1);sex=replace(sex,"'","''");}
if(inline.substring(0,inline.indexOf(' ')).equalsIgnoreCase("picNo")){picNo=inline.substring(inline.indexOf(' ')+1);picNo=replace(picNo,"'","''");}
if(inline.substring(0,inline.indexOf(' ')).equalsIgnoreCase("info")){info=inline.substring(inline.indexOf(' ')+1);info=replace(info,"\r","\n");info=replace(info,"'","''");}
if(inline.substring(0,inline.indexOf(' ')).equalsIgnoreCase("tel")){tel=inline.substring(inline.indexOf(' ')+1);tel=replace(tel,"'","''");}
if(inline.substring(0,inline.indexOf(' ')).equalsIgnoreCase("addr")){addr=inline.substring(inline.indexOf(' ')+1);addr=replace(addr,"'","''");}
if(inline.substring(0,inline.indexOf(' ')).equalsIgnoreCase("http")){http=inline.substring(inline.indexOf(' ')+1);http=replace(http,"'","''");}
if(inline.substring(0,inline.indexOf(' ')).equalsIgnoreCase("mobile")){mobile=inline.substring(inline.indexOf(' ')+1);mobile=replace(mobile,"'","''");}
if(inline.substring(0,inline.indexOf(' ')).equalsIgnoreCase("email")){email=inline.substring(inline.indexOf(' ')+1);email=replace(email,"'","''");}
if(inline.substring(0,inline.indexOf(' ')).equalsIgnoreCase("pass")){pass=inline.substring(inline.indexOf(' ')+1);pass=replace(pass,"'","''");}
if(inline.substring(0,inline.indexOf(' ')).equalsIgnoreCase("check")){check=inline.substring(inline.indexOf(' ')+1);check=replace(check,"'","''");}
if(inline.substring(0,inline.indexOf(' ')).equalsIgnoreCase("age")){age=inline.substring(inline.indexOf(' ')+1);age=replace(age,"'","''");}
}while(!inline.substring(0,inline.indexOf(' ')).equalsIgnoreCase("end"));
sql="insert into icqNo (icqNo,nick,password,info,picNo,sex,email,age,tel,mobile,addr,http,check,ip,statu,forward) values("+icqNo+",'"+nick+"','"+pass+"','"+info+"',"+picNo+",'"+sex+"','"+email+"','"+age+"','"+tel+"','"+mobile+"','"+addr+"','"+http+"','"+check+"','ip',0,' ')";
stmt.executeUpdate(sql);
sendString(outstream,"OK");
sendString(outstream,icqNo);
ICQserver.errmsg(" No:"+icqNo+" registed,at IP:"+client.getInetAddress().toString());
return;
}catch(Exception e){ICQserver.errmsg(" registy() error:"+e);}
sendString(outstream,"FAIL");
}
String genNo()
{
String sql=null;
String icqNo=null;
try
{
int icqNoNum=0;
int segment=8192;
icqNoNum=Math.abs(new Random().nextInt()/segment);
do{
icqNoNum++;
if(icqNoNum<1000)continue;
//ICQserver.errmsg(" Begining to registy()");
icqNo=new Integer(icqNoNum).toString();
sql="select icqNo from icqNo where icqNo="+icqNo;
rs=stmt.executeQuery(sql);
}while(rs.next());
}catch(Exception e){ICQserver.errmsg(" getNo() error:"+e);return null;}
return icqNo;
}
void modify(String inline,DataInputStream instream,PrintStream outstream)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -