/*
**  PRODIGY AutoLogon File Generator
**
**  Rich Taylor, 3/17/90
*/

/* includes */
#include    <stdlib.h>
#include    <stdio.h>
#include    <string.h>
#include    <ctype.h>
#include    <conio.h>

/* defines */
#define TRUE    1
#define FALSE   0

/* prototypes */
static void near pascal  write_keytrace_string( FILE *fp, char *str );
static void near pascal  write_keytrace_char( FILE *fp, char byCh );
static unsigned char near pascal check_prodigy_id( char * );
static void near pascal  get_string( char *, int );

int main (int argc, char *argv[]);

/* -------------------------------------------------------------------------- */
int main (int argc, char *argv[])
{
FILE    *fp;
int     i;
char    szFileName[_MAX_PATH];
char    szId[100];
char    szPass[100];
unsigned char   bIdFound, bPassFound;

    /* print banner */
    printf( "PRODIGY AutoLogon Generator, v1.00.00, 3/17/90\n" );
    printf( "Copyright (C) 1990, Rich Taylor (ID = PFTM42A)\n" );
    printf( "PRODIGY is a registered service mark and trademark of Prodigy Services Co.\n\n" );

    printf( "Use /? command line option for HELP.\n\n" );

    /* check for command line data */
       /* /?   -  display help */
       /* /o   -  alternate output filename */
       /* /i   -  ID string */
       /* /p   -  Password string */

    /* search for /? : print help */
    for( i=1; i<=argc; i++ )
        if( 0 == strcmp( "/?", argv[i]) )
            {
            printf( "This program will create a keystroke input file that performs an automatic\n" );
            printf( "logon to the PRODIGY service.  The data is written to a file named\n" );
            printf( "KEYTRACE.AUT (unless you override the filename).  If you do not specify\n" );
            printf( "your ID and password on the command line, the program will prompt you.\n\n" );

            printf( "KEYTRACE.AUT must be in your PRODIGY directory to work.\n\n" );

            printf( "Note - PRODIGY ID's are checked for valid form: AAAADDM, where\n" );
            printf( "       A = letter (A-Z), D = digit (0-9), and M = member letter (A-F)\n\n" );

            printf( "Command Line Arguments:\n" );
            printf( "  /o <filename>       to change output filename\n" );
            printf( "  /i <ID>             to indicate ID\n" );
            printf( "  /p <Password chars> to indicate password\n\n" );
            exit( 0 );
            }
    printf( "<CR> terminates program from input prompt.\n" );

    /* search for /o : new output filename */
    strcpy( szFileName, "KEYTRACE.AUT" );
    for( i=1; i<=argc; i++ )
        if( 0 == strcmp( "/o", argv[i]) || 0 == strcmp( "/O", argv[i]) )
            {
            if( i+1 > argc )
                {
                printf( "Missing output filename\n" );
                exit( 10 );
                }
            else
                strcpy( szFileName, argv[i+1] );
            }

    /* search for /i : ID string */
    bIdFound = FALSE;
    for( i=1; i<=argc; i++ )
        if( 0 == strcmp( "/i", argv[i]) || 0 == strcmp( "/I", argv[i]) )
            {
            if( i+1 > argc )
                {
                printf( "Missing ID.\n" );
                exit( 10 );
                }
            else
                {
                bIdFound = TRUE;
                strcpy( szId, argv[i+1] );
                }
            }

    /* search for /p : password string */
    bPassFound = FALSE;
    for( i=1; i<=argc; i++ )
        if( 0 == strcmp( "/p", argv[i]) || 0 == strcmp( "/P", argv[i]) )
            {
            if( i+1 > argc )
                {
                printf( "Missing Password.\n" );
                exit( 10 );
                }
            else
                {
                bPassFound = TRUE;
                strcpy( szPass, argv[i+1] );
                }
            }

    printf( "\n" );     /* insert space */

    /* Handle ID */
    if( !bIdFound )
        {  /* prompt for ID */
        while( !bIdFound )
            {
            printf( "Enter Prodigy ID: " );
            get_string( szId, 100 );

            if( 0 == strlen( szId ) )   /* No entry -> exit */
                return( 0 );

            bIdFound = check_prodigy_id( szId );
            if( !bIdFound )
                printf( "    <- Not valid PRODIGY ID\n" );
            else
                printf( "\n" );
            }   /* ID entry loop */
        }
    else
        /* check ID for correct PRODIGY format & change ID to uppercase */
        if( !check_prodigy_id( szId ) )
            {
            printf( "\"%s\" is an invalid PRODIGY ID - must follow AAAADDM format,\n", szId );
            printf( "    where A = letter (A-Z), D = digit (0-9), and M = member letter (A-F).\n" );
            exit( 10 );
            }

    /* prompt for Password */
    if( !bPassFound )
        {
        printf( "Enter password: " );
        get_string( szPass, 100 );
        printf( "\n" );

        if( 0 == strlen( szPass ) )  /* No entry -> exit */
            return( 0 );
        }

     /* Open output file */
    if( NULL == (fp = fopen( szFileName, "wb" )) )
        {
        printf( "Error opening file: [%s]\n", szFileName );
        exit( 10 );
        }

    /* write file */
    write_keytrace_string( fp, szId );
    write_keytrace_char( fp, '\x0D' );
    write_keytrace_string( fp, szPass );
    write_keytrace_char( fp, '\x0D' );

    fclose( fp );

    printf( "\nID = \"%s\", Password = \"%s\" written to %s\n", szId, szPass, szFileName );

    return( 0 );

} /* main() */

/* -------------------------------------------------------------------------- */
static void near pascal  write_keytrace_string( FILE *fp, char *str )
{
    while( *str )
        write_keytrace_char( fp, *str++ );

} /* write_keytrace_string() */

/* -------------------------------------------------------------------------- */
static void near pascal  write_keytrace_char( FILE *fp, char byCh )
{

    fprintf( fp, "%c", byCh );  /* write char */
    fprintf( fp, "%c", '\0' );  /* write zero/null */

} /* write_keytrace_char() */

/* -------------------------------------------------------------------------- */
static void near pascal get_string( str, maxlen )
char    *str;
int     maxlen;
{
int     pos = 0;
unsigned char    crh = FALSE;

#define CR  0x0D
#define BS  0x08

    while( !crh  &&  pos < maxlen-1 )
        {
        str[pos] = (char)getch();
        if( CR == str[pos] )
            {
            crh = TRUE;
            continue;
            }
        else if( BS == str[pos] )
            {
            if( 0 == pos )
                continue;
            putch( 0x08 );  /* Backup over existing char */
            putch( ' ' );   /* Erase char */
            putch( 0x08 );  /* Backup of space */
            pos --;
            continue;
            }

        putch( str[pos] );  /* echo */
        pos++;
        } /* character input loop */

    str[pos] = '\0';

} /* get_string() */

/* -------------------------------------------------------------------------- */
static unsigned char near pascal check_prodigy_id( szId )
char    *szId;
{
int     i, nlen;

    /* check for correct length */
    if( 7 != (nlen = strlen( szId )) )
        return( FALSE );

    /* first, convert string to uppercase */
    for( i=0; i<nlen; i++ )
        szId[i] = toupper( szId[i] );

    /* check that first 4 chars are alpha */
    for( i=0; i<4; i++ )
        if( !isalpha( szId[i] ) )
            return( FALSE );

    /* check that next 2 chars are alpha */
    for( i=4; i<6; i++ )
        if( !isdigit( szId[i] ) )
            return( FALSE );

    /* check that last character is in 'A' - 'F' */
    if( NULL == strpbrk( &(szId[6]), "ABCDEF" ) )
        return( FALSE );

    return( TRUE );

} /* check_prodigy_id() */
