Load X/Open message catalogs

This is going to enable making changes to ERR and RPL messages without
modifying the source code.

Localized messages could be interesting. :)
This commit is contained in:
Přemysl Eric Janouch 2014-07-13 23:47:29 +02:00
parent aaedbf93f1
commit f52fab9448
1 changed files with 26 additions and 1 deletions

View File

@ -22,6 +22,7 @@
#define PROGRAM_VERSION "alpha" #define PROGRAM_VERSION "alpha"
#include "common.c" #include "common.c"
#include <nl_types.h>
// --- Configuration (application-specific) ------------------------------------ // --- Configuration (application-specific) ------------------------------------
@ -29,6 +30,7 @@ static struct config_item g_config_table[] =
{ {
{ "server_name", NULL, "Server name" }, { "server_name", NULL, "Server name" },
{ "motd", NULL, "MOTD filename" }, { "motd", NULL, "MOTD filename" },
{ "catalog", NULL, "catgets localization catalog" },
{ "bind_host", NULL, "Address of the IRC server" }, { "bind_host", NULL, "Address of the IRC server" },
{ "bind_port", "6667", "Port of the IRC server" }, { "bind_port", "6667", "Port of the IRC server" },
@ -325,6 +327,7 @@ struct server_context
bool polling; ///< The event loop is running bool polling; ///< The event loop is running
struct str_vector motd; ///< MOTD (none if empty) struct str_vector motd; ///< MOTD (none if empty)
nl_catd catalog; ///< Message catalog for server msgs
}; };
static void static void
@ -347,6 +350,7 @@ server_context_init (struct server_context *self)
self->polling = false; self->polling = false;
str_vector_init (&self->motd); str_vector_init (&self->motd);
self->catalog = (nl_catd) -1;
} }
static void static void
@ -374,6 +378,8 @@ server_context_free (struct server_context *self)
poller_free (&self->poller); poller_free (&self->poller);
str_vector_free (&self->motd); str_vector_free (&self->motd);
if (self->catalog != (nl_catd) -1)
catclose (self->catalog);
} }
// --- Main program ------------------------------------------------------------ // --- Main program ------------------------------------------------------------
@ -794,6 +800,24 @@ error_ssl_1:
return false; return false;
} }
static bool
irc_initialize_catalog (struct server_context *ctx, struct error **e)
{
hard_assert (ctx->catalog == (nl_catd) -1);
const char *catalog = str_map_find (&ctx->config, "catalog");
if (!catalog)
return true;
ctx->catalog = catopen (catalog, NL_CAT_LOCALE);
if (ctx->catalog == (nl_catd) -1)
{
error_set (e, IO_ERROR, IO_ERROR_FAILED, "%s: %s",
"failed reading the message catalog file", strerror (errno));
return false;
}
return true;
}
static bool static bool
irc_initialize_motd (struct server_context *ctx, struct error **e) irc_initialize_motd (struct server_context *ctx, struct error **e)
{ {
@ -1046,7 +1070,8 @@ main (int argc, char *argv[])
if (!irc_initialize_ssl (&ctx)) if (!irc_initialize_ssl (&ctx))
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
if (!irc_listen (&ctx, &e)) if (!irc_initialize_catalog (&ctx, &e)
|| !irc_listen (&ctx, &e))
{ {
print_error ("%s", e->message); print_error ("%s", e->message);
error_free (e); error_free (e);