/* remote.cpp - main, convert */

#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "dev.h"
#include "sockio.h"

#define LINELEN     255
#define BUFSIZE     1023

#define PROMPT "% "

int convert(char *cmd, char *buf, char *pwd);

/*------------------------------------------------------------------------
 *
 *  sReadL  -  read until len chars or  \n is received
 *
 *------------------------------------------------------------------------
 */

int sReadL(int s, char *buf, int len)
{
    int  n, i;
       
    for (i=0; i < len; i++ ) {
        n = sRead(s, &buf[i], 1);
        if (n <= 0) {
            return n;
        }
        if (buf[i] == '\n') {
            return i+1;
        }
    }
    return len;
}
/*----------------------------------------------------------------
 *
 * main - NAT32 remote access program
 *
 *----------------------------------------------------------------
 */
int main(int argc, char **argv)
{
    char    cmd[LINELEN+1];
    char    buf[BUFSIZE+1];
    char    password[256] = "";
    int     s, len;
    char    server[128];
    char    port[128];
    char    *ptr;
    
    if (argc > 4) {
        printf("Usage: remote [server] [port] [password]\n");
        return -1;
    }
    
    if (argc > 1)
        strcpy(server, argv[1]);
    else
        strcpy(server, "localhost");
    
    if (argc >= 3)
        strcpy(port, argv[2]);
    else
        strcpy(port,"8080");

    if (argc == 4)
        strcpy(password, argv[3]);

    netinit(0, 1);              // initialise WinSock

    printf("Remote Shell Interface Version 1.1\n");

    while (1) 
	{
        printf("%s", PROMPT);

        // read a command line

        fgets(cmd, LINELEN, stdin);

        // strip the \n

        len = strlen(cmd);
        cmd[len-1] = '\0';
        
        // exit command entered?

        if (!strcmp(cmd, "exit"))
            break;
    
        // converts the command line to the form:
        // "GET /shell?cmd=abc+def+...+password /HTTP1.0\r\n"
        
        len = convert(buf, cmd, password);

        // connect to the server

        s = connectTCP(server, port);

        if (s < 0) {
            netinit(0,0);
            printf("ERROR: can't connect to %s:%s\n", server, port);
            return -1;
        }

        // Send the command

        sWrite(s, buf, len);        // buf holds the converted cmd 

        sWrite(s, "\r\n", 2);
        
        // read response

        while (1) {
            len = sReadL(s, buf, BUFSIZE);
            if (len == 2)
                break;
            buf[len] = 0;
        }

        while (1) {
            len = sReadL(s, buf, BUFSIZE);
            if (len <= 0)
                break;
            else {
                buf[len] = 0;
                
                // Strip html
                
                ptr = strstr(buf, "<html><pre>");
                if (ptr) {
                    fwrite(ptr+11, 1, len-11, stdout);
                    continue;
                }
                ptr = strstr(buf, "</pre></html>");
                if (ptr) {
                    fwrite(buf, 1, len-13, stdout);
                    continue;
                }
                fwrite(buf, 1, len, stdout);
            }
        }
        sClose(s);
    }

    netinit(0, 0);              // uninitialise WinSock

    return 0;
}


/*----------------------------------------------------------------
 *
 *  convert - converts cmd to HTML GET command format in buf
 *
 *----------------------------------------------------------------
 */

int convert(char *buf, char *cmd, char *pwd)
{
    int flag = 0;
    char *token;

    sprintf(buf, "GET /shell?cmd=");
    
    token = strtok(cmd, " ");
	
    while (token) {
        if (flag)
            strcat(buf, "+");
        else
            flag = 1;

        strcat(buf, token);
        token = strtok(NULL, " ");
	}

    if (*pwd) {
        strcat(buf, "+");
        strcat(buf, pwd);
    }
        
    strcat(buf," HTTP/1.0\r\n");
    return strlen(buf);
}

