-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.c
135 lines (124 loc) · 3.59 KB
/
Utils.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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#define CONFIG_FILE "users.txt"
#define MAX_LINE 500
#define MAX_CONTENT 10000
#define EMAIL_CONTENT 4000
#define ATTACH_CONTENT 5000
#define SMALL_STRING 50
#define MAX_STRING 250
//#define CONFIG_FILE "/Users/chinnababusadam/Documents/UHCL/Test Function/Test Function/users.txt"
int CheckIfUserExist(char *userName) {
FILE *fp;
char _userName[SMALL_STRING];
char readLine[MAX_LINE];
fp = fopen(CONFIG_FILE, "r+");
if (fp) {
while (fgets(readLine, MAX_LINE, fp) != NULL) {
//printf("%s",readLine);
printf("Reach here\n");
sscanf(readLine, "%[^;]", _userName);
if (strcmp(userName, _userName) == 0) {
fclose(fp);
return 1;
}
}
fclose(fp);
return 0;
}
return 0;
}
char* IsClientAuthenticated(char *userName, char *password) {
FILE *fp;
fp = fopen(CONFIG_FILE, "r+");
char _userName[SMALL_STRING];
char _password[SMALL_STRING];
char _firstName[SMALL_STRING];
char _lastName[SMALL_STRING];
char readLine[MAX_LINE];
char *returnMessage = malloc(sizeof(char) * MAX_LINE);
while (fgets(readLine, MAX_LINE, fp) != NULL) {
//printf("%s",readLine);
printf("Reach here\n");
sscanf(readLine, "%[^;];%[^;];%[^;];%[^;]", _userName, _password,
_firstName, _lastName);
printf("Line - user: %s , pass: %s , first: %s, last: %s\n", _userName,
_password, _firstName, _lastName);
if ((strcmp(_userName, userName) == 0)
&& strcmp(password, _password) == 0) {
fclose(fp);
//snprintf(returnMessage, MAX_LINE, "%d:%s:%s", 1,_firstName,_lastName);
sprintf(returnMessage, "%d:%s:%s", 1, _firstName, _lastName);
return returnMessage;
}
}
fclose(fp);
return "NONE";
}
int ClientRegistration(char *userName, char *pwd, char *firstName,
char *lastName) {
printf("Server GOT: %s;%s;%s;%s\n", userName, pwd, firstName, lastName);
if (!CheckIfUserExist(userName)) {
char fileName[50];
//snprintf(fileName,100,"%s.txt",userName);
sprintf(fileName, "%s.txt", userName);
printf("File Name = %s\n", fileName);
FILE *file = fopen(CONFIG_FILE, "ab+");
if (file) {
printf("Before save: %s;%s;%s;%s;%s\n", userName, pwd, firstName,
lastName, fileName);
fprintf(file, "%s;%s;%s;%s;%s\n", userName, pwd, firstName,
lastName, fileName);
fclose(file);
FILE *clientFile = fopen(fileName, "ab+");
if (clientFile) {
fclose(clientFile);
return 1;
} else {
fclose(clientFile);
return 0;
}
} else {
fclose(file);
return 0;
}
} else {
return 0;
}
}
void doProcessEmailMessage(char *messageContent) {
char subject[SMALL_STRING];
char from[SMALL_STRING];
char senderEMail[SMALL_STRING];
//char firstName[SMALL_STRING];
//char lastName[SMALL_STRING];
char toName[SMALL_STRING];
char sendTime[SMALL_STRING];
char attachment[ATTACH_CONTENT];
char emailContent[EMAIL_CONTENT];
int count;
printf("%s", messageContent);
count =
sscanf(messageContent,
"%s EmailSubject: %[^#]#EmailContent: %[^#]#Attachment: %[^#]#ToName: %[^#]#SendTime: %[^#]#FromName: %[^'\0']",
senderEMail, subject, emailContent, attachment, toName,
sendTime, from);
printf("Count: %d\n", count);
printf("%s\n", subject);
printf("%s\n", emailContent);
printf("%s\n", attachment);
printf("%s\n", toName);
printf("%s\n", sendTime);
printf("%s\n", from);
FILE *file = fopen(strcat(toName, ".txt"), "a+");
if (file) {
fprintf(file,
"~Un-Read\nFrom: %s\nSender Email: %s\nDate: %s\nSubject: %s\nContent: %s\nAttachment: %s\n#END\n",
from, senderEMail, sendTime, subject, emailContent, attachment);
fclose(file);
} else {
printf("Cannot find file %s", toName);
}
}