// not UNIX

#define NO_TEST_

#include "stdafx.h"
#include "Strawberry.h"

#include <mapi.h>
#include "smapi.h"

#define szMAPIDLL  _T("MAPI32.DLL")

HINSTANCE hlibMAPI = 0;

LPMAPILOGON lpfnMAPILogon = NULL;
LPMAPILOGOFF lpfnMAPILogoff = NULL;
LPMAPISENDMAIL lpfnMAPISendMail = NULL;
LPMAPISENDDOCUMENTS lpfnMAPISendDocuments = NULL;
LPMAPIFINDNEXT lpfnMAPIFindNext = NULL;
LPMAPIREADMAIL lpfnMAPIReadMail = NULL;
LPMAPISAVEMAIL lpfnMAPISaveMail = NULL;
LPMAPIDELETEMAIL lpfnMAPIDeleteMail = NULL;
LPMAPIFREEBUFFER lpfnMAPIFreeBuffer = NULL;
LPMAPIADDRESS lpfnMAPIAddress = NULL;
LPMAPIDETAILS lpfnMAPIDetails = NULL;
LPMAPIRESOLVENAME lpfnMAPIResolveName = NULL;


static LHANDLE lhSession = 0L;


// - - - - P R O T O T Y P E S - - - - 
BOOL OK();
BOOL SMAPIInstalled(void);
int PrepareMAPI();

int LogON(LPSTR name, LPSTR pass)
{
  ULONG ulResult;
  int res; 
  
  res=PrepareMAPI();

  if (res!=1) return res;

 //  VERIFY(OK());
 // if (!OK()) return -1;

  ulResult = lpfnMAPILogon(0, name, pass, MAPI_NEW_SESSION, 0, &lhSession);
  
  #ifdef _DEBUG
     if (ulResult==MAPI_E_FAILURE) TRACE("! SIMPLE MAPI ERROR DURING LOGON ! \n One or more unspecified errors occurred during logon. \n  No session handle was returned. \n");
     if (ulResult==MAPI_E_INSUFFICIENT_MEMORY) TRACE("! SIMPLE MAPI ERROR DURING LOGON ! \n There was insufficient memory to proceed. \n  No session handle was returned. \n"); 
     if (ulResult==MAPI_E_LOGIN_FAILURE) TRACE("! SIMPLE MAPI ERROR DURING LOGON ! \n  There was no default logon, and the user failed to log on successfully when the logon dialog box was displayed. \n No session handle was returned. \n"); 
     if (ulResult==MAPI_E_TOO_MANY_SESSIONS) TRACE("! SIMPLE MAPI ERROR DURING LOGON ! \n The user had too many sessions open simultaneously. \n No session handle was returned. \n"); 
     if (ulResult==MAPI_E_USER_ABORT) TRACE("! SIMPLE MAPI ERROR DURING LOGON ! \n The user canceled the logon dialog box. \n No session handle was returned. \n"); 
  #endif 


  #ifdef _TEST_
     if (ulResult==MAPI_E_FAILURE) MessageBoxA(NULL,"MAPI_E_FAILURE","MAPILogon",MB_OK);
     if (ulResult==MAPI_E_INSUFFICIENT_MEMORY) MessageBoxA(NULL,"MAPI_E_INSUFFICIENT_MEMORY","MAPILogon",MB_OK); 
     if (ulResult==MAPI_E_LOGIN_FAILURE)  MessageBoxA(NULL,"MAPI_E_LOGIN_FAILURE","MAPILogon",MB_OK);
     if (ulResult==MAPI_E_TOO_MANY_SESSIONS)  MessageBoxA(NULL,"MAPI_E_TOO_MANY_SESSIONS","MAPILogon",MB_OK);
     if (ulResult==MAPI_E_USER_ABORT)  MessageBoxA(NULL,"MAPI_E_USER_ABORT","MAPILogon",MB_OK);
  #endif 


  if (ulResult != SUCCESS_SUCCESS) return -1; 
 
 return 1;
}


int LogOFF()
{
  ULONG ulResult;

  if (!hlibMAPI) return -1;
  
  ulResult=lpfnMAPILogoff(lhSession,0,0,0);

  #ifdef _TEST_
     if (ulResult==MAPI_E_FAILURE) MessageBoxA(NULL,"MAPI_E_FAILURE","MAPILogoff",MB_OK);
     if (ulResult==MAPI_E_INSUFFICIENT_MEMORY) MessageBoxA(NULL,"MAPI_E_INSUFFICIENT_MEMORY","MAPILogoff",MB_OK); 
     if (ulResult==MAPI_E_INVALID_SESSION)  MessageBoxA(NULL,"MAPI_E_INVALID_SESSION","MAPILogoff",MB_OK);
  #endif 
  
  if (ulResult != SUCCESS_SUCCESS) return -1;  

  FreeLibrary(hlibMAPI);
  hlibMAPI = 0;
  return 1;
}


int SendMail(LPSTR  to, LPSTR subj, LPSTR text)
{
  ULONG ulResult;
  MapiMessage msg;
  MapiRecipDesc *lpRecips;
   
  if (!hlibMAPI) return -1;

  ulResult = lpfnMAPIResolveName(lhSession, 0, to, 0, 0, &lpRecips);

  #ifdef _TEST_
     if (ulResult==MAPI_E_AMBIGUOUS_RECIPIENT) MessageBoxA(NULL,"MAPI_E_AMBIGUOUS_RECIPIENT","MAPIResolveName",MB_OK);
     if (ulResult==MAPI_E_UNKNOWN_RECIPIENT) MessageBoxA(NULL,"MAPI_E_UNKNOWN_RECIPIENT","MAPIResolveName",MB_OK);
     if (ulResult==MAPI_E_FAILURE) MessageBoxA(NULL,"MAPI_E_FAILURE","MAPIResolveName",MB_OK);
     if (ulResult==MAPI_E_INSUFFICIENT_MEMORY) MessageBoxA(NULL,"MAPI_E_INSUFFICIENT_MEMORY","MAPIResolveName",MB_OK);
     if (ulResult==MAPI_E_LOGIN_FAILURE) MessageBoxA(NULL,"MAPI_E_LOGIN_FAILURE","MAPIResolveName",MB_OK);
     if (ulResult==MAPI_E_NOT_SUPPORTED) MessageBoxA(NULL,"MAPI_E_NOT_SUPPORTED","MAPIResolveName",MB_OK);
     if (ulResult==MAPI_E_USER_ABORT) MessageBoxA(NULL,"MAPI_E_USER_ABORT","MAPIResolveName",MB_OK);
 #endif

  msg.lpszMessageType = NULL; 
  msg.lpszSubject = subj;
  msg.lpszNoteText = text;
  msg.nRecipCount = 1;
  msg.lpRecips = lpRecips;
  msg.nFileCount = 0;
  msg.lpFiles = NULL;
  msg.flFlags = 0;

  ulResult = lpfnMAPISendMail (lhSession, 0, &msg, 0, 0);
  
  lpfnMAPIFreeBuffer(lpRecips);

  #ifdef _TEST_
     if (ulResult==MAPI_E_AMBIGUOUS_RECIPIENT) MessageBoxA(NULL,"MAPI_E_AMBIGUOUS_RECIPIENT","MAPISendMail",MB_OK);
     if (ulResult==MAPI_E_ATTACHMENT_NOT_FOUND) MessageBoxA(NULL,"MAPI_E_ATTACHMENT_NOT_FOUND","MAPISendMail",MB_OK);
     if (ulResult==MAPI_E_ATTACHMENT_OPEN_FAILURE) MessageBoxA(NULL,"MAPI_E_ATTACHMENT_OPEN_FAILURE","MAPISendMail",MB_OK);
     if (ulResult==MAPI_E_BAD_RECIPTYPE) MessageBoxA(NULL,"MAPI_E_BAD_RECIPTYPE","MAPISendMail",MB_OK);
     if (ulResult==MAPI_E_FAILURE) MessageBoxA(NULL,"MAPI_E_FAILURE","MAPISendMail",MB_OK);
     if (ulResult==MAPI_E_INSUFFICIENT_MEMORY) MessageBoxA(NULL,"MAPI_E_INSUFFICIENT_MEMORY","MAPISendMail",MB_OK);
     if (ulResult==MAPI_E_INVALID_RECIPS) MessageBoxA(NULL,"MAPI_E_INVALID_RECIPS","MAPISendMail",MB_OK);
     if (ulResult==MAPI_E_LOGIN_FAILURE) MessageBoxA(NULL,"MAPI_E_LOGIN_FAILURE","MAPISendMail",MB_OK);
     if (ulResult==MAPI_E_TEXT_TOO_LARGE) MessageBoxA(NULL,"MAPI_E_TEXT_TOO_LARGE","MAPISendMail",MB_OK);
     if (ulResult==MAPI_E_TOO_MANY_FILES) MessageBoxA(NULL,"MAPI_E_TOO_MANY_FILES","MAPISendMail",MB_OK);
     if (ulResult==MAPI_E_TOO_MANY_RECIPIENTS) MessageBoxA(NULL,"MAPI_E_TOO_MANY_RECIPIENTS","MAPISendMail",MB_OK);
     if (ulResult==MAPI_E_UNKNOWN_RECIPIENT) MessageBoxA(NULL,"MAPI_E_UNKNOWN_RECIPIENT","MAPISendMail",MB_OK);
     if (ulResult==MAPI_E_USER_ABORT) MessageBoxA(NULL,"MAPI_E_USER_ABORT","MAPISendMail",MB_OK);
 #endif 

  if (ulResult != SUCCESS_SUCCESS) return -1;  

 return 1;
}

int ReadNewMail(LPSTR From, LPSTR Subj, LPSTR Text)
{
  ULONG ulResult;
  char szMsgID[512];
  lpMapiMessage lpMessage;
  

  if (!hlibMAPI) return -1;
 
  ulResult = lpfnMAPIFindNext (lhSession, 0 , NULL, NULL,
        MAPI_UNREAD_ONLY | MAPI_GUARANTEE_FIFO | MAPI_LONG_MSGID, 0, szMsgID);

  #ifdef _TEST_
     if (ulResult==MAPI_E_FAILURE) MessageBoxA(NULL,"MAPI_E_FAILURE","MAPIFindNext",MB_OK);
     if (ulResult==MAPI_E_INSUFFICIENT_MEMORY) MessageBoxA(NULL,"MAPI_E_INSUFFICIENT_MEMORY","MAPIFindNext",MB_OK);
     if (ulResult==MAPI_E_INVALID_MESSAGE) MessageBoxA(NULL,"MAPI_E_INVALID_MESSAGE","MAPIFindNext",MB_OK);
     if (ulResult==MAPI_E_INVALID_SESSION) MessageBoxA(NULL,"MAPI_E_INVALID_SESSION","MAPIFindNext",MB_OK);
     if (ulResult==MAPI_E_NO_MESSAGES) MessageBoxA(NULL,"MAPI_E_NO_MESSAGES","MAPIFindNext",MB_OK);
  #endif 

  if (ulResult != SUCCESS_SUCCESS) return -1;

//    ulResult = lpfnMAPIReadMail (lhSession, 0, szMsgID, MAPI_PEEK | MAPI_ENVELOPE_ONLY, 0, &lpMessage);
  ulResult = lpfnMAPIReadMail (lhSession, 0, szMsgID, 0, 0, &lpMessage);

 #ifdef _TEST_
     if (ulResult==MAPI_E_ATTACHMENT_WRITE_FAILURE) MessageBoxA(NULL,"MAPI_E_ATTACHMENT_WRITE_FAILURE","MAPIReadMail",MB_OK);
     if (ulResult==MAPI_E_DISK_FULL) MessageBoxA(NULL,"MAPI_E_DISK_FULL","MAPIReadMail",MB_OK);
     if (ulResult==MAPI_E_FAILURE) MessageBoxA(NULL,"MAPI_E_FAILURE","MAPIReadMail",MB_OK);
     if (ulResult==MAPI_E_INSUFFICIENT_MEMORY) MessageBoxA(NULL,"MAPI_E_INSUFFICIENT_MEMORY","MAPIReadMail",MB_OK);
     if (ulResult==MAPI_E_INVALID_MESSAGE) MessageBoxA(NULL,"MAPI_E_INVALID_MESSAGE","MAPIReadMail",MB_OK);
     if (ulResult==MAPI_E_INVALID_SESSION) MessageBoxA(NULL,"MAPI_E_INVALID_SESSION","MAPIReadMail",MB_OK);
     if (ulResult==MAPI_E_TOO_MANY_FILES) MessageBoxA(NULL,"MAPI_E_TOO_MANY_FILES","MAPIReadMail",MB_OK);
     if (ulResult==MAPI_E_TOO_MANY_RECIPIENTS) MessageBoxA(NULL,"MAPI_E_TOO_MANY_RECIPIENTS","MAPIReadMail",MB_OK);
 #endif 

 if (ulResult != SUCCESS_SUCCESS) return -1;
 
 if (lpMessage->lpRecips->lpszName) strcpy(From,lpMessage->lpRecips->lpszName);
  else strcpy(From,"- - -");

 if (lpMessage->lpszSubject) strcpy(Subj,lpMessage->lpszSubject);
  else strcpy(From,"- - -");
      
 if (lpMessage->lpszNoteText) strcpy(Text,lpMessage->lpszNoteText);
  else strcpy(Text,"- - -"); 
 
 lpfnMAPIFreeBuffer(lpMessage);

return 1; 
}


/////////////////////////////////////////////////////////////////////////

BOOL OK()
{
if (hlibMAPI)
 if (lpfnMAPILogon) 
  if (lpfnMAPILogoff)
   if (lpfnMAPISendMail)
    if (lpfnMAPISendDocuments) 
     if (lpfnMAPIFindNext)
      if (lpfnMAPIReadMail)
       if (lpfnMAPISaveMail)
        if (lpfnMAPIDeleteMail)
         if (lpfnMAPIFreeBuffer)
          if (lpfnMAPIAddress)
           if (lpfnMAPIDetails)
            if (lpfnMAPIResolveName) return TRUE;

return FALSE;
}


BOOL MAPIInstalled(void)
{
      LONG lr;
    HKEY hkWMS;
    
    #define MAPIVSize 16 // was 8 but for sure

    char szMAPIValue[MAPIVSize];
    DWORD dwType;
    DWORD cbMAPIValue = MAPIVSize;


        lr = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                        _T("SOFTWARE\\Microsoft\\Windows Messaging Subsystem"),
                         0, KEY_READ, &hkWMS);
        if(ERROR_SUCCESS == lr)
        {
            lr = RegQueryValueEx(hkWMS, _T("MAPI"), 0, &dwType,(unsigned char *) szMAPIValue, &cbMAPIValue);
            RegCloseKey(hkWMS);
            if(ERROR_SUCCESS == lr)
            {
               // Assert(dwType == REG_SZ);
                if(strcmp(szMAPIValue, "1") == 0)
                    return TRUE;
            }
        }
        
        return FALSE;
   
} // of function


int PrepareMAPI()
{
    if(!MAPIInstalled()) return MAPI_NOT_INSTALLED;

    hlibMAPI = LoadLibrary(szMAPIDLL);

    if (!(lpfnMAPILogon = (LPMAPILOGON) GetProcAddress (hlibMAPI, "MAPILogon")))
    return -1;
    if (!(lpfnMAPILogoff = (LPMAPILOGOFF) GetProcAddress (hlibMAPI, "MAPILogoff")))
    return -1;
    if (!(lpfnMAPISendMail = (LPMAPISENDMAIL) GetProcAddress (hlibMAPI, "MAPISendMail")))
    return -1;
    if (!(lpfnMAPISendDocuments = (LPMAPISENDDOCUMENTS) GetProcAddress (hlibMAPI, "MAPISendDocuments")))
    return -1;
    if (!(lpfnMAPIFindNext = (LPMAPIFINDNEXT) GetProcAddress (hlibMAPI, "MAPIFindNext")))
    return -1;
    if (!(lpfnMAPIReadMail = (LPMAPIREADMAIL) GetProcAddress (hlibMAPI, "MAPIReadMail")))
    return -1;
    if (!(lpfnMAPISaveMail = (LPMAPISAVEMAIL) GetProcAddress (hlibMAPI, "MAPISaveMail")))
    return -1;
    if (!(lpfnMAPIDeleteMail = (LPMAPIDELETEMAIL) GetProcAddress (hlibMAPI, "MAPIDeleteMail")))
    return -1;
    if (!(lpfnMAPIFreeBuffer = (LPMAPIFREEBUFFER) GetProcAddress (hlibMAPI, "MAPIFreeBuffer")))
    return -1;
    if (!(lpfnMAPIAddress = (LPMAPIADDRESS) GetProcAddress (hlibMAPI, "MAPIAddress")))
    return -1;
    if (!(lpfnMAPIDetails = (LPMAPIDETAILS) GetProcAddress (hlibMAPI, "MAPIDetails")))
    return -1;
    if (!(lpfnMAPIResolveName = (LPMAPIRESOLVENAME) GetProcAddress (hlibMAPI, "MAPIResolveName")))
    return -1;

    return 1;

} // of function
