-
Notifications
You must be signed in to change notification settings - Fork 0
/
CONVOAPP.CPP
628 lines (588 loc) · 14.6 KB
/
CONVOAPP.CPP
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#include <iostream.h>
#include <conio.h>
#include <time.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<fstream.h>
#include<ctype.h>
//v with pwd
void sp(int down,int left);
class user;
class chatroom;
void messageloop(int temp,char chatroom[30]);
void main();
void newUser();
void existingUser();
void chat(char chatname[30]);
void userControlPanel(char *uname);
char current_user[30]; //current user
//end:Public function prototypes and global
//start: global functions
//ui designing function
void sp(int down, int left) {
for(int i =0;i<down;i++) {
cout << "\n";
}
for(int l =0;l<left;l++) {
cout << " ";
}
}
//callBatchfile
void callBatchFile(char batchp[30]) {
strcat(batchp,".bat");
system(batchp);
}
//end: global functions
/*start: Message class definition
//Sample implementation code
void main() {
clrscr();
message msg1;
msg1.inputMessage();
msg1.output();
getch();
}
//Status: Fully working and operational
*/
class message {
char title[30];
char from[30];
char to[30];
char msg[200];
public:
int msg_status;
/* msg status of 1 indicates user confirmed success delivery of message
msg status of 0 indicates a error */
//prototypes
void inputMessage();
void writetoDb(char filename[30],message m);
void checkInput(char x[300],int max);
void output();
//default constructor
message() {
strcpy(title,"error") ;
strcpy(from,"error");
strcpy(to,"error");
strcpy(msg,"error");
msg_status = 0;
}
}newm,om[50],tempm[50]; //end of message class
//validator for inputs to the message objects
void message::checkInput(char x[300],int max) {
if(!(strlen(x) <= max)) {
cout << "Input must be less than 30 characters";
msg_status = 0;
//add input function here.
}
}
//Output message
void message :: output() {
cout<< endl <<endl;
if(strcmp(title,"error") != 0) {
cout << "Message from ";
puts(from) ;
cout << endl << "Title: ";
puts(title); cout << endl;
puts(msg); cout << endl;
cout << endl;
}
}
//Input message
void message :: inputMessage() {
char ch;
cin.ignore();
strcpy(from,current_user); //Chage crt user with the logged in user
cout << "Input title of message : " << endl;
cin.getline(title,30);
cout << "Input message(Enter . to terminate : " << endl;
cin.getline(msg,200,'.');
cout << "Send ? ... (Y/N) ";
cin >> ch;
if(ch == 'Y' || ch == 'y') {
msg_status = 1;
cout << "Message send.";
} else {
cout << endl;
cout << "NewDraft: " << endl;
inputMessage();
}
cout << endl;
}
void message :: writetoDb(char filename[30],message m) {
ofstream chat;
strcat(filename,".txt");
chat.open(filename,ios::app | ios::binary);
chat.write((char*)&m,sizeof(m));
chat.close();
}
//end: Message class definition
//start: Chat function start comes here after the join a chatrrom function
void chat(char chatname[30]) {
char filename[30];
int tempmsgcount =0 ;
strcpy(filename,chatname);
strcat(filename,".txt");
clrscr();
sp(2,0);
cout << chatname << " : ";
fstream chat;
chat.open(filename,ios::in,ios::noreplace);
chat.seekg(0,ios::end);
// if file is empty
if(chat.tellg() == -1) {
chat.close();
sp(3,10);
newm.inputMessage();
newm.writetoDb(chatname,newm);
} else {
ifstream chat;
chat.open(filename,ios::in | ios::noreplace);
int count = 0;
//Lists existing messages
om[count].msg_status =1;
while(!chat.eof()) {
chat.read((char*)&om[count],sizeof(om[count]));
if(om[count].msg_status== 1){
om[count].output();
om[count].msg_status =0;
}
getch();
}//end of while
chat.close();
messageloop(tempmsgcount,chatname);
} //end of else
} //end of function
void messageloop(int tempmsgcount,char chatname[30]) {
char user_ch;
ifstream chat;
cout << endl << "Do you wish to write a message[Y,N]: ";
cin >> user_ch;
if(user_ch == 'Y' || user_ch == 'y') {
chat.read((char*)&tempm[tempmsgcount],sizeof(tempm[tempmsgcount]));
tempm[tempmsgcount].inputMessage();
tempm[tempmsgcount].writetoDb(chatname,tempm[tempmsgcount]);
tempmsgcount++;
messageloop(tempmsgcount,chatname);
} else {
clrscr();
chat.close();
userControlPanel(current_user);
}
}
//end: Chat function end
//1.
//start: User class definition
//Users start
class user {
char username[30];
char pw[30];
char about[100];
float activity;
float start,mins,end,seconds;
public:
//default constuctor
user() {
strcpy(username,"empty");
strcpy(pw,"empty");
strcpy(about,"empty");
activity = 0;
}
//start timer on login
void timestart() {
start= time(0);
}
//Create new user
void create() {
sp(1,2);
cout << "Registration: ";
cin.ignore();
sp(3,2);
cout<<"Enter user name: ";
cin.getline(username,30);
cin.ignore(0,'\n');
sp(1,2);
cout<<"Enter password: ";
cin.getline(pw,30);
sp(1,2);
cout << "About yourself [100 wrds max] : " ;
sp(1,2);
cout << "(Terminate with .)" ;
sp(1,0);
cin.getline(about,100,'.');
}
//Print out user details
void printUserData() {
cout << username << endl;
cout << pw << endl;
cout << about << endl;
}
//stop and log time on user logout
void logout() {
end=time(0);
seconds=difftime(end,start);
mins=seconds/60;
}
//write user data to users.txt
void writeUserData(user obj) {
//obj must be intialized with values at this point
ofstream db("users.txt",ios::app|ios::binary);
db.write((char*)&obj,sizeof(obj));
db.close();
sp(0,2);
cout<<"Registeration complete!!";
}
//intialises a temp variable with username value
void uname(char input[30]) {
for(int i =0;i<30;i++) {
input[i] = username[i];
}
}
//initialises a temp variable with pwd value
void pwd(char input[30]) {
for(int i =0;i<30;i++) {
input[i] = pw[i];
}
}
//read All User from db
void readUserFromDb() {
user ts[100];
ifstream db;
db.open("users.txt",ios::in|ios::binary);
clrscr();
db.seekg(0);
int i =0; //user object array counter
cout << "The Users Databbase: " << endl;
while(!db.eof()) {
db.read((char*)&ts[i],sizeof(ts[i]));
ts[i].printUserData();
cout << endl;
i++;
getch();
} // end of while
} //end of read db function
//search user database for user
void searchUser(char* login_name,char* login_code) {
user ts[100];
ifstream db;
db.open("users.txt",ios::in|ios::binary);
clrscr();
db.seekg(0);
int i =0; //user object array counter
int flag = 0;
char uname[30];
char pwd[30];
sp(2,0);
while(!db.eof()) {
db.read((char*)&ts[i],sizeof(ts[i]));
ts[i].uname(uname);
ts[i].pwd(pwd);
//comparing credentails
if(!strcmp(uname,login_name)) {
if(!strcmp(pwd,login_code)) {
clrscr();
flag=1;
strcpy(current_user,login_name);
userControlPanel(uname);
getch();
break;
} else {
clrscr();
cout << "User identified with Wrong password";
getch();
abort();
}
} else {
continue; // user does not match , check next record
}
i++;
} //end of while
if(!flag) {
clrscr();
cout << "No such user exists .";
getch();
abort();
}
} //end of fucntion searchUser
};
//end: User class definition
//2.
//start:Chatroom class definition
class chatroom {
//participants
char name[30];
char pc[30]; //passcode
int max;
int pri; //1 indicates private chatroom,0 indicates public chtaroom
public:
int ppsize ;
chatroom() {
ppsize = 0;
max =20;
strcpy(name,"empty");
strcpy(pc,"empty");
}
//custom max users
chatroom(int i) {
max =i;
}
//prototypes
void create(char *creator);
//inline functions
//compares the name with input
int searchname(char input[30]) {
if(strcmp(name,input) == 0) {
return 1;
} else {
return 0;
}
}
int searchpwd(char input[30]) {
if(strcmp(pc,input) == 0) {
return 1;
} else {
return 0;
}
}
int isPri() {
return pri;
}
//prints out the name
void printName() {
cout << name;
}
} newChatroom,tc[50],rc[50] ;
void chatroom :: create(char *creator) {
clrscr();
char ch;
char uch;
//intializing p_input and pp size
cout << endl << "Create new chatroom ? (Y/N): ";
cin >>ch;
if(ch == 'Y' || ch == 'y') {
cout << endl << "Enter name of chatroom: ";
gets(name);
//cap on max users is 100 by defulat
cout << "Add password ? [Y/N]:";
cin >> uch;
if(uch == 'Y' || uch == 'y') {
cout << "Enter passcode: " ;
gets(pc);
pri = 1;
}else {
pri = 0;
}
char temp[30];
strcpy(temp,name);
strcat(temp,".txt");
//creating on empty txt file
fstream file;
file.open(temp,ios::out | ios::binary);
file.close();
} else {
userControlPanel(creator);
}
}; //endof create function;
//end: Chatroom class defintion
//start: Homescreen functions
//User Registration
void newUser() {
user newUser;
clrscr();
newUser.create();
newUser.writeUserData(newUser);
getch();
abort();
}
//to handle existing users
void existingUser() {
//login screen
clrscr();
char login_name[30];
char login_pwd[30];
sp(1,2);
cout << "Login: ";
cin.ignore();
sp(3,2);
cout<<"Enter user name: ";
cin.getline(login_name,30);
cin.ignore(0,'\n');
sp(1,2);
cout<<"Enter password: ";
cin.getline(login_pwd,30);
sp(1,2);
user temp_loginuser;
temp_loginuser.searchUser(login_name,login_pwd);
} ;
//User Menu Screen
void userControlPanel(char *uname) {
int ch ;
strcpy(current_user,uname);
sp(1,1);
cout << "Welcome back, " << uname;
sp(3,3);
cout << "1.Create Chatroom" ;
sp(2,3);
cout << "2.Join Chatroom";
sp(2,3);
cout << "3.Back ";
sp(3,3);
cout << "Input ch number: " ;
cin >> ch;
if(ch == 1 ) {
//:start chatroom database handling (inserting records)
newChatroom.create(uname);
ofstream db("chatrooms.txt",ios::app|ios::binary);
db.write((char*)&newChatroom,sizeof(newChatroom));
db.close();
sp(2,0);
cout<<"Chatroom registered !";
getch();
clrscr();
userControlPanel(uname);
//:end chatroom database handling (inserting records)
} else if(ch == 2) {
//:start listing out records in chatrooms.txt
ifstream wb;
wb.open("chatrooms.txt",ios::in|ios::binary);
clrscr();
sp(1,3);
cout << "Active Chatrooms" << endl;
sp(1,3);
wb.seekg(0);
int c =0; //user object array counter
int temp_c =1;
while(!wb.eof()) {
wb.read((char*)&rc[c],sizeof(rc[c]));
//checks for empty object
if(rc[c].searchname("empty")) {
break;
}
cout << temp_c << "." ;
rc[c].printName();
sp(1,3);
c++;
temp_c++;
getch();
} // end of while
//end: listing out records in chatrooms.txt
//:start chatroom database handling (searching records)
char login_chatname[30]; //user_input
char temp_name[30];
char login_pwd[30];
sp(2,0);
cout << "Enter name of chatroom: " ;
cin.ignore();
gets(login_chatname);
// search chatroom db for input here
ifstream db;
db.open("chatrooms.txt",ios::in|ios::binary);
clrscr();
db.seekg(0);
int i =0; //user object array counter
int flag = 0;
sp(2,0);
while(!db.eof()) {
db.read((char*)&tc[i],sizeof(tc[i]));
int search_result = tc[i].searchname(login_chatname);
if(search_result == 1) {
if(tc[i].isPri()) {
cout << "Enter pwd of chatroom: " ;
gets(login_pwd);
int pwd_search_result = tc[i].searchpwd(login_pwd);
if(pwd_search_result == 1) {
flag =1;
chat(login_chatname);
break;
}
} else {
chat(login_chatname);
break;
}
}
}
if (flag==0) {
clrscr();
cout << "No such chatroom exists or password entered is wrong.";
getch();
db.close();
abort();
}
//:end chatroom database handling (searching records)
} else if(ch == 3) {
abort(); //back to homescreen
}else {
clrscr();
cout << "invalid option" ;
getch();
clrscr();
userControlPanel(uname);
}
};
//end: Homescreen functions
void main() {
clrscr();
int user_ch;
//Homescreen
sp(1,0);
cout << "LAN Messenger:";
sp(3,3);
cout << "1.New User" ;
sp(1,3);
cout << "2.Existing user";
sp(1,3);
cout << "3.Settings";
sp(1,3);
cout << "4.Exit";
sp(2,3);
cout << "Input path no. ";
cin >> user_ch;
if(user_ch == 1) {
//User Regstration path
newUser();
}else if(user_ch == 2) {
//Login path
existingUser();
}else if(user_ch == 3) {
//Admin Backend path
clrscr();
cout << "Welcome to admin settings:" << endl;
cout << "Input pwd : ";
char pwd[10];
gets(pwd);
if(!strcmp(pwd,"admin") || !strcmp(pwd,"ADMIN")){
clrscr();
user admin;
admin.readUserFromDb();
}else if(!strcmp(pwd,"chat") || !strcmp(pwd,"CHAT")) {
ifstream wb;
wb.open("chatrooms.txt",ios::in|ios::binary);
clrscr();
wb.seekg(0);
int c =0; //user object array counter
while(!wb.eof()) {
wb.read((char*)&rc[c],sizeof(rc[c]));
//checks for empty object
if(rc[c].searchname("empty")) {
break;
}
cout << c << "." ;
rc[c].printName();
cout << endl;
c++;
getch();
} // end of while
}else {
cout << "Wrong password";
}
} else if(user_ch == 4) {
abort();
}else {
clrscr();
cout << "Invalid choice" << endl;
getch();
clrscr();
abort();
}
getch();
}