// ***********************************************************
// Program:  vinemail.dll, Visual C++ 5.0
//
// Author:   scott c rogan
//
//
//  Version:  1.7  8/25/98
//
// Purpose:  To supply 32 bit Visual Basic programs with some
//     commonly used VINES services
//
//
// Exported
// Routines:
//     SendMail:  Send a mail message with attachments.
//
//     GetSTNameFromNick32: Search for the full streettalk
//     name for a nickname.
//
//     GetSTNameFromNick32Org: Search for the full streettalk
//     name for a nickname and a given organization.
//
//     GetTotalMsgs:  Get the total number of
//     messages for the designated mailbox.
//
//     GetMailMsgIDs:  Get all the mail message id's for the
//     logged in user.
//
//     MoveMailMsg:  Move a message from one folder to another
//     for the logged in user.
//
//     DeleteMailMsg:  Delete a mail message for the logged in
//     user.
//
//     GetMainBodyPart:  Get the main body part for a particular
//     message id.
//
//     IsStNameValid:  Validate a streettalk name against the
//     streettalk database.
//
//     GetNextMailMsgID:  Obtain the next mail message id from the logged
//     in user's mailbox.
//
//     GetVINESUserName:  Return the logged in user's Streettalk name.
//
//
//
// Return   Generally, 0 if successful; non-zero if failure.
// Values   The error message depends
//     on the context of the error.  If the failure
//     happened in the VINES API then the error code
//     is documented in the VINES Mail Client Programming
//     Interface or VINES StreetTalk and STDA Programming
//     Interface.
//     If the failure happens in the Windows API then
//     the error code is documented in the Windows API
//     reference.  The comment returned in lpFailText
//     should make clear where the fault occurred.
//
// Build
// Considerations:  For resolving VINES API references this build
//        should include VNSAPI32.LIB and VNSMAI32.LIB.
//        The dll's, VNSAPI32.DLL and VNSMAI32.DLL, are generally
//     distributed by the VINES client setup
//
//        Also, set Project Settings, C/C++, Code Generation,
//     Struct Member Alignment = 1 Byte (compiler
//     switch /Zp1) or the VINES API calls will not
//     work correctly.
//
//
//
//     When building a Debug version, make
//     sure all optimizations (Project/Settings/C/C++/Optimizations)
//     are disabled.  You will then be able to step thru this
//     program in the Developer Studio using, say, a Visual
//     Basic executable program to drive it (see Project/Settings/
//     Debug/Executable for Debug Sessions).
//
// Modification
// History
//
// Version  Date  By  Comments
// 1.0   6/11/97  scr  initial coding
// 1.1   11/11/97 scr  remove the *@*@* pattern from
//         GetStNameFromNick32 since it can
//         unnessarily slow the search for a
//         bad employee number.  Also try to
//         fix an intermittent problem by
//         which an apparently good streettalk
//         user is flagged as bad.  Set up
//         a pointer to an area that the
//         SafeArrayGetElement function can
//         use and then copy this result to
//         a standard VINES STDA buffer.  Make
//         sure each is initialized for every
//         recipient.
//  1.2   1/5/98  scr  Lower the default message priority
//         to LOW_PRIORITY instead of MED_PRIORITY
//
// 1.3   4/27/98  scr  Add exported function DeleteMailMsg.
//         Make sure lpFailText is cleared first
//         in all functions that fill it.
// 1.4   6/9/98  scr  Do NOT validate users in STDA for SendMail.
//         It is possible for valid users to NOT be in
//         STDA.  The list of recipients is expected to
//         have been validated before passing to SendMail
//         See IsSTNameValid.
//         Upgrade MoveMailMsg to create the
//         destination mail folder if not present.
// 1.5   7/15/98  scr  Trimmed MAXMAILMSG size down to 900 bytes
//         from 1024 bytes because the VnsStoreMailBody
//         Part routine in SendMail was failing.  The
//         buffer size was consistently 1048 bytes when
//         the message exceeded 1024 bytes and it will
//         fail on anything larger than 1024.  When using
//         900, calloc seems to actually allocate 920.
//
//  1.6   8/14/98  scr  Removed the call to VNSDONE from various routines --
//         this was leading to Access Violation traps under
//         Windows NT.  Added the following exportable routines:
//         GetVinesUserName, GetNextMailMsgID.
//
//  1.7   8/25/98  scr  Add vinemail.rc (a VS_VERSION_INFO structure) to set the
//         program's product version.
 

// ************************************************************
#define INCL_ST
#define INCL_WS
#define INCL_STDA
#define INCL_SVC
#define INCL_MAIL
#define INCL_VAN
#define NT
#define cplusplus
#include <string.h>
#include <windows.h>
#include <iostream.h>
#include <stdio.h>
#include <vnsapi.h>
#include <io.h>
#include <fcntl.h>
#include <stdlib.h>

#define MAXMSGLEN  900
#define MAXATTACHMENTS 10
#define MAXATTACHBUFFER 1024
#define MAXRECIP 64
#define MSGLISTLIMIT 100
#define MSGLIMIT 1000
#define MSGIDLEN 14
#define BODYBLOCKSZ 1024
#define FOLDERLEN 13
#define MAXPROFBUFFER 1025

class MailService
{
 char *sUserName;
 char *sMailSvc;
 char *sFailText;
 HVNMA MSHandle;
 CallStatus status;

public:
 char *getUserName (void) { return sUserName; }
 char *getMailService (void) { return sMailSvc; }
 HVNMA getMailHandle (void) { return MSHandle; }
 char *getFailText (void) { return sFailText; }
 CallStatus initDetails(void);
 MailService (void);
 ~MailService (void);
 
 
 
 
 
 

};

MailService::MailService(void)
{
 sUserName = (char *)calloc(STNAMELEN, sizeof(char));
 sMailSvc =  (char *)calloc(255, sizeof(char));
 sFailText = (char *)calloc(255, sizeof(char));
 
 
 
 
 

}

MailService::~MailService(void)
{
 
 if (MSHandle != 0)
  status = VnsEndMailSession(MSHandle);

 free(sUserName);
 free(sMailSvc);
 free(sFailText);

 status = VnsDone();
 

}
 

CallStatus MailService::initDetails(void)

// Start a mail session, get the logged in user, get the mailservice name
{
 unsigned short nServiceRev;

 int i;

 char *sMailSvcRaw;
 char *sDefGroup;
 char sBuffer[20];

 status = VnsGetUserName(sUserName);
 if (status != 0)
 {
  strcpy(sFailText, "Vines API - VnsGetUserName failed with an unexpected status ");
  strcat(sFailText, itoa(status, sBuffer, 10));
  return status;
 }

 // Get logged in user's mail service from their profile
 sMailSvcRaw = (char *)calloc(255, sizeof(char));
 sDefGroup = (char *)calloc(STNAMELEN, sizeof(char));
 status = VnsGetStProfileEntryEx(sUserName, "SETMAIL", 1, VNS_ALL, VNS_UNAVAIL, sMailSvcRaw, sDefGroup);
 if (status != 0)
 {
  strcpy(sFailText, "Vines API - VnsGetStProfileEntryEx failed");
  return status;
 }

 // Now move sMailSvcRaw to sMailSvc removing the quotes
 
 for (i = 0; *sMailSvcRaw; sMailSvcRaw++)
 {
   if (*sMailSvcRaw != '\"')
    sMailSvc[i++] = *sMailSvcRaw;
 }

 // Start a new mail session
 status = VnsStartMailSession(sUserName, sMailSvc, &nServiceRev, &MSHandle);
 if (status != 0)
 {
  strcpy(sFailText, "Vines API - VnsStartMailSession failed");
  return status;
 }

 free(sMailSvcRaw);
 free(sDefGroup);

 return 0;
}

#ifdef _WIN32
 #define CCONV _stdcall
#else
 #define CCONV FAR PASCAL _export
#endif
 
 

// Function prototypes
CallStatus startMailSessionAndGetHandle(HVNMA *, char *, char *);

// global definitions
 
 

int CCONV SendMail(LPSTR lpMessage, LPSAFEARRAY FAR *lpWhoTo, LPSTR lpSubject, LPSAFEARRAY FAR *lpAttachments, LPSTR lpFailText, boolean bKeepCopy)
{

 // Purpose: Send a mail message with associated attachments to a recipient or group
 //    of recipients.
 //
 // Parameters: lpMessage (input) - Message to be sent.
 //    lpWhoTo (input) - Array of recipients to receive message.
 //    lpSubject (input) - Subject of message.
 //    lpAttachments (input) - Array of attachment paths.
 //    lpFailText (output) - Text of any error messages.
 //    bKeepCopy (input) - If true, keep a copy in sender's mailbox.
 //
 // Returns: 0 if successful, status code if not.
 //
 // Author:  scott c. rogan
 //
 // Created:
 //
 // Modification
 // History:
 //

 CallStatus status;
 char *sMsgID;
 char *sFolder;
 char *buffer;
 char *attachBuffer;
 char sMyUserName[STNAMELEN];

 // _splitpath parameters
 char splitDrive[_MAX_DRIVE];
 char splitDir[_MAX_DIR];
 char splitFile[_MAX_FNAME];
 char splitExt[_MAX_EXT];
 
 BodyPart bpList[11];

 Env envelope;

 int i;
 int j;

 long int lCallsRequired;
 long int lOffset;
 long int lCtr;
 long int lMsgLen;

 unsigned short nNumAttach;
 
 HVNMA MSHandle;

 
 // time definitions
 short tz_off;
 char tz[4];
 unsigned long gmt_sec;

 // For SAFEARRAY manipulation
 char *sAttach[MAXATTACHMENTS];
 char sWhoBuffer[STNAMELEN];
 long rgIndices;
 HRESULT hresult;
 long int lUBound;
 long int lLBound;
 long int lFileLen;
 char *element;

 char *sFailText;

 int nBufferLen;
 int nHandle;
 int nBytesRead;

 // For STDA calls
 IPCPORT stdaPort = NULLPORT;
 

 // Clear failure text buffer
 lstrcpy(lpFailText,"");

 
 // Get the upper and lower boundaries of the attachment
 // array.
 hresult = SafeArrayGetUBound(*lpAttachments, 1, &lUBound);
 if (hresult != 0)
 {
  lstrcpy(lpFailText, "Ole Automation - SafeArrayGetUBound failed");
  return hresult;
 }
 

 hresult = SafeArrayGetLBound(*lpAttachments, 1, &lLBound);
 if (hresult != 0)
 {
  lstrcpy(lpFailText, "Ole Automation - SafeArrayGetLBound failed");
  return hresult;
 }
 

 sFailText = (char *)calloc(255, sizeof(char));

 sMyUserName[0]=0x00;

 status = startMailSessionAndGetHandle(&MSHandle, sFailText, sMyUserName);
 if (status != 0)
 {
  lstrcpy(lpFailText, sFailText);
  return status;
 }
 

 // Get each element of the attachment array, up to MAXATTACHMENTS
 // and put into local sAttach for later use.
 for (i = 0, rgIndices = lLBound; (rgIndices <= lUBound) && (i < MAXATTACHMENTS); rgIndices++)
 {
  hresult = SafeArrayGetElement(*lpAttachments, &rgIndices, &sAttach[i++]);
  if (hresult != 0)
  {
   lstrcpy(lpFailText, "Ole Automation - SafeArrayGetElement failed");
   return hresult;
  }
 }

 // Preserve number of attachments encountered
 nNumAttach = i;
 

 // Initialize an envelope
 sMsgID = (char *)calloc(MAXIDLEN, sizeof(char));
 status = VnsInitNewMailEnvelope(MSHandle, sMsgID);
 if (status != 0)
 {
  lstrcpy(lpFailText, "Vines API - VnsInitNewMailEnvelope failed");
  return status;
 }

 // Initialize body part list
 for (i = 0; i < 11; i++)
  memset(&bpList[i], 0x0, sizeof(BodyPart));

 bpList[MAINBODY].present = PRESENT;
 bpList[MAINBODY].bodypartId = MAINBODY;
 bpList[MAINBODY].contentType = FREETXT1;
 strcpy(bpList[MAINBODY].label, "Main");
 

 

 // Store attachment file names in body part list
 j = 1;
 for (i = 1; i <= nNumAttach; i++)
 {
  if (strlen(sAttach[i - 1]) > 0)
  {
   bpList[j].present = PRESENT;

   // Only include the filename and extension in the body part list and
   // not the complete pathname.  If we supply the complete pathname,
   // it shows up that way in the mail message's attachment list

   _splitpath(sAttach[i - 1], splitDrive, splitDir, splitFile, splitExt);
   strncpy(bpList[j].label, splitFile, _MAX_FNAME);
   strncat(bpList[j].label, splitExt, _MAX_EXT);
   bpList[j].bodypartId = j;
   bpList[j].contentType = UNDEFCONT;
   j++;
  }
 }

 status = VnsStoreMailBodyPartList(MSHandle, sMsgID, bpList, j);
 if (status != 0)
 {
  lstrcpy(lpFailText, "Vines API - VnsStoreMailBodyPartList failed");
  return status;
 }
 

 

 

 // Set the folder to write messages to
 sFolder = (char *)calloc(13, sizeof(char));
 strcpy(sFolder, "General");

 // Can store message MAXMSGLEN bytes at a time
  lCallsRequired = (lstrlen(lpMessage) / MAXMSGLEN) + 1;

 buffer = (char *)calloc(MAXMSGLEN, sizeof(char));
 
 for (lCtr = 1; lCtr <= lCallsRequired; lCtr++)
 {
  lMsgLen = lstrlen(lpMessage);

  if (lMsgLen > MAXMSGLEN)
  {
   strncpy(buffer, lpMessage, MAXMSGLEN);
   lpMessage += MAXMSGLEN;
  }
  else
   strncpy(buffer, lpMessage, lMsgLen);
 

  lOffset = lCtr - 1;

  nBufferLen = strlen(buffer);
  status = VnsStoreMailBodyPart(MSHandle, sMyUserName, sFolder, sMsgID, MAINBODY, buffer, nBufferLen, lOffset);
  if (status != 0)
  {
   lstrcpy(lpFailText, "Vines API - VnsStoreMailBodyPart failed");
   return status;
  }
 

 }

 free (buffer);
 

 // Open each attachment, read in up to 1024 bytes at a time
 // and call VnsStoreMailBodyPart
 attachBuffer = (char *)calloc(MAXATTACHBUFFER, sizeof(char));

 for (i = 0; i < nNumAttach; i++)
 {
  if (strlen(sAttach[i]) > 0)
  {
   nHandle = open(sAttach[i], O_RDONLY|O_BINARY);
   if (nHandle != -1)
   {
    // File was found
    lFileLen = filelength(nHandle);
    lCallsRequired = (lFileLen / MAXATTACHBUFFER) + 1;
    for (j = 0; j < lCallsRequired; j++)
    {
     *attachBuffer = 0x00;
     nBytesRead = read(nHandle, attachBuffer, MAXATTACHBUFFER);
     status = VnsStoreMailBodyPart(MSHandle, sMyUserName, sFolder, sMsgID, i + 1, attachBuffer, nBytesRead, j);
     if (status != 0)
     {
      lstrcpy(lpFailText, "Vines API - VnsStoreMailBodyPart failed");
      return status;
     }

    }

   }
   else
   {
    lstrcpy(lpFailText, "Standard io - (open) - Unable to open attachment ");
    lstrcat(lpFailText, sAttach[i]);
    return nHandle;
   }
   close(nHandle);
  }
 
 }

 free (attachBuffer);
 
 

 
 // Get the upper and lower bounds from the recipient array
 hresult = SafeArrayGetUBound(*lpWhoTo, 1, &lUBound);
 if (hresult != 0)
 {
  lstrcpy(lpFailText, "Ole Automation - SafeArrayGetUBound failed");
  return hresult;
 }

 hresult = SafeArrayGetLBound(*lpWhoTo, 1, &lLBound);
 if (hresult != 0)
 {
  lstrcpy(lpFailText, "Ole Automation - SafeArrayGetLBound failed");
  return hresult;
 }
 

 
 

 // Retrieve each recipient from the array (should be a streettalk name)
 // and build an entry in the recipient list.  Recipients are NOT
 // verified here.  That should have been done outside.
 
 for (lCtr = 0; lCtr <= lUBound; lCtr++)
 {
  // Clear recipient buffer before each use
  for (int i = 0; i < STNAMELEN; i++)
   sWhoBuffer[i] = 0x00;

  // Set up a clean area to receive safearray element
  element = (char *)calloc(200, sizeof(char));

 
  hresult = SafeArrayGetElement(*lpWhoTo, &lCtr, &element);
  if (hresult != 0)
  {
   lstrcpy(lpFailText, "Ole Automation - SafeArrayGetElement failed");
   return hresult;
  }

  // Copy element to restricted size VINES buffer
  strncpy(sWhoBuffer, element, STNAMELEN);

  if (strlen(sWhoBuffer) > 0)
  {
   status = VnsStoreMailRecipient(MSHandle, sMsgID, RECIPIENTTO, sWhoBuffer);
   if (status != 0)
   {
    lstrcpy(lpFailText, "Vines API - VnsStoreMailRecipient failed");
    return status;
   }
  }

 } // For Loop

 
 free (element);
 

 
 

 // Build the necessary envelope structures
 
 envelope.forwarder[0] = 0x00;
 envelope.traceinfo[0] = 0x00;
 envelope.status = MA_NEW;
 envelope.replyTo[0] = 0x00;
 envelope.priority = LOW_PRIORITY;
 envelope.messageType = 0x00;
 envelope.alternateRecip[0] = 0x00;
 envelope.nondeliv = 0x00;

 // envelope.from MUST be the same as the logged on
 // streettalk user
 memset(&envelope.from, 0x00, MAXFROMSTR);
 strncpy(envelope.from, sMyUserName, strlen(sMyUserName));

 memset(&envelope.subject, 0x00, MAXSUBSTR);
 strncpy(envelope.subject, lpSubject, strlen(lpSubject));

 memset(&envelope.messageId, 0x00, MAXIDLEN);
 strncpy(envelope.messageId, sMsgID, strlen(sMsgID));

 envelope.certified = NOMESCONFIRM;

 VnsGetTime(&gmt_sec, &tz_off, tz);
 envelope.creationDate.gmt_seconds = gmt_sec;
 envelope.creationDate.tz_off = tz_off;
 strcpy(envelope.creationDate.tz, tz);

 VnsGetTime(&gmt_sec, &tz_off, tz);
 envelope.sendDate.gmt_seconds = gmt_sec;
 envelope.sendDate.tz_off = tz_off;
 strcpy(envelope.sendDate.tz, tz);
 

 // No expiration date
 VnsGetTime(&gmt_sec, &tz_off, tz);
 envelope.expireDate.gmt_seconds = 0;
 envelope.expireDate.tz_off = 0;
 strcpy(envelope.expireDate.tz,"    ");

 envelope.delivDate.gmt_seconds = 0;
 envelope.delivDate.tz_off = 0;
 strcpy(envelope.delivDate.tz, "    ");

 envelope.deferDate.gmt_seconds = 0;
 envelope.deferDate.tz_off = 0;
 strcpy(envelope.deferDate.tz, "    ");
 

 envelope.messageType='M';

 
 

 status = VnsStoreMailEnvelope(MSHandle, sMyUserName, sFolder, sMsgID, &envelope);
 if (status != 0)
 {
  lstrcpy(lpFailText, "Vines API - VnsStoreMailEnvelope failed");
  return status;
 }

 
 
 
 status = VnsSendMailMsg(MSHandle, sMyUserName, sFolder, sMsgID);
 if (status != 0)
 {
  lstrcpy(lpFailText, "Vines API - VnsSendMailMsg failed");
  return status;
 }

 

 if (!bKeepCopy)
 {
  // Delete the sender's copy
  status = VnsDeleteMailMsg(MSHandle, sMyUserName, sFolder, sMsgID);
  if (status != 0)
  {
   lstrcpy(lpFailText, "Vines API - VnsDeleteMailMsg failed");
   return status;
  }
 }

 

 status = VnsReleaseMailEnvelope(MSHandle, sMsgID);
 if (status != 0)
 {
  lstrcpy(lpFailText, "Vines API - VnsReleaseMailEnvelope failed");
  return status;
 }

 
 
 // End mail session
 status = VnsEndMailSession(MSHandle);

 
 free (sFailText);
 free (sMsgID);
 free (sFolder);

 return 0;
 
 

}
 

int CCONV GetSTNameFromNick32(LPSTR lpEmpNo, LPSTR lpSTDA, LPSTR lpFailText)
{
 // Purpose: Return a Streettalk name (if possible) for a given nickname.
 //
 // Parameters: lpEmpno (input) - Nickname to search (usually employee number).
 //    lpMsgIDArr (output) - Array of message id's found.
 //    lpNumMsgs (output) - Number of messages in folder.
 //    lpFailText (output) - Text of error message.
 //
 // Returns: 0 if successful, status code if not.
 //
 // Author:  scott c. rogan
 //
 // Created:
 //
 // Modification
 // History:
 //

 Catselect cat;
 CallStatus status;
 //LPIPCPORT pStPort;
 VNS_ST_SESS stSession;
 STRecord stRecord;
 //Boolean bIntl;
 int i;
 unsigned short uPassNum;
 char *sPattern[5];
 

 // Clear failure text buffer
 lstrcpy(lpFailText,"");

 
 // Get some memory for patterns
 for (i=0; i < 5; i++)
 {
  sPattern[i] = (char *)calloc(64, sizeof(char));
  strcpy(sPattern[i], lpEmpNo);
 }
 
 // Build up the patterns to search
 strcat(sPattern[0], "@*@*office1");
 strcat(sPattern[1], "@*@*office2");
 strcat(sPattern[2], "@*@*office3");
 strcat(sPattern[3], "@*@*office4");
 strcat(sPattern[4], "@*@*office5");
 

 
 // Initialize port settings
 //pStPort -> uAddressFamily = true;
 //pStPort -> ulSerial = true;
 //pStPort -> uSubnet = true;
 //pStPort -> uPort = true;

 //pStPort -> uHop = true;
 //pStPort -> ulFiller = true;

 cat.cats.length = 0;

 status = VnsStartStSession(NULL, true, NULL, &stSession);
 if (status != 0)
 {
  lstrcpy(lpFailText, "Vines API - VnsStartStSession failed");
  return status;
 }
 

 // Successively try each pattern with the supplied employee
 // number to see if we can locate a user id.
 for (i = 0; i < 5; i++)
 {
        // Must be zero before each use in VnsListSStNames
        uPassNum = 0;

     // Clear out stRecord memory area
  memset(&stRecord, 0x00, sizeof(stRecord));

        status = VnsListSStNames(&stSession, sPattern[i], OBJTYPE, UNSPECCLASS, &cat, &uPassNum, &stRecord);
  free (sPattern[i]);
        if (status == 0 && stRecord.ENUM_case.length > 0)
  {
            lstrcpy(lpSTDA, stRecord.ENUM_case.sequence[0]);
   status = VnsEndStSession(&stSession);
            return 0;
        }
 
 }
 
 
 
    status = VnsEndStSession(&stSession);
 return -1;
}
 

int CCONV GetSTNameFromNick32Org(LPSTR lpEmpNo, LPSTR lpSTDA, LPSTR lpFailText, LPSTR lpOrg)
{
 // Purpose: Return a streettalk name for a given nickname (if possible) using a
 //    particular organization to limit the search.
 //
 // Parameters: lpEmpNo (input) - Nickname, usually employee number.
 //    lpSTDA (output) - Resulting streettalk name.
 //    lpFailText (output) - Text of any error messages.
 //    lpOrg (input) - Organization to restrict the search by.
 //
 // Returns: 0 if successful, status code if not.
 //
 // Author:  scott c. rogan
 //
 // Created:
 //
 // Modification
 // History:
 //

 Catselect cat;
 CallStatus status;
 //LPIPCPORT pStPort;
 VNS_ST_SESS stSession;
 STRecord stRecord;
 //Boolean bIntl;
 unsigned short uPassNum;
 char *sPattern;
 

 // Clear failure text buffer
 lstrcpy(lpFailText,"");

 sPattern = (char *)calloc(64, sizeof(char));
 strcpy(sPattern, lpEmpNo);
 
 // Build up the pattern to search
 strcat(sPattern, "@*@");
 strcat(sPattern, lpOrg);
 

 cat.cats.length = 0;

 status = VnsStartStSession(NULL, true, NULL, &stSession);
 if (status != 0)
 {
  lstrcpy(lpFailText, "Vines API - VnsStartStSession failed");
  return status;
 }
 

 // Try the supplied pattern to see if we can locate a user id.
 // Must be zero before each use in VnsListSStNames
    uPassNum = 0;

    // Clear out stRecord memory area
 memset(&stRecord, 0x00, sizeof(stRecord));

    status = VnsListSStNames(&stSession, sPattern, OBJTYPE, UNSPECCLASS, &cat, &uPassNum, &stRecord);
    if (status == 0 && stRecord.ENUM_case.length > 0)
 {
  lstrcpy(lpSTDA, stRecord.ENUM_case.sequence[0]);
  status = VnsEndStSession(&stSession);
  free (sPattern);
        return 0;
    }
 
 
 
 
 
    status = VnsEndStSession(&stSession);

 free (sPattern);

 return -1;
}
 

int CCONV GetTotalMsgs(LPSTR lpMbxName, unsigned far int *npUnread, LPSTR lpFailText)
{

 // Purpose: Get the total number of unread mail messages for the indicated user.
 //
 // Parameters: lpMbxName (input) - Streettalk name of mailbox to be examined.
 //    npUnread (output) - Number of unread messages in lpMbxName.
 //    lpFailText (output) - Text of any error messages.
 //
 // Returns: 0 if successful, status code if not.
 //
 // Author:  scott c. rogan
 //
 // Created:
 //
 // Modification
 // History:
 //

 int status;

 
 HVNMA MSHandle;

 StatInfo Stat;
 char *folder;

 MailService *MS = new MailService;

 
 // Clear failure text buffer
 lstrcpy(lpFailText,"");
 

 // Start a mail session, get the logged in user name, get the mail service
 // name from the users profile and format it properly
 
 status = MS -> initDetails();
 if (status != 0)
 {
  lstrcpy(lpFailText, MS -> getFailText());
  return status;
 }

 MSHandle = MS -> getMailHandle();

 folder = (char *)calloc(FOLDERLEN, sizeof(char));

 status = VnsGetMailboxStats(MSHandle, lpMbxName, folder, &Stat);
 
 if (status != 0)
 {
  lstrcpy(lpFailText, "Vines API - VnsGetMailboxStats failed");
  return status;
 }

 *npUnread = Stat.total;
 

 // Release MailServer object
 delete MS;

 free(folder);
 return 0;
}
 

int CCONV GetMailMsgIDs(LPSTR lpFolder, LPSAFEARRAY far *lpMsgIDArr, unsigned far int *lpNumMsgs, LPSTR lpFailText)
{
 // Purpose: Return mail message id's for the logged in user.
 //
 // Parameters: lpFolder (input) - Folder to list mail message id's from.
 //    lpMsgIDArr (output) - Array of message id's found.
 //    lpNumMsgs (output) - Number of messages in folder.
 //    lpFailText (output) - Text of error message.
 //
 // Returns: 0 if successful, status code if not.
 //
 // Author:  scott c. rogan
 //
 // Created:
 //
 // Modification
 // History:
 //
 int status;
 int nCurrent;

 long int i;

 unsigned short nReturned;
 unsigned short nTotalMsgs;

 char *sMsgID;
 
 BSTR wcElement = NULL;
 BSTR element = NULL;

 MsgInfo *msgList;
 

 HVNMA MSHandle;
 HRESULT hResult;

 // Clear failure text buffer
 lstrcpy(lpFailText,"");
 

 MailService *MS = new MailService;
 
 status = MS -> initDetails();
 if (status != 0)
 {
  lstrcpy(lpFailText, MS -> getFailText());
  return status;
 }

 msgList = (MsgInfo *)calloc(MSGLISTLIMIT,sizeof(MsgInfo));
 sMsgID = (char *)calloc(MSGIDLEN, sizeof(char));

 MSHandle = MS -> getMailHandle();
 

 status = VnsListMailMsgs(MSHandle, MS -> getUserName(), lpFolder, msgList, 0, 100, &nReturned, &nTotalMsgs, 0);
 if (status != 0)
 {
  lstrcpy(lpFailText, "Vines API - VnsListMailMsgs failed");
  return status;
 }

 nCurrent = 0;
 while (nReturned > 0)
 {
  for (i = nCurrent; i - nCurrent < nReturned && i < MSGLIMIT; i++)
  {
   strncpy(sMsgID, msgList[i - nCurrent].id, MSGIDLEN);

   SysFreeString(element);
   element = SysAllocString( (BSTR) sMsgID );
 
   hResult = SafeArrayPutElement(*lpMsgIDArr, &i, element);
   if (hResult != 0)
   {
    lstrcpy(lpFailText, "OLE Automation - SafeArrayPutElement failed");
    return hResult;
   }

     }

  SysFreeString(element);

  nCurrent = i;

  // Clear out message list
  memset(msgList, 0x00, sizeof(msgList));

  status = VnsListMailMsgs(MSHandle, MS -> getUserName(), lpFolder, msgList, nCurrent, 100, &nReturned, &nTotalMsgs, 0);
  if (status != 0)
  {
   lstrcpy(lpFailText, "Vines API - VnsListMailMsgs failed");
   return status;
  }
 

 }

 *lpNumMsgs = nCurrent;

 // Release MailServer object
 delete MS;

 free(sMsgID);
 free(msgList);

 return 0;

}

int CCONV MoveMailMsg (LPSTR lpFromFolder, LPSTR lpToFolder, LPSTR lpMsgID, LPSTR lpFailText)
{
 int status;
 char sMyUserName[STNAMELEN];
 char *sFailText;
 

 HVNMA MSHandle;

 // Clear failure text buffer
 lstrcpy(lpFailText,"");

 
 sFailText = (char *)calloc(255, sizeof(char));
 status = startMailSessionAndGetHandle(&MSHandle, sFailText, sMyUserName);
 if (status != 0)
 {
  lstrcpy(lpFailText, sFailText);
  return status;
 }
 

 // Create the lpToFolder.  If it exists, a 3030 error results
 status=VnsCreateMailFolder(MSHandle, sMyUserName, lpToFolder);
 if (status != 0 && status != 3030)
 {
  lstrcpy(lpFailText, "VINES API - VnsCreateMailFolder failed");
  return status;
 }
 

 // Move message
 status = VnsMoveMailMsg(MSHandle, sMyUserName, lpFromFolder, lpMsgID, lpToFolder);
 

 if (status != 0)
 {
  lstrcpy(lpFailText, "Vines API - VnsMoveMailMsg failed");
  return status;
 }

 free (sFailText);

 return 0;
}

int CCONV DeleteMailMsg (LPSTR lpFolder, LPSTR lpMsgID, LPSTR lpFailText)
{
 int status;

 char sMyUserName[STNAMELEN];
 char *sFailText;
 
 HVNMA MSHandle;
 

 // Clear failure text buffer
 lstrcpy(lpFailText,"");

 
 sFailText = (char *)calloc(255, sizeof(char));
 status = startMailSessionAndGetHandle(&MSHandle, sFailText, sMyUserName);
 if (status != 0)
 {
  lstrcpy(lpFailText, sFailText);
  return status;
 }

 
 status = VnsDeleteMailMsg(MSHandle, sMyUserName, lpFolder, lpMsgID);
 

 if (status != 0)
 {
  lstrcpy(lpFailText, "Vines API - VnsDeleteMailMsg failed");
  return status;
 }

 free (sFailText);

 return 0;
}
 
 

int CCONV GetMainBodyPart (LPSTR lpFolder, LPSTR lpMsgID, LPSTR lpMessage, unsigned far int *lpBytesLeft, unsigned far short uOffset, LPSTR lpSubject, LPSTR lpFailText)
{
 int status;
 int nBytesLeft;

 char *sBuffer;
 
 char *sFailText;
 char sMyUserName[STNAMELEN];

 HVNMA MSHandle;
 Env envelope;

 unsigned long uMsgSize;
 

 unsigned short uBufferSize;

 sFailText = (char *)calloc(255, sizeof(char));
 status = startMailSessionAndGetHandle(&MSHandle, sFailText, sMyUserName);
 if (status != 0)
 {
  lstrcpy(lpFailText, sFailText);
  return status;
 }
 
 sBuffer = (char *)calloc(BODYBLOCKSZ + 1, sizeof(char));

 
 // Get the size of this messages MAINBODY part
 status = VnsGetMailBodyPartSize(MSHandle, sMyUserName, lpFolder, lpMsgID, MAINBODY, &uMsgSize);

 
 if (status != 0)
 {
  lstrcpy(lpFailText, "Vines API - VnsGetMailBodyPartSize failed");
  return status;
 }

 
 status = VnsGetMailBodyPart(MSHandle, sMyUserName, lpFolder, lpMsgID, MAINBODY, sBuffer, uOffset, &uBufferSize);
 if (status != 0)
 {
  lstrcpy(lpFailText, "Vines API - VnsGetMailBodyPart failed");
  return status;
 }

 lstrcpy(lpMessage, sBuffer);
 
 nBytesLeft = uMsgSize - (1024 * uOffset);
 if (nBytesLeft < 0)
  *lpBytesLeft = 0;
 else
  *lpBytesLeft = nBytesLeft;
 

 

 // Now capture the envelope and return the subject of the message
 memset(&envelope, 0x00, sizeof(envelope));
 status = VnsGetMailEnvelope(MSHandle, sMyUserName, lpFolder, lpMsgID, &envelope);
 if (status != 0)
 {
  lstrcpy(lpFailText, "Vines API - VnsGetMailEnvelope failed");
  return status;
 }

 lstrcpy(lpSubject, envelope.subject);
 
 free (sFailText);
 free (sBuffer);

 return 0;
 

}
 

int CCONV IsStNameValid (LPSTR lpSTName, LPSTR lpFailText)

{
 Catselect cat;
 CallStatus status;
 //LPIPCPORT pStPort;
 VNS_ST_SESS stSession;
 unsigned short uBufferLen;
 unsigned short uNameLen;
 char *sUserName;

 char szBuffer[MAXPROFBUFFER + 1];
 char szOBuffer[STNAMELEN];

 // Clear failure text buffer
 lstrcpy(lpFailText,"");

 sUserName = (char *)calloc(64, sizeof(char));
 strcpy(sUserName, lpSTName);
 
 
 cat.cats.length = 0;

 status = VnsStartStSession(NULL, true, NULL, &stSession);
 if (status != 0)
 {
  lstrcpy(lpFailText, "Vines API - VnsStartStSession failed");
  return status;
 }
 

 uBufferLen = MAXPROFBUFFER + 1;
 uNameLen = STNAMELEN;

 status = VnsGetSStProfile(&stSession, sUserName, szBuffer, &uBufferLen, szOBuffer, &uNameLen);
 if (status != 0)
 {
  lstrcpy(lpFailText, "Vines API - VnsGetSStProfile failed");
  return status;

 }
 
    status = VnsEndStSession(&stSession);

 free(sUserName);
 return 0;
 
 
 

}

int CCONV GetVINESUserName (LPSTR lpUserName, LPSTR lpFailText)
{
 char *sMyUserName;
 char sBuffer[20];
 CallStatus status;

 sMyUserName = (char *)calloc(STNAMELEN, sizeof(char));
 
 status = VnsGetUserName(sMyUserName);
 if (status != 0)
 {
  strcpy(lpFailText, "Vines API - VnsGetUserName failed with an unexpected status ");
  strcat(lpFailText, itoa(status, sBuffer, 10));
  return status;
 }

 lstrcpy(lpUserName, sMyUserName);

 free (sMyUserName);

 return 0;
 
 
 

}
 
 

CallStatus startMailSessionAndGetHandle(HVNMA *nHandle, char *sFailText, char *sUserName)
{
 
 

 unsigned short nServiceRev;

 int i;
 
 CallStatus status;

 LPMEMORYSTATUS lpMemStatus;
 

 char *sMailSvcRaw;
 char *sDefGroup;
 char *sMailSvc;
 char sBuffer[20];
 char *sMyUserName;

 sMyUserName = (char *)calloc(STNAMELEN, sizeof(char));
 
 //lpMemStatus = (LPMEMORYSTATUS)calloc(1, sizeof(LPMEMORYSTATUS));

 //GlobalMemoryStatus(lpMemStatus);

 sMailSvc =  (char *)calloc(255, sizeof(char));
 

 status = VnsGetUserName(sMyUserName);
 if (status != 0)
 {
  strcpy(sFailText, "Vines API - VnsGetUserName failed with an unexpected status ");
  strcat(sFailText, itoa(status, sBuffer, 10));
  return status;
 }
 strncpy(sUserName, sMyUserName, STNAMELEN - 1);

 // Get logged in user's mail service from their profile
 sMailSvcRaw = (char *)calloc(255, sizeof(char));
 sDefGroup = (char *)calloc(STNAMELEN, sizeof(char));

 //status = VnsGetStProfileEntryEx(sUserName, "SETMAIL", 1, VNS_ALL, VNS_UNAVAIL, sMailSvcRaw, sDefGroup);
 //if (status != 0)
 //{
 // strcpy(sFailText, "Vines API - VnsGetStProfileEntryEx failed with an unexpected status ");
 // strcat(sFailText, itoa(status, sBuffer, 10));
 // return status;
 //}

 status = VnsGetStProfileEntry(sMyUserName, "SETMAIL", sMailSvcRaw, sDefGroup);
 if (status != 0)
 {
  strcpy(sFailText, "Vines API - VnsGetStProfileEntry failed with an unexpected status ");
  strcat(sFailText, itoa(status, sBuffer, 10));
  strcat(sFailText, " user: ");
  strcat(sFailText, sMyUserName);
  strcat(sFailText, " mailsvc: ");
  strcat(sFailText, sMailSvcRaw);
  strcat(sFailText, " defgrp: ");
  strcat(sFailText, sDefGroup);
  return status;
 }

 // Now move sMailSvcRaw to sMailSvc removing the quotes
 
 for (i = 0; *sMailSvcRaw; sMailSvcRaw++)
 {
   if (*sMailSvcRaw != '\"')
    sMailSvc[i++] = *sMailSvcRaw;
 }

 // Start a new mail session
 status = VnsStartMailSession(sUserName, sMailSvc, &nServiceRev, nHandle);
 if (status != 0)
 {
  strcpy(sFailText, "Vines API - VnsStartMailSession failed with an unexpected status ");
  strcat(sFailText, itoa(status, sBuffer, 10));
  return status;
 }

 free (sMyUserName);
 free (sMailSvc);
 free (sMailSvcRaw);
 free (sDefGroup);

 return 0;

}
 

int CCONV GetNextMailMsgID(LPSTR lpFolder, LPSTR lpMsgID, unsigned far int *lpNumberRead, LPSTR lpFailText)
{
 // Purpose: Return the next mail message id for the logged in user.
 //
 // Parameters: lpFolder (input) - Folder to list mail message id's from.
 //    lpMsgID (output) - Message id of next message.
 //    lpNumberRead (output) - Number of message ids returned (0 or 1)
 //    lpFailText (output) - Text of error message.
 //
 // Returns: 0 if successful, status code if not.
 //
 // Author:  scott c. rogan
 //
 // Created:
 //
 // Modification
 // History:
 //
 int status;
 
 char *sFailText;
 char sMyUserName[STNAMELEN];
 
 unsigned short nReturned;
 unsigned short nTotalMsgs;
 unsigned short nOffset;
 unsigned short nLen;

 MsgInfo msgList[1];
 

 HVNMA MSHandle;
 
 // Clear failure text buffer
 lstrcpy(lpFailText,"");
 

 sFailText = (char *)calloc(255, sizeof(char));
 status = startMailSessionAndGetHandle(&MSHandle, sFailText, sMyUserName);
 if (status != 0)
 {
  lstrcpy(lpFailText, sFailText);
  return status;
 }

 

 
 nOffset = 0;
 nLen = 1;
 status = VnsListMailMsgs(MSHandle, sMyUserName, lpFolder, msgList, nOffset, nLen, &nReturned, &nTotalMsgs, 0);
 if (status != 0)
 {
  lstrcpy(lpFailText, "Vines API - VnsListMailMsgs failed");
  return status;
 }

 lstrcpy(lpMsgID, msgList[0].id);
 *lpNumberRead = nReturned;
 
 // End mail session
 status = VnsEndMailSession(MSHandle);

 free (sFailText);

 return 0;

}
 
 
 
 
 
 

Hosting by WebRing.
Navigation by WebRing.