moontalk/client/moontalk-cli.c

335 lines
7.7 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;
#define g_y LINES
#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;
while (buf - base) {
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-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)
{ sendbuf[edit++] = ch; ++sendlen; }
/* mvwchgat(input, 2, sendlen - 1, 1, A_REVERSE, 0, NULL); */
mvwaddnstr(input, 2, 0, sendbuf, sendlen);
}
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
/* mvwaddch(0, sendminlen, ' '); */
/* mvwchgat(input, 2, 0, 1, A_STANDOUT, 0, NULL); */
bodyrefresh = inputrefresh = 1;
clearline(input, 2);
edit = sendlen = sendminlen;
}
2024-02-07 21:30:26 -05:00
else if (ch == BACKSPACE || ch == C_H) {
inputrefresh = 1;
2024-02-09 16:48:08 -05:00
clearline(input, 2);
if (sendlen - 1 >= sendminlen)
2024-02-09 16:48:08 -05:00
{ mvwaddch(input, 2, --sendlen, ' '); --edit; }
mvwaddnstr(input, 2, 0, sendbuf, sendlen);
wmove(input, 2, sendlen);
}
2024-02-09 16:48:08 -05:00
else if (ch == KEY_LEFT) {
/* if (edit > sendminlen) { --edit; } */
}
else if (ch == KEY_RIGHT) {
/* if (edit - 1 < 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);
while (off - recvbuf < RECVMAX && *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);
while (off - recvbuf > 0) { --off; }
/* wclear(body); */
bodyrefresh = 1;
2024-02-07 21:30:26 -05:00
}
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;
2024-02-09 16:48:08 -05:00
clearline(input, 2);
2024-02-07 21:30:26 -05:00
}
2024-02-09 16:48:08 -05:00
}
/* update and rendering */
if (ct % frame == 0 || inputrefresh || bodyrefresh) {
UPDATE_TIME();
/* wclear(input); */
mvwaddnstr(input, 2, 0, sendbuf, sendlen);
ret = recv(sockfd, recvbuf + recvlen, RECVMAX - recvlen, MSG_DONTWAIT);
if (errno != EAGAIN)
{ mvwaddstr(input, 1, 0, strerror(errno)); }
if (bodyrefresh) {
bodyrefresh = 0;
if (!(ret > -1))
goto _bodyrefresh;
2024-02-07 21:30:26 -05:00
}
2024-02-09 16:48:08 -05:00
if (ret > -1) {
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
}