home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: InfoMgt
/
InfoMgt.zip
/
MAILMIND.ZOO
/
NETMMIND.C
< prev
next >
Wrap
Text File
|
1992-03-11
|
10KB
|
318 lines
/* netmmind.c - check for mail & send messages
* Thomas A. Marciniak, M.D. = ytm@nihccpc1.bitnet
* Division of Cancer Prevention & Control, NCI
*/
/* Revision history:
* 1.11 ytm 03/10/92 allocate memory dynamically
* 1.1 ytm 02/22/92 read from file, make MSC & OS/2 compatible
* 1.02 ytm 03/28/91 add new users
* 1.01 ytm 03/20/91 eliminate Lyn Frey
* 1.00 ytm 02/07/91 non-TSR release
*/
/* Program notes:
* This module produces different executable files dependent upon two
* symbols: If OS2 is defined, then a protected mode OS/2 program is
* generated, else a DOS program is generated. If TEST is defined,
* the resulting program only scans the mail directories once, else (for
* the OS/2 version only) the directories are scanned continuously.
*
* Three command line parameters are supported: The first is the path of
* the mail directories (default C:\3OPEN\3MAIL\INBOX\); the second is the
* path of the ini file (default C:\3OPEN\3MAIL\NETMMIND.INI); and the third
* is the delay between each user check in milliseconds (default = 10000L).
* The ini file must be similar in format to the MBXS mailbox file:
* the first line is skipped and subsequent lines have the directory numbers
* and user names as follows:
* 004425070950 - This first line is skipped!
* 0 Admin:NCIDCPC1:NIH
* 82 John Doe:NCIDCPC1:NIH
* A leading ';' indicates a remark. The domain and organization are not
* needed. The name must be a workstation netbios name. If that is different
* than the 3+Mail name (as it is in DCPC), the MBXS file can not be used--
* you must make a similar file with the workstation netbios names.
*/
#include <local.h> /* standard definitions */
#ifdef OS2
#include <netcons.h> /* from Lan Manager toolkit */
#include <ncb.h> /* NETBIOS OS/2 class */
#include <netbios.h> /* NETBIOS OS/2 calls */
#include <os2.h> /* special OS/2 header */
#else
#include "netbios2.h" /* NETBIOS DOS defs */
#endif
/* defines */
#define USER_NUM 2 /* should be computer name */
#define MAX_ERRORS 100
#define MAX_PATH 32
#define NCB_CHAIN_SINGLE 0
#define DELAY 10000L /* in milliseconds */
#define NO_INI 1
#define ERR_READ 2
#define TOO_MANY 3
#define NO_USERS 4
#define STATUS_UNKNOWN 0xff
/* globals */
NCB gNcb;
char cgaMailPath[MAX_PATH] = "C:\\3OPEN\\3MAIL\\INBOX\\";
char cgaIniPath[MAX_PATH] = "C:\\3OPEN\\3MAIL\\NETMMIND.INI";
char cgaMsg[] = "M";
#ifdef OS2
FILEFINDBUF gFFBlk;
USHORT ugiDir;
USHORT ugiSearch;
#else
struct ffblk gFFBlk;
#endif
/* function prototypes */
short main(short argc, string argv[]);
#ifdef OS2
word wNetSend(char far *sName, string sMsg, short iNbNum);
#else
word wNetSend(string sName, string sMsg, short iNbNum);
#endif
/* main */
short main(short argc, string argv[])
{
boolean bMore;
char caLine[MAXLINE];
FILE *pFile;
long lDelay = DELAY;
short iErrors = 0;
short iPass;
string sC;
string sSubDir;
word wBytes = 0;
word wBytesOld;
word wErr;
word wStatus = 0;
#ifdef OS2
PIDINFO PIDInfo;
SEL selUser;
char far *sUser;
char far *sUserBase;
char far *sUserOld;
#else
string sUser;
string sUserBase;
string sUserOld;
#endif
if (argc > 1) /* get command line params */
{
strcpy(cgaMailPath, argv[1]);
if (argc > 2)
{
strcpy(cgaIniPath, argv[2]);
if (argc > 3)
{
lDelay = atol(argv[3]);
if (lDelay <= 0) lDelay = DELAY;
}
}
}
for (sSubDir = cgaMailPath; *sSubDir; sSubDir++) ; /* set subdir ptr */
if (*(sSubDir - 1) != '\\') *sSubDir++ = '\\';
if ((pFile = fopen(cgaIniPath, "rt")) == NULL) wStatus = NO_INI;
else /* read ini file */
{
for (iPass = 0; iPass < 2 && !wStatus; iPass++)
{ /* pass 0: get length of User area */
/* pass 1: set up User */
fgets(caLine, MAXLINE, pFile); /* skip first line */
bMore = TRUE;
while (bMore && !wStatus)
{
if (fgets(caLine, MAXLINE, pFile) == NULL)
{
if (feof(pFile)) bMore = FALSE;
else wStatus = ERR_READ;
}
else
{
if (*caLine != ';' && *caLine != NEWLINE) /* skip blank & remarks */
{
if (iPass) sUserOld = sUser; /* save loc in case bad line */
else wBytesOld = wBytes;
for (sC = caLine; *sC && *sC != ' '; sC++)
{
if (iPass) *sUser++ = *sC; /* get first param = user subdir */
else wBytes++;
}
if (*sC)
{
if (iPass) *sUser++ = '\0'; /* put separator null */
else wBytes++;
while (*sC == ' ') sC++; /* skip blanks */
for ( ; *sC && *sC != ':' && *sC != NEWLINE; sC++)
{
if (*sC == CTLZ)
{
*sC = '\0';
bMore = FALSE;
}
else
{ /* get second param = user name */
if (iPass) *sUser++ = *sC;
else wBytes++;
}
}
if (iPass) *sUser++ = '\0'; /* put separator null */
else wBytes++;
}
else
{
if (iPass) sUser = sUserOld; /* restore old ptrs if bad line */
else wBytes = wBytesOld;
}
}
} /* if (*caLine */
} /* while (bMore */
if (!wBytes) wStatus = NO_USERS;
else if (iPass == 0)
{
wBytes++; /* for final '\0' */
#ifdef OS2
DosAllocSeg(wBytes, &selUser, 0); /* allocate user area */
sUserBase = MAKEP(selUser, 0);
#else
if ((sUserBase = malloc((size_t) wBytes)) == NULL)
wStatus = TOO_MANY;
#endif
sUser = sUserBase;
rewind(pFile);
}
} /* for (iPass < 2 && !wStatus */
fclose(pFile);
}
#ifdef OS2
for (sC = cgaIniPath; *sC && *sC != '.'; sC++) ; /* set up PID file name */
strcpy(sC, ".PID");
if ((pFile = fopen(cgaIniPath, "w")) != NULL)
{
if (DosGetPID(&PIDInfo) == SUCCESS) /* write PID to file if OS2 */
fputs(itoa(PIDInfo.pid, caLine, 10), pFile);
fclose(pFile);
}
#endif
if (!wStatus)
{
*sUser = '\0'; /* put final null to user area */
sUser = sUserBase; /* and initialize user ptr */
}
#ifdef TEST
while (*sUser && !wStatus)
{
#else
#ifdef OS2
while (!wStatus)
{
DosSleep(lDelay);
if (*sUser == '\0') sUser = sUserBase; /* repeat if OS2 */
#else
while (*sUser && !wStatus)
{
#endif
#endif
sC = sSubDir;
while (*sUser) *sC++ = *sUser++; /* copy user subdir to SubDir */
strcpy(sC, "\\*.*"); /* find any file */
sUser++; /* skip null */
#ifdef OS2
ugiDir = 1;
ugiSearch = 1; /* check for files in user subdir */
if (DosFindFirst(cgaMailPath, &ugiDir, FA_NORMAL,
&gFFBlk, sizeof(gFFBlk), &ugiSearch, 0L) == SUCCESS)
#else
if (findfirst(cgaMailPath, &gFFBlk, FA_NORMAL) == SUCCESS)
#endif /* if files, send msg to user */
{
wErr = wNetSend(sUser, cgaMsg, USER_NUM); /* abort if too many errors */
if (wErr)
{
if (++iErrors > MAX_ERRORS) wStatus = wErr;
}
else if (iErrors) iErrors--;
}
while (*sUser++) ; /* position past user name & null */
} /* loop */
#ifdef OS2
if ((pFile = fopen(cgaIniPath, "w")) != NULL) /* record exit status */
{
fputs("Status ", pFile);
fputs(itoa(wStatus, caLine, 10), pFile);
fclose(pFile);
}
#else
if (wStatus) putch(BELL);
printf("\nnetmmind status: %d", wStatus);
#endif
return(wStatus);
} /* main */
/* send sMsg to sName from iNbNum */
#ifdef OS2
word wNetSend(char far *sName, string sMsg, short iNbNum)
{
short i;
string sC;
word wStatus;
word wHandle; /* handle to logical network */
wStatus = NetBiosOpen("NET1", 0, NB_REGULAR, &wHandle);
if (wStatus == SUCCESS)
{
memset(&gNcb, 0, sizeof(NCB));
gNcb.ncb_command = NCBDGSEND;
gNcb.ncb_num = (byte) iNbNum;
for (i = 0; sName[i] && i < NETBIOS_NAME_LEN; i++)
gNcb.ncb_callname[i] = sName[i];
for (sC = gNcb.ncb_callname + NETBIOS_NAME_LEN - 2;
sC >= gNcb.ncb_callname; sC--)
*sC = ((*sC) ? toupper(*sC) : ' ');
gNcb.ncb_buffer = (void far *) sMsg;
gNcb.ncb_length = strlen(sMsg);
gNcb.ncb_cmd_cplt = (byte) STATUS_UNKNOWN;
NetBiosSubmit(wHandle, NCB_CHAIN_SINGLE, &gNcb);
NetBiosClose(wHandle, 0);
wStatus = (word) gNcb.ncb_retcode;
}
return(wStatus);
} /* wNetSend for OS/2 */
#else
word wNetSend(string sName, string sMsg, short iNbNum)
{
char far *sfNcb;
short i;
string sC;
struct SREGS SegRegs;
union REGS InRegs, OutRegs;
memset(&gNcb, 0, sizeof(NCB));
gNcb.ncb_command = SEND_DATAGRAM_WAIT;
gNcb.ncb_num = iNbNum;
for (i = 0; sName[i] && i < NETBIOS_NAME_LEN; i++)
gNcb.ncb_callname[i] = sName[i];
for (sC = gNcb.ncb_callname + NETBIOS_NAME_LEN - 2;
sC >= gNcb.ncb_callname; sC--)
*sC = ((*sC) ? toupper(*sC) : ' ');
gNcb.ncb_buffer = (void far *) sMsg;
gNcb.ncb_length = strlen(sMsg);
gNcb.ncb_cmd_cplt = STATUS_UNKNOWN;
#ifdef MSC
sfNcb = (char far *) &gNcb;
SegRegs.es = FP_SEG(sfNcb);
InRegs.x.bx = FP_OFF(sfNcb);
#else
SegRegs.es = FP_SEG(&gNcb);
InRegs.x.bx = FP_OFF(&gNcb);
#endif
InRegs.x.ax = 0x0100;
int86x(0x5c, &InRegs, &OutRegs, &SegRegs);
return(gNcb.ncb_cmd_cplt);
} /* wNetSend for DOS */
#endif