#import "MailMessage.h"
#import "EnhancedText.h"
#import "TokenString.h"
#import <stdio.h>

@implementation MailMessage

-initForMUA: (const char *)anApp  andMTA: (const char *)aMailer
{
   [super init];
   MUA = [[StringStorage alloc] init: anApp];
   MTA = [[StringStorage alloc] init: aMailer];
   to = [[StringStorage alloc] init];
   subject = [[StringStorage alloc] init];
   cc  = [[StringStorage alloc] init];
   text  = [[StringStorage alloc] init];
   mailFile  = [[StringStorage alloc] init];
   envelope  = [[StringStorage alloc] init];
   body = [[StringStorage alloc] init];
   commandLine = [[StringStorage alloc] init];

   [commandLine appendStringValue: [MTA stringValue]];
   [commandLine appendCharValue: ' '];
   fp = (FILE *)0;
   return self;
}

-free
{
   [MUA free];
   [MTA free];
   [to free];
   [subject free];
   [cc free];
   [text free];
   [mailFile free];
   [envelope free];
   [body free];
   [commandLine free];
   return [super free];
}

-commandArgs
{
   const char *str;
   id tokens = [[TokenString alloc] init: [to stringValue]];

   [tokens setSeparator: ','];
   
   while(str = [tokens popString])
   {
      [commandLine appendStringValue: str];
      [commandLine appendCharValue:' '];
   }
   
   str = [cc stringValue];
   if(str && *str)
   {
      [tokens setStringValue: str];
      while(str = [tokens popString])
      {
	 [commandLine appendStringValue: str];
	 [commandLine appendCharValue:' '];
      }
   }
   [tokens free];
   return self;
}

-buildEnvelope
{
   const char *str;
   [envelope appendStringValue: "Received: by "];
   [envelope appendStringValue: [MUA stringValue]];       
   [envelope appendCharValue: '\n'];
					     /* to: */
   [envelope appendStringValue: "To: "];
   [envelope appendStringValue: [to stringValue]];
   [envelope appendCharValue: '\n'];

					     /* subject: */
   [envelope appendStringValue: "Subject: "];
   [envelope appendStringValue: [subject stringValue]];
   [envelope appendCharValue: '\n'];

					     /* cc: */
   str = [cc stringValue];
   if(str && *str)
   {
      [envelope appendStringValue: "CC: "];
      [envelope appendStringValue: str];
      [envelope appendCharValue: '\n'];
   }

   if(*[mailFile stringValue] && fp)   /* generating a file - not memory image */
       fwrite([envelope stringValue], 1, [envelope strlen], fp);
   
   return self;
}

-buildBody
{
   [body setStringValue: "\n"];
   [body appendStringValue: [text stringValue]];
   return self;
}

-deliverTo: (const char *)receivers
   subject: (const char *)about
        cc: (const char *)alsoReceives 
      text: (const char *)str
{
   [to setStringValue: receivers];
   [subject setStringValue: about];
   [cc setStringValue: alsoReceives];
   [text setStringValue: str];
   [self commandArgs];
   [self buildEnvelope];
   [self buildBody];

   if(*[mailFile stringValue] && fp)   /* generated a file - not memory image */
   {
      fflush(fp);
      [commandLine appendStringValue: " < "];
      [commandLine appendStringValue: [mailFile stringValue]];
      system([commandLine stringValue]);
   }
   else
   {
      FILE *cmd;

      cmd = popen([commandLine stringValue], "w");
      if(!cmd)
      {
	 NXLogError("command failed: %s", [commandLine stringValue]);
	 return nil;
      }
   
      fputs([envelope stringValue], cmd);
      fputs([body stringValue], cmd);
      pclose(cmd);
   }
   
   return self;
}


-(BOOL)supportsAttachments
{
   return NO;
}

- setAttachments: fileList
{
   attachments = fileList;
   return self;
}


@end
