home *** CD-ROM | disk | FTP | other *** search
- head 1.6;
- access;
- symbols
- C_1:1.6;
- locks; strict;
- comment @ * @;
-
-
- 1.6
- date 93.11.15.22.34.16; author Aussem; state Exp;
- branches;
- next 1.5;
-
- 1.5
- date 93.10.31.14.23.31; author Aussem; state Exp;
- branches;
- next 1.4;
-
- 1.4
- date 93.10.28.23.52.17; author Aussem; state Exp;
- branches;
- next 1.3;
-
- 1.3
- date 93.10.28.23.30.31; author Aussem; state Exp;
- branches;
- next 1.2;
-
- 1.2
- date 93.10.28.23.15.11; author Aussem; state Exp;
- branches;
- next 1.1;
-
- 1.1
- date 93.10.17.21.44.32; author Aussem; state Exp;
- branches;
- next ;
-
-
- desc
- @append BSMTP mails to uuspool:batch/hostname
- @
-
-
- 1.6
- log
- @NOFROM define for removing the from line
- CRLF for adding \n\r to the mail (needed for SMTP over TCPIP)
- @
- text
- @/*
- * Batches a mail to uuspool:batch/<host>
- * in BSMTP format
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * $Log: batch_smtp.c,v $
- * Revision 1.5 1993/10/31 14:23:31 Aussem
- * \n -> \r\n conversion removed
- * only need in SMTP with TCPIP
- *
- * Revision 1.4 1993/10/28 23:52:17 Aussem
- * fmode() deleted. There are problems with the length of the mail
- *
- * Revision 1.2 1993/10/28 23:15:11 Aussem
- * \n is now converted to \r\n
- * file reading now lines instead the full file
- *
- * Revision 1.1 1993/10/17 21:44:32 Aussem
- * Initial revision
- *
- *
- */
-
- static char *rcsid="$Id: batch_smtp.c,v 1.5 1993/10/31 14:23:31 Aussem Exp Aussem $";
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <dos.h>
- #include <proto/dos.h>
- #include <pragmas/dos_pragmas.h>
- #include <proto/exec.h>
- #include <pragmas/exec_pragmas.h>
-
- extern struct DosLibrary *DOSBase;
-
- /* global Vars for this modul */
- static const char *uuspool = "UUSPOOL:batch";
- static const char *version = "$VER: batch_smtp V1.0 © 1993 by Ralph-Thomas Aussem ("__DATE__","__TIME__")";
- static const int maxretry = 30;
- static const int retrysecs = 20;
- static char *argv0 = "batch_smtp";
-
- /* prototypes */
- static long filelength (char *);
- static void sleep (unsigned long time);
- char * findconfig(char *);
-
-
- /*
- * sleep():
- * wait <time> seconds
- */
- static void
- sleep (unsigned long time)
- {
- Delay (50L * time);
- }
-
- /*
- * filelength():
- * returns the length of file <fname>
- * it returns -1 if the file does not exists or locked
- * by someone else
- */
- static
- long
- filelength (char *fname)
- {
- struct FileInfoBlock __aligned fileinfo;
- struct FileLock *lock;
- long groesse=-2;
-
- if (!(lock = (struct FileLock *) Lock (fname, ACCESS_READ)))
- return (-1);
- Examine ((BPTR) lock, &fileinfo);
- if(fileinfo.fib_DirEntryType<0)
- groesse = (long) fileinfo.fib_Size;
- UnLock ((BPTR) lock);
- return (groesse);
- }
-
- long getpid(void)
- {
- struct Process *proc;
-
- Forbid();
- proc=(struct Process *)FindTask(NULL);
- Permit();
- if(proc)return(proc->pr_TaskNum);
- return(0);
- }
-
- int lock_with_file(char *lockfilename)
- {
- FILE *file;
- char buf[BUFSIZ];
-
- while( ( file = fopen( lockfilename, "r" ) ) != NULL )
- {
- fgets( buf, sizeof(buf), file );
- fclose( file );
- #ifdef DEBUG
- printf( "Mail locked by proc '%s'. Waiting 10 secs\n", buf);
- #endif
- sleep( 10 );
- }
-
- #ifdef DEBUG
- printf("locking Mail.\n");
- #endif
- if( ( file = fopen( lockfilename, "w" ) ) == NULL ) return -1;
- fprintf( file, "%d\n", getpid() );
- fclose( file );
- return 0;
- }
-
- int unlock_with_file( char *lockfilename )
- {
- #ifdef DEBUG
- printf("unlock Mail.\n");
- #endif
- return remove( lockfilename );
- }
-
-
- /*
- * MAIN
- */
- int
- main (int argc, char *argv[])
- {
- FILE *in,*out;
- char file[128],lockfile[128], puf[128],*host,*batch,*myhost,*p;
- char text[1024];
- long len;
- int rc, retry, flag=0;
-
- argv0 = argv[0];
-
- if (argc != 4)
- {
- printf ("usage %s file host myhostname\n", argv0);
- exit (10);
- }
-
- batch=argv[1];
- host=argv[2];
- myhost=argv[3];
-
- retry=0;
- printf("sending mail to '%s'...",host);
-
- strcpy (lockfile, "T:");
- strcat (lockfile, host);
- strcat (lockfile,".lck");
- lock_with_file(lockfile);
-
- if(filelength((char *)uuspool)!=-2)
- {
- int rc;
-
- rc=mkdir(uuspool);
- if(rc)
- printf("\n%s: error make '%s'\n",argv0,uuspool);
- }
-
- strcpy (file, uuspool);
- AddPart(file,host,sizeof(file));
-
- len=filelength(file);
-
- if(len>0)
- {
- strcpy (puf, "copy ");
- strcat (puf, file);
- strcat (puf, " to ");
- strcat (puf, file);
- strcat (puf, ".o");
- rc = system (puf);
- if (rc)
- {
- printf ("\n%s: Unable to create backup file (%s)\n", argv0, puf);
- unlock_with_file(lockfile);
- exit (10);
- }
- }
- strcat (file, ".o");
-
- do
- {
- retry++;
- out = fopen (file, "r+");
- if (out)
- {
- fseek(out,-strlen("QUIT\n"),SEEK_END);
- break;
- }
- out = fopen (file, "w");
- if (out)
- {
- fprintf(out,"HELO %s\n",myhost);
- break;
- }
- printf ("\n%s: unable to open %s, waiting %d secs\n", argv0, file, retrysecs);
- sleep (retrysecs);
- }
- while (retry < maxretry);
-
- if (!out)
- {
- printf ("\n%s: Unable to open the backup batch-folder '%s' \n", argv0,file);
- remove (file);
- unlock_with_file(lockfile);
- exit (10);
- }
-
- in = fopen (batch, "r");
- if (!in)
- {
- printf ("\n%s: Unable to open the batch file '%s' \n", argv0,batch);
- remove (file);
- unlock_with_file(lockfile);
- exit (10);
- }
-
- while(fgets(text, sizeof(text), in) != NULL)
- {
- #ifdef CRLF
- /*
- * the conversion from \n to \r\n is
- * only needed for SMTP over TCP/IP connections
- */
- p=strrchr(text,'\n');
- if(p)
- {
- p[0]='\r';
- p[1]='\n';
- p[2]='\0';
- }
- #endif /* CRLF */
- #ifdef NOFROM
- /*
- * removes the From line
- *
- */
- /* from line */
- if(!flag && (strnicmp(text,"From ",5)==0))
- {flag=1;continue;}
- /* continue line */
- if(flag==1)
- {
- if(text[0]=='\t')
- {flag=1;continue;}
- else
- {flag=2;}
- }
- #endif /* NOFROM */
- (void) fputs(text, out);
- }
-
- fclose (in);
- fclose (out);
-
- strcpy (puf, "copy ");
- strcat (puf, file);
- strcat (puf, " to ");
- strcat (puf, file);
- len = strlen (puf);
- if (len >= 2)
- puf[len - 2] = '\0';
- rc = system (puf);
- if (rc)
- {
- printf ("\n%s: Unable to rename backup file (%s)\n", argv0, puf);
- remove (file);
- unlock_with_file(lockfile);
- exit (10);
- }
-
- remove (file);
- unlock_with_file(lockfile);
- exit (0);
- }
- @
-
-
- 1.5
- log
- @\n -> \r\n conversion removed
- only need in SMTP with TCPIP
- @
- text
- @d20 4
- d37 1
- a37 1
- static char *rcsid="$Id: batch_smtp.c,v 1.4 1993/10/28 23:52:17 Aussem Exp Aussem $";
- d150 1
- a150 1
- int rc, retry;
- d242 5
- a247 2
- /* the conversion from \n to \r\n is only needed for SMTP on TCP/IP
- connections
- d254 18
- a271 1
- */
- @
-
-
- 1.4
- log
- @fmode() deleted. There are problems with the length of the mail
- @
- text
- @d20 3
- d33 1
- a33 1
- static char *rcsid="$Id: batch_smtp.c,v 1.2 1993/10/28 23:15:11 Aussem Exp $";
- d205 1
- a205 1
- fseek(out,-strlen("QUIT\r\n"),SEEK_END);
- d211 1
- a211 1
- fprintf(out,"HELO %s\r\n",myhost);
- d239 2
- d247 1
- @
-
-
- 1.3
- log
- @converting \n to \r\n by using fmode()
- @
- text
- @d30 1
- a30 1
- static char *rcsid="$Id: batch_smtp.c,v 1.2 1993/10/28 23:15:11 Aussem Exp Aussem $";
- d140 1
- a140 1
- char file[128],lockfile[128], puf[128],*host,*batch,*myhost;
- d232 1
- a232 1
- fmode(out,0);
- d234 10
- a243 1
- fputs(text, out);
- @
-
-
- 1.2
- log
- @\n is now converted to \r\n
- file reading now lines instead the full file
- @
- text
- @d20 4
- d30 1
- a30 1
- static char *rcsid="$Id: batch_smtp.c,v 1.1 1993/10/17 21:44:32 Aussem Exp Aussem $";
- d140 1
- a140 1
- char file[128],lockfile[128], puf[128],*host,*batch,*myhost,*p;
- d232 1
- a232 1
-
- d234 1
- a234 10
- {
- p=strrchr(text,'\n');
- if(p)
- {
- p[0]='\r';
- p[1]='\n';
- p[2]='\0';
- }
- (void) fputs(text, out);
- }
- @
-
-
- 1.1
- log
- @Initial revision
- @
- text
- @d19 3
- a21 1
- * $Log:$
- d23 1
- d26 1
- a26 1
- static char *rcsid="$Id$";
- a46 1
- static char *readfile (char *);
- a127 56
- /*
- * readfile():
- * read file <filename> and returns the buffer
- *
- */
- static
- char *
- readfile (char *filename)
- {
- char *puf;
- size_t len, read;
- FILE *fp;
-
- #ifdef DEBUG
- printf("Reading %s.\n",filename);
- #endif
-
- if (!filename)
- return (NULL);
-
- len = filelength (filename);
- if (len < 1)
- {
- printf ("\n%s: unable to open file %s\n", argv0, filename);
- return (NULL);
- }
-
- puf = malloc ((len + 1) * sizeof (char));
- if (!puf)
- {
- printf ("\n%s: unable to allocate %d bytes\n", argv0, (len + 1) * sizeof (char));
- free (puf);
- return (NULL);
- }
-
- fp = fopen (filename, "r");
- if (!fp)
- {
- printf ("\n%s: unable to open file %s\n", argv0, filename);
- free (puf);
- return (NULL);
- }
-
- read = fread (puf, len, 1, fp) * len;
- if (read != len)
- {
- printf ("\n%s: unable to read from %s %d (%d) bytes\n", argv0, filename, len, read);
- free (puf);
- fclose (fp);
- return (NULL);
- }
- puf[len]='\0';
- fclose (fp);
- return (puf);
- }
-
- d135 3
- a137 2
- FILE *fp;
- char file[128],lockfile[128], puf[128], *text,*host,*batch,*myhost;
- a191 9
- text = readfile (batch);
- if (!text)
- {
- /* error messages are generated by readfile() */
- remove (file);
- unlock_with_file(lockfile);
- exit (10);
- }
-
- d195 2
- a196 2
- fp = fopen (file, "r+");
- if (fp)
- d198 1
- a198 1
- fseek(fp,-strlen("QUIT\n"),SEEK_END);
- d201 2
- a202 2
- fp = fopen (file, "w");
- if (fp)
- d204 1
- a204 1
- fprintf(fp,"HELO %s\n",myhost);
- d212 1
- a212 1
- if (!fp)
- a215 2
- if (text)
- free (text);
- d220 2
- a221 29
- len = strlen (text);
-
- /*
- {
- register char *c,*last_c,*last_last_c;
-
- last_last_c=text;
- last_c=text+1;
- if(last_c!='\0' && last_last_c!='\0')
- {
- fputc(*last_last_c,fp);
- fputc(*last_c,fp);
- c=text+2;
- while(*c!='\0')
- {
- if(*last_last_c=='\n' &&
- *last_c=='.' &&
- *c=='\n')
- fputc('.',fp);
-
- fputc(*c,fp);
- last_last_c++;
- last_c++;
- c++;
- }
- }
- }
- */
- if ((fwrite (text, len, 1, fp) * len) != len)
- d223 1
- a223 4
- printf ("%s: Error appending mail to batch-folder %s\n", argv0, file);
- fclose (fp);
- if (text)
- free (text);
- d228 15
- a242 3
- fclose (fp);
- if (text)
- free (text);
- @
-