/*
** Copyright 1998 - 2023 S. Varshavchik.
** See COPYING for distribution information.
*/

#include	"config.h"
#include	"maildir.h"
#include	"rfc822/rfc822.h"
#include	"alarmtimer.h"
#include	"alarmsleep.h"
#include	"config.h"
#include	"xconfig.h"
#include	"funcs.h"
#include	"varlist.h"
#if HAVE_SYS_STAT_H
#include	<sys/stat.h>
#endif
#include	<errno.h>
#include	<stdlib.h>
#include	"mytime.h"

#if HAVE_UNISTD_H
#include	<unistd.h>
#endif
#include	<iostream>
#include	"../maildir/maildirquota.h"
#include	"../maildir/maildircreate.h"
#include	"../maildir/maildirmisc.h"
#include	"../maildir/maildirkeywords.h"
#include	"../maildir/maildirwatch.h"

#include "../maildir/maildirquota.h"
extern int quota_warn_percent;
extern const char *quota_warn_message;

Maildir::Maildir() : is_open(0) , is_afs(0)
{
}

Maildir::~Maildir()
{
	MaildirAbort();
}

////////////////////////////////////////////////////////////////////////////
//
//  Attempt to detect if this is a Maildir directory.
//
////////////////////////////////////////////////////////////////////////////

int Maildir::IsMaildir(const char *name)
{
std::string	dirname;
std::string	subdirname;
struct	stat stat_buf;

	if (!name || !*name)	return (0);	// Nope, not a Maildir
	dirname=name;

	if (dirname.back() == SLASH_CHAR)
		dirname.pop_back();	// Strip trailing /
	subdirname=dirname;
	subdirname += "/tmp";
	if ( stat( subdirname.c_str(), &stat_buf ) ||
		! S_ISDIR(stat_buf.st_mode) )	return (0);
	subdirname=dirname;
	subdirname += "/new";
	if ( stat( subdirname.c_str(), &stat_buf ) ||
		! S_ISDIR(stat_buf.st_mode) )	return (0);
	subdirname=dirname;
	subdirname += "/cur";
	if ( stat( subdirname.c_str(), &stat_buf ) ||
		! S_ISDIR(stat_buf.st_mode) )	return (0);
	return (1);	// If it looks like a duck, walks like a duck...
}

int	Maildir::MaildirOpen(const char *dir, rfc822::fdstreambuf &file, off_t s)
{
	std::string	buf;
	struct maildirsize quotainfo;

	const char *quotap;

	std::string quotabuf;

	quotabuf= GetVar("MAILDIRQUOTA");

	quotap=quotabuf.c_str();

	if (!*quotap)
		quotap=NULL;

	MaildirAbort();

AlarmTimer	abort_timer;
static long	counter=0;

	set_integer(buf, counter++);

	struct maildir_tmpcreate_info createInfo;

	maildir_tmpcreate_init(&createInfo);

	createInfo.maildir=dir;
	createInfo.uniq=buf.c_str();
	createInfo.msgsize=s;
	createInfo.openmode=0666;

	abort_timer.Set( 24 * 60 * 60 );
	while (!abort_timer.Expired())
	{
		std::string name_buf;

		std::string um=GetVar("UMASK");
		unsigned int umask_val=077;

		sscanf(um.c_str(), "%o", &umask_val);

		umask_val=umask(umask_val);

		int f=maildir_tmpcreate_fd(&createInfo);
		umask(umask_val);

		if (f >= 0)
		{
			std::string b;

			std::string flags=GetVar("FLAGS");

			tmpname=createInfo.tmpname;

			if (!flags.empty())
			{
				const char *p=flags.c_str();

				while (*p)
				{
					if (strchr("DRSF", *p) == NULL)
					{
						f=0;
						break;
					}
					++p;
				}
			}

			if (!flags.empty())
			{
				newname=createInfo.curname;
				newname += ":2,";
				newname += flags;
			}
			else
			{
				newname=createInfo.newname;
			}

			maildir_tmpcreate_free(&createInfo);

			file=rfc822::fdstreambuf{f};
			is_open=1;
			maildirRoot=dir;

			if (maildir_quota_add_start(dir, &quotainfo, s,
						    1, quotap))
			{
				file=rfc822::fdstreambuf{};
				unlink( tmpname.c_str() );
				is_open=0;
				maildir_deliver_quota_warning(dir,
							      quota_warn_percent,
							      quota_warn_message);
				std::cerr << "maildrop: maildir over quota.\n"
					  << std::flush;
				return (-1);
			}

			maildir_quota_add_end(&quotainfo, s, 1);
			return (0);
		}

		if (errno != EAGAIN)
		{
			std::cerr << "maildrop: " << dir << ": "
				  << strerror(errno)
				  << "\n"
				  << std::flush;
			return -1;
		}

		AlarmSleep	try_again(2);
	}

	std::cerr << "maildrop: time out on maildir directory.\n"
		  << std::flush;
	return (-1);
}

void	Maildir::MaildirSave()
{
	if (is_open)
	{
		std::string keywords=GetVar("KEYWORDS");

		const char *keywords_s=keywords.c_str();

		while (*keywords_s && *keywords_s == ',')
			++keywords_s;

		if (*keywords_s)
		{
			mail::keywords::list keywords;

			while (*keywords_s)
			{
				if (*keywords_s == ',')
				{
					++keywords_s;
					continue;
				}

				const char *p=keywords_s;

				while (*keywords_s && *keywords_s != ',')
					++keywords_s;

				keywords.emplace(p, keywords_s);
			}

			if (!mail::keywords::update(
				    maildirRoot,
				    strrchr(newname.c_str(), '/')+1,
				    keywords))
			{
				/* Maybe the keyword directory needs creating */

				maildir::create_keyword_dir(
					maildirRoot
				);

				if (!mail::keywords::update(
					    maildirRoot,
					    strrchr(newname.c_str(), '/')+1,
					    keywords))
				{
					throw strerror(errno);
				}
			}
		}

		std::string dir;

		if (link( tmpname.c_str(), newname.c_str()) < 0)
		{
			if (errno == EXDEV){
				if(rename(tmpname.c_str(), newname.c_str()) < 0)
					throw "rename() failed.";
				is_afs = 1;
			}
			else
			{
				throw "link() failed.";
			}
		}
		dir=newname;
		const char *p=dir.c_str();
		const char *q=strrchr(p, '/');

		if (q)
		{
			dir.resize(q-p);

#if EXPLICITDIRSYNC
			int syncfd=open(dir.c_str(), O_RDONLY);

			if (syncfd >= 0)
			{
				fsync(syncfd);
				close(syncfd);
			}
#endif

			dir.resize(q-p);
			dir += "/../";

			maildir_deliver_quota_warning(dir.c_str(),
						      quota_warn_percent,
						      quota_warn_message);
		}
	}
}

void	Maildir::MaildirAbort()
{
	if (is_open && !is_afs)	unlink( tmpname.c_str() );
}
