/*********************************************************************
 * f2i - version 0.2
 * Wed Jul 13 13:09:47 GMT+0100 1994
 * by liuk <liuk@linux.it>
 *
 * Convert a fidonet address into the corresponding internet format
 * Usage: f2i zzz:nnn/fff.ppp@abcdefnet.ftn
 * or
 * echo zzz:nnn/fff.ppp@foobarnet.ftn | f2i  (ie: input from stdin)
 *
 * The domain field may be omitted; f2i will try to guess the right
 * one from the zone number; if no domain is specified fidonet is default;
 * the ending .ftn or .org aren't processed (it's unuseful)
 * If the input address is correct f2i returns 0 and the converted
 * internet address on stdout
 * Returns a value between 1 and 9 otherwise
 * NOTE: 2:332/206.0 is considered as 2:332/206
 *       while 2:332/206.00 is considered incorrect
 *
 * Installation (as root):
 * gcc -O2 -o /usr/local/lib/ifmail/f2i f2i.c
 *
 *********************************************************************/
#include <stdio.h>
#include <ctype.h>

main(int argc,char* argv[])
    {
    int zone,node,fnode,pnt;
    int i,j,len,c;
    char* fad;
    char buf[32];
    char str[80];

    i=0;j=0;pnt=0;

    if(argc==2) fad=argv[1];
    else /* take input from stdin */
        {
        while(isgraph(c=getc(stdin)))
            str[j++]=c;
        str[j]='\0';
        if(strlen(str)==0) exit(1);
        fad=str;
        }

    len=strlen(fad);j=0;
    while(isdigit(fad[i])) buf[j++]=fad[i++];
    buf[j]='\0';
    /* in buf there is the Zone */
    if((zone=atoi(buf))==0)
        exit(2); /* Not good  */

    if(fad[i++]!=':') exit(3); /* Malformed */

    j=0;
    while(isdigit(fad[i])) buf[j++]=fad[i++];
    buf[j]='\0';
    /* in buf there is the Netregion */
    if((node=atoi(buf))==0) exit(4); /* Not good */

    if(fad[i++]!='/') exit(5); /* Malformed */
    j=0;
    while(isdigit(fad[i])) buf[j++]=fad[i++];
    buf[j]='\0';
    /* in buf there is the fnode  */
    if((fnode=atoi(buf))==0)
        exit(6);

    if(fad[i]=='.') /* Habemus point */
        {
        j=0;i++;
        while(isdigit(fad[i])) buf[j++]=fad[i++];
        buf[j]='\0';
        if(strcmp(buf,"0")==0) pnt=0;   /* .0 is correct  */
        else if((pnt=atoi(buf))==0) exit(7); /* syntax error */
        }

    if(i==len) /* there is no domain field */
        {
        /* try to guess the domain */
        switch(zone)
            {
            case 10:
                strcpy(buf,"qa.ftn");break;
            case 42:
                strcpy(buf,"guidenet.ftn");break;
            case 61:
                strcpy(buf,"peacelink.ftn");break;
            case 65:
                strcpy(buf,"cybernet.ftn");break;
            case 91:
                strcpy(buf,"pnet.ftn");break;
            default:
                strcpy(buf,"fidonet.org");break;
            }
        if(pnt) fprintf(stdout,"p%d.",pnt);
        fprintf(stdout,"f%d.n%d.z%d.%s\n",fnode,node,zone,buf);
        exit(0);
        }

    if(fad[i++]!='@')
        exit(8); /* Malformed address */

    j=0;
    while(isalpha(fad[i])) buf[j++]=fad[i++];
    buf[j]='\0';
    if(strlen(buf)==0) exit(9);
    if(pnt) fprintf(stdout,"p%d.",pnt);
    fprintf(stdout,"f%d.n%d.z%d.%s\n",fnode,node,zone,buf);
    exit(0);
    }
