/*
    Pirate Bulletin Board System
    Copyright (C) 1990, Edward Luke

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 1, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


    Email address:
    lush@Athena.EE.MsState.EDU
*/
#include "bbs.h"
#include "io.h"

char currmaildir[4096] ;

m_init()
{
	sprintf(currmaildir,"mail/%s/%s",currentuser.userid,DIR) ;
}

do_send(userid,title)
char *userid, *title ;
{
	struct fileheader newmessage ;
	char fname[4096] ;
	char *ip ;
	struct stat st ;
	int fp ;

	if(!getuser(userid))
	  return -1 ;
	sprintf(genbuf,"mail/%s",userid) ;
	if(stat(genbuf,&st) == -1) {
		if(mkdir(genbuf,0755) == -1) 
		  return -1 ;
	} else {	
		if(!(st.st_mode & S_IFDIR))
		  return -1 ;
	}
	bzero(&newmessage,sizeof(newmessage)) ;
    sprintf(fname,"M.%d.A",time(0)) ;
	sprintf(genbuf,"mail/%s/%s",userid,fname) ;
	ip = rindex(fname,'A') ;
	while((fp = open(genbuf,O_CREAT|O_EXCL|O_WRONLY,0644)) == -1) {
		if(*ip == 'Z')
		  ip++,*ip = 'A', *(ip + 1) = '\0' ;
		else
		  (*ip)++ ;
		sprintf(genbuf,"mail/%s/%s",userid,fname) ;
	}
	close(fp) ;
	strcpy(newmessage.filename,fname) ;
	if(!title)
      getdata(2,0,"Title: ",newmessage.title,STRLEN,DOECHO,NULL) ;
	else 
	  strncpy(newmessage.title,title,STRLEN) ;
	sprintf(genbuf,"%s (%s)",currentuser.userid,currentuser.username) ;
	strncpy(newmessage.owner,genbuf,STRLEN) ;
	sprintf(genbuf,"%s -mt \001%s\001 mail/%s/%s",EDITOR,
            newmessage.title,userid,fname) ;
    if(do_exec(genbuf,NULL) != 0) {
        sprintf(genbuf,"mail/%s/%s",userid,fname) ;
        unlink(genbuf) ;
        return -1 ;
    }
	clear() ;
	sprintf(genbuf,"mail/%s/%s",userid,DIR) ;
	if(append_record(genbuf,&newmessage,sizeof(newmessage)) == -1)
	  return -1 ;
	return 0 ;
}

m_send()
{
	char uident[STRLEN] ;

	u_namelist() ;
	move(2,0) ;
	prints("<Enter Userid>\n") ;
	move(1,0) ;
	clrtoeol() ;
	namecomplete("To: ",uident) ;
	if(uident[0] == '\0') {
		clear() ;
		return 0 ;
	}
	if(do_send(uident,NULL))
	  prints("Mail was aborted.\n") ;
	else
	  prints("File Sent\n") ;
	pressreturn() ;
	return 0 ;
}


read_mail(fptr)
struct fileheader *fptr ;
{
	sprintf(genbuf,"mail/%s/%s",currentuser.userid,fptr->filename) ;
	more(genbuf,NA) ;
	fptr->accessed[0] = 1 ;
	return 0 ;
}

int mrd ;

int delmsgs[1024] ;
int delcnt ;

read_new_mail(fptr)
struct fileheader *fptr ;
{
	static int idc ;

	if(fptr == NULL) {
		delcnt = 0 ;
		idc = 0 ;
		return 0;
	}
	idc++ ;
	if(fptr->accessed[0])
	  return 0 ;
	prints("Read message titled '%s' from: %s?\n",fptr->title,fptr->owner) ;
	prints("(Yes, or No): ") ;
	getdata(1,0,"(Yes, or No): ",genbuf,4,DOECHO,NULL) ;
	if(genbuf[0] != 'y' && genbuf[0] != 'Y' && genbuf[0] != '\0') {
		clear() ;
		return 0 ;
	}
	read_mail(fptr) ;
	mrd = 1 ;
	if(substitute_record(currmaildir,fptr,sizeof(*fptr),idc))
	  return -1 ;
	prints("Delete message?  (Yes, or No): ") ;
	getdata(0,0,NULL,genbuf,4,DOECHO,NULL) ;
	if(genbuf[0] != 'y' && genbuf[0] != 'Y') {
		clear() ;
		return 0;
	}
	sprintf(genbuf,"mail/%s/%s",currentuser.userid,fptr->filename) ;
	unlink(genbuf) ;
	delmsgs[delcnt++] = idc ;
	clear() ;
    return 0 ;
}

m_new()
{
	clear() ;
	mrd = 0 ;
	read_new_mail(NULL) ;
	if(apply_record(currmaildir,read_new_mail,sizeof(struct fileheader)) == -1) {
		clear() ;
		move(0,0) ;
		prints("No new messages\n\n\n") ;
		return -1 ;
	}
	if(delcnt) {
		while(delcnt--)
		  delete_record(currmaildir,sizeof(struct fileheader),delmsgs[delcnt]) ;
	}
	clear() ;
	move(0,0) ;
	if(mrd)
	  prints("No more messages.\n\n\n") ;
	else
	  prints("No new messages.\n\n\n") ;
	return -1 ;
}

mailtitle()
{
	prints("Interactive Read Menu                (FOR Mail)\n") ;
	prints("(n)ext Message     (p)revious Message     (r)ead Message     (e)xit Read Menu\n");
	prints("(## <cr>) go to message ##     ($) go to last message     (h) Get Help Screen\n");
	prints(" ENT   %-20s %s\n","From","Header") ;
	clrtobot() ;
}

char *
maildoent(num,ent)
struct fileheader *ent ;
{
	static char buf[512] ;
	char b2[512] ;
	char *t ;

	strncpy(b2,ent->owner,STRLEN) ;
	if (t = index(b2,' '))
	  *t = '\0' ;
	sprintf(buf," %3d %c %-20s %s",num,ent->accessed[0]?' ':'N',b2,ent->title) ;
	return buf ;
}

mail_read(ent,fileinfo,direct)
int ent ;
struct fileheader *fileinfo ;
char *direct ;
{
	char buf[512] ;
	char *t ;

	clear() ;
	strcpy(buf,direct) ;
	if(t = rindex(buf,'/'))
	  *t = '\0' ;
	sprintf(genbuf, "%s/%s",buf,fileinfo->filename) ;
	more(genbuf, YEA) ;
	fileinfo->accessed[0] = 1 ;
	substitute_record(currmaildir, fileinfo, sizeof(*fileinfo),ent) ;
	return FULLUPDATE ;
}

/*ARGSUSED*/
mail_reply(ent,fileinfo,direct)
int ent ;
struct fileheader *fileinfo ;
char *direct ;
{
	char uid[STRLEN] ;
	char title[STRLEN] ;
	char *t ;

	strncpy(uid,fileinfo->owner,STRLEN) ;
	if(t = index(uid,' '))
	  *t = '\0' ;
	strcpy(title,"Re: ") ;
	strncat(title,fileinfo->title,STRLEN-5) ;
	if(do_send(uid,title))
	  prints("Mail was Aborted.\n") ;
	else
	  prints("File Sent\n") ;
	pressreturn() ;
	return FULLUPDATE ;
}

mail_del(ent,fileinfo,direct)
int ent ;
struct fileheader *fileinfo ;
char *direct ;
{
	char buf[512] ;
	char *t ;
	extern int cmpfilename() ;
	extern char currfile[] ;

	clear() ;
	prints("Delete Message '%s' ",fileinfo->title) ;
	Getyn(genbuf) ;
	if(strcmp(genbuf,"Yes") && strcmp(genbuf,"yes")) { /* if not yes quit */
		move(2,0) ;
		prints("Quiting Delete Post\n") ;
		pressreturn() ;
		clear() ;
		return FULLUPDATE ;
	}		
	strcpy(buf,direct) ;
	if (t = rindex(buf,'/'))
	  *t = '\0' ;
	strncpy(currfile,fileinfo->filename,STRLEN) ;
	if(!delete_file(direct,sizeof(struct fileheader),ent,cmpfilename)) {
		sprintf(genbuf,"%s/%s",buf,fileinfo->filename) ;
		unlink(genbuf) ;
		return FULLUPDATE ;
	}
	move(2,0) ;
	prints("Delete failed\n") ;
	pressreturn() ;
	clear() ;
	return FULLUPDATE ;
}

extern int mailreadhelp() ;

struct one_key  mail_comms[] = {
	'd',        mail_del,
	'r',        mail_read,
	'R',        mail_reply,
        'h',        mailreadhelp,
        0x0a,       mailreadhelp, /* CTRL(J) */
	'\0',       NULL
  } ;

m_read()
{
    i_read(currmaildir,mailtitle,maildoent,&mail_comms[0]) ;
    return 0 ;
}
