home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / MPack1_2_1.lha / MPack / src / amigapk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-31  |  9.3 KB  |  251 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. #include <libraries/netsupport.h>
  26.  
  27. #ifdef __SASC
  28. #include <proto/exec.h>
  29. #include <proto/dos.h>
  30. #include <proto/netsupport.h>
  31. #else
  32. #include <clib/exec_protos.h>
  33. #include <clib/dos_protos.h>
  34. #include <clib/netsupport_protos.h>
  35. #endif
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <ctype.h>
  41. #include <errno.h>
  42.  
  43. #include "version.h"
  44. #include "xmalloc.h"
  45.  
  46. #ifdef __SASC_60
  47. #if (__REVISION__ - 50)
  48. static const char DOSId[] = "\0$VER: MPack " MPACK_VERSION " " __AMIGADATE__ ;
  49. #else
  50. static const char DOSId[] = "\0$VER: MPack " MPACK_VERSION " (" __DATE__ ")" ;
  51. #endif
  52. #endif
  53.  
  54. #define MAXADDRESS 100
  55.  
  56. extern char *myGetConfig(char *, char *);
  57. extern int errno;
  58. extern int optind;
  59. extern char *optarg;
  60. struct NetSupportLibrary *NetSupportBase;
  61.  
  62. #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"
  63. enum {
  64.         FROM ,
  65.         DESTINATION ,
  66.         TO ,
  67.         SUBJECT ,
  68.         DESCRIPTION ,
  69.         CONTENTS ,
  70.         SPLITSIZE ,
  71.         NEWS ,
  72.         OPT_COUNT
  73.         } ;
  74.  
  75. #define HELPSTRING "mpack version " MPACK_VERSION "\n\
  76. Pack the given file into a MIME message for sending to a remote machine\n\
  77. by either mail or news. Automatically splits the file into a multi-part\n\
  78. message if it is larger than splitsize. The options are:\n\
  79. From/A                  The file you are going to send.\n\
  80. Dest=Destination/M      One or more electronic mail addresses or newsgroups.\n\
  81.                         May not be used with the To option.\n\
  82. -o=To/K                 A file  to output to. The message will be written to\n\
  83.                         the given file name. If more than one message is\n\
  84.                         needed, this is the base name, and a sequence number\n\
  85.                         is provided as a suffix.\n\
  86. -s=Subject/K            Subject of the mail message or article. Will be put\n\
  87.                         in each message sent, with sequence numbers appended\n\
  88.                         for more than one messages.\n\
  89. -d=Description/K        The name of a file that describes the file being\n\
  90.                         sent. Add more here later, Mike.\n\
  91. -c=Contents/K           Mime content-type field for the file being sent. The\n\
  92.                         default is application/octet-stream (binary data)\n\
  93.                         but some other MIME types will be recognized.\n\
  94. -m=SplitSize/K/N        Maximum size of a single message in bytes. The\n\
  95.                         default is taken from the environment variable\n\
  96.                         SPLITSIZE. If that is not defined, then there is no\n\
  97.                         limit.\n\
  98. -n=News/S               Causes the destinations to be interpreted as\n\
  99.                         newsgroups to be posted to instead of electronic mail\n\
  100.                         addresses. Has no effect if there are no destinations\n\
  101.                         (i.e. - To is used to write to a file).\n"
  102.  
  103. /* The one thing we have to fre by hand */
  104. static struct RDArgs *my_args = NULL, *args = NULL ;
  105.  
  106. /* A simple utilities */
  107. void post(char *, char *) ;
  108. void warn(char *) ;
  109.  
  110. void
  111. FreeSystem(void) {
  112.  
  113.         if (NetSupportBase) {
  114.                 UnLockFiles() ;
  115.                 CloseLibrary((struct Library *) NetSupportBase) ;
  116.                 }
  117.         if (args) FreeArgs(args) ;
  118.         if (my_args) FreeDosObject(DOS_RDARGS, my_args) ;
  119.         }
  120.  
  121. int
  122. main(int argc, char **argv) {
  123.         char *p, **pp, *from, *to, *subject, *description, *contents ;
  124.         char *header = NULL ;
  125.         char **destination ;
  126.         long news, count, splitsize = 0 ;
  127.         char buffer[512] ;
  128.         long part, opts[OPT_COUNT] ;
  129.  
  130.         if (!(NetSupportBase = (struct NetSupportLibrary *) OldOpenLibrary(NETSUPPORTNAME)))
  131.                 warn("No NetSupport.Library: Can't parse configfiles.");
  132.  
  133.         onexit(FreeSystem) ;
  134.         memset((char *) opts, 0, sizeof(opts)) ;
  135.         if ((p = myGetConfig("SPLITSIZE", NULL)) && *p >= '0' && *p <= '9')
  136.                 splitsize = atoi(p) ;
  137.  
  138.         opts[SPLITSIZE] = (long) &splitsize ;
  139.  
  140.         if (!(my_args = AllocDosObject(DOS_RDARGS, NULL))) {
  141.                 PrintFault(IoErr(), *argv) ;
  142.                 exit(RETURN_FAIL) ;
  143.                 }
  144.         my_args->RDA_ExtHelp = HELPSTRING ;
  145.         if (!(args = ReadArgs(TEMPLATE, opts, my_args))) {
  146.                 PrintFault(IoErr(), *argv) ;
  147.                 exit(RETURN_FAIL) ;
  148.                 }
  149.  
  150.         from = (char *) opts[FROM] ;
  151.         to = (char *) opts[TO] ;
  152.         subject = (char *) opts[SUBJECT] ;
  153.         description = (char *) opts[DESCRIPTION] ;
  154.         contents = (char *) opts[CONTENTS] ;
  155.         news = opts[NEWS] ;
  156.         splitsize = *((long *) opts[SPLITSIZE]) ;
  157.         destination = (char **) opts[DESTINATION] ;
  158.  
  159.         /* Make sure we're sending something reasonable. */
  160.         if (contents) {
  161.                 if (!cistrncmp(contents, "text/", 5)) {
  162.                         fprintf(stderr, "This program is not appropriate for encoding textual data\n") ;
  163.                         exit(RETURN_ERROR) ;
  164.                         }
  165.                 if (cistrncmp(contents, "application/", 12)
  166.                 && cistrncmp(contents, "audio/", 6)
  167.                 && cistrncmp(contents, "image/", 6)
  168.                 && cistrncmp(contents, "video/", 6)) {
  169.                         fprintf(stderr, "Content type must be subtype of application, audio, image, or video\n") ;
  170.                         exit(RETURN_ERROR) ;
  171.                         }
  172.                 }
  173.  
  174.         /* Gotta have something to send! */
  175.         if (!from) {
  176.                 fprintf(stderr, "The From argument is required\n") ;
  177.                 exit(RETURN_ERROR) ;
  178.                 }
  179.  
  180.         /* We must have either To or Destinations, but not both! */
  181.         if (to && destination) {
  182.                 fprintf(stderr, "The To keyword and Destination are mutually exclusive.\n") ;
  183.                 exit(RETURN_ERROR) ;
  184.                 }
  185.         else if (!to && !destination) {
  186.                 fprintf(stderr, "Either a destination or the To keyword is required\n");
  187.                 exit(RETURN_ERROR) ;
  188.                 }
  189.  
  190.         /* And we gotta have a subject! */
  191.         if (!subject) {
  192.                 fputs("Subject: ", stdout) ;
  193.                 fflush(stdout) ;
  194.                 if (!fgets(buffer, sizeof(buffer), stdin)) {
  195.                         fprintf(stderr, "A subject is required\n") ;
  196.                         exit(RETURN_ERROR) ;
  197.                         }
  198.                 if (p = strchr(buffer, '\n')) *p = '\0' ;
  199.                 subject = buffer ;
  200.                 }
  201.  
  202.         /* Build the To: or Newsgroups: line */
  203.         if (destination) {
  204.                 for (count = 25, pp = destination; *pp; pp += 1)
  205.                         count += strlen(*pp) + 3 ;
  206.                 header = xmalloc(count) ;
  207.                 p = stpcpy(header, news ? "Newsgroups: " : "To: ") ;
  208.                 p = stpcpy(p, *destination) ;
  209.                 for (pp = destination + 1; *pp; pp += 1) {
  210.                         p = stpcpy(p, news ? "," : ",\n\t") ;
  211.                         p = stpcpy(p, *pp) ;
  212.                         }
  213.                 stpcpy(p, "\n") ;
  214.                 }
  215.  
  216.         /* Get a name to put the output into */
  217.         if (!to) to = tmpnam(NULL) ;
  218.  
  219.         if (encode(from, description, subject, header, splitsize, contents, to))
  220.                 exit(RETURN_FAIL) ;
  221.  
  222.         /* Hey, we did it. Now send it if we need to */
  223.         if (destination)
  224.                 if (!access(to, R_OK)) {
  225.                         post(to, news ? POSTNEWS : SENDMAIL) ;
  226.                         remove(to) ;
  227.                         }
  228.                 else
  229.                         for (part = 1;; part += 1) {
  230.                                 sprintf(buffer, "%s.%02d", to, part) ;
  231.                                 if (access(buffer, R_OK)) break ;
  232.                                 post(buffer, news ? POSTNEWS : SENDMAIL) ;
  233.                                 remove(buffer) ;
  234.                                 }
  235.  
  236.         exit(RETURN_OK) ;
  237.         }
  238.  
  239. void
  240. post(char *name, char *command) {
  241.         char *p, buffer[512] ;
  242.  
  243.         sprintf(buffer, "%s < %s", myGetConfig(command, command), name);
  244.         system(buffer) ;
  245.         }
  246.  
  247. void
  248. warn(char *s) {
  249.         fprintf(stderr, "mpack: warning: %s\n", s);
  250.         }
  251.