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

  1. /*+++
  2.  *  title:      MailProxy.m
  3.  *  abstract:   Implementation of MailProxy class for mailapp-utilities.
  4.  *  author:     Tom Hageman <tom@basil.icce.rug.nl>
  5.  *  created:    February 1998
  6.  *  modified:   (see RCS Log at end)
  7.  *  copyleft:
  8.  *
  9.  *    Copyright (C) 1998  Tom R. Hageman, but otherwise perfect freeware.
  10.  *
  11.  *  description:
  12.  *      (see corresponding *.h file)
  13.  *---*/
  14.  
  15. static const char * const RCSid = ((void)&RCSid,
  16.     "@(#)MailProxy.m,v 1.2 1998/02/15 17:03:17");
  17. #define RCS_MailProxy_ID
  18.  
  19. #import "compat.h"
  20. #import "mailutil.h"
  21. #import <stdlib.h>
  22. #import <mach/mach.h>
  23.  
  24. #define _MailProxy_common_ivars \
  25.    char *hostname; \
  26.    char *mailer; \
  27.    BOOL isConnected;
  28.  
  29. #define _RELEASE_COMMON_IVARS \
  30.    free(hostname); \
  31.    free(mailer);
  32.  
  33. #if RHAPSODY
  34.  
  35. #  import <Message/NSMailDelivery.h>
  36.  
  37. #  define _MailProxy_ivars \
  38.    _MailProxy_common_ivars
  39.  
  40. #  define _RELEASE_IVARS \
  41.    _RELEASE_COMMON_IVARS
  42.  
  43. #else // NEXTSTEP || OPENSTEP
  44.  
  45. #  import "MailSpeaker.h"
  46.  
  47. #  define _MailProxy_ivars \
  48.    _MailProxy_common_ivars \
  49.    MailSpeaker *mailSpeaker;
  50.  
  51. #  if NEXTSTEP
  52. #     define _RELEASE_IVARS \
  53.    _RELEASE_COMMON_IVARS \
  54.    [mailSpeaker free];
  55. #  else // OPENSTEP
  56. #     define _RELEASE_IVARS \
  57.    _RELEASE_COMMON_IVARS \
  58.    [mailSpeaker release];
  59. #  endif
  60.  
  61. #endif
  62.  
  63. #import "MailProxy.h"
  64.  
  65.  
  66. @implementation MailProxy
  67.  
  68. // Implementation feature tests.
  69. + (BOOL)canEditMessage
  70. {
  71. #if RHAPSODY
  72.    return NO;
  73. #else
  74.    return YES;
  75. #endif
  76. }
  77.  
  78. + (BOOL)canIncorporateNewMail
  79. {
  80. #if RHAPSODY
  81.    return NO;
  82. #else
  83.    return YES;
  84. #endif
  85. }
  86.  
  87.  
  88. - initWithMailer:(const char *)aMailer
  89.    host:(const char *)aHostName
  90.    forceLaunch:(BOOL)forceLaunch
  91. {
  92.    if ((self = [super init]))
  93.    {
  94.       if (aHostName == NULL) aHostName = "";
  95.       hostname = strcpy(malloc(strlen(aHostName)+1), aHostName);
  96.       // XXX find name of local host?
  97.  
  98.       if (!aMailer) aMailer = "";
  99.  
  100. #if RHAPSODY
  101.       /* Message framework currently (DR1) only seems to support connection
  102.          to MAIL_APP, on local host. */
  103.       if (strcmp(hostname, "") != 0)
  104.       {
  105.      [self release];
  106.      return nil;
  107.       }
  108.       aMailer = MAIL_APP;
  109.       // Check if mailer is running.
  110.       isConnected = ([[NSPortNameServer defaultPortNameServer]
  111.               portForName:[NSString stringWithCString:aMailer]] != nil);
  112. #else
  113.       mailSpeaker = [[MailSpeaker alloc] init];
  114.       {
  115.      port_t mailPort = NXPortNameLookup(MAILSEND_PORTNAME, hostname);
  116.  
  117.      if (mailPort == PORT_NULL && forceLaunch)
  118.      {
  119.         if ((mailPort = NXPortFromName(aMailer, hostname)) == PORT_NULL)
  120.         {
  121.            if (!*aMailer) aMailer = NXGetDefaultValue("GLOBAL", "Mailer");
  122.            if (!aMailer || !*aMailer) aMailer = MAIL_APP;
  123.            mailPort = NXPortFromName(aMailer, hostname);
  124.            if (mailPort == PORT_NULL)
  125.            {
  126.           aMailer = MAIL_APP;
  127.           mailPort = NXPortFromName(aMailer, hostname);
  128.            }
  129.            if (mailPort != PORT_NULL) port_deallocate(task_self(), mailPort);
  130.         }
  131.         mailPort = NXPortNameLookup(MAILSEND_PORTNAME, hostname);
  132.      }
  133.      if (mailPort != PORT_NULL)
  134.      {
  135.         [mailSpeaker setSendPort:mailPort];
  136.         isConnected = YES;
  137.      }
  138.       }
  139. #endif
  140.       mailer = strcpy(malloc(strlen(aMailer)+1), aMailer);
  141.    }
  142.    return self;
  143. }
  144.  
  145. #if NEXTSTEP
  146. - free
  147. {
  148.    _RELEASE_IVARS
  149.    return [super free];
  150. }
  151.  
  152. - (void)release { [self free]; }
  153. #else
  154. - (void)dealloc
  155. {
  156.     _RELEASE_IVARS
  157.     [super dealloc];
  158. }
  159. #endif
  160.  
  161.  
  162. // Access methods.
  163.  
  164. - (const char *)hostname   { return hostname; }
  165. - (const char *)mailer       { return mailer; }
  166. - (BOOL)isConnected       { return isConnected; }
  167.  
  168. // Other methods.
  169.  
  170. - (BOOL)sendMailTo:(const char *)to
  171.    subject:(const char *)subject
  172.    body:(const char *)body
  173.    cc:(const char *)cc 
  174.    deliver:(BOOL)deliver
  175. {
  176. #if RHAPSODY
  177.    NSMutableDictionary *headers = [NSMutableDictionary dictionary];
  178.    NSAttributedString *messageBody = nil;
  179.  
  180.    if (!deliver) return NO;
  181.  
  182.    if (to)      [headers setObject:[NSString stringWithCString:to]      forKey:@"To"];
  183.    if (subject)    [headers setObject:[NSString stringWithCString:subject] forKey:@"Subject"];
  184.    if (cc)    [headers setObject:[NSString stringWithCString:cc]      forKey:@"Cc"];
  185.    if (body)
  186.    {
  187.       messageBody = [[[NSAttributedString alloc]
  188.               initWithString:[NSString stringWithCString:body]] autorelease];
  189.    }
  190.    return [NSMailDelivery deliverMessage:messageBody headers:headers
  191.        format:NSASCIIMailFormat protocol:nil];
  192. #else
  193.    int status = 0;
  194.    int window;
  195.  
  196.    if ([mailSpeaker openSend:&window] != 0) return NO;
  197.  
  198.    if (to)    status |= [mailSpeaker setTo:(char *)to          inWindow:window];
  199.    if (subject)    status |= [mailSpeaker setSubject:(char *)subject inWindow:window];
  200.    if (cc)    status |= [mailSpeaker setCc:(char *)cc          inWindow:window];
  201.    if (body)    status |= [mailSpeaker setBody:(char *)body      inWindow:window];
  202.  
  203.    if (deliver)
  204.    {
  205.       if (to && (subject || body)) status |= [mailSpeaker deliver:window];
  206.    }
  207.    return (status == 0);
  208. #endif
  209. }
  210.  
  211. - (BOOL)incorporateNewMail
  212. {
  213. #if RHAPSODY
  214.    return NO;
  215. #else
  216.    return [mailSpeaker incorporateNewMail] == 0;
  217. #endif
  218. }
  219.  
  220. @end // MailProxy
  221.