/* ---- WVMAPI.C ---------------------------------------------- 
 *  This file contains the porocedures required for the MAPI
 *  (MS Mail) interface
 *
 *  MAtthew Bretherton     18/2/1993
 */

/*
 * $Id: wvmapi.c 1.17 1996/08/13 04:54:52 dumoulin Exp $
 */

#include <windows.h>
#include <windowsx.h>
#include "wvglob.h"
#include "winvn.h"
#include "mapi.h"
#include "wvmapi.h"

HINSTANCE hLibrary = 0;
ULONG ulResult;
LHANDLE hMAPISession;
FLAGS flFlag = 0L;
BOOL LoggedOn;                  // required for demand login

char buf[MAXOUTLINE];
char mess[MAXOUTLINE];


int 
mlMAPIInit (HWND hWnd)
{
/*   WVMessageBox (hWnd, "MS MAil Initialisation", "Debug!", MB_OK | MB_ICONHAND); */
  LoggedOn = FALSE;


  if (InitMAPI ()) {
// this is confusing to newbies
    //     MessageBox (hWnd, "Error in Initialisation\r\nMail Interface Disabled", "Microsoft Mail", MB_OK | MB_ICONHAND);
    return (-1);
  };

  if (MailDemandLogon)
    return (0);
  else
    return (mlMAPILogon (hWnd));
}


int 
mlMAPILogon (HWND hWnd)
{
  flFlag = MAPI_LOGON_UI;

  ulResult = (ULONG) (*lpfnMAPILogon) ( (ULONG_PTR) hWnd, NULL, NULL, flFlag, 0L, (LPLHANDLE) & hMAPISession);
  if (ulResult != 0) {
// this is confusing to newbies
    //     sprintf(mess,"Failure to Login\r\nMail Interface Disabled\r\nError = %ld",ulResult);
    //     MessageBox (hWnd,mess , "Microsoft Mail", MB_OK | MB_ICONHAND);
    return (-1);
  }
  LoggedOn = TRUE;
  MailEnableLogout (LoggedOn);
  return (0);
}

/* -------- mlMAPILogout ---------------

 * Logout from MAPI service but leave resources available
 * (loaded)
 *
 * --------------------------------------*/

int 
mlMAPILogout (HWND hWnd)
{
/*   WVMessageBox (hWnd, "MS MAil Logout", "Debug!", MB_OK | MB_ICONHAND); */
  ulResult = (ULONG) (*lpfnMAPILogoff) (hMAPISession, (ULONG_PTR) hWnd, 0L, 0L);
  if (ulResult == 0) {
    LoggedOn = FALSE;
    MailEnableLogout (LoggedOn);
    return (0);
  }
  return (-1);
}

/* -------- mlMAPIClose ---------------

 * Remove all trace of MAPI services - log out (if necessary)
 * and unload resources
 *
 * --------------------------------------*/

int 
mlMAPIClose (HWND hWnd)
{
  if (LoggedOn)
    mlMAPILogout (hWnd);
  DeInitMAPI ();
  return (0);
}

/******** mlMAPISend *************************************************** 
 *   
 *  Send Mail for MAPI Interface 
 *
 *
 */

int mlMAPISend (HWND hWnd, TypDoc * Doc, int DocType)
{
  char myBuf[MAXHEADERLINE + 1], Address[MAXHEADERLINE + 1];
  char Name[MAXHEADERLINE +1];
  char *body;
  static MapiMessage mmMapiMessage;
  static MapiRecipDesc recips;
  /* enhancement opportunity:
   * should allow multiple recipients and copies, filling lpRecips and
   * using MAPI_CC for copies
   */

  if (!LoggedOn && mlMAPILogon (hWnd) != 0) {
    return (-1);                // No Logon - No Go !

  }

  /* Prepare To, Subject, and body 
   * (jsc 10/16/94) 
   */
  if (Doc) {
    /* Don't generate To line if this is a forward */
    if (DocType == DOCTYPE_MAIL) {
      GetToAddress (myBuf, MAXHEADERLINE, Doc, hWnd);
      if (strchr (myBuf, '@') != NULL) {
//      _snprintf (Address, MAXHEADERLINE, "SMTP:%s", myBuf);
        _snprintf (Address, MAXHEADERLINE, "%s%s", MAPIPrefix, myBuf);
      }
      else {
        strntcpy (Address, myBuf, MAXHEADERLINE);
      }
      _snprintf(Name,MAXHEADERLINE,"%s <%s>",NameString,myBuf);
      
      mmMapiMessage.nRecipCount = 1L;
      recips.ulReserved = 0L;
      recips.lpszAddress = Address;
      recips.ulRecipClass = MAPI_TO;
      recips.lpszName = Name;
      recips.ulEIDSize = 0L;
      recips.lpEntryID = NULL;
      mmMapiMessage.lpRecips = &recips;
    }
    else {
      mmMapiMessage.nRecipCount = 0L;
      mmMapiMessage.lpRecips = NULL;
    }
    GetSubject (myBuf, MAXHEADERLINE, Doc, DocType);
    if (!strcmp (myBuf, "")) {
      mmMapiMessage.lpszSubject = NULL;
    }
    else {
      mmMapiMessage.lpszSubject = myBuf;
    }

    if (MakeBody (&body, Doc, DocType, hWnd, NULL, FALSE) != SUCCESS) {
      return FALSE;
    }

  }
  else {
    body = (char *) NULL;
    mmMapiMessage.nRecipCount = 0L;
    mmMapiMessage.lpRecips = NULL;
  }

  mmMapiMessage.lpszNoteText = body;

  mmMapiMessage.ulReserved = 0L;
  mmMapiMessage.lpszMessageType = NULL;
  mmMapiMessage.lpszDateReceived = NULL;
  mmMapiMessage.lpszConversationID = NULL;
  mmMapiMessage.flFlags = 0L;
  mmMapiMessage.lpOriginator = NULL;
  mmMapiMessage.nFileCount = 0L;
  mmMapiMessage.lpFiles = NULL;

  /* MAPI_DIALOG shows a windows for message editing , addressing etc.
   * MAPI_LOGON_UI in helpreq.c example  
   */
  flFlag = MAPI_DIALOG;
  ulResult = (ULONG) (*lpfnMAPISendMail) (hMAPISession, (ULONG_PTR) hWnd, &mmMapiMessage,
                                          flFlag, 0L);

  if (body != (char *) NULL) {
    GlobalFreePtr (body);
  }

  if (ulResult > 1) {
    sprintf (buf, "MS MAil Error - No. %u", ulResult);
    WVMessageBox (hWnd, buf, "Microsoft Mail Send", MB_OK | MB_ICONHAND);
    return (-1);
  }

  return (0);
}


/* Following code extracted from Microsofts MAPI Sample 
   MAPIAPP.C on the MS MAIL 3.0 distibution disks */

int FAR PASCAL InitMAPI ()
{
#ifdef _WIN32
  if ((hLibrary = LoadLibrary (MAPIDLL)) == NULL)
#else
  if ((hLibrary = LoadLibrary (MAPIDLL)) < HINSTANCE_ERROR)
#endif
    return (ERR_LOAD_LIB);

  if ((lpfnMAPILogon =
    (ULONG (WINAPI *) (ULONG_PTR, LPSTR, LPSTR, FLAGS, ULONG, LPLHANDLE))
    GetProcAddress (hLibrary, SZ_MAPILOGON)) == NULL)
    return (ERR_LOAD_FUNC);

  if ((lpfnMAPILogoff =
    (ULONG (WINAPI *) (LHANDLE, ULONG_PTR, FLAGS, ULONG))
    GetProcAddress (hLibrary, SZ_MAPILOGOFF)) == NULL)
    return (ERR_LOAD_FUNC);

  if ((lpfnMAPISendMail =
    (ULONG (WINAPI *) (LHANDLE, ULONG_PTR, lpMapiMessage, FLAGS, ULONG))
    GetProcAddress (hLibrary, SZ_MAPISENDMAIL)) == NULL)
    return (ERR_LOAD_FUNC);

  if ((lpfnMAPIAddress =
    (ULONG (WINAPI *) (LHANDLE, ULONG_PTR, LPSTR, ULONG, LPSTR, ULONG,
      lpMapiRecipDesc, FLAGS, ULONG, LPULONG, lpMapiRecipDesc FAR *))
    GetProcAddress (hLibrary, SZ_MAPIADDRESS)) == NULL)
    return (ERR_LOAD_FUNC);

  if ((lpfnMAPIFreeBuffer =
    (ULONG (WINAPI *) (LPVOID))
    GetProcAddress (hLibrary, SZ_MAPIFREEBUFFER)) == NULL)
    return (ERR_LOAD_FUNC);

  /* Following Functions are not required 

     if ((lpfnMAPISendDocuments= GetProcAddress(hLibrary,SZ_MAPISENDDOC)) == NULL)
     return(ERR_LOAD_FUNC);

     if ((lpfnMAPIFindNext= GetProcAddress(hLibrary,SZ_MAPIFINDNEXT)) == NULL)
     return(ERR_LOAD_FUNC);

     if ((lpfnMAPIReadMail= GetProcAddress(hLibrary,SZ_MAPIREADMAIL)) == NULL)
     return(ERR_LOAD_FUNC);

     if ((lpfnMAPISaveMail= GetProcAddress(hLibrary,SZ_MAPISAVEMAIL)) == NULL)
     return(ERR_LOAD_FUNC);

     if ((lpfnMAPIDeleteMail= GetProcAddress(hLibrary,SZ_MAPIDELMAIL)) == NULL)
     return(ERR_LOAD_FUNC);

     if ((lpfnMAPIDetails= GetProcAddress(hLibrary,SZ_MAPIDETAILS)) == NULL)
     return(ERR_LOAD_FUNC);

     if ((lpfnMAPIResolveName= GetProcAddress(hLibrary,SZ_MAPIRESOLVENAME)) == NULL)
     return(ERR_LOAD_FUNC);

   */

  return (0);
}


int FAR PASCAL 
DeInitMAPI ()
{
  lpfnMAPILogon = NULL;
  lpfnMAPILogoff = NULL;
  lpfnMAPISendMail = NULL;
  lpfnMAPIFreeBuffer = NULL;
  lpfnMAPIAddress = NULL;

  /* following functions are unused
     lpfnMAPISendDocuments= NULL;
     lpfnMAPIFindNext= NULL;
     lpfnMAPIReadMail= NULL;
     lpfnMAPISaveMail= NULL;
     lpfnMAPIDeleteMail= NULL;
     lpfnMAPIDetails = NULL;
     lpfnMAPIResolveName = NULL
   */

  FreeLibrary (hLibrary);

  return (0);
}

//
// Local Variables:
// tab-width: 2
// end:
//
