mailserver2client.cpp

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

CPP
1,844
字号
              hr = E_FAIL;
              break;
            }

            // Open this message store
            hr = pSession->OpenMsgStore (NULL,
                           psrs->aRow[0].lpProps[0].Value.bin.cb,
                          (ENTRYID *) psrs->aRow[0].lpProps[0].Value.
                           bin.lpb, NULL, 0, &pStore);
            EXIT_ON_FAILED (hr);

            // Now get the Inbox folder.
            hr = pStore->GetProps ((SPropTagArray *) rgTags, MAPI_UNICODE, &cValues, &rgprops);
            EXIT_ON_FAILED (hr);

            ASSERT (rgprops);

            //
            // watch at the folder that we want to sync
            //
            for (i = 0; i < folderNumber; i++) {
                wchar_t* t = c2wc(server_folderToSync[i]);
                // we don't want to delete the mail that may be in Outbox.
                // Otherwise the O/ can be a mail that has been moved in Sent folder.
                // it can happen that a mail sent and moved in Sent could be removed.
                if (wcscmp(t, m->getParent()) != 0 || wcscmp(t, TEXT("O")) == 0) {
                    if (t) {
                        delete [] t; t = NULL;
                    }
                    continue;
                }

                // open the message store
                hr = pStore->OpenEntry (rgprops[i].Value.bin.cb,
                            (LPENTRYID)rgprops[i].Value.bin.lpb,
                            NULL,
                            MAPI_MODIFY,
                            NULL,
                            (IUnknown **) &pfldrInbox);
                EXIT_ON_FAILED (hr);

                ASSERT (pfldrInbox);

                // Decode the id
                char* eid = NULL;
                size_t len = decodeEntryId(m->getEntryId(), &eid);
                if(!eid){
                    LOG.error("DeleteMessage: it is not possible to delete the mail with id %S. Continue sync...", m->getEntryId());
                    return -1;
                }

                ENTRYLIST entryList;

                entryList.cValues = 0;
                MAPIAllocateBuffer(sizeof(SBinary), (LPVOID*)&(entryList.lpbin));
                entryList.lpbin[entryList.cValues].cb = len;
                MAPIAllocateBuffer(len, (LPVOID*)&(entryList.lpbin[entryList.cValues].lpb) );
                memmove(entryList.lpbin[entryList.cValues].lpb, eid, len);
                entryList.cValues++;

                delete [] eid;

                hr = pfldrInbox->DeleteMessages (&entryList, 0, NULL, 0);
                EXIT_ON_FAILED (hr);

                MAPIFreeBuffer (entryList.lpbin);

                msgset = true;
                break;
            }
        }
        // Clean up
        MAPIFreeBuffer (rgprops);
        MAPIFreeBuffer (rgprops2);
        MAPIFreeBuffer (rgpropsMsg);
        FreeProws   (psrs);

        rgprops   =  NULL;
        rgpropsMsg =  NULL;
        psrs    =  NULL;

        RELEASE_OBJ (pmsg);
        RELEASE_OBJ (pfldrInbox);
        RELEASE_OBJ (pStore);
    }
    if (accountFound == FALSE) {
        LOG.error("No " PROVIDER " account found");
        goto FuncExit;
    }
FuncExit:

    MAPIFreeBuffer (rgprops);
    MAPIFreeBuffer (rgpropsMsg);
    FreeProws   (psrs);

    RELEASE_OBJ (pmsg);
    RELEASE_OBJ (pfldrInbox);
    RELEASE_OBJ (pStore);
    RELEASE_OBJ (pTable);
    RELEASE_OBJ (pSession);

    if (rgTags)
        delete rgTags;
    LOG.debug("Exiting from deleteMessage function");
    return hr;
}

bool MailServer2Client::messageExistInOubox_deleteIt(IMsgStore* pStore, IN const char* msgid) {

    if (msgid == NULL)
        return false;

    wchar_t* msgID = toWideChar(msgid);
    ULONG           cValues     =   0;
    SPropValue  *   rgprops     =   NULL;
    HRESULT hr                  = S_OK;
    bool bFound                 = false;

    SBinary    sbEntry = {0};
    LPMAPITABLE pContentsTable = NULL;
    LPMAPIFOLDER pfldrOutbox =   NULL;
    LPSRowSet pRows = NULL;
    SRow aRow = {0};

    ULONG rgTags[] = { 1, PR_IPM_OUTBOX_ENTRYID };

    enum {
        ePR_ENTRYID,            // 0
        ePR_CUSTOM_MESSAGE_ID,            // 2
        NUM_COLS                // number of columns (put it to the end)
    };

    // These tags represent the message information we would like to pick up
    SizedSPropTagArray(NUM_COLS, Columns) =
    {
        NUM_COLS,
        PR_ENTRYID,
        PR_CUSTOM_MESSAGE_ID
    };

    hr = pStore->GetProps((SPropTagArray *)rgTags, MAPI_UNICODE, &cValues, &rgprops);
    EXIT_ON_FAILED(hr);

    ASSERT(rgprops);
    ASSERT(rgprops[0].ulPropTag == PR_IPM_OUTBOX_ENTRYID);

    hr = pStore->OpenEntry (rgprops[0].Value.bin.cb,
                            (LPENTRYID)rgprops[0].Value.bin.lpb,
                            NULL,
                            MAPI_MODIFY,
                            NULL,
                            (IUnknown **) &pfldrOutbox);

    EXIT_ON_FAILED(hr);

    hr = pfldrOutbox->GetContentsTable(0, &pContentsTable);
    EXIT_ON_FAILED(hr);

    hr = pContentsTable->SetColumns((LPSPropTagArray)&Columns, 0);
    EXIT_ON_FAILED(hr);

    // iterate message one by one (each time only read 1)
    while(!FAILED(hr = pContentsTable->QueryRows(1, 0, &pRows))) {
        // this is the end of the content table
        if(pRows->cRows == 0) break;

        // check this Row
        aRow = pRows->aRow[0];

        // Correctly returned?
        if ((&aRow.lpProps[ePR_ENTRYID])->ulPropTag == PR_ENTRYID
            && (&aRow.lpProps[ePR_CUSTOM_MESSAGE_ID])->ulPropTag == PR_CUSTOM_MESSAGE_ID) {

            // Is CUSTOM_MESSAGE_ID found?
            wchar_t* mm = (&aRow.lpProps[ePR_CUSTOM_MESSAGE_ID])->Value.lpszW;

            // the message-id there is in the client has the O/AABBCC....
            // the message-id sent by server could have only AABBCC...

            if (wcsstr(mm, msgID) != 0) {

                wchar_t* entryId = convertBinaryToWChar(aRow.lpProps[ePR_ENTRYID].Value.bin, 'O');
                char* eid = NULL;
                size_t len = decodeEntryId(entryId, &eid);
                if(!eid){
                    LOG.error("messageExistInOubox_deleteIt: error decoding entryId %S", entryId);
                    return false;
                }

                ENTRYLIST entryList;

                entryList.cValues = 0;
                MAPIAllocateBuffer(sizeof(SBinary), (LPVOID*)&(entryList.lpbin));
                entryList.lpbin[entryList.cValues].cb = len;
                MAPIAllocateBuffer(len, (LPVOID*)&(entryList.lpbin[entryList.cValues].lpb) );
                memmove(entryList.lpbin[entryList.cValues].lpb, eid, len);
                entryList.cValues++;

                delete [] eid;
                delete [] entryId;

                hr = pfldrOutbox->DeleteMessages (&entryList, 0, NULL, 0);
                EXIT_ON_FAILED (hr);
                bFound = true;

                break;
            }
        } // if

        // Free pRows for each row
        FreeProws(pRows);
        pRows = NULL;
    } // while

FuncExit:
    if(pRows) {
        // Free each element in the pRows buffer
        FreeProws(pRows);
        pRows = NULL;
    }
    RELEASE_OBJ(pContentsTable);

    if (msgID)  {
        delete [] msgID; msgID = NULL;
    }

    return bFound;
}


wchar_t* MailServer2Client::doesMessageExist (IN const char* msgid, LPMAPIFOLDER pfldrInbox, wchar_t folder) {

    if (msgid == NULL)
        return false;

    wchar_t* msgID = toWideChar(msgid);
    HRESULT hr = S_OK;
    wchar_t* ret = NULL;
    SBinary    sbEntry = {0};
    LPMAPITABLE pContentsTable = NULL;
    LPSRowSet pRows = NULL;
    SRow aRow = {0};

    enum {
        ePR_ENTRYID,            // 0
        ePR_CUSTOM_MESSAGE_ID,            // 2
        NUM_COLS                // number of columns (put it to the end)
    };

    // These tags represent the message information we would like to pick up
    SizedSPropTagArray(NUM_COLS, Columns) =
    {
        NUM_COLS,
        PR_ENTRYID,
        PR_CUSTOM_MESSAGE_ID
    };

    hr = pfldrInbox->GetContentsTable(0, &pContentsTable);
    EXIT_ON_FAILED(hr);

    hr = pContentsTable->SetColumns((LPSPropTagArray)&Columns, 0);
    EXIT_ON_FAILED(hr);

    // iterate message one by one (each time only read 1)
    while(!FAILED(hr = pContentsTable->QueryRows(1, 0, &pRows))) {
        // this is the end of the content table
        if(pRows->cRows == 0) break;

        // check this Row
        aRow = pRows->aRow[0];

        // Correctly returned?
        if ((&aRow.lpProps[ePR_ENTRYID])->ulPropTag == PR_ENTRYID
            && (&aRow.lpProps[ePR_CUSTOM_MESSAGE_ID])->ulPropTag == PR_CUSTOM_MESSAGE_ID) {

            // Is CUSTOM_MESSAGE_ID found?
            wchar_t* mm = aRow.lpProps[ePR_CUSTOM_MESSAGE_ID].Value.lpszW;
            if (wcscmp(mm, msgID) == 0) {
                ret = convertBinaryToWChar(aRow.lpProps[ePR_ENTRYID].Value.bin, folder);
                break;
            }
        } // if

        // Free pRows for each row
        FreeProws(pRows);
        pRows = NULL;
    } // while

FuncExit:
    if(pRows) {
        // Free each element in the pRows buffer
        FreeProws(pRows);
        pRows = NULL;
    }
    RELEASE_OBJ(pContentsTable);

    if (msgID)  {
        delete [] msgID; msgID = NULL;
    }

    return ret;
}



HRESULT SetReplyTo(LPMAPISESSION lpSession, IMessage* lpMessage, LPSTR pszReplyTo)
{

    HRESULT hRes = S_OK;
    LPADRLIST lpAddrList = NULL;
    LPADRBOOK lpAddrBook = NULL;
    SPropValue pspvReply[2];
    LPFLATENTRYLIST lpReplyEntryList = NULL;
    LPFLATENTRY lpReplyEntry = NULL;
    int cValues = 2;
    ULONG i = 0;

    // Allocate an address list structure.
    int cb = CbNewADRLIST(1);

    hRes = MAPIAllocateBuffer(cb, (LPVOID *)&lpAddrList);
    if (FAILED(hRes)) goto Cleanup;

    ZeroMemory(lpAddrList, cb);

    hRes = MAPIAllocateBuffer(
        cValues * sizeof(SPropValue),
        (LPVOID FAR *)&lpAddrList->aEntries[0].rgPropVals);
    if (FAILED(hRes)) goto Cleanup;

    // Open the address book to resolve Reply To recipient.
    hRes = lpSession->OpenAddressBook(0, NULL, 0, &lpAddrBook);
    if (FAILED(hRes)) goto Cleanup;

    // Set up address list structure.
    lpAddrList->cEntries = 1;
    lpAddrList->aEntries[0].rgPropVals[0].ulPropTag = PR_DISPLAY_NAME;
    lpAddrList->aEntries[0].rgPropVals[0].Value.lpszA = pszReplyTo;

    lpAddrList->aEntries[0].rgPropVals[1].ulPropTag = PR_RECIPIENT_TYPE;
    lpAddrList->aEntries[0].rgPropVals[1].Value.l = MAPI_TO;
    lpAddrList->aEntries[0].cValues = cValues;

    // Resolve name.
    hRes = lpAddrBook->ResolveName(0, 0, NULL, lpAddrList);
    if (FAILED(hRes)) goto Cleanup;

    // Get the Entry ID.
    for (i = 0; i < lpAddrList->aEntries[0].cValues; i++)
    {
        if (lpAddrList->aEntries[0].rgPropVals[i].ulPropTag == PR_ENTRYID)
        {
            // Allocate a new FLATENTRY structure for
            // the PR_REPLY_RECIPIENT_ENTRIES property.
            cb = CbNewFLATENTRY(
                   lpAddrList->aEntries[0].rgPropVals[i].Value.bin.cb);
            hRes = MAPIAllocateBuffer(cb, (LPVOID *)&lpReplyEntry);
            if (FAILED(hRes)) goto Cleanup;
            ZeroMemory((VOID *)lpReplyEntry, cb);

            // Copy the bits of the Entry ID into the FLATENTRY structure.
            CopyMemory(lpReplyEntry->abEntry,
                       lpAddrList->aEntries[0].rgPropVals[i].Value.bin.lpb,
                       lpAddrList->aEntries[0].rgPropVals[i].Value.bin.cb);
            lpReplyEntry->cb = lpAddrList->
                                 aEntries[0].rgPropVals[i].Value.bin.cb;

 

⌨️ 快捷键说明

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