home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Amiga / Workbench / Archivers / mpackPPC.lha / mpackPPC / src / amigapk.c < prev    next >
C/C++ Source or Header  |  1998-04-08  |  10KB  |  282 lines

  1. /* (C) Copyright 1993 by Mike W. Meyer
  2.  *
  3.  * Permission to use, copy, modify, distribute, and sell this software
  4.  * and its documentation for any purpose is hereby granted without
  5.  * fee, provided that the above copyright notice appear in all copies
  6.  * and that both that copyright notice and this permission notice
  7.  * appear in supporting documentation, and that the name of Mike W.
  8.  * Meyer not be used in advertising or publicity pertaining to
  9.  * distribution of the software without specific, written prior
  10.  * permission.  Mike W. Meyer makes no representations about the
  11.  * suitability of this software for any purpose.  It is provided "as
  12.  * is" without express or implied warranty.
  13.  *
  14.  * MIKE W. MEYER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  15.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16.  * FITNESS, IN NO EVENT SHALL MIKE W. MEYER BE LIABLE FOR ANY SPECIAL,
  17.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  18.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  19.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  20.  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22. #include <exec/types.h>
  23. #include <exec/execbase.h>
  24. #include <dos/dos.h>
  25. #ifndef __PPC__
  26. #include <libraries/netsupport.h>
  27. #else
  28. #define POSTNEWS ""
  29. #define SENDMAIL ""
  30. #endif
  31.  
  32. #ifdef __SASC
  33. #include <proto/exec.h>
  34. #include <proto/dos.h>
  35. #ifndef __PPC__
  36. #include <proto/netsupport.h>
  37. #endif
  38. #else
  39. #include <clib/exec_protos.h>
  40. #include <clib/dos_protos.h>
  41. #include <clib/netsupport_protos.h>
  42. #endif
  43.  
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <ctype.h>
  48. #include <errno.h>
  49.  
  50. #include "xmalloc.h"
  51. #include "version.h"
  52.  
  53. #if defined(__SASC) && (__VERSION__ > 5) && (__REVISION__ > 50)
  54. static const char DOSId[] = "\0$VER: MPack " MPACK_VERSION " " __AMIGADATE__ ;
  55. #else
  56. static const char DOSId[] = "\0$VER: MPack " MPACK_VERSION " (" __DATE__ ")" ;
  57. #endif
  58.  
  59. #define MAXADDRESS 100
  60.  
  61. extern char *myGetConfig(char *, char *);
  62. extern int errno;
  63. extern int optind;
  64. extern char *optarg;
  65. #ifndef __PPC__
  66. struct NetSupportLibrary *NetSupportBase;
  67. #endif
  68.  
  69. #define TEMPLATE "From/A,Dest=Destination/M,-o=To/K,-s=Subject/K,-d=Description/K,-c=Contents/K,-m=SplitSize/K/N,-n=News/S"
  70. enum {
  71.         FROM ,
  72.         DESTINATION ,
  73.         TO ,
  74.         SUBJECT ,
  75.         DESCRIPTION ,
  76.         CONTENTS ,
  77.         SPLITSIZE ,
  78.         NEWS ,
  79.         OPT_COUNT
  80.         } ;
  81.  
  82. #define HELPSTRING "mpack version " MPACK_VERSION "\n\
  83. Pack the given file into a MIME message for sending to a remote machine\n\
  84. by either mail or news. Automatically splits the file into a multi-part\n\
  85. message if it is larger than splitsize. The options are:\n\
  86. From/A                  The file you are going to send.\n\
  87. Dest=Destination/M      One or more electronic mail addresses or newsgroups.\n\
  88.                         May not be used with the To option.\n\
  89. -o=To/K                 A file  to output to. The message will be written to\n\
  90.                         the given file name. If more than one message is\n\
  91.                         needed, this is the base name, and a sequence number\n\
  92.                         is provided as a suffix.\n\
  93. -s=Subject/K            Subject of the mail message or article. Will be put\n\
  94.                         in each message sent, with sequence numbers appended\n\
  95.                         for more than one messages.\n\
  96. -d=Description/K        The name of a file that describes the file being\n\
  97.                         sent. Add more here later, Mike.\n\
  98. -c=Contents/K           Mime content-type field for the file being sent. The\n\
  99.                         default is application/octet-stream (binary data)\n\
  100.                         but some other MIME types will be recognized.\n\
  101. -m=SplitSize/K/N        Maximum size of a single message in bytes. The\n\
  102.                         default is taken from the environment variable\n\
  103.                         SPLITSIZE. If that is not defined, then there is no\n\
  104.                         limit.\n\
  105. -n=News/S               Causes the destinations to be interpreted as\n\
  106.                         newsgroups to be posted to instead of electronic mail\n\
  107.                         addresses. Has no effect if there are no destinations\n\
  108.                         (i.e. - To is used to write to a file).\n"
  109.  
  110. /* The one thing we have to fre by hand */
  111. static struct RDArgs *my_args = NULL, *args = NULL ;
  112.  
  113. /* A simple utilities */
  114. void post(char *, char *) ;
  115. void warn(char *) ;
  116.  
  117. void
  118. FreeSystem(void) {
  119.  
  120. #ifndef __PPC__
  121.         if (NetSupportBase) {
  122.                 UnLockFiles() ;
  123.                 CloseLibrary((struct Library *) NetSupportBase) ;
  124.                 }
  125. #endif
  126.  
  127.         if (args) FreeArgs(args) ;
  128.         if (my_args) FreeDosObject(DOS_RDARGS, my_args) ;
  129.         }
  130.  
  131. int
  132. main(int argc, char **argv) {
  133.         FILE *infile, *descfile;
  134.         char *p, **pp, *from, *to, *subject, *description, *contents ;
  135.         char *header = NULL ;
  136.         char **destination ;
  137.         long news, count, splitsize = 0 ;
  138.         char buffer[512] ;
  139.         long part, opts[OPT_COUNT] ;
  140.  
  141. #ifndef __PPC__
  142.         if (!(NetSupportBase = (struct NetSupportLibrary *) OldOpenLibrary(NETSUPPORTNAME)))
  143.                 fprintf(stderr,
  144.                         "No NetSupport.Library: Can't parse configfiles.\n");
  145. #endif
  146.  
  147.         onexit(FreeSystem) ;
  148.         memset((char *) opts, 0, sizeof(opts)) ;
  149.         if ((p = myGetConfig("SPLITSIZE", NULL)) && *p >= '0' && *p <= '9')
  150.                 splitsize = atoi(p) ;
  151.  
  152.         opts[SPLITSIZE] = (long) &splitsize ;
  153.  
  154.         if (!(my_args = AllocDosObject(DOS_RDARGS, NULL))) {
  155.                 PrintFault(IoErr(), *argv) ;
  156.                 exit(RETURN_FAIL) ;
  157.                 }
  158.         my_args->RDA_ExtHelp = HELPSTRING ;
  159.         if (!(args = ReadArgs(TEMPLATE, opts, my_args))) {
  160.                 PrintFault(IoErr(), *argv) ;
  161.                 exit(RETURN_FAIL) ;
  162.                 }
  163.  
  164.         from = (char *) opts[FROM] ;
  165.         to = (char *) opts[TO] ;
  166.         subject = (char *) opts[SUBJECT] ;
  167.         description = (char *) opts[DESCRIPTION] ;
  168.         contents = (char *) opts[CONTENTS] ;
  169.         news = opts[NEWS] ;
  170.         splitsize = *((long *) opts[SPLITSIZE]) ;
  171.         destination = (char **) opts[DESTINATION] ;
  172.  
  173.         /* Make sure we can open the description file. */
  174.         if (description) {
  175.                 if (!(descfile = fopen(description, "r"))) {
  176.                         fprintf(stderr, "Can't open the description file \"%s\"!\n", description);
  177.                         exit(RETURN_ERROR) ;
  178.                 }
  179.         }
  180.         else
  181.                 descfile = NULL;
  182.  
  183.         /* Make sure we're sending something reasonable. */
  184.         if (contents) {
  185.                 if (!cistrncmp(contents, "text/", 5)) {
  186.                         fprintf(stderr, "This program is not appropriate for encoding textual data\n") ;
  187.                         exit(RETURN_ERROR) ;
  188.                         }
  189.                 if (cistrncmp(contents, "application/", 12)
  190.                 && cistrncmp(contents, "audio/", 6)
  191.                 && cistrncmp(contents, "image/", 6)
  192.                 && cistrncmp(contents, "video/", 6)) {
  193.                         fprintf(stderr, "Content type must be subtype of application, audio, image, or video\n") ;
  194.                         exit(RETURN_ERROR) ;
  195.                         }
  196.                 }
  197.  
  198.         /* Gotta have something to send! */
  199.         if (!from) {
  200.                 fprintf(stderr, "The From argument is required\n") ;
  201.                 exit(RETURN_ERROR) ;
  202.                 }
  203.  
  204.         /* We must have either To or Destinations, but not both! */
  205.         if (to && destination) {
  206.                 fprintf(stderr, "The To keyword and Destination are mutually exclusive.\n") ;
  207.                 exit(RETURN_ERROR) ;
  208.                 }
  209.         else if (!to && !destination) {
  210.                 fprintf(stderr, "Either a destination or the To keyword is required\n");
  211.                 exit(RETURN_ERROR) ;
  212.                 }
  213.  
  214.         /* And we gotta have a subject! */
  215.         if (!subject) {
  216.                 fputs("Subject: ", stdout) ;
  217.                 fflush(stdout) ;
  218.                 if (!fgets(buffer, sizeof(buffer), stdin)) {
  219.                         fprintf(stderr, "A subject is required\n") ;
  220.                         exit(RETURN_ERROR) ;
  221.                         }
  222.                 if (p = strchr(buffer, '\n')) *p = '\0' ;
  223.                 subject = buffer ;
  224.                 }
  225.  
  226.         /* Build the To: or Newsgroups: line */
  227.         if (destination) {
  228.                 for (count = 25, pp = destination; *pp; pp += 1)
  229.                         count += strlen(*pp) + 3 ;
  230.                 header = xmalloc(count) ;
  231.                 p = stpcpy(header, news ? "Newsgroups: " : "To: ") ;
  232.                 p = stpcpy(p, *destination) ;
  233.                 for (pp = destination + 1; *pp; pp += 1) {
  234.                         p = stpcpy(p, news ? "," : ",\n\t") ;
  235.                         p = stpcpy(p, *pp) ;
  236.                         }
  237.                 stpcpy(p, "\n") ;
  238.                 }
  239.  
  240.         /* Get a name to put the output into */
  241.         if (!to) to = tmpnam(NULL) ;
  242.  
  243.         /* Sigh */
  244.         infile = fopen(from, "r");
  245.         if (!infile) {
  246.                 os_perror(from);
  247.                 exit(1);
  248.                 }
  249.  
  250.         if (encode(infile, (FILE *) 0, from, descfile, subject, header, splitsize, contents, to))
  251.                 exit(RETURN_FAIL) ;
  252.  
  253.         /* Hey, we did it. Now send it if we need to */
  254.         if (destination)
  255.                 if (!access(to, R_OK)) {
  256.                         post(to, news ? POSTNEWS : SENDMAIL) ;
  257.                         remove(to) ;
  258.                         }
  259.                 else
  260.                         for (part = 1;; part += 1) {
  261.                                 sprintf(buffer, "%s.%02d", to, part) ;
  262.                                 if (access(buffer, R_OK)) break ;
  263.                                 post(buffer, news ? POSTNEWS : SENDMAIL) ;
  264.                                 remove(buffer) ;
  265.                                 }
  266.  
  267.         exit(RETURN_OK) ;
  268.         }
  269.  
  270. void
  271. post(char *name, char *command) {
  272.         char buffer[512] ;
  273.  
  274.         sprintf(buffer, "%s < %s", myGetConfig(command, command), name);
  275.         system(buffer) ;
  276.         }
  277.  
  278. void
  279. warn(char *s) {
  280.         abort() ;
  281.         }
  282.