/*:ts=8*/
/*****************************************************************************
 * FIDOGATE --- Gateway UNIX Mail/News <-> FIDO NetMail/EchoMail
 *
 * $Id: rfcaddr.c,v 3.9.2.0 1995/06/12 17:14:03 mj Exp $
 *
 * RFCAddr struct handling
 *
 *****************************************************************************
 * Copyright (C) 1990-1995
 *  _____ _____
 * |     |___  |   Martin Junius             FIDO:      2:2452/110.1
 * | | | |   | |   Republikplatz 3           Internet:  mj@sungate.fido.de
 * |_|_|_|@home|   D-52072 Aachen, Germany   Phone:     ++49-241-86931 (voice)
 *
 * This file is part of FIDOGATE.
 *
 * FIDOGATE 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, or (at your option) any
 * later version.
 *
 * FIDOGATE 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 FIDOGATE; see the file COPYING.  If not, write to the Free
 * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *****************************************************************************/

#include "fidogate.h"

#include "shuffle.h"



/*
 * DotNames option from CONFIG file: User.Name instead of User_Name
 */
static int dot_names = FALSE;


void rfcaddr_dot_names(f)
    int f;
{
    dot_names = f;
}



/*
 * rfcaddr_from_ftn() --- Generate RFCAddr struct from FTN name and node
 */
#define NOT_ALLOWED_ATOMS	"()<>@,;::\\\"[]"
#define NOT_ALLOWED_QTEXT	"\"\\"
/*#define NOT_ALLOWED_FULLNAME	"()<>"*/
#define NOT_ALLOWED_FULLNAME	"<>"

RFCAddr rfcaddr_from_ftn(name, node)
    char *name;
    Node *node;
{
    RFCAddr rfc;
    char buf[MAXUSERNAME];
    int i, c;
    char *p;
    int must_quote;

    rfc.flags = 0;
    
    /*
     * Internet address part
     */
#ifdef LOCAL_FTN_ADDRESSES
    if( ! hosts_lookup(node, NULL) )
	strncpy0(rfc.addr, cf_fqdn(), sizeof(rfc.addr));
    else
#endif
	strncpy0(rfc.addr, ftn_to_inet(node, FALSE), sizeof(rfc.addr));

    /*
     * Translate special chars >= 0x80 and removed ctrl chars
     */
    for(i=0; *name && i<MAXUSERNAME-1; )
    {
	c = (unsigned char)(*name++);
	if(c >= 0x80)			/* Must translate */
	{
	    p = charset_xlate(c);
	    if(p)
		while(*p && i<MAXUSERNAME-1)
		    buf[i++] = *p++;
	}
	else if(c>=' ' && c<0x7f)
	    buf[i++] = c;
    }
    buf[i] = 0;

    /*
     * Check name for characters not allowed in RFC 822 atoms,
     * requiring that name must be quoted with "...". Copying name
     * to rfc.user[].
     */
    must_quote = FALSE;
    for(p=buf; *p; p++)
	if(strchr(NOT_ALLOWED_ATOMS, *p))
	    must_quote = TRUE;
    
    i = 0;
    if(must_quote)
	rfc.user[i++] = '\"';
    for(p=buf; i<MAXUSERNAME-2 && *p; p++)
	if(!strchr(NOT_ALLOWED_QTEXT, *p))
	{
	    rfc.user[i] = *p;
	    if(rfc.user[i] == ' ')
		rfc.user[i] = dot_names ? '.' : '_';
	    i++;
	}
    if(must_quote)
	rfc.user[i++] = '\"';
    rfc.user[i] = 0;

    /*
     * Check for MAUS address
     */
#ifdef MAUS_DE
    if( (p = strstr(rfc.user, MAUS_STRING)) )
    {
#ifdef MAUS_DE_FROM
	char *maus;
	Maus *m;
	
	maus = p + strlen(MAUS_STRING);
	m    = maus_lookup(maus);

	if(m && m->domain)
	{
	    *p = 0;

	    if(*m->domain == '.')
	    {
		strncpy0(rfc.addr, maus     , sizeof(rfc.addr));
		strncat0(rfc.addr, m->domain, sizeof(rfc.addr));
	    }
	    else
		strncpy0(rfc.addr, m->domain, sizeof(rfc.addr));
	}
#endif /**MAUS_DE_FROM**/
	rfc.flags |= ADDR_MAUS;
    }
#endif /**MAUS_DE**/

#ifdef LOCAL_FTN_ADDRESSES
    if( ! hosts_lookup(node, NULL) )
    {
	/* Add "%p.f.n.z" to user name */
	strncat0(rfc.user, "%"                      , sizeof(rfc.user));
	strncat0(rfc.user, node_to_pfnz(node, FALSE), sizeof(rfc.user));
    }
#endif
    
    /*
     * Copy name to real name field, removing addressing after `@' or `%'
     */
    i = 0;
    for(p=buf; i<MAXUSERNAME-1 && *p && *p!='@' && *p!='%'; p++)
	if(!strchr(NOT_ALLOWED_FULLNAME, *p))
	    rfc.real[i++] = *p;
    rfc.real[i] = 0;
    for(i=strlen(rfc.real)-1; i>=0; i++)	/* Remove trailing spaces */
	if(rfc.real[i] == ' ')
	    rfc.real[i] = 0;
	else
	    break;
    
    return rfc;
}



/*
 * rfcaddr_from_rfc() --- Generate RFCAddr struct from RFC address
 *
 * Supported formats:
 *     user@domain
 *     user@domain (Full Name)
 *     Full Name <user@domain>
 */
RFCAddr rfcaddr_from_rfc(addr)
    char *addr;
{
    RFCAddr rfc;
    char bufn[MAXUSERNAME];
    char bufa[MAXINETADDR];
    char *p, *s, *r;
    int i;

    rfc.flags = 0;
    
    /*
     * Full name <user@domain>
     */
    if( (p = strchr(addr, '<')) && (r = strrchr(p+1, '>')) )
    {
	/* Full name */
	for(s=addr; is_space(*s); s++) ;
	for(i=0; i<MAXUSERNAME-1 && *s && *s!='<'; i++, s++)
	    bufn[i] = *s;
	bufn[i] = 0;
	/* Address */
	for(i=0, s=p+1; i<MAXINETADDR-1 && *s && s<r; i++, s++)
	    bufa[i] = *s;
	bufa[i] = 0;
    }
    /*
     * user@domain (Full Name)
     */
    else if( (p = strchr(addr, '(')) && (r = strrchr(p+1, ')')) )
    {
	/* Full name */
	for(i=0, s=p+1; i<MAXUSERNAME-1 && *s && s<r; i++, s++)
	    bufn[i] = *s;
	bufn[i] = 0;
	/* Address */
	for(s=addr; is_space(*s); s++) ;
	for(i=0; i<MAXINETADDR-1 && *s && *s!='('; i++, s++)
	    bufa[i] = *s;
	bufa[i] = 0;
    }
    /*
     * user@domain
     */
    else 
    {
	/* No full name */
	bufn[0] = 0;
	/* Address */
	for(s=addr; is_space(*s); s++) ;
	for(i=0; i<MAXINETADDR-1 && *s; i++, s++)
	    bufa[i] = *s;
	bufa[i] = 0;
    }

    /*
     * Copy full name to RFCAddr struct, remove surrounding "..."
     * and leading/trailing spaces
     */
    p = bufn;
    if(*p == '\"')
	p++;
    while(is_space(*p))
	p++;
    i = strlen(p) -1;
    while(i>=0 && p[i]==' ')
	p[i--] = 0;
    if(i>=0 && p[i]=='\"')
	p[i--] = 0;
    while(i>=0 && p[i]==' ')
	p[i--] = 0;
    strncpy0(rfc.real, p, sizeof(rfc.real));

    /*
     * Removed leading/trailing spaces from address
     */
    for(p=bufa; is_space(*p); p++) ;
    for(i=strlen(p)-1; i>=0 && is_space(p[i]); p[i--]=0) ;
    
    /*
     * Address type  user@domain
     */
    if( (r = strrchr(p, '@')) )
    {
	/* User name */
	for(i=0, s=p; i<MAXUSERNAME-1 && *s && s<r; i++, s++)
	    rfc.user[i] = *s;
	rfc.user[i] = 0;
	/* Internet address */
	for(i=0, s=r+1; i<MAXINETADDR-1 && *s; i++, s++)
	    rfc.addr[i] = *s;
	rfc.addr[i] = 0;
    }
    /*
     * Address type  host!user
     */
    else if( (r = strchr(p, '!')) )
    {
	/* User name */
	for(i=0, s=r+1; i<MAXUSERNAME-1 && *s; i++, s++)
	    rfc.user[i] = *s;
	rfc.user[i] = 0;
	/* Internet address */
	for(i=0, s=p; i<MAXINETADDR-1 && *s && s<r; i++, s++)
	    rfc.addr[i] = *s;
	rfc.addr[i] = 0;
    }
    /*
     * Address type  user%domain
     */
    else if( (r = strrchr(p, '%')) )
    {
	/* User name */
	for(i=0, s=p; i<MAXUSERNAME-1 && *s && s<r; i++, s++)
	    rfc.user[i] = *s;
	rfc.user[i] = 0;
	/* Internet address */
	for(i=0, s=r+1; i<MAXINETADDR-1 && *s; i++, s++)
	    rfc.addr[i] = *s;
	rfc.addr[i] = 0;
    }
    /*
     * Adress type  user
     */
    else
    {
	/* User name */
	strncpy(rfc.user, p, sizeof(rfc.user));
	/* Internet address */
	rfc.addr[0] = 0;
    }
    
    
    return rfc;
}



/*
 * rfcaddr_to_asc() --- Text representation of RFCAddr
 */
char *rfcaddr_to_asc(rfc, real_flag)
    RFCAddr *rfc;
    int real_flag;			/* TRUE=with real name, FALSE=without*/
{
    SHUFFLEBUFFERS;
    
    if(real_flag && rfc->real[0])
	sprintf(tcharp, "%s%s%s (%s)",
		rfc->user, rfc->addr[0] ? "@" : "", rfc->addr, rfc->real );
    else
	sprintf(tcharp, "%s%s%s",
		rfc->user, rfc->addr[0] ? "@" : "", rfc->addr );

    return tcharp;
}



#ifdef TEST /****************************************************************/

int main(argc, argv)
    int argc;
    char *argv[];
{
#if 0
    Node node;
    char *name;
    RFCAddr rfc;

    cf_initialize();
    cf_read_config_file(NULL);
    hosts_init();
    maus_init();
    
    if(argc != 3)
    {
	fprintf(stderr, "usage: testrfc name Z:N/F.P\n");
	exit(1);
    }
    
    name = argv[1];
    if(asc_to_node(argv[2], &node, FALSE) == ERROR)
    {
	fprintf(stderr, "testrfc: invalid FTN address\n");
	exit(1);
    }
    
    rfc = rfcaddr_from_ftn(name, &node);
    
    printf("RFCAddr: %s@%s (%s)\n", rfc.user, rfc.addr, rfc.real);
#endif

#if 1
    char *addr;
    RFCAddr rfc;

    cf_initialize();
    cf_read_config_file(NULL);
    hosts_init();
    maus_init();
    
    if(argc != 2)
    {
	fprintf(stderr, "usage: testrfc addr");
	exit(1);
    }
    
    addr = argv[1];

    rfc = rfcaddr_from_rfc(addr);
    printf("RFCAddr: %s@%s (%s)\n", rfc.user, rfc.addr, rfc.real);
#endif
    
    exit(0);
}

#endif /**TEST***************************************************************/
