degesch: don't require configuration to run

Well, techincally.
This commit is contained in:
Přemysl Eric Janouch 2015-05-02 23:49:38 +02:00
parent c23898166c
commit 6d0fff6a71
1 changed files with 44 additions and 38 deletions

View File

@ -1813,7 +1813,7 @@ init_buffers (struct app_context *ctx)
global->name = xstrdup (PROGRAM_NAME); global->name = xstrdup (PROGRAM_NAME);
server->type = BUFFER_SERVER; server->type = BUFFER_SERVER;
server->name = xstrdup (get_config_string (ctx, "server.irc_host")); server->name = xstrdup ("server");
server->server = &ctx->server; server->server = &ctx->server;
LIST_APPEND_WITH_TAIL (ctx->buffers, ctx->buffers_tail, global); LIST_APPEND_WITH_TAIL (ctx->buffers, ctx->buffers_tail, global);
@ -4311,6 +4311,12 @@ irc_connect (struct server *s, struct error **e)
const char *irc_host = get_config_string (ctx, "server.irc_host"); const char *irc_host = get_config_string (ctx, "server.irc_host");
int64_t irc_port_int = get_config_integer (ctx, "server.irc_port"); int64_t irc_port_int = get_config_integer (ctx, "server.irc_port");
if (!get_config_string (ctx, "server.irc_host"))
{
error_set (e, "No hostname specified in configuration");
return false;
}
const char *socks_host = get_config_string (ctx, "server.socks_host"); const char *socks_host = get_config_string (ctx, "server.socks_host");
int64_t socks_port_int = get_config_integer (ctx, "server.socks_port"); int64_t socks_port_int = get_config_integer (ctx, "server.socks_port");
const char *socks_username = const char *socks_username =
@ -4464,6 +4470,7 @@ autofill_user_info (struct app_context *ctx, struct error **e)
if (!pwd) if (!pwd)
FAIL ("cannot retrieve user information: %s", strerror (errno)); FAIL ("cannot retrieve user information: %s", strerror (errno));
// FIXME: set_config_strings() writes errors on its own
if (!nickname) if (!nickname)
set_config_string (ctx, "server.nickname", pwd->pw_name); set_config_string (ctx, "server.nickname", pwd->pw_name);
if (!username) if (!username)
@ -4514,46 +4521,52 @@ read_file (const char *filename, struct str *output, struct error **e)
return false; return false;
} }
static bool static struct config_item_ *
load_configuration (struct app_context *ctx, struct error **e) load_configuration_file (const char *filename, struct error **e)
{ {
char *filename = resolve_config_filename (PROGRAM_NAME ".conf"); struct config_item_ *root = NULL;
if (!filename)
{
error_set (e, "cannot find configuration");
return false;
}
struct str data; struct str data;
str_init (&data); str_init (&data);
bool success = read_file (filename, &data, e); if (!read_file (filename, &data, e))
free (filename); goto end;
if (!success)
{
str_free (&data);
return false;
}
struct error *error = NULL; struct error *error = NULL;
struct config_item_ *root = if (!(root = config_item_parse (data.str, data.len, false, &error)))
config_item_parse (data.str, data.len, false, &error);
str_free (&data);
if (!root)
{ {
error_set (e, "configuration parse error: %s", error->message); error_set (e, "configuration parse error: %s", error->message);
error_free (error); error_free (error);
return false; }
end:
str_free (&data);
return root;
}
static void
load_configuration (struct app_context *ctx)
{
struct config_item_ *root = NULL;
struct error *e = NULL;
char *filename = resolve_config_filename (PROGRAM_NAME ".conf");
if (filename)
root = load_configuration_file (filename, &e);
else
print_status ("configuration file not found");
free (filename);
if (e)
{
print_error ("%s", e->message);
error_free (e);
e = NULL;
} }
config_load (&ctx->config, root); config_load (&ctx->config, root ? root : config_item_object ());
if (!autofill_user_info (ctx, &e))
if (!autofill_user_info (ctx, e))
return false;
if (!get_config_string (ctx, "server.irc_host"))
{ {
error_set (e, "no hostname specified in configuration"); print_error ("%s: %s", "failed to fill in user details", e->message);
return false; error_free (e);
} }
ctx->reconnect = ctx->reconnect =
@ -4562,7 +4575,6 @@ load_configuration (struct app_context *ctx, struct error **e)
get_config_boolean (ctx, "behaviour.isolate_buffers"); get_config_boolean (ctx, "behaviour.isolate_buffers");
ctx->reconnect_delay = ctx->reconnect_delay =
get_config_integer (ctx, "server.reconnect_delay"); get_config_integer (ctx, "server.reconnect_delay");
return true;
} }
static char * static char *
@ -4727,14 +4739,7 @@ main (int argc, char *argv[])
setup_signal_handlers (); setup_signal_handlers ();
register_config_modules (&ctx); register_config_modules (&ctx);
load_configuration (&ctx);
struct error *e = NULL;
if (!load_configuration (&ctx, &e))
{
print_error ("%s", e->message);
error_free (e);
exit (EXIT_FAILURE);
}
init_colors (&ctx); init_colors (&ctx);
init_poller_events (&ctx); init_poller_events (&ctx);
@ -4743,6 +4748,7 @@ main (int argc, char *argv[])
refresh_prompt (&ctx); refresh_prompt (&ctx);
// TODO: connect asynchronously (first step towards multiple servers) // TODO: connect asynchronously (first step towards multiple servers)
struct error *e = NULL;
if (!irc_connect (&ctx.server, &e)) if (!irc_connect (&ctx.server, &e))
{ {
buffer_send_error (&ctx, ctx.server.buffer, "%s", e->message); buffer_send_error (&ctx, ctx.server.buffer, "%s", e->message);