]> Pileus Git - ~andy/lamechat/commitdiff
Add log files
authorAndy Spencer <andy753421@gmail.com>
Wed, 18 Oct 2017 07:52:44 +0000 (07:52 +0000)
committerAndy Spencer <andy753421@gmail.com>
Wed, 18 Oct 2017 07:52:44 +0000 (07:52 +0000)
chat.c
chat.h

diff --git a/chat.c b/chat.c
index 83fd2c9584d1cc1155206669151b332a660d108c..d59abdad57f965e08642f76c7720538b9cd6cb8f 100644 (file)
--- a/chat.c
+++ b/chat.c
 #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"
@@ -33,6 +38,7 @@ message_t *messages;
 int        history;
 
 /* Local data */
+static char *log_dir;
 static buf_t msg_buf;
 
 static const char *proto_map[] = {
@@ -40,12 +46,83 @@ 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();
 }
@@ -56,6 +133,12 @@ void chat_config(const char *group, const char *name, const char *key, const cha
        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, "")) {
@@ -121,6 +204,7 @@ void chat_recv(channel_t *channel, const char *from, const char *text)
        msg->text = strcopy(text);
 
        history++;
+       write_log(msg);
        view_draw();
 }
 
diff --git a/chat.h b/chat.h
index 38c6f58f1de59dcce13058487a96329a72c93c29..2a6f671e99c001cddcbd5ec3d9dabfc376e8d032 100644 (file)
--- a/chat.h
+++ b/chat.h
@@ -35,6 +35,7 @@ typedef struct server_t {
 typedef struct channel_t {
        server_t   *server;
        char       *name;
+       FILE       *log;
        channel_t  *next;
 } channel_t;