nmps

simple network-based command-line rpg wrote in c99
git clone git://git.kocotian.pl/nmps.git
Log | Files | Refs | LICENSE

commit 230a38043e2783bdfa7fd5dfa8d8a89e3a764f66
parent bc23b3e1fdd312f1cc82a3edb4d38ea8210ac39f
Author: kocotian <kocotian@kocotian.pl>
Date:   Wed, 11 Nov 2020 18:52:14 +0100

request sending, changed argv in request(), customizable PS1

Diffstat:
Aconfig.h | 1+
Mnmps | 0
Mnmps.c | 108++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
3 files changed, 80 insertions(+), 29 deletions(-)

diff --git a/config.h b/config.h @@ -0,0 +1 @@ +static const char *PS1 = "\033[0;37m%s@%s \033[0m$ "; diff --git a/nmps b/nmps Binary files differ. diff --git a/nmps.c b/nmps.c @@ -11,23 +11,30 @@ #include "http.h" #include "util.c" -#define VERSION "a0.1.4.1" +#define VERSION "a0.2" extern void herror(const char *s); static size_t authorize(char *host, const char *port, char *username, char *password); -static size_t request(char *hostname, unsigned short port, char *command, char *args[], char **buffer); +static void mkconnect(char *username, char *host, char *port); +static size_t request(char *hostname, unsigned short port, char *command, char *args, char **buffer); +static int shell(char *username, char *host, char *port); static void usage(void); static char *authtoken = NULL; char *argv0; +#include "config.h" + static size_t authorize(char *host, const char *port, char *username, char *password) { size_t size; - char *buffer, *args[] = {username, password, NULL}, + char *buffer, *args, *duplicate, *tokenized; + args = calloc(strlen(username) + strlen(password) + 2, 1); + strcat(args, username); strcat(args, "\1"); + strcat(args, password); strcat(args, "\0"); if (!(size = request(host, atoi(port), "auth", args, &buffer))) return 1; else { @@ -42,24 +49,80 @@ authorize(char *host, const char *port, char *username, char *password) return 0; } +static void +mkconnect(char *username, char *host, char *port) +{ + int character, chiter = -1; + char password[64] = ""; + + fprintf(stderr, "Connecting to %s ", host); + if (gethostbyname(host) == NULL) { + herror("failed"); + exit(1); + } + + fprintf(stderr, "succeeded!\n"); + fprintf(stderr, "%s@%s's password: \033[8m", username, host); + while ((character = fgetc(stdin)) != '\n') { + password[++chiter] = character; + } + password[++chiter] = 0; + fprintf(stderr, "\033[0m"); + + if(authorize(host, port, username, password)) + printf("Authorization successful!\nAuth token: %s\n", authtoken); +} + static size_t request(char *hostname, unsigned short port, - char *command, char *args[], char **buffer) + char *command, char *args, char **buffer) { - char *argv = calloc(0, 1); char *request_template = "GET /%s HTTP/1.0\r\nargv: %s\r\nAuth-Token: %s\r\nHost: %s\r\n\r\n"; char request[BUFSIZ]; - int iter = -1, argvsize = 0, request_length; - while (args[++iter] != NULL) { - argv = realloc(argv, argvsize += strlen(args[iter]) + 1); - strcat(argv, args[iter]); - strcat(argv, "\1"); - } + size_t request_length; request_length = snprintf(request, BUFSIZ, request_template, - command, argv, authtoken, hostname); + command, args, authtoken, hostname); return sendHTTPRequest(hostname, port, request, request_length, buffer); } +static int +shell(char *username, char *host, char *port) +{ + size_t linesize = 256; + char *line, *linedup, *token, *command, + *args, *buffer, *truncbuf; + size_t argsize, reqsize; + + line = malloc(linesize); + + while (1) { + argsize = 0; + printf(PS1, + username, host); + getline(&line, &linesize, stdin); + if (line[strlen(line) - 1] == '\n') + line[strlen(line) - 1] = 0; + linedup = strdup(line); + command = strtok_r(linedup, " ", &linedup); + args = calloc(0, 1); + while ((token = strtok_r(linedup, " ", &linedup))) { + argsize += strlen(token) + 1; + args = realloc(args, argsize); + strcat(args, token); strcat(args, "\001"); + } + if (!(reqsize = request(host, atoi(port), command, args, &buffer))) + die("\033[1;31mSomething went wrong with your request!\033[0m"); + truncbuf = truncateHeader(buffer); + if (*truncbuf == 1) + ++truncbuf; /* there will be steering sequences, + reserved for simple comunication client <-> server */ + printf("%s%c", truncbuf, buffer[reqsize - 1] == '\n' ? '\0' : '\n'); + free(buffer); + } + free(line); + return 0; +} + static void usage(void) { @@ -69,8 +132,8 @@ usage(void) int main(int argc, char *argv[]) { - char *port = "80", *host = "localhost", username[32] = "unknown", password[64] = ""; - int character, chiter = -1; + char *port = "80", *host = "localhost", + username[32] = "unknown"; ARGBEGIN { case 'p': @@ -88,21 +151,8 @@ main(int argc, char *argv[]) getlogin_r(username, 32); host = argv[argc - 1]; - fprintf(stderr, "Connecting to %s ", host); - if (gethostbyname(host) == NULL) { - herror("failed"); - return -1; - } + mkconnect(username, host, port); + shell(username, host, port); - fprintf(stderr, "succeeded!\n"); - fprintf(stderr, "%s@%s's password: \033[8m", username, host); - while ((character = fgetc(stdin)) != '\n') { - password[++chiter] = character; - } - password[++chiter] = 0; - fprintf(stderr, "\033[0m"); - - if(authorize(host, port, username, password)) - printf("Authorization successful!\nAuth token: %s\n", authtoken); return 0; }