📄 icqserver.java
字号:
String sql=null;
String icqNo=inline.substring(inline.indexOf("icqNo:")+6);
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(" modify() 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);
if(inline.substring(0,inline.indexOf(' ')).equalsIgnoreCase("picNo"))picNo=inline.substring(inline.indexOf(' ')+1);
if(inline.substring(0,inline.indexOf(' ')).equalsIgnoreCase("info")){info=inline.substring(inline.indexOf(' ')+1);info=replace(info,"'","''");info=replace(info,"\r","\n");}
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);}
if(inline.substring(0,inline.indexOf(' ')).equalsIgnoreCase("age")){age=inline.substring(inline.indexOf(' ')+1);}
}while(!inline.substring(0,inline.indexOf(' ')).equalsIgnoreCase("end"));
sql="update icqNo set nick='"+nick+"',sex='"+sex+"',picNo="+picNo+",info='"+info+"',tel='"+tel+"',addr='"+addr+"',http='"+http+"',mobile='"+mobile+"',password='"+pass+"',email='"+email+"',check='"+check+"',age="+age+" where icqNo="+icqNo;
//ICQserver.errmsg(" sql="+sql);
stmt.executeUpdate(sql);
sendString(outstream,"OK");
sendString(outstream,icqNo);
return;
}catch(Exception e){ICQserver.errmsg(" modify() error:"+e);}
sendString(outstream,"FAIL");
}
void serverMon(String inline,DataInputStream instream,PrintStream outstream)
{
try
{
String rPorts=null,ip=null;
String sMsgs=null;
String icqNo=null,id=null;
long thistime=0,pasttime=0;
int inc=3*60*1000;
thistime=pasttime=new java.util.Date().getTime();
pasttime+=inc;
icqNo=inline.substring(inline.indexOf("icqNo:")+6);
icqNo=icqNo.substring(0,icqNo.indexOf(' '));
id=inline.substring(inline.indexOf("id:")+3);
//if 'icqNo' has already online then logout
Mission m=ICQserver.get(icqNo);
if(m!=null){m.dupLogon=true;}
ICQserver.set(icqNo,this);
inline=null;
//boolean exitwhile=false;
client.setSoTimeout(200);
while(!offline)
try{
if(outCmd.size()!=0){
sendString(outstream,getCmd());
}
inline=receivString(instream);
if(dupLogon){
sendString(outstream,"SYSMSG \nDUPLOGON "+icqNo+"正被另一个人使用。");
return;
}
sleep(100);
thistime= new java.util.Date().getTime();
//add the expence
if(thistime>pasttime){
String sql="update icqNo set integral=integral+0.05 where icqNo="+icqNo;
stmt.executeUpdate(sql);
pasttime=thistime+inc;
}
}catch(Exception e){}
//ICQserver.errmsg(" preper logout.");
String sql="update icqNo set statu=0 where icqNo="+icqNo;
stmt.executeUpdate(sql);
sql="select icqNo from friend where friendNo="+icqNo;
rs = stmt.executeQuery(sql);
String fIcqNo=null;
while(rs.next())
try{
fIcqNo=rs.getString("icqNo");
sMsgs="LOGOUT icqNo:"+icqNo;
Mission fm=ICQserver.get(fIcqNo);
if(fm!=null)fm.addCmd(sMsgs);
}catch(Exception e){ICQserver.errmsg(" logout() distrbut ip or port error:"+e);}
ICQserver.del(icqNo);
ICQserver.errmsg(" "+icqNo+" logout,at IP:"+client.getInetAddress().toString());
}catch(Exception e){ICQserver.errmsg(" serverMon() error:"+e);}
}
public void addCmd(String s){outCmd.addElement(s);}
public String getCmd()
{
String s=(String)outCmd.elementAt(0);
outCmd.removeElementAt(0);
return s;
}
void seek(String inline,DataInputStream instream,PrintStream outstream)
{
try
{
String sql="select top 15 icqNo,nick,sex,age,addr,statu from icqNo";
String sqlAdd="";
if(-1!=inline.indexOf("SEEK nick:"))sqlAdd+=" where nick like '%"+inline.substring(inline.indexOf("nick:")+5).trim()+"%'";
if(-1!=inline.indexOf("SEEK icqNo:")){String icqNo=inline.substring(inline.indexOf("icqNo:")+6);try{Integer.parseInt(icqNo);}catch(Exception e){sendString(outstream,"FAIL 输入的号码不正确:"+e);return;}sqlAdd+=" where icqNo="+icqNo;}
if(-1!=inline.indexOf("SEEK email:"))sqlAdd+=" where email like '%"+inline.substring(inline.indexOf("email:")+6).trim()+"%'";
sql+=sqlAdd;
int page=0,pagesize=12;
if(-1!=inline.indexOf("SEEK online:")){
page=new Integer(inline.substring(inline.indexOf("online:")+7)).intValue()+1;
String num=new Integer(page*pagesize).toString();
sql="select top "+num+" icqNo,nick,sex,age,addr,statu from icqNo where statu=1";
}
//ICQserver.errmsg(" sql="+sql);
rs = stmt.executeQuery(sql);
String addr=null;
String s="";
if (page>0){for(int i=0;i<(page-1)*pagesize&&rs.next();i++);}
while(rs.next())
{
s+=rs.getString("icqNo")+" ";
if(rs.getString("statu").equals("1"))s+="在线 ";
else s+="离线 ";
s+=rs.getString("nick")+" ";
s+=rs.getString("sex")+" ";
s+=rs.getString("age")+" ";
s+=rs.getString("addr");
s+="\n";
}
if(s.equals("")){sendString(outstream,"FAIL 没找到");}
else
{
//ICQserver.errmsg(" s="+s);
sendString(outstream,"OK");
sendString(outstream,s);
}
}catch(Exception e){sendString(outstream,"FAIL 服务器操作时出错:"+e);ICQserver.errmsg(" seek() error:"+e);}
}
void join(String inline,DataInputStream instream,PrintStream outstream)
{
try{
String icqNo=inline.substring(inline.indexOf("icqNo:")+6);
icqNo=icqNo.substring(0,icqNo.indexOf(' '));
String friend=inline.substring(inline.indexOf("join:")+5);
String sql="select check,forward from icqNo where icqNo="+friend;
rs = stmt.executeQuery(sql);
if(rs.next()){
String note=rs.getString("forward");if(note==null)note="";
String check=rs.getString("check");if(check==null)check="否";
if(check.equals("否")){ //needn't check
sql="select * from friend where icqNo="+icqNo+" and friendNo="+friend;
rs = stmt.executeQuery(sql);
if(!rs.next()){//if is friend already
Mission m=ICQserver.get(friend);
if(m!=null){
m.addCmd("SYSMSG \nJOINED icqNo:"+icqNo+" 把你加为好友。");
}
else{ //if friend offline
note+="JOINED icqNo:"+icqNo+" 把你加为好友。\n";
sql="update icqNo set forward='"+note+"' where icqNo="+friend;
}
sql="insert into friend (icqNo,friendNo) values("+icqNo+","+friend+")";
stmt.executeUpdate(sql);
sql="select * from friend where icqNo="+friend+" and friendNo="+icqNo;
rs = stmt.executeQuery(sql);
if(!rs.next()){ //I am your friend.
sql="insert into friend (icqNo,friendNo) values("+friend+","+icqNo+")";
stmt.executeUpdate(sql);
}
sendString(outstream,"OK");
}
else sendString(outstream,"FAIL 你已经加他为好友了。");
//sendString(outstream,"FAIL 添加到数据库时出错.");
}
else //need check
{
Mission m=ICQserver.get(friend);
if(m!=null){
m.addCmd("SYSMSG \nJOIN icqNo:"+icqNo+" 要把你加为好友。");
}
else{ //if offline
note+="JOIN icqNo:"+icqNo+" 要把你加为好友。\n";
sql="update icqNo set forward='"+note+"' where icqNo="+friend;
stmt.executeUpdate(sql);
}
sendString(outstream,"FAIL 需要验证。"); //notfound
}
}
}catch(Exception e){sendString(outstream,"FAIL 操作时出错。");ICQserver.errmsg(" join() error:"+e);}
//end of this friend exist
}
void joined(String inline,DataInputStream instream,PrintStream outstream)
{
try{
String icqNo=inline.substring(inline.indexOf("icqNo:")+6);
icqNo=icqNo.substring(0,icqNo.indexOf(' '));
String friend=inline.substring(inline.indexOf("join:")+5);
String sql=null;
Mission m=ICQserver.get(friend);
if(m!=null){
m.addCmd("SYSMSG \nJOINED icqNo:"+icqNo+" 把你加为好友。");
}
else{ //if friend offline
sql="select forward from icqNo where icqNo="+friend;
rs = stmt.executeQuery(sql);
String note=rs.getString("forward");if(note==null)note="";
note+="JOINED icqNo:"+icqNo+" 把你加为好友。\n";
sql="update icqNo set forward='"+note+"' where icqNo="+friend;
stmt.executeUpdate(sql);
}
//I am your friend
sql="insert into friend (icqNo,friendNo) values("+friend+","+icqNo+")";
stmt.executeUpdate(sql);
//you are my friend
sql="select * from friend where icqNo="+icqNo+" and friendNo="+friend;
rs = stmt.executeQuery(sql);
if(!rs.next()){
sql="insert into friend (icqNo,friendNo) values("+icqNo+","+friend+")";
stmt.executeUpdate(sql);
}else {
sendString(outstream,"FAIL 你已经加他为好友了。");
return;
}
sendString(outstream,"OK");
}catch(Exception e){sendString(outstream,"FAIL 操作时出错。");ICQserver.errmsg(" joined() error:"+e.getMessage());e.printStackTrace();}
}
void forward(String inline,DataInputStream instream,PrintStream outstream)
{
try
{
String from=inline.substring(inline.indexOf("from:")+5);
from=from.substring(0,from.indexOf(' '));
String to=inline.substring(inline.indexOf("to:")+3);
String msg=to.substring(to.indexOf(' ')+1);
to=to.substring(0,to.indexOf(' '));
Mission m=ICQserver.get(to);
if(m!=null){
m.addCmd("SYSMSG \n"+msg);
sendString(outstream,"OK");
}
else{
String sql="select forward from icqNo where icqNo="+to;
rs = stmt.executeQuery(sql);
if(rs.next()){
String note=rs.getString("forward");
if(note.lastIndexOf('\n')!=note.length()-1)note+="\n";
msg=replace(msg,"'","''"); //if the forward msg comtain ' then the sql will error
note+=msg+"\n";
sql="update icqNo set forward='"+note+"' where icqNo="+to;
//ICQserver.errmsg(" sql="+sql);
stmt.executeUpdate(sql);
sendString(outstream,"OK");
}
else sendString(outstream,"FAIL 需要验证。");
}
}catch(Exception e){sendString(outstream,"FAIL 操作时出错。");ICQserver.errmsg(" forward() error:"+e);e.printStackTrace();}
//end of this friend exist
}
void delFriend(String inline,DataInputStream instream,PrintStream outstream)
{
try
{
String icqNo=inline.substring(inline.indexOf("icqNo:")+6);
icqNo=icqNo.substring(0,icqNo.indexOf(' '));
String del=inline.substring(inline.indexOf("del:")+4);
String sql="delete from friend where icqNo="+icqNo+" and friendNo="+del;
//System.out.println(" delFriend sql="+sql);
stmt.executeUpdate(sql);
sendString(outstream,"OK");
}catch(Exception e){sendString(outstream,"FAIL 操作时出错。");ICQserver.errmsg(" delFriend() error:"+e);}
//end of this friend exist
}
void stranger(String inline,DataInputStream instream,PrintStream outstream)
{
try
{
sendString(outstream,"OK 完成。");
}catch(Exception e){sendString(outstream,"FAIL 操作时出错。");ICQserver.errmsg(" stranger() error:"+e);}
//end of this friend exist
}
String replace(String s1,String s2,String s3)
{
if(s1==null||s2==null||s3==null)return s1;
String s="";
try{
while(-1!=s1.indexOf(s2)){
s+=s1.substring(0,s1.indexOf(s2))+s3;
s1=s1.substring(s1.indexOf(s2)+s2.length());
}
s+=s1;
}catch(Exception e){ICQserver.errmsg(" replace() error:"+e);}
return s;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -