#include <string.h>
#include <time.h>
+#include <errno.h>
+#include <wordexp.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
#include "util.h"
#include "conf.h"
#include "chat.h"
int history;
/* Local data */
+static char *log_dir;
static buf_t msg_buf;
static const char *proto_map[] = {
[XMPP] "xmpp",
};
+/* Local functions */
+void strdcat(char *dst, const char *src, size_t n)
+{
+ int di = 0, si = 0;
+ while (di < (n-1) && dst[di])
+ di++;
+ while (di < (n-1) && src[si]) {
+ if (src[si] == '/')
+ dst[di++] = (si++,'_');
+ else
+ dst[di++] = src[si++];
+ }
+ dst[di] = '\0';
+}
+
+static void init_logs()
+{
+ if (!log_dir)
+ return;
+
+ wordexp_t wexp;
+ wordexp(log_dir, &wexp, WRDE_NOCMD);
+ if (wexp.we_wordc > 1)
+ error("Found multiple log dirs: %s\n %s\n %s\n ...",
+ log_dir, wexp.we_wordv[0], wexp.we_wordv[1]);
+ strset(&log_dir, wexp.we_wordv[0]);
+ if (mkdir(log_dir, 0755) && (errno != EEXIST))
+ error("Failed to create log dir: %s", log_dir);
+ wordfree(&wexp);
+}
+
+static void write_log(message_t *msg)
+{
+ static char log_path[4096];
+
+ channel_t *chan = msg->channel;
+ server_t *srv = chan->server;
+
+ if (!chan->log) {
+ /* Create server directory */
+ strncpy(log_path, log_dir, sizeof(log_path));
+ strncat(log_path, "/", sizeof(log_path));
+ strdcat(log_path, srv->name, sizeof(log_path));
+ if (!mkdir(log_path, 0755)) {
+ debug("Failed to create server log dir\n");
+ return;
+ }
+
+ /* Open log file directory */
+ strncat(log_path, "/", sizeof(log_path));
+ strdcat(log_path, chan->name, sizeof(log_path));
+ strncat(log_path, ".log", sizeof(log_path));
+ if (!(chan->log = fopen(log_path, "a+"))) {
+ debug("Cannot open log file: %s\n", log_path);
+ return;
+ }
+ }
+
+ time_t timep = msg->when;
+ struct tm *tm = gmtime(&timep);
+
+ fprintf(chan->log, "(%04d-%02d-%02d %02d:%02d:%02d) %s%s %s\n",
+ tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
+ tm->tm_hour, tm->tm_min, tm->tm_sec,
+ msg->from ?: "***",
+ msg->from ? ":" : "",
+ msg->text);
+ fflush(chan->log);
+}
+
/* Chat init */
void chat_init(void)
{
messages = (message_t*)msg_buf.data;
history = 0;
+ init_logs();
irc_init();
xmpp_init();
}
server_t *server = NULL;
channel_t *channel = NULL;
+ if (match(group, "general")) {
+ if (match(key, "logdir")) {
+ log_dir = strcopy(get_string(value));
+ }
+ }
+
if (match(group, "server")) {
server = find_server(name);
if (match(key, "")) {
msg->text = strcopy(text);
history++;
+ write_log(msg);
view_draw();
}