forked from andresusanto/BintangMessenger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelper.cpp
More file actions
433 lines (364 loc) · 9.34 KB
/
Helper.cpp
File metadata and controls
433 lines (364 loc) · 9.34 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
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
432
433
// ceritanya satu untuk semua, satu helper untuk client dan server (code reusable :D )
#include "Helper.h"
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <algorithm>
using namespace std;
vector<string>& Helper::split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
vector<string> Helper::split(const string &s, char delim) {
vector<string> elems;
Helper::split(s, delim, elems);
return elems;
}
bool Helper::login(string username,string password){
string i_user, i_password;
ifstream infile("user.txt");
while (infile >> i_user >> i_password)
{
if (i_user.compare(username) == 0){
if (i_password.compare(password) == 0){
return true;
}else{
return false;
}
}
}
return false;
}
bool Helper::userExist(string username){
string i_user, i_password;
ifstream infile("user.txt");
while (infile >> i_user >> i_password)
{
if (i_user.compare(username) == 0){
return true;
}
}
return false;
}
bool Helper::signup(string username,string password){
string i_user, i_password;
ifstream infile("user.txt");
while (infile >> i_user >> i_password)
{
if (i_user.compare(username) == 0){
return false;
}
}
ofstream outfile("user.txt", ios_base::app | ios_base::out);
outfile << username << " " << password << "\n";
return true;
}
vector<Pesan> Helper::pesanUser(string username){
string line;
vector<Pesan> res;
Pesan tmp;
string namafile = "pending_" + username + ".txt";
ifstream infile(namafile.c_str());
while (getline(infile, line))
{
vector<string> dataLine = split(line, ';');
tmp.dari = dataLine[0];
tmp.waktu = stol(dataLine[4]);
tmp.pesan = dataLine[3];
tmp.gid = dataLine[1];
tmp.tipe = dataLine[2][0];
tmp.read = false;
res.push_back(tmp);
}
remove( namafile.c_str() );
return res;
}
void Helper::storePesan(string username, Pesan &pesan){
string namafile = "pending_" + username + ".txt";
ofstream outfile(namafile, ios_base::app | ios_base::out);
outfile << pesan.dari << ";" << pesan.gid << ";" << pesan.tipe << ";" << pesan.pesan << ";" << pesan.waktu << "\n";
}
vector<Pesan> Helper::loadChat(string username){
string line;
vector<Pesan> res;
Pesan tmp;
string namafile = "pesan_" + username + ".txt";
ifstream infile(namafile.c_str());
while (getline(infile, line))
{
vector<string> dataLine = split(line, ';');
tmp.dari = dataLine[0];
tmp.waktu = stol(dataLine[4]);
tmp.pesan = dataLine[3];
tmp.gid = dataLine[1];
tmp.tipe = dataLine[2][0];
if (tmp.tipe == 'G'){
tmp.read = viewACK( username, tmp.gid, tmp.tipe, tmp.waktu );
}else{
tmp.read = viewACK( username, tmp.dari, tmp.tipe, tmp.waktu );
}
int ukuran = res.size();
for(int i = 0; i < ukuran; i++){
if (res[i].tipe == 'G' && tmp.tipe == 'G'){
if (res[i].gid.compare(tmp.gid) == 0){
res.erase(res.begin() + i);
res.insert(res.begin(), tmp);
i = 99;
}
}else{
if (res[i].dari.compare(tmp.dari) == 0 && tmp.tipe != 'G' && res[i].tipe != 'G'){
res.erase(res.begin() + i);
res.insert(res.begin(), tmp);
i = 99;
}
}
if (i == ukuran - 1){
if (ukuran == 10){
res.erase(res.begin() + 9);
}
res.insert(res.begin(), tmp);
}
}
if (ukuran == 0){
res.insert(res.begin(), tmp);
}
}
return res;
}
vector<Pesan> Helper::loadpesanUser(string username, string filter, char tipe){
string line;
vector<Pesan> res;
Pesan tmp;
string namafile = "pesan_" + username + ".txt";
ifstream infile(namafile.c_str());
while (getline(infile, line))
{
vector<string> dataLine = split(line, ';');
tmp.dari = dataLine[0];
tmp.waktu = stol(dataLine[4]);
tmp.pesan = dataLine[3];
tmp.gid = dataLine[1];
tmp.tipe = dataLine[2][0];
tmp.read = readACK ( username, tmp.dari, tmp.tipe, tmp.waktu );
if (tipe == 'G' && tmp.tipe == 'G' && tmp.gid.compare(filter) == 0){
res.push_back(tmp);
}else if(tmp.tipe != 'G' && tipe != 'G' && tmp.dari.compare(filter) == 0){
res.push_back(tmp);
}
}
return res;
}
void Helper::simpanpesanUser(string username, Pesan &pesan){
string namafile = "pesan_" + username + ".txt";
ofstream outfile(namafile, ios_base::app | ios_base::out);
outfile << pesan.dari << ";" << pesan.gid << ";" << pesan.tipe << ";" << pesan.pesan << ";" << pesan.waktu << "\n";
}
bool Helper::readACK(string username, string dari, char tipe, long time){
string i_dari;
char i_tipe;
long i_time;
string namafile = "read_" + username + ".txt";
ifstream infile(namafile);
while (infile >> i_dari >> i_tipe >> i_time)
{
if (i_dari.compare(dari) == 0){
if (time <= i_time)
return true;
else
return false;
}
}
return false;
}
void Helper::savereadACK(string username, string dari, char tipe, long time){
string i_dari;
char i_tipe;
long i_time;
stringstream ss;
string namafile = "read_" + username + ".txt";
ifstream infile(namafile);
while (infile >> i_dari >> i_tipe >> i_time)
{
if (!(i_dari.compare(dari) == 0 && i_tipe == tipe)){
ss << i_dari << " " << i_tipe << " " << i_time << "\n";
}
}
ss << dari << " " << tipe << " " << time << "\n";
ofstream outfile(namafile, ios_base::out);
outfile << ss.str();
}
vector<string> Helper::loadRaw(string username){
string namafile = "raw_" + username + ".txt";
ifstream infile(namafile);
string line;
vector<string> tmp;
while (std::getline(infile, line))
{
tmp.push_back(line);
}
remove( namafile.c_str() );
return tmp;
}
void Helper::saveRaw(string username, string rawData){
string namafile = "raw_" + username + ".txt";
ofstream outfile(namafile, ios_base::app | ios_base::out);
outfile << rawData << "\n";
}
bool Helper::viewACK(string username, string dari, char tipe, long time){
string i_dari;
char i_tipe;
long i_time;
string namafile = "view_" + username + ".txt";
ifstream infile(namafile);
while (infile >> i_dari >> i_tipe >> i_time)
{
if (i_tipe == tipe && tipe == 'G' && i_dari.compare(dari) == 0){
if (time <= i_time)
return true;
else
return false;
}else if(i_tipe != 'G' && tipe != 'G' && i_dari.compare(dari) == 0){
if (time <= i_time)
return true;
else
return false;
}
}
return false;
}
void Helper::saveviewACK(string username, string dari, char tipe, long time){
string i_dari;
char i_tipe;
long i_time;
stringstream ss;
string namafile = "view_" + username + ".txt";
ifstream infile(namafile);
while (infile >> i_dari >> i_tipe >> i_time)
{
if (!(i_dari.compare(dari) == 0 && i_tipe == tipe)){
ss << i_dari << " " << i_tipe << " " << i_time << "\n";
}
}
ss << dari << " " << tipe << " " << time << "\n";
ofstream outfile(namafile, ios_base::out);
outfile << ss.str();
}
bool Helper::createGroup(string name){
string i_name;
ifstream infile("group.txt");
while (infile >> i_name)
{
if (i_name.compare(name) == 0){
return false;
}
}
ofstream outfile("group.txt", ios_base::app | ios_base::out);
outfile << name << "\n";
return true;
}
bool Helper::isGroup(string name){
string i_name;
ifstream infile("group.txt");
while (infile >> i_name)
{
if (i_name.compare(name) == 0){
return true;
}
}
return false;
}
bool Helper::isMemberGroup(string name, string username){
string i_name;
string namafile = "group_" + name + ".txt";
ifstream infile(namafile);
while (infile >> i_name)
{
if (i_name.compare(username) == 0){
return true;
}
}
return false;
}
void Helper::deleteGroup(string name){
string i_name;
stringstream ss;
ifstream infile("group.txt");
while (infile >> i_name)
{
if (i_name.compare(name) != 0){
ss << i_name << "\n";
}
}
ofstream outfile("group.txt", ios_base::out);
outfile << ss.str();
string namafile = "group_" + name + ".txt";
remove( namafile.c_str() );
}
bool Helper::joinGroup(string name, string user){
if (!isGroup(name)) return false;
string i_name;
string namafile = "group_" + name + ".txt";
ifstream infile(namafile);
while (infile >> i_name)
{
if (i_name.compare(user) == 0){
return false;
}
}
ofstream outfile(namafile, ios_base::app | ios_base::out);
outfile << user << "\n";
return true;
}
bool Helper::leaveGroup(string name, string user){
bool isi = false;
bool isRemoved = false;
string i_name;
stringstream ss;
string namafile = "group_" + name + ".txt";
ifstream infile(namafile);
while (infile >> i_name)
{
if (i_name.compare(user) == 0){
isRemoved = true;
}else{
isi = true;
ss << i_name << "\n";
}
}
ofstream outfile(namafile, ios_base::out);
outfile << ss.str();
if (!isi){
deleteGroup(name);
}
return isRemoved;
}
vector<string> Helper::memberGroup(string name){
vector<string> member;
string i_name;
string namafile = "group_" + name + ".txt";
ifstream infile(namafile);
while (infile >> i_name)
{
member.push_back(i_name);
}
return member;
}
int Helper::ukuranFile(string namafile){
ifstream in(namafile, std::ifstream::ate | std::ifstream::binary);
return in.tellg();
}
void Helper::serverLog(string log){
time_t rawtime;
time (&rawtime);
struct tm * timeinfo = localtime (&rawtime);
string waktu = asctime(timeinfo);
replace(waktu.begin(), waktu.end(), '\n', ']');
cout << "[" << waktu << " " << log << "\n";
ofstream outfile("server.log", ios_base::app | ios_base::out);
outfile << "[" << waktu << " " << log << "\n";
}