-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailclient.c
637 lines (517 loc) · 19.8 KB
/
mailclient.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define MAXLINE 100
// Function to list the mails in the given fomat in a temprory file
void listing(){
FILE * list = fopen("list.txt", "w");
FILE * temp = fopen("temp.txt", "r");
char line[256];
int sno = 0;
char received[100];
char emailid[100];
char subject[100];
char tosend[256];
while(fgets(line, sizeof(line), temp)){
char *colon_pos = strstr(line, ":");
char *start = colon_pos + 1;
if(colon_pos != NULL){
char *start = colon_pos + 1;
while(*start == ' '){
start++;
}
if(strstr(line, "From:")!= NULL){
sscanf(start, "%[^\n]", emailid);
emailid[strlen(emailid) - 1] = '\0';
}
else if(strstr(line, "Recieved:")!= NULL){
sscanf(start, "%[^\n]", received);
received[strlen(received) - 1] = '\0';
sno++;
fprintf(list, "%d %s %s %s\n", sno, emailid, received, subject);
}
else if(strstr(line, "Subject:")!= NULL){
sscanf(start, "%[^\n]", subject);
subject[strlen(subject) - 1] = '\0';
}
}
}
fclose(temp);
remove("temp.txt");
fclose(list);
return;
}
// Function to check if the given string is in the format of "xxxxxxxx@xxxxxx"
int check_xy(char str[], char *username){
int cnt = 0, space = 0;
for(int i = 0; i<strlen(str); i++){
if(str[i] == '@'){
cnt++;
}
if(str[i] == ' '){
space++;
}
}
if(cnt != 1 || space != 0){
return 0;
}
char *x = strtok(str, "@");
char *y = strtok(NULL, "@");
if(strlen(x) == 0 || strlen(y) == 0){
return 0;
}
if(strcmp(x,username) != 0){
return 0;
}
return 1;
}
// Function to replace '\n' with '\r\n' in the given buffer
void replace(char *buffer){
size_t len = strlen(buffer);
if(buffer[len-1] == '\n'){
buffer[len-1] = '\r';
buffer[len] = '\n';
buffer[len+1] = '\0';
}else{
buffer[len] = '\r';
buffer[len+1] = '\n';
buffer[len+2] = '\0';
}
return;
}
int main(int argc, char *argv[]){
if(argc != 4){
printf("run as: ./mailclient <server_ip> <smtp_port> <pop3_port>\n");
return 0;
}
char *server_ip = argv[1];
int smtp_port = atoi(argv[2]);
int pop3_port = atoi(argv[3]);
char user[MAXLINE], pass[MAXLINE];
printf("Enter username: ");
scanf("%s", user);
printf("Enter password: ");
scanf("%s", pass);
while(1){
int choice;
printf("Choose from the following:\n");
printf("1. Manage Mail\n");
printf("2. Send Mail\n");
printf("3. Quit\n");
printf("Choice = ");
scanf("%d", &choice);
// If use wants to manage mail
if(choice == 1){
int sockfd;
struct sockaddr_in server_addr;
//Creating socket
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
perror("Error in creating socket\n");
exit(0);
}
server_addr.sin_family = AF_INET;
inet_aton(server_ip, &server_addr.sin_addr);
server_addr.sin_port = htons(pop3_port);
//Connecting to server
if((connect(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr))) < 0){
perror("Unable to connect to server\n");
exit(0);
}
char buffer[MAXLINE];
memset(buffer, 0, MAXLINE);
ssize_t len = recv(sockfd, buffer, MAXLINE, 0);
//printf("S : %s", buffer);
if(strncmp(buffer, "+OK", 3) != 0){
printf("Error in connecting to POP3 server\n");
close(sockfd);
continue;
}
//Sending username and password
memset(buffer, 0, MAXLINE);
strcpy(buffer, "USER ");
strcat(buffer, user);
strcat(buffer, "\r\n");
send(sockfd, buffer, strlen(buffer), 0);
memset(buffer, 0, MAXLINE);
len = recv(sockfd, buffer, MAXLINE, 0);
printf("S : %s", buffer);
if(strncmp(buffer, "-ERR", 4) == 0){
printf("Invalid user\n");
close(sockfd);
return 0;
}
memset(buffer, 0, MAXLINE);
strcpy(buffer, "PASS ");
strcat(buffer, pass);
strcat(buffer, "\r\n");
send(sockfd, buffer, strlen(buffer), 0);
memset(buffer, 0, MAXLINE);
len = recv(sockfd, buffer, MAXLINE, 0);
//Checking password
if(strncmp(buffer, "-ERR", 4) == 0){
printf("Invalid password\n");
close(sockfd);
return 0;
}
//Using command STAT to get the number of mails
memset(buffer, 0, MAXLINE);
strcpy(buffer, "STAT\r\n");
send(sockfd, buffer, strlen(buffer), 0);
memset(buffer, 0, MAXLINE);
len = recv(sockfd, buffer, MAXLINE, 0);
//printf("S : %s", buffer);
int n;
char temp[MAXLINE];
int i = 3;
int z = 0;
for(i=0; i<strlen(buffer); i++){
if(buffer[i] == ' '){
i++;
while(buffer[i] != ' '){
temp[z++] = buffer[i++];
}
break;
}
}
// printf("temp = %s\n", temp);
// strcat(temp, "\0");
n = atoi(temp);
int deleted[n];
for(int i = 0; i<n; i++){
deleted[i] = 0;
}
printf("\nYou have %d mails\n\n", n);
//Using command RETF to recieve all mails to create list in the given format
memset(buffer, 0, MAXLINE);
FILE *file = fopen("temp.txt", "w");
for(int i = 0; i< n; i++){
if(deleted[i] == 1){
continue;
}
char tempchar[10];
memset(buffer, 0, MAXLINE);
strcpy(buffer, "RETR ");
sprintf(tempchar,"%d", i+1);
strcat(buffer, tempchar);
strcat(buffer, "\r\n");
send(sockfd, buffer, strlen(buffer), 0);
memset(buffer, 0, MAXLINE);
len = recv(sockfd, buffer, MAXLINE, 0);
if(strncmp(buffer, "-ERR", 4) == 0){
printf("Error in retrieving mail\n");
remove("list.txt");
close(sockfd);
continue;
}
while(1){
memset(buffer, 0, MAXLINE);
len = recv(sockfd, buffer, MAXLINE, 0);
for(int j = 0; j<len ; j++){
if(buffer[j] == '.' && (j+1 < len) && buffer[j+1] == '\r'&& (j+2 < len) && buffer[j+2] == '\n'){
fputs(buffer, file);
goto next3;
}
else if(buffer[j] == '\r' && (j+1)<len && buffer[j+1] == '\n' && j+2 < len &&buffer[j+2] == '.'){
fputs(buffer, file);
goto next3;
}
else if(buffer[j] == '\n' && (j+1)<len && buffer[j+1] == '.'){
fputs(buffer, file);
goto next3;
}
}
fputs(buffer, file);
}
next3:
}
fclose(file);
//Creating list of mails
listing();
//
int input;
int delete[n+1];
for(int i = 1; i<=n; i++){
delete[i] = 0;
}
while(1){
FILE *list = fopen("list.txt", "r");
char line[256];
for(int i = 1; i<=n ; i++){
fgets(line,sizeof(line), list);
if(delete[i] == 1){
continue;
}
//memset(line, 256, 0);
printf("%s", line);
}
fclose(list);
printf("\nEnter the mail no. to see: ");
scanf("%d", &input);
// if input is -1 then quit
if(input == -1){
memset(buffer, 0, MAXLINE);
strcpy(buffer, "QUIT\r\n");
send(sockfd, buffer, strlen(buffer), 0);
break;
}
// Check for valid input
while(input < 1 || input > n){
printf("Invalid input\n");
printf("\nEnter the mail no. to see: ");
scanf("%d", &input);
if(input > 0 && input <=n){
break;
}
}
// Using command RETR to recieve the mail body
memset(buffer, 0, MAXLINE);
strcpy(buffer, "RETR ");
memset(temp, 0, MAXLINE);
sprintf(temp, "%d", input);
strcat(buffer, temp);
strcat(buffer, "\r\n");
send(sockfd, buffer, strlen(buffer), 0);
memset(buffer, 0, MAXLINE);
len = recv(sockfd, buffer, MAXLINE, 0);
printf("S : %s", buffer);
if(strncmp(buffer, "-ERR", 4) == 0){
printf("Error in retrieving mail\n");
continue;
}
// Recieveing mail body
while(1){
memset(buffer, 0, MAXLINE);
len = recv(sockfd, buffer, MAXLINE, 0);
for(int j = 0; j<len ; j++){
if(buffer[j] == '.' && (j+1 < len) && buffer[j+1] == '\r'&& (j+2 < len) && buffer[j+2] == '\n'){
memset(temp, 0, MAXLINE);
strncpy(temp, buffer, j);
printf("%s\n", buffer);
goto next1;
}
else if(buffer[j] == '\r' && (j+1)<len && buffer[j+1] == '\n' && j+2 < len &&buffer[j+2] == '.'){
memset(temp, 0, MAXLINE);
strncpy(temp, buffer, j);
printf("%s\n", buffer);
goto next1;
}
else if(buffer[j] == '\n' && (j+1)<len && buffer[j+1] == '.'){
memset(temp, 0, MAXLINE);
strncpy(temp, buffer, j);
printf("%s\n", buffer);
goto next1;
}
}
printf("%s", buffer);
}
next1:
printf("Enter character 'd' to delete above mail\n");
while((getchar()) != '\n');
char inputchar;
inputchar = getchar();
if(inputchar == 'd'){
memset(buffer, 0, MAXLINE);
//Using command DELE to delete the mail
strcpy(buffer, "DELE ");
memset(temp, 0, MAXLINE);
sprintf(temp, "%d", input);
strcat(buffer, temp);
strcat(buffer, "\r\n");
send(sockfd, buffer, strlen(buffer), 0);
memset(buffer, 0, MAXLINE);
len = recv(sockfd, buffer, MAXLINE, 0);
//printf("%s", buffer);
if(strncmp(buffer, "-ERR", 4) == 0){
printf("Error in deleting mail\n");
remove("list.txt");
close(sockfd);
continue;
}
if(strncmp(buffer, "+OK", 3) == 0){
printf("Mail deleted\n");
//printf("input = %d\n", input);
delete[input] = 1;
}
}
}
if(input == -1){
close(sockfd);
remove("list.txt");
continue;
}
}
//If user wants to send mail
else if(choice == 2){
int sockfd;
struct sockaddr_in server_addr;
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
perror("Error in creating socket\n");
exit(0);
}
server_addr.sin_family = AF_INET;
inet_aton(server_ip, &server_addr.sin_addr);
server_addr.sin_port = htons(smtp_port);
if((connect(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr))) < 0){
perror("Unable to connect to server\n");
exit(0);
}
char from[MAXLINE], to[MAXLINE], subject[MAXLINE], body[50][MAXLINE];
printf("****Type the mail****\n");
fflush(stdout);
fgets(body[0], MAXLINE,stdin);
fgets(from, MAXLINE,stdin);
fgets(to, MAXLINE,stdin);
fgets(subject, MAXLINE,stdin);
int i = -1;
do{
i++;
fgets(body[i], MAXLINE,stdin);
}while(strcmp(body[i], ".\n") != 0);
//checking the format of the mail
char domain[MAXLINE];
for(int j = 0; j< MAXLINE; j++){
if(from[j] == '@'){
int k;
for(k = j+1; k< strlen(from); k++){
if(from[k] == '\n'){
domain[k-j-1] = '\0';
break;
}
domain[k-j-1] = from[k];
}
if(domain[k - j -1] == '\0'){
break;
}
}
}
if(strcmp(domain, server_ip) != 0){
printf("Domain of the sender should be the ip address fo the server\n");
continue;
}
//Checking the format of from, to and subject
if((strncmp(from, "From:", 5) != 0) || (strncmp(to, "To:", 3) != 0) || (strncmp(subject, "Subject:", 8) != 0)){
printf("Incorrect format\n");
continue;
}
int pos = 0;
char from_username[MAXLINE], to_username[MAXLINE];
for(int j = 6; j<strlen(from); j++){
if(from[j] != ' '){
pos = j;
break;
}
}
strncpy(from_username, from+pos, strlen(from)-pos-1);
if(!check_xy(from_username,user)){
printf("Incorrect format\n");
continue;
}
pos = 0;
for(int j = 3; j<strlen(to); j++){
if(to[j] != ' '){
pos = j;
break;
}
}
memset(to_username, 0, MAXLINE);
strncpy(to_username, to+pos, strlen(to)-pos-1);
if(!check_xy(to_username,to_username)){
printf("Incorrect format\n");
continue;
}
char buffer[MAXLINE];
memset(buffer, 0, MAXLINE);
ssize_t len = recv(sockfd, buffer, MAXLINE, 0);
// Send command HELO to server to identity
memset(buffer, 0, MAXLINE);
strcpy(buffer, "HELO ");
strcat(buffer, server_ip);
strcat(buffer, "\r\n");
send(sockfd, buffer, strlen(buffer), 0);
memset(buffer, 0, MAXLINE);
len = recv(sockfd, buffer, MAXLINE, 0);
memset(buffer, 0, MAXLINE);
// Send command MAIL to server to verify senders mail
strcpy(buffer, "MAIL ");
strcat(buffer, "FROM: <");
char cleaned_from[MAXLINE];
memset(cleaned_from, 0, MAXLINE);
strcat(cleaned_from, user);
strcat(cleaned_from, "@");
strcat(cleaned_from, server_ip);
strcat(buffer, cleaned_from);
strcat(buffer, ">");
strcat(buffer, "\r\n");
send(sockfd, buffer, strlen(buffer), 0);
memset(buffer, 0, MAXLINE);
len = recv(sockfd, buffer, MAXLINE, 0);
//Sending command RCPT to server to verify recievers mail
memset(buffer, 0, MAXLINE);
strcpy(buffer, "RCPT TO: ");
char cleaned_to[MAXLINE];
memset(cleaned_to, 0, MAXLINE);
strcat(cleaned_to,"<");
strcat(cleaned_to, to_username);
strcat(cleaned_to, "@");
strcat(cleaned_to, server_ip);
strcat(cleaned_to, ">");
strcat(buffer, cleaned_to);
strcat(buffer, "\r\n");
send(sockfd, buffer, strlen(buffer), 0);
memset(buffer, 0, MAXLINE);
len = recv(sockfd, buffer, MAXLINE, 0);
char temp[4];
strncpy(temp, buffer,3);
int code = atoi(temp);
if(code == 550){
printf("Error in sending mail: %s\n", buffer);
close(sockfd);
continue;
}else if(code == 250){
memset(buffer, 0, MAXLINE);
//Sending command DATA to server to send the mail
strcpy(buffer, "DATA\r\n");
send(sockfd, buffer, strlen(buffer), 0);
memset(buffer, 0, MAXLINE);
len = recv(sockfd, buffer, MAXLINE, 0);
replace(from);
send(sockfd, from, strlen(from), 0);
replace(to);
send(sockfd, to, strlen(to), 0);
replace(subject);
send(sockfd, subject, strlen(subject), 0);
for(int j = 0; j<=i; j++){
memset(buffer, 0, MAXLINE);
strcpy(buffer, body[j]);
for(int j = 0; j<MAXLINE; j++){
if(buffer[j] == '\n'){
buffer[j] = '\r';
buffer[j+1] = '\n';
break;
}
}
send(sockfd, buffer, strlen(buffer), 0);
}
memset(buffer, 0, MAXLINE);
len = recv(sockfd, buffer, MAXLINE, 0);
memset(buffer, 0, MAXLINE);
//Sending command QUIT to server to quit
strcpy(buffer, "QUIT\r\n");
send(sockfd, buffer, strlen(buffer), 0);
memset(buffer, 0, MAXLINE);
len = recv(sockfd, buffer, MAXLINE, 0);
close(sockfd);
printf("Mail sent successfully\n");
}
}else if(choice == 3){
break;
}
}
return 0;
}