
#import <appkit/appkit.h>
#import <foundation/foundation.h>
#import <pwd.h>
#import "../Alexandra.h"
#import "MailAddress.h"
#import "FromHeaderController.h"
#import "FromHeaderPanel.h"
#import "askresolverfordomain.h" 

#define NO_ADDRESS_SET "You have not specified a `From' header in the preferences. Please check the default and correct it if necessary."
#define MALFORMED_ADDRESS "The address you specified for the `From' header does not conform to Usenet standards. One of the allowed formats is: `Alexandra Info <alexandra-info@object-factory.com>'"

@implementation FromHeaderController

+ new
	{
	static FromHeaderController *me=nil;
	
	if(!me)
		me=[[FromHeaderController alloc] init];
	return me;
	}


- (char *)computeAddress;
	{
	MailAddress 	*mailAddress;
	NSMutableString	*realName;
    char 			*domain_name;
    struct passwd 	*pwent;
	const char 		*user_name;
	NSRange			ampRange;
	 
	user_name = NXUserName();
    pwent = getpwnam(user_name);
//	These days it is more likely that the machine name is not part of the
//	mail address. Hence, we ignore it.
    if((domain_name = askresolverfordomain()) == NULL)
		domain_name = "";

	mailAddress = [MailAddress mailAddressWithLocalPart:[NSString stringWithCString:user_name] domain:[NSString stringWithCString:domain_name]];
	if(pwent != NULL)
		{
		realName = [NSMutableString stringWithCString:pwent->pw_gecos];
		if((ampRange = [realName rangeOfString:@"&"]).length != 0)
			[realName replaceCharactersInRange:ampRange withString:[NSString stringWithCString:pwent->pw_name]];
		[mailAddress setRealName:realName];
		}
    return NXCopyStringBufferFromZone([[mailAddress stringRepresentation] cString],[self zone]);
	}


- (char *)validFromAddress;
	{
	KVPair		*pair;
	const char	*d;
	char		defname[200], defvalue[200],*fromAddress=NULL;
	int			i,n=[NXApp defaultIntValue:DEFAULT_XHEADER_COUNT],ret;
	BOOL		stopSearching=NO;

	for(i=0;i<n && !stopSearching;i++)
		{
		sprintf(defname,DEFAULT_XHEADERS,i+1);
		d=[NXApp defaultValue:defname];
		if(d)
			{
			pair=[[KVPair alloc] init:d delimiter:':'];
			if([pair isNull])
				stopSearching=YES;
			else if(!strcmp([pair key],"From"))
				{
				fromAddress=NXCopyStringBufferFromZone([pair value],[self zone]);
				stopSearching=YES;
				}				
			pair=[pair free];
			}
		}
	
	while((fromAddress == NULL) /*|| ([MailAddress isValidMailAddress:
		[NSString stringWithCString:fromAddress]] == NO)*/ )
		{
		if(!thePanel)
			[NXApp loadNibSection:"FromHeader.nib" owner:self];
		[thePanel makeKeyAndOrderFront:self];
		if(fromAddress == NULL)
			{
			fromAddress=[self computeAddress];	
		 	ret=[thePanel runModalForAddress:fromAddress withExplanation:NO_ADDRESS_SET];
			NXZoneFree([self zone],fromAddress);
			}
		else
			{
		 	ret=[thePanel runModalForAddress:fromAddress
		      withExplanation:MALFORMED_ADDRESS];
			}
		[thePanel orderOut:self];
		if(ret == 0)
			return NULL;
		fromAddress=(char *)[thePanel fromAddress];	
		}
		
	if(i==n)
		[NXApp setDefault:DEFAULT_XHEADER_COUNT toInt:++i];
	sprintf(defvalue, "From: %s",fromAddress);
	sprintf(defname,DEFAULT_XHEADERS,i);
	[NXApp setDefault:defname to:defvalue];
	EM_ERROR(ENOTEPrefsChanged,self,NULL);

	return NXCopyStringBufferFromZone(fromAddress,[self zone]);
	}		
	

@end
