Skip to content

Commit 13ccdcb

Browse files
author
PascalHarris
authored
Correction to delete email code
Email files are now correctly deleted from the server when the client quits
1 parent cf5ffac commit 13ccdcb

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

MailServerGit/netgate_tools.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,37 @@ string getMessageID(string email) {
5151
return returnValue;
5252
}
5353

54+
bool filePresent(POPListing &poplist, const char* filepath) {
55+
for (int i=0;i<poplist.POPEntry.size();i++) {
56+
if (strncmp(poplist.POPEntry[i].messagePath,filepath,strlen(filepath))==0) {
57+
return YES;
58+
}
59+
}
60+
return NO;
61+
}
62+
5463
//--------------------------------POP Tools
5564
int getList(const char* mailDirectory, POPListing &poplist) {
56-
POPListing returnInfo;
5765
DIR *dir;
5866
struct dirent *ent;
5967
int count = 0, cumulative_size=0;
6068
if ((dir = opendir (mailDirectory)) != NULL) {
61-
// print all the files and directories within directory
69+
// print all the files and directories within directory
6270
while ((ent = readdir (dir)) != NULL) {
6371
if ((strncmp(ent->d_name, ".", 1) != 0)) {
64-
poplist.POPEntry.push_back(POPEnt()); //create new entry
6572
char filepath[MAXPATHLENGTH];
6673
strcpy(filepath, mailDirectory);
6774
strcat(filepath, "/");
6875
strcat(filepath, ent->d_name);
6976

70-
strcpy(poplist.POPEntry[count].messagePath, filepath);
71-
struct stat stat_buf;
72-
int rc = stat(filepath, &stat_buf);
73-
poplist.POPEntry[count].messageSize = (rc == 0 ? stat_buf.st_size : 0);
74-
poplist.POPEntry[count].deleted = false;
77+
if (filePresent(poplist,filepath)==NO) {
78+
poplist.POPEntry.push_back(POPEnt()); //create new entry
79+
strcpy(poplist.POPEntry[count].messagePath, filepath);
80+
struct stat stat_buf;
81+
int rc = stat(filepath, &stat_buf);
82+
poplist.POPEntry[count].messageSize = (rc == 0 ? stat_buf.st_size : 0);
83+
poplist.POPEntry[count].deleted = false;
84+
}
7585
cumulative_size += poplist.POPEntry[count].messageSize;
7686
count++;
7787
}

MailServerGit/pop_server.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ char pop_user[USERNAMELENGTH];
5454
char pop_pass[PASSWORDLENGTH]; //maybe need the path here too?
5555
char pop_path[MAXPATHLENGTH];
5656
bool pop_logged_in;
57+
POPListing popList;
5758

5859
//----------------Mail Events Processing
5960

@@ -90,15 +91,14 @@ void pop_respond(int client_sockfd, char* request, UserDictionary users) {
9091
char param[10];
9192
get_args;
9293
copy_args(pa,pb,param);
93-
POPListing popList;
9494
int success = getList(pop_path, popList);
9595
if (success == FUNCTION_SUCCESS) {
9696
if ((atoi(param)>0) && (atoi(param)-1<popList.POPEntry.size())) {
9797
if (popList.POPEntry[atoi(param)-1].deleted) {
9898
send_data(client_sockfd, format("%s\r\n",format(responses[ERR],
9999
format(popmessages[4],atoi(param)).c_str()).c_str()).c_str());
100100
} else {
101-
popList.POPEntry[atoi(param)-1].deleted = true; //mark as deleted
101+
popList.POPEntry[atoi(param)-1].deleted = YES; //mark as deleted
102102
send_data(client_sockfd, format("%s\r\n",format(responses[OK],
103103
format(popmessages[5],atoi(param)).c_str()).c_str()).c_str());
104104
}
@@ -112,7 +112,6 @@ void pop_respond(int client_sockfd, char* request, UserDictionary users) {
112112
char param[10];
113113
get_args;
114114
copy_args(pa,pb,param);
115-
POPListing popList;
116115
int success = getList(pop_path, popList);
117116
if (success == FUNCTION_SUCCESS) {
118117
if (atoi(param)>0) {
@@ -136,12 +135,15 @@ void pop_respond(int client_sockfd, char* request, UserDictionary users) {
136135
} else if ((strncmp(request, "NOOP", 4) == 0) && pop_logged_in) {
137136
send_empty_ok;
138137
} else if (strncmp(request, "QUIT", 4) == 0) {
139-
POPListing popList;
140138
int success = getList(pop_path, popList);
141139
if (success == FUNCTION_SUCCESS) {
142140
for (int i=0; i<popList.POPEntry.size(); i++) {
143-
if (popList.POPEntry[i].deleted) {
144-
remove(popList.POPEntry[i].messagePath); //delete all messages tagged for deletion
141+
cout << "checking " << i << endl;
142+
if (popList.POPEntry[i].deleted == YES) {
143+
create_log_entry(APPNAME, format("POP Deleting: %s", popList.POPEntry[i].messagePath));
144+
if (remove(popList.POPEntry[i].messagePath) != FUNCTION_SUCCESS) { //delete all messages tagged for deletion
145+
create_log_entry(APPNAME, format("POP **Error** Deleting: %s", popList.POPEntry[i].messagePath));
146+
}
145147
}
146148
}
147149
}
@@ -153,7 +155,6 @@ void pop_respond(int client_sockfd, char* request, UserDictionary users) {
153155
char param[10];
154156
get_args;
155157
copy_args(pa,pb,param);
156-
POPListing popList;
157158
int success = getList(pop_path, popList);
158159
if (success == FUNCTION_SUCCESS) {
159160
if ((atoi(param)>0) && (atoi(param)-1<popList.POPEntry.size())) {
@@ -173,7 +174,6 @@ void pop_respond(int client_sockfd, char* request, UserDictionary users) {
173174
send_err_message;
174175
}
175176
} else if ((strncmp(request, "RSET", 4) == 0) && pop_logged_in) {
176-
POPListing popList;
177177
int success = getList(pop_path, popList);
178178
if (success == FUNCTION_SUCCESS) {
179179
for (int i=0; i<popList.POPEntry.size(); i++) {
@@ -186,7 +186,6 @@ void pop_respond(int client_sockfd, char* request, UserDictionary users) {
186186
send_err_message;
187187
}
188188
} else if ((strncmp(request, "STAT", 4) == 0) && pop_logged_in) { // don't include deleted messages
189-
POPListing popList;
190189
int success = getList(pop_path, popList);
191190
if (success == FUNCTION_SUCCESS) {
192191
sprintf(result,"%s %lu %lu\r\n",format(responses[OK],"").c_str(),popList.POPEntry.size(),popList.totalSize);
@@ -210,7 +209,6 @@ void pop_respond(int client_sockfd, char* request, UserDictionary users) {
210209
} else {
211210
send_err_message;
212211
}
213-
POPListing popList;
214212
int success = getList(pop_path, popList);
215213
if ((success == FUNCTION_SUCCESS) && ((atoi(lines_co)>0) && (atoi(lines_co)<200))) {
216214
if ((atoi(message_no)>0) && (atoi(message_no)-1<popList.POPEntry.size())) {
@@ -254,7 +252,6 @@ void pop_respond(int client_sockfd, char* request, UserDictionary users) {
254252
char param[10];
255253
get_args;
256254
copy_args(pa,pb,param);
257-
POPListing popList;
258255
int success = getList(pop_path, popList);
259256
if (success == FUNCTION_SUCCESS) { // don't show deleted messages
260257
if (atoi(param)>0) {

0 commit comments

Comments
 (0)