summary refs log tree commit diff stats
diff options
context:
space:
mode:
authoraabacchus <ben@bvnf.space>2023-05-27 19:38:30 +0100
committeraabacchus <ben@bvnf.space>2023-05-27 19:38:30 +0100
commit45f87c7ab21048ae642cc5c53565386142798e25 (patch)
treec6add5d43103a03fe6de0c36c3e2d4f07c9d40be
parent50427b7b632ebb13215bd9f1f4cfee1b2202cdf2 (diff)
downloadcbot-main.tar.gz
parse_cgi.c write to socket HEAD main
-rw-r--r--parse_cgi.c39
1 files changed, 35 insertions, 4 deletions
diff --git a/parse_cgi.c b/parse_cgi.c
index fb345a0..5e92359 100644
--- a/parse_cgi.c
+++ b/parse_cgi.c
@@ -3,14 +3,44 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/socket.h>
+#include <sys/un.h>
 #include <unistd.h>
 
 #include "cJSON.h"
 
+#define SOCK_PATH "/tmp/kissbot"
+
 #ifndef __OpenBSD__
 #define pledge(a, b) (0)
 #endif
 
+int
+sock_write(char *msg, ...) {
+    struct sockaddr_un remote = {.sun_family = AF_UNIX};
+    int s = socket(AF_UNIX, SOCK_STREAM, 0);
+    if (s == -1)
+        err(1, "socket");
+
+    strlcpy(remote.sun_path, SOCK_PATH,
+        sizeof(remote.sun_path)-1);
+    if (connect(s, (struct sockaddr *)&remote,
+        sizeof(SOCK_PATH) + sizeof(remote.sun_family)) == -1)
+        err(1, "connect");
+
+    va_list ap;
+    va_start(ap, msg);
+    if (vdprintf(s, msg, ap) < 0) {
+        va_end(ap);
+        close(s);
+        err(1, "send");
+    }
+    va_end(ap);
+
+    close(s);
+    return 0;
+}
+
 int main(int argc, char **argv) {
     if (pledge("stdio rpath", NULL) == -1) {
         perror("pledge");
@@ -56,10 +86,11 @@ int main(int argc, char **argv) {
 
 	    printf("<h2>stdin</h2>\n<pre>");
 
-	    if (cJSON_IsString(name))
-	    	printf("%s commited %.10s: %s\n", name->valuestring, id->valuestring, msg->valuestring);
-	    else
-		printf("Failed to find commit info.\n");
+	    if (cJSON_IsString(name)) {
+	        printf("%s commited %.10s: %s\n", name->valuestring, id->valuestring, msg->valuestring);
+	        sock_write("%s commited %.10s: %s\n", name->valuestring, id->valuestring, msg->valuestring);
+	    } else
+	        printf("Failed to find commit info.\n");
 
 	    cJSON_Delete(json);
 	    printf("</pre>\n");