#include "config.h"
/*
** Copyright 2000-2006 S. Varshavchik.  See COPYING for
** distribution information.
*/

/*
*/
#include	"sqwebmail.h"
#include	<stdio.h>
#include	<stdlib.h>
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif
#include	<string.h>
#include	"cgi/cgi.h"
#include	"ldapaddressbook/ldapaddressbook.h"
#include	"maildir/maildircreate.h"
#include	"numlib/numlib.h"
#include	"htmllibdir.h"
#include	"addressbook.h"
#include	"pref.h"
#include	"rfc2045/base64.h"

#define	LOCALABOOK	"sqwebmail-ldapaddressbook"

extern "C" {
	extern void output_scriptptrget();
};

extern void output_attrencoded(const char *);
extern void output_attrencoded_oknl(const char *p);
extern void output_urlencoded(const char *);
extern void output_attrencoded_fp(const char *, FILE *);
extern void output_attrencoded_oknl_fp(const char *, FILE *);

#include	<courier-unicode.h>

void	ldaplist()
{
struct ldapabook *abooks[2];
int	i;
struct ldapabook *p;
const char	*delabook=getarg("DELABOOK");
const char	*sysbook=getarg("SYSBOOK");

	if (!delabook)	delabook="";
	if (!sysbook)	sysbook="";

	if (*cgi("addabook"))
	{
		struct ldapabook newbook;

		memset(&newbook, 0, sizeof(newbook));

		newbook.name=(char *)cgi("name");
		newbook.host=(char *)cgi("host");
		newbook.port=(char *)cgi("port");
		newbook.suffix=(char *)cgi("suffix");

		if (*newbook.name && *newbook.host &&
			ldapabook_add(LOCALABOOK, &newbook) < 0)
		{
			printf("<pre>\n");
			perror("ldapabook_add");
			printf("</pre>\n");
		}
	}

	if (*cgi("delabook"))
	{
		maildir::tmpcreate_info createInfo;
		int fd;

		createInfo.uniq="abook";

		if ((fd=createInfo.fd()) >= 0)
		{
			close(fd);
			unlink(createInfo.tmpname.c_str());
			ldapabook_del(LOCALABOOK, createInfo.tmpname.c_str(),
				      cgi("ABOOK"));
		}
	}

	abooks[0]=ldapabook_read(LDAPADDRESSBOOK);
	abooks[1]=ldapabook_read(LOCALABOOK);

	printf("<table border=\"0\" cellpadding=\"8\" width=\"100%%\">\n");
	for (i=0; i<2; i++)
	{
		for (p=abooks[i]; p; p=p->next)
		{
			printf("<tr valign=\"top\"><td align=\"right\">");
			printf("<input type=\"radio\" name=\"ABOOK\"");

			if (pref_ldap.size() && pref_ldap == p->name)
				printf(" checked=\"checked\"");

			printf(" value=\"");
			output_attrencoded(p->name);
			printf("\" /></td><td><font size=\"+1\""
			       " class=\"ldaplist-name\">%s</font><br />"
			       "&nbsp;&nbsp;&nbsp;<span class=\"tt\"><font size=\"-2\""
			       " class=\"ldaplist-ldapurl\">ldap://", p->name);
			if (*p->binddn || *p->bindpw)
			{
				printf("%s", p->binddn);
				if (*p->bindpw)
					printf(":%s", p->bindpw);
				printf("@");
			}
			printf("%s", p->host);
			if (atoi(p->port) != 389)
				printf(":%s", p->port);
			if (*p->suffix)
			{
				std::string q;

				printf("/");
				q.reserve(cgi_encode::estimate(p->suffix,
					cgi_encode::noeq));
				cgi_encode::encode(std::back_inserter(q), p->suffix,
					cgi_encode::noeq);

				printf("%s", q.c_str());
			}
			printf("</font></span>%s</td></tr>",
				i ? "":sysbook);
		}
	}

	if (abooks[1])
	{
		printf("<tr><td></td><td>");
		printf("<input type=\"submit\" name=\"delabook\" value=\"%s\" />",
				delabook);
		printf("</td></tr>\n");
	}
	printf("</table>\n");
	ldapabook_free(abooks[0]);
	ldapabook_free(abooks[1]);
}

static std::string getfilter()
{
	const char *p;

	if (!*cgi("ldapsearch"))
		return NULL;

	p=cgi("value");

	if (!*p)
		return NULL;

	return unicode::iconvert::convert(p, sqwebmail_content_charset, unicode::utf_8);
}

struct search_info {
	FILE *fpw;
	char errmsgbuf[1024];
	unsigned counter;
};

int	ldapsearch()
{
	if (*cgi("ABOOK") == 0) return (-1);

	if (getfilter().empty())
		return -1;
	return 0;
}

static void save_errmsg(const char *errmsg,
			void *voidarg)
{
	struct search_info *si=(struct search_info *)voidarg;

	si->errmsgbuf[0]=0;
	strncat(si->errmsgbuf, errmsg, sizeof(si->errmsgbuf)-1);
}


static int parsesearch(const char *cn, const char *mail,
		       void *voidarg)
{
	struct search_info *si=(struct search_info *)voidarg;

	char	numbuf[NUMBUFSIZE];
	char	numbuf2[NUMBUFSIZE+10];

	std::string cn_native=unicode::iconvert::convert(
		cn,
		unicode::utf_8,
		sqwebmail_content_charset
	);

	fprintf(si->fpw, "<tr valign=\"top\"><td><input type=\"checkbox\" "
				"name=\"%s\" value=\"&lt;",
		strcat(strcpy(numbuf2, "ADDY"),
		       libmail_str_size_t(si->counter++, numbuf)));

	output_attrencoded_fp(mail, si->fpw);
	fprintf(si->fpw, "&gt;");

	output_attrencoded_fp(cn_native.c_str(), si->fpw);

	fprintf(si->fpw, "&quot;\" /></td><td><font size=\"+1\" class=\"ldapsearch-name\">\"");

	output_attrencoded_fp(cn_native.c_str(), si->fpw);
	fprintf(si->fpw,
		"\"</font><font size=\"+1\" class=\"ldapsearch-addr\">&lt;");
	output_attrencoded_fp(mail, si->fpw);
	fprintf(si->fpw, "&gt;</font>");
	fprintf(si->fpw, "</td></tr>\n");
	return 0;
}

void	doldapsearch()
{
std::string f;
struct ldapabook *abooks[2];
const struct ldapabook *ptr;

	abooks[0]=ldapabook_read(LDAPADDRESSBOOK);
	abooks[1]=ldapabook_read(LOCALABOOK);

	ptr=ldapabook_find(abooks[0], cgi("ABOOK"));
	if (!ptr)
		ptr=ldapabook_find(abooks[1], cgi("ABOOK"));

	if (ptr && !(f=getfilter()).empty())
	{
		struct search_info si;
		maildir::tmpcreate_info createInfo;

		pref_setldap(ptr->name);
		printf("<pre>");
		fflush(stdout);


		si.fpw=NULL;
		si.errmsgbuf[0]=0;
		si.counter=0;

		createInfo.uniq="ldap";
		createInfo.doordie=true;

		si.fpw=createInfo.fp();

		if (ldapabook_search(ptr, LDAPSEARCH, f.c_str(), parsesearch,
				     save_errmsg, &si) == 0)
		{
			int	c;

			printf("</pre>");

			if (si.counter == 0)
				printf("%s", getarg("NOTFOUND"));

			printf("<table border=\"0\" cellpadding=\"4\">\n");

			fflush(si.fpw);
			rewind(si.fpw);
			while ((c=getc(si.fpw)) != EOF)
				putchar(c);

			printf("<tr><td colspan=\"2\"><hr width=\"90%%\" />"
			       "<input type=\"hidden\" name=\"ADDYCNT\" value=\"%u\" />\n"
			       "</td></tr>\n", si.counter);
			printf("<tr><td colspan=\"2\"><table>");

			printf("<tr><td align=\"right\">%s</td><td>"
			       "<select name=\"nick1\"><option value=\"\"></option>\n", getarg("ADD"));
			ab_listselect_fp(stdout);
			printf("</select></td></tr>\n");
			printf("<tr><td align=\"right\">%s</td><td>"
			       "<input type=\"text\" name=\"nick2\" /></td></tr>\n", getarg("CREATE"));
			printf("<tr><td></td><td>"
			       "<input type=\"submit\" name=\"import\" value=\"%s\" /></td></tr>",
			       getarg("SUBMIT"));
			printf("</table></td></tr></table>\n");
		}
		else if (si.errmsgbuf[0])
		{
			output_attrencoded_oknl(si.errmsgbuf);
			printf("</pre>");

		}
		fclose(si.fpw);

		unlink(createInfo.tmpname.c_str());
	}
}
