📄 mailserver2client.cpp
字号:
pval->ulPropTag = PR_RECIPIENT_TYPE;
pval->Value.ul = ulType;
++pval;
// Set the address type (we only do SMTP)...
pval->ulPropTag = PR_ADDRTYPE;
pval->Value.lpszW = szSMTP;
++pval;
// Set the address...
pval->ulPropTag = PR_EMAIL_ADDRESS;
pval->Value.lpszW = pszLocalRecips;
++pval;
/* Modify the plst->aEntries[plst->cEntries].cValues = 4;
// Set the name...
pval->ulPropTag = PR_DISPLAY_NAME;
pval->Value.lpszW = TEXT("Fabio M");
++pval;
*/
++plst->cEntries;
pszLocalRecips += wcslen(pszLocalRecips) + 1;
if (*pszEnd != L'\0')
++pszEnd;
psz = pszEnd;
}
LOG.debug("Exiting from AddRecipients function");
}
HRESULT MailServer2Client::SetMessageRecipients (LPMESSAGE pmsg, MailClientData* m) {
LOG.debug("Enter into SetMessageRecipients function");
ULONG cRecipients = 0;
ULONG cchTotalRecips = 0;
ADRLIST* plst = NULL;
LPSPropValue pval = NULL;
BYTE* pb = NULL;
LPWSTR pszLocalRecips;
HRESULT hr;
wchar_t *to = NULL, *cc = NULL, *bcc = NULL;
if (m->getEmailData()->getEmailItem().getTo()) {
to = toWideChar(m->getEmailData()->getEmailItem().getTo());
cRecipients += CountRecipsInString(to);
cchTotalRecips += wcslen(to) + 3;
}
if (m->getEmailData()->getEmailItem().getCc()) {
cc = toWideChar(m->getEmailData()->getEmailItem().getCc());
cRecipients += CountRecipsInString(cc);
cchTotalRecips += wcslen(cc) + 3;
}
if (m->getEmailData()->getEmailItem().getBcc()) {
bcc = toWideChar(m->getEmailData()->getEmailItem().getBcc());
cRecipients += CountRecipsInString(bcc);
cchTotalRecips += wcslen(bcc) + 3;
}
//
// Allocate one big block of memory to hold all the strings.
// The block is arranged as follows:
//
// ADRLIST SPropValue's Copy of addresses
// |---------------------|--------------------|-------------------|
//
DWORD cb = sizeof(ADRLIST) + ((sizeof(ADRENTRY) + sizeof(SPropValue) * 3) * cRecipients);
pb = new BYTE[cb + (cchTotalRecips * sizeof(WCHAR))];
if (pb == NULL)
return E_OUTOFMEMORY;
ZeroMemory(pb, cb + (cchTotalRecips * sizeof(WCHAR)));
plst = (ADRLIST*) pb;
pszLocalRecips = (LPWSTR) (pb + cb);
pb += sizeof(ADRLIST) + (sizeof(ADRENTRY) * cRecipients);
pval = (SPropValue*) pb;
AddRecipients(to, MAPI_TO, plst, pszLocalRecips, pval);
AddRecipients(cc, MAPI_CC, plst, pszLocalRecips, pval);
AddRecipients(bcc, MAPI_BCC, plst, pszLocalRecips, pval);
hr = pmsg->ModifyRecipients(MODRECIP_ADD, plst);
delete[] (BYTE*) plst;
if (to) {delete [] to; to = NULL;}
if (cc) {delete [] cc; cc = NULL;}
if (bcc) {delete [] bcc; bcc = NULL;}
LOG.debug("Exiting from SetMessageRecipients function");
return hr;
}
void MailServer2Client::bt2ft(const BasicTime& b, FILETIME& f)
{
SYSTEMTIME local, s1, s2;
local.wYear = b.getYear();
local.wMonth = b.getMonth();
local.wDay = b.getDay();
local.wDayOfWeek = DayOfWeek(local.wYear, local.wMonth, local.wDay); // b.getWeekday(); // 0 sunday, 1monday
local.wHour = b.getHour();
local.wMinute = b.getMin();
local.wSecond = b.getSec();
local.wMilliseconds = 0;
s1 = DT_AddDiff(nano100SecInHour, -(b.getTzHour()), &local);
if (b.getTzMin() != 0 && b.getTzHour() != 0)
s2 = DT_AddDiff(nano100SecInMin, -(abs(b.getTzHour())/b.getTzHour())*(b.getTzMin()), &s1);
else s2 = DT_AddDiff(nano100SecInMin,b.getTzMin(),&s1);
SystemTimeToFileTime(&s2, &f);
}
/*
* OnlyFlags says the client has to set only the PR_MESSAGE_FLAGS of the message the PR_MSG_STATUS.
* This is for the update mail. In our case the draft modification are sent from server
* as a delete of the old one and a new insert of the new one.
*/
HRESULT MailServer2Client::SetMessageProps(IMAPISession* pSession, LPMESSAGE pmsg, MailClientData* m, BOOL onlyFlags) {
LOG.debug("Enter into SetMessageProps function");
SPropValue rgprops[11] = {0};
ULONG cProps = 0;
HRESULT hr;
BodyPart bp;
wchar_t *subj = NULL, *from = NULL,
*messId = NULL, *headers = NULL;
// it is used only when the email is partial. To store the remaining email
// to be downloaded
long emailSizeToDownload = 0; //
LPSTREAM pstm = NULL;
ULONG messageFlag = (ULONG) 0x00000000;
ULONG messageStatus = (ULONG) 0x00000000;
//
// Set this flag only for Inbox and Draft. This shows the From in the
// visualization of the mail into the pocket pc
if (wcscmp(m->getParent(), TEXT("I")) == 0 ||
wcscmp(m->getParent(), TEXT("T")) == 0) {
messageFlag = messageFlag | MSGFLAG_UNMODIFIED;
} else if (wcscmp(m->getParent(), TEXT("D")) == 0) {
messageFlag = messageFlag | MSGFLAG_FROMME | MSGFLAG_UNSENT;
} else if (wcscmp(m->getParent(), TEXT("S")) == 0) {
messageFlag = messageFlag | MSGFLAG_FROMME ;
}
if (m->getEmailData()->getRead())
messageFlag = messageFlag | MSGFLAG_READ;
// Set the flags and a subject if they exist.
rgprops[cProps].ulPropTag = PR_MESSAGE_FLAGS;
rgprops[cProps].Value.ul = messageFlag;
++cProps;
// rgprops[cProps].Value.ul = MSGSTATUS_RECTYPE_SMTP; //MSGSTATUS_HEADERONLY; // MSGSTATUS_PARTIAL | MSGSTATUS_RECTYPE_SMTP | // // | MSGSTATUS_PARTIAL | MSGSTATUS_HAS_PR_CE_MIME_TEXT; //
// rgprops[cProps].Value.ul = MSGSTATUS_HEADERONLY | MSGSTATUS_PENDING_ATTACHMENTS; // MSGSTATUS_PARTIAL | MSGSTATUS_RECTYPE_SMTP | // // | MSGSTATUS_PARTIAL | MSGSTATUS_HAS_PR_CE_MIME_TEXT; // chn
/*
* how understand if the file is to download or it has some other to be downloaded
*/
BOOL foundToBeDownloaded = FALSE;
/*
* internal isInclusive drives the choices of the action to perform.
* it can be true even if the getIsInclusive is false because some choice can
* converge
*/
bool isInclusive = m->getIsSyncInclusive();
rgprops[cProps].ulPropTag = PR_MSG_STATUS;
if (m->getEmailData()->getIsMailPartial()) {
if (isInclusive == false) {
if (m->getCurrentMessageSizeFilter() >= m->getMaxMailMessageSize()) {
isInclusive = true;
}
}
if (isInclusive == false) {
if(m->getEmailData()->getRemainingBodySize() > 0) {
foundToBeDownloaded = TRUE;
} else {
for (unsigned int i = 0; i < m->getEmailData()->getRemainingAttachNumber(); i++) {
if (m->getEmailData()->getAttachmentSize(i) > 0 &&
(m->getEmailData()->getAttachmentSize(i) < m->getMaxMailMessageSize())) {
foundToBeDownloaded = TRUE;
}
}
}
}
if (foundToBeDownloaded) {
messageStatus = messageStatus | MSGSTATUS_HEADERONLY | MSGSTATUS_PARTIAL;
}
// The following code must be used when the partial attachments are handled properly
// if (m->getEmailData()->getRemainingAttachNumber() > 0) { // there is any attach to be downloaded
// messageStatus = messageStatus | MSGSTATUS_PENDING_ATTACHMENTS;
// }
// if (m->getEmailData()->getRemainingBodySize() > 0) { // there is any body to be downloaded
// messageStatus = messageStatus | MSGSTATUS_HEADERONLY;
// if (m->getEmailData()->getRemainingAttachNumber() == 0) {
// messageStatus = messageStatus | MSGSTATUS_PARTIAL;
// }
// }
}
if (messageStatus == (ULONG) 0x00000000) {
messageStatus = MSGSTATUS_RECTYPE_SMTP;
}
rgprops[cProps].Value.ul = messageStatus;
++cProps;
//if (!onlyFlags) { // no more used when the inclusive filter was inserted. If the msg MailMessage is
// emty no other actions are done
MailMessage &msg = m->getEmailData()->getEmailItem();
/*
* empty checks the date of the message instead of the message id
*/
if (!msg.empty()) {
if (msg.attachmentCount() > 0) {
rgprops[cProps].ulPropTag = PR_HASATTACH;
rgprops[cProps].Value.ul = msg.attachmentCount();
++cProps;
}
// Set the recipients up.
hr = SetMessageRecipients(pmsg, m);
EXIT_ON_FAILED(hr);
//hr = SetReplyTo(pSession, pmsg, "Sync4j@yahoogroups.com");
//EXIT_ON_FAILED(hr);
if (msg.getSubject() != NULL) {
rgprops[cProps].ulPropTag = PR_SUBJECT;
subj = toWideChar(msg.getSubject());
rgprops[cProps].Value.lpszW = subj;
++cProps;
}
if (msg.getFrom() != NULL) {
rgprops[cProps].ulPropTag = PR_SENDER_EMAIL_ADDRESS;
from = toWideChar(msg.getFrom());
rgprops[cProps].Value.lpszW = from;
++cProps;
}
FILETIME ft;
// Received time (Received: header) to view externally. currently it isn't of use
bt2ft(msg.getReceived(), ft);
rgprops[cProps].ulPropTag = PR_MESSAGE_DELIVERY_TIME;
rgprops[cProps].Value.ft = ft;
++cProps;
// Sent time (Date: header). to view internally. this is right
bt2ft(msg.getDate(), ft);
rgprops[cProps].ulPropTag = PR_CLIENT_SUBMIT_TIME; // PR_DELIVER_TIME; // // PR_LAST_MODIFICATION_TIME;
rgprops[cProps].Value.ft = ft;
++cProps;
// TODO: MODIFY THE rgprops TOO!|!!!!!!!!
//
rgprops[cProps].ulPropTag = PR_CUSTOM_MESSAGE_ID;
messId = toWideChar(msg.getMessageId());
if (messId) {
rgprops[cProps].Value.lpszW = messId;
++cProps;
}
rgprops[cProps].ulPropTag = PR_CUSTOM_MESSAGE_HEADER;
headers = toWideChar(msg.getHeaders());
if (headers) {
rgprops[cProps].Value.lpszW = headers;
++cProps;
}
// Body
bp = msg.getBody();
// add to the body the name of the attachments and their sizes
if (m->getEmailData()->getIsMailPartial()) {
int attachNum = 0, attachSize = 0;
unsigned long maxMsg = 0, bodySize = 0, totalBodySize = 0, filter;
string appendToBody;
string maxMsgString;
string filterString;
string totalBodySizeString;
char tmp[64];
// are in bytes
bodySize = m->getEmailData()->getRemainingBodySize() > 0 ?
m->getEmailData()->getRemainingBodySize() : 0;
filter = m->getCurrentMessageSizeFilter();
totalBodySize = bodySize + filter;
attachNum = m->getEmailData()->getRemainingAttachNumber() > 0 ?
m->getEmailData()->getRemainingAttachNumber() : 0;
maxMsg = m->getMaxMailMessageSize();
//
// round to the nearest ten value. This because
// the 500 kb supported are not really 500 but a multiple 1024
// so a round is needed to not write 496 kb
//
unsigned long toShow = (maxMsg/1024 % 5);
toShow = (5 - toShow) + maxMsg/1024;
sprintf(tmp, "%luKb", toShow);
maxMsgString += tmp;
if ((toShow = filter/1024) > 450) {
toShow = (filter/1024 % 5);
toShow = (5 - toShow) + filter/1024;
}
sprintf(tmp, "%luKb", toShow);
filterString += tmp;
sprintf(tmp, "%luKb", totalBodySize/1024);
totalBodySizeString += tmp;
if (isInclusive == false) {
if (filter >= maxMsg) {
isInclusive = true;
}
}
// inside here filter < maxMsg
if (isInclusive == false) {
if (bodySize > 0) {
if (totalBodySize > maxMsg) {
if (attachNum > 0) {
//case Pre-action 10
appendToBody += "Click here to download the rest of the message";
appendToBody += " up to the maximum allowed size of ";
appendToBody += maxMsgString;
} else {
// case Pre-action 3
appendToBody += "Only first ";
appendToBody += filterString;
appendToBody += " (of " + totalBodySizeString + ").";
appendToBody += " Click below to get the rest";
appendToBody += " up to the maximum allowed size of ";
appendToBody += maxMsgString;
}
} else {
if (attachNum > 0) {
bool existsMinorOfMaxMsg = false;
bool existsMajorOfMaxMsg = false;
for (int i = 0; i < attachNum; i++) {
if (m->getEmailData()->getAttachmentSize(i) > 0 &&
(m->getEmailData()->getAttachmentSize(i) < maxMsg)) {
existsMinorOfMaxMsg = true;
} else {
existsMajorOfMaxMsg = true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -