home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: InfoMgt / InfoMgt.zip / MAILMIND.ZOO / NETMMIND.C < prev    next >
Text File  |  1992-03-11  |  10KB  |  318 lines

  1. /* netmmind.c - check for mail & send messages
  2.  * Thomas A. Marciniak, M.D. = ytm@nihccpc1.bitnet
  3.  * Division of Cancer Prevention & Control, NCI
  4.  */
  5.  
  6. /* Revision history:
  7.  * 1.11  ytm  03/10/92  allocate memory dynamically
  8.  * 1.1   ytm  02/22/92  read from file, make MSC & OS/2 compatible
  9.  * 1.02  ytm  03/28/91  add new users
  10.  * 1.01  ytm  03/20/91  eliminate Lyn Frey
  11.  * 1.00  ytm  02/07/91  non-TSR release
  12.  */
  13.  
  14. /* Program notes:
  15.  * This module produces different executable files dependent upon two
  16.  * symbols:  If OS2 is defined, then a protected mode OS/2 program is
  17.  * generated, else a DOS program is generated.  If TEST is defined,
  18.  * the resulting program only scans the mail directories once, else (for
  19.  * the OS/2 version only) the directories are scanned continuously.
  20.  *
  21.  * Three command line parameters are supported:  The first is the path of
  22.  * the mail directories (default C:\3OPEN\3MAIL\INBOX\);  the second is the
  23.  * path of the ini file (default C:\3OPEN\3MAIL\NETMMIND.INI);  and the third
  24.  * is the delay between each user check in milliseconds (default = 10000L).
  25.  * The ini file must be similar in format to the MBXS mailbox file:
  26.  * the first line is skipped and subsequent lines have the directory numbers
  27.  * and user names as follows:
  28.  * 004425070950 - This first line is skipped!
  29.  * 0 Admin:NCIDCPC1:NIH
  30.  * 82 John Doe:NCIDCPC1:NIH
  31.  * A leading ';' indicates a remark.  The domain and organization are not
  32.  * needed.  The name must be a workstation netbios name.  If that is different
  33.  * than the 3+Mail name (as it is in DCPC), the MBXS file can not be used--
  34.  * you must make a similar file with the workstation netbios names.
  35.  */
  36.  
  37. #include <local.h>                      /* standard definitions */
  38. #ifdef OS2
  39. #include <netcons.h>                    /* from Lan Manager toolkit */
  40. #include <ncb.h>                        /* NETBIOS OS/2 class */
  41. #include <netbios.h>                    /* NETBIOS OS/2 calls */
  42. #include <os2.h>                        /* special OS/2 header */
  43. #else
  44. #include "netbios2.h"                   /* NETBIOS DOS defs */
  45. #endif
  46.  
  47. /* defines */
  48. #define USER_NUM 2                      /* should be computer name */
  49. #define MAX_ERRORS 100
  50. #define MAX_PATH 32
  51. #define NCB_CHAIN_SINGLE 0
  52. #define DELAY    10000L                 /* in milliseconds */
  53. #define NO_INI   1
  54. #define ERR_READ 2
  55. #define TOO_MANY 3
  56. #define NO_USERS 4
  57. #define STATUS_UNKNOWN 0xff
  58.  
  59. /* globals */
  60. NCB     gNcb;
  61. char    cgaMailPath[MAX_PATH] = "C:\\3OPEN\\3MAIL\\INBOX\\";
  62. char    cgaIniPath[MAX_PATH] = "C:\\3OPEN\\3MAIL\\NETMMIND.INI";
  63. char    cgaMsg[] = "M";
  64. #ifdef OS2
  65. FILEFINDBUF   gFFBlk;
  66. USHORT        ugiDir;
  67. USHORT        ugiSearch;
  68. #else
  69. struct  ffblk gFFBlk;
  70. #endif
  71.  
  72. /* function prototypes */
  73. short main(short argc, string argv[]);
  74. #ifdef OS2
  75. word  wNetSend(char far *sName, string sMsg, short iNbNum);
  76. #else
  77. word  wNetSend(string sName, string sMsg, short iNbNum);
  78. #endif
  79.  
  80. /* main */
  81. short main(short argc, string argv[])
  82. {
  83. boolean bMore;
  84. char    caLine[MAXLINE];
  85. FILE    *pFile;
  86. long    lDelay = DELAY;
  87. short   iErrors = 0;
  88. short   iPass;
  89. string  sC;
  90. string  sSubDir;
  91. word    wBytes = 0;
  92. word    wBytesOld;
  93. word    wErr;
  94. word    wStatus = 0;
  95. #ifdef OS2
  96. PIDINFO PIDInfo;
  97. SEL     selUser;
  98. char far *sUser;
  99. char far *sUserBase;
  100. char far *sUserOld;
  101. #else
  102. string  sUser;
  103. string  sUserBase;
  104. string  sUserOld;
  105. #endif
  106. if (argc > 1)                           /* get command line params */
  107.   {
  108.   strcpy(cgaMailPath, argv[1]);
  109.   if (argc > 2)
  110.     {
  111.     strcpy(cgaIniPath, argv[2]);
  112.     if (argc > 3)
  113.       {
  114.       lDelay = atol(argv[3]);
  115.       if (lDelay <= 0) lDelay = DELAY;
  116.       }
  117.     }
  118.   }
  119. for (sSubDir = cgaMailPath; *sSubDir; sSubDir++) ; /* set subdir ptr */
  120. if (*(sSubDir - 1) != '\\') *sSubDir++ = '\\';
  121. if ((pFile = fopen(cgaIniPath, "rt")) == NULL) wStatus = NO_INI;
  122. else                                    /* read ini file */
  123.   {
  124.   for (iPass = 0; iPass < 2 && !wStatus; iPass++)
  125.     {                                   /* pass 0: get length of User area */
  126.                                         /* pass 1: set up User */
  127.     fgets(caLine, MAXLINE, pFile);      /* skip first line */
  128.     bMore = TRUE;
  129.     while (bMore && !wStatus)
  130.       {
  131.       if (fgets(caLine, MAXLINE, pFile) == NULL)
  132.         {
  133.         if (feof(pFile)) bMore = FALSE;
  134.         else wStatus = ERR_READ;
  135.         }
  136.       else
  137.         {
  138.         if (*caLine != ';' && *caLine != NEWLINE) /* skip blank & remarks */
  139.           {
  140.           if (iPass) sUserOld = sUser;  /* save loc in case bad line */
  141.           else wBytesOld = wBytes;
  142.           for (sC = caLine; *sC && *sC != ' '; sC++)
  143.             {
  144.             if (iPass) *sUser++ = *sC;  /* get first param = user subdir */
  145.             else wBytes++;
  146.             }
  147.           if (*sC)
  148.             {
  149.             if (iPass) *sUser++ = '\0'; /* put separator null */
  150.             else wBytes++;
  151.             while (*sC == ' ') sC++;    /* skip blanks */
  152.             for ( ; *sC && *sC != ':' && *sC != NEWLINE; sC++)
  153.               {
  154.               if (*sC == CTLZ)
  155.                 {
  156.                 *sC = '\0';
  157.                 bMore = FALSE;
  158.                 }
  159.               else
  160.                 {                       /* get second param = user name */
  161.                 if (iPass) *sUser++ = *sC;
  162.                 else wBytes++;
  163.                 }
  164.               }
  165.             if (iPass) *sUser++ = '\0'; /* put separator null */
  166.             else wBytes++;
  167.             }
  168.           else
  169.             {
  170.             if (iPass) sUser = sUserOld; /* restore old ptrs if bad line */
  171.             else wBytes = wBytesOld;
  172.             }
  173.           }
  174.         } /* if (*caLine */
  175.       } /* while (bMore */
  176.     if (!wBytes) wStatus = NO_USERS;
  177.     else if (iPass == 0)
  178.       {
  179.       wBytes++;                         /* for final '\0' */
  180. #ifdef OS2
  181.       DosAllocSeg(wBytes, &selUser, 0); /* allocate user area */
  182.       sUserBase = MAKEP(selUser, 0);
  183. #else
  184.       if ((sUserBase = malloc((size_t) wBytes)) == NULL)
  185.         wStatus = TOO_MANY;
  186. #endif
  187.       sUser = sUserBase;
  188.       rewind(pFile);
  189.       }
  190.     } /* for (iPass < 2 && !wStatus */
  191.   fclose(pFile);
  192.   }
  193. #ifdef OS2
  194. for (sC = cgaIniPath; *sC && *sC != '.'; sC++) ; /* set up PID file name */
  195. strcpy(sC, ".PID");
  196. if ((pFile = fopen(cgaIniPath, "w")) != NULL)
  197.   {
  198.   if (DosGetPID(&PIDInfo) == SUCCESS)   /* write PID to file if OS2 */
  199.     fputs(itoa(PIDInfo.pid, caLine, 10), pFile);
  200.   fclose(pFile);
  201.   }
  202. #endif
  203. if (!wStatus)
  204.   {
  205.   *sUser = '\0';                        /* put final null to user area */
  206.   sUser = sUserBase;                    /* and initialize user ptr */
  207.   }
  208. #ifdef TEST
  209. while (*sUser && !wStatus)
  210.   {
  211. #else
  212.   #ifdef OS2
  213. while (!wStatus)
  214.   {
  215.   DosSleep(lDelay);
  216.   if (*sUser == '\0') sUser = sUserBase; /* repeat if OS2 */
  217.   #else
  218. while (*sUser && !wStatus)
  219.   {
  220.   #endif
  221. #endif
  222.   sC = sSubDir;
  223.   while (*sUser) *sC++ = *sUser++;      /* copy user subdir to SubDir */
  224.   strcpy(sC, "\\*.*");                  /* find any file */
  225.   sUser++;                              /* skip null */
  226. #ifdef OS2
  227.   ugiDir = 1;
  228.   ugiSearch = 1;                        /* check for files in user subdir */
  229.   if (DosFindFirst(cgaMailPath, &ugiDir, FA_NORMAL,
  230.     &gFFBlk, sizeof(gFFBlk), &ugiSearch, 0L) == SUCCESS)
  231. #else
  232.   if (findfirst(cgaMailPath, &gFFBlk, FA_NORMAL) == SUCCESS)
  233. #endif                                  /* if files, send msg to user */
  234.     {
  235.     wErr = wNetSend(sUser, cgaMsg, USER_NUM); /* abort if too many errors */
  236.     if (wErr)
  237.       {
  238.       if (++iErrors > MAX_ERRORS) wStatus = wErr;
  239.       }
  240.     else if (iErrors) iErrors--;
  241.     }
  242.   while (*sUser++) ;                    /* position past user name & null */
  243.   }                                     /* loop */
  244. #ifdef OS2
  245. if ((pFile = fopen(cgaIniPath, "w")) != NULL) /* record exit status */
  246.   {
  247.   fputs("Status ", pFile);
  248.   fputs(itoa(wStatus, caLine, 10), pFile);
  249.   fclose(pFile);
  250.   }
  251. #else
  252. if (wStatus) putch(BELL);
  253. printf("\nnetmmind status: %d", wStatus);
  254. #endif
  255. return(wStatus);
  256. } /* main */
  257.  
  258. /* send sMsg to sName from iNbNum */
  259. #ifdef OS2
  260. word wNetSend(char far *sName, string sMsg, short iNbNum)
  261. {
  262. short  i;
  263. string sC;
  264. word   wStatus;
  265. word   wHandle;                         /* handle to logical network */
  266. wStatus = NetBiosOpen("NET1", 0, NB_REGULAR, &wHandle);
  267. if (wStatus == SUCCESS)
  268.   {
  269.   memset(&gNcb, 0, sizeof(NCB));
  270.   gNcb.ncb_command = NCBDGSEND;
  271.   gNcb.ncb_num = (byte) iNbNum;
  272.   for (i = 0; sName[i] && i < NETBIOS_NAME_LEN; i++)
  273.     gNcb.ncb_callname[i] = sName[i];
  274.   for (sC = gNcb.ncb_callname + NETBIOS_NAME_LEN - 2;
  275.        sC >= gNcb.ncb_callname; sC--)
  276.     *sC = ((*sC) ? toupper(*sC) : ' ');
  277.   gNcb.ncb_buffer = (void far *) sMsg;
  278.   gNcb.ncb_length = strlen(sMsg);
  279.   gNcb.ncb_cmd_cplt = (byte) STATUS_UNKNOWN;
  280.   NetBiosSubmit(wHandle, NCB_CHAIN_SINGLE, &gNcb);
  281.   NetBiosClose(wHandle, 0);
  282.   wStatus = (word) gNcb.ncb_retcode;
  283.   }
  284. return(wStatus);
  285. } /* wNetSend for OS/2 */ 
  286. #else
  287. word wNetSend(string sName, string sMsg, short iNbNum)
  288. {
  289. char far *sfNcb;
  290. short  i;
  291. string sC;
  292. struct SREGS SegRegs;
  293. union  REGS InRegs, OutRegs;
  294. memset(&gNcb, 0, sizeof(NCB));
  295. gNcb.ncb_command = SEND_DATAGRAM_WAIT;
  296. gNcb.ncb_num = iNbNum;
  297. for (i = 0; sName[i] && i < NETBIOS_NAME_LEN; i++)
  298.   gNcb.ncb_callname[i] = sName[i];
  299. for (sC = gNcb.ncb_callname + NETBIOS_NAME_LEN - 2;
  300.      sC >= gNcb.ncb_callname; sC--)
  301.   *sC = ((*sC) ? toupper(*sC) : ' ');
  302. gNcb.ncb_buffer = (void far *) sMsg;
  303. gNcb.ncb_length = strlen(sMsg);
  304. gNcb.ncb_cmd_cplt = STATUS_UNKNOWN;
  305. #ifdef MSC
  306. sfNcb = (char far *) &gNcb;
  307. SegRegs.es = FP_SEG(sfNcb);
  308. InRegs.x.bx = FP_OFF(sfNcb);
  309. #else
  310. SegRegs.es = FP_SEG(&gNcb);
  311. InRegs.x.bx = FP_OFF(&gNcb);
  312. #endif
  313. InRegs.x.ax = 0x0100;
  314. int86x(0x5c, &InRegs, &OutRegs, &SegRegs);
  315. return(gNcb.ncb_cmd_cplt);
  316. } /* wNetSend for DOS */ 
  317. #endif
  318.