mailserver2client.cpp

来自「funambol window mobile客户端源代码」· C++ 代码 · 共 1,844 行 · 第 1/5 页

CPP
1,844
字号
}


static TCHAR szSMTP[] = L"SMTP";

void AddRecipients(LPWSTR pszSrc, ULONG ulType, ADRLIST* plst, LPWSTR& pszLocalRecips, LPSPropValue& pval)
{
    LOG.debug("Enter into AddRecipients function");
    if (pszSrc == NULL) {
        LOG.debug("Exiting from AddRecipients function: no recipients");
        return;
    }
    LPWSTR psz = pszSrc;
    while (*psz != L'\0') {
        while (*psz == L' ' && *psz != '\0')
            ++psz;

        if (*psz == L'\0')
            break;

        LPWSTR pszEnd = psz;
        // while (*pszEnd != L' ' && (*pszEnd != ';' || *pszEnd != ',') && *pszEnd != '\0')
        while (*pszEnd != ',' && *pszEnd != '\0')
            ++pszEnd;

        int cch = pszEnd - psz;

        wcsncpy(pszLocalRecips, psz, cch);
        *(pszLocalRecips + cch) = L'\0';

        plst->aEntries[plst->cEntries].cValues = 3;
        plst->aEntries[plst->cEntries].rgPropVals = pval;

        // Set the type (To, Cc, Bcc)...
        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[12] = {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 ;

    } 
    // the only flag needed to insert an email. User for send log only
    else if (wcscmp(m->getParent(), TEXT("O")) == 0) {
        
        messageFlag = 40 ;
    }

    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()) {
                if (m->getEmailData()->getRemainingAttachNumber() <= 0) {
                   isInclusive = true;
                } else {
                    MailMessage &msg2 = m->getEmailData()->getEmailItem();
                    if (!msg2.empty()) {
                        BodyPart& bpart = msg2.getBody();
                        if (bpart.getContent() != NULL) {
                            unsigned long len = strlen(bpart.getContent());
                            if (len >= 400000 && m->getEmailData()->getRemainingBodySize() > 0) {
                                isInclusive = true;
                            }
                        }
                    }
                }
            }
        }
        
        if (isInclusive == false) {
            if (m->getCurrentMessageSizeFilter() >= m->getMaxMailMessageSize() && 
                    (m->getEmailData()->getRemainingAttachNumber() <= 0 ||
                     m->getEmailData()->getRemainingBodySize() > 0))
                        isInclusive = true;
                
            }
        }
        */

        if (isInclusive == false) {
            if (m->getCurrentMessageSizeFilter() >= m->getMaxMailMessageSize() && 
                (m->getIncludeAttachment() == true                  ||
                 m->getEmailData()->getRemainingBodySize() > 0      ||
                 m->getEmailData()->getRemainingAttachNumber() <= 0 )) {    
                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;
        }
        
        // insert importance // the priority
        if (msg.getImportance() != NULL) {
            rgprops[cProps].ulPropTag = PR_IMPORTANCE;
            long priority = IMPORTANCE_NORMAL;
            if (strcmp(msg.getImportance(), "1") == 0) {
                priority = IMPORTANCE_HIGH;
            } else if (strcmp(msg.getImportance(), "5") == 0) {
                priority = IMPORTANCE_LOW;
            }
            rgprops[cProps].Value.l = priority;
            ++cProps;
        }
        
        if (wcscmp(m->getParent(), TEXT("O")) != 0) {
            
            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;
        }
        
        rgprops[cProps].ulPropTag = PR_CUSTOM_MESSAGE_ID;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?