home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Mail / mailapp-utilities-2.1-MIHS / Source / nextmail.m < prev    next >
Encoding:
Text File  |  1998-02-14  |  4.1 KB  |  147 lines

  1. /* -*-ObjC-*-
  2. *******************************************************************************
  3. *
  4. * File:         nextmail.m
  5. * RCS:          /usr/local/sources/CVS/mailapp-utilities/nextmail.m,v 1.7 1998/02/14 17:34:47 tom Exp
  6. * Description:  A 'mail' compatible program to open a Mail.app Compose window
  7. * Author:       Carl Edman
  8. * Created:      Thu Mar  4 13:42:52 1993
  9. * Modified:     Fri Feb 13 20:05:47 1998 Tom Hageman <tom@basil.icce.rug.nl>
  10. * Language:     Objective C
  11. * Package:      mailapp-utilities
  12. * Status:       Experimental
  13. *
  14. * (C) Copyright 1993-1998, but otherwise this file is perfect freeware.
  15. *
  16. *******************************************************************************
  17. */
  18.  
  19. #import <libc.h>
  20. //#import <stdlib.h>
  21. //#import <stdio.h>
  22. #import "compat.h"
  23. #import "MailProxy.h"
  24. #import "mailutil.h"
  25. #import "mailtoc.h"
  26. #import "optutil.h"
  27.  
  28. #if NEXTSTEP
  29. #  define NXHOST  "NXHost"
  30. #else
  31. #  define NXHOST  "NSHost"
  32. #endif
  33.  
  34. #define USAGE "\
  35. Usage: %s [-e] [-s subject] [-c user]... [-"NXHOST" host] [-V|-H] [user...]\n"
  36.  
  37. #define HELP "\
  38.  -e           edit message in Compose window (default is to deliver immediately)\n\
  39.  -s subject   set Subject: to `subject'\n\
  40.  -c user      cc: to `user' (allowed multiple times)\n\
  41.  -m mailer    use `mailer' instead of default (\"dread -g Mailer\", or Mail.app)\n\
  42.  -NXHost host connect to Mailer on given host\n\
  43.  -V           show version\n\
  44.  -H           this help\n\
  45. "
  46.  
  47. #define EXIT(status) \
  48.    POOL_RELEASE \
  49.    return(status)
  50.  
  51. int main(int ac,char *av[])
  52. {
  53.    id         mailProxy;
  54.    char *to=0,*subject=0,*cc=0,*content=0;
  55.    const char *mailer=0;
  56.    int tolen=0,subjectlen=0,cclen=0,contentlen=0;
  57.    int tomaxlen=0,subjectmaxlen=0,ccmaxlen=0,contentmaxlen=0;
  58.    int editflg=0,c,i;
  59.    char *host="";
  60.    int status=EXIT_SUCCESS;
  61.    POOL_INIT
  62.  
  63.    set_progname(av[0]);
  64.  
  65.    while((c=getopt(ac,av,"s:c:m:eN:VH"))!=EOF) switch(c)
  66.    {
  67.     case 's':
  68.       if (subject) appstring(&subject,&subjectlen,&subjectmaxlen," ",1);
  69.       appstring(&subject,&subjectlen,&subjectmaxlen,optarg,strlen(optarg));
  70.       break;
  71.     case 'c':
  72.       if (cc) appstring(&cc,&cclen,&ccmaxlen,",",1);
  73.       appstring(&cc,&cclen,&ccmaxlen,optarg,strlen(optarg));
  74.     case 'm':
  75.       mailer=optarg;
  76.       break;
  77.     case 'e':
  78.       editflg++;
  79.       break;
  80.     case 'N':
  81.       if ((strcmp(optarg,"XHost")==0||strcmp(optarg,"SHost")==0)&&(optind<ac))
  82.       {
  83.      host=av[optind++];
  84.      break;
  85.       }
  86.       status=EXIT_USAGE;
  87.       break;
  88.     case 'V':
  89.       status=EXIT_VERSION;
  90.       break;
  91.     case 'H':
  92.       status=EXIT_HELP;
  93.       break;
  94.     case '?':
  95.     default:
  96.       status=EXIT_USAGE;
  97.       break;
  98.    }
  99.    handle_usage_help_version(status, USAGE, HELP);
  100.  
  101.    if (editflg && ![MailProxy canEditMessage])
  102.    {
  103.       fprintf(stderr,"%s: [-e] editing not supported on this platform\n", progname());
  104.       EXIT(EXIT_USAGE);
  105.    }
  106.  
  107.    for(;optind<ac;optind++)
  108.    {
  109.       if (to) appstring(&to,&tolen,&tomaxlen,",",1);
  110.       appstring(&to,&tolen,&tomaxlen,av[optind],strlen(av[optind]));
  111.    }
  112.  
  113.    mailProxy = [[MailProxy alloc] initWithMailer:mailer host:host forceLaunch:YES];
  114.    if (mailProxy == nil)
  115.    {
  116.       fprintf(stderr,"%s: unable to connect to %s.app on %s\n", progname(), mailer, host ? host : "localhost");
  117.       EXIT(EXIT_FAILURE);
  118.    }
  119.    if (![mailProxy isConnected])
  120.    {
  121.       fprintf(stderr,"%s: unable to connect to %s.app on %s\n", progname(), [mailProxy mailer], [mailProxy hostname]);
  122.       EXIT(EXIT_FAILURE);
  123.    }
  124.  
  125.    while((i=fread(growstring(&content,&contentlen,&contentmaxlen,LINELEN*16),
  126.                   1,LINELEN*16,stdin))>0)
  127.    {
  128.       contentlen+=i;
  129.    }
  130.  
  131.    // zero-terminate strings:
  132.    if (to)      appstring(&to,&tolen,&tomaxlen,"",1);
  133.    if (subject) appstring(&subject,&subjectlen,&subjectmaxlen,"",1);
  134.    if (cc)      appstring(&cc,&cclen,&ccmaxlen,"",1);
  135.    if (content) appstring(&content,&contentlen,&contentmaxlen,"",1);
  136.  
  137.    if (![mailProxy sendMailTo:to subject:subject body:content cc:cc deliver:!editflg])
  138.    {
  139.       fprintf(stderr, "%s: unable to send message to %s.app on %s\n", progname(), [mailProxy mailer], [mailProxy hostname]);
  140.       EXIT(EXIT_FAILURE);
  141.    }
  142.    
  143.    [mailProxy release];
  144.  
  145.    EXIT(EXIT_SUCCESS);
  146. }
  147.