/*
**
** Copyright (C) 1994 Swedish University Network (SUNET)
**
**
** This program is developed by UDAC, Uppsala University by commission
** of the Swedish University Network (SUNET). 
**
** 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 2 of the License, 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 FITTNESS 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.
**
**
**                                        Martin.Wendel@udac.uu.se
**                                        Torbjorn.Wictorin@udac.uu.se
**
**                                        UDAC	
**                                        P.O. Box 174
**                                        S-751 04 Uppsala
**                                        Sweden
**
*/


#include <time.h>
#include "emil.h"

char *getmimebound();

void
encode_mime(struct message *m)
{
  char buf[HDRLEN];
  char *ct;

  if (m->sd->format == MIME && m->sd == m->td)
    return;
  if (m->level == 0)
    {
      add_header(m, "MIME-Version", "1.0", MIME);
    }
  if ((ct = (char *)confextr("MIME", NULL, m->type)) != NULL ||
      (ct = (char *)confextr("MIME", NULL, "DEFAULT")) != NULL) 
    {
      if (match(m->type, "TEXT"))
	{
	  if (m->td->charset != NULL)
	    sprintf(buf, "%s; charset=\"%s\"", ct, m->td->charset);
	  else
	    sprintf(buf, "%s", ct);
	}
      else
	if (match(m->type, "MULTIPART"))
	  {
	    char *bb;
	    bb = (char *)getmimebound();
	    sprintf(buf,"%s; boundary=\"%s\"", ct, bb);
	    m->td->startbound = (char *)Yalloc(MIMEBOUNDLEN + 5);
	    m->td->endbound = (char *)Yalloc(MIMEBOUNDLEN + 7);
	    sprintf(m->td->startbound, "--%s", bb);
	    sprintf(m->td->endbound, "--%s--", bb);
	  }
      else
	{
	  sprintf(buf, "%s", ct);
	}
    }
  else
    /* Generic default */
    sprintf(buf, "Application/Octet-Stream");

  if (m->td->encoding == EBINHEX)
    sprintf(buf, "application/mac-binhex40");
  
  if (m->name != NULL)
    {
      sprintf(buf, "%s; name=\"%s\"", buf, m->name);
    }
  add_header(m, "Content-Type", buf, MIME);
  switch(m->td->encoding)
    {
    case EQP:
      add_header(m, "Content-Transfer-Encoding", "Quoted-Printable", MIME);
      break;
    case EBASE64:
      add_header(m, "Content-Transfer-Encoding", "Base64", MIME);
      break;
    case E8BIT:
      add_header(m, "Content-Transfer-Encoding", "8bit", MIME);
      break;
    case EUUENCODE:
      add_header(m, "Content-Transfer-Encoding", "X-UUENCODE", MIME);
      break;
    case E7BIT:
    case EBINHEX:
      add_header(m, "Content-Transfer-Encoding", "7bit", MIME);
      break;
    default:
      break;
    }
}

int
decode_mime(struct message *m)
{

  if (m->level == 0)
    {
      if (matchheader(m, "MIME-Version", "1.0", MIME))
	{
	  m->sd->format = MIME;
	}
      else
	return(NOK);
    }

  if (m->sd->format == MIME)
    {
      char *line = NULL;
      
      /* Process content-type header line */
      if((line = (char *)gethval(m, "Content-Type", MIME)) == NULL)
	{
	      /* Use default if missing */
	  m->type = NEWSTR("TEXT");
	  m->sd->charset = NEWSTR("US-ASCII");
	  m->sd->encoding = E7BIT;
	}
      else
	{
	  /* Set type */
	  if ((m->type = (char *)
	       confextr("MIME", stringpart(line, ";", 0), NULL)) == NULL)
	    if ((m->type =  (char *)
		 confextr("MIME", "DEFAULT", NULL))
		== NULL)
	      m->type = NEWSTR("APPLICATION");
	  
	  /* Get parameters for a few important types */
	  if (match(m->type, "TEXT"))
	    {
	      m->sd->encoding = E7BIT;
	      /* Get m->charset for text */
	      m->sd->charset = (char *)fixstring(getpartrest(line, "charset=", ";"), NULL, NULL,
				  (short) UNQUOTE);
	    }
	  else
	  if (match(m->type, "MULTIPART"))
	    {
	      /* Get boundaries for multipart */
	       m->sd->startbound = (char *)fixstring(getpartrest(line, "boundary=", ";"),
				      "--", NULL, (short) UNQUOTE);
	       m->sd->endbound = (char *)fixstring(getpartrest(line, "boundary=", ";"),
				      "--", "--", (short) UNQUOTE);
	       m->sd->encoding = EMULTI;
	       m->sd->check = EMULTI;
	    }	    
	  m->name = (char *)fixstring(getpartrest(line, "name=", ";"),
				      NULL, NULL, (short) UNQUOTE);
	}

      /* Process content-transfer-m->encoding header line */
      if (m->sd->encoding != EMULTI)
	{
	  if ((line = 
	       (char *)gethval(m, "Content-Transfer-encoding", MIME)) == NULL)
	    {
	      /* Use default if missing */
	      if (m->sd->encoding == 0)
		m->sd->encoding = E7BIT;
	    }
	  else
	    {
	      if (cmatch(line, "7bit"))
		m->sd->encoding = E7BIT;
	      else
		if (cmatch(line, "quoted-printable"))
		  {
		    m->sd->encoding = EQP;
		  }
		else
		  if (cmatch(line, "base64"))
		    {
		      m->sd->encoding = EBASE64;
		    }
		  else
		    if (cmatch(line, "8bit"))
		      m->sd->encoding = E8BIT;
		    else
		      if (cmatch(line, "binary"))
			m->sd->encoding = EBINARY;
		      else
			if (cmatch(line, "x-uuencode"))
			  {
			    m->sd->encoding = EUUENCODE;
			  }
			else
			  if (cmatch(line, "x-binhex"))
			    {
			      m->sd->encoding = EBINHEX;
			    }
	    }
	}
      m->description = (char *)gethval(m, "Content-description", MIME);
      line = (char *)gethval(m, "Content-Disposition", MIME);

    }
  else
    return(NOK);
  return OK;
}


char *
getmimebound()
{
	char	* bound;
	static	long	t;
	int	i;

	bound	= Yalloc(MIMEBOUNDLEN + 1);

	if (t==0) { time(&t); srand((unsigned int) t); }
	for (i=0; i < MIMEBOUNDLEN; i++)
	   bound[i]=
           "ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz"[rand()%50];
	return bound;
}
