changeset 979:489d80468925

irccdctl: add hook commands
author David Demelier <markand@malikania.fr>
date Tue, 09 Feb 2021 21:10:00 +0100
parents 74af4d672842
children 3afd375f308b
files irccd/peer.c irccdctl/main.c lib/irccd/server.c
diffstat 3 files changed, 71 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/irccd/peer.c	Tue Feb 09 21:10:00 2021 +0100
+++ b/irccd/peer.c	Tue Feb 09 21:10:00 2021 +0100
@@ -134,8 +134,8 @@
 		return EINVAL;
 	if (!(plg = require_plugin(p, args[0])))
 		return 0;
-
-	fp = fmemopen(out, sizeof (out) - 1, "w");
+	if (!(fp = fmemopen(out, sizeof (out) - 1, "w")))
+		return errno;
 
 	if (argsz == 3) {
 		set(plg, args[1], args[2]);
@@ -241,7 +241,12 @@
 static int
 cmd_hook_remove(struct peer *p, char *line)
 {
-	irc_bot_hook_remove(line);
+	const char *args[1] = {0};
+
+	if (parse(line, args, 1) != 1)
+		return EINVAL;
+
+	irc_bot_hook_remove(args[0]);
 
 	return ok(p);
 }
@@ -315,7 +320,8 @@
 	FILE *fp;
 	char out[IRC_BUF_LEN];
 
-	fp = fmemopen(out, sizeof (out) - 1, "w");
+	if (!(fp = fmemopen(out, sizeof (out) - 1, "w")))
+		return errno;
 
 	fprintf(fp, "OK ");
 
@@ -739,8 +745,8 @@
 		return EINVAL;
 	if (!(s = require_server(p, args[0])))
 		return 0;
-
-	fp = fmemopen(out, sizeof (out) - 1, "w");
+	if (!(fp = fmemopen(out, sizeof (out) - 1, "w")))
+		return errno;
 
 	fprintf(fp, "OK %s\n", s->name);
 	fprintf(fp, "%s %u%s\n", s->conn.hostname, s->conn.port,
@@ -826,11 +832,11 @@
 	(void)line;
 
 	struct irc_server *s;
+	char out[IRC_BUF_LEN];
 	FILE *fp;
-	char *out;
-	size_t outsz;
 
-	fp = open_memstream(&out, &outsz);
+	if (!(fp = fmemopen(out, sizeof (out), "w")))
+		return error(p, "%s", strerror(errno));
 
 	fprintf(fp, "OK ");
 
@@ -843,7 +849,6 @@
 
 	fclose(fp);
 	peer_send(p, out);
-	free(out);
 
 	return 0;
 }
--- a/irccdctl/main.c	Tue Feb 09 21:10:00 2021 +0100
+++ b/irccdctl/main.c	Tue Feb 09 21:10:00 2021 +0100
@@ -387,7 +387,52 @@
 			printf("%-16s: %s\n", line, p + 1);
 		}
 	}
+}
 
+static void
+response_list(const char *cmd)
+{
+	char *list;
+
+	req(cmd);
+
+	if (strncmp(list = poll(), "OK ", 3) != 0)
+		errx(1, "failed to retrieve plugin list");
+
+	list += 3;
+
+	for (char *p; (p = strchr(list, ' ')); )
+		*p = '\n';
+
+	if (*list)
+		puts(list);
+}
+
+static void
+cmd_hook_add(int argc, char **argv)
+{
+	(void)argc;
+
+	req("HOOK-ADD %s %s", argv[0], argv[1]);
+	ok();
+}
+
+static void
+cmd_hook_list(int argc, char **argv)
+{
+	(void)argc;
+	(void)argv;
+
+	response_list("HOOK-LIST");
+}
+
+static void
+cmd_hook_remove(int argc, char **argv)
+{
+	(void)argc;
+
+	req("HOOK-REMOVE %s", argv[0]);
+	ok();
 }
 
 static void
@@ -430,19 +475,7 @@
 	(void)argc;
 	(void)argv;
 
-	char *list;
-
-	req("PLUGIN-LIST");
-
-	if (strncmp(list = poll(), "OK ", 3) != 0)
-		errx(1, "failed to retrieve plugin list");
-
-	list += 3;
-
-	for (char *p; (p = strchr(list, ' ')); )
-		*p = '\n';
-
-	puts(list);
+	response_list("PLUGIN-LIST");
 }
 
 static void
@@ -682,21 +715,7 @@
 	(void)argc;
 	(void)argv;
 
-	char *list;
-
-	req("SERVER-LIST");
-
-	if (strncmp(list = poll(), "OK ", 3) != 0)
-		errx(1, "failed to retrieve server list");
-
-	/* Skip "OK " */
-	list += 3;
-
-	/* Since list is separated by spaces, just convert them to \n */
-	for (char *p; (p = strchr(list, ' ')); )
-		*p = '\n';
-
-	puts(list);
+	response_list("SERVER-LIST");
 }
 
 static void
@@ -797,6 +816,9 @@
 	void (*exec)(int, char **);
 } cmds[] = {
 	/* name                 min     max     exec                   */
+	{ "hook-add",           2,      2,      cmd_hook_add            },
+	{ "hook-list",          0,      0,      cmd_hook_list           },
+	{ "hook-remove",        1,      1,      cmd_hook_remove         },
 	{ "plugin-config",      1,      3,      cmd_plugin_config       },
 	{ "plugin-info",        1,      1,      cmd_plugin_info         },
 	{ "plugin-list",        0,      0,      cmd_plugin_list         },
--- a/lib/irccd/server.c	Tue Feb 09 21:10:00 2021 +0100
+++ b/lib/irccd/server.c	Tue Feb 09 21:10:00 2021 +0100
@@ -16,8 +16,11 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <compat.h>
+
 #include <assert.h>
 #include <errno.h>
+#include <err.h>
 #include <poll.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -441,7 +444,9 @@
 
 	/* Construct a string list for every user in the channel. */
 	ch = irc_server_find(s, ev->names.channel);
-	fp = open_memstream(&ev->names.names, &length);
+
+	if (!(fp = open_memstream(&ev->names.names, &length)))
+		err(1, "open_memstream");
 
 	LIST_FOREACH(u, &ch->users, link) {
 		if (u->symbol)