2024-02-05 22:17:23 -05:00
|
|
|
/* moontalk.c - @BAKE cc -O2 -std=gnu99 -Wall -Wextra -Wpedantic -Wno-format-truncation $@ -o $* \
|
|
|
|
-D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600 -lncurses -ltinfo $+
|
|
|
|
* Written by Emil.
|
|
|
|
* Licensed under the GPLv3 only.
|
|
|
|
*
|
|
|
|
* TODO Add proper editing facilities
|
|
|
|
*/
|
2024-02-03 12:27:55 -05:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <time.h>
|
2024-02-05 22:17:23 -05:00
|
|
|
#include <errno.h>
|
2024-02-07 21:30:26 -05:00
|
|
|
#include <ctype.h>
|
2024-02-03 12:27:55 -05:00
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <ncurses.h>
|
|
|
|
|
|
|
|
#define SERV "7ks473deh6ggtwqsvbqdurepv5i6iblpbkx33b6cydon3ajph73sssad.onion"
|
|
|
|
#define PORT "50000"
|
|
|
|
|
2024-02-05 22:17:23 -05:00
|
|
|
#define TAB 9 /* */
|
2024-02-05 16:20:06 -05:00
|
|
|
#define BACKSPACE 263 /* ^G */
|
2024-02-05 22:17:23 -05:00
|
|
|
#define C_C 4 /* ... */
|
|
|
|
#define C_D 4 /* quit */
|
|
|
|
#define C_A 1 /* BOL */
|
|
|
|
#define C_B 2 /* BOL */
|
|
|
|
#define C_E 5 /* EOL */
|
|
|
|
#define C_H 8 /* BACKSPACE */
|
2024-02-07 21:30:26 -05:00
|
|
|
#define C_L 12 /* REFRESH */
|
|
|
|
#define C_U 21 /* CLR TO BOL */
|
2024-02-05 22:17:23 -05:00
|
|
|
#define C_W 23 /* DELETE PREVWORD */
|
|
|
|
#define C_L 12 /* Signal SIGWINCH */
|
|
|
|
#define C_R 18 /* hah not going to implement that */
|
|
|
|
|
2024-02-07 21:30:26 -05:00
|
|
|
#define C_T 20 /* scroll up */
|
|
|
|
#define C_N 14 /* scroll down */
|
|
|
|
|
2024-02-05 22:17:23 -05:00
|
|
|
#define UP 258
|
|
|
|
#define DOWN 259
|
|
|
|
#define LEFT 260
|
|
|
|
#define RIGHT 261
|
2024-02-05 16:20:06 -05:00
|
|
|
|
|
|
|
#define TIMESTR "<%Y/%m/%d %H:%M:%S "
|
|
|
|
#define TIMESTRMAX 21
|
|
|
|
|
|
|
|
#define SENDMAX (1 << 8) - 1
|
2024-02-05 22:17:23 -05:00
|
|
|
#define RECVMAX 17663 /* (1 << 11) */
|
2024-02-05 16:20:06 -05:00
|
|
|
|
2024-02-03 12:27:55 -05:00
|
|
|
#define streq(a,b) (!strcmp(a,b))
|
2024-02-05 16:20:06 -05:00
|
|
|
#define strneq(a,b,c) (!memcmp(a,b,c))
|
2024-02-03 12:27:55 -05:00
|
|
|
|
2024-02-05 16:20:06 -05:00
|
|
|
int g_y, g_x;
|
|
|
|
int g_sockfd = -1;
|
2024-02-03 12:27:55 -05:00
|
|
|
|
2024-02-05 22:17:23 -05:00
|
|
|
void fillline(int y, int xstart, char c) {
|
|
|
|
int i = xstart, x = g_x;
|
|
|
|
for (; i < x; ++i)
|
|
|
|
{ mvaddch(y,i,c); }
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TITLE(str) do { mvprintw(0,0,str); fillline(0,strlen(str),'-'); } while (0)
|
|
|
|
|
2024-02-07 21:30:26 -05:00
|
|
|
void decoration(void) {
|
|
|
|
/* mvprintw(g_y - 2, 0, "max screen %d; max ours %d", (g_y - 3) * SENDMAX, RECVMAX); */
|
2024-02-05 22:17:23 -05:00
|
|
|
TITLE("-- MOONTALK ");
|
|
|
|
fillline(g_y - 3, 0, '-');
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef TITLE
|
|
|
|
|
|
|
|
int g_hardrefresh = 0;
|
|
|
|
|
2024-02-05 16:20:06 -05:00
|
|
|
void init_screen(int x) {
|
|
|
|
(void)x;
|
|
|
|
signal(SIGWINCH, SIG_IGN);
|
2024-02-03 12:27:55 -05:00
|
|
|
|
2024-02-05 16:20:06 -05:00
|
|
|
endwin();
|
|
|
|
struct winsize w;
|
|
|
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
|
|
|
g_y = w.ws_row;
|
|
|
|
g_x = w.ws_col;
|
2024-02-03 12:27:55 -05:00
|
|
|
initscr();
|
2024-02-05 22:17:23 -05:00
|
|
|
raw();
|
2024-02-03 12:27:55 -05:00
|
|
|
noecho();
|
|
|
|
keypad(stdscr, ERR);
|
|
|
|
nodelay(stdscr, TRUE);
|
2024-02-05 22:17:23 -05:00
|
|
|
ESCDELAY = 0;
|
2024-02-03 12:27:55 -05:00
|
|
|
clear();
|
2024-02-07 21:30:26 -05:00
|
|
|
decoration();
|
2024-02-05 22:17:23 -05:00
|
|
|
g_hardrefresh = 1;
|
2024-02-03 12:27:55 -05:00
|
|
|
|
2024-02-05 16:20:06 -05:00
|
|
|
signal(SIGWINCH, init_screen);
|
2024-02-03 12:27:55 -05:00
|
|
|
}
|
|
|
|
|
2024-02-05 22:17:23 -05:00
|
|
|
void stop(void) {
|
|
|
|
int sockfd = g_sockfd;
|
|
|
|
endwin();
|
2024-02-07 21:30:26 -05:00
|
|
|
printf("stopping now.\n");
|
2024-02-05 22:17:23 -05:00
|
|
|
if (sockfd > -1) { close(sockfd); }
|
2024-02-03 12:27:55 -05:00
|
|
|
g_sockfd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* always returns an accessible socket */
|
|
|
|
int init_connection(char * serv, char * port) {
|
|
|
|
int status, sockfd;
|
|
|
|
struct addrinfo hints, * res;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
2024-02-05 22:17:23 -05:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
2024-02-03 12:27:55 -05:00
|
|
|
|
|
|
|
if ((status = getaddrinfo(serv, port, &hints, &res)) != 0) {
|
2024-02-05 22:17:23 -05:00
|
|
|
perror("init_connection");
|
2024-02-03 12:27:55 -05:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1)
|
|
|
|
{ goto error; }
|
|
|
|
if (connect(sockfd, res->ai_addr, res->ai_addrlen))
|
|
|
|
{ goto error; }
|
|
|
|
|
|
|
|
freeaddrinfo(res);
|
|
|
|
|
|
|
|
g_sockfd = sockfd;
|
|
|
|
return sockfd;
|
|
|
|
error:
|
2024-02-05 22:17:23 -05:00
|
|
|
if (sockfd > -1) { close(sockfd); }
|
2024-02-03 12:27:55 -05:00
|
|
|
perror("init_connection");
|
|
|
|
exit(1);
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
|
2024-02-05 16:20:06 -05:00
|
|
|
#if 0
|
|
|
|
int getanonval(char * buf, size_t len) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int countlines(char * buf, size_t len) {
|
|
|
|
size_t i = 0;
|
|
|
|
for (buf += len - 1; *buf; --buf)
|
|
|
|
{ i += (*buf == '\n'); }
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2024-02-05 22:17:23 -05:00
|
|
|
size_t getoff(char * buf, size_t len, size_t count, char c) {
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; count && i < len; ++i) {
|
|
|
|
count -= (buf[i] == c) * 1;
|
|
|
|
++buf;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2024-02-05 16:20:06 -05:00
|
|
|
size_t lastline(char * buf, size_t len) {
|
|
|
|
size_t ret;
|
|
|
|
char * start = buf;
|
|
|
|
for (buf += len - 1; *buf; --buf) {
|
|
|
|
ret = (*buf == '\n') ? buf - start : 0;
|
|
|
|
if (ret) { return ret; }
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clearline(int y) {
|
|
|
|
int oldy, oldx;
|
|
|
|
getyx(stdscr, oldy, oldx);
|
2024-02-07 21:30:26 -05:00
|
|
|
move(y, 0);
|
2024-02-05 16:20:06 -05:00
|
|
|
clrtoeol();
|
|
|
|
move(oldy, oldx);
|
|
|
|
}
|
|
|
|
|
2024-02-07 21:30:26 -05:00
|
|
|
#define UPDATE_TIME() do { \
|
2024-02-05 16:20:06 -05:00
|
|
|
t = time(NULL); \
|
|
|
|
tm = gmtime(&t); \
|
|
|
|
strftime(sendbuf, TIMESTRMAX, TIMESTR, tm); \
|
|
|
|
sendbuf[TIMESTRMAX - 1] = ' '; } while (0)
|
|
|
|
|
|
|
|
|
2024-02-03 12:27:55 -05:00
|
|
|
int main (int argc, char ** argv) {
|
|
|
|
char * argv0 = argv[0];
|
2024-02-05 16:20:06 -05:00
|
|
|
char * serv = SERV, * port = PORT, name[32] = "anonymous";
|
2024-02-03 12:27:55 -05:00
|
|
|
|
|
|
|
int sockfd;
|
|
|
|
|
|
|
|
while (++argv, --argc) {
|
|
|
|
if (streq(*argv, "-help")) {
|
|
|
|
printf("%s: HELP\n", argv0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (argc - 1)
|
|
|
|
{ --argc; ++argv; }
|
|
|
|
else {
|
|
|
|
printf("%s: %s requires argument\n", argv0, *argv);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (streq(*(argv-1), "-serv")) {
|
|
|
|
serv = *argv;
|
|
|
|
}
|
|
|
|
else if (streq(*(argv-1), "-port")) {
|
2024-02-05 16:20:06 -05:00
|
|
|
port = *argv;
|
2024-02-03 12:27:55 -05:00
|
|
|
}
|
|
|
|
else if (streq(*(argv-1), "-name")) {
|
2024-02-05 16:20:06 -05:00
|
|
|
memset(name, 0, 31);
|
|
|
|
strncpy(name, *argv, 31);
|
2024-02-03 12:27:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-05 16:20:06 -05:00
|
|
|
init_screen(-1);
|
2024-02-05 22:17:23 -05:00
|
|
|
move(1,0);
|
2024-02-03 12:27:55 -05:00
|
|
|
printw("Connecting to %s:%s as %s\n", serv, port, name);
|
2024-02-05 16:20:06 -05:00
|
|
|
printw("g_y: %d; g_x: %d\n", g_y, g_x);
|
2024-02-03 12:27:55 -05:00
|
|
|
refresh();
|
2024-02-05 16:20:06 -05:00
|
|
|
clear();
|
|
|
|
|
2024-02-05 22:17:23 -05:00
|
|
|
atexit(stop);
|
2024-02-03 12:27:55 -05:00
|
|
|
sockfd = init_connection(serv, port);
|
2024-02-05 16:20:06 -05:00
|
|
|
|
|
|
|
char raw[SENDMAX + RECVMAX];
|
2024-02-07 21:30:26 -05:00
|
|
|
char * sendbuf = raw, * recvbuf = raw + SENDMAX, * off = recvbuf;
|
2024-02-05 16:20:06 -05:00
|
|
|
size_t sendminlen;
|
2024-02-07 21:30:26 -05:00
|
|
|
size_t sendlen = sendminlen, recvlen = 1, offlen = (g_y - 4) * 255;
|
2024-02-05 16:20:06 -05:00
|
|
|
|
2024-02-03 12:27:55 -05:00
|
|
|
time_t t;
|
|
|
|
struct tm * tm;
|
2024-02-07 21:30:26 -05:00
|
|
|
useconds_t frame = 30;
|
2024-02-05 22:17:23 -05:00
|
|
|
useconds_t interval = 1000000 / frame;
|
2024-02-05 16:20:06 -05:00
|
|
|
int32_t ct = 0;
|
2024-02-07 21:30:26 -05:00
|
|
|
/* int lc, off; */
|
|
|
|
int hardrefresh = 0, inputrefresh = 1;
|
2024-02-05 16:20:06 -05:00
|
|
|
int ch;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
sendminlen = TIMESTRMAX;
|
|
|
|
|
|
|
|
memcpy(sendbuf + sendminlen, name, strlen(name));
|
|
|
|
sendminlen += strlen(name);
|
|
|
|
memcpy(sendbuf + sendminlen, "> ", 2);
|
|
|
|
sendminlen += 2;
|
|
|
|
sendlen = sendminlen;
|
|
|
|
|
2024-02-07 21:30:26 -05:00
|
|
|
decoration();
|
2024-02-05 22:17:23 -05:00
|
|
|
mvaddnstr(g_y - 1, 0, sendbuf, sendlen);
|
2024-02-05 16:20:06 -05:00
|
|
|
|
2024-02-03 12:27:55 -05:00
|
|
|
while (1) {
|
2024-02-07 21:30:26 -05:00
|
|
|
nextloop:
|
|
|
|
hardrefresh |= g_hardrefresh;
|
2024-02-05 16:20:06 -05:00
|
|
|
/* update */
|
2024-02-07 21:30:26 -05:00
|
|
|
if (ct % (frame) == 0 || inputrefresh) {
|
2024-02-05 16:20:06 -05:00
|
|
|
UPDATE_TIME();
|
2024-02-05 22:17:23 -05:00
|
|
|
mvaddnstr(g_y - 1, 0, sendbuf, sendlen);
|
2024-02-05 16:20:06 -05:00
|
|
|
}
|
2024-02-07 21:30:26 -05:00
|
|
|
/* recv */
|
2024-02-05 16:20:06 -05:00
|
|
|
if (ct % (frame * 2) == 0) {
|
2024-02-07 21:30:26 -05:00
|
|
|
ret = recv(sockfd, recvbuf + recvlen - 1, RECVMAX - recvlen + 1, MSG_DONTWAIT);
|
|
|
|
mvprintw(g_y - 2, 150, "ct:%d:%d:called", ct, ret);
|
|
|
|
if (ret > -1) {
|
2024-02-05 22:17:23 -05:00
|
|
|
recvlen += ret;
|
2024-02-07 21:30:26 -05:00
|
|
|
|
|
|
|
mvprintw(g_y - 2, 50, "rendstat ct:%d:%d:%ld", ct, ret, recvlen);
|
|
|
|
if (ret > 0 && recvbuf[recvlen-1] == '\n') {
|
|
|
|
mvprintw(g_y - 2, 0, "newline at %ld", recvlen-1);
|
|
|
|
}
|
|
|
|
mvprintw(g_y - 2, 130, "ct:%d:rendered", ct);
|
|
|
|
hardrefresh = 1;
|
2024-02-05 22:17:23 -05:00
|
|
|
}
|
2024-02-05 16:20:06 -05:00
|
|
|
}
|
2024-02-07 21:30:26 -05:00
|
|
|
if (hardrefresh) {
|
|
|
|
mvprintw(g_y - 2, 100, "ct:%d:REFRESH INVOKED", ct);
|
|
|
|
mvaddnstr(1, 0, off, offlen);
|
|
|
|
hardrefresh = 0;
|
|
|
|
g_hardrefresh = 0;
|
|
|
|
}
|
2024-02-03 12:27:55 -05:00
|
|
|
/* send */
|
2024-02-05 22:17:23 -05:00
|
|
|
while ((ch = getch()) != -1) {
|
|
|
|
if (ch == '\n') {
|
2024-02-07 21:30:26 -05:00
|
|
|
if (sendlen == sendminlen) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (sendlen + 1 < SENDMAX)
|
|
|
|
{ sendbuf[sendlen++] = '\n'; }
|
|
|
|
if (send(sockfd, sendbuf, sendlen, 0) > 0) {
|
|
|
|
memcpy(recvbuf + recvlen - 1, sendbuf, (sendlen + recvlen - 1 < RECVMAX) * sendlen);
|
|
|
|
recvlen += sendlen;
|
|
|
|
hardrefresh = 1;
|
|
|
|
} else {
|
|
|
|
mvprintw(g_y - 2, 0, "message failed: %s", strerror(errno));
|
|
|
|
}
|
2024-02-05 22:17:23 -05:00
|
|
|
mvaddch(g_y - 1, sendminlen, ' ');
|
2024-02-07 21:30:26 -05:00
|
|
|
inputrefresh = 1;
|
2024-02-05 22:17:23 -05:00
|
|
|
clearline(g_y - 1);
|
|
|
|
sendlen = sendminlen;
|
|
|
|
}
|
2024-02-07 21:30:26 -05:00
|
|
|
else if ((ch > 31 && ch < 127)) {
|
|
|
|
if (sendlen + 1 < SENDMAX)
|
|
|
|
{ sendbuf[sendlen++] = ch; }
|
|
|
|
mvaddnstr(g_y - 1, 0, sendbuf, sendlen);
|
2024-02-05 22:17:23 -05:00
|
|
|
}
|
2024-02-07 21:30:26 -05:00
|
|
|
else if (ch == BACKSPACE || ch == C_H) {
|
|
|
|
inputrefresh = 1;
|
2024-02-05 22:17:23 -05:00
|
|
|
clearline(g_y - 1);
|
|
|
|
if (sendlen - 1 >= sendminlen)
|
|
|
|
{ mvaddch(g_y - 1, --sendlen, ' '); }
|
|
|
|
mvaddnstr(g_y - 1, 0, sendbuf, sendlen);
|
|
|
|
move(g_y - 1, sendlen);
|
2024-02-07 21:30:26 -05:00
|
|
|
goto nextloop;
|
2024-02-05 22:17:23 -05:00
|
|
|
}
|
2024-02-07 21:30:26 -05:00
|
|
|
else if (ch == C_T) {
|
|
|
|
while (*off != '\n') { --off; }
|
|
|
|
if (*off == '\n') { ++off; }
|
|
|
|
hardrefresh = 1;
|
|
|
|
}
|
|
|
|
else if (ch == C_N) {
|
|
|
|
while (*off != '\n') { ++off; }
|
|
|
|
if (*off == '\n') { ++off; }
|
|
|
|
hardrefresh = 1;
|
|
|
|
}
|
|
|
|
else if (ch == C_W) {
|
|
|
|
while (sendlen > sendminlen && ispunct(sendbuf[sendlen - 1])) { --sendlen; }
|
|
|
|
while (sendlen > sendminlen && isspace(sendbuf[sendlen - 1])) { --sendlen; }
|
|
|
|
while (sendlen > sendminlen && isalnum(sendbuf[sendlen - 1])) { --sendlen; }
|
|
|
|
inputrefresh = 1;
|
|
|
|
clearline(g_y - 1);
|
|
|
|
goto nextloop;
|
|
|
|
}
|
|
|
|
else if (ch == C_L) {
|
|
|
|
clear();
|
|
|
|
decoration();
|
|
|
|
hardrefresh = 1;
|
|
|
|
ct = 0;
|
|
|
|
goto nextloop;
|
|
|
|
}
|
|
|
|
else if (ch == C_D) {
|
|
|
|
exit(0);
|
2024-02-05 22:17:23 -05:00
|
|
|
}
|
2024-02-05 16:20:06 -05:00
|
|
|
}
|
2024-02-07 21:30:26 -05:00
|
|
|
/* frame update */
|
2024-02-05 16:20:06 -05:00
|
|
|
move(g_y - 1, sendlen);
|
|
|
|
++ct;
|
|
|
|
refresh();
|
2024-02-05 22:17:23 -05:00
|
|
|
usleep(interval);
|
2024-02-03 12:27:55 -05:00
|
|
|
}
|
2024-02-05 22:17:23 -05:00
|
|
|
return 0;
|
2024-02-03 12:27:55 -05:00
|
|
|
}
|