Style cleanup, no functional change.

This commit is contained in:
Axel Dörfler
2015-01-06 15:21:12 +01:00
parent f06916d572
commit 6a6b7ae7a0
2 changed files with 106 additions and 119 deletions
@@ -1,13 +1,15 @@
/* /*
* Copyright 2007-2011, Haiku, Inc. All rights reserved. * Copyright 2007-2012, Haiku, Inc. All rights reserved.
* Copyright 2001-2002 Dr. Zoidberg Enterprises. All rights reserved. * Copyright 2001-2002 Dr. Zoidberg Enterprises. All rights reserved.
* Copyright 2011, Clemens Zeidler <[email protected]> * Copyright 2011, Clemens Zeidler <[email protected]>
* *
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
//! POP3Protocol - implementation of the POP3 protocol //! POP3Protocol - implementation of the POP3 protocol
#include "pop3.h" #include "pop3.h"
#include <errno.h> #include <errno.h>
@@ -46,8 +48,12 @@
#define B_TRANSLATION_CONTEXT "pop3" #define B_TRANSLATION_CONTEXT "pop3"
static void NotHere(BStringList &that, BStringList &otherList, #define POP3_RETRIEVAL_TIMEOUT 60000000
BStringList *results) #define CRLF "\r\n"
static void
NotHere(BStringList& that, BStringList& otherList, BStringList* results)
{ {
for (int32 i = 0; i < otherList.CountStrings(); i++) { for (int32 i = 0; i < otherList.CountStrings(); i++) {
if (!that.HasString(otherList.StringAt(i))) if (!that.HasString(otherList.StringAt(i)))
@@ -56,8 +62,7 @@ static void NotHere(BStringList &that, BStringList &otherList,
} }
#define POP3_RETRIEVAL_TIMEOUT 60000000 // #pragma mark -
#define CRLF "\r\n"
POP3Protocol::POP3Protocol(BMailAccountSettings* settings) POP3Protocol::POP3Protocol(BMailAccountSettings* settings)
@@ -92,8 +97,8 @@ POP3Protocol::~POP3Protocol()
status_t status_t
POP3Protocol::Connect() POP3Protocol::Connect()
{ {
status_t error = Open(fSettings.FindString("server"), fSettings.FindInt32("port"), status_t error = Open(fSettings.FindString("server"),
fSettings.FindInt32("flavor")); fSettings.FindInt32("port"), fSettings.FindInt32("flavor"));
if (error != B_OK) if (error != B_OK)
return error; return error;
@@ -147,7 +152,7 @@ POP3Protocol::SyncMessages()
} }
ReportProgress(0, 1, B_TRANSLATE("Getting UniqueIDs" B_UTF8_ELLIPSIS)); ReportProgress(0, 1, B_TRANSLATE("Getting UniqueIDs" B_UTF8_ELLIPSIS));
error = _UniqueIDs(); error = _RetrieveUniqueIDs();
if (error < B_OK) { if (error < B_OK) {
ResetProgress(); ResetProgress();
Disconnect(); Disconnect();
@@ -226,9 +231,8 @@ POP3Protocol::SyncMessages()
ReportProgress(0, 1); ReportProgress(0, 1);
if (file.WriteAttr("MAIL:unique_id", B_STRING_TYPE, 0, uid, if (file.WriteAttr("MAIL:unique_id", B_STRING_TYPE, 0, uid,
strlen(uid)) < 0) { strlen(uid)) < 0)
error = B_ERROR; error = B_ERROR;
}
file.WriteAttr("MAIL:size", B_INT32_TYPE, 0, &size, sizeof(int32)); file.WriteAttr("MAIL:size", B_INT32_TYPE, 0, &size, sizeof(int32));
@@ -255,7 +259,7 @@ POP3Protocol::FetchBody(const entry_ref& ref)
if (error < B_OK) if (error < B_OK)
return error; return error;
error = _UniqueIDs(); error = _RetrieveUniqueIDs();
if (error < B_OK) { if (error < B_OK) {
Disconnect(); Disconnect();
return error; return error;
@@ -314,7 +318,7 @@ POP3Protocol::DeleteMessage(const entry_ref& ref)
if (error < B_OK) if (error < B_OK)
return error; return error;
error = _UniqueIDs(); error = _RetrieveUniqueIDs();
if (error < B_OK) { if (error < B_OK) {
Disconnect(); Disconnect();
return error; return error;
@@ -380,7 +384,7 @@ POP3Protocol::Open(const char* server, int port, int)
BNetworkAddress(server, port)); BNetworkAddress(server, port));
} else { } else {
fServerConnection = new(std::nothrow) BSocket(BNetworkAddress( fServerConnection = new(std::nothrow) BSocket(BNetworkAddress(
server, port)); server, port));
} }
if (fServerConnection == NULL) if (fServerConnection == NULL)
@@ -416,22 +420,19 @@ POP3Protocol::Open(const char* server, int port, int)
status_t status_t
POP3Protocol::Login(const char *uid, const char *password, int method) POP3Protocol::Login(const char* uid, const char* password, int method)
{ {
status_t err; status_t err;
BString error_msg, userString; BString errorMessage(B_TRANSLATE("Error while authenticating user %user"));
error_msg << B_TRANSLATE("Error while authenticating user %user"); errorMessage.ReplaceFirst("%user", uid);
userString << uid;
error_msg.ReplaceFirst("%user", userString);
if (method == 1) { //APOP if (method == 1) { //APOP
int32 index = fLog.FindFirst("<"); int32 index = fLog.FindFirst("<");
if(index != B_ERROR) { if(index != B_ERROR) {
ReportProgress(0, 0, B_TRANSLATE("Sending APOP authentication" ReportProgress(0, 0, B_TRANSLATE("Sending APOP authentication"
B_UTF8_ELLIPSIS)); B_UTF8_ELLIPSIS));
int32 end = fLog.FindFirst(">",index); int32 end = fLog.FindFirst(">", index);
BString timestamp(""); BString timestamp("");
fLog.CopyInto(timestamp, index, end - index + 1); fLog.CopyInto(timestamp, index, end - index + 1);
timestamp += password; timestamp += password;
@@ -445,15 +446,15 @@ POP3Protocol::Login(const char *uid, const char *password, int method)
err = SendCommand(cmd.String()); err = SendCommand(cmd.String());
if (err != B_OK) { if (err != B_OK) {
error_msg << B_TRANSLATE(". The server said:\n") << fLog; errorMessage << B_TRANSLATE(". The server said:\n") << fLog;
ShowError(error_msg.String()); ShowError(errorMessage.String());
return err; return err;
} }
return B_OK; return B_OK;
} else { } else {
error_msg << B_TRANSLATE(": The server does not support APOP."); errorMessage << B_TRANSLATE(": The server does not support APOP.");
ShowError(error_msg.String()); ShowError(errorMessage.String());
return B_NOT_ALLOWED; return B_NOT_ALLOWED;
} }
} }
@@ -465,8 +466,8 @@ POP3Protocol::Login(const char *uid, const char *password, int method)
err = SendCommand(cmd.String()); err = SendCommand(cmd.String());
if (err != B_OK) { if (err != B_OK) {
error_msg << B_TRANSLATE(". The server said:\n") << fLog; errorMessage << B_TRANSLATE(". The server said:\n") << fLog;
ShowError(error_msg.String()); ShowError(errorMessage.String());
return err; return err;
} }
@@ -477,8 +478,8 @@ POP3Protocol::Login(const char *uid, const char *password, int method)
err = SendCommand(cmd.String()); err = SendCommand(cmd.String());
if (err != B_OK) { if (err != B_OK) {
error_msg << B_TRANSLATE(". The server said:\n") << fLog; errorMessage << B_TRANSLATE(". The server said:\n") << fLog;
ShowError(error_msg.String()); ShowError(errorMessage.String());
return err; return err;
} }
@@ -494,9 +495,10 @@ POP3Protocol::Stat()
if (SendCommand("STAT" CRLF) < B_OK) if (SendCommand("STAT" CRLF) < B_OK)
return B_ERROR; return B_ERROR;
int32 messages, dropSize; int32 messages;
int32 dropSize;
if (sscanf(fLog.String(), "+OK %" B_SCNd32" %" B_SCNd32, &messages, if (sscanf(fLog.String(), "+OK %" B_SCNd32" %" B_SCNd32, &messages,
&dropSize) < 2) &dropSize) < 2)
return B_ERROR; return B_ERROR;
fNumMessages = messages; fNumMessages = messages;
@@ -530,10 +532,10 @@ void
POP3Protocol::CheckForDeletedMessages() POP3Protocol::CheckForDeletedMessages()
{ {
{ {
//---Delete things from the manifest no longer on the server // Delete things from the manifest no longer on the server
BStringList temp; BStringList list;
NotHere(fUniqueIDs, fManifest, &temp); NotHere(fUniqueIDs, fManifest, &list);
fManifest.Remove(temp); fManifest.Remove(list);
} }
if (!fSettings.FindBool("delete_remote_when_local") if (!fSettings.FindBool("delete_remote_when_local")
@@ -580,42 +582,42 @@ POP3Protocol::CheckForDeletedMessages()
status_t status_t
POP3Protocol::Retrieve(int32 message, BPositionIO *write_to) POP3Protocol::Retrieve(int32 message, BPositionIO* to)
{ {
status_t returnCode;
BString cmd; BString cmd;
cmd << "RETR " << message + 1 << CRLF; cmd << "RETR " << message + 1 << CRLF;
returnCode = RetrieveInternal(cmd.String(), message, write_to, true); status_t status = RetrieveInternal(cmd.String(), message, to, true);
ReportProgress(0 /* bytes */, 1 /* messages */); ReportProgress(0, 1);
if (returnCode == B_OK) { // Some debug code. if (status == B_OK) {
int32 message_len = MessageSize(message); // Check if the actual message size matches the expected one
write_to->Seek (0, SEEK_END); int32 size = MessageSize(message);
if (write_to->Position() != message_len) { to->Seek(0, SEEK_END);
printf ("POP3Protocol::Retrieve Note: message size is %" B_PRIdOFF if (to->Position() != size) {
", was expecting %" B_PRId32 ", for message #%" B_PRId32 ". " printf("POP3Protocol::Retrieve Note: message size is %" B_PRIdOFF
"Could be a transmission error or a bad POP server " ", was expecting %" B_PRId32 ", for message #%" B_PRId32
". Could be a transmission error or a bad POP server "
"implementation (does it remove escape codes when it counts " "implementation (does it remove escape codes when it counts "
"size?).\n", write_to->Position(), message_len, message); "size?).\n", to->Position(), size, message);
} }
} }
return returnCode; return status;
} }
status_t status_t
POP3Protocol::GetHeader(int32 message, BPositionIO *write_to) POP3Protocol::GetHeader(int32 message, BPositionIO* to)
{ {
BString cmd; BString cmd;
cmd << "TOP " << message + 1 << " 0" << CRLF; cmd << "TOP " << message + 1 << " 0" << CRLF;
return RetrieveInternal(cmd.String(),message,write_to, false); return RetrieveInternal(cmd.String(), message, to, false);
} }
status_t status_t
POP3Protocol::RetrieveInternal(const char *command, int32 message, POP3Protocol::RetrieveInternal(const char* command, int32 message,
BPositionIO *write_to, bool post_progress) BPositionIO* to, bool postProgress)
{ {
const int bufSize = 1024 * 30; const int bufSize = 1024 * 30;
@@ -629,14 +631,14 @@ POP3Protocol::RetrieveInternal(const char *command, int32 message,
amountToReceive = bufSize - 1; amountToReceive = bufSize - 1;
BString bufBString; // Used for auto-dealloc on return feature. BString bufBString; // Used for auto-dealloc on return feature.
char *buf = bufBString.LockBuffer (bufSize); char* buf = bufBString.LockBuffer(bufSize);
int amountInBuffer = 0; int amountInBuffer = 0;
int amountReceived; int amountReceived;
int testIndex; int testIndex;
char *testStr; char* testStr;
bool cont = true; bool cont = true;
bool flushWholeBuffer = false; bool flushWholeBuffer = false;
write_to->Seek(0,SEEK_SET); to->Seek(0, SEEK_SET);
if (SendCommand(command) != B_OK) if (SendCommand(command) != B_OK)
return B_ERROR; return B_ERROR;
@@ -656,8 +658,8 @@ POP3Protocol::RetrieveInternal(const char *command, int32 message,
amountToReceive); amountToReceive);
if (amountReceived < 0) { if (amountReceived < 0) {
fLog = strerror(errno); fLog = strerror(amountReceived);
return errno; return amountReceived;
} }
if (amountReceived == 0) { if (amountReceived == 0) {
fLog = "POP3 data supposedly ready to receive but not received!"; fLog = "POP3 data supposedly ready to receive but not received!";
@@ -719,16 +721,17 @@ POP3Protocol::RetrieveInternal(const char *command, int32 message,
// comparison continuity, in case the line starting with a period // comparison continuity, in case the line starting with a period
// crosses a buffer boundary. // crosses a buffer boundary.
if (amountInBuffer > 4) { if (amountInBuffer > 4) {
write_to->Write(buf, amountInBuffer - 4); to->Write(buf, amountInBuffer - 4);
if (post_progress) if (postProgress)
ReportProgress(amountInBuffer - 4,0); ReportProgress(amountInBuffer - 4, 0);
memmove (buf, buf + amountInBuffer - 4, 4); memmove (buf, buf + amountInBuffer - 4, 4);
amountInBuffer = 4; amountInBuffer = 4;
} }
} else { // Dump everything - end of message or flushing the whole buffer. } else {
write_to->Write(buf, amountInBuffer); // Dump everything - end of message or flushing the whole buffer.
if (post_progress) to->Write(buf, amountInBuffer);
ReportProgress(amountInBuffer,0); if (postProgress)
ReportProgress(amountInBuffer, 0);
amountInBuffer = 0; amountInBuffer = 0;
} }
} }
@@ -747,9 +750,9 @@ POP3Protocol::Delete(int32 num)
#if DEBUG #if DEBUG
puts(fLog.String()); puts(fLog.String());
#endif #endif
/* The mail is just marked as deleted and removed from the server when // The mail is just marked as deleted and removed from the server when
sending the QUIT command. Because of that the message number stays the same // sending the QUIT command. Because of that the message number stays
and we keep the uid in the uid list. */ // the same and we keep the uid in the uid list.
} }
@@ -761,7 +764,7 @@ POP3Protocol::MessageSize(int32 index)
int32 int32
POP3Protocol::ReceiveLine(BString &line) POP3Protocol::ReceiveLine(BString& line)
{ {
int32 len = 0; int32 len = 0;
bool flag = false; bool flag = false;
@@ -780,9 +783,9 @@ POP3Protocol::ReceiveLine(BString &line)
bytesReceived = fServerConnection->Read((char*)&c, 1); bytesReceived = fServerConnection->Read((char*)&c, 1);
if (bytesReceived < 0) if (bytesReceived < 0)
return errno; return bytesReceived;
if (c == '\n' || bytesReceived == 0 /* EOF */) if (c == '\n' || bytesReceived == 0)
break; break;
if (c == '\r') { if (c == '\r') {
@@ -805,25 +808,19 @@ POP3Protocol::ReceiveLine(BString &line)
status_t status_t
POP3Protocol::SendCommand(const char* cmd) POP3Protocol::SendCommand(const char* cmd)
{ {
//printf(cmd);
// Flush any accumulated garbage data before we send our command, so we // Flush any accumulated garbage data before we send our command, so we
// don't misinterrpret responses from previous commands (that got left over // don't misinterrpret responses from previous commands (that got left over
// due to bugs) as being from this command. // due to bugs) as being from this command.
while (fServerConnection->WaitForReadable(1000) == B_OK) { while (fServerConnection->WaitForReadable(1000) == B_OK) {
int amountReceived; char buffer[4096];
char tempString [1024]; ssize_t amountReceived = fServerConnection->Read(buffer,
sizeof(buffer) - 1);
amountReceived = fServerConnection->Read(tempString,
sizeof(tempString) - 1);
if (amountReceived < 0) if (amountReceived < 0)
return errno; return amountReceived;
tempString [amountReceived] = 0; buffer[amountReceived] = 0;
printf ("POP3Protocol::SendCommand Bug! Had to flush %d bytes: %s\n", printf("POP3Protocol::SendCommand Bug! Had to flush %" B_PRIdSSIZE
amountReceived, tempString); " bytes: %s\n", amountReceived, buffer);
//if (amountReceived == 0)
// break;
} }
if (fServerConnection->Write(cmd, ::strlen(cmd)) < 0) { if (fServerConnection->Write(cmd, ::strlen(cmd)) < 0) {
@@ -834,28 +831,21 @@ POP3Protocol::SendCommand(const char* cmd)
} }
fLog = ""; fLog = "";
status_t err = B_OK; int32 length = ReceiveLine(fLog);
if (length <= 0 || fLog.ICompare("+OK", 3) == 0)
return B_OK;
while (true) { if (fLog.ICompare("-ERR", 4) == 0) {
int32 len = ReceiveLine(fLog); printf("POP3Protocol::SendCommand \"%s\" got error message "
if (len <= 0 || fLog.ICompare("+OK", 3) == 0) "from server: %s\n", cmd, fLog.String());
break; return B_ERROR;
if (fLog.ICompare("-ERR", 4) == 0) {
printf("POP3Protocol::SendCommand \"%s\" got error message "
"from server: %s\n", cmd, fLog.String());
err = B_ERROR;
break;
} else {
printf("POP3Protocol::SendCommand \"%s\" got nonsense message "
"from server: %s\n", cmd, fLog.String());
err = B_BAD_VALUE;
// If it's not +OK, and it's not -ERR, then what the heck
// is it? Presume an error
break;
}
} }
return err;
printf("POP3Protocol::SendCommand \"%s\" got nonsense message "
"from server: %s\n", cmd, fLog.String());
return B_BAD_DATA;
// If it's not +OK, and it's not -ERR, then what the heck
// is it? Presume an error
} }
@@ -883,7 +873,7 @@ POP3Protocol::MD5Digest(unsigned char *in, char *asciiDigest)
status_t status_t
POP3Protocol::_UniqueIDs() POP3Protocol::_RetrieveUniqueIDs()
{ {
fUniqueIDs.MakeEmpty(); fUniqueIDs.MakeEmpty();
@@ -928,9 +918,9 @@ void
POP3Protocol::_ReadManifest() POP3Protocol::_ReadManifest()
{ {
fManifest.MakeEmpty(); fManifest.MakeEmpty();
BString attr_name = "MAIL:"; BString attribute = "MAIL:";
attr_name << fAccountSettings.AccountID() << ":manifest"; attribute << fAccountSettings.AccountID() << ":manifest";
//--- In case someone puts multiple accounts in the same directory // In case someone puts multiple accounts in the same directory
BNode node(fDestinationDir); BNode node(fDestinationDir);
if (node.InitCheck() != B_OK) if (node.InitCheck() != B_OK)
@@ -941,15 +931,11 @@ POP3Protocol::_ReadManifest()
// be found on the first run as it will be later created by // be found on the first run as it will be later created by
// the INBOX system filter. // the INBOX system filter.
attr_info info; attr_info info;
/*status_t status = node.GetAttrInfo(attr_name.String(), &info); if (node.GetAttrInfo(attribute.String(), &info) != B_OK || info.size == 0)
printf("read manifest3 status %i\n", (int)status);
status = node.GetAttrInfo(attr_name.String(), &info);
printf("read manifest3 status2 %i\n", (int)status);*/
if (node.GetAttrInfo(attr_name.String(), &info) != B_OK)
return; return;
void* flatmanifest = malloc(info.size); void* flatmanifest = malloc(info.size);
node.ReadAttr(attr_name.String(), fManifest.TypeCode(), 0, node.ReadAttr(attribute.String(), fManifest.TypeCode(), 0,
flatmanifest, info.size); flatmanifest, info.size);
fManifest.Unflatten(fManifest.TypeCode(), flatmanifest, info.size); fManifest.Unflatten(fManifest.TypeCode(), flatmanifest, info.size);
free(flatmanifest); free(flatmanifest);
@@ -959,9 +945,9 @@ POP3Protocol::_ReadManifest()
void void
POP3Protocol::_WriteManifest() POP3Protocol::_WriteManifest()
{ {
BString attr_name = "MAIL:"; BString attribute = "MAIL:";
attr_name << fAccountSettings.AccountID() << ":manifest"; attribute << fAccountSettings.AccountID() << ":manifest";
//--- In case someone puts multiple accounts in the same directory // In case someone puts multiple accounts in the same directory
BNode node(fDestinationDir); BNode node(fDestinationDir);
if (node.InitCheck() != B_OK) { if (node.InitCheck() != B_OK) {
ShowError("Error while saving account manifest: cannot use " ShowError("Error while saving account manifest: cannot use "
@@ -969,11 +955,11 @@ POP3Protocol::_WriteManifest()
return; return;
} }
node.RemoveAttr(attr_name.String()); node.RemoveAttr(attribute.String());
ssize_t manifestsize = fManifest.FlattenedSize(); ssize_t manifestsize = fManifest.FlattenedSize();
void* flatmanifest = malloc(manifestsize); void* flatmanifest = malloc(manifestsize);
fManifest.Flatten(flatmanifest, manifestsize); fManifest.Flatten(flatmanifest, manifestsize);
status_t err = node.WriteAttr(attr_name.String(), status_t err = node.WriteAttr(attribute.String(),
fManifest.TypeCode(), 0, flatmanifest, manifestsize); fManifest.TypeCode(), 0, flatmanifest, manifestsize);
if (err < 0) { if (err < 0) {
BString error = "Error while saving account manifest: "; BString error = "Error while saving account manifest: ";
@@ -1,5 +1,5 @@
/* /*
* Copyright 2007-2011, Haiku Inc. All Rights Reserved. * Copyright 2007-2012, Haiku Inc. All Rights Reserved.
* Copyright 2001-2002 Dr. Zoidberg Enterprises. All rights reserved. * Copyright 2001-2002 Dr. Zoidberg Enterprises. All rights reserved.
* Copyright 2011, Clemens Zeidler <[email protected]> * Copyright 2011, Clemens Zeidler <[email protected]>
* *
@@ -64,10 +64,11 @@ protected:
void MD5Digest(unsigned char *in, char *out); void MD5Digest(unsigned char *in, char *out);
private: private:
status_t _UniqueIDs(); status_t _RetrieveUniqueIDs();
void _ReadManifest(); void _ReadManifest();
void _WriteManifest(); void _WriteManifest();
private:
BString fLog; BString fLog;
int32 fNumMessages; int32 fNumMessages;
size_t fMailDropSize; size_t fMailDropSize;