diff options
author | thing1 <thing1@seacrossedlovers.xyz> | 2025-06-30 07:54:25 +0100 |
---|---|---|
committer | thing1 <thing1@seacrossedlovers.xyz> | 2025-06-30 07:54:25 +0100 |
commit | bfb5a9ad837e8985aa66acb8a9d6e6790488dcd4 (patch) | |
tree | 04822c32837b15f0497d76f431df2305b5c90e92 |
init commit, should remake send
-rw-r--r-- | caln/Makefile | 28 | ||||
-rwxr-xr-x | caln/addevent | 7 | ||||
-rwxr-xr-x | caln/agenda | 5 | ||||
-rwxr-xr-x | caln/caln | bin | 0 -> 21872 bytes | |||
-rw-r--r-- | caln/caln.c | 178 | ||||
-rw-r--r-- | caln/caln.o | bin | 0 -> 14024 bytes | |||
-rwxr-xr-x | caln/delevent | 7 | ||||
-rwxr-xr-x | caln/mkevent | bin | 0 -> 19392 bytes | |||
-rw-r--r-- | caln/mkevent.c | 39 | ||||
-rw-r--r-- | caln/mkevent.o | bin | 0 -> 8416 bytes | |||
-rwxr-xr-x | caln/sortevents | 6 | ||||
-rw-r--r-- | send/Makefile | 24 | ||||
-rw-r--r-- | send/arg.h | 63 | ||||
-rwxr-xr-x | send/send | bin | 0 -> 24680 bytes | |||
-rw-r--r-- | send/send.c | 127 | ||||
-rw-r--r-- | send/send.o | bin | 0 -> 14112 bytes | |||
-rwxr-xr-x | send/sendd | bin | 0 -> 25568 bytes | |||
-rw-r--r-- | send/sendd.c | 207 | ||||
-rw-r--r-- | send/sendd.o | bin | 0 -> 16528 bytes | |||
-rw-r--r-- | send/users.c | 13 | ||||
-rw-r--r-- | send/users.h | 1 | ||||
-rw-r--r-- | send/users.o | bin | 0 -> 6984 bytes | |||
-rw-r--r-- | send/util.h | 27 |
23 files changed, 732 insertions, 0 deletions
diff --git a/caln/Makefile b/caln/Makefile new file mode 100644 index 0000000..5b7cd37 --- /dev/null +++ b/caln/Makefile @@ -0,0 +1,28 @@ +CFLAGS = -std=c23 -ggdb +LIBS = -lm + +SRC = caln.c mkevent.c +OBJ = $(SRC:.c=.o) + +all: caln + +.c.o: + cc -c ${CFLAGS} $< + +caln: caln.o mkevent.o + cc caln.o -o caln ${CFLAGS} ${LIBS} + cc mkevent.o -o mkevent ${CFLAGS} ${LIBS} + +install: caln + cp caln mkevent addevent delevent sortevents agenda /usr/local/bin/ + +uninstall: + rm /usr/local/bin/caln + rm /usr/local/bin/mkevent + rm /usr/local/bin/addevent + rm /usr/local/bin/delevent + rm /usr/local/bin/sortevents + rm /usr/local/bin/agenda + +clean: + rm -rf *.o caln mkevent diff --git a/caln/addevent b/caln/addevent new file mode 100755 index 0000000..51dc52e --- /dev/null +++ b/caln/addevent @@ -0,0 +1,7 @@ +#!/bin/sh + +eventfile="${HOME}/.cache/caln" + +touch $eventfile + +(mkevent "$@" || exit 1) >> $eventfile diff --git a/caln/agenda b/caln/agenda new file mode 100755 index 0000000..b9179f0 --- /dev/null +++ b/caln/agenda @@ -0,0 +1,5 @@ +#!/bin/sh + +calnfile="${HOME}/.cache/caln" + +caln < $calnfile diff --git a/caln/caln b/caln/caln Binary files differnew file mode 100755 index 0000000..b01246b --- /dev/null +++ b/caln/caln diff --git a/caln/caln.c b/caln/caln.c new file mode 100644 index 0000000..5c36db9 --- /dev/null +++ b/caln/caln.c @@ -0,0 +1,178 @@ +#include <time.h> +#include <stdio.h> +#include <stdbool.h> +#include <string.h> +#include <stdlib.h> + +#define DATEFMT "%a %d %b %Y" +#define SEP " ~> " + +typedef struct event { + char *name; + time_t time; +} event; + +static bool cal = true; + +static const char *days[] = { + "Su", + "Mo", + "Tu", + "We", + "Th", + "Fr", + "Sa" +}; + +static const char *months[] = { + "Jan", + "Feb", + "Mar", + "Apr", + "May", + "Jun", + "Jul", + "Aug", + "Sep", + "Oct", + "Nov", + "Dec" +}; + +static const int daysinmonth[] = { + 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 +}; + +static bool +isleap(int year) { + if ((year % 4) == 0 && (year % 100) != 0) + return true; + else if ((year % 400) == 0) + return true; + return false; +} + +static void +print_month(struct tm *t) { + printf(" %s %d\n", months[t->tm_mon], t->tm_year + 1900); +} + +static void +print_days() { + for (int i = 0; i < 7; i++) + printf("%s ", days[i]); + putchar('\n'); +} + +static void +print_padded(int n, int l) { + int i = 1; +loop: + if ((n / i) > 0) { + i *= 10; + l--; + goto loop; + } + + if (l != 0) + printf("%*s", l, " "); + if (l < 0) + return; + + printf("%d", n); +} + +static void +print_month_grid(struct tm *t) { + int day = 1; + int total = daysinmonth[t->tm_mon]; + if (t->tm_mon == 1 && isleap(t->tm_year + 1900)) + total += 1; + + int offset = (t->tm_yday % 7) - 1; + for (int i = 0; i < offset; i++) + printf(" "); + + for (; day < total + 1; day++) { + if (day == t->tm_mday) { + printf("\e[4;34m"); + print_padded(day, 2); + printf("\e[0m"); + } else + print_padded(day, 2); + putchar(' '); + if (((day + offset) % 7) == 0) + putchar('\n'); + } + putchar('\n'); +} + +static void +print_cal(struct tm *t) { + print_month(t); + print_days(); + print_month_grid(t); +} + +static event * +read_event(FILE *f) { + event *e = malloc(sizeof(event)); + char buf[256] = {0}; + if (!fgets(buf, 256, f)) { + free(e); + return NULL; + } + char *time = buf; + char *name = strchr(buf, '|'); + if (!name) { + fprintf(stderr, "invalid event! %s\n", buf); + exit(1); + } + *name = 0; + name++; + + e->name = strdup(name); + e->time = atoll(time); + return e; +} + +static void +print_event(event *e) { + struct tm *t = localtime(&e->time); + char buf[256]; + strftime(buf, 256, DATEFMT, t); + printf("%s" SEP "%s", buf, e->name); +} + +static void +free_event(event *e) { + free(e->name); + free(e); +} + +int +main(int argc, char **argv) { + if (argc > 1) + if (strcmp(argv[1], "-h") == 0) + cal = false; + + time_t ulocal = time(NULL); + struct tm *local = localtime(&ulocal); + + if (cal) + print_cal(local); + + bool dirty = false; + event *e; + while (e = read_event(stdin)) { + if (ulocal + (86400 * 30) > e->time) { + print_event(e); + dirty = true; + } + free_event(e); + } + if (!dirty) + printf("You have nothing upcoming\n"); + + return 0; +} diff --git a/caln/caln.o b/caln/caln.o Binary files differnew file mode 100644 index 0000000..3678ef4 --- /dev/null +++ b/caln/caln.o diff --git a/caln/delevent b/caln/delevent new file mode 100755 index 0000000..eb13b08 --- /dev/null +++ b/caln/delevent @@ -0,0 +1,7 @@ +#!/bin/sh + +calnfile="${HOME}/.cache/caln" + +line=$(grep $@ $calnfile | fzf || exit 1) + +sed -i.bak "/${line}/d" $calnfile diff --git a/caln/mkevent b/caln/mkevent Binary files differnew file mode 100755 index 0000000..57e9742 --- /dev/null +++ b/caln/mkevent diff --git a/caln/mkevent.c b/caln/mkevent.c new file mode 100644 index 0000000..6c3f025 --- /dev/null +++ b/caln/mkevent.c @@ -0,0 +1,39 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +static void +usage() { + fprintf(stderr, "mkevent \"contents\" Tomorrow | NUM NUM [NUM]\n"); + exit(1); +} + +int +main (int argc, char **argv) { + if (argc < 3) + usage(); + + time_t ut = time(NULL); + struct tm *t = localtime(&ut); + + if ((strcmp(argv[2], "tomorrow") & strcmp(argv[2], "Tomorrow") & strcmp("Tom", argv[2])) == 0) + ut += 86400; + else { + char buf[256] = {0}; + snprintf(buf, 256, "%s %s", + (argv[2]) ? argv[2] : "", + (argv[3]) ? argv[3] : ""); + int day, month; + int ret = sscanf(buf, "%d %d", &day, &month); + + t->tm_mday = day; + t->tm_mon = month - 1; + + if (ret < 2) usage(); + + ut = mktime(t); + } + + printf("%zu|%s\n", ut, argv[1]); +} diff --git a/caln/mkevent.o b/caln/mkevent.o Binary files differnew file mode 100644 index 0000000..f16d483 --- /dev/null +++ b/caln/mkevent.o diff --git a/caln/sortevents b/caln/sortevents new file mode 100755 index 0000000..ebedb3c --- /dev/null +++ b/caln/sortevents @@ -0,0 +1,6 @@ +#!/bin/sh + +calnfile="${HOME}/.cache/caln" + +(sort $calnfile || exit 1) > /tmp/caln.sort +mv /tmp/caln.sort $calnfile diff --git a/send/Makefile b/send/Makefile new file mode 100644 index 0000000..100e6df --- /dev/null +++ b/send/Makefile @@ -0,0 +1,24 @@ +CFLAGS = -ggdb -D_GNU_SOURCE +LIBS = `pkg-config --libs openssl` + +SRC = sendd.c send.c users.c +OBJ = $(SRC:.c=.o) + +all: sendd + +.c.o: + cc -c ${CFLAGS} $< + +sendd: sendd.o send.o users.o + cc sendd.o users.o -o sendd ${CFLAGS} ${LIBS} + cc send.o users.o -o send ${CFLAGS} ${LIBS} + +install: sendd + cp sendd send addevent delevent sortevents agenda /usr/local/bin/ + +uninstall: + rm /usr/local/bin/sendd + rm /usr/local/bin/send + +clean: + rm -rf *.o sendd send diff --git a/send/arg.h b/send/arg.h new file mode 100644 index 0000000..4df77a7 --- /dev/null +++ b/send/arg.h @@ -0,0 +1,63 @@ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef ARG_H__ +#define ARG_H__ + +extern char *argv0; + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][1]\ + && argv[0][0] == '-';\ + argc--, argv++) {\ + char argc_;\ + char **argv_;\ + int brk_;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + for (brk_ = 0, argv[0]++, argv_ = argv;\ + argv[0][0] && !brk_;\ + argv[0]++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][0];\ + switch (argc_) + +/* Handles obsolete -NUM syntax */ +#define ARGNUM case '0':\ + case '1':\ + case '2':\ + case '3':\ + case '4':\ + case '5':\ + case '6':\ + case '7':\ + case '8':\ + case '9' + +#define ARGEND }\ + } + +#define ARGC() argc_ + +#define ARGNUMF(base) (brk_ = 1, estrtol(argv[0], (base))) + +#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#endif diff --git a/send/send b/send/send Binary files differnew file mode 100755 index 0000000..0cb238c --- /dev/null +++ b/send/send diff --git a/send/send.c b/send/send.c new file mode 100644 index 0000000..aae33c2 --- /dev/null +++ b/send/send.c @@ -0,0 +1,127 @@ +#include <sys/socket.h> +#include <arpa/inet.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <unistd.h> +#include <string.h> +#include <time.h> + +#include "util.h" +#include "arg.h" +#include "users.h" + +FILE *fout; +char *argv0; + +static int +init_client(char *host, int port) { + int s = socket(AF_INET, SOCK_STREAM, 0); + + struct sockaddr_in addr = {AF_INET, htons(port)}; + if (!inet_pton(AF_INET, host, &addr.sin_addr)) + error("couldn't resolve host"); + + if (connect(s, (struct sockaddr *)&addr, sizeof(addr))) + error("Failed to connect"); + + return s; +} + +static void +stop_client(int s) { + close(s); +} + +static void +connect_client(int s, char *nick) { + char *msg; + int len = asprintf(&msg, "connect %s", nick); + send(s, msg, len, 0); + free(msg); +} + +static void +send_msg(int s, char *contents) { + char *msg; + time_t t = time(NULL); + + int len = asprintf(&msg, "msg %.5d %s", strlen(contents), contents); + + send(s, msg, len, 0); + send(s, &t, sizeof(time_t), 0); + free(msg); +} + +static void +resend_msg(int s, char *contents) { + char *msg; + time_t t = time(NULL); + + int len = asprintf(&msg, "remsg %.5d %s", strlen(contents), contents); + + send(s, msg, len, 0); + send(s, &t, sizeof(time_t), 0); + free(msg); +} + +static void +usage() { + fprintf(stderr, "send [-h host] [-p port] [-P password] [-n nick] -m msg\n"); + exit(1); +} + +static int +recv_response(int s) { + int i; + recv(s, &i, sizeof(int), 0); + return i; +} + +int +main(int argc, char **argv) { + char *host = "127.0.0.1"; + int port = 1543; + char *nick = getenv("SENDUSER"); + char *pass = getenv("SENDPASS"); + char *msg = NULL; + + ARGBEGIN { + case 'h': + host = EARGF(usage()); + break; + case 'p': + port = atoi(EARGF(usage())); + break; + case 'P': + pass = EARGF(usage()); + break; + case 'n': + nick = EARGF(usage()); + break; + case 'm': + msg = EARGF(usage()); + break; + } ARGEND; + + if (!msg) + usage(); + if (!nick) + error("failed to get nickname, use -n!"); + if (!pass) + error("failed to get password, use -p!"); + + int s = init_client(host, port); + connect_client(s, nick); + + int msg_hash = make_mhash(msg); + + send_msg(s, msg); + + while (recv_response(s) != msg_hash) + resend_msg(s, msg); + + printf("valid msg sent\n"); + + stop_client(s); +} diff --git a/send/send.o b/send/send.o Binary files differnew file mode 100644 index 0000000..2be577e --- /dev/null +++ b/send/send.o diff --git a/send/sendd b/send/sendd Binary files differnew file mode 100755 index 0000000..cf79a9e --- /dev/null +++ b/send/sendd diff --git a/send/sendd.c b/send/sendd.c new file mode 100644 index 0000000..1b0c34e --- /dev/null +++ b/send/sendd.c @@ -0,0 +1,207 @@ +#include <sys/socket.h> +#include <arpa/inet.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <unistd.h> +#include <string.h> +#include <netinet/in.h> +#include <netinet/tcp.h> +#include <ctype.h> +#include <stdbool.h> + +#include "util.h" + +/* + * CONNECT MSG: "connect NAME" + * SEND MSG: "msg XXXXX CONTENTS TIME" + * VALID MSG: 0 + * INVALID MSG: 1 + */ + +typedef struct client { + char *name; + int fd; + bool connected; +} client; + +typedef struct msg { + client *sender; + char *contents; + time_t time; +} msg; + +FILE *fout; +int VALID = 0; +int INVALID = 1; + +static int +init_daemon(int port) { + int s = socket(AF_INET, SOCK_STREAM, 0); + + struct sockaddr_in addr = {AF_INET, htons(port), INADDR_ANY}; + + if (bind(s, (struct sockaddr *)&addr, sizeof(addr))) + error("Failed to bind"); + + int flags = 10; + if (setsockopt(s, SOL_TCP, TCP_KEEPIDLE, (void *)&flags, sizeof(flags))) + error("Couldn't set socket opts"); + + flags = 5; + if (setsockopt(s, SOL_TCP, TCP_KEEPCNT, (void *)&flags, sizeof(flags))) + error("Couldn't set socket opts"); + + flags = 5; + if (setsockopt(s, SOL_TCP, TCP_KEEPINTVL, (void *)&flags, sizeof(flags))) + error("Couldn't set socket opts"); + + listen(s, 10); + return s; +} + +static void +stop_daemon(int s) { + close(s); +} + +static client * +accept_client(int s) { + client *c = malloc(sizeof(client)); + c->fd = accept(s, NULL, NULL); + + char buf[256] = {0}; + char *Pbuf = buf; + int n = recv(c->fd, buf, 256, 0); + if (n <= 0) { + warn("Client hungup"); + close(c->fd); + return NULL; + } + if (n <= 8) { + warn("Malformed connect msg"); + close(c->fd); + return NULL; + } + + if (memcmp(buf, "connect ", 8) != 0) { + warn("Malformed connect msg"); + close(c->fd); + return NULL; + } + + Pbuf += 8; + c->name = strdup(Pbuf); + c->connected = true; + + return c; +} + +static void +close_client(client *c) { + close(c->fd); + free(c->name); + free(c); +} + +static msg* +recv_msg(client *c) { + char buf[11] = {0}; + char *Pbuf = buf + 4; + int n = recv(c->fd, buf, 10, 0); + if (n <= 0) { + warn("Client hungup"); + close(c->fd); + c->connected = false; + return NULL; + } + if (n < 10) { + warn("Malformed msg msg"); + send(c->fd, &INVALID, sizeof(int), 0); + return NULL; + } + if (memcmp(buf, "msg ", 4) != 0) { + warn("Malformed msg msg"); + send(c->fd, &INVALID, sizeof(int), 0); + return NULL; + } + for (int i = 4; buf[i] != 0 && i < 9; i++) { + if (!isdigit(buf[i])) { + warn("Malformed msg msg"); + send(c->fd, &INVALID, sizeof(int), 0); + return NULL; + } + } + + int len = atoi(Pbuf); + char *contents = malloc(len); + memset(contents, 0, len); + + n = recv(c->fd, contents, len, 0); + if (n <= 0) { + warn("Client hungup"); + c->connected = false; + close(c->fd); + return NULL; + } + + msg *m = malloc(sizeof(msg)); + m->sender = c; + m->contents = contents; + + time_t t; + n = recv(c->fd, &t, sizeof(time_t), 0); + if (n <= 0) { + warn("Client hungup"); + c->connected = false; + close(c->fd); + return NULL; + } + m->time = t; + + send(c->fd, &VALID, sizeof(int), 0); + + return m; +} + +static void +free_msg(msg *m) { + free(m->contents); + free(m); +} + +static void +msg_loop(client *c) { + msg *m; + while (c->connected && (m = recv_msg(c))) { + fprintf(fout, "%lld:%s:%s\n", m->time, m->sender->name, m->contents); + free_msg(m); + } + close_client(c); + exit(0); +} + +int +main(int argc, char **argv) { + char *out; + if (argc <= 1) + out = "/dev/stdout"; + else + out = argv[1]; + + if (!(fout = fopen(out, "w"))) + error("Couldn't open outfile"); + + int s = init_daemon(1543); + + pid_t pid; + for (;;) { + client *c = accept_client(s); + if ((pid = fork()) == 0) + msg_loop(c); + else + continue; + } + + stop_daemon(s); +} diff --git a/send/sendd.o b/send/sendd.o Binary files differnew file mode 100644 index 0000000..0d37b2e --- /dev/null +++ b/send/sendd.o diff --git a/send/users.c b/send/users.c new file mode 100644 index 0000000..7b161f9 --- /dev/null +++ b/send/users.c @@ -0,0 +1,13 @@ +#include <stdio.h> +#include <stdlib.h> +#include <openssl/sha.h> + +#include "util.h" + +char * +make_userid(char *nick, char *pass) { + char *prehash, *hash; + int len = asprintf(&prehash, "%s:%s", nick, pass); + hash = SHA1(prehash, len, NULL); + return hash; +} diff --git a/send/users.h b/send/users.h new file mode 100644 index 0000000..47577fb --- /dev/null +++ b/send/users.h @@ -0,0 +1 @@ +char *make_userid(char *nick, char *pass); diff --git a/send/users.o b/send/users.o Binary files differnew file mode 100644 index 0000000..131ef6f --- /dev/null +++ b/send/users.o diff --git a/send/util.h b/send/util.h new file mode 100644 index 0000000..5f1a698 --- /dev/null +++ b/send/util.h @@ -0,0 +1,27 @@ +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> + +static void +warn(const char *msg) { + if (errno != 0) + perror(msg); + else + fprintf(stderr, "error: %s\n", msg); +} + +static void +error(const char *msg) { + warn(msg); + exit(1); +} + +static int +make_mhash(char *m) { + int hash = 0; + while (*m != 0) { + hash += *m; + m++; + } + return hash; +} |