/* Config parser */
static void on_config(const char *group, const char *name, const char *key, const char *value)
{
+ util_config(group, name, key, value);
view_config(group, name, key, value);
chat_config(group, name, key, value);
}
/* Misc setup */
signal(SIGINT, on_sigint);
- /* Initialize */
- util_init();
- net_init();
-
+ /* Early setup */
args_setup(argc, argv);
conf_setup(".lamechatrc", on_config);
+ /* Initialize */
+ util_init();
+ net_init();
chat_init();
view_init();
- /* Mode main */
+ /* Main loop */
while (poll_run(1000)) {
view_sync();
conf_sync();
#include <time.h>
#include <unistd.h>
#include <regex.h>
+#include <wordexp.h>
#include <aspell.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
+#include "conf.h"
#include "view.h"
#include "util.h"
/* Static data */
static int epoll = 0;
-static FILE *debug_fd = NULL;
+static char *log_file = NULL;
+static FILE *log_fd = NULL;
static int running = 0;
static AspellSpeller *speller = NULL;
extern void view_message(const char *prefix, const char *msg);
/* Helper functions */
-static void message(FILE *output_fd, const char *prefix, const char *fmt, va_list ap)
+static void message(FILE *out_fd, const char *prefix, const char *fmt, va_list ap)
{
va_list tmp;
struct timespec ts;
unsigned int msec = ts.tv_nsec / 1000000;
/* Log to standard out */
- if (output_fd) {
+ if (out_fd) {
va_copy(tmp, ap);
- fprintf(output_fd, "%u.%03u: ", sec, msec);
- fprintf(output_fd, "%s: ", prefix);
- vfprintf(output_fd, fmt, tmp);
- fprintf(output_fd, "\n");
- fflush(output_fd);
+ fprintf(out_fd, "%u.%03u: ", sec, msec);
+ fprintf(out_fd, "%s: ", prefix);
+ vfprintf(out_fd, fmt, tmp);
+ fprintf(out_fd, "\n");
+ fflush(out_fd);
}
/* Log to debug file */
- if (debug_fd) {
+ if (log_fd) {
va_copy(tmp, ap);
- fprintf(debug_fd, "%u.%03u: ", sec, msec);
- fprintf(debug_fd, "%s: ", prefix);
- vfprintf(debug_fd, fmt, tmp);
- fprintf(debug_fd, "\n");
- fflush(debug_fd);
+ fprintf(log_fd, "%u.%03u: ", sec, msec);
+ fprintf(log_fd, "%s: ", prefix);
+ vfprintf(log_fd, fmt, tmp);
+ fprintf(log_fd, "\n");
+ fflush(log_fd);
}
/* Log to status bar */
/* Initialize */
void util_init(void)
{
- epoll = epoll_create(1);
- debug_fd = fopen("/tmp/lamechat.log", "w+");
- running = 1;
+ /* Init epol */
+ epoll = epoll_create(1);
+
+ /* Init log file */
+ if (log_file) {
+ wordexp_t wexp;
+ wordexp(log_file, &wexp, WRDE_NOCMD);
+ if (wexp.we_wordc > 1)
+ error("Found multiple log files: %s\n %s\n %s\n ...",
+ log_file, wexp.we_wordv[0], wexp.we_wordv[1]);
+ if (!(log_fd = fopen(wexp.we_wordv[0], "w+")))
+ error("Failed to create log file: %s", log_file);
+ wordfree(&wexp);
+ }
+ /* Init spelling */
AspellConfig *config = new_aspell_config();
AspellCanHaveError *status = new_aspell_speller(config);
if (aspell_error_number(status))
error("ASpell error: %s\n", aspell_error_message(status));
speller = to_aspell_speller(status);
+
+ /* Start running */
+ running = 1;
}
/* Cleanup */
void util_exit(void)
{
close(epoll);
- fclose(debug_fd);
+ if (log_fd)
+ fclose(log_fd);
+ free0(&log_file);
aspell_speller_save_all_word_lists(speller);
delete_aspell_speller(speller);
}
+/* Config */
+void util_config(const char *group, const char *name, const char *key, const char *value)
+{
+ if (match(group, "general")) {
+ if (match(key, "logfile"))
+ strset(&log_file, get_string(value));
+ }
+}
+
/* String functions */
void strsub(char *str, char find, char repl)
{