Load an MOTD on start

This commit is contained in:
Přemysl Eric Janouch 2014-07-13 21:27:38 +02:00
parent 03ec980f26
commit aaedbf93f1
2 changed files with 43 additions and 0 deletions

View File

@ -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)
{

View File

@ -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);