From 6d0fff6a71ab6d5d495d612a54f820c1421201e1 Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Sat, 2 May 2015 23:49:38 +0200
Subject: degesch: don't require configuration to run
Well, techincally.
---
degesch.c | 78 ++++++++++++++++++++++++++++++++++-----------------------------
1 file changed, 42 insertions(+), 36 deletions(-)
diff --git a/degesch.c b/degesch.c
index 1235b5c..ffb8625 100644
--- a/degesch.c
+++ b/degesch.c
@@ -1813,7 +1813,7 @@ init_buffers (struct app_context *ctx)
global->name = xstrdup (PROGRAM_NAME);
server->type = BUFFER_SERVER;
- server->name = xstrdup (get_config_string (ctx, "server.irc_host"));
+ server->name = xstrdup ("server");
server->server = &ctx->server;
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");
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");
int64_t socks_port_int = get_config_integer (ctx, "server.socks_port");
const char *socks_username =
@@ -4464,6 +4470,7 @@ autofill_user_info (struct app_context *ctx, struct error **e)
if (!pwd)
FAIL ("cannot retrieve user information: %s", strerror (errno));
+ // FIXME: set_config_strings() writes errors on its own
if (!nickname)
set_config_string (ctx, "server.nickname", pwd->pw_name);
if (!username)
@@ -4514,46 +4521,52 @@ read_file (const char *filename, struct str *output, struct error **e)
return false;
}
-static bool
-load_configuration (struct app_context *ctx, struct error **e)
+static struct config_item_ *
+load_configuration_file (const char *filename, struct error **e)
{
- char *filename = resolve_config_filename (PROGRAM_NAME ".conf");
- if (!filename)
- {
- error_set (e, "cannot find configuration");
- return false;
- }
+ struct config_item_ *root = NULL;
struct str data;
str_init (&data);
- bool success = read_file (filename, &data, e);
- free (filename);
- if (!success)
- {
- str_free (&data);
- return false;
- }
+ if (!read_file (filename, &data, e))
+ goto end;
struct error *error = NULL;
- struct config_item_ *root =
- config_item_parse (data.str, data.len, false, &error);
- str_free (&data);
- if (!root)
+ if (!(root = config_item_parse (data.str, data.len, false, &error)))
{
error_set (e, "configuration parse error: %s", error->message);
error_free (error);
- return false;
}
+end:
+ str_free (&data);
+ return root;
+}
- config_load (&ctx->config, root);
+static void
+load_configuration (struct app_context *ctx)
+{
+ struct config_item_ *root = NULL;
+ struct error *e = NULL;
- if (!autofill_user_info (ctx, e))
- return false;
+ 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 (!get_config_string (ctx, "server.irc_host"))
+ if (e)
{
- error_set (e, "no hostname specified in configuration");
- return false;
+ print_error ("%s", e->message);
+ error_free (e);
+ e = NULL;
+ }
+
+ config_load (&ctx->config, root ? root : config_item_object ());
+ if (!autofill_user_info (ctx, &e))
+ {
+ print_error ("%s: %s", "failed to fill in user details", e->message);
+ error_free (e);
}
ctx->reconnect =
@@ -4562,7 +4575,6 @@ load_configuration (struct app_context *ctx, struct error **e)
get_config_boolean (ctx, "behaviour.isolate_buffers");
ctx->reconnect_delay =
get_config_integer (ctx, "server.reconnect_delay");
- return true;
}
static char *
@@ -4727,14 +4739,7 @@ main (int argc, char *argv[])
setup_signal_handlers ();
register_config_modules (&ctx);
-
- struct error *e = NULL;
- if (!load_configuration (&ctx, &e))
- {
- print_error ("%s", e->message);
- error_free (e);
- exit (EXIT_FAILURE);
- }
+ load_configuration (&ctx);
init_colors (&ctx);
init_poller_events (&ctx);
@@ -4743,6 +4748,7 @@ main (int argc, char *argv[])
refresh_prompt (&ctx);
// TODO: connect asynchronously (first step towards multiple servers)
+ struct error *e = NULL;
if (!irc_connect (&ctx.server, &e))
{
buffer_send_error (&ctx, ctx.server.buffer, "%s", e->message);
--
cgit v1.2.3-70-g09d2