-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUserManagement.h
431 lines (369 loc) · 13.2 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#ifndef __USERMANAGEMENT_H__
#define __USERMANAGEMENT_H__
#include "TcpSocket.h"
#include <mysql++/mysql++.h>
#include <mysql++/result.h>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <fstream>
class UserManagement
{
private:
const int key=23;
ServerTcpSocket& server;
mysqlpp::Connection con;
mysqlpp::Query query;
mysqlpp::StoreQueryResult result;
std::string userID;
std::string logFile;
public:
enum Flag{end};
UserManagement(ServerTcpSocket& _server,
std::string DBname,
std::string IP,
std::string id,
std::string pw,
std::string logFile) :
server(_server) ,
con(DBname.c_str(),IP.c_str(),id.c_str(),pw.c_str(),3306) ,
query(con.query()) ,
logFile(logFile)
{
}
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;
}
static int specialCharCheck(std::string param,std::string plot="")
{
for(char tmp:param)
for(char plotTmp:plot)
if(tmp==plotTmp)
return false;
return true;
}
void writeLog(std::string param)
{
std::ofstream log(logFile,std::ios::app);
time_t nowTime=time(NULL);
std::string timeString(ctime(&nowTime));
log<<timeString.substr(0,timeString.length()-1)<<" : "<<param<<std::endl;
log.close();
}
UserManagement& login() //send userdata
{
if(userID.length()!=0)
throw end;
std::string id=server.receive(key);
std::string pw=server.receive(key);
writeLog("login request ID("+id+")");
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(std::string(result.at(0)["NAME"]),key);
writeLog("login success ID("+userID+") ");
}
else
{
server.send("fail",key);
writeLog("login fail ID("+id+") ");
}
return *this;
}
UserManagement& logout()
{
std::string tmp=userID;
writeLog("logout request ID("+userID+")");
if(userID.length()==0)
throw end;
userID="";
writeLog("logout success ID("+tmp+")");
}
UserManagement& IDoverlapCheck()
{
std::string id=server.receive(key);
writeLog("request ID overlap check ID("+id+")");
if(id.length()<5||id.length()>20)
throw "ID length error";
query<<"select ID from userdata where ID='"+id+"'";
result=query.store();
if(result.num_rows()==0)
{
server.send("success",key);
writeLog("ID overlap check success ID("+id+") ");
}
else
{
server.send("ID OverLap!",key);
writeLog("ID overlap check fail ID("+id+") ");
}
return *this;
}
UserManagement& join()
{
if(userID.length()!=0)
throw end;
std::string id=server.receive(key);
std::string pw=server.receive(key);
std::string name=server.receive(key);
writeLog("join request ID("+id+")");
try
{
if(id.length()<5||id.length()>20||pw.length()<5||pw.length()>20)
throw end;
if(specialCharCheck(id,""))
query<<"insert into userdata(ID,PW,NAME,_) values('"+id+"',password('"+pw+"'),'"+name+"','"+pw+"')";
result=query.store();
server.send("success",key);
writeLog("join success ID("+id+")");
}
catch(mysqlpp::BadQuery& e)
{
if(e.errnum()==1062)
throw end;
}
return *this;
}
UserManagement& withdraw()
{
if(userID.length()==0)
throw end;
std::string pw=server.receive(key);
writeLog("withdraw request ID("+userID+")");
query<<"select * from userdata where ID='"+userID+"' and PW=password('"+pw+"')";
result=query.store();
if(result.num_rows()==0)
{
server.send("PW different",key);
writeLog("withdraw fail ID("+userID+")");
}
else
{
std::vector<std::string> friendArray=list2array(std::string(result.at(0)["friendsList"]));
for(std::string friendID: friendArray)
{
query<<"select friendsList from userdata where ID='"+friendID+"'";
result=query.store();
std::vector<std::string> array=list2array(std::string(result.at(0)["friendsList"]));
std::string list;
for(std::string tmp:array)
if(tmp!=userID)
list+=tmp+";";
query<<"update userdata set friendsList='"+list+"' where ID='"+friendID+"'";
query.store();
}
query<<"delete from userdata where ID='"+userID+"' and PW=password('"+pw+"')";
result=query.store();
userID="";
server.send("success",key);
writeLog("withdraw success ID("+userID+")");
}
return *this;
}
UserManagement& searchID()
{
std::string searchID=server.receive(key);
writeLog("searchID request ID("+userID+") friendID("+searchID+")");
query<<"select * from userdata where ID = '"+searchID+"'"; //regexp
result=query.store();
if(result.num_rows()==1)
{
server.send(std::string(result.at(0)["ID"]),key);
server.send(std::string(result.at(0)["NAME"]),key);
writeLog("searchID success ID("+userID+") friendID("+searchID+")");
}
else
{
server.send("not Find!",key);
writeLog("searchID fail ID("+userID+") friendID("+searchID+")");
}
return *this;
}
UserManagement& addFriend()
{
if(userID.length()==0)
throw end;
std::string friendsID=server.receive(key);
writeLog("addFriend request ID("+userID+") friendID("+friendsID+")");
query<<"select friendsList from userdata where ID='"+userID+"'";
result=query.store();
std::vector<std::string> array=list2array(std::string(result.at(0)["friendsList"]));
array.push_back(friendsID);
std::sort(array.begin(),array.end());
std::unique(array.begin(),array.end());
std::string list;
for(std::string tmp:array)
list+=tmp+";";
query<<"update userdata set friendsList='"+list+"' where ID='"+userID+"'";
result=query.store();
query<<"select friendsList from userdata where ID='"+friendsID+"'";
result=query.store();
array.clear();
array=list2array(std::string(result.at(0)["friendsList"]));
array.push_back(userID);
std::sort(array.begin(),array.end());
std::unique(array.begin(),array.end());
list="";
for(std::string tmp:array)
list+=tmp+";";
query<<"update userdata set friendsList='"+list+"' where ID='"+friendsID+"'";
result=query.store();
writeLog("addFriend success ID("+userID+") friendID("+friendsID+")");
return *this;
}
UserManagement& myFriendsList()
{
if(userID.length()==0)
throw end;
writeLog("friendsList request ID("+userID+")");
query<<"select friendsList from userdata where ID='"+userID+"'";
result=query.store();
std::vector<std::string> array=list2array(std::string(result.at(0)["friendsList"]));
for(std::string tmp:array)
{
query<<"select NAME from userdata where ID='"+tmp+"'";
result=query.store();
server.send(tmp,key);
server.send(std::string(result.at(0)["NAME"]),key);
}
server.send("end!",key);
writeLog("friendsList success ID("+userID+")");
return *this;
}
UserManagement& deleteFriend()
{
if(userID.length()==0)
throw end;
std::string friendsID=server.receive(key);
writeLog("deleteFriend request ID("+userID+") friendID("+friendsID+")");
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)
list+=tmp+";";
query<<"update userdata set friendsList='"+list+"' where ID='"+userID+"'";
result=query.store();
writeLog("deleteFriend success ID("+userID+") friendID("+friendsID+")");
return *this;
}
UserManagement& makeChatRoom()
{
if(userID.length()==0)
throw end;
std::string roomname;
std::vector<std::string> invite;
roomname=server.receive(key);
writeLog("makeChatRoom request ID("+userID+")");
std::string tmp;
invite.push_back(userID);
while(1)
{
tmp=server.receive(key);
if(tmp=="end!")
break;
invite.push_back(tmp);
}
std::string list;
for(std::string tmp:invite)
list+=tmp+";";
srand(time(NULL)*getpid());
int checkValue=rand()%99999+1;
query<<"insert into roomnumber (name,memberlist,`check`) values ('"+roomname+"','"+list+"','"+std::to_string(checkValue)+"')";
query.store();
query<<"select number from roomnumber where `check`='"+std::to_string(checkValue)+"'";
result=query.store();
int roomNumber=result.at(result.num_rows()-1)["number"];
query<<"update roomnumber set `check`='0' where `check`='"+std::to_string(checkValue)+"'";
result=query.store();
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::sort(array.begin(),array.end());
std::unique(array.begin(),array.end());
std::string list1;
for(std::string tmp1:array)
list1+=tmp1+";";
query<<"update userdata set chatList='"+list1+"' where ID='"+tmp+"'";
result=query.store();
}
query<<"create table chatroom.room"+std::to_string(roomNumber)+" (number int primary key auto_increment ,date timestamp not null,sender varchar(20) not null,msg varchar(200) not null)";
query.store();
writeLog("makeChatRoom success ID("+userID+")");
return *this;
}
UserManagement& myChatList()
{
if(userID.length()==0)
throw end;
writeLog("chatList request ID("+userID+")");
query<<"select chatList from userdata where ID='"+userID+"'";
result=query.store();
std::vector<std::string> array=list2array(std::string(result.at(0)["chatList"]));
for(std::string tmp:array)
{
query<<"select name from roomnumber where number='"+tmp+"'";
result=query.store();
server.send(tmp,key);
server.send(std::string(result.at(0)["name"]),key);
}
server.send("end!",key);
writeLog("chatList success ID("+userID+")");
return *this;
}
UserManagement& outChatRoom()
{
if(userID.length()==0)
throw end;
std::string roomNumber=server.receive(key);
writeLog("outChatRoom request ID("+userID+") roomNumber("+roomNumber+")");
//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();
writeLog("outChatRoom success ID("+userID+") roomNumber("+roomNumber+")");
return *this;
}
};
#endif