about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--chat.h12
-rw-r--r--command.c32
-rw-r--r--edit.c32
-rw-r--r--ui.c5
5 files changed, 72 insertions, 10 deletions
diff --git a/Makefile b/Makefile
index 63f719d..05f8bb8 100644
--- a/Makefile
+++ b/Makefile
@@ -6,6 +6,7 @@ CFLAGS += -std=c11 -Wall -Wextra -Wpedantic
 LDLIBS = -lcurses -lcrypto -ltls
 
 OBJS += chat.o
+OBJS += command.o
 OBJS += edit.o
 OBJS += handle.o
 OBJS += irc.o
diff --git a/chat.h b/chat.h
index c754357..c8b31c2 100644
--- a/chat.h
+++ b/chat.h
@@ -20,6 +20,7 @@
 #include <string.h>
 #include <sysexits.h>
 #include <time.h>
+#include <wchar.h>
 
 #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
 #define BIT(x) x##Bit, x = 1 << x##Bit, x##Bit_ = x##Bit
@@ -109,6 +110,7 @@ void ircFormat(const char *format, ...)
 	__attribute__((format(printf, 1, 2)));
 
 void handle(struct Message msg);
+void command(size_t id, char *input);
 
 enum Heat { Cold, Warm, Hot };
 void uiInit(void);
@@ -122,8 +124,14 @@ void uiFormat(
 	size_t id, enum Heat heat, const time_t *time, const char *format, ...
 ) __attribute__((format(printf, 4, 5)));
 
-const char *editHead(void);
-const char *editTail(void);
+enum Edit {
+	EditKill,
+	EditInsert,
+	EditEnter,
+};
+void edit(size_t id, enum Edit op, wchar_t ch);
+char *editHead(void);
+char *editTail(void);
 
 static inline enum Color hash(const char *str) {
 	if (*str == '~') str++;
diff --git a/command.c b/command.c
new file mode 100644
index 0000000..ab05587
--- /dev/null
+++ b/command.c
@@ -0,0 +1,32 @@
+/* Copyright (C) 2020  C. McEnroe <june@causal.agency>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "chat.h"
+
+void command(size_t id, char *input) {
+	ircFormat("PRIVMSG %s :%s\r\n", idNames[id], input);
+	struct Message msg = {
+		.nick = self.nick,
+		// TODO: .user,
+		.cmd = "PRIVMSG",
+		.params[0] = idNames[id],
+		.params[1] = input,
+	};
+	handle(msg);
+}
diff --git a/edit.c b/edit.c
index e142507..68593d1 100644
--- a/edit.c
+++ b/edit.c
@@ -23,22 +23,40 @@
 #include "chat.h"
 
 enum { Cap = 512 };
-static wchar_t buf[Cap] = L"foo\0033bar\3baz";
-static size_t len = 12;
-static size_t pos = 6;
+static wchar_t buf[Cap];
+static size_t len;
+static size_t pos;
 
-const char *editHead(void) {
+char *editHead(void) {
 	static char mbs[MB_LEN_MAX * Cap];
 	const wchar_t *ptr = buf;
-	size_t n = wcsnrtombs(mbs, &ptr, pos, sizeof(mbs), NULL);
+	size_t n = wcsnrtombs(mbs, &ptr, pos, sizeof(mbs) - 1, NULL);
 	assert(n != (size_t)-1);
+	mbs[n] = '\0';
 	return mbs;
 }
 
-const char *editTail(void) {
+char *editTail(void) {
 	static char mbs[MB_LEN_MAX * Cap];
 	const wchar_t *ptr = &buf[pos];
-	size_t n = wcsnrtombs(mbs, &ptr, len - pos, sizeof(mbs), NULL);
+	size_t n = wcsnrtombs(mbs, &ptr, len - pos, sizeof(mbs) - 1, NULL);
 	assert(n != (size_t)-1);
+	mbs[n] = '\0';
 	return mbs;
 }
+
+void edit(size_t id, enum Edit op, wchar_t ch) {
+	switch (op) {
+		break; case EditKill: len = pos = 0;
+		break; case EditInsert: {
+			if (len == Cap) break;
+			buf[pos++] = ch;
+			len++;
+		}
+		break; case EditEnter: {
+			pos = 0;
+			command(id, editTail());
+			len = 0;
+		}
+	}
+}
diff --git a/ui.c b/ui.c
index 8f813b4..b764f84 100644
--- a/ui.c
+++ b/ui.c
@@ -498,8 +498,11 @@ static void keyMeta(wchar_t ch) {
 }
 
 static void keyCtrl(wchar_t ch) {
+	size_t id = windows.active->id;
 	switch (ch) {
+		break; case L'J': edit(id, EditEnter, 0);
 		break; case L'L': clearok(curscr, true);
+		break; case L'U': edit(id, EditKill, 0);
 	}
 }
 
@@ -518,7 +521,7 @@ void uiRead(void) {
 		} else if (iswcntrl(ch)) {
 			keyCtrl(ch ^ L'@');
 		} else {
-			// TODO: Insert.
+			edit(windows.active->id, EditInsert, ch);
 		}
 		meta = false;
 	}