📄 account.cpp
字号:
/*
* Copyright (C) 2003-2007 Funambol, Inc
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*/
/***********************************************************************
* Email account handling functions
***********************************************************************/
#include <windows.h>
#include <stdio.h>
#include <cfgmgrapi.h>
#include "pim/account.h"
#include "base/Log.h"
#include "customization.h"
#define EXIT_ON_FAILED(_hr) \
if (FAILED(_hr)) { \
goto FuncExit; \
}
#ifndef RELEASE_OBJ
#define RELEASE_OBJ(s) \
if (s != NULL) \
{ \
s->Release(); \
s = NULL; \
}
#endif //RELEASE_OBJ
// #define ACCOUNT_KEY L"F9043C85-F6F2-101A-A3C9-08002B2F49FB" // no more used
#define ACCOUNT_QUERY L"<wap-provisioningdoc>\
<characteristic type=\"EMAIL2\">\
<characteristic-query type=\"%s\"/>\
</characteristic>\
</wap-provisioningdoc>"
#define MODIFY_ACCOUNT L"<wap-provisioningdoc>\
<characteristic type=\"EMAIL2\">\
<characteristic type=\"%s\">\
<parm name=\"REPLYADDR\" value=\"%s\"/>\
<parm name=\"NAME\" value=\"%s\"/>\
</characteristic>\
</characteristic>\
</wap-provisioningdoc>"
/*
* Modify visible name and email address on Funambol Account
*/
HRESULT modifyAccount(const wchar_t* name, const wchar_t* replyAddress) {
LPWSTR out = NULL;
wchar_t* guid = getFunambolGUID();
wchar_t account[512];
wsprintf(account, MODIFY_ACCOUNT, guid, replyAddress, name);
HRESULT ret = DMProcessConfigXML(account, CFGFLAG_PROCESS, &out);
if (out) delete [] out;
if (guid) { delete [] guid; guid = NULL; }
return ret;
}
static HRESULT getParameter(const wchar_t *xml, const wchar_t *parname, wchar_t *value)
{
wchar_t pname[ACCOUNT_PARAM_SIZE];
wsprintf(pname, L"\"%s\"", parname);
const wchar_t VAL[] = L"value=\"";
wchar_t *p1 = wcsstr(xml, pname);
if(!p1){
LOG.error("getAccountInfo: param '%s' not found.", parname);
return -1;
}
p1 = wcsstr(p1, VAL);
if(!pname){
LOG.error("getAccountInfo: error parsing '%s'.", parname);
return -1;
}
p1 += wcslen(VAL);
wchar_t *end = wcsstr(p1, L"\"");
if(!end){
LOG.error("getAccountInfo: error parsing '%s'.", parname);
return -1;
}
size_t len = end-p1;
if(len > ACCOUNT_PARAM_SIZE-1){
LOG.info("getAccountInfo: '%s' truncated.", parname);
len = ACCOUNT_PARAM_SIZE-1;
}
wcsncpy(value, p1, len);
value[len]=0;
return 0;
}
HRESULT getAccountInfo(wchar_t* name, wchar_t* replyaddress) {
return getAccountInfoSession(name, replyaddress, NULL);
}
BOOL doesFunambolAccountExist() {
wchar_t* guid = getFunambolGUID(NULL);
if (guid == NULL) // funambol account not found
return FALSE;
else {
delete [] guid;
return TRUE;
}
}
/*
* Get Funambol account information
*/
HRESULT getAccountInfoSession(wchar_t* name, wchar_t* replyaddress, IMAPISession* pSession)
{
LPWSTR out = NULL;
wchar_t* guid = getFunambolGUID(pSession);
if (guid == NULL) // funambol account not found
return -1;
wchar_t account[512];
wsprintf(account, ACCOUNT_QUERY, guid);
HRESULT ret = DMProcessConfigXML(account, CFGFLAG_PROCESS, &out);
if (ret) {
LOG.error("getAccountInfo: error %s in DMProcessConfigXML.", ret);
return ret;
}
if (!out) {
LOG.error("getAccountInfo: DMProcessConfigXML returned null.");
return -1;
}
#if 0
//////////////////////////////////////////DEBUG
FILE *f = fopen("/AccountLog.txt", "w");
if(f){
fprintf(f, "%ls", out);
fclose(f);
}
else{
LOG.error("Error opening file");
}
//////////////////////////////////////////DEBUG
#endif
// Get Name ----------------------------------------------
ret = getParameter(out, L"NAME", name);
if (ret){
goto finally;
}
// Get Address -------------------------------------------
ret = getParameter(out, L"REPLYADDR", replyaddress);
if(ret){
goto finally;
}
finally:
if (guid) {delete [] guid; guid = NULL; }
delete [] out;
return ret;
}
enum {
ePR_ENTRYID,
ePR_DISPLAY_NAME,
ePR_CE_UNIQUE_STORE_ID,
NUM_COLS
};
// These tags represent the message information we would like to pick up
static SizedSPropTagArray(NUM_COLS, Columns) =
{
NUM_COLS,
PR_ENTRYID,
PR_DISPLAY_NAME,
PR_CE_UNIQUE_STORE_ID
};
/*
* It always return a valid GUID. At the beginning it look for the existing one created in the prorammatically
* creation of the account. If non exists, maybe because the user deleted the old and created a new one manually,
* the procedure set a new GUID and return it to delete the account properly.
* the same GUID is {A3D64F8C-1BDC-4ac6-92A4-4D85213F96CD}.
* In the same way there is a similar procedure written in setup.cpp to always modify the account.
* If the account was generated manually, this same guid is inserted.
*/
wchar_t* getFunambolGUID(IMAPISession* pSession) {
HRESULT hr;
IMAPITable* pTable = NULL;
SRowSet* psrs = NULL;
IMsgStore* pStore = NULL;
BOOL accountFound = FALSE;
wchar_t* ret = NULL;
BOOL isSessionCreated = FALSE;
if (!pSession) {
isSessionCreated = TRUE;
hr = MAPILogonEx (NULL, NULL, NULL, NULL, &pSession);
EXIT_ON_FAILED (hr);
}
// Get the message stores table
hr = pSession->GetMsgStoresTable (MAPI_UNICODE, &pTable);
EXIT_ON_FAILED (hr);
hr = pTable->SetColumns((LPSPropTagArray)&Columns, 0);
EXIT_ON_FAILED(hr);
while (1) {
// Get a row
hr = pTable->QueryRows (1, 0, &psrs);
EXIT_ON_FAILED (hr);
// Did we hit the end of the table?
if (psrs->cRows != 1) {
break;
}
if (psrs->aRow[0].lpProps[ePR_DISPLAY_NAME].ulPropTag == PR_DISPLAY_NAME) {
wchar_t* displayName = psrs->aRow[0].lpProps[ePR_DISPLAY_NAME].Value.lpszW;
//MessageBox (NULL, displayName, TEXT ("Debug eVC"), MB_SETFOREGROUND |MB_OK);
if (wcscmp(displayName, PROVIDER_W) == 0) {
accountFound = TRUE;
// Now we got the GUID
LPGUID pGuid = NULL;
ret = new wchar_t[64];
memset(ret, 0, 64*sizeof(wchar_t));
if (psrs->aRow[0].lpProps[ePR_CE_UNIQUE_STORE_ID].ulPropTag == PR_CE_UNIQUE_STORE_ID) {
//MessageBox (NULL, TEXT("GUID ok"), TEXT ("Debug eVC"), MB_SETFOREGROUND |MB_OK);
pGuid = psrs->aRow[0].lpProps[ePR_CE_UNIQUE_STORE_ID].Value.lpguid;
}
else {
//MessageBox (NULL, TEXT("GUID NULL"), TEXT ("Debug eVC"), MB_SETFOREGROUND |MB_OK);
// 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);
SPropValue rgprops[1] = {0};
ULONG cProps = 0;
rgprops[cProps].ulPropTag = PR_CE_UNIQUE_STORE_ID;
pGuid = new GUID();
pGuid->Data1 = 0xA3D64F8C;
pGuid->Data2 = 0x1BDC;
pGuid->Data3 = 0x4ac6;
pGuid->Data4[0] = (char)0x92;
pGuid->Data4[1] = (char)0xA4;
pGuid->Data4[2] = (char)0x4D;
pGuid->Data4[3] = (char)0x85;
pGuid->Data4[4] = (char)0x21;
pGuid->Data4[5] = (char)0x3F;
pGuid->Data4[6] = (char)0x96;
pGuid->Data4[7] = (char)0xCD;
rgprops[cProps].Value.lpguid = pGuid;
++cProps;
hr = pStore->SetProps(cProps, rgprops, NULL);
}
wsprintf(ret,
TEXT("{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"),
pGuid->Data1, pGuid->Data2, pGuid->Data3,
pGuid->Data4[0], pGuid->Data4[1],
pGuid->Data4[2], pGuid->Data4[3],
pGuid->Data4[4], pGuid->Data4[5],
pGuid->Data4[6], pGuid->Data4[7]);
//MessageBox (NULL, ret, TEXT ("Debug eVC"), MB_SETFOREGROUND |MB_OK);
}
}
FreeProws (psrs);
psrs = NULL;
RELEASE_OBJ (pStore);
if (accountFound) {
break;
}
}
FuncExit:
FreeProws (psrs);
RELEASE_OBJ (pStore);
RELEASE_OBJ (pTable);
if (isSessionCreated)
RELEASE_OBJ (pSession);
return ret;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -