From aaedbf93f1a8e6da3fbd2ce7433888d45c9efe5f Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Sun, 13 Jul 2014 21:27:38 +0200
Subject: Load an MOTD on start
---
src/common.c | 7 +++++++
src/kike.c | 36 ++++++++++++++++++++++++++++++++++++
2 files changed, 43 insertions(+)
diff --git a/src/common.c b/src/common.c
index 8ec6646..f35493f 100644
--- a/src/common.c
+++ b/src/common.c
@@ -277,6 +277,13 @@ str_vector_free (struct str_vector *self)
self->vector = NULL;
}
+static void
+str_vector_reset (struct str_vector *self)
+{
+ str_vector_free (self);
+ str_vector_init (self);
+}
+
static void
str_vector_add_owned (struct str_vector *self, char *s)
{
diff --git a/src/kike.c b/src/kike.c
index 9734f2b..60774cf 100644
--- a/src/kike.c
+++ b/src/kike.c
@@ -28,6 +28,8 @@
static struct config_item g_config_table[] =
{
{ "server_name", NULL, "Server name" },
+ { "motd", NULL, "MOTD filename" },
+
{ "bind_host", NULL, "Address of the IRC server" },
{ "bind_port", "6667", "Port of the IRC server" },
{ "ssl_cert", NULL, "Server SSL certificate (PEM)" },
@@ -321,6 +323,8 @@ struct server_context
struct poller poller; ///< Manages polled description
bool quitting; ///< User requested quitting
bool polling; ///< The event loop is running
+
+ struct str_vector motd; ///< MOTD (none if empty)
};
static void
@@ -341,6 +345,8 @@ server_context_init (struct server_context *self)
poller_init (&self->poller);
self->quitting = false;
self->polling = false;
+
+ str_vector_init (&self->motd);
}
static void
@@ -366,6 +372,8 @@ server_context_free (struct server_context *self)
str_map_free (&self->users);
str_map_free (&self->channels);
poller_free (&self->poller);
+
+ str_vector_free (&self->motd);
}
// --- Main program ------------------------------------------------------------
@@ -786,6 +794,32 @@ error_ssl_1:
return false;
}
+static bool
+irc_initialize_motd (struct server_context *ctx, struct error **e)
+{
+ hard_assert (ctx->motd.len == 0);
+ const char *motd = str_map_find (&ctx->config, "motd");
+ if (!motd)
+ return true;
+
+ FILE *fp = fopen (motd, "r");
+ if (!fp)
+ {
+ error_set (e, IO_ERROR, IO_ERROR_FAILED,
+ "%s: %s", "failed reading the MOTD file", strerror (errno));
+ return false;
+ }
+
+ struct str line;
+ str_init (&line);
+ while (read_line (fp, &line))
+ str_vector_add_owned (&ctx->motd, str_steal (&line));
+ str_free (&line);
+
+ fclose (fp);
+ return true;
+}
+
static bool
irc_initialize_server_name (struct server_context *ctx, struct error **e)
{
@@ -834,6 +868,8 @@ irc_listen (struct server_context *ctx, struct error **e)
if (!irc_initialize_server_name (ctx, e))
return false;
+ if (!irc_initialize_motd (ctx, e))
+ return false;
struct addrinfo gai_hints, *gai_result, *gai_iter;
memset (&gai_hints, 0, sizeof gai_hints);
--
cgit v1.2.3-70-g09d2