forked from sejinRyu/Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatserverbackup
More file actions
201 lines (191 loc) · 7.92 KB
/
chatserverbackup
File metadata and controls
201 lines (191 loc) · 7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "TcpSocket.h"
#include <mysql++/mysql++.h>
#include <mysql++/result.h>
#include <vector>
using namespace std;
vector<string> list2array(string list,char plot=';')
{
string tmp;
vector<string> ret;
for(int i=0;i<list.length();i++)
{
if(list.at(i)==plot)
{
ret.push_back(tmp);
tmp="";
}
else tmp.push_back(list.at(i));
}
return ret;
}
int main(int argc,char** argv)
{
const int key=23;
try
{
ServerTcpSocket server;
while(server.accept()>0)
{
pid_t pid;
if((pid=fork())==0)
{
cout<<"connect!"<<endl;
mysqlpp::Connection con("chatserver","10.156.145.48","root","shangus1");
mysqlpp::Query query = con.query();
string loginUserID;
while(1)
{
int uuid=atoi(server.receive(key).c_str());
if(uuid==0)//login
{
string id=server.receive(key);
string pw=server.receive(key);
query<<"select * from userdata where ID='"+id+"' and PW=password('"+pw+"')";
mysqlpp::StoreQueryResult res=query.store();
if(res.num_rows()==1)
{
loginUserID=string(res.at(0)["ID"]);
server.send("success",key);
}
else server.send("fail",key);
}
else if(uuid==1)//add user
{
string id=server.receive(key);
string pw=server.receive(key);
string name=server.receive(key);
try
{
query<<"insert into userdata values('"+id+"',password('"+pw+"'),'"+name+"','"+pw+"')";
mysqlpp::StoreQueryResult res=query.store();
server.send("sucecss",key);
}
catch(mysqlpp::BadQuery& e)
{
if(e.errnum()==1062)
server.send("ID OverLap!",key);
}
}
else if(uuid==2)//delete user //edit need //secure
{
string id=server.receive(key);
string pw=server.receive(key);
query<<"delete from userdata where ID='"+id+"' and PW=password('"+pw+"')";
mysqlpp::StoreQueryResult res=query.store();
}
else if(uuid==3)//search ID -> send List
{
string searchID=server.receive(key);
query<<"select * from userdata where ID regexp '"+searchID+"'";
mysqlpp::StoreQueryResult res=query.store();
for(int i=0;i<res.num_rows();i++)
server.send(string(res.at(i)["ID"]),key);
server.send("end",key); //end flag //edit need
}
else if(uuid==4)//add friends //edit need //nothing account
{
string friendsID=server.receive(key);
query<<"select friendsList from userdata where ID='"+loginUserID+"'";
mysqlpp::StoreQueryResult res=query.store();
vector<string> array=list2array(string(res.at(0)[0]));
array.push_back(friendsID);
string list;
for(string tmp:array)
list+=tmp+";";
query<<"update userdata set friendsList='"+list+"' where ID='"+loginUserID+"'";
res=query.store();
}
else if(uuid==5)//friends list ->vector ->send
{
query<<"select friendsList from userdata where ID='"+loginUserID+"'";
mysqlpp::StoreQueryResult res=query.store();
vector<string> array=list2array(string(res.at(0)[0]));
for(string tmp:array)
server.send(tmp,key);
server.send("end",key); //end flag //need edit
}
else if(uuid==6)//delete friend
{
string friendsID=server.receive(key);
query<<"select friendsList from userdata where ID='"+loginUserID+"'";
mysqlpp::StoreQueryResult res=query.store();
vector<string> array=list2array(string(res.at(0)[0]));
string list;
for(string tmp:array)
{
if(tmp==friendsID)
continue;
list+=tmp+";";
}
query<<"update userdata set friendsList='"+list+"' where ID='"+loginUserID+"'";
res=query.store();
}
else if(uuid==7)//make chatroom
{
string roomname;
vector<string> invite;
roomname=server.receive(key);
string tmp;
while(1)
{
tmp=server.receive(key);
if(tmp=="end")//end flag //need edit
break;
invite.push_back(tmp);
}
string list;
for(string tmp:invite)
list+=tmp+";";
query<<"insert into roomnumber(name,memberlist) values('"+roomname+"','"+list+"')";
mysqlpp::StoreQueryResult res=query.store();
query<<"select number from roomnumber where name='"+roomname+"'";
res=query.store();
int roomNumber=res.at(res.num_rows()-1)[0];
for(string tmp:invite)
{
query<<"select chatList from userdata where ID='"+tmp+"'";
res=query.store();
vector<string> array=list2array(string(res.at(0)[0]));
array.push_back(to_string(roomNumber));
string list1;
for(string tmp1:array)
list1+=tmp1+";";
query<<"update userdata set chatList='"+list1+"' where ID='"+tmp+"'";
res=query.store();
}
//add need invite
}
else //echo
{
pid_t pid1=fork();
string tmp;
while(1)
{
if(pid1>0)
{
tmp=server.receive(key);
cout<<tmp<<endl;
}
else
{
cin>>tmp;
server.send(tmp,key);
}
}
}
}
}
wait(0);
}
}
catch (mysqlpp::BadQuery& e)
{
cerr << "BadQuery : " << e.what() << endl;
cerr<<e.errnum()<<endl;
}
catch(const char* e)
{
perror(e);
}
return 0;
}