home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / comm / mail / smail / src / rcs / batch_smtp.c,v < prev    next >
Text File  |  1993-12-21  |  11KB  |  593 lines

  1. head    1.6;
  2. access;
  3. symbols
  4.     C_1:1.6;
  5. locks; strict;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.6
  10. date    93.11.15.22.34.16;    author Aussem;    state Exp;
  11. branches;
  12. next    1.5;
  13.  
  14. 1.5
  15. date    93.10.31.14.23.31;    author Aussem;    state Exp;
  16. branches;
  17. next    1.4;
  18.  
  19. 1.4
  20. date    93.10.28.23.52.17;    author Aussem;    state Exp;
  21. branches;
  22. next    1.3;
  23.  
  24. 1.3
  25. date    93.10.28.23.30.31;    author Aussem;    state Exp;
  26. branches;
  27. next    1.2;
  28.  
  29. 1.2
  30. date    93.10.28.23.15.11;    author Aussem;    state Exp;
  31. branches;
  32. next    1.1;
  33.  
  34. 1.1
  35. date    93.10.17.21.44.32;    author Aussem;    state Exp;
  36. branches;
  37. next    ;
  38.  
  39.  
  40. desc
  41. @append BSMTP mails to uuspool:batch/hostname
  42. @
  43.  
  44.  
  45. 1.6
  46. log
  47. @NOFROM define for removing the from line
  48. CRLF for adding \n\r to the mail (needed for SMTP over TCPIP)
  49. @
  50. text
  51. @/*
  52.  * Batches a mail to uuspool:batch/<host>
  53.  * in BSMTP format
  54.  *
  55.  * This program is free software; you can redistribute it and/or
  56.  * modify it under the terms of the GNU General Public License as
  57.  * published by the Free Software Foundation; either version 2 of
  58.  * the License, or (at your option) any later version.
  59.  *
  60.  * This program is distributed in the hope that it will be useful,
  61.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  62.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  63.  * General Public License for more details.
  64.  *
  65.  * You should have received a copy of the GNU General Public License
  66.  * along with this program; if not, write to the Free Software
  67.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  68.  *
  69.  * $Log: batch_smtp.c,v $
  70.  * Revision 1.5  1993/10/31  14:23:31  Aussem
  71.  * \n -> \r\n conversion removed
  72.  * only need in SMTP with TCPIP
  73.  *
  74.  * Revision 1.4  1993/10/28  23:52:17  Aussem
  75.  * fmode() deleted. There are problems with the length of the mail
  76.  *
  77.  * Revision 1.2  1993/10/28  23:15:11  Aussem
  78.  * \n is now converted to \r\n
  79.  * file reading now lines instead the full file
  80.  *
  81.  * Revision 1.1  1993/10/17  21:44:32  Aussem
  82.  * Initial revision
  83.  *
  84.  *
  85.  */
  86.  
  87. static char     *rcsid="$Id: batch_smtp.c,v 1.5 1993/10/31 14:23:31 Aussem Exp Aussem $";
  88.  
  89. #include <stdio.h>
  90. #include <string.h>
  91. #include <stdlib.h>
  92. #include <dos.h>
  93. #include <proto/dos.h>
  94. #include <pragmas/dos_pragmas.h>
  95. #include <proto/exec.h>
  96. #include <pragmas/exec_pragmas.h>
  97.  
  98. extern struct DosLibrary *DOSBase;
  99.  
  100. /* global Vars for this modul */
  101. static const char *uuspool = "UUSPOOL:batch";
  102. static const char *version = "$VER: batch_smtp V1.0 © 1993 by Ralph-Thomas Aussem ("__DATE__","__TIME__")";
  103. static const int maxretry = 30;
  104. static const int retrysecs = 20;
  105. static char *argv0 = "batch_smtp";
  106.  
  107. /* prototypes */
  108. static long filelength (char *);
  109. static void sleep (unsigned long time);
  110. char * findconfig(char *);
  111.  
  112.  
  113. /*
  114.  * sleep():
  115.  * wait <time> seconds
  116.  */
  117. static void
  118. sleep (unsigned long time)
  119. {
  120.   Delay (50L * time);
  121. }
  122.  
  123. /*
  124.  * filelength():
  125.  * returns the length of file <fname>
  126.  * it returns -1 if the file does not exists or locked 
  127.  * by someone else
  128.  */
  129. static
  130. long
  131. filelength (char *fname)
  132. {
  133.   struct FileInfoBlock __aligned fileinfo;
  134.   struct FileLock *lock;
  135.   long groesse=-2;
  136.  
  137.   if (!(lock = (struct FileLock *) Lock (fname, ACCESS_READ)))
  138.     return (-1);
  139.   Examine ((BPTR) lock, &fileinfo);
  140.   if(fileinfo.fib_DirEntryType<0)
  141.       groesse = (long) fileinfo.fib_Size;
  142.   UnLock ((BPTR) lock);
  143.   return (groesse);
  144. }
  145.  
  146. long getpid(void)
  147. {
  148. struct Process *proc;
  149.  
  150. Forbid();
  151. proc=(struct Process *)FindTask(NULL);
  152. Permit();
  153. if(proc)return(proc->pr_TaskNum);
  154. return(0);
  155. }
  156.  
  157. int lock_with_file(char *lockfilename)
  158. {
  159.     FILE *file;
  160.     char buf[BUFSIZ];
  161.  
  162.     while( ( file = fopen( lockfilename, "r" ) ) != NULL )
  163.     {
  164.         fgets( buf, sizeof(buf), file );
  165.         fclose( file );
  166. #ifdef DEBUG
  167.         printf( "Mail locked by proc '%s'. Waiting 10 secs\n", buf);
  168. #endif
  169.         sleep( 10 );
  170.     }
  171.  
  172. #ifdef DEBUG
  173.     printf("locking Mail.\n");
  174. #endif
  175.     if( ( file = fopen( lockfilename, "w" ) ) == NULL ) return -1;
  176.     fprintf( file, "%d\n", getpid() );
  177.     fclose( file );
  178.     return 0;
  179. }
  180.  
  181. int unlock_with_file( char *lockfilename )
  182. {
  183. #ifdef DEBUG
  184.     printf("unlock Mail.\n");
  185. #endif
  186.     return remove( lockfilename );
  187. }
  188.  
  189.  
  190. /*
  191.  * MAIN
  192.  */
  193. int
  194. main (int argc, char *argv[])
  195. {
  196.   FILE *in,*out;
  197.   char file[128],lockfile[128], puf[128],*host,*batch,*myhost,*p;
  198.   char text[1024];
  199.   long len;
  200.   int rc, retry, flag=0;
  201.  
  202.   argv0 = argv[0];
  203.  
  204.   if (argc != 4)
  205.     {
  206.       printf ("usage %s file host myhostname\n", argv0);
  207.       exit (10);
  208.     }
  209.  
  210.   batch=argv[1];
  211.   host=argv[2];
  212.   myhost=argv[3];
  213.  
  214.   retry=0;
  215.   printf("sending mail to '%s'...",host);
  216.  
  217.   strcpy (lockfile, "T:");
  218.   strcat (lockfile, host);
  219.   strcat (lockfile,".lck");
  220.   lock_with_file(lockfile);
  221.  
  222.   if(filelength((char *)uuspool)!=-2)
  223.       {
  224.    int rc;
  225.  
  226.    rc=mkdir(uuspool);
  227.    if(rc)
  228.        printf("\n%s: error make '%s'\n",argv0,uuspool);
  229.    }
  230.  
  231.   strcpy (file, uuspool);
  232.   AddPart(file,host,sizeof(file));
  233.  
  234.   len=filelength(file);
  235.  
  236.   if(len>0)
  237.       {
  238.       strcpy (puf, "copy ");
  239.       strcat (puf, file);
  240.       strcat (puf, " to ");
  241.       strcat (puf, file);
  242.       strcat (puf, ".o");
  243.       rc = system (puf);
  244.       if (rc)
  245.         {
  246.           printf ("\n%s: Unable to create backup file (%s)\n", argv0, puf);
  247.             unlock_with_file(lockfile);
  248.           exit (10);
  249.         }
  250.    }
  251.   strcat (file, ".o");
  252.  
  253.   do
  254.     {
  255.       retry++;
  256.       out = fopen (file, "r+");
  257.       if (out)
  258.           {
  259.              fseek(out,-strlen("QUIT\n"),SEEK_END);
  260.             break;
  261.             }
  262.       out = fopen (file, "w");
  263.       if (out)
  264.           {
  265.          fprintf(out,"HELO %s\n",myhost);
  266.             break;
  267.             }
  268.       printf ("\n%s: unable to open %s, waiting %d secs\n", argv0, file, retrysecs);
  269.       sleep (retrysecs);
  270.     }
  271.   while (retry < maxretry);
  272.  
  273.   if (!out)
  274.     {
  275.       printf ("\n%s: Unable to open the backup batch-folder '%s' \n", argv0,file);
  276.       remove (file);
  277.         unlock_with_file(lockfile);
  278.       exit (10);
  279.     }
  280.  
  281.  in = fopen (batch, "r");
  282.  if (!in)
  283.     {
  284.       printf ("\n%s: Unable to open the batch file '%s' \n", argv0,batch);
  285.       remove (file);
  286.         unlock_with_file(lockfile);
  287.       exit (10);
  288.     }
  289.  
  290.  while(fgets(text, sizeof(text), in) != NULL)
  291.      {
  292. #ifdef CRLF
  293. /*
  294.  * the conversion from \n to \r\n is
  295.  * only needed for SMTP over TCP/IP connections
  296.  */
  297.          p=strrchr(text,'\n');
  298.          if(p)
  299.              {
  300.             p[0]='\r';
  301.             p[1]='\n';
  302.             p[2]='\0';
  303.              }
  304. #endif /* CRLF */
  305. #ifdef NOFROM
  306. /*
  307.  * removes the From line
  308.  *
  309.  */
  310.          /* from line */
  311.          if(!flag && (strnicmp(text,"From ",5)==0))
  312.                  {flag=1;continue;}
  313.          /* continue line */
  314.          if(flag==1)
  315.          {
  316.                 if(text[0]=='\t')
  317.                  {flag=1;continue;}
  318.              else
  319.                  {flag=2;}
  320.          }
  321. #endif /* NOFROM */
  322.             (void) fputs(text, out);
  323.     }
  324.  
  325.   fclose (in);
  326.   fclose (out);
  327.  
  328.   strcpy (puf, "copy ");
  329.   strcat (puf, file);
  330.   strcat (puf, " to ");
  331.   strcat (puf, file);
  332.   len = strlen (puf);
  333.   if (len >= 2)
  334.     puf[len - 2] = '\0';
  335.   rc = system (puf);
  336.   if (rc)
  337.     {
  338.       printf ("\n%s: Unable to rename backup file (%s)\n", argv0, puf);
  339.       remove (file);
  340.         unlock_with_file(lockfile);
  341.       exit (10);
  342.     }
  343.  
  344.   remove (file);
  345.   unlock_with_file(lockfile);
  346.   exit (0);
  347. }
  348. @
  349.  
  350.  
  351. 1.5
  352. log
  353. @\n -> \r\n conversion removed
  354. only need in SMTP with TCPIP
  355. @
  356. text
  357. @d20 4
  358. d37 1
  359. a37 1
  360. static char     *rcsid="$Id: batch_smtp.c,v 1.4 1993/10/28 23:52:17 Aussem Exp Aussem $";
  361. d150 1
  362. a150 1
  363.   int rc, retry;
  364. d242 5
  365. a247 2
  366. /* the conversion from \n to \r\n is only needed for SMTP on TCP/IP 
  367.    connections
  368. d254 18
  369. a271 1
  370. */
  371. @
  372.  
  373.  
  374. 1.4
  375. log
  376. @fmode() deleted. There are problems with the length of the mail
  377. @
  378. text
  379. @d20 3
  380. d33 1
  381. a33 1
  382. static char     *rcsid="$Id: batch_smtp.c,v 1.2 1993/10/28 23:15:11 Aussem Exp $";
  383. d205 1
  384. a205 1
  385.              fseek(out,-strlen("QUIT\r\n"),SEEK_END);
  386. d211 1
  387. a211 1
  388.          fprintf(out,"HELO %s\r\n",myhost);
  389. d239 2
  390. d247 1
  391. @
  392.  
  393.  
  394. 1.3
  395. log
  396. @converting \n to \r\n by using fmode()
  397. @
  398. text
  399. @d30 1
  400. a30 1
  401. static char     *rcsid="$Id: batch_smtp.c,v 1.2 1993/10/28 23:15:11 Aussem Exp Aussem $";
  402. d140 1
  403. a140 1
  404.   char file[128],lockfile[128], puf[128],*host,*batch,*myhost;
  405. d232 1
  406. a232 1
  407.  fmode(out,0);
  408. d234 10
  409. a243 1
  410.             fputs(text, out);
  411. @
  412.  
  413.  
  414. 1.2
  415. log
  416. @\n is now converted to \r\n
  417. file reading now lines instead the full file
  418. @
  419. text
  420. @d20 4
  421. d30 1
  422. a30 1
  423. static char     *rcsid="$Id: batch_smtp.c,v 1.1 1993/10/17 21:44:32 Aussem Exp Aussem $";
  424. d140 1
  425. a140 1
  426.   char file[128],lockfile[128], puf[128],*host,*batch,*myhost,*p;
  427. d232 1
  428. a232 1
  429.  
  430. d234 1
  431. a234 10
  432.      {
  433.          p=strrchr(text,'\n');
  434.          if(p)
  435.              {
  436.             p[0]='\r';
  437.             p[1]='\n';
  438.             p[2]='\0';
  439.              }
  440.             (void) fputs(text, out);
  441.     }
  442. @
  443.  
  444.  
  445. 1.1
  446. log
  447. @Initial revision
  448. @
  449. text
  450. @d19 3
  451. a21 1
  452.  * $Log:$
  453. d23 1
  454. d26 1
  455. a26 1
  456. static char     *rcsid="$Id$";
  457. a46 1
  458. static char *readfile (char *);
  459. a127 56
  460. /*
  461.  * readfile():
  462.  * read file <filename> and returns the buffer
  463.  *
  464.  */
  465. static
  466. char *
  467. readfile (char *filename)
  468. {
  469.   char *puf;
  470.   size_t len, read;
  471.   FILE *fp;
  472.  
  473. #ifdef DEBUG
  474.     printf("Reading %s.\n",filename);
  475. #endif
  476.  
  477.   if (!filename)
  478.     return (NULL);
  479.  
  480.   len = filelength (filename);
  481.   if (len < 1)
  482.     {
  483.       printf ("\n%s: unable to open file %s\n", argv0, filename);
  484.       return (NULL);
  485.     }
  486.  
  487.   puf = malloc ((len + 1) * sizeof (char));
  488.   if (!puf)
  489.     {
  490.       printf ("\n%s: unable to allocate %d bytes\n", argv0, (len + 1) * sizeof (char));
  491.       free (puf);
  492.       return (NULL);
  493.     }
  494.  
  495.   fp = fopen (filename, "r");
  496.   if (!fp)
  497.     {
  498.       printf ("\n%s: unable to open file %s\n", argv0, filename);
  499.       free (puf);
  500.       return (NULL);
  501.     }
  502.  
  503.   read = fread (puf, len, 1, fp) * len;
  504.   if (read != len)
  505.     {
  506.       printf ("\n%s: unable to read from %s %d (%d) bytes\n", argv0, filename, len, read);
  507.       free (puf);
  508.       fclose (fp);
  509.       return (NULL);
  510.     }
  511.   puf[len]='\0';
  512.   fclose (fp);
  513.   return (puf);
  514. }
  515.  
  516. d135 3
  517. a137 2
  518.   FILE *fp;
  519.   char file[128],lockfile[128], puf[128], *text,*host,*batch,*myhost;
  520. a191 9
  521.   text = readfile (batch);
  522.   if (!text)
  523.     {
  524.       /* error messages are generated by readfile() */
  525.       remove (file);
  526.         unlock_with_file(lockfile);
  527.       exit (10);
  528.     }
  529.  
  530. d195 2
  531. a196 2
  532.       fp = fopen (file, "r+");
  533.       if (fp)
  534. d198 1
  535. a198 1
  536.              fseek(fp,-strlen("QUIT\n"),SEEK_END);
  537. d201 2
  538. a202 2
  539.       fp = fopen (file, "w");
  540.       if (fp)
  541. d204 1
  542. a204 1
  543.          fprintf(fp,"HELO %s\n",myhost);
  544. d212 1
  545. a212 1
  546.   if (!fp)
  547. a215 2
  548.       if (text)
  549.             free (text);
  550. d220 2
  551. a221 29
  552.   len = strlen (text);
  553.  
  554. /*
  555.   {
  556.     register char *c,*last_c,*last_last_c;
  557.  
  558.    last_last_c=text;
  559.    last_c=text+1;
  560.    if(last_c!='\0' && last_last_c!='\0')
  561.        {
  562.        fputc(*last_last_c,fp);
  563.        fputc(*last_c,fp);
  564.        c=text+2;
  565.         while(*c!='\0')
  566.                 {
  567.             if(*last_last_c=='\n' &&
  568.                *last_c=='.' &&
  569.                     *c=='\n')
  570.                     fputc('.',fp);
  571.  
  572.                 fputc(*c,fp);
  573.                 last_last_c++;
  574.                 last_c++;
  575.                 c++;
  576.                 }
  577.         }
  578.   }
  579. */
  580.   if ((fwrite (text, len, 1, fp) * len) != len)
  581. d223 1
  582. a223 4
  583.       printf ("%s: Error appending mail to batch-folder %s\n", argv0, file);
  584.       fclose (fp);
  585.       if (text)
  586.             free (text);
  587. d228 15
  588. a242 3
  589.   fclose (fp);
  590.   if (text)
  591.     free (text);
  592. @
  593.