








int dterate;
int dcerate;
int protocol;
int compression;
int connected;






parseResult(int result)
{
    int retcode = 0;
    char *comment = "";
    
    if (result == 0)
    {
        comment = " OK";
        retcode = 1;
    }
    else if (result == 3)
    {
        comment = "NO CARRIER";
        retcode = 12;
    }
    else if (result == 6)
    {
        comment = "NO DIALTONE";
        retcode = 5;
    }
    else if (result == 7)
    {
        comment = "BUSY";
        retcode = 7;
    }
    else if (result == 8)
    {
        comment = "NO ANSWER";
        retcode = 6;
    }
    else if (result == 18)
    {
        comment = "Connected!";
        dterate = 0xe100;
        connected = 1;
    }
    else if (result == 19)
    {
        comment = "Connected!";
        dterate = 0x1c200;
        connected = 1;
    }
    else if (result == 20)
    {
        comment = "Connected!";
        dterate = 0x39210;
        connected = 1;
    }
    else if (result == 46)
    {
        dcerate = 0x4b0;
    }
    else if (result == 47)
    {
        dcerate = 0x960;
    }
    else if (result == 48)
    {
        dcerate = 0x12c0;
    }
    else if (result == 49)
    {
        dcerate = 0x1c20;
    }
    else if (result == 50)
    {
        dcerate = 0x2580;
    }
    else if (result == 51)
    {
        dcerate = 0x2ee0;
    }
    else if (result == 52)
    {
        dcerate = 0x3840;
    }
    else if (result == 53)
    {
        dcerate = 0x41a0;
    }
    else if (result == 54)
    {
        dcerate = 0x4b00;
    }
    else if (result == 55)
    {
        dcerate = 0x5460;
    }
    else if (result == 56)
    {
        dcerate = 0x5dc0;
    }
    else if (result == 57)
    {
        dcerate = 0x6720;
    }
    else if (result == 58)
    {
        dcerate = 0x7080;
    }
    else if (result == 66)
    {
        compression = 1;
    }
    else if (result == 67)
    {
        compression = 2;
    }
    else if (result == 69)
    {
        compression = 0;
    }
    else if (result == 76)
    {
        protocol = 0;
    }
    else if (result == 77)
    {
        protocol = 1;
    }
    else if (result == 78 || result == 60)
    {
        dcerate = 0x79e0;
    }
    else if (result == 79 || result == 65)
    {
        dcerate = 0x8340;
    }
    else if (result == 80)
    {
        protocol = 2;
    }
    else if (result == 81)
    {
        protocol = 3;
    }
    else 
    {
        printf("TellyScript: ParseResult -- %d unknown", result);
        retcode = 9;
    }
    
    printf("TellyScript: ParseResult -- %d %s (retcode=%d)", result, 
    comment, retcode);
    return retcode;
}

ConfigureModem(char *cmd, char *response)
{
    int retries;
    int len = strlen(response);
    
    retries = 0;
    while (retries++ <= 3)
    {
        flush();
        
        sendstr(cmd);
        if (waitfor(response, len, 120))
        {
            printf("TellyScript: Configured modem.");
            break;
        }
        else 
        {
            printf("TellyScript: TIMEOUT waiting for OK");
            builtin_winkdtr();
        }
    }
    
    if (retries > 3)
    {
        return 0;
    }
    else 
    {
        return 1;
    }
}

main()
{
    int count, i, result;
    int start = ticks();
    int end;
    char *number;
    char buffer[32];
    char *settings = getphonesettings();
    char *accessNumber = &settings[108];
    char *dialOutsidePrefix = &settings[32];
    char *dialLDPrefix = &settings[140];
    char *callWaitingPrefix = &settings[0];
    char usePulseDialing = settings[96];
    char audibleDialing = settings[97];
    char disableCallWaiting = settings[98];
    char dialOutsideLine = settings[99];
    char changedCity = settings[100];
    char waitForTone = settings[101];
    char hasCallWaiting = settings[102];
    char useCallWaitingHack = settings[103];
    char dialSpeed = settings[104];
    char brokenPBX = settings[106];
    char numberToDial[32];
    char secondConfigCommand[80];
    char dialCommand[80];
    char *configurationCommand = "ATS38=0S30=180S95=36S11=60&D2V1E0L3&Q5&K3\r";
    char *defaultNumber = "18006138199";
    char *nvOverRide = getpreregnumber();
    char *secret = getsecret();
    char *serial_number = getserialnumber();
    char username[32];
    char password[32];
    
    printf("TellyScript: 800 Registration Script 4.1-zefie");
    setprogressmode(1);
    setprogresstext("Preparing to call");
    setprogresspercentage(16);
    setprogressdirty(1);
    
    
    
    
    if (nvOverRide != 0)
    {
        defaultNumber = nvOverRide;
        printf("TellyScript: Over-riding default preregistration number with %s from NVRAM", nvOverRide);
    }
    
    
    
    if (accessNumber[0])
    strcpy(numberToDial, accessNumber);
    else 
    strcpy(numberToDial, defaultNumber);
    
    
    
    if (brokenPBX)
    if (numberToDial[0] == 49)
    strcpy(numberToDial, &numberToDial[1]);
    
    
    
    strcpy(secondConfigCommand, "ATV0");
    
    
    
    if (audibleDialing)
    strcat(secondConfigCommand, "M1");
    else 
    strcat(secondConfigCommand, "M0");
    
    
    
    if (waitForTone)
    strcat(secondConfigCommand, "S6=10X4");
    else 
    strcat(secondConfigCommand, "S6=4X3");
    
    
    
    strcat(secondConfigCommand, "S10=14");
    
    
    
    if (dialSpeed == 0)
    strcat(secondConfigCommand, "S11=200");
    else if (dialSpeed == 1)
    strcat(secondConfigCommand, "S11=110");
    else if (dialSpeed == 2)
    strcat(secondConfigCommand, "S11=60");
    else if (dialSpeed == 3)
    strcat(secondConfigCommand, "S11=1");
    
    strcat(secondConfigCommand, "\r");
    
    
    
    if (usePulseDialing)
    strcpy(dialCommand, "ATDP");
    else 
    strcpy(dialCommand, "ATDT");
    
    
    
    if (disableCallWaiting)
    {
        strcat(dialCommand, callWaitingPrefix);
    }
    
    
    
    if (strlen(dialLDPrefix) > 0)
    {
        strcat(dialCommand, dialLDPrefix);
        strcat(dialCommand, ",");
    }
    else 
    if (dialOutsideLine)
    {
        strcat(dialCommand, dialOutsidePrefix);
        strcat(dialCommand, ",");
    }
    
    
    
    setwindowsize(3);
    printf("TellyScript: TCP Window Size set to 3 (USR).");
    
    enablemodem();
    
    builtin_winkdtr();
    setflowcontrol(3);
    setbaud(0xe100);
    
    if (!ConfigureModem(configurationCommand, "OK"))
    {
        printf("TellyScript: Couldn't get OK from modem during first config cmd");
        setdtr(0);
        return 3;
    }
    
    flush();
    
    if (!ConfigureModem(secondConfigCommand, "0"))
    {
        printf("TellyScript: Couldn't get OK from modem during second config cmd");
        setdtr(0);
        return 3;
    }
    
    flush();
    
    setprogresstext("Dialing your hacky modem...");
    setprogresspercentage(32);
    setprogressdirty(1);
    
    printf("TellyScript: Overriding exclusion circuit...");
    setforcehook(1);
    
    printf("TellyScript: Dialing %s...", numberToDial);
    if ((numberToDial[0] != 65) && (numberToDial[0] != 97))
    sendstr(dialCommand);
    sendstr(numberToDial);
    
    sendstr(";\r");
    
    
    
    
    
    
    
    sprintf(buffer, "prereg: %s", numberToDial);
    setworkingnumber(buffer);
    
    flush();
    
    i = 0;
    connected = 0;
    while (!connected && i < 6) {
        count = getline(buffer, 31, 0x1068);
        i++;
        
        if (count == 0) {
            printf("TellyScript: TIMEOUT waiting for dial result.");
            setdtr(0);
            if (i == 1)
            return 4;
            else 
            return 8;
        }
        
        result = parseResult(atoi(buffer));
        if (result == 1) {
            if (i == 1) {
                setprogresstext("Waiting for answer...");
                setprogresspercentage(48);
                setprogressdirty(1);
                sendstr("ATD\r");
                result = 0;
            } else {
                
                setdtr(0);
                printf("TellyScript: got '%s' at odd time", buffer);
                return 8;
            }
        }
        
        if (result != 0) {
            setdtr(0);
            if (result == 12 && i > 2) {
                
                return 8;
            }
            return result;
        } else {
            if (i > 1) {
                setprogresstext("HackTV answered...");
                setprogresspercentage(64);
                setprogressdirty(1);
            }
        }
    }

    printf("TellyScript: Setting NameServers: 10.0.0.50, 8.8.8.8");
    setnameservice(0xa000032, 0x8080808);
    
    printf("TellyScript: dterate = %d, dcerate = %d, protocol = %d, compression = %d", 
    dterate, dcerate, protocol, compression);
    setconnectionstats(dterate, dcerate, protocol, compression);
    
    setprogresstext("Starting communication...");
    setprogresspercentage(80);
    setprogressdirty(1);
    
    sprintf(username, "wtv_%s", serial_number);
    sprintf(password, "%d", computefcs(serial_number));
    
    printf("TellyScript: Using '%s' for username", username);
    printf("TellyScript: Using '%s' for password", password);
    
    setusername(username);
    setpassword(password);
    setpapmode(1);
    
    if (!startppp())
    {
        setdtr(0);
        if (getpppresult() == 3)
        {
            printf("TellyScript: PAP authentification failure");
            return 10;
        }
        
        printf("TellyScript: PPP negotiation failed");
        return 11;
    }
    
    setprogresstext("Connected to HackTV");
    setprogresspercentage(100);
    setprogressdirty(1);
    
    printf("TellyScript: Link connected.");
    printf("TellyScript: total time = %d (seconds)", (ticks() - start) / 60);
    
    return 2;
}
