forked from sejinRyu/Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserManagement.h
282 lines (252 loc) · 8.29 KB
/
UserManagement.h
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#ifndef __USERMANAGEMENT_H__
#define __USERMANAGEMENT_H__
#include "TcpSocket.h"
#include <mysql++/mysql++.h>
#include <mysql++/result.h>
#include <vector>
class UserManagement //edit key
{
private:
const int key=23;
ServerTcpSocket& server;
mysqlpp::Connection con;
mysqlpp::Query query;
mysqlpp::StoreQueryResult result;
std::string userID;
public:
enum Flag{end};
UserManagement(ServerTcpSocket& _server,
std::string DBname,
std::string IP,
std::string id,
std::string pw) :
server(_server) ,
con(DBname.c_str(),IP.c_str(),id.c_str(),pw.c_str(),3306) ,
query(con.query())
{
//con.connect(DBname.c_str(),IP.c_str(),id.c_str(),pw.c_str(),3306);
//query=con.query();
}
static std::vector<std::string> list2array(std::string list,char plot=';')
{
std::string tmp;
std::vector<std::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;
}
UserManagement& login() //send userdata
{
if(userID.length()!=0)
throw end;
std::string id=server.receive(key);
std::string pw=server.receive(key);
query<<"select * from userdata where ID='"+id+"' and PW=password('"+pw+"')";
result=query.store();
if(result.num_rows()==1)
{
userID=std::string(result.at(0)["ID"]);
server.send("success",key);
}
else server.send("fail",key);
return *this;
}
UserManagement& logout()
{
if(userID.length()==0)
throw end;
userID="";
server.send("logout seccess",key);
}
UserManagement& join() //delete throw
{
if(userID.length()!=0)
throw end;
std::string id=server.receive(key);
std::string pw=server.receive(key);
std::string name=server.receive(key);
try
{
if(id.length()<5||id.length()>20)
throw "ID length error";
if(pw.length()<5||pw.length()>20)
throw "PW length error";
query<<"insert into userdata(ID,PW,NAME,_) values('"+id+"',password('"+pw+"'),'"+name+"','"+pw+"')";
result=query.store();
server.send("sucecss",key);
}
catch(mysqlpp::BadQuery& e)
{
if(e.errnum()==1062)
server.send("ID OverLap!",key);
}
catch(const char* e)
{
server.send(std::string(e),key);
}
std::cout<<"a"<<std::endl;
return *this;
}
UserManagement& withdraw()
{
if(userID.length()==0)
throw end;
std::string pw=server.receive(key);
query<<"select * from userdata where ID='"+userID+"' and PW=password('"+pw+"')";
result=query.store();
if(result.num_rows()==0)
server.send("PW different",key);
else
{
query<<"delete from userdata where ID='"+userID+"' and PW=password('"+pw+"')";
result=query.store();
server.send("success",key);
}
return *this;
}
UserManagement& searchID()
{
std::string searchID=server.receive(key);
query<<"select * from userdata where ID regexp '"+searchID+"'";
result=query.store();
for(int i=0;i<result.num_rows();i++)
server.send(std::string(result.at(i)["ID"]),key);
server.send("end",key); //end flag //edit need
return *this;
}
UserManagement& addFriend()
{
if(userID.length()==0)
throw end;
std::string friendsID=server.receive(key);
query<<"select friendsList from userdata where ID='"+userID+"'";
result=query.store();
std::vector<std::string> array=list2array(std::string(result.at(0)[0]));
array.push_back(friendsID);
std::string list;
for(std::string tmp:array)
list+=tmp+";";
query<<"update userdata set friendsList='"+list+"' where ID='"+userID+"'";
result=query.store();
return *this;
}
UserManagement& myFriendsList() //send ID //edit need name send
{
if(userID.length()==0)
throw end;
query<<"select friendsList from userdata where ID='"+userID+"'";
result=query.store();
std::vector<std::string> array=list2array(std::string(result.at(0)[0]));
for(std::string tmp:array)
server.send(tmp,key);
server.send("end",key); //end flag //need edit
return *this;
}
UserManagement& deleteFriend()
{
if(userID.length()==0)
throw end;
std::string friendsID=server.receive(key);
query<<"select friendsList from userdata where ID='"+userID+"'";
result=query.store();
std::vector<std::string> array=list2array(std::string(result.at(0)[0]));
std::string list;
for(std::string tmp:array)
{
if(tmp==friendsID)
continue;
list+=tmp+";";
}
query<<"update userdata set friendsList='"+list+"' where ID='"+userID+"'";
result=query.store();
}
UserManagement& makeChatRoom()
{
if(userID.length()==0)
throw end;
std::string roomname;
std::vector<std::string> invite;
roomname=server.receive(key);
std::string tmp;
while(1)
{
tmp=server.receive(key);
if(tmp=="end")//end flag //need edit
break;
invite.push_back(tmp);
}
std::string list;
for(std::string tmp:invite)
list+=tmp+";";
query<<"insert into roomnumber(name,memberlist) values('"+roomname+"','"+list+"')";
result=query.store();
query<<"select number from roomnumber where name='"+roomname+"'";
result=query.store();
int roomNumber=result.at(result.num_rows()-1)[0];
for(std::string tmp:invite)
{
query<<"select chatList from userdata where ID='"+tmp+"'";
result=query.store();
std::vector<std::string> array=list2array(std::string(result.at(0)[0]));
array.push_back(std::to_string(roomNumber));
std::string list1;
for(std::string tmp1:array)
list1+=tmp1+";";
query<<"update userdata set chatList='"+list1+"' where ID='"+tmp+"'";
result=query.store();
}
return *this;
}
UserManagement& myChatList() //send num //edit need name send
{
if(userID.length()==0)
throw end;
query<<"select chatList from userdata where ID='"+userID+"'";
result=query.store();
std::vector<std::string> array=list2array(std::string(result.at(0)[0]));
for(std::string tmp:array)
server.send(tmp,key);
server.send("end",key); //end flag //need edit
}
UserManagement& outChatRoom()
{
if(userID.length()==0)
throw end;
std::string roomNumber=server.receive(key);
//userdata update
query<<"select chatList from userdata where ID='"+userID+"'";
result=query.store();
std::vector<std::string> array=list2array(std::string(result.at(0)[0]));
std::string list;
for(std::string tmp:array)
{
if(tmp==roomNumber)
continue;
list+=tmp+";";
}
query<<"update userdata set chatList='"+list+"' where ID='"+userID+"'";
result=query.store();
//roomnumber update
query<<"select memberList from roomnumber where number='"+roomNumber+"'";
result=query.store();
array=list2array(std::string(result.at(0)[0]));
list.clear();
for(std::string tmp:array)
{
if(tmp==userID)
continue;
list+=tmp+";";
}
query<<"update roomnumber set chatList='"+list+"' where number='"+roomNumber+"'";
result=query.store();
//add need send signal to chating server
}
};
#endif