Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions | Variables
mapiutl.cpp File Reference
#include <windows.h>
#include <time.h>
#include <sys/stat.h>
#include <io.h>
#include "xpapi.h"
#include "trace.h"
#include "mapiipc.h"
#include "mapiutl.h"

Go to the source code of this file.

Defines

#define MAXTRY   9999
#define kMaxTempFiles   10
#define kMaxListLength   (10 * _MAX_PATH)

Functions

void SetLoggingEnabled (BOOL val)
void LogString (LPCSTR pStr1)
HWND GetCommunicatorIPCWindow (void)
void BuildMemName (LPSTR name, ULONG winSeed)
DWORD ValidateFile (LPCSTR szFile)
DWORD SanityCheckAttachmentFiles (lpMapiMessage lpMessage)
DWORD GetFileCount (LPSTR pFiles, LPSTR delimChar)
BOOL ExtractFile (LPSTR pFiles, LPSTR delimChar, DWORD fIndex, LPSTR fName)
ULONG GetFileSize (LPSTR fName)
LPVOID LoadBlobToMemory (LPSTR fName)
LONG WriteMemoryBufferToDisk (LPSTR fName, LONG bufSize, LPSTR buf)
LPSTR GetTheTempDirectoryOnTheSystem (void)
LONG GetTempAttachmentName (LPSTR fName)
int CheckForInlineHTML (char *noteBody, DWORD len, DWORD *curPos, char *newBody, DWORD *realLen)
LPSTR StripSignedMessage (LPSTR noteText, DWORD totalCR)
LPSTR StripHTML (LPSTR noteText)
UINT GetTempMailNameWithExtension (LPSTR szTempFileName, LPSTR origName)
void GetTempFiles (LPSTR pBuf, int lenBuf)
void WriteTempFiles (LPSTR pBuf)
void AddTempFile (LPCSTR pFileName)
void DeleteFirstTempFile (LPSTR pFiles)
void RemoveAllTempFiles (void)
void CheckAgeTempFiles (void)
void CleanupMAPITempFiles (void)
voidCleanMalloc (size_t mallocSize)
void SafeFree (void *ptr)

Variables

BOOL gLoggingEnabled = FALSE

Define Documentation

Definition at line 754 of file mapiutl.cpp.

Definition at line 753 of file mapiutl.cpp.

#define MAXTRY   9999

Definition at line 664 of file mapiutl.cpp.


Function Documentation

void AddTempFile ( LPCSTR  pFileName)

Definition at line 769 of file mapiutl.cpp.

{
  if ( (!pFileName) || (pFileName[0] == '\0') )
    return;

  char *files = (char *)malloc(kMaxListLength);

  if (!files)
    return;

  GetTempFiles(files, kMaxListLength);
  if ((lstrlen(files) + lstrlen(pFileName) + 2) >= kMaxListLength)
  {
    free(files);
    return;
  }

  if (lstrlen(files) != 0)
  {
          lstrcat(files, ";");
  }

  lstrcat(files, pFileName);
  WriteTempFiles(files);
  free(files);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void BuildMemName ( LPSTR  name,
ULONG  winSeed 
)

Definition at line 150 of file mapiutl.cpp.

{
  static DWORD    id = 0;

  if (id == 0)
  {
    // Seed the random-number generator with current time so that
    // the numbers will be different every time we run.
    srand( (unsigned)time( NULL ) );
    id = rand();
  }

  wsprintf(name, "MAPI_IPC_SMEM-%d", (winSeed + id++));
  TRACE("Shared Memory Name = [%s]\n", name);
}

Definition at line 839 of file mapiutl.cpp.

{
  char *files = (char *)malloc(kMaxListLength);

  if (!files)
    return;

  GetTempFiles(files, kMaxListLength);
  int i = 0;
  LPSTR pTemp = files;
  while (TRUE)
  {
    pTemp = strchr(pTemp, ';');
    if (!pTemp)
      break;
    ++pTemp;
    ++i;
  }

  if (i >= 10)
  {
    DeleteFirstTempFile(files);
    WriteTempFiles(files);
  }

  free(files);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int CheckForInlineHTML ( char *  noteBody,
DWORD  len,
DWORD curPos,
char *  newBody,
DWORD realLen 
)

Definition at line 410 of file mapiutl.cpp.

{
  LPSTR   tags[] = {"&nbsp;", "&lt;", "&amp;", NULL};
  UCHAR   tagsSubst[] = {' ', '<', '&', NULL};
  int     x = 0;

  while (tags[x])
  {
    // should we check for first tag
    if ( (*curPos+strlen(tags[x])) < len)
    {
      if (strncmp(tags[x], noteBody, strlen(tags[x])) == 0)
      {
        *curPos += strlen(tags[x]) - 1;
        newBody[*realLen] = tagsSubst[x];
        *realLen += 1;
        return(-1);
      }
    }

    ++x;
  }

  return(0);
}

Here is the caller graph for this function:

void* CleanMalloc ( size_t  mallocSize)

Definition at line 881 of file mapiutl.cpp.

{
  void *ptr = malloc(mallocSize);
  if (!ptr)
    return(NULL);

  memset(ptr, 0, mallocSize);
  return(ptr);
}

Here is the call graph for this function:

Definition at line 868 of file mapiutl.cpp.

{
  if (Is_16_OR_32_BIT_CommunitorRunning() == 0)
  {
    RemoveAllTempFiles();           // if Communicator not running, clean up all the temp files
  }
  else
  {
    CheckAgeTempFiles();
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 796 of file mapiutl.cpp.

{
  if (!*pFiles)
    return;
  LPSTR pTemp = strchr(pFiles, ';');
  if (pTemp)
  {
    *pTemp = 0;
  }
 
//#ifndef _DEBUG  
  _unlink(pFiles);
//#endif

  if (pTemp)
  {
    memmove(pFiles, pTemp + 1, lstrlen(pTemp + 1) + 1);
  }
  else
  {
    *pFiles = 0;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

BOOL ExtractFile ( LPSTR  pFiles,
LPSTR  delimChar,
DWORD  fIndex,
LPSTR  fName 
)

Definition at line 227 of file mapiutl.cpp.

{
  LPSTR   ptr = pFiles;
  DWORD   loc;
  DWORD   count = 0;

       if ((!pFiles) || (!*pFiles))
              return(0);

  // Get to the fIndex'th entry
  for (loc=0; loc<strlen(pFiles); loc++)
  {
    if (count == fIndex)
      break;
    if (pFiles[loc] == delimChar[0])
      count++;
  }

  if (loc >= strlen(pFiles))    // Got to the end of string!
    return(FALSE);

  lstrcpy(fName, (LPSTR)pFiles + loc);
  //
  // Truncate at 2nd delimiter
  //
  for (DWORD i=0; i<strlen(fName); i++)
  {
    if (fName[i] == delimChar[0])
    {
      fName[i] = '\0';
      break;
    }
  }

  return(TRUE);
}

Here is the caller graph for this function:

Definition at line 104 of file mapiutl.cpp.

{
  HWND  hWnd = NULL; 
  DWORD timeCount = 0;
  BOOL  launchTry = FALSE;

  //
  // This will wait for 10 seconds before giving up and failing
  //
  while ((hWnd == NULL) && (timeCount < 20))
  {
    if ((hWnd = FindWindow("AfxFrameOrView", NULL)) && !FindWindow("aHiddenFrameClass", NULL))
      return(hWnd);
    else if ((hWnd = FindWindow("aHiddenFrameClass", NULL)))
      return(hWnd);

    if (!launchTry)
    {
    char    szPath[_MAX_PATH] = "";
              DWORD   nMAPIERROR; 

      if ((nMAPIERROR = XP_GetInstallLocation(szPath, _MAX_PATH)) != SUCCESS_SUCCESS)
      {
        return(NULL); 
      }
              
                WORD nReturn = XP_CallProcess(szPath, " -MAPICLIENT");
      launchTry = TRUE;
    }

    //
    // Pause for 1/2 a second and try to connect again...
    //
#ifdef WIN32
    Sleep(500);
#else 
    Yield();
#endif        

    timeCount++;
  }

       return(hWnd);
}

Here is the call graph for this function:

Here is the caller graph for this function:

DWORD GetFileCount ( LPSTR  pFiles,
LPSTR  delimChar 
)

Definition at line 205 of file mapiutl.cpp.

{
  DWORD   count = 1;
       if ((!pFiles) || (!*pFiles))
              return(0);

  for (DWORD i=0; i<strlen(pFiles); i++)
  {
    if (pFiles[i] == delimChar[0])
    {
      ++count;
    }
  }

  return(count);
}
ULONG GetFileSize ( LPSTR  fName)

Definition at line 265 of file mapiutl.cpp.

{
  struct _stat buf;
  int result;
  
  result = _stat( fName, &buf );
  if (result != 0)
    return(0);
  
  return(buf.st_size);
}

Here is the caller graph for this function:

Definition at line 376 of file mapiutl.cpp.

{
  UINT        res;
  static UINT uUnique = 1;

  if (!fName)
    return(-1);

  LPSTR szTempPath = GetTheTempDirectoryOnTheSystem();

TRYAGAIN:
#ifdef WIN32
  res = GetTempFileName(szTempPath, "MAPI", uUnique++, fName);
#else
  res = ISGetTempFileName(szTempPath, "MAPI", uUnique++, fName);
#endif

  if (ValidateFile(fName) != 1) 
  {
    if (uUnique < 32000)
    {
      goto TRYAGAIN;
    }
    else
    {
      return(-1);
    }
  }

  return 0;
}

Here is the call graph for this function:

void GetTempFiles ( LPSTR  pBuf,
int  lenBuf 
)

Definition at line 756 of file mapiutl.cpp.

{
  if (!GetConfigInfoStr(szMapiSection, szTempFiles, pBuf, lenBuf, HKEY_ROOT))
  {
    *pBuf = 0;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

UINT GetTempMailNameWithExtension ( LPSTR  szTempFileName,
LPSTR  origName 
)

Definition at line 667 of file mapiutl.cpp.

{
  UINT        res = 1;
  UINT        uUnique = 0;
  char        *szTempPath = GetTheTempDirectoryOnTheSystem();
  char        *tmpPtr;
  char        *realFileName = NULL;

  if ( (origName != NULL) && (*origName != '\0') )
  {
    tmpPtr = origName;
  }
  else
  {
    tmpPtr = szTempFileName;
  }

  realFileName = strrchr(tmpPtr, '\\');
  if (!realFileName)
    realFileName = tmpPtr;
  else
    realFileName++;

TRYAGAIN:

#ifdef WIN32
  if (uUnique == 0)
  {
    wsprintf(szTempFileName, "%s\\%s", szTempPath, realFileName);
  }
  else
  {
    wsprintf(szTempFileName, "%s\\%d_%s", 
                szTempPath, uUnique, realFileName);
  }
#else // WIN16
  if ( (uUnique == 0) && (strlen(realFileName) <= 12) )
  {
    wsprintf(szTempFileName, "%s\\%s", szTempPath, realFileName);
  }
  else
  {              
    if (uUnique < 10)
    {
      GetWin16TempName(realFileName, szTempPath, szTempFileName, uUnique);
    }
    else 
    { 
      res = ISGetTempFileName(szTempPath, "ns", uUnique++, szTempFileName);    
    }

    // Now add the correct extension...
    char *origExt = strrchr(realFileName, '.');
    if (origExt != NULL)
    {
      char *tmpExt = strrchr(szTempFileName, '.');
      if (tmpExt != NULL)
      {
        origExt++;
        tmpExt++;

        while ( ((tmpExt) && (origExt)) && (*origExt != '\0') )
        {
          *tmpExt = *origExt;
          tmpExt++;
          origExt++;
        }

        *tmpExt = '\0';
      }
    }
  }
#endif

  if ( (ValidateFile(szTempFileName) != 1) && (uUnique < MAXTRY) )
  {
    uUnique++;
    if (uUnique >= MAXTRY)
      return(1);
    goto TRYAGAIN;
  }

  return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 345 of file mapiutl.cpp.

{
 static UCHAR retPath[_MAX_PATH];
  
  if (getenv("TEMP"))
  {
    lstrcpy((LPSTR) retPath, getenv("TEMP"));  // environmental variable
  } 
  else if (getenv("TMP"))
  {
    lstrcpy((LPSTR) retPath, getenv("TMP"));  // How about this environmental variable?
  }
  else
  {
    GetWindowsDirectory((LPSTR) retPath, sizeof(retPath));
  }

  return((LPSTR) &(retPath[0]));
}

Here is the caller graph for this function:

Definition at line 278 of file mapiutl.cpp.

{
  UCHAR     *ptr = NULL;
  ULONG     bufSize = GetFileSize(fName);

  if (bufSize == 0)
  {
    _unlink(fName);
    return(NULL);
  }

  ptr = (UCHAR *)malloc( (size_t) bufSize);
  if (!ptr)
  {
    _unlink(fName);
    return(NULL);
  }

  HFILE hFile = _lopen(fName, OF_READ);
  if (hFile == HFILE_ERROR)
  {
    _unlink(fName);
    free(ptr);
    return(NULL);
  }

  UINT numRead = _lread(hFile, ptr, (size_t) bufSize);
  _lclose(hFile);

  if (numRead != bufSize)
  {
    _unlink(fName);
    free(ptr);
    return(NULL);
  }

  _unlink(fName);
       return(ptr);  
}

Here is the call graph for this function:

Here is the caller graph for this function:

void LogString ( LPCSTR  pStr1)

Definition at line 64 of file mapiutl.cpp.

{
  // Off of the declaration line...
  LPCSTR pStr2 = NULL;
  BOOL useStr1 = TRUE;

       if (gLoggingEnabled)
       {
              char tempPath[_MAX_PATH] = "";
    if (getenv("TEMP"))
    {
      lstrcpy((LPSTR) tempPath, getenv("TEMP"));  // environmental variable
    } 

              int len = lstrlen(tempPath);
              if ((len > 1) && tempPath[len - 1] != '\\')
              {
                     lstrcat(tempPath, "\\");
              }
              lstrcat(tempPath, szMapiLog);
              HFILE hFile = _lopen(tempPath, OF_WRITE);
              if (hFile == HFILE_ERROR)
              {
                     hFile = _lcreat(tempPath, 0);
              }
              if (hFile != HFILE_ERROR)
              {
                     _llseek(hFile, 0, SEEK_END);              // seek to the end of the file
                     LPCSTR pTemp = useStr1 ? pStr1 : pStr2;
                     _lwrite(hFile, pTemp, lstrlen(pTemp));
                     _lclose(hFile);
              }
       }
}

Definition at line 821 of file mapiutl.cpp.

{
  char *files = (char *)malloc(kMaxListLength);

  if (!files)
    return;

  GetTempFiles(files, kMaxListLength);

  while (*files)
  {
    DeleteFirstTempFile(files);
  }

  WriteTempFiles(files);
  free(files);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void SafeFree ( void ptr)

Definition at line 892 of file mapiutl.cpp.

{
  if (!ptr)
    return;

  free(ptr);
  ptr = NULL;
}
DWORD SanityCheckAttachmentFiles ( lpMapiMessage  lpMessage)

Definition at line 188 of file mapiutl.cpp.

{
  ULONG i;
  DWORD rc;

  for (i=0; i<lpMessage->nFileCount; i++)
  {
    if ((rc = ValidateFile(lpMessage->lpFiles[i].lpszPathName)) != 0)
    {
      return(rc);
    }
  }

  return(0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 57 of file mapiutl.cpp.

Here is the caller graph for this function:

LPSTR StripHTML ( LPSTR  noteText)

Definition at line 548 of file mapiutl.cpp.

{
  char    *newBuf;
  LPSTR   signTag = "This is a cryptographically signed message in MIME format.";
  LPSTR   mimeTag = "This is a multi-part message in MIME format.";

  DWORD   i;
  DWORD   realLen = 0;
  DWORD   totalCR = 0;

  // do sanity checking...
  if ((!noteText) || (!(*noteText)))
    return(noteText);

  // more sanity checking...
  DWORD len = strlen(noteText) + 1;
  if (len <= 0) 
    return(noteText);

  // Get the number of CR's in this message and add room for
  // the LF's
  for (i=0; i<len; i++)
  {
    if ( (*(noteText + i)) == 0x0D )
      ++totalCR;
  }

  // This is a check for a signed message in the start of a message
  // check for sign line...    
  if ( strlen(signTag) < len)
  {
    if ( 
        (strncmp(signTag, noteText, strlen(signTag)) == 0) ||
        (strncmp(mimeTag, noteText, strlen(mimeTag)) == 0) 
       )
    {
      return( StripSignedMessage(noteText, totalCR) );
    }
  }

  // create a new buffer...
  newBuf = (char *) malloc((size_t)(len + totalCR));
  if (!newBuf)
    return(noteText);

  newBuf[0] = '\0';

  BOOL    firstChar = FALSE;

  // Now do the translation for the body of the note...
  for (i=0; i<len; i++)
  {
    char  *ptr = (noteText + i);
    
    if ( ((*ptr == 0x0D) || (*ptr == 0x20)) && (!firstChar) )
      continue;
    else
      firstChar = TRUE;

    if (CheckForInlineHTML(ptr, len, &i, newBuf, &realLen))
      continue;

    newBuf[realLen++] = *ptr;
    if ( *ptr == 0x0D )
    {
      newBuf[realLen++] = 0x0A;
    }
  }

  // terminate the buffer - reallocate and move on...
  newBuf[realLen++] = '\0';
  newBuf = (LPSTR) realloc(newBuf, (size_t) realLen);

  // check if the realloc worked and if so, free old memory and
  // return...if not, just return the original buffer
  if (!newBuf)
  {
    return(noteText);
  }
  else
  {
    free(noteText);
    return(newBuf);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

LPSTR StripSignedMessage ( LPSTR  noteText,
DWORD  totalCR 
)

Definition at line 440 of file mapiutl.cpp.

{
char    *newBuf;
LPSTR   startTag = "<HTML>";
LPSTR   endTag = "/HTML>";
DWORD   i;
DWORD   realLen = 0;
DWORD   startPos = 0;
DWORD   len = strlen(noteText);

  // create a new buffer...
  newBuf = (char *) malloc((size_t)(len + totalCR));
  if (!newBuf)
    return(noteText);

  newBuf[0] = '\0';

  // First, find the start of the HTML for the message...
  for (i=0; i<len; i++)
  {
    // should we check for first tag
    if ( (i+strlen(startTag)) < len)
    {
      if (strncmp(startTag, (noteText + i), strlen(startTag)) == 0)
      {
        startPos = i + strlen(startTag);
        break;
      }
    }
  }

  // Didn't find any HTML start tag
  if (i == len)
    return(noteText);

  BOOL    inHTML = FALSE;
  BOOL    firstChar = FALSE;

  for (i=startPos; i<len; i++)
  {
    char  *ptr = (noteText + i);

    if ( ((*ptr == 0x0D) || (*ptr == 0x20)) && (!firstChar) )
      continue;
    else
      firstChar = TRUE;

    // First, check for the end /HTML> tag
    if ( (i+strlen(endTag)) < len)
    {
      if (strncmp(endTag, ptr, strlen(endTag)) == 0)
      {
        break;
      }
    }

    // If we are in HTML, check for a ">"...
    if (inHTML)
    {
      if (*ptr == '>')
      {
        inHTML = FALSE;
      }
      continue;
    }

    // Check for NEW HTML...
    if (*ptr == '<')
    {
      inHTML = TRUE;
      continue;
    }

    if (CheckForInlineHTML(ptr, len, &i, newBuf, &realLen))
      continue;

    newBuf[realLen++] = *ptr;
    // Tack on a line feed if we hit a CR...
    if ( *ptr == 0x0D )
    {
      newBuf[realLen++] = 0x0A;
    }
  }

  // terminate the buffer - reallocate and move on...
  newBuf[realLen++] = '\0';
  newBuf = (LPSTR) realloc(newBuf, (size_t) realLen);

  // check if the realloc worked and if so, free old memory and
  // return...if not, just return the original buffer
  if (!newBuf)
  {
    return(noteText);
  }
  else
  {
    free(noteText);
    return(newBuf);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

DWORD ValidateFile ( LPCSTR  szFile)

Definition at line 167 of file mapiutl.cpp.

{
       struct _stat buf;
    int result;

    result = _stat( szFile, &buf );
    if (result != 0)
      return(1);

    if (!(buf.st_mode & S_IREAD))
      return(2);

    return(0);
}
LONG WriteMemoryBufferToDisk ( LPSTR  fName,
LONG  bufSize,
LPSTR  buf 
)

Definition at line 319 of file mapiutl.cpp.

{
  if (!buf)
  {
    return(-1);
  }

  HFILE hFile = _lcreat(fName, 0);
  if (hFile == HFILE_ERROR)
  {
    return(-1);
  }

  LONG writeCount = _lwrite(hFile, buf, (size_t) bufSize);
  _lclose(hFile);

  if (writeCount != bufSize)
  {
    _unlink(fName);
    return(-1);
  }

  return(0);
}

Here is the caller graph for this function:

Definition at line 764 of file mapiutl.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 54 of file mapiutl.cpp.