143 lines
3.1 KiB
C
143 lines
3.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <tls.h>
|
|
#include <ncurses.h>
|
|
|
|
typedef struct ll {
|
|
void *dat;
|
|
struct ll *next;
|
|
struct ll *prev;
|
|
} ll;
|
|
|
|
char *tls_incremental_line_read(struct tls *context) {
|
|
char buffer;
|
|
int bufsize = 40;
|
|
int current = 0;
|
|
int read;
|
|
char *initial = malloc(bufsize);
|
|
for(;;) {
|
|
read = tls_read(context, &buffer, 1); /* if malloc err check this */
|
|
if(!read) {
|
|
free(initial);
|
|
return NULL;
|
|
}
|
|
switch(buffer) {
|
|
case '\n':
|
|
case '\r':
|
|
case '\0':
|
|
initial[current] = '\0';
|
|
return initial;
|
|
}
|
|
initial[current++] = buffer;
|
|
if(current == bufsize - 2) {
|
|
initial = realloc(initial, bufsize *= 2);
|
|
}
|
|
}
|
|
}
|
|
|
|
int getHeaderData(struct tls *context, int *i, char **s) {
|
|
char *buf = tls_incremental_line_read(context);
|
|
char *token = strtok(buf, " ");
|
|
*i = atoi(token);
|
|
token = strtok(NULL, " ");
|
|
if(!token) return 0;
|
|
*s = strdup(token);
|
|
free(buf);
|
|
return 1;
|
|
}
|
|
|
|
int main(void) {
|
|
initscr();
|
|
keypad(stdscr, TRUE);
|
|
nonl();
|
|
cbreak();
|
|
noecho();
|
|
|
|
if(has_colors()) {
|
|
init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
|
|
init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
|
|
init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
|
|
init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
|
|
init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
|
|
init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
|
|
init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
|
|
init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
|
|
}
|
|
|
|
int y = 1;
|
|
int links = 0;
|
|
char towrite[] = "gemini://tilde.club/\r\n";
|
|
struct tls_config *c = tls_config_new();
|
|
tls_config_insecure_noverifycert(c);
|
|
tls_config_insecure_noverifyname(c);
|
|
|
|
struct tls *client = tls_client();
|
|
tls_configure(client, c);
|
|
|
|
tls_connect(client, "tilde.club", "1965");
|
|
|
|
tls_write(client, &towrite, sizeof towrite);
|
|
|
|
int statuscode;
|
|
char *mime;
|
|
|
|
int status = getHeaderData(client, &statuscode, &mime);
|
|
|
|
printf("%i %i %s -- custom status display\n", status, statuscode, mime);
|
|
|
|
int my, mx;
|
|
getmaxyx(stdscr, my, mx);
|
|
int counter = 1;
|
|
|
|
ll *cll = NULL, *ptr = cll;
|
|
|
|
for(;;) {
|
|
char *out = tls_incremental_line_read(client);
|
|
int disable = FALSE;
|
|
if(!out) break;
|
|
if(counter == my) disable = TRUE;
|
|
if(!cll) {
|
|
cll = malloc(sizeof *cll);
|
|
cll->prev = NULL;
|
|
ptr = cll;
|
|
} else {
|
|
cll->next = malloc(sizeof *cll);
|
|
cll->next->prev = cll;
|
|
cll = cll->next;
|
|
}
|
|
|
|
cll->dat = out;
|
|
cll->next = NULL;
|
|
|
|
if(!disable) mvprintw(y++, 0, "%s", out);
|
|
if(out[0] == '=' && out[1] == '>') {
|
|
if(!disable) printw(" [%i]", ++links);
|
|
}
|
|
counter++;
|
|
}
|
|
|
|
ll *scroll = ptr;
|
|
tls_close(client);
|
|
tls_free(client);
|
|
for(;;) {
|
|
char c = getch();
|
|
erase();
|
|
|
|
int ycounter = 1;
|
|
|
|
if(c == 'k') scroll = scroll->prev;
|
|
else scroll = scroll->next;
|
|
ll *drawingPointer = scroll;
|
|
while(drawingPointer) {
|
|
mvprintw(ycounter++, 0, "%s", drawingPointer->dat);
|
|
drawingPointer = drawingPointer->next;
|
|
if(ycounter == my) break;
|
|
}
|
|
}
|
|
endwin();
|
|
|
|
return 0;
|
|
}
|