-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22CS60R70_server.c
1708 lines (1594 loc) · 52.9 KB
/
22CS60R70_server.c
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>
#include <stdbool.h>
#include <wait.h>
#define PORT "9034" // port we're listening on
/////////////////////////////////////////////////////////////////////////
// ID: 22CS60R70 //
// SERVER CODE //
// How to run? //
// - gcc 22CS60R70_A7_P1_server.c -o server //
// - ./server //
// //
/////////////////////////////////////////////////////////////////////////
struct client
{
int id;
int socketfd;
int grpCount;
int joinedGroups[128];
int abuseCount;
};
struct client *clientList[10];
struct message
{
int from;
int to;
char *message;
};
struct Group
{
int groupid; // unique group-id
int membersid[5]; // members of group
int tempMembers[4]; // pending member requests
bool isCreated; // group is active or not
int grpsize; // no. of members
int reqSize; // no. of request sent
int admins[5]; // id of admins
int adminlen; // no. of admins
bool isBroadcast; // admin-only or not
};
struct Group groups[128];
int grpIndex = 0;
struct AdminRequest
{
int grpid;
int posCount;
int negCount;
int totalAdmins;
int memberid;
bool status;
};
struct AdminRequest adminRequests[100];
int admReqIndex = 0;
// get client id from records
int getClientId(int clientfd)
{
int sender;
for (int i = 0; i < 10; i++)
{
if (clientList[i] != NULL && clientList[i]->socketfd == clientfd)
{
sender = clientList[i]->id;
return sender;
}
}
return -1;
}
// get client fd from records
int getClientFD(int clientid)
{
int sender;
for (int i = 0; i < 10; i++)
{
if (clientList[i] != NULL && clientList[i]->id == clientid)
{
sender = clientList[i]->socketfd;
return sender;
}
}
return -1;
}
// check whether a client exist or not
int isClient(int clientid)
{
for (int i = 0; i < 10; i++)
{
if (clientList[i] != NULL && clientList[i]->id == clientid)
{
return 1;
}
}
return 0;
}
// get index of group in records
int getGroupIndex(int grpid)
{
for (int i = 0; i < grpIndex; i++)
{
if (groups[i].groupid == grpid)
return i;
}
return -1;
}
// authority function
int isAdmin(int _grpindex, int adminid)
{
for (int i = 0; i < 5; i++)
{
if (groups[_grpindex].admins[i] == adminid)
return 1;
}
return 0;
}
// membership function
int isMember(int _grpindex, int clientid)
{
for (int i = 0; i < 5; i++)
{
if (groups[_grpindex].membersid[i] == clientid)
return 1;
}
return 0;
}
// request checker
int isInvited(int _grpindex, int clientid)
{
for (int i = 0; i < 4; i++)
{
// printf("Temp: %d\n",groups[_grpindex].tempMembers[i]);
if (groups[_grpindex].tempMembers[i] == clientid)
return 1;
}
return 0;
}
// check whether a group is still active or not
bool isActiveGroup(int grpid)
{
int _grpindex = getGroupIndex(grpid);
if (_grpindex == -1)
return false;
return groups[_grpindex].isCreated;
}
// function to generate 5 digit random id for client
int generateRandomId()
{
srand(time(NULL)); // Seed the random number generator with current time
int random_num = rand() % 90000 + 10000;
return random_num;
}
// function to generate 3 digit random id for group
int generateRandomGrpId()
{
srand(time(NULL)); // Seed the random number generator with current time
int random_num = rand() % 900 + 100;
return random_num;
}
void logger(char *log_message, ...)
{
va_list args;
va_start(args, log_message);
vfprintf(stderr, log_message, args);
va_end(args);
}
// send message in group
void sendGroup(char *message, int groupid, int clientfd)
{
char buffer[256];
int _grpindex = getGroupIndex(groupid);
if (_grpindex == -1)
{
bzero(buffer, 256);
sprintf(buffer, "Group does not exist!\n");
send(clientfd, buffer, sizeof(buffer), 0);
return;
}
if (groups[_grpindex].isCreated == false)
{
bzero(buffer, 256);
sprintf(buffer, "The group is not created yet! Waiting for everyone's response");
send(clientfd, buffer, sizeof(buffer), 0);
return;
}
int clientid = getClientId(clientfd);
if (isMember(_grpindex, clientid) == 0)
{
bzero(buffer, 256);
sprintf(buffer, "You are not a member of this group\n");
send(clientfd, buffer, sizeof(buffer), 0);
return;
}
if (groups[_grpindex].isBroadcast == true && isAdmin(_grpindex, clientid) == 0)
{
bzero(buffer, 256);
sprintf(buffer, "It's a admin-only group. You can't send message to this group\n");
send(clientfd, buffer, sizeof(buffer), 0);
return;
}
pid_t pid = 0;
int status;
char *arglist[] = {"python3", "exam.py", message, NULL};
pid = fork();
if (pid == 0)
{
// printf("I am the child.\\n");
execvp("python3", arglist);
perror("In exec(): ");
}
if (pid > 0)
{
// printf("I am the parent, and the child is %d.\\n", pid);
pid = wait(&status);
FILE *fptr;
fptr = fopen("res.txt", "r");
char line[100]; // buffer to hold each line
fgets(line, sizeof(line), fptr);
puts(line);
if (strncmp(line, "hate_abusive", 12) == 0)
{
char buf[512];
bzero(buf, 512);
sprintf(buf, "Sent abusive message. !!Msg locked!!");
send(clientfd, buf, sizeof(buf), 0);
printf("Message hasn't been sent to client because of abusiveness!!\n");
clientList[clientfd % 10]->abuseCount++;
if (clientList[clientfd % 10]->abuseCount >= 5)
{
removeRecClient(clientfd);
}
return;
}
// printf("End of process %d: ", pid);
if (WIFEXITED(status))
{
printf("The process ended with exit(%d).\\n", WEXITSTATUS(status));
}
if (WIFSIGNALED(status))
{
printf("The process ended with kill -%d.\\n", WTERMSIG(status));
}
}
if (pid < 0)
{
perror("In fork():");
}
int senderId = getClientId(clientfd);
for (int i = 0; i < groups[_grpindex].grpsize; i++)
{
if (groups[_grpindex].membersid[i] != senderId)
{
bzero(buffer, 256);
sprintf(buffer, "[Group %d : Sender : %d] %s", groupid, senderId, message);
send(getClientFD(groups[_grpindex].membersid[i]), buffer, sizeof(buffer), 0);
}
}
}
// function to make a normal group
void makeGroup(char *request, int adminfd)
{
char req[32];
int adminid = getClientId(adminfd);
// intializing group props
groups[grpIndex].groupid = generateRandomGrpId();
groups[grpIndex].membersid[0] = adminid;
// first admin is calling client
groups[grpIndex].admins[0] = adminid;
groups[grpIndex].adminlen = 1;
groups[grpIndex].isCreated = true;
groups[grpIndex].isBroadcast = false;
char message[256];
int argcount = sscanf(request, "%s %d %d %d %d\n", req, &groups[grpIndex].membersid[1], &groups[grpIndex].membersid[2], &groups[grpIndex].membersid[3], &groups[grpIndex].membersid[4]);
if (argcount == 1)
{
bzero(message, 256);
sprintf(message, "Please provide member ids\n");
send(adminfd, message, sizeof(message), 0);
return;
}
groups[grpIndex].grpsize = 1;
groups[grpIndex].adminlen = 1;
// incrementing group count of admin client
clientList[adminfd % 10]->joinedGroups[clientList[adminfd % 10]->grpCount++] = groups[grpIndex].groupid;
for (int i = 1; i < argcount; i++)
{
int fd = getClientFD(groups[grpIndex].membersid[i]);
if (fd != -1)
{
bzero(message, 256);
sprintf(message, "[Group %d Admin: %d] : You are added in the group.\n", groups[grpIndex].groupid, adminid);
send(fd, message, sizeof(message), 0);
// Insert group id in client object
clientList[fd % 10]->joinedGroups[clientList[fd % 10]->grpCount++] = groups[grpIndex].groupid;
// puts(message);
(groups[grpIndex].grpsize)++;
}
else
{
bzero(message, 256);
sprintf(message, "**Member with id %d does not exist**\n", groups[grpIndex].membersid[i]);
// sprintf(message, "You are added in a group %d by %d\n", groups[grpIndex].groupid, adminid);
send(fd, message, sizeof(message), 0);
}
}
bzero(message, 256);
sprintf(message, "**Group created with id %d**\n", groups[grpIndex].groupid);
send(adminfd, message, sizeof(message), 0);
grpIndex++;
}
// send request to join the group
void makeGroupReq(char *request, int adminfd)
{
char req[32];
int adminid = getClientId(adminfd);
// intializing group props
groups[grpIndex].groupid = generateRandomGrpId();
groups[grpIndex].membersid[0] = adminid;
// first admin is calling client
groups[grpIndex].admins[0] = adminid;
groups[grpIndex].isCreated = false;
groups[grpIndex].isBroadcast = false;
groups[grpIndex].reqSize = 0;
// incrementing group count of admin client
clientList[adminfd % 10]->joinedGroups[clientList[adminfd % 10]->grpCount++] = groups[grpIndex].groupid;
char message[256];
// extracting ids to whom we send join request
int argcount = sscanf(request, "/makegroupreq %d %d %d %d\n", &groups[grpIndex].tempMembers[0], &groups[grpIndex].tempMembers[1], &groups[grpIndex].tempMembers[2], &groups[grpIndex].tempMembers[3]);
if (argcount == 1)
{
bzero(message, 256);
sprintf(message, "Please provide member ids\n");
send(adminfd, message, sizeof(message), 0);
return;
}
groups[grpIndex].grpsize = 1;
groups[grpIndex].adminlen = 1;
// sending joiing request to each existing client from list
for (int i = 0; i < argcount; i++)
{
int fd = getClientFD(groups[grpIndex].tempMembers[i]);
if (fd != -1)
{
bzero(message, 256);
sprintf(message, "[%d] : Wanna join the group %d?\n", adminid, groups[grpIndex].groupid);
send(fd, message, sizeof(message), 0);
groups[grpIndex].reqSize++;
}
else
{
bzero(message, 256);
sprintf(message, "Member with id %d does not exist\n", groups[grpIndex].tempMembers[i]);
send(fd, message, sizeof(message), 0);
}
}
// confirmation to admin client
bzero(message, 256);
sprintf(message, "**Request sent to all existing members for group %d**\n", groups[grpIndex].groupid);
send(adminfd, message, sizeof(message), 0);
grpIndex++;
}
// accept the joining request to group
void joinGroup(int grpid, int clientfd)
{
int _grpindex = getGroupIndex(grpid);
char message[256];
if (_grpindex == -1)
{
bzero(message, 256);
sprintf(message, "**Group does not exist!**\n");
send(clientfd, message, sizeof(message), 0);
return;
}
int clientid = getClientId(clientfd);
if (isMember(_grpindex, clientid) == 1)
{
bzero(message, 256);
sprintf(message, "**You are already in this group!**\n");
send(clientfd, message, sizeof(message), 0);
return;
}
if (isInvited(_grpindex, clientid) == 0)
{
bzero(message, 256);
sprintf(message, "**You are not invited to this group!**\n");
send(clientfd, message, sizeof(message), 0);
return;
}
if (groups[_grpindex].grpsize == 5)
{
bzero(message, 256);
sprintf(message, "**Group is full now!**\n");
send(clientfd, message, sizeof(message), 0);
return;
}
// pushing new member to members array in group at _grpindex
groups[_grpindex].membersid[groups[_grpindex].grpsize] = clientid;
bzero(message, 256);
sprintf(message, "[Group %d] : You are added the group!\n", groups[_grpindex].groupid);
send(clientfd, message, sizeof(message), 0);
bzero(message, 256);
sprintf(message, "[Group %d] : **%d accepted the request to join the group**\n", grpid, clientid);
send(getClientFD(groups[_grpindex].admins[0]), message, sizeof(message), 0);
// broadcasting message to all members
bzero(message, 256);
sprintf(message, "[Group %d] : %d is added the group!\n", groups[_grpindex].groupid, clientid);
sendGroup(message, grpid, groups[_grpindex].admins[0]);
// Insert group id in client object
clientList[clientfd % 10]->joinedGroups[clientList[clientfd % 10]->grpCount++] = grpid;
// decrease pending request count and increment group size
groups[_grpindex].reqSize--;
groups[_grpindex].grpsize++;
// create group if all pending requests are processed
if (groups[_grpindex].reqSize == 0)
{
groups[_grpindex].isCreated = true;
}
}
// adjust pending request counts of group
void removeAndAdjustRequests(int _grpindex, int rmindex)
{
// shifting array elements by one position from removal index
for (int i = rmindex; i < 3; i++)
{
groups[_grpindex].tempMembers[i] = groups[_grpindex].tempMembers[i + 1];
}
groups[_grpindex].tempMembers[3] = 0;
// decreasing request size
groups[_grpindex].reqSize--;
}
// decline the request to join the group
void declineGroup(int grpid, int clientfd)
{
int _grpindex = getGroupIndex(grpid);
char message[256];
if (_grpindex == -1)
{
bzero(message, 256);
sprintf(message, "**Group does not exist!**\n");
send(clientfd, message, sizeof(message), 0);
return;
}
int clientid = getClientId(clientfd);
if (isInvited(_grpindex, clientid) == 0)
{
bzero(message, 256);
sprintf(message, "**You are not invited to this group!**\n");
send(clientfd, message, sizeof(message), 0);
return;
}
for (int i = 0; i < groups[_grpindex].reqSize; i++)
{
if (groups[_grpindex].tempMembers[i] == clientid)
{
// removing member and adjusting the member array
removeAndAdjustRequests(_grpindex, i);
bzero(message, 256);
sprintf(message, "[Group %d] : **%d declined the request to join the group**\n", grpid, clientid);
send(getClientFD(groups[_grpindex].admins[0]), message, sizeof(message), 0);
if (groups[_grpindex].reqSize == 0)
{
groups[_grpindex].isCreated = true;
}
break;
}
}
}
// make a client admin of the group
void addAdmin(int grpid, int clientid, int adminid)
{
printf("GRPID: %d CLIENTID: %d ADMINID: %d\n", grpid, clientid, adminid);
int _grpindex = getGroupIndex(grpid);
char message[256];
// group does not exist
if (_grpindex == -1)
{
bzero(message, 256);
sprintf(message, "Group does not exist!\n");
send(getClientFD(adminid), message, sizeof(message), 0);
return;
}
if (groups[_grpindex].isCreated == false)
{
bzero(message, 256);
sprintf(message, "The group is not created yet or deactivated\n");
send(getClientFD(adminid), message, sizeof(message), 0);
return;
}
// caller client is not an admin
if (isAdmin(_grpindex, adminid) == 0)
{
bzero(message, 256);
sprintf(message, "You are not an admin of this group!\n");
send(getClientFD(adminid), message, sizeof(message), 0);
return;
}
// client is not in the froup
if (isMember(_grpindex, clientid) == 0)
{
bzero(message, 256);
sprintf(message, "Client is not in this group!\n");
send(getClientFD(adminid), message, sizeof(message), 0);
return;
}
// clientid is already an admin
if (isAdmin(_grpindex, clientid) == 1)
{
bzero(message, 256);
sprintf(message, "Client is already an admin!\n");
send(getClientFD(adminid), message, sizeof(message), 0);
return;
}
groups[_grpindex].admins[groups[_grpindex].adminlen] = clientid;
groups[_grpindex].adminlen++;
bzero(message, 256);
sprintf(message, "[Group %d] : You are made an admin of group by %d!\n", groups[_grpindex].groupid, adminid);
send(getClientFD(clientid), message, sizeof(message), 0);
// broadcasting new admin added message
bzero(message, 256);
sprintf(message, "%d is made an admin of group\n", clientid);
sendGroup(message, grpid, getClientFD(clientid));
}
// add a new memeber to group
void addMember(char *request, int adminfd)
{
int grpid;
int clientArgs[3];
char message[256];
// splitting request
int argCount = sscanf(request, "/addtogroup %d %d %d %d\n", &grpid, &clientArgs[0], &clientArgs[1], &clientArgs[2]);
// no member id provided
if (argCount == 1)
{
bzero(message, 256);
sprintf(message, "Provide at least one member id\n");
send(adminfd, message, sizeof(message), 0);
return;
}
// getting groupindex
int _grpindex = getGroupIndex(grpid);
// group does not exist
if (_grpindex == -1)
{
bzero(message, 256);
sprintf(message, "Group does not exist!\n");
send(adminfd, message, sizeof(message), 0);
return;
}
// caller client is not an admin
if (isAdmin(_grpindex, getClientId(adminfd)) == 0)
{
bzero(message, 256);
sprintf(message, "You are not an admin of this group!\n");
send(adminfd, message, sizeof(message), 0);
return;
}
if (groups[_grpindex].isCreated == false)
{
bzero(message, 256);
sprintf(message, "The group is not created yet! Waiting for everyone's response.\n");
send(adminfd, message, sizeof(message), 0);
return;
}
int i = 0;
// inserting one by one into group
while (--argCount)
{
// group is full
if (groups[_grpindex].grpsize == 5)
{
bzero(message, 256);
sprintf(message, "Group is already full! Can't add more member\n");
send(adminfd, message, sizeof(message), 0);
return;
}
// member does not exist in our records
if (isClient(clientArgs[i]) == 0)
{
bzero(message, 256);
sprintf(message, "%d does not exist\n", clientArgs[i++]);
send(adminfd, message, sizeof(message), 0);
continue;
}
// already in group
if (isMember(_grpindex, clientArgs[i]) == 1)
{
bzero(message, 256);
sprintf(message, "%d is already in the group\n", clientArgs[i++]);
send(adminfd, message, sizeof(message), 0);
continue;
}
// pushing new member to members array in group at _grpindex
groups[_grpindex].membersid[groups[_grpindex].grpsize] = clientArgs[i];
bzero(message, 256);
sprintf(message, "[Group %d] : You are added to group!\n", groups[_grpindex].groupid);
send(getClientFD(clientArgs[i]), message, sizeof(message), 0);
// send(adminfd, message, sizeof(message), 0);
int fd = getClientFD(clientArgs[i]);
// Insert group id in client object
clientList[fd % 10]->joinedGroups[(clientList[fd % 10]->grpCount)++] = grpid;
// increase groupsize
groups[_grpindex].grpsize++;
// sending success message to all members
bzero(message, 256);
sprintf(message, "[Group %d] : %d is added to group!\n", groups[_grpindex].groupid, clientArgs[i]);
sendGroup(message, grpid, clientArgs[i]);
i++;
}
}
// function to remove from admins
void removeFromAdmins(int _grpindex, int adminid)
{
int rmindex;
for (int i = 0; i < 5; i++)
{
if (adminid == groups[_grpindex].admins[i])
{
rmindex = i;
break;
}
}
for (int i = rmindex; i < 4; i++)
{
groups[_grpindex].admins[i] = groups[_grpindex].admins[i + 1];
}
groups[_grpindex].admins[4] = 0;
groups[_grpindex].adminlen--;
}
// adjust indexes of members in group after removing a client
void removeAndAdjustGroupMembers(int _grpindex, int rmindex)
{
// remove from admin also if he is an admin
if (isAdmin(_grpindex, groups[_grpindex].membersid[rmindex]) == 1)
{
removeFromAdmins(_grpindex, groups[_grpindex].membersid[rmindex]);
}
// shifting array elements by one position from removal index
for (int i = rmindex; i < 4; i++)
{
groups[_grpindex].membersid[i] = groups[_grpindex].membersid[i + 1];
}
groups[_grpindex].membersid[4] = 0;
// decreasing group size
groups[_grpindex].grpsize--;
}
// remove a member from group
void removeMember(char *request, int adminfd)
{
int grpid;
int clientArgs[4];
char message[256];
// splitting request
int argCount = sscanf(request, "/removefromgroup %d %d %d %d %d\n", &grpid, &clientArgs[0], &clientArgs[1], &clientArgs[2], &clientArgs[3]);
// no member id provided
if (argCount == 1)
{
bzero(message, 256);
sprintf(message, "Provide at least one member id\n");
send(adminfd, message, sizeof(message), 0);
return;
}
// getting groupindex
int _grpindex = getGroupIndex(grpid);
// group does not exist
if (_grpindex == -1)
{
bzero(message, 256);
sprintf(message, "Group does not exist!\n");
send(adminfd, message, sizeof(message), 0);
return;
}
// caller client is not an admin
if (isAdmin(_grpindex, getClientId(adminfd)) == 0)
{
bzero(message, 256);
sprintf(message, "You are not an admin of this group!\n");
send(adminfd, message, sizeof(message), 0);
return;
}
if (groups[_grpindex].isCreated == false)
{
bzero(message, 256);
sprintf(message, "The group is not created yet! Waiting for everyone's response.\n");
send(adminfd, message, sizeof(message), 0);
return;
}
int j = 0;
// inserting one by one into group
while (--argCount)
{
// member does not exist in our records
if (isClient(clientArgs[j]) == 0)
{
bzero(message, 256);
sprintf(message, "%d does not exist\n", clientArgs[j++]);
send(adminfd, message, sizeof(message), 0);
continue;
}
for (int i = 0; i < 5; i++)
{
if (groups[_grpindex].membersid[i] == clientArgs[j])
{
// removing member and adjusting the member array
removeAndAdjustGroupMembers(_grpindex, i);
bzero(message, 256);
sprintf(message, "[Group %d] : You are removed from the group\n", grpid);
send(getClientFD(clientArgs[j]), message, sizeof(message), 0);
bzero(message, 256);
sprintf(message, "[Group %d] : %d is removed from the group\n", grpid, clientArgs[j]);
sendGroup(message, grpid, getClientId(adminfd));
}
}
}
}
// change the group to admin-only group
void makeGroupBroadcast(int grpid, int clientfd)
{
int _grpindex = getGroupIndex(grpid);
char message[256];
// group does not exist
if (_grpindex == -1)
{
bzero(message, 256);
sprintf(message, "Group does not exist!\n");
send(clientfd, message, sizeof(message), 0);
return;
}
// caller client is not an admin
if (isAdmin(_grpindex, getClientId(clientfd)) == 0)
{
bzero(message, 256);
sprintf(message, "You are not an admin of this group!\n");
send(clientfd, message, sizeof(message), 0);
return;
}
if (groups[_grpindex].isCreated == false)
{
bzero(message, 256);
sprintf(message, "The group is not created yet! Waiting for everyone's response.\n");
send(clientfd, message, sizeof(message), 0);
return;
}
groups[_grpindex].isBroadcast = true;
sendGroup("**This group is now admin-only!**", grpid, clientfd);
}
// remove client from all groups and records
void removeClientFromAllGroups(int clientid)
{
// fetch client fd and index
int clientfd = getClientFD(clientid);
int clientindex = clientfd % 10;
char message[256];
// get number of groups client has joined
int _grpcount = clientList[clientindex]->grpCount;
printf("Grp count: %d\n", _grpcount);
for (int gindex = 0; gindex < _grpcount; gindex++)
{
// take up group one by one and remove this client
int grpid = clientList[clientindex]->joinedGroups[gindex];
int _grpindex = getGroupIndex(grpid);
if (_grpindex == -1)
continue;
if (groups[_grpindex].isCreated == false)
continue;
for (int i = 0; i < 5; i++)
{
if (groups[_grpindex].membersid[i] == clientid)
{
// removing member and adjusting the member array
removeAndAdjustGroupMembers(_grpindex, i);
bzero(message, 256);
sprintf(message, "**%d left the group %d.**", clientid, grpid);
// notify other members about him
sendGroup(message, grpid, clientfd);
// if all are removed then close the group
if (groups[_grpindex].grpsize == 0 || groups[_grpindex].adminlen == 0)
{
groups[_grpindex].isCreated = false;
printf(">> All admins of group %d has left. Group has been deactivated!\n", groups[_grpindex].groupid);
}
break;
}
}
}
}
// return all groups, a client is member of
void activeGroups(int clientfd)
{
int _clientindex = clientfd % 10;
char message[256];
int grpcount = clientList[_clientindex]->grpCount;
bool flag = false;
if (grpcount == 0)
{
bzero(message, 256);
sprintf(message, "No groups found!");
send(clientfd, message, sizeof(message), 0);
return;
}
for (int i = 0; i < grpcount; i++)
{
if (isActiveGroup(clientList[_clientindex]->joinedGroups[i]) == true)
{
flag = true;
// getting group details
int grpid = clientList[_clientindex]->joinedGroups[i];
int _grpindex = getGroupIndex(grpid);
// send group id
bzero(message, 256);
sprintf(message, "======= Group ID : %d ========", grpid);
send(clientfd, message, sizeof(message), 0);
// get members list
char members[256];
bzero(members, 256);
char temp[8];
bzero(message, 256);
sprintf(message, "Members:");
send(clientfd, message, sizeof(message), 0);
for (int m = 0; m < groups[_grpindex].grpsize; m++)
{
bzero(temp, 8);
sprintf(temp, " %d ", groups[_grpindex].membersid[m]);
strcat(members, temp);
}
// send member list
send(clientfd, members, sizeof(members), 0);
// puts(members);
// get admins list
char admins[256];
bzero(admins, 256);
bzero(message, 256);
sprintf(message, "Admins:");
send(clientfd, message, sizeof(message), 0);
for (int m = 0; m < groups[_grpindex].adminlen; m++)
{
bzero(temp, 8);
sprintf(temp, " %d ", groups[_grpindex].admins[m]);
strcat(admins, temp);
}
// send admin list
send(clientfd, admins, sizeof(admins), 0);
// puts(admins);
}
}
if (flag == false)
{
bzero(message, 256);
sprintf(message, "No active groups found!");
send(clientfd, message, sizeof(message), 0);
}
}
// send admin request from client to all admins
void adminRequest(int grpid, int clientid)
{
int _grpindex = getGroupIndex(grpid);
char message[256];
// group does not exist
if (_grpindex == -1)
{
bzero(message, 256);
sprintf(message, "Group does not exist!\n");
send(getClientFD(clientid), message, sizeof(message), 0);
return;
}
if (groups[_grpindex].isCreated == false)
{
bzero(message, 256);
sprintf(message, "The group is not created yet! Waiting for everyone's response.\n");
send(getClientFD(clientid), message, sizeof(message), 0);
return;
}
// client is not in the froup
if (isMember(_grpindex, clientid) == 0)
{
bzero(message, 256);
sprintf(message, "Client is not in this group!\n");
send(getClientFD(clientid), message, sizeof(message), 0);
return;
}
// clientid is already an admin
if (isAdmin(_grpindex, clientid) == 1)
{
bzero(message, 256);
sprintf(message, "Client is already an admin!\n");
send(getClientFD(clientid), message, sizeof(message), 0);
return;
}
// adding admin request in records to track it anytime
adminRequests[admReqIndex].grpid = grpid;
adminRequests[admReqIndex].memberid = clientid;
adminRequests[admReqIndex].status = false;
adminRequests[admReqIndex].posCount = 0;
adminRequests[admReqIndex].negCount = 0;
adminRequests[admReqIndex].totalAdmins = groups[_grpindex].adminlen;
// notifying admins about admin request from a member
for (int i = 0; i < groups[_grpindex].adminlen; i++)
{
bzero(message, 256);
sprintf(message, "[Group %d] : %d wants to be an admin!\n", grpid, clientid);
send(getClientFD(groups[_grpindex].admins[i]), message, sizeof(message), 0);
}
admReqIndex++;
}
int getAdminRequestIndex(int grpid, int clientid)
{
for (int i = 0; i < admReqIndex; i++)
{
if (adminRequests[i].grpid == grpid && adminRequests[i].memberid == clientid)
return i;
}
return -1;
}
// approve admin request of a client
void approveAdminReq(int grpid, int clientid, int adminid)
{
// get adminrequest object from records
int _reqindex = getAdminRequestIndex(grpid, clientid);
char message[256];
if (_reqindex == -1 || adminRequests[_reqindex].status == true)
{