moontalk/client/moontalk-cli.c

357 lines
8.2 KiB
C
Raw Normal View History

/* moontalk.c - @BAKE cc -O2 -std=gnu99 -Wall -Wextra -Wpedantic -Wno-format-truncation $@ -o $* \
2024-02-09 16:48:08 -05:00
-D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600 -lncurses -ltinfo $+
* Written by Emil.
* Licensed under the GPLv3 only.
*/
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>
#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-09 16:48:08 -05:00
#define NAME "anonymous"
#define streq(a,b) (!strcmp(a,b))
#define strneq(a,b,c) (!memcmp(a,b,c))
int g_sockfd;
2024-02-10 20:02:02 -05:00
#define g_y LINES
2024-02-09 16:48:08 -05:00
#define g_x COLS
#define HELP \
"%s [options ...]\n" \
"\n-serv SERVER Sets the server to connect to [default: " SERV "]" \
"\n-port PORT Sets the port [default: " PORT "]" \
"\n-name NAME Sets your display name [default: " NAME "]\n" \
"\nControls\n" \
"\nC-l Refreshes the screen" \
"\nC-w Delete the previous word" \
"\nC-c Close the client" \
"\nUp/Down Scrolls all the way up/Scrolls down by a line\n"
/* I know, and I don't care */
2024-02-03 12:27:55 -05:00
#define TAB 9 /* */
2024-02-05 16:20:06 -05:00
#define BACKSPACE 263 /* ^G */
#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 */
#define C_W 23 /* DELETE PREVWORD */
2024-02-09 16:48:08 -05:00
#define C_L 12 /* Signal full refresh */
2024-02-09 16:48:08 -05:00
/****/
2024-02-09 16:48:08 -05:00
void disconnect(void) {
int sockfd = g_sockfd;
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-09 16:48:08 -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) {
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:
if (sockfd > -1) { close(sockfd); }
2024-02-03 12:27:55 -05:00
perror("init_connection");
exit(1);
__builtin_unreachable();
}
2024-02-09 16:48:08 -05:00
//
2024-02-05 16:20:06 -05:00
2024-02-09 16:48:08 -05:00
void fillline(WINDOW * w, int y, int xstart, char c) {
int i = xstart, x = g_x;
for (; i < x; ++i)
{ mvwaddch(w,y,i,c); }
2024-02-05 16:20:06 -05:00
}
2024-02-09 16:48:08 -05:00
void clearline(WINDOW * w, int y) {
wmove(w, y, 0);
wclrtoeol(w);
}
2024-02-09 16:48:08 -05:00
void sanitize(char * buf, size_t rem) {
char * base = buf;
buf += rem;
2024-02-10 20:02:02 -05:00
while (*buf && buf - base) {
2024-02-09 16:48:08 -05:00
if (*buf < ' ' || *buf > '~') {
if (*buf != '\n')
{ *buf = '!'; }
}
--buf;
2024-02-05 16:20:06 -05:00
}
}
2024-02-03 12:27:55 -05:00
int main (int argc, char ** argv) {
2024-02-09 16:48:08 -05:00
char * serv = SERV, * port = PORT, name[32] = NAME;
2024-02-03 12:27:55 -05:00
int sockfd;
2024-02-09 16:48:08 -05:00
{
char * argv0 = argv[0];
2024-02-03 12:27:55 -05:00
2024-02-09 16:48:08 -05:00
while (++argv, --argc) {
if (streq(*argv, "-help")) {
printf(HELP, argv0);
exit(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")) {
port = *argv;
} else if (streq(*(argv-1), "-name")) {
memset(name, 0, 31);
strncpy(name, *argv, 31);
}
2024-02-03 12:27:55 -05:00
}
2024-02-09 16:48:08 -05:00
printf("Connecting to %s:%s as %s\n", serv, port, name);
atexit(disconnect);
sockfd = init_connection(serv, port);
2024-02-03 12:27:55 -05:00
}
2024-02-09 16:48:08 -05:00
initscr();
noecho();
keypad(stdscr, ERR);
nodelay(stdscr, TRUE);
ESCDELAY = 0;
curs_set(0);
2024-02-10 20:02:02 -05:00
if (has_colors() && can_change_color()) {
short bg, fg;
/* leaks memory :( */
start_color();
for (bg = 0; bg < 8; ++bg) {
for (fg = 0; fg < 8; ++fg) {
init_pair(16 + fg + (bg * 8), fg, bg);
}
}
}
2024-02-05 16:20:06 -05:00
clear();
2024-02-09 16:48:08 -05:00
#define WINCOUNT 3
WINDOW * w[WINCOUNT];
#define header w[0]
#define body w[1]
#define input w[2]
#define SENDMAX (1 << 8) - 1
#define RECVMAX 17663 /* (1 << 11) */
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-09 16:48:08 -05:00
size_t sendminlen, sendlen, recvlen = 0, offlen = recvlen;
size_t edit;
#define TIMESTR "<%Y/%m/%d %H:%M:%S "
#define TIMESTRMAX 21
#define UPDATE_TIME()\
do { \
t = time(NULL); \
tm = gmtime(&t); \
strftime(sendbuf, TIMESTRMAX, TIMESTR, tm); \
sendbuf[TIMESTRMAX - 1] = ' '; \
} while (0)
2024-02-05 16:20:06 -05:00
2024-02-03 12:27:55 -05:00
time_t t;
struct tm * tm;
2024-02-09 16:48:08 -05:00
2024-02-07 21:30:26 -05:00
useconds_t frame = 30;
2024-02-09 16:48:08 -05:00
useconds_t interval = 1000000. / frame;
int32_t ct;
2024-02-05 16:20:06 -05:00
2024-02-09 16:48:08 -05:00
int ch, ret;
size_t i;
2024-02-05 16:20:06 -05:00
2024-02-09 16:48:08 -05:00
size_t namelen = strlen(name);
2024-02-05 16:20:06 -05:00
2024-02-09 16:48:08 -05:00
edit = sendlen = sendminlen = TIMESTRMAX + namelen + 2;
2024-02-05 16:20:06 -05:00
2024-02-09 16:48:08 -05:00
/* fill in the name */
memcpy(sendbuf + TIMESTRMAX, name, namelen);
memcpy(sendbuf + TIMESTRMAX + namelen, "> ", 2);
2024-02-07 21:30:26 -05:00
2024-02-09 16:48:08 -05:00
int inputrefresh, bodyrefresh;
hardrefresh:
ct = 0;
inputrefresh = bodyrefresh = 1;
header = newwin(1, g_x, 0, 0);
body = newwin(g_y - 4, g_x, 1, 0);
input = newwin(3, g_x, g_y - 3, 0);
fillline(header, 0, 0, '-');
mvwprintw(header, 0, 2, " moontalk ");
fillline(input, 0, 0, '-');
while (1) {
/* input */
while ((ch = getch()) != -1) {
2024-02-09 16:48:08 -05:00
if (ch == KEY_RESIZE || ch == C_L) {
for (i = 0; i < WINCOUNT; ++i)
{ delwin(w[i]); }
endwin();
erase();
refresh();
clear();
flushinp();
goto hardrefresh;
}
else if ((ch > 31 && ch < 127)) {
if (sendlen + 1 < SENDMAX)
2024-02-10 20:02:02 -05:00
{
memmove(sendbuf + edit + 1, sendbuf + edit, sendlen - edit);
sendbuf[edit++] = ch; ++sendlen;
}
inputrefresh = 1;
2024-02-09 16:48:08 -05:00
}
else if (ch == '\n') {
if (sendlen == sendminlen)
{ continue; }
2024-02-07 21:30:26 -05:00
if (sendlen + 1 < SENDMAX)
{ sendbuf[sendlen++] = '\n'; }
if (send(sockfd, sendbuf, sendlen, 0) > 0) {
2024-02-09 16:48:08 -05:00
memcpy(recvbuf + recvlen, sendbuf, (sendlen + recvlen < RECVMAX) * sendlen);
2024-02-07 21:30:26 -05:00
recvlen += sendlen;
2024-02-09 16:48:08 -05:00
offlen += sendlen;
2024-02-07 21:30:26 -05:00
} else {
2024-02-09 16:48:08 -05:00
mvwprintw(input, 1, 0, "message failed: %s", strerror(errno));
2024-02-07 21:30:26 -05:00
}
2024-02-09 16:48:08 -05:00
bodyrefresh = inputrefresh = 1;
edit = sendlen = sendminlen;
}
2024-02-07 21:30:26 -05:00
else if (ch == BACKSPACE || ch == C_H) {
inputrefresh = 1;
2024-02-10 20:02:02 -05:00
if (sendlen - 1 >= sendminlen && edit - 1 >= sendminlen)
{
memmove(sendbuf + edit - 1, sendbuf + edit, sendlen - edit);
--sendlen; --edit;
}
inputrefresh = 1;
}
2024-02-09 16:48:08 -05:00
else if (ch == KEY_LEFT) {
2024-02-10 20:02:02 -05:00
if (edit > sendminlen) { --edit; }
2024-02-09 16:48:08 -05:00
}
else if (ch == KEY_RIGHT) {
2024-02-10 20:02:02 -05:00
if (edit < sendlen) { ++edit; }
2024-02-07 21:30:26 -05:00
}
2024-02-09 16:48:08 -05:00
else if (ch == KEY_DOWN) {
mvwprintw(input, 1, 150, "scroll down %ld", offlen);
2024-02-10 20:02:02 -05:00
while ((size_t)(off - recvbuf) < recvlen && *off != '\n') { ++off; }
2024-02-07 21:30:26 -05:00
if (*off == '\n') { ++off; }
2024-02-09 16:48:08 -05:00
wclear(body);
bodyrefresh = 1;
}
else if (ch == KEY_UP) {
mvwprintw(input, 1, 150, "scroll up %ld", offlen);
2024-02-10 20:02:02 -05:00
if (off - 2 - recvbuf > 0) { off -= 2; }
while (off - recvbuf > 0 && *off != '\n') { --off; }
if (*off == '\n') { ++off; }
2024-02-09 16:48:08 -05:00
bodyrefresh = 1;
2024-02-07 21:30:26 -05:00
}
else if (ch == C_W) {
2024-02-10 20:02:02 -05:00
i = edit;
while (i > sendminlen && isspace(sendbuf[i - 1])) { --i; }
while (i > sendminlen && !isspace(sendbuf[i - 1])) { --i; }
if (i == edit) { continue; }
mvwprintw(input, 1, 200, "diff:%ld", sendlen - edit);
/* memmove(sendbuf + i, sendbuf + edit, sendlen - edit); */
/* sendlen -= edit; */
/* edit = i; */
/* mvwprintw(input, 1, 200, "i:%ld:%ld:sendl:%3ld", */
/* i - sendminlen, (sendbuf + edit) - (sendbuf + i), sendlen - sendminlen); */
2024-02-07 21:30:26 -05:00
inputrefresh = 1;
}
2024-02-09 16:48:08 -05:00
}
/* update and rendering */
2024-02-10 20:02:02 -05:00
if (inputrefresh) {
clearline(input, 2);
2024-02-09 16:48:08 -05:00
mvwaddnstr(input, 2, 0, sendbuf, sendlen);
2024-02-10 20:02:02 -05:00
mvwchgat(input, 2, edit, 1, A_REVERSE, 0, NULL);
}
if (ct % frame == 0) {
UPDATE_TIME();
}
2024-02-09 16:48:08 -05:00
2024-02-10 20:02:02 -05:00
if (ct % frame == 0 || bodyrefresh) {
2024-02-09 16:48:08 -05:00
ret = recv(sockfd, recvbuf + recvlen, RECVMAX - recvlen, MSG_DONTWAIT);
2024-02-10 20:02:02 -05:00
if (errno && errno != EAGAIN)
2024-02-09 16:48:08 -05:00
{ mvwaddstr(input, 1, 0, strerror(errno)); }
if (bodyrefresh) {
bodyrefresh = 0;
2024-02-10 20:02:02 -05:00
if (!(ret > 0))
2024-02-09 16:48:08 -05:00
goto _bodyrefresh;
2024-02-07 21:30:26 -05:00
}
2024-02-10 20:02:02 -05:00
if (ret > 0) {
2024-02-09 16:48:08 -05:00
sanitize(recvbuf + recvlen, ret);
if (ret + recvlen < RECVMAX)
{
recvlen += ret;
offlen += ret;
}
_bodyrefresh:
mvwprintw(input, 1, 50, "render ct:%d ret:%d", ct, ret);
mvwaddnstr(body, 0,0, off, offlen);
}
2024-02-05 16:20:06 -05:00
}
refresh();
2024-02-09 16:48:08 -05:00
for (i = 0; i < WINCOUNT; ++i)
{ wnoutrefresh(w[i]); }
doupdate();
usleep(interval);
2024-02-09 16:48:08 -05:00
++ct;
2024-02-03 12:27:55 -05:00
}
2024-02-09 16:48:08 -05:00
endwin();
2024-02-03 12:27:55 -05:00
}