-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests-pop.c
132 lines (110 loc) · 3.4 KB
/
requests-pop.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
#include "pop.h"
int PopUser(char* requete, int desc, pop* response)
{
if (verifieSyntaxe(requete, "USER") != 0) return -1;
envoieServeur(requete, desc);
//lecture Reponse
FILE* fdesc = fdopen(desc, "r");
char reponse[LINELENGTH];
fflush(NULL);
if (reponsePositive(fdesc, &reponse[0]) != 0) {
return -1;
}
printf("%s\n", reponse);
return 0;
}
int PopPass(char* requete, int desc, pop* response)
{
if (verifieSyntaxe(requete, "PASS") != 0) return -1;
envoieServeur(requete, desc);
//lecture Reponse
FILE* fdesc = fdopen(desc, "r");
char reponse[LINELENGTH];
if (reponsePositive(fdesc, reponse) != 0) return -1;
printf("%s\n", reponse);
return 0;
}
int PopQuit(char* requete, int desc, pop* response)
{
if (verifieSyntaxe(requete, "QUIT") != 0) return -1;
envoieServeur(requete, desc);
//lecture Reponse
FILE* fdesc = fdopen(desc, "r");
char reponse[LINELENGTH];
if (reponsePositive(fdesc, reponse) != 0) return -1;
printf("%s", reponse);
//correcte sortie du programme
printf("Vous avez choisi de Quitter l'application...\n");
printf("Merci pour avoir utiliser notre Client POP.\n");
exit(EXIT_SUCCESS);
}
int PopMuet(char* requete, int desc, pop* response)
{
return -1;
}
int PopTop(char* requete, int desc, pop* response)
{
if (verifieSyntaxe(requete, "TOP") != 0) return -1;
envoieServeur(requete, desc);
//lecture 1ere ligne
FILE* fdesc = fdopen(desc, "r");
char reponse[LINELENGTH];
if (reponsePositive(fdesc, reponse) != 0) return -1;
printf("%s", reponse);
//recuperation id message
int mexId = 0;
if (!sscanf(requete, "TOP %d 0\n", &mexId)) {
printf("Erreur recuperation ID\n");
return -1;
}
//printf("MEX ID inserted: %d\n", mexId);
//if(mexId <= 0) return -1;
AnalyseEntetes(requete, mexId, fdesc, response);
//sortie avec erreur
return -1;
}
int PopRetr(char* requete, int desc, pop* response)
{
if (verifieSyntaxe(requete, "RETR") != 0) return -1;
envoieServeur(requete, desc);
//lecture 1ere ligne
FILE* fdesc = fdopen(desc, "r");
char reponse[LINELENGTH];
if (reponsePositive(fdesc, &reponse[0]) != 0) return -1;
printf("%s", reponse);
//recuperation id message
int mexId = 0;
if (!sscanf(requete, "RETR %d\n", &mexId)) {
printf("Erreur recuperation ID\n");
return -1;
}
//if(mexId <= 0) return -1;
AnalyseEntetes(requete, mexId, fdesc, response);
return 0;
}
int PopList(char* requete, int desc, pop* response)
{
if (verifieSyntaxe(requete, "LIST") != 0) return -1;
envoieServeur(requete, desc);
//lecture 1ere ligne
FILE* fdesc = fdopen(desc, "r");
char reponse[LINELENGTH];
if (reponsePositive(fdesc, reponse) != 0) return -1;
printf("%s", reponse);
cleanPop(response);
//lecture corps reponse
while (fgets(reponse, LINELENGTH, fdesc)) {
printf("%s", reponse);
//condition d'arret
if (!strncmp(".", reponse, strlen("."))) {
return 0;
}
//creation et remplissage du message, ajout à la liste
message* mex = (message*)malloc(sizeof(message));
sscanf(reponse, "%d %d\n", &mex->id, &mex->taille);
//printf("mex id : %d\nmex taille : %d\n", mex->id, mex->taille);
addMessage(response, mex);
}
//sortie avec erreur
return -1;
}