home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / b / bmh02src.zip / SMTP.C < prev    next >
C/C++ Source or Header  |  1992-08-19  |  5KB  |  196 lines

  1. /*
  2.    smtp.c
  3.  
  4.    smtp queue management routines
  5.  
  6.    Taken from bm.
  7.  
  8.    Copyright 1986 Bdale Garbee, All Rights Reserved.
  9.    Permission granted for non-commercial copying and use, provided
  10.    this notice is retained.
  11.    Copyright 1987 1988 Dave Trulli NN2Z, All Rights Reserved.
  12.    Permission granted for non-commercial copying and use, provided
  13.    this notice is retained.
  14.  
  15.    911218 : Added this header
  16.    920802 : Added file locking to get_msgid, bm wasn't locking at all.
  17.    920819 : Allow killing of mail in a mqueue directory whose path has
  18.             '.' in..
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <io.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include <time.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <fcntl.h>
  29. #include <io.h>
  30. #include "smtp.h"
  31. #include "lock.h"
  32. #include "misc.h"
  33. #include "pc.h"
  34. #include "rc.h"
  35. #include "help.h"
  36. #include "pager.h"
  37.  
  38. #ifdef BMH
  39. #define main smtp_main
  40. #endif
  41.  
  42. /*
  43.  * get a message id from the sequence file
  44.  */
  45. long 
  46. get_msgid(char *mqueue)
  47. {
  48.    FILE *sfile = fopenlocked(mqueue, "sequence", ".seq", "r+");
  49.    long sequence = 0L;
  50.  
  51.    /*
  52.     *  if sequence file exists, get the value, otherwise set it
  53.     */
  54.    if (sfile != NULL) {
  55.       if ( fscanf(sfile, "%ld", &sequence) == EOF)
  56.          sequence = -1L;
  57.       else {
  58.          /*
  59.           *  Keep it in range, use 8 digit number for dos name prefix.
  60.           */
  61.          if ( (sequence < 0L) || (sequence > 99999999L) )
  62.             sequence = 0L;
  63.          if (fseek(sfile, 0L, SEEK_SET) != 0)
  64.             sequence = -1L;
  65.          }
  66.       }
  67.    else {
  68.       fprintf(stderr, "smtp: creating sequence file\n");
  69.       if ( (sfile = fopenlocked(mqueue, "sequence", ".seq", "w")) == NULL)
  70.          return -1L;
  71.       }
  72.  
  73.    if (sequence != -1L)
  74.       fprintf(sfile, "%ld", ++sequence);
  75.  
  76.    fcloselocked(sfile, mqueue, "sequence");
  77.  
  78.    return sequence;
  79. }
  80.  
  81. /*
  82.  * list jobs wating to be sent in the mqueue
  83.  */
  84. void
  85. listqueue(char *mqueue)
  86. {
  87.    char tstring[80];
  88.    char workfile[80];
  89.    char line[20];
  90.    char host[LINELEN];
  91.    char to[LINELEN];
  92.    char from[LINELEN];
  93.    char *p;
  94.    char   status;
  95.    struct stat stbuf;
  96.    struct tm *tminfo;
  97.    FILE *fp;
  98.  
  99.    char buf[256];
  100.  
  101.    page_setup();
  102.    page_puts("S     Job    Size Date  Time  Host                 From");
  103.  
  104.    sprintf(workfile,"%s/*.wrk", mqueue);  /* *.wrk */
  105.    filedir(workfile, 0, line);
  106.  
  107.    while(line[0] != '\0') {
  108.       sprintf(tstring,"%s/%s", mqueue,line);
  109.       if ((fp = fopen(tstring,"r")) == NULL) {
  110.          perror(tstring);
  111.          continue;
  112.       }
  113.       if ((p = strrchr(line,'.')) != NULL)
  114.          *p = '\0';
  115.       sprintf(tstring,"%s/%s.lck",mqueue,line);
  116.       if (access(tstring,0))
  117.          status = ' ';
  118.       else
  119.          status = 'L';
  120.       sprintf(tstring, "%s/%s%s", mqueue, line, EXT);
  121.       stat(tstring,&stbuf);
  122.       tminfo = localtime(&stbuf.st_ctime);
  123.       fgets(host,sizeof(host),fp);
  124.       rip(host);
  125.       fgets(from,sizeof(from),fp);
  126.       rip(from);
  127.  
  128.       sprintf(buf, "%c %7s %7ld %02d/%02d %02d:%02d %-20s %s",
  129.          status, line, stbuf.st_size,
  130.          tminfo ->tm_mon+1,
  131.          tminfo->tm_mday,
  132.          tminfo->tm_hour,
  133.          tminfo->tm_min,
  134.          host,from);
  135.       page_puts(buf);
  136.  
  137.       while (fgets(to,sizeof(to),fp) != NULL) {
  138.          rip(to);
  139.          sprintf(buf, "         %s", to);
  140.          page_puts(buf);
  141.       }
  142.  
  143.       (void) fclose(fp);
  144.       filedir(workfile,1,line);
  145.    }
  146.    fflush(stdout);
  147. }
  148.  
  149. int
  150. killjob(char *mqueue, char *j)
  151. {
  152.    char s[LINELEN];
  153.  
  154.    sprintf(s, "%s/%s.lck", mqueue, j);
  155.  
  156.    if (access(s, 0) == 0) {
  157.       char tbuf[20];
  158.  
  159.       fprintf(stderr, "smtp: warning job %s is locked by SMTP, remove? (y,n)", j);
  160.       fgets(tbuf, sizeof(tbuf), stdin);
  161.       if (tolower(*tbuf) != 'y')
  162.          return -1;
  163.       if (unlink(s) == -1)
  164.          fprintf(stderr, "smtp: error deleting %s\n", s);
  165.       }
  166.  
  167.    sprintf(s, "%s/%s.wrk", mqueue, j);
  168.    if (unlink(s) == -1)
  169.       fprintf(stderr, "smtp: job %s not found\n", s);
  170.  
  171.    sprintf(s, "%s/%s.txt", mqueue, j);
  172.  
  173.    return unlink(s);
  174. }
  175.  
  176. #ifdef SMTP_PROG
  177. int
  178. main(int argc, char *argv[])
  179. {
  180.    int i;
  181.  
  182.    dohelp(argc, argv, "smtp [<job 1> <job 2> ... <job n>]");
  183.  
  184.    if (setupbm() == -1)
  185.       return -1;
  186.  
  187.    if (argc == 1)
  188.       listqueue(getrc(mqueue));
  189.    else                                     /* Kill jobs */
  190.       for (i=1; i<argc; i++)
  191.          killjob(getrc(mqueue), argv[i]);
  192.  
  193.    return 0;
  194. }
  195. #endif
  196.