/****************************************************************************** FILENAME: Ath16imp.cpp MODULE: Import Athena 16 folders PURPOSE: Contains routines for initialization,deinitialization, geting list of folders, releasing folder list, and importing folder. FUNCTIONS: WIN95 Win32 functions: HRESULT AthGetFolderList(HWND hwnd, IMPFOLDERNODE **pplist); HRESULT AthInit(HWND hwnd); void AthDeinit(); void AthFreeFolderList(IMPFOLDERNODE *pnode); HRESULT AthImportFolder(HWND hwnd, HANDLE hnd, LPARAM lparam); HRESULT GetAthInstallPath(LPTSTR &szInstallPath); HRESULT GetAthSubFolderList(LPTSTR szInstallPath, IMPFOLDERNODE **ppList, IMPFOLDERNODE *); void GetNewRecurseFolder(LPTSTR szInstallPath, LPTSTR szDir, LPTSTR szInstallNewPath); HRESULT ProcessMessages(HANDLE hnd,LPTSTR szFileName); HANDLE OpenMsgFile(LPTSTR szFileName); long GetMessageCount(HANDLE hFile); HRESULT ProcessMsgList(HANDLE hnd,HANDLE hFile,LPTSTR szPath); HRESULT ParseMsgBuffer(LPTSTR szmsgbuffer,LPTSTR szPath,HANDLE hnd); HRESULT GetMsgFileName(LPTSTR szmsgbuffer,BOOL Flag,LPTSTR *szfilename); HRESULT GetDateBuffer(LPTSTR szmsgbuffer,TCHAR *szsendDate, TCHAR *szrecvDate); HRESULT GetFileinBuffer(HANDLE hnd,LPTSTR *szBuffer); void CopyStringA1(TOKEN *msgToken); HRESULT ProcessSingleMessage(HANDLE hnd,LPTSTR szBuffer,BOOL Flag, IMSG *imsg); HRESULT ProcessTokens(TOKEN *msgToken,IMSG *imsg,HANDLE hnd, LPTSTR szBuffer); HRESULT FillPriority(IMSG *imsg,TOKEN *msgToken,ULONG counter, LPTSTR szBuffer); void AthTimeParse(TCHAR * szBuffer1,IMSG *imsg); HRESULT MessageAttachA(IMSG *imsg,TOKEN *msgToken,TCHAR *szboundary, int tokcount,TCHAR *szBuffer1); void AthGetTimeBuffer(TCHAR * szBuffer, IMSG *imsg); ******************************************************************************/ /** include files **/ #include "pch.hxx" #include "string.h" #include "stdio.h" #include "impapi.h" #include "imnapi.h" #include "comconv.h" #include "ath16imp.h" #include "mapi.h" #include "mapix.h" #include "error.h" #include "commdlg.h" #include "import.h" /****************************************************************************** * FUNCTION NAME:AthGetFolderList * * PURPOSE:To Get the Athena16 Folder List * * PARAMETERS: * * IN: handle to the parent window * * OUT: Node in which the first folder will be returned * * RETURNS: HRESULT ******************************************************************************/ HRESULT AthGetFolderList(HWND hwnd, IMPFOLDERNODE **pplist) { HRESULT hr=S_OK; TCHAR szInstallPath[MAX_FILE_PATH]; IMPFOLDERNODE *plist=NULL; hr = GetAthInstallPath(hwnd,szInstallPath); if(hr==hrInvalidFolderName) return(hrInvalidFolderName); if(S_OK!=(hr=GetAthSubFolderList(szInstallPath, &plist, NULL))) return(hr); if(NULL==plist) hr=hrReadFile; *pplist = plist; return hr; } /****************************************************************************** * FUNCTION NAME:AthFreeFolderList * * PURPOSE:To free the Folders List structure * * PARAMETERS: * * IN: First folder node * OUT: * * RETURNS: HRESULT ******************************************************************************/ void AthFreeFolderList(IMPFOLDERNODE *pnode) { if (pnode->pchild != NULL) AthFreeFolderList(pnode->pchild); if (pnode->pnext != NULL) AthFreeFolderList(pnode->pnext); if (pnode->szName != NULL) delete(pnode->szName); if (pnode->lparam != NULL) delete [] (LPTSTR)pnode->lparam; delete(pnode); } /****************************************************************************** * FUNCTION NAME:AthImportFolder * * PURPOSE:Starts the process of importing the selected athena16 folder * * PARAMETERS: * * IN: Handle to the window,handle and lparam which stores info about * path of the selected folder * * OUT: * * RETURNS: HRESULT ******************************************************************************/ HRESULT AthImportFolder(HWND hwnd, HANDLE hnd, LPARAM lparam) { HRESULT hr=S_OK; hr=ProcessMessages(hnd,(LPTSTR)lparam); return hr; } /****************************************************************************** * FUNCTION NAME:AthInit * * PURPOSE:Initialization * * PARAMETERS: * * IN: Handle to the window * * OUT: * * RETURNS: HRESULT ******************************************************************************/ HRESULT AthInit(HWND hwnd) { HRESULT hr=S_OK; return hr; } /****************************************************************************** * FUNCTION NAME:AthDeinit * * PURPOSE:Deinitialization . * * PARAMETERS: * * IN: * * OUT: * RETURNS: HRESULT ******************************************************************************/ void AthDeinit() { return; } /****************************************************************************** * FUNCTION NAME:GetAthInstallPath * * PURPOSE:To Get the Athena16 Installation path * * PARAMETERS: * * IN: * * OUT: Installation path * * RETURNS: HRESULT ******************************************************************************/ HRESULT GetAthInstallPath(HWND hwnd,LPTSTR szInstallPath) { HRESULT hr=E_FAIL; hr = DispDialog(hwnd,szInstallPath); if(hr==hrInvalidFolderName) return(hr); lstrcat(szInstallPath,"\\folders"); return S_OK; } /****************************************************************************** * FUNCTION NAME:GetAthSubFolderList * * PURPOSE:To Get the Athena16 Folders List * * PARAMETERS: * * IN: Installation path of Athena16 mail directory, Parent node * * OUT: Node in which the first folder will be returned * * RETURNS: HRESULT ******************************************************************************/ HRESULT GetAthSubFolderList(LPTSTR szInstallPath, IMPFOLDERNODE **ppList, IMPFOLDERNODE *pParent) { HRESULT hr= S_OK; IMPFOLDERNODE *pNode=NULL, *pNew=NULL, *pLast=NULL; IMPFOLDERNODE *pPrevious=NULL; IMPFOLDERNODE *ptemp=NULL; BOOL Flag=TRUE; BOOL child=TRUE; TCHAR szInstallPathNew[MAX_FILE_PATH]; TCHAR szInstallPathCur[MAX_FILE_PATH]; LPTSTR szT=NULL; WIN32_FIND_DATA fFindData; HANDLE hnd=NULL; GetNewRecurseFolder(szInstallPath, "\\*", szInstallPathCur); hnd = FindFirstFile(szInstallPathCur, &fFindData); if (hnd == INVALID_HANDLE_VALUE) return(E_FAIL); do { if((fFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { if(!lstrcmpi(fFindData.cFileName, ".") || !lstrcmpi(fFindData.cFileName, "..")) continue; pNew = new IMPFOLDERNODE; if(!pNew) return E_FAIL; ZeroMemory(pNew, sizeof(IMPFOLDERNODE)); pNew->szName = (TCHAR *)new TCHAR[MAX_FILE_PATH]; if(!pNew->szName) { hr=hrMemory; goto error; } lstrcpy(pNew->szName, fFindData.cFileName); szT = (LPTSTR)new TCHAR[MAX_FILE_PATH]; if(!szT) { hr=hrMemory; goto error; } GetNewRecurseFolder(szInstallPath, fFindData.cFileName, szInstallPathNew); lstrcpy(szT, szInstallPathNew); pNew->lparam = (long)szT; pNew->pparent= pParent; pNew->depth = (pParent != NULL) ? pParent->depth + 1 : 0; if(pNode == NULL) pNode = pNew; pLast = pNew; if(Flag) pPrevious=pNew; else { if(pPrevious) { pPrevious->pnext=pNew; pPrevious=pNew; } } if(child) { if(pParent) pParent->pchild=pNew; child=FALSE; } GetAthSubFolderList(szInstallPathNew, &pNew->pchild,pNew); Flag = FALSE; } }while(FindNextFile(hnd, &fFindData)); *ppList = pNode; if(hnd) FindClose(hnd); hnd=NULL; return hr; error: Freetempbuffer(pNew->szName); Freetempbuffer(szT); if(pNew) delete(pNew); pNew=NULL; return(hr); } /****************************************************************************** * FUNCTION NAME:GetNewRecurseFolder * * PURPOSE:To Get the path of the next level of the Folders. * * PARAMETERS: * * IN: Current directory path,szDir * * OUT: Next level directory path * * RETURNS: void ******************************************************************************/ void GetNewRecurseFolder(LPTSTR szInstallPath, LPTSTR szDir, LPTSTR szInstallNewPath) { lstrcpy(szInstallNewPath, szInstallPath); lstrcat(szInstallNewPath, "\\"); lstrcat(szInstallNewPath, szDir); } /****************************************************************************** * FUNCTION NAME:ProcessMessages * * PURPOSE:Starts processing messages within a folder * * PARAMETERS: * * IN: Handle, folder path * * OUT: * * RETURNS: HRESULT ******************************************************************************/ HRESULT ProcessMessages(HANDLE hnd,LPTSTR szFileName) { HANDLE hFile=NULL; long uCount=0; long i=0; HRESULT hr=0; TCHAR szpath[MAX_FILE_PATH]; ULONG cError=0; lstrcpy(szpath,szFileName); lstrcat(szFileName,"\\msg_list"); if(INVALID_HANDLE_VALUE ==(hFile = OpenMsgFile(szFileName))) { MessageBox(NULL,"Message File Not Present","Message Folder Could not be imported",MB_OK); goto Error; } uCount = GetMessageCount(hFile); if(!uCount) { MessageBox(NULL,"No messages to import","Message Folder Could not be imported",MB_OK); return(hrNoMessages); } for(i=0;i=ul) return(hrReadFile); //error corrupt msgheader lstrcpyn(szsendDate,&szmsgbuffer[counter+2],28); lstrcpyn(szrecvDate,&szmsgbuffer[counter+30],28); return(S_OK); } /****************************************************************************** * FUNCTION NAME:GetFileinBuffer * * PURPOSE:To Get the message file in a buffer. * * PARAMETERS: * * IN: Handle of the message file. * * OUT: Buffer comatining the message. * * RETURNS: HRESULT ******************************************************************************/ HRESULT GetFileinBuffer(HANDLE hnd,LPTSTR *szBuffer) { ULONG ulFileSize; ULONG ulRead; HRESULT hResult=0; ulFileSize = GetFileSize(hnd, NULL); if(!ulFileSize) goto Error; *szBuffer = new TCHAR[ulFileSize+1]; if(!ReadFile(hnd, (LPVOID)*szBuffer,ulFileSize,&ulRead,NULL)) { hResult=hrReadFile; goto Error; } (*szBuffer)[ulFileSize]='\0'; Error: return(hResult); } /****************************************************************************** * FUNCTION NAME:ProcessSingleMessage * * PURPOSE:Processes individual messages * * PARAMETERS: * * IN: Handle,Buffer containing message * * OUT: Node in which the first folder will be returned * * RETURNS: HRESULT ******************************************************************************/ HRESULT ProcessSingleMessage(HANDLE hnd,LPTSTR szBuffer,IMSG *imsg) { TOKEN msgToken[19]; ZeroMemory(msgToken,sizeof(TOKEN)*19); ULONG uTokens=19; ULONG i=0; ULONG uMsgSize=lstrlen(szBuffer); HRESULT hResult=0; CopyStringA1(msgToken); for(ULONG j=0;j= (ULONG)lstrlen(szBuffer)) //error:MessageBody Could not be distinguished return(hrCorruptMessage); ArrangePointers(msgToken,uTokens); for(i=0;strcmp(msgToken[i].cType,"MsgBody")!=0;i++); if(i!=uTokens-1) { for(i=i+1;iwPriority=PRI_NORMAL; else { if('U'==msgToken[counter].cAddress[10]) imsg->wPriority = PRI_HIGH; if('N'==msgToken[counter].cAddress[10]) imsg->wPriority = PRI_LOW; } return(S_OK); } /****************************************************************************** * FUNCTION NAME:MessageAttachA * * PURPOSE:Parse messages and attachments * * PARAMETERS: * * IN:pointer msgtoken,boundary,counter,buffer conatining message * * OUT: Imsg * * RETURNS: HRESULT ******************************************************************************/ HRESULT MessageAttachA(IMSG *imsg,TOKEN * msgToken,TCHAR *szboundary,int i,TCHAR *szBuffer1) { HRESULT hr=0; TCHAR *szBuff=NULL; TCHAR szfilename[_MAX_PATH]; ULONG AttachCount=0; ULONG ccount=0; ULONG sizebuff; TCHAR *dummy1=NULL,*dummy2=NULL,*dummy3=NULL, *dummy4=NULL,*temp=NULL,*temp1=NULL; ULONG flag; //to check whether message body is present or not ULONG cpoint=0; BOOL embeddmessage=FALSE; ULONG tcount=0; TCHAR *szattachbuffer=NULL; if(szboundary == NULL) { sizebuff = lstrlen(szBuffer1)-(msgToken[i].cAddress-szBuffer1+2); // 2 since messagebody points to blank line if((szBuff = new TCHAR[sizebuff+1])==0) return(hrMemory); for( ccount=0;ccount < sizebuff;ccount++) szBuff[ccount] = msgToken[i].cAddress[ccount+2]; szBuff[ccount] = '\0'; } else { dummy1= strstr(msgToken[i].cAddress,szboundary); if(dummy1==NULL) { hr=hrCorruptMessage; goto error; } dummy1=dummy1+lstrlen(szboundary)+2; dummy2=strstr(dummy1,szboundary); if(dummy2==NULL) { hr=hrCorruptMessage; goto error; } if((temp1=strstr(dummy1,"Content-Type:"))==NULL) { hr=hrCorruptMessage; goto error; } while(cpoint < (ULONG)lstrlen(dummy1)) { if(dummy1[cpoint]==13 && dummy1[cpoint+1]==10 ) { dummy3= &dummy1[cpoint]+2; break; } for(;(!(dummy1[cpoint]==13 && dummy1[cpoint+1]==10 )&&cpoint<=(ULONG)lstrlen(dummy1));cpoint++); cpoint+=2; } if(cpoint > (ULONG)lstrlen(dummy1)) { hr=hrCorruptMessage; goto error; } if((strstr(temp1,"name=")!=NULL) && (strstr(temp1,"name=")< dummy3)) { flag=1; sizebuff=1; } else { flag=0; sizebuff=dummy2- dummy3-2; //2 for 13 and 10 (3 new lines) } if((szBuff = new TCHAR[sizebuff+1])==0) { hr=hrMemory; goto error; } if(sizebuff==1) lstrcpy(szBuff,""); else { for( ccount=0;ccount < sizebuff;ccount++) szBuff[ccount] = dummy3[ccount]; szBuff[ccount] = '\0'; } //to get attachmentcount temp=dummy2; if(szboundary!=NULL) { if (flag==0) dummy1=dummy2+lstrlen(szboundary)+2; while((dummy2=strstr(dummy1,szboundary))!=NULL) { AttachCount++; dummy1=dummy2+lstrlen(szboundary)+2; } } //parsing attachments dummy2=temp; //allocate imsg->Iattach memory imsg->cAttach = AttachCount; if (AttachCount==0) { imsg->lpIatt = NULL; if((hr=MessageBodytoImsg(szBuff,imsg))!=S_OK) goto error; else { hr=S_OK; goto error; } } if((imsg->lpIatt = new IATTINFO [sizeof(IATTINFO)*AttachCount])==0) { Freetempbuffer(szBuff); return(hrMemory); } ZeroMemory(imsg->lpIatt,sizeof(IATTINFO)*AttachCount); for(ccount=0;ccountlpIatt[ccount].fError = TRUE; //just to make sure that if attachment error occurs there should not be any problem. ULONG j=0; if(szboundary !=NULL && dummy2 != NULL) { dummy2=dummy2+lstrlen(szboundary)+2; while((dummy3 = strstr(dummy2,szboundary))!=NULL && j dummy3) { dummy4=strstr(dummy2,"message/rfc822"); if(dummy4==NULL||dummy4 > dummy3) { j++; dummy2=dummy3+lstrlen(szboundary)+4; imsg->lpIatt[j-1].fError = TRUE; continue; } else embeddmessage=TRUE; } if(!(embeddmessage)) { dummy4=dummy4+lstrlen("name=")+1; for(ccount=0;ccount <(ULONG)lstrlen(dummy4);ccount++) { if(dummy4[ccount]=='"' ) break; szfilename[ccount]=dummy4[ccount]; } if(dummy4[ccount]!='"' ) { j++; dummy2=dummy3+lstrlen(szboundary)+4; imsg->lpIatt[j-1].fError = TRUE; continue; //error } szfilename[ccount]='\0'; } else { // dummy4=dummy4+lstrlen("message/rfc822")+2; dummy4=dummy4+16; szfilename[0]='\0'; } cpoint=0; while(cpoint < (ULONG)lstrlen(dummy4)) { if(dummy4[cpoint]==13 && dummy4[cpoint+1]==10 ) { dummy4= &dummy4[cpoint]+2; break; } for(;(!(dummy4[cpoint]==13 && dummy4[cpoint+1]==10 )&&cpoint<= (ULONG)lstrlen(dummy4));cpoint++); cpoint+=2; } //convert to istream if(dummy3lpIatt[j].fError = TRUE; hr=hrCorruptMessage; goto error; } tcount = dummy3-dummy4-8; if((szattachbuffer = new TCHAR[tcount+1])==0) { hr=hrMemory; goto error; } for(ccount=0;ccount