#include "microdot.h"

#include "ogre.h"
#include "ogre_protos.h"

#define GID_LV 56
#define GID_EMPS 55
#define GID_LVNEU 16
#define GID_LVBRETT 17
#define GID_LVUSER 18
#define GID_LVLOESCH 19
#define GID_CBZ00		9
#define GID_CBDIREKT	3
#define GID_CBCLEARBEZ	4
#define GID_CBBIN		8
#define GID_CBEB		1
#define GID_EBWAITDAYS  2

#define GID_BETREFF 60
#define GID_COMMENT 61
#define GID_DISKUIN 62
#define GID_DISKUINSEL 63
#define GID_DISKUINME 64

#define GID_BINFILE 70
#define GID_BINNAME 71
#define GID_BINDATE 72
#define GID_BINFILESEL 73

#define GID_SEND 15
#define GID_EDIT 14
#define GID_EDITCOMMENT 30
#define GID_EDITHEADER 32
#define GID_CANCEL 13

#define GID_INFO 31
#define GID_PGP 33
#define GID_SENDHOLD 34

static char *mailbetreff,*windowtitle,*mailcomment,**empliste,*mailbezug,*maildiskin;
static long mailflags;

static char *moab;
static char *mqbez;
static char **moem;
static char *quoteid;
static char *mfto;

static int pgp_sign, pgp_addkey, pgp_nocrypt;

long str2dat(char *date)
{
	char x[6],*p,*p2;
	int c;

	memset(x,0,6);
	if(!date) return(0);
	date=stpblk(date);
	if(!date[0]) return(0);

	/* zuerst zeit */
	p=strchr(date,':');
	if(p) {
		p2=p-1;
		while(isdigit(*p2)) p2--;
		x[4]=atoi(++p);
		x[3]=atoi(++p2);
		p=strchr(p,':');
		if(p) x[5]=atoi(++p);
	}

	/* datum */
	p=strchr(date,'.');
	if(p) {
		p2=p-1;
		while(isdigit(*p2)) p2--;
		x[1]=atoi(++p);
		x[2]=atoi(++p2);
		p=strchr(p,'.');
		if(p) {
			c=atoi(++p);
			if(c>1970) c-=1970; else c-=70;
			x[0]=c;
		}
	}

	return(utpack(x));
}

char *dat2str(long t)
{
	static char ds[32];
	char x[6];

	utunpk(t,x);

	sprintf(ds,"%02ld.%02ld.%ld %02ld:%02ld:%02ld",
		x[2],x[1],x[0]+1970,x[3],x[4],x[5]);
	return(ds);
}

/*void linkmail(char *from,char *to)
{
	BPTR l;
	int rc;

	if( !isv37 || ( prefs.flags2 & MDF2_NOLINKS ) )
	{
		copyfile( from, to );
		return;
	}

	l = Lock( from, SHARED_LOCK );
	if( !l )
		return;
	DeleteFile( to );
	rc = MakeLink( to, l, 0 );
	UnLock( l );
	if( !rc )
		copyfile( from, to );
}
*/

struct emp
{
	struct MinNode n;
	char emp[ 256 ];
	char pgpid[ 256 ];
};

#define SF_PM 1
#define SF_PGP 2

static int reallysendmails(struct ogwin *ogw, struct hlist *addhlist, struct List *emplist, int flags, int binmail, int hold )
{
	char path2[256];
	int c,d;
	char *p,**sppp;
	struct mbrett *sp;
	char tmp[44];
	struct emp *emp;
	struct hlist *hlist;
	struct pindex *pix;
	struct mail new, newpm;
	int oldhold;

	if( binmail )
	{
		if( !moab )
			p = ogreStringValue( ogw, GID_BINFILE );
		else p = TMPMSGNAME;
		c = getfilelen(p);
		if( c<1 )
		{
			c = IoErr();
			askreq("Fehler beim Verschicken:\nImportdatei '%s'\nläßt sich nicht öffnen (%ld)\n%s\n\nPrüfen Sie, ob der Dos-Pfad für die zu verschickende Datei\nrichtig eingetragen ist.","Abbruch",p, c, errorstring(c) );
			return(0);
		}
	}
	else
	{
		c=getfilelen( TMPMSGNAME );
		if(c<1)
		{
			c = IoErr();
			askreq("Fehler beim Verschicken:\nDatei '" TMPMSGNAME "'\nläßt sich nicht öffnen (%ld)\n%s\n\nBitte stellen Sie sicher, daß Sie\neinen Nachrichtentext eingegeben haben.","Abbruch",c, errorstring(c) );
			return(0);
		}
	}

	sp = findbrett( "/»SPOOL", 0 );
	if( !sp )
	{
		askreq( "Interner Versandfehler:\nSpoolbrett nicht gefunden.", "Abbruch" );
		return( NULL );
	}

	//str2_7bit( ogreStringValue( ogw, GID_BETREFF ), path2 );

	/* Header schreiben */
	makedateheader( tmp, time(0) );

	/* Datum auf 0 setzen */
	if( prefs.flags & MDF_TIME00 )
		memset( &tmp[8], '0', 6 );

	hlist = hl_initheader();
	if( !hlist )
	{
		askreq( "Interner Versandfehler:\nKein Speicher für die Headererzeugung.", "Abbruch" );
		return( NULL );
	}

	if( pgpavail )
	{
		if( pgp_sign )
			hl_addheader( hlist, "SIGNED", "PGP" );
	}

	if( pgp_nocrypt )
		hl_addheader( hlist, "STAT", "NOCIPHER" );

	hl_addheader( hlist, "BET", ogreStringValue( ogw, GID_BETREFF ) );
	hl_addheader( hlist, "MID", makemid() );
	hl_addheader( hlist, "EDA", tmp );

	p = ogreStringValue( ogw, GID_COMMENT );
	if(p && p[0])
	{
		//str2_7bit( p, path2 );
		hl_addheader( hlist, "ZUSAMMENFASSUNG", p );
	}

	p = ogreStringValue( ogw, GID_DISKUIN );
	if(p && p[0])
		hl_addheader( hlist, "DISKUSSION-IN", stpblk(p));

	p = ogreStringValue( ogw, GID_BINNAME );
	if(p && p[0])
		hl_addheader( hlist, "FILE", stpblk(p));

	/* Empfangsbestaetigung? */
	if( ( flags & SF_PM ) && ( prefs.flags & MDF_EB ) )
		hl_addheader( hlist, "EB", "" );

	if( moab && moab[0] )
	{
		hl_addheader( hlist, "OAB", moab );
		sppp=moem;
		while(*sppp) 
			hl_addheader( hlist, "OEM", *sppp++ );
	}

	/* Direkt? */
	if( ( flags & SF_PM ) && ogreValue( ogw, GID_CBDIREKT ) )
		hl_addheader( hlist, "PRIO", "10" );

	if( flags & SF_PGP )
	{
		if( binmail == 1 )
		{
			hl_addheader( hlist, "CRYPT-CONTENT-TYP", "BIN" );
			hl_addheader( hlist, "TYP", "BIN" );
		}
		hl_addheader( hlist, "CRYPT", "PGP" );
	}
	else
	{
		/* Bin? */
		if( binmail == 1 )
			hl_addheader( hlist, "TYP", "BIN" );
	}
	
	/* Quote-ID */
#if 0
	if(quoteid)
		hl_addheader( hlist, "X-QUOTE", quoteid );
#endif

	/* Bezuge */
	if( mailbezug && !ogreValue( ogw, GID_CBCLEARBEZ ) )
	{
		if( mqbez )
			hl_addheader( hlist, "BEZ", mqbez );
		hl_addheader( hlist, "BEZ", mailbezug );
		if( mfto )
			hl_addheader( hlist, "F-TO", mfto );
	}

	/* PGP */
	if( pgp_addkey )
		pgp_addpubkey( hlist );
	else if( pgpavail )
		hl_addheader( hlist, "PGP-KEY-AVAIL", prefs.pgp_avail );

	/* EMPs */
	emp = (struct emp *) emplist->lh_Head;
	while( emp->n.mln_Succ )
	{
		hl_addheader( hlist, "EMP", emp->emp );
		emp = (struct emp *) emp->n.mln_Succ;
	}

	/* Zusätzliche */
	if( addhlist )
	{
		struct header *h = hl_findheader( addhlist, NULL );

		while( h )
		{
			hl_addheader( hlist, h->head, h->data );
			h = hl_nextheader( h );
		}
	}

	/* weiter */

	p = TMPMSGNAME;
	if( binmail && !moab )
		p = ogreStringValue( ogw, GID_BINFILE );

	memset( &new, 0, sizeof( new ) );
	header2mail( hlist, &new );

	if( getfilelen( TMPCOMMENTNAME ) > 0 )
		pix = msg_store( TMPCOMMENTNAME, -1, p, -1,
			&new.pufferID, &new.mailID, hlist );
	else
		pix = msg_store( NULL, 0, p, -1,
			&new.pufferID, &new.mailID, hlist );

	if( !pix )
	{
		hl_freeheader( hlist );
		askreq( "Interner Versandfehler.", "Abbruch" );
		return( 0 );
	}

	/*new.maillen = pix->datasize + pix->commentsize;*/
	new.maillen = getfilelen( p );

	/* Spoolbrett */
	oldhold = new.flags & MMF_ARCHIVE;
	if( hold )
		new.flags |= MMF_ARCHIVE;
	else
		new.flags &= ~MMF_ARCHIVE;
	storemail2ix( &new, sp, NULL );
	new.flags |= oldhold;

	if( ! ( prefs.flags2 & MDF2_OWNMAILSNEW ) )
	{
		new.flags &= ~ MMF_UNSEEN;
		new.flags |= MMF_READ;
	}

/*	if( prefs.flags2 & MDF2_OWNMAILSREAD )
	{
		new.flags |= MMF_READ;
	}*/

	/* Nun in die Bretter */

	newpm = new;

	sprintf( new.absender, "%s@%s%s", prefs.username, ISRFC ? prefs.pointname : prefs.boxname, prefs.boxdomain );
	strcpy( new.absenderreal, prefs.userrealname );

	emp = (struct emp *) emplist->lh_Head;
	d = 0;
	while( emp->n.mln_Succ )
	{
		sp = NULL;
		if( emp->emp[0]=='/' )
		{
			sp = findbrett( emp->emp, 0 );
			retest:
			if(!sp)
			{
				int r;
				r = askreq("Warnung:\n%s\nin der Empfängerliste\nist nicht als Brett eingetragen.","Brett eintragen|Ignorieren", emp->emp);
				if( r )
				{
					sp = findbrett( emp->emp, TRUE );
					sortbrettlist();
					calcnumentries();
					makebrettview();
					goto retest;
				}
			}
		}
		else if( !d )
		{
			sp = findpmbrett( hlist );
			d = 1;
			storemail2ix( &newpm, sp, NULL );
			if( ! ( prefs.flags2 & MDF2_OWNMAILSNEW ) )
				sp->b.newmails--;
			if( prefs.flags2 & MDF2_OWNMAILSREAD )
				sp->b.unreadmails--;
			sp = NULL;
		}

		if( sp )
		{
			storemail2ix( &new, sp, NULL );
			if( ! ( prefs.flags2 & MDF2_OWNMAILSNEW ) )
				sp->b.newmails--;
			if( prefs.flags2 & MDF2_OWNMAILSREAD )
				sp->b.unreadmails--;
		}

		emp = (struct emp *) emp->n.mln_Succ;
	}

	hl_freeheader( hlist );
	closeallwriteixfiles();

	return(2);
}

static int reallysend( struct ogwin *ogw, struct hlist *hlist, int hold )
{
	struct List	brettlist;
	struct List pmlist;
	struct List pgplist;
	void *emppool;
	int bin = ogreValue( ogw, GID_CBBIN );
	int c;
	char tmp[256], *p;
	struct emp *emp;
	int rc;
	struct ogrevn *vn;

	ogreCopyStringValue( ogw, GID_BETREFF, tmp );
	if( !tmp[ 0 ] )
	{
		rc = askreq( "Sie haben keinen Betreff angegeben.", "Trotzdem weiter|Abbruch" );
		if( !rc )
			return( 0 );
	}

	NewList( &brettlist );
	NewList( &pmlist );
	NewList( &pgplist );

	emppool = LibCreatePool( NULL, 2048, 2048 );

	loaduserlist();

	for( c = 0; ; c++ )
	{
		vn = ogreLVGetEntry( ogw, GID_LV, c );
		if( !vn )
			break;
		emp = LibAllocPooled( emppool, sizeof( *emp ) );
		strcpy( emp->emp, vn->txt );
		if( emp->emp[0]=='/' )	/* Brett */
		{
			strupr(emp->emp);
			AddTail( &brettlist, (struct Node *) emp );
			continue;
		}
		else /* PM */
		{
			struct user u;

/*			strcpy( tmp, emp->emp );
			p = strchr( tmp, ' ' );
			if( p )
				*p = 0;*/
			if( !pgp_nocrypt && !getuser( emp->emp, &u ) && ( u.flags & UF_PGP ) )
				AddTail( &pgplist, (struct Node *) emp );
			else
				AddTail( &pmlist, (struct Node *) emp );
		}
	}

	/* Gut, die Empfängerlisten stehen bereit */

	freeuserlist( FALSE );

	rc = 0;

	if( !IsListEmpty( &pmlist ) )
		rc |= reallysendmails( ogw, hlist, &pmlist, SF_PM, bin, hold );

	if( !IsListEmpty( &pgplist ) )
		rc |= reallysendmails( ogw, hlist, &pgplist, SF_PM | SF_PGP, bin, hold );

	if( !IsListEmpty( &brettlist ) )
		rc |= reallysendmails( ogw, hlist, &brettlist, 0, bin, hold );

	LibDeletePool( emppool );

	if( !c )
		askreq( "Sie haben keine Empfänger angegeben!", "Abbruch" );

	return( rc );
}


static char *bin_options[] = { "Text", "Binär", "Textdatei", NULL };

/*static char *signmodes[] = { "Clearsig (lesbar)", "Voll (unlesbar)", "Nein", NULL };*/

#define PS_OK 1
#define PS_NOCRYPT 2
#define PS_SIGN 3
#define PS_ADDKEY 4

static void setpgp( void )
{
	struct ogwin *ogw;
	struct IntuiMessage *im;
	struct Window *iw;
	int Done = FALSE;

	ogw = ogreInitWindow( scr, WFLG_RMBTRAP, 0, "PGP-Status" );

	ogreAddGroup( ogw, 0 | OGGP_EXPANDSTRING, OGFRAME_OUTLINE, NULL );

	ogreAddCheckbox( ogw, 0, 'k', "Eigenen _Key anfügen:", PS_ADDKEY, pgp_addkey );
	ogreAddCheckbox( ogw, 1, 'n', "Nachricht _nicht verschlüsseln:", PS_NOCRYPT, pgp_nocrypt );
	ogreAddCheckbox( ogw, 2, 's', "Nachricht _signieren:", PS_SIGN, pgp_sign );
//	ogreAddCycle( ogw, 2, 's', "Nachricht _signieren:", PS_SIGN, signmodes, 20, pgp_sign );

	ogreAddGroup( ogw, 1, OGFRAME_NONE, NULL );
	ogreAddButton( ogw, 0 | OGB_ONENTER, 'o', "_OK", PS_OK );

	iw = ogreOpenWindow( ogw );
	if( !iw )
		return;

	while( !Done )
	{
		while( ( im = ogreIM( ogw ) ) )
		{
			if( im->Class == IDCMP_GADGETHELP)
				showguidenum( "editpgp_gads", im->Code );
			else if( im->Class == GADGETUP )
			{
				switch( im->Code )
				{
					case PS_OK:
						Done = 1;
						break;

				}
			}
		}
	}

	pgp_addkey = ogreValue( ogw, PS_ADDKEY );
	pgp_sign = ogreValue( ogw, PS_SIGN );
	pgp_nocrypt = ogreValue( ogw, PS_NOCRYPT );

	ogreExitWindow( ogw );
}

int sendwinauto;
int dosendwin( 
	char *title,
	char *betreff,
	char *comment,
	char *bezug,
	char **emp,
	long flags,
	char *oab,
	char **oem,
	char *diskin,
	char *quote,
	char *qbez,
	char *binfile,
	char *fileheader,
	char *ftoheader
)
{
	struct IntuiMessage *im;
	struct ogwin *ogw;
	struct ogrevn *vn;
	struct Window *iw;
	int Done = FALSE;
	int bin = flags;
	int isediting = -1;
	char *p;
	void *rtwinlock;
	long tv;
	struct MsgPort *awmp = NULL;
	struct AppWindow *aw = NULL;
	struct AppMessage *am;
	ULONG aws = 0, sig, isig;
	struct WBArg *wba;
	char buffer[ 128 ];
	int c, d;
	struct hlist *hlist = hl_initheader();

	windowtitle	= title;
	mailbetreff	= betreff;
	mailcomment	= comment;
	mailbezug   = bezug;
	empliste    = emp;
	quoteid     = quote;
	mailflags	= flags;
    maildiskin	= diskin;
	moab		= oab;
	moem		= oem;
	mqbez		= qbez;
	mfto		= ftoheader;

	pgp_addkey = pgp_sign = pgp_nocrypt = 0;

	ogw = ogreInitWindow( scr, WFLG_RMBTRAP, 0, title );

	ogreAddGroup( ogw, 0 | OGGP_EXPANDSTRING , OGFRAME_OUTLINE, " Empfänger " );
	ogreAddList( ogw, 0, 0xff, NULL, GID_LV, 40, 4, 0, NULL );
	ogreLVInit( ogw, GID_LV );
	if( emp )
	{
		while( *emp )
		{
			ogreLVAddEntry( ogw, GID_LV, *emp++, 2, NULL );
		}
	}

	ogreAddString( ogw, 1, '\n', NULL, GID_EMPS, NULL, 40, 256 | OGSF_NONEXTACT );
	ogreAddButton( ogw, 2, 'n', "_Neu", GID_LVNEU );
	ogreAddButton( ogw, 2, 'b', "_Brett", GID_LVBRETT );
	ogreAddButton( ogw, 2, 'u', "_UserIn", GID_LVUSER );
	ogreAddButton( ogw, 2, 'l', "_Löschen", GID_LVLOESCH );

	ogreAddGroup( ogw, 0 | OGGP_EXPANDSTRING, OGFRAME_OUTLINE, " Status " );
	ogreAddCheckbox( ogw, 0, '0', "Zeit _00:00:00", GID_CBZ00, prefs.flags & MDF_TIME00 );
	ogreAddCheckbox( ogw, 1, 'z', "Direkt_zustellung", GID_CBDIREKT, FALSE );
	ogreAddCheckbox( ogw, 2, 0, "Bezüge löschen", GID_CBCLEARBEZ, FALSE );
	ogreAddCheckbox( ogw, 3, 'p', "Em_pfangsbestätigung", GID_CBEB, prefs.flags & MDF_EB );
/*	ogreAddInteger( ogw, 4, 'w', "_Wartezeit Tage:", GID_EBWAITDAYS, 7, 0, MAXINT, 11, 3 );*/
	ogreAddCycle( ogw, 4, 'r', "Ve_rsandart", GID_CBBIN, bin_options, 10, bin );

	ogreAddGroup( ogw, 1 | OGGP_EXPANDSTRING, OGFRAME_OUTLINE, " Info " );
	ogreAddString( ogw, 0, 't', "      Be_treff:", GID_BETREFF, betreff, 40, 256 );
	ogreAddString( ogw, 1, 'k', "_Kurzkommentar:", GID_COMMENT, comment, 40, 256 );
	ogreAddString( ogw, 2, 's', "     Di_sku-In:", GID_DISKUIN, diskin, 40, 128 );
	ogreAddButton( ogw, 2 | OGGP_KEEPSIZE, 'S', "?", GID_DISKUINSEL );
	ogreAddButton( ogw, 2 | OGGP_KEEPSIZE, 0, "·", GID_DISKUINME );

	ogreAddGroup( ogw, 2 | OGGP_EXPANDSTRING, OGFRAME_OUTLINE, " Importieren " );
	ogreAddString( ogw, 0, 'd', "_DOS-Datei:", GID_BINFILE, binfile, 40, 256 );
	ogreAddButton( ogw, 0 | OGGP_KEEPSIZE, 'D', "?", GID_BINFILESEL );
	ogreAddString( ogw, 1, 'i', "Date_iname:", GID_BINNAME, fileheader, 16, 256 );
	ogreAddString( ogw, 1, 'm', "Dateidatu_m:", GID_BINDATE, NULL, 10, 32 );

	ogreAddGroup( ogw, 3 | OGGP_EXPANDSTRING, OGFRAME_NONE, NULL );
	ogreAddText( ogw, 0, "Info:", GID_INFO, "", 56, TRUE );
	ogreAddButton( ogw, 1 | OGB_ONENTER, 'v', "_Verschicken", GID_SEND );
	ogreAddButton( ogw, 1, 0, "Parken", GID_SENDHOLD );
	ogreAddButton( ogw, 1, 'e', "_Editieren", GID_EDIT );
	ogreAddButton( ogw, 1, 'o', "K_ommentar", GID_EDITCOMMENT );
	ogreAddButton( ogw, 1, 'h', "_Header", GID_EDITHEADER );

	if( pgpavail )
		ogreAddButton( ogw, 1, 'g', "P_GP", GID_PGP );

	ogreAddButton( ogw, 1, 0x5f, "Hilfe", OGID_HELP );
	ogreAddButton( ogw, 1 | OGB_ONESC, 'a', "_Abbruch", GID_CANCEL );

	/* Bezüge löschen abschalten? */
	if( !bezug )
		ogreEnable( ogw, FALSE, GID_CBCLEARBEZ, 0 );
	ogreEnable( ogw, bin, GID_BINFILE, GID_BINFILESEL, 0 );
	ogreEnable( ogw, bin == 1, GID_BINNAME, GID_BINDATE, 0 );
	ogreEnable( ogw, !bin, GID_EDIT, 0 );
	/*ogreEnable( ogw, prefs.flags & MDF_EB, GID_EBWAITDAYS, 0 );*/
	ogreEnable( ogw, isediting >= 0, GID_EMPS, GID_LVLOESCH, 0 );
	ogreEnable( ogw, !moab, GID_EDIT, GID_CBBIN, GID_BETREFF, 0 );

	iw = ogreOpenWindow( ogw );
	if( !iw )
		return( 0 );

	isig = 1 << iw->UserPort->mp_SigBit;

	if( wbwindow && WorkbenchBase )
	{
		awmp 	= CreateMsgPort();
		aws 	= 1 << awmp->mp_SigBit;
		aw 		= AddAppWindow( 0, NULL, iw, awmp, TAG_DONE );
	}

	goto viewinfo;

	while( !Done )
	{
		if( sendwinauto )
		{
			rtwinlock = rtLockWindow( iw );
			Done = reallysend( ogw, hlist, FALSE );
			rtUnlockWindow( iw, rtwinlock );
			sendwinauto = FALSE;
		}

		sig = mWait( isig | aws );

		if( sig & aws )
		{
			while( ( am = ( struct AppMessage * ) GetMsg( awmp ) ) )
			{
				if( am->am_NumArgs )
				{
					char dirname[ 256 ];

					wba = am->am_ArgList;
					NameFromLock( wba->wa_Lock, dirname, 255 );
					AddPart( dirname, wba->wa_Name, 255 );

					ogreSetStringValue( ogw, GID_BINFILE, dirname );

					tv = getft( dirname );
					if( tv>0 )
					{
						ogreSetStringValue( ogw, GID_BINDATE, dat2str( tv ) );
						ogreSetStringValue( ogw, GID_BINNAME, filepart( dirname ) );

						if( !bin )
							bin = 2;
					}
				}
				ReplyMsg( am );
			}
		}

		while( ( im = ogreIM( ogw ) ) )
		{
			if( im->Class == IDCMP_GADGETHELP)
				showguidenum( "sendwin_gads", im->Code );
			else if( im->Class == GADGETUP )
			{
				switch( im->Code )
				{

					case GID_EMPS:
						if( isediting >= 0 )
						{
							if( ! ( * ( stpblk( ogreStringValue( ogw, GID_EMPS ) ) ) ) )
							{
								ogreLVSelect( ogw, GID_LV, -1 );
								ogreLVSetEntry( ogw, GID_LV, isediting, ogreStringValue( ogw, GID_EMPS ), 2 );
								ogreLVRefresh( ogw, GID_LV );					
								isediting = -1;
								ogreSetStringValue( ogw, GID_EMPS, "" );
							}
							else
							{
								ogreLVSelect( ogw, GID_LV, -1 );
								ogreLVSetEntry( ogw, GID_LV, isediting, ogreStringValue( ogw, GID_EMPS ), 2 );
								ogreLVRefresh( ogw, GID_LV );					
								isediting = -1;
								ogreSetStringValue( ogw, GID_EMPS, "" );
							}
						}
						break;

					case GID_CBBIN:
						bin = ogreValue( ogw, GID_CBBIN );
						break;

					case GID_CBEB:
						if( ogreValue( ogw, GID_CBEB ) )
							prefs.flags |= MDF_EB;
						else
							prefs.flags &= ~MDF_EB;
						break;

					case GID_CBZ00:
						if( ogreValue( ogw, GID_CBZ00 ) )
							prefs.flags |= MDF_TIME00;
						else
							prefs.flags &= ~MDF_TIME00;
						break;

					case GID_LV:
						isediting = ogreValue( ogw, GID_LV );
						vn = ogreLVGetEntry( ogw, GID_LV, isediting );
						if( vn )
						{
							ogreEnable( ogw, TRUE, GID_EMPS, 0 );
							ogreSetStringValue( ogw, GID_EMPS, vn->txt );
							if( im->Qualifier )
								ogreActivate( ogw, GID_EMPS );
						}
						break;

					case GID_LVLOESCH:
						if( isediting >= 0 )
						{
							ogreLVRemEntry( ogw, GID_LV, isediting );
							ogreLVRefresh( ogw, GID_LV );
							isediting = -1;
							ogreSetStringValue( ogw, GID_EMPS, "" );
						}
						break;

					case GID_LVBRETT:
						rtwinlock = rtLockWindow( iw );
						while( ( p = dobrettreqmulti() ) )
						{
							if( !ogreLVFindEntry( ogw, GID_LV, p ) )
							{
								ogreLVAddEntry( ogw, GID_LV, p, 2, 0 );
								isediting = -1;
								ogreLVSelect( ogw, GID_LV, -1 );
								ogreLVRefresh( ogw, GID_LV );
							}
						}
						rtUnlockWindow( iw, rtwinlock );
						break;
					
					case GID_LVUSER:
						rtwinlock = rtLockWindow( iw );
						while( ( p = douserreqmulti() ) )
						{
							if( !ogreLVFindEntry( ogw, GID_LV, p ) )
							{
								ogreLVAddEntry( ogw, GID_LV, p, 2, 0 );
								isediting = -1;
								ogreLVSelect( ogw, GID_LV, -1 );
								ogreLVRefresh( ogw, GID_LV );
							}
						}
						rtUnlockWindow( iw, rtwinlock );
						break;

					case GID_LVNEU:
						isediting = ogreLVAddEntry( ogw, GID_LV, "", 1, 0 );
						ogreLVRefresh( ogw, GID_LV );
						ogreSetStringValue( ogw, GID_EMPS, "" );
						ogreActivate( ogw, GID_EMPS );
						break;

					case GID_BINFILESEL:
						newdofrselfile( ogw, GID_BINFILE, "Importdatei auswählen..." );
						p = ogreStringValue( ogw, GID_BINFILE );
						tv = getft( p );
						if( tv>0 )
						{
							ogreSetStringValue( ogw, GID_BINDATE, dat2str( tv ) );
							ogreSetStringValue( ogw, GID_BINNAME, filepart( p ) );
						}
						p = ogreStringValue( ogw, GID_BETREFF );
						if( !*p )
							ogreSetStringValue( ogw, GID_BETREFF, ogreStringValue( ogw, GID_BINNAME ) );
						break;

					case GID_DISKUINSEL:
						rtwinlock = rtLockWindow( iw );
						p = dobrettreq();
						rtUnlockWindow( iw, rtwinlock );
						if( !p )
							break;
						ogreSetStringValue( ogw, GID_DISKUIN, p );
						break;

					case GID_DISKUINME:
						sprintf( buffer, "%s@%s%s", prefs.username, ISRFC ? prefs.pointname : prefs.boxname, prefs.boxdomain );
						ogreSetStringValue( ogw, GID_DISKUIN, buffer );
						break;

					case GID_EDIT:
						rtwinlock = rtLockWindow( iw );
						calleditor( TMPMSGNAME );
						rtUnlockWindow( iw, rtwinlock );
						ActivateWindow( iw );
						break;

					case GID_EDITCOMMENT:
						rtwinlock = rtLockWindow( iw );
						calleditor( TMPCOMMENTNAME );
						rtUnlockWindow( iw, rtwinlock );
						ActivateWindow( iw );
						break;

					case GID_EDITHEADER:
						rtwinlock = rtLockWindow( iw );
						doeditheader( hlist, "Zusätzliche Headerzeilen anfügen:" );
						rtUnlockWindow( iw, rtwinlock );
						ActivateWindow( iw );
						break;

					case GID_PGP:
						ogreLockWindow( ogw );
						setpgp();
						ogreUnlockWindow( ogw );
						break;

					case GID_SEND:
						rtwinlock = rtLockWindow( iw );
						Done = reallysend( ogw, hlist, FALSE );
						rtUnlockWindow( iw, rtwinlock );
						break;

					case GID_SENDHOLD:
						rtwinlock = rtLockWindow( iw );
						Done = reallysend( ogw, hlist, TRUE );
						rtUnlockWindow( iw, rtwinlock );
						break;

					case GID_CANCEL:
						Done = 1;
						break;
				}
			}

viewinfo:

			/* Infotext erzeugen */
			c = getfilelen( TMPCOMMENTNAME );
			d = getfilelen( ( bin ) ? ogreStringValue( ogw, GID_BINFILE ) : TMPMSGNAME );
			buffer[ 0 ] = 0;
			if( d > 0 )
				sprintf( buffer, "%lD Byte Inhalt", d );
			if( c > 0 )
			{
				if( buffer[ 0 ] )
					strcat( buffer, ", " );
				asprintf( buffer, "%lD Byte Kommentar", c );
			}

			ogreSetStringValue( ogw, GID_INFO, buffer );

			ogreEnable( ogw, bin, GID_BINFILE, GID_BINFILESEL, 0 );
			ogreEnable( ogw, bin == 1, GID_BINNAME, GID_BINDATE, 0 );
			ogreEnable( ogw, !bin && !moab, GID_EDIT, 0 );
		/*	ogreEnable( ogw, prefs.flags & MDF_EB, GID_EBWAITDAYS, 0 );*/
			ogreEnable( ogw, isediting >= 0, GID_EMPS, GID_LVLOESCH, 0 );
		}
	}

	if( aw )
		RemoveAppWindow( aw );

	if( awmp )
		DeleteMsgPort( awmp );

	ogreExitWindow( ogw );

	hl_freeheader( hlist );

	DeleteFile( TMPMSGNAME );
	DeleteFile( TMPCOMMENTNAME );
	return( Done == 2 );
}

#define HE_OK 1
#define HE_CANCEL 2
#define HE_HLV 3
#define HE_HL 5
#define HE_NEW 6
#define HE_DEL 7
#define HE_DLV 4

static char *hdesc[] = {
	"EMP:             Empfänger",
	"BEZ:             BezugsID",
	"STAT:            Status",
	"ANTWORT-AN:      Adresse",
	"DISKUSSION-IN:   Brett/Adresse",
	"STICHWORT:       Stichwörter",
	"ZUSAMMENFASSUNG: Kurzkommentar",
	"EB:              Empfangsbestätigung",
	NULL
};

#if 0
static char *hexcl[] = {
	"ABS",
	"MID",
	"EDA",
	"DDA",
	"FILE",
	"BET",
	"MAILER",
	"LEN",
	"KOM",
	NULL
};
#endif

int doeditheader( struct hlist *hlist, char *title )
{
	struct ogwin *ogw;
	struct IntuiMessage *im;
	struct ogrevn *vn;
	struct Window *iw;
	int Done = FALSE;
	char **ptr;
	struct header *hl = ( struct header * ) hlist->l.mlh_Head;
	char buffer[ 8192 + 256 ];
	int isediting = -1;
	char *p;
	int c;

	ogw = ogreInitWindow( scr, WFLG_RMBTRAP, 0, title );

	ogreAddGroup( ogw, 0 | OGGP_EXPANDSTRING , OGFRAME_NONE, NULL );
	ogreAddList( ogw, 0, 0xff, NULL, HE_HLV, 32, 12, 0, NULL );
	ogreAddList( ogw, 0, 0, NULL, HE_DLV, 32, 12, 0, NULL );

	ogreLVInit( ogw, HE_HLV );
	while( hl->n.mln_Succ )
	{
		sprintf( buffer, "%s:%s", hl->head, hl->data );
		ogreLVAddEntry( ogw, HE_HLV, buffer, 2, hl );
		hl = ( struct header * ) hl->n.mln_Succ;
	}

	ogreLVInit( ogw, HE_DLV );
	ptr = hdesc;
	while( *ptr )
	{
		ogreLVAddEntry( ogw, HE_DLV, *ptr++, FALSE, NULL );
	}

	ogreAddString( ogw, 1, '\n', NULL, HE_HL, NULL, 40, 8192 | OGSF_NONEXTACT );
	ogreAddButton( ogw, 2, 'n', "_Neu", HE_NEW );
	ogreAddButton( ogw, 2, 'l', "_Löschen", HE_DEL );

	ogreAddGroup( ogw, 1 , OGFRAME_NONE, NULL );
	ogreAddButton( ogw, 0 | OGB_ONENTER, 'o', "_OK", HE_OK );
	ogreAddHelp( ogw, 0 );
	ogreAddButton( ogw, 0 | OGB_ONESC, 'a', "_Abbruch", HE_CANCEL );

	ogreEnable( ogw, FALSE, HE_HL, HE_DEL, 0 );

	iw = ogreOpenWindow( ogw );
	if( !iw )
		return( 0 );

	while( !Done )
	{
		ogreEnable( ogw, isediting >= 0, HE_HL, HE_DEL, 0 );

		if( ( im = ogreWaitIM( ogw ) ) )
		{
			if( im->Class == IDCMP_GADGETHELP)
				showguidenum( "editheader_gads", im->Code );
			else if( im->Class == GADGETUP )
			{
				switch( im->Code )
				{
					case HE_OK:
						Done = 1;
						break;

					case HE_CANCEL:
						Done = 2;
						break;

					case HE_DEL:
						if( isediting >= 0 )
						{
							ogreLVRemEntry( ogw, HE_HLV, isediting );
							ogreLVRefresh( ogw, HE_HLV );
							isediting = -1;
							ogreSetStringValue( ogw, GID_EMPS, "" );
						}
						break;

					case HE_NEW:
						isediting = ogreLVAddEntry( ogw, HE_HLV, "", 1, 0 );
						ogreLVRefresh( ogw, HE_HLV );
						ogreSetStringValue( ogw, HE_HL, "" );
						ogreActivate( ogw, HE_HL );
						break;

					case HE_DLV:
						vn = ogreLVGetEntry( ogw, HE_DLV, ogreValue( ogw, HE_DLV ) );
						if( vn )
						{
							strcpy( buffer, vn->txt );
							p = strchr( buffer, ':' );
							if( p )
								p[ 1 ] = 0;
							isediting = ogreLVAddEntry( ogw, HE_HLV, buffer, 3, 0 );
							ogreLVRefresh( ogw, HE_HLV );
							ogreSetStringValue( ogw, HE_HL, buffer );
							ogreActivate( ogw, HE_HL );
						}
						break;

					case HE_HLV:
						isediting = ogreValue( ogw, HE_HLV );
						vn = ogreLVGetEntry( ogw, HE_HLV, isediting );
						if( vn )
						{
							ogreSetStringValue( ogw, HE_HL, vn->txt );
							if( im->Qualifier )
								ogreActivate( ogw, HE_HL );
						}
						break;

					case HE_HL:
						if( isediting >= 0 )
						{
							ogreLVSelect( ogw, HE_HLV, -1 );
							ogreLVSetEntry( ogw, HE_HLV, isediting, ogreStringValue( ogw, HE_HL ), 2 );
							ogreLVRefresh( ogw, HE_HLV );					
							isediting = -1;
							ogreSetStringValue( ogw, HE_HL, "" );
						}
						break;
				}
			}
		}
	}

	if( Done == 1 )
	{
		hl_clearhlist( hlist );

		for( c = 0; ; c++ )
		{
			vn = ogreLVGetEntry( ogw, HE_HLV, c );
			if( !vn )
				break;
			strcpy( buffer, vn->txt );
			p = strchr( buffer, ':' );
			if( !p )
				continue;
			*p++ = 0;
			p = stpblk( p );
			hl_addheader( hlist, buffer, p );
		}

	}

	ogreExitWindow( ogw );
	return( Done == 1 );
}
