
/* Generated by Interface Builder */


#import "NXPGPDelegate.h"
#import "TextView.h"
#import "Crypter.h"
#import <appkit/publicWraps.h>
#import <appkit/Application.h>
#import <appkit/Listener.h>
#import <appkit/Window.h>
#import <appkit/Pasteboard.h>
#import <appkit/FormCell.h>
#import <appkit/TextField.h>
#import <appkit/Text.h>
#import <appkit/Form.h>
#import <appkit/Button.h>
#import <defaults/defaults.h>
#import <appkit/NXCType.h>
#import <streams/streams.h>
#define DECRYPT 0
#define SIGN 1
#define DECRYPTINWIN 2
#import <libc.h>
#import <stdio.h>
#import <strings.h>
#import <stdlib.h>
#import <ctype.h>

@implementation NXPGPDelegate

-appDidInit:sender
{
	id theListener = [NXApp appListener];

	[theListener setServicesDelegate: self];

	return self;
}

- appWillInit:sender
{
	char                name[64];
	char                temp[1024];
	int                 namelen = 64,c;
	const char         *secring;
	static NXDefaultsVector NXPGPDefaults =
	{
      {"PGPPATH", ""},
      {"secring", ""},
      {"userid", ""},
      {"signWhenEncrypt","1"},
      {NULL}
	};


    NXRegisterDefaults ("NXPGP", NXPGPDefaults);

    sprintf (userid, NXGetDefaultValue ("NXPGP", "userid"));
    if (!strcmp (userid, ""))
	{
		gethostname (name, namelen);
		sprintf (userid, "%s@%s", getenv ("USER"), name);
		NXSetDefault ("NXPGP", "userid", userid);
	}
    sprintf (temp, NXGetDefaultValue ("NXPGP", "PGPPATH"));
    if (!strcmp (temp, ""))
	{

		sprintf (temp, "%s/.pgp", getenv ("HOME"));
		NXSetDefault ("NXPGP", "PGPPATH", temp);
	}
    secring = NXGetDefaultValue ("NXPGP", "secring");

	[signForm setStringValue: userid at:1];
	[PGPPATHForm setStringValue: temp at:0];
	[signForm setStringValue: secring at:0];
    sprintf (temp, NXGetDefaultValue ("NXPGP", "signWhenEncrypt"));
	if(isdigit(temp[0]))
	{
		c=temp[0]-60;
		[signSwitch setIntValue: c];
	}
	else [signSwitch setIntValue:1];
	[decryptWin setBackgroundGray:NX_LTGRAY];
	decryptTextView =[[TextView alloc] setup:decryptWin];
	decryptText =[decryptTextView getText:self];
	[self clearText:decryptText];
	[decryptTextView display];

	[statusWin setBackgroundGray:NX_LTGRAY];
	statusTextView =[[TextView alloc] setup:statusWin];
    [statusTextView display];
	statusText =[statusTextView getText:self];
	[self clearText:statusText];
	[statusTextView display];
	[self setAll:self];
    return self;
}

-(BOOL)appAcceptsAnotherFile:sender
{
	return YES;
}

-(int)app:sender openFile:(const char *)name type:(const char *)type
{
    char              **outdata;
	int len=1024;
	char tt[strlen(name)+1];

	strcpy(tt, name);

    ppMode = DECRYPT;
	
	[[passPhrasePanel center] makeKeyAndOrderFront:self];
	[genericPassPhraseTextField selectText:self];
	[self clearText:statusText];
	previousApp =[NXApp activateSelf:YES];

	if( [NXApp runModalFor:passPhrasePanel]!=1)
	{
		char *aFile;

		[NXApp activate:previousApp];

		aFile=strtok(tt, "\t");

		while(aFile) {
			outdata =[myCrypter decodeFile: aFile length:&len];
			[statusText setText:outdata[1]];
			if(returnLength)
			{
				//[pboard declareTypes: types num: 1 owner:self];
				//[pboard writeType: NXAsciiPboardType data: outdata[0] length:returnLength];
			}
			else
			{
				[statusWin makeKeyAndOrderFront:self];
			}
			
			free (outdata[0]);
			free (outdata[1]);

			aFile=strtok(NULL, "\t");
		}
	}
	else
	{
		[NXApp activate:previousApp];
		[passPhrasePanel orderOut:self];
	}

	[self clearPassPhrase:genericPassPhraseTextField];
	return 1;
}

-settingsSet:(id) pboard
 userData:(const char *) userData
 error:(char **) msg
{
	[NXApp activateSelf:YES];
	[settingsWindow makeKeyAndOrderFront:self];

    return self;
}

-encryptPBoard:(id) pboard
 userData:(const char *) userData
 error:(char **) msg
{
	const char *types[1];
	char **outdata;

	types[0] = NXAsciiPboardType;

	if (!strcmp ([pboard findAvailableTypeFrom: types num:1], NXAsciiPboardType))
	{
		[pboard readType: NXAsciiPboardType data: &pbdata length:&datalen];
	}
	else
	  *msg = "No ASCII data.";

	[[encryptPanel center] makeKeyAndOrderFront:self];
	[encryptToForm selectTextAt:0];
	[self clearText:statusText];
	previousApp =[NXApp activateSelf:YES];

	if([NXApp runModalFor:encryptPanel]!=1)
	{
		[NXApp activate:previousApp];

		outdata =[myCrypter encodeText: pbdata length:datalen];
		[statusText setText:outdata[1]];
		if(returnLength)
		{
			[pboard declareTypes: types num: 1 owner:self];
			[pboard writeType: NXAsciiPboardType data: outdata[0] length:returnLength];
		}
		else
		{
			[statusWin makeKeyAndOrderFront:self];
		}

		free (outdata[0]);
		free (outdata[1]);
	}
	else
	{
		[NXApp activate:previousApp];
		[encryptPanel orderOut:self];
	}

	[self clearPassPhrase:passTextField];
	return self;
}

-encryptFilePBoard:(id) pboard
 userData:(const char *) userData
 error:(char **) msg
{
	const char *types[1];
	char **outdata;

	types[0] = NXFilenamePboardType;

	if (!strcmp ([pboard findAvailableTypeFrom: types num:1], NXFilenamePboardType))
	{
		[pboard readType: NXFilenamePboardType data: &pbdata length:&datalen];
	}
	else
	  *msg = "No Filename data.";

	[[encryptPanel center] makeKeyAndOrderFront:self];
	[encryptToForm selectTextAt:0];
	[self clearText:statusText];
	previousApp =[NXApp activateSelf:YES];

	if([NXApp runModalFor:encryptPanel]!=1)
	{	
		char *aFile;
		char tt[strlen(pbdata)+1];
		
		strcpy(tt, pbdata);
		
		[NXApp activate:previousApp];

		aFile=strtok(tt, "\t");

		while(aFile) {
			outdata =[myCrypter encodeFile: aFile length:datalen];
			[statusText setText:outdata[1]];
			if(returnLength)
			{
				//[pboard declareTypes: types num: 1 owner:self];
				//[pboard writeType: NXAsciiPboardType data: outdata[0] length:returnLength];
			}
			else
			{
				[statusWin makeKeyAndOrderFront:self];
			}
			
			free (outdata[0]);
			free (outdata[1]);

			aFile=strtok(NULL, "\t");
		}
	}
	else
	{
		[NXApp activate:previousApp];
		[encryptPanel orderOut:self];
	}

	[self clearPassPhrase:passTextField];
	return self;
}

-doEncrypt:sender
{
	int tempsign; 
	char asc[32];
	tempsign=[signSwitch intValue];
	[myCrypter setuserString: [encryptToForm stringValueAt:0]];
	if(tempsign)
	{
		[myCrypter setpassPhrase:[passTextField stringValue]];
	}
	[myCrypter setsign:tempsign];
	sprintf(asc,"%d",tempsign);
	NXSetDefault ("NXPGP", "signWhenEncrypt", asc);
	[encryptPanel orderOut:self];
    [NXApp stopModal];
    return self;

}

-decryptPBoard:(id) pboard
 userData:(const char *) userData
 error:(char **) msg
{
    const char         *types[1];
    char              **outdata;

    ppMode = DECRYPT;

    types[0] = NXAsciiPboardType;

	if (!strcmp ([pboard findAvailableTypeFrom: types num:1], NXAsciiPboardType))
	{
		[pboard readType: NXAsciiPboardType data: &pbdata length:&datalen];
	}
    else
	  *msg = "No ASCII data.";

	[[passPhrasePanel center] makeKeyAndOrderFront:self];
	[genericPassPhraseTextField selectText:self];
	[self clearText:statusText];
	previousApp =[NXApp activateSelf:YES];

	if( [NXApp runModalFor:passPhrasePanel]!=1)
	{

		[NXApp activate:previousApp];


		outdata =[myCrypter decodeText: pbdata length:&datalen];
		[statusText setText:outdata[1]];
		if(returnLength)
		{
			[pboard declareTypes: types num: 1 owner:self];
			[pboard writeType: NXAsciiPboardType data: outdata[0] length:returnLength];
		}
		else
		{
			[statusWin makeKeyAndOrderFront:self];
		}

		free (outdata[0]);
		free (outdata[1]);
	}
	else
	{
		[NXApp activate:previousApp];
		[passPhrasePanel orderOut:self];
	}

	[self clearPassPhrase:genericPassPhraseTextField];
	return self;
}

-decryptFilePBoard:(id) pboard
 userData:(const char *) userData
 error:(char **) msg
{
    const char         *types[1];
    char              **outdata;

    ppMode = DECRYPT;

    types[0] = NXFilenamePboardType;

	if (!strcmp ([pboard findAvailableTypeFrom: types num:1], NXFilenamePboardType))
	{
		[pboard readType: NXFilenamePboardType data: &pbdata length:&datalen];
	}
    else
	  *msg = "No Filename data.";

	[[passPhrasePanel center] makeKeyAndOrderFront:self];
	[genericPassPhraseTextField selectText:self];
	[self clearText:statusText];
	previousApp =[NXApp activateSelf:YES];

	if( [NXApp runModalFor:passPhrasePanel]!=1)
	{
		char *aFile;
		char tt[strlen(pbdata)+1];
		
		strcpy(tt, pbdata);
				
		[NXApp activate:previousApp];


		aFile=strtok(tt, "\t");

		while(aFile) {
			outdata =[myCrypter decodeFile: aFile length:&datalen];
			[statusText setText:outdata[1]];
			if(returnLength)
			{
				//[pboard declareTypes: types num: 1 owner:self];
				//[pboard writeType: NXAsciiPboardType data: outdata[0] length:returnLength];
			}
			else
			{
				[statusWin makeKeyAndOrderFront:self];
			}
			
			free (outdata[0]);
			free (outdata[1]);

			aFile=strtok(NULL, "\t");
		}
	}
	else
	{
		[NXApp activate:previousApp];
		[passPhrasePanel orderOut:self];
	}

	[self clearPassPhrase:genericPassPhraseTextField];
	return self;
}

-doDecrypt:sender
{

	[myCrypter setpassPhrase:[genericPassPhraseTextField stringValue]];
	[passPhrasePanel orderOut:self];

    [NXApp stopModal];
    return self;
}

-signPBoard:(id) pboard
 userData:(const char *) userData
 error:(char **) msg
{
    const char         *types[1];
    char              **outdata;

    ppMode = SIGN;

    types[0] = NXAsciiPboardType;

	if (!strcmp ([pboard findAvailableTypeFrom: types num:1], NXAsciiPboardType))
	{
		[pboard readType: NXAsciiPboardType data: &pbdata length:&datalen];
	}
    else
	  *msg = "No ASCII data.";

	[[passPhrasePanel center] makeKeyAndOrderFront:self];
	[genericPassPhraseTextField selectText:self];
	[self clearText:statusText];
	previousApp =[NXApp activateSelf:YES];

	if( [NXApp runModalFor:passPhrasePanel]!=1)
	{


		[NXApp activate:previousApp];


		outdata =[myCrypter signText: pbdata length:&datalen];

		[statusText setText:outdata[1]];

		if(returnLength)
		{
			[pboard declareTypes: types num: 1 owner:self];
			[pboard writeType: NXAsciiPboardType data: outdata[0] length:returnLength];
		}
		else
		{
			[statusWin makeKeyAndOrderFront:self];
		}
		free (outdata[0]);
		free (outdata[1]);
	}
	else
	{
		[NXApp activate:previousApp];
		[passPhrasePanel orderOut:self];
	}

	[self clearPassPhrase:genericPassPhraseTextField];
    return self;
}

-signFilePBoard:(id) pboard
 userData:(const char *) userData
 error:(char **) msg
{
    const char         *types[1];
    char              **outdata;

    ppMode = SIGN;

    types[0] = NXFilenamePboardType;

	if (!strcmp ([pboard findAvailableTypeFrom: types num:1], NXFilenamePboardType))
	{
		[pboard readType: NXFilenamePboardType data: &pbdata length:&datalen];
	}
    else
	  *msg = "No ASCII data.";

	[[passPhrasePanel center] makeKeyAndOrderFront:self];
	[genericPassPhraseTextField selectText:self];
	[self clearText:statusText];
	previousApp =[NXApp activateSelf:YES];

	if( [NXApp runModalFor:passPhrasePanel]!=1)
	{
		char *aFile;
		char tt[strlen(pbdata)+1];
		
		strcpy(tt, pbdata);

		[NXApp activate:previousApp];

		aFile=strtok(tt, "\t");

		while(aFile) {

			outdata =[myCrypter signFile: aFile length:&datalen];
			
			[statusText setText:outdata[1]];
			
			if(returnLength)
			{
				//[pboard declareTypes: types num: 1 owner:self];
				//[pboard writeType: NXAsciiPboardType data: outdata[0] length:returnLength];
			}
			else
			{
				[statusWin makeKeyAndOrderFront:self];
			}
			free (outdata[0]);
			free (outdata[1]);

			aFile=strtok(NULL, "\t");
		}
	}
	else
	{
		[NXApp activate:previousApp];
		[passPhrasePanel orderOut:self];
	}

	[self clearPassPhrase:genericPassPhraseTextField];
    return self;
}

-doSign:sender
{

	[myCrypter setpassPhrase:[genericPassPhraseTextField stringValue]];
	[passPhrasePanel orderOut:self];

    [NXApp stopModal];

    return self;
}

-checksigPBoard:(id) pboard
 userData:(const char *) userData
 error:(char **) msg
{
    const char         *types[1];
    char              **outdata;

    types[0] = NXAsciiPboardType;

	if (!strcmp ([pboard findAvailableTypeFrom: types num:1], NXAsciiPboardType))
	{
		[pboard readType: NXAsciiPboardType data: &pbdata length:&datalen];
	}
    else
	{
		*msg = "No ASCII data.";
		return self;
	}

	[self clearText:statusText];
	[statusWin display];
	outdata =[myCrypter checkSig: pbdata length:&datalen];
	[statusText setText:outdata[1]];
    free (outdata[0]);
    free (outdata[1]);
	[[statusWin display] makeKeyAndOrderFront:self];
	[NXApp activateSelf:YES];

    return self;
}

-checkFilesigPBoard:(id) pboard
 userData:(const char *) userData
 error:(char **) msg
{
    const char         *types[1];
    char              **outdata;
	char *aFile;

    types[0] = NXFilenamePboardType;

	if (!strcmp ([pboard findAvailableTypeFrom: types num:1], NXFilenamePboardType))
	{
		[pboard readType: NXFilenamePboardType data: &pbdata length:&datalen];
	}
    else
	{
		*msg = "No ASCII data.";
		return self;
	}

	[self clearText:statusText];
	[statusWin display];

  {
	  char tt[strlen(pbdata)+1];
	  
	  strcpy(tt, pbdata);
	  aFile=strtok(tt, "\t");

	  while(aFile) {
		  outdata =[myCrypter checkFileSig: aFile length:&datalen];
		  [statusText setText:outdata[1]];
		  free (outdata[0]);
		  free (outdata[1]);

		  aFile=strtok(NULL, "\t");
	  }
  }
	[[statusWin display] makeKeyAndOrderFront:self];
	[NXApp activateSelf:YES];

    return self;
}


-addkeyPBoard:(id) pboard
 userData:(const char *) userData
 error:(char **) msg
{
    const char         *types[1];
    char              **outdata;

    types[0] = NXAsciiPboardType;

	if (!strcmp ([pboard findAvailableTypeFrom: types num:1], NXAsciiPboardType))
	{
		[pboard readType: NXAsciiPboardType data: &pbdata length:&datalen];
	}
    else
	{
		*msg = "No ASCII data.";
		return self;
	}

	[self clearText:statusText];
	[statusWin display];
	outdata =[myCrypter addKey: pbdata length:&datalen];
	[statusText setText:outdata[1]];
    free (outdata[0]);
    free (outdata[1]);
	[[statusWin display] makeKeyAndOrderFront:self];
	[NXApp activateSelf:YES];

    return self;
}

-addkeyFilePBoard:(id) pboard
 userData:(const char *) userData
 error:(char **) msg
{
    const char         *types[1];
    char              **outdata;
	char *aFile;

    types[0] = NXFilenamePboardType;

	if (!strcmp ([pboard findAvailableTypeFrom: types num:1], NXFilenamePboardType))
	{
		[pboard readType: NXFilenamePboardType data: &pbdata length:&datalen];
	}
    else
	{
		*msg = "No ASCII data.";
		return self;
	}

	[self clearText:statusText];
	[statusWin display];

  {
	  char tt[strlen(pbdata)+1];
	  
	  strcpy(tt, pbdata);
	  aFile=strtok(tt, "\t");
	  
	  while(aFile) {
		  outdata =[myCrypter addKeyFile: aFile length:&datalen];
		  [statusText setText:outdata[1]];
		  free (outdata[0]);
		  free (outdata[1]);
		  
		  aFile=strtok(NULL, "\t");
	  }
  }
	[[statusWin display] makeKeyAndOrderFront:self];
	[NXApp activateSelf:YES];

    return self;
}

-decryptPBoardInWindow:(id) pboard
 userData:(const char *) userData
 error:(char **) msg
{
    char              **outdata;
    const char         *types[1];

    types[0] = NXAsciiPboardType;

	if (!strcmp ([pboard findAvailableTypeFrom: types num:1], NXAsciiPboardType))
	{
		[pboard readType: NXAsciiPboardType data: &pbdata length:&datalen];
	}
    else
	{
		*msg = "No ASCII data.";
		return self;
	}

    ppMode = DECRYPTINWIN;

	[[passPhrasePanel center] makeKeyAndOrderFront:self];
	[genericPassPhraseTextField selectText:self];
	previousApp =[NXApp activateSelf:YES];

	if( [NXApp runModalFor:passPhrasePanel]!=1)
	{
		[self clearText:statusText];
		[self clearText:decryptText];
		[[statusWin display] orderFront:self];
		[[decryptWin display] orderFront:self];
		outdata =[myCrypter decodeText: pbdata length:&datalen];
		[decryptText setText:outdata[0]];
		[statusText setText:outdata[1]];
		free (outdata[0]);
		free (outdata[1]);
		[[statusWin display] orderFront:self];
		[[decryptWin display] makeKeyAndOrderFront:self];
	}
	else
	{
		[NXApp activate:previousApp];
		[passPhrasePanel orderOut:self];
	}
	[self clearPassPhrase:genericPassPhraseTextField];
    return self;
}

-doDecryptInWin:sender
{

	[myCrypter setpassPhrase:[genericPassPhraseTextField stringValue]];
	[passPhrasePanel orderOut:self];

	[NXApp stopModal];

    return self;
}


-setAll:sender
{
    const char         *tempstr;

	tempstr =[PGPPATHForm stringValueAt:0];
	[myCrypter setPGPPATH:tempstr];
    NXWriteDefault ("NXPGP", "PGPPATH", tempstr);
	tempstr =[signForm stringValueAt:0];
    if (strlen (tempstr) != 0)
	{
		[myCrypter setkeyRing:tempstr];
		NXWriteDefault ("NXPGP", "secring", tempstr);
	}
    else
      [myCrypter setkeyRing:"secring.pgp"];

	tempstr =[signForm stringValueAt:1];
    if (strlen (tempstr) != 0)
	{
		[myCrypter setuserID:tempstr];
		NXWriteDefault ("NXPGP", "userid", tempstr);
	}
    else
	{
		[signForm setStringValue: userid at:1];
		[myCrypter setuserID:userid];
	}
    if (sender != self)
	{
		[NXApp deactivateSelf];
		[settingsWindow orderOut:self];
	}
    return self;
}

-setLength:(int) length
{
    returnLength = length;
    return self;
}

-encryptAction:sender;
{
    if (sender == encryptToForm)
	{
		if ([signSwitch intValue])
	      if (strlen ([passTextField stringValue]) == 0)
		  {
			  [passTextField selectText:self];
		  }
	      else
		  {
			  [encryptOK performClick:self];
		  }
		else
	    {
			[encryptOK performClick:self];
	    }
	}
    else if (sender == passTextField)
	{
		if (strlen ([encryptToForm stringValueAt:0]) == 0)
	    {
			NXBeep ();
			[encryptToForm selectTextAt:0];
	    }
		else
	    {
			[encryptOK performClick:self];
	    }
	}
    return self;
}

-passPhraseOk:sender
{
    if (ppMode == DECRYPT)
	{
		[self doDecrypt:self];
	}
    else if (ppMode == SIGN)
	{
		[self doSign:self];
	}
    else if (ppMode == DECRYPTINWIN)
	{
		[self doDecryptInWin:self];
	}
    return self;
}

-infoPanel:sender
{
    if (!infoPanel)
	{
		[NXApp loadNibSection: "InfoHelp.nib" owner:self];
	}
	[[infoPanel center] makeKeyAndOrderFront:sender];
    return self;
}

-helpPanel:sender
{
    if (!helpPanel)
	{
		[NXApp loadNibSection: "InfoHelp.nib" owner:self];
	}
	[helpPanel makeKeyAndOrderFront:sender];
    return self;
}

-cancelSet:sender
{
	[settingsWindow orderOut:self];
    return self;
}
-cancelRequest:sender
{
	[NXApp stopModal:1];
	return self;
}

//Reset all this crud.
-clearText:(id)thisText
{
	[[[[[[[thisText setMonoFont:NO] setSelGray:NX_LTGRAY] selectAll:self] delete:self] setSelGray:NX_WHITE] setTextGray:NX_BLACK] setBackgroundGray:NX_LTGRAY];
	return self;
}
//This may help keep the passphrase from being stored in the memory somewhere.
-clearPassPhrase:(id)thisPassTextField
{
	char *junk;
	int i;

	junk=malloc(256);
	for(i=0;i<255;junk[i++]='X');
	junk[256]='\0';
	[thisPassTextField setStringValue:junk];
	[thisPassTextField setStringValue:""];
	free(junk);
	return self;
}
@end
