irced

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 86869eb0fb2587b6b104aa7299c4c2ae42b27516
parent 46a673c73306014e31a07a0ea63af82a18f6145d
Author: aabacchus <ben@bvnf.space>
Date:   Sun, 24 Jul 2022 00:42:34 +0100

usernames

Diffstat:
Mirced.c | 30+++++++++++++++++++++++++-----
1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/irced.c b/irced.c @@ -14,7 +14,9 @@ #include <unistd.h> #define NUM_CONNS 3 -#define PROGRAM "/bin/ed" +#define UNAME_LEN 10 + +char *program[] = {"/bin/ed", "-p", "* ", NULL}; void fail(char *s, ...) { @@ -106,7 +108,7 @@ accept_one(int sfd) { } void -fork_rw(char *cmd, int *wr, int *rd) { +fork_rw(char *cmd[], int *wr, int *rd) { int to[2], from[2]; pipe(to); pipe(from); @@ -124,7 +126,7 @@ fork_rw(char *cmd, int *wr, int *rd) { close(from[0]); dup2(to[0], 0); dup2(from[1], 1); - execl(cmd, cmd, 0); + execv(cmd[0], cmd); fail("exec %s failed: %s", strerror(errno)); break; default: @@ -138,12 +140,13 @@ int loop(int sfd) { int pollret, to_ed, from_ed, n; struct pollfd fds[NUM_CONNS + 2]; /* NUM_CONNS clients + server listening + program. */ + char unames[NUM_CONNS][UNAME_LEN + 1] = {'\0'}; char buf[0x100] = {'\0'}; ssize_t buf_used; n = 0; fds[0].fd = sfd; n++; fds[0].events = POLLIN; - fork_rw(PROGRAM, &to_ed, &from_ed); + fork_rw(program, &to_ed, &from_ed); fds[1].fd = from_ed; n++; fds[1].events = POLLIN; @@ -169,6 +172,7 @@ loop(int sfd) { continue; } + dprintf(newfd, "welcome to the ed server!\nplease provide a username (max %d chars): ", UNAME_LEN); fds[n].fd = newfd; fds[n].events = POLLIN; n++; @@ -184,6 +188,20 @@ loop(int sfd) { fprintf(stderr, "got '"); write(2, buf, buf_used); fprintf(stderr, "'\n"); + + if (i >=2 && unames[i-2][0] == '\0') { + /* first message from client; use as username */ + fprintf(stderr, "using as username\n"); + char *p; + memcpy(unames[i-2], buf, UNAME_LEN); + if ((p = memchr(unames[i-2], '\n', UNAME_LEN))) + *p = '\0'; + if ((p = memchr(unames[i-2], '\r', UNAME_LEN))) + *p = '\0'; + unames[i-2][UNAME_LEN] = '\0'; + continue; + } + /* send to ed */ if (i != 1 && write(to_ed, buf, buf_used) == -1) fail("couldn't write to ed: %s", strerror(errno)); @@ -191,7 +209,9 @@ loop(int sfd) { for (int j = 2; j < n; j++) { if (i == j) /* don't send to self */ continue; - /* TODO: prefix messages with user id/name? */ + /* write username: */ + if (i >= 2) + dprintf(fds[j].fd, "%s: ", unames[i - 2]); if (send(fds[j].fd, buf, buf_used, 0) == -1) { fprintf(stderr, "couldn't write to fd %d: %s\n", fds[j].fd, strerror(errno)); continue;