/* * log.c * * Copyright (C) 2012 - 2018 James Booth * * This file is part of Profanity. * * Profanity is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Profanity is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Profanity. If not, see . * * In addition, as a special exception, the copyright holders give permission to * link the code of portions of this program with the OpenSSL library under * certain conditions as described in each individual source file, and * distribute linked combinations including the two. * * You must obey the GNU General Public License in all respects for all of the * code used other than OpenSSL. If you modify file(s) with this exception, you * may extend this exception to your version of the file(s), but you are not * obligated to do so. If you do not wish to do so, delete this exception * statement from your version. If you delete this exception statement from all * source files in the program, then also delete it here. * */ #include #include #include #include #include #include #include #include "glib.h" #include "glib/gstdio.h" #include "log.h" #include "common.h" #include "config/files.h" #include "config/preferences.h" #include "xmpp/xmpp.h" #define PROF "prof" static FILE *logp; GString *mainlogfile; static GTimeZone *tz; static GDateTime *dt; static log_level_t level_filter; static GHashTable *logs; static GHashTable *groupchat_logs; static GDateTime *session_started; enum { STDERR_BUFSIZE = 4000, STDERR_RETRY_NR = 5, }; static int stderr_inited; static log_level_t stderr_level; static int stderr_pipe[2]; static char *stderr_buf; static GString *stderr_msg; struct dated_chat_log { gchar *filename; GDateTime *date; }; static gboolean _log_roll_needed(struct dated_chat_log *dated_log); static struct dated_chat_log* _create_log(const char *const other, const char *const login); static struct dated_chat_log* _create_groupchat_log(const char *const room, const char *const login); static void _free_chat_log(struct dated_chat_log *dated_log); static gboolean _key_equals(void *key1, void *key2); static char* _get_log_filename(const char *const other, const char *const login, GDateTime *dt, gboolean create); static char* _get_groupchat_log_filename(const char *const room, const char *const login, GDateTime *dt, gboolean create); static void _rotate_log_file(void); static char* _log_string_from_level(log_level_t level); static void _chat_log_chat(const char *const login, const char *const other, const gchar *const msg, chat_log_direction_t direction, GDateTime *timestamp); void log_debug(const char *const msg, ...) { va_list arg; va_start(arg, msg); GString *fmt_msg = g_string_new(NULL); g_string_vprintf(fmt_msg, msg, arg); log_msg(PROF_LEVEL_DEBUG, PROF, fmt_msg->str); g_string_free(fmt_msg, TRUE); va_end(arg); } void log_info(const char *const msg, ...) { va_list arg; va_start(arg, msg); GString *fmt_msg = g_string_new(NULL); g_string_vprintf(fmt_msg, msg, arg); log_msg(PROF_LEVEL_INFO, PROF, fmt_msg->str); g_string_free(fmt_msg, TRUE); va_end(arg); } void log_warning(const char *const msg, ...) { va_list arg; va_start(arg, msg); GString *fmt_msg = g_string_new(NULL); g_string_vprintf(fmt_msg, msg, arg); log_msg(PROF_LEVEL_WARN, PROF, fmt_msg->str); g_string_free(fmt_msg, TRUE); va_end(arg); } void log_error(const char *const msg, ...) { va_list arg; va_start(arg, msg); GString *fmt_msg = g_string_new(NULL); g_string_vprintf(fmt_msg, msg, arg); log_msg(PROF_LEVEL_ERROR, PROF, fmt_msg->str); g_string_free(fmt_msg, TRUE); va_end(arg); } void log_init(log_level_t filter) { level_filter = filter; tz = g_time_zone_new_local(); char *log_file = files_get_log_file(); logp = fopen(log_file, "a"); g_chmod(log_file, S_IRUSR | S_IWUSR); mainlogfile = g_string_new(log_file); free(log_file); } void log_reinit(void) { log_close(); log_init(level_filter); } char* get_log_file_location(void) { return mainlogfile->str; } log_level_t log_get_filter(void) { return level_filter; } void log_close(void) { g_string_free(mainlogfile, TRUE); g_time_zone_unref(tz); if (logp) { fclose(logp); } } void log_msg(log_level_t level, const char *const area, const char *const msg) { if (level >= level_filter && logp) { dt = g_date_time_new_now(tz); char *level_str = _log_string_from_level(level); gchar *date_fmt = g_date_time_format(dt, "%d/%m/%Y %H:%M:%S"); fprintf(logp, "%s: %s: %s: %s\n", date_fmt, area, level_str, msg); g_date_time_unref(dt); fflush(logp); g_free(date_fmt); if (prefs_get_boolean(PREF_LOG_ROTATE)) { long result = ftell(logp); if (result != -1 && result >= prefs_get_max_log_size()) { _rotate_log_file(); } } } } log_level_t log_level_from_string(char *log_level) { assert(log_level != NULL); if (strcmp(log_level, "DEBUG") == 0) { return PROF_LEVEL_DEBUG; } else if (strcmp(log_level, "INFO") == 0) { return PROF_LEVEL_INFO; } else if (strcmp(log_level, "WARN") == 0) { return PROF_LEVEL_WARN; } else if (strcmp(log_level, "ERROR") == 0) { return PROF_LEVEL_ERROR; } else { // default to info return PROF_LEVEL_INFO; } } static void _rotate_log_file(void) { char *log_file = files_get_log_file(); size_t len = strlen(log_file); char *log_file_new = malloc(len + 3); memcpy(log_file_new, log_file, len); log_file_new[len] = '.'; log_file_new[len+1] = '1'; log_file_new[len+2] = 0; log_close(); rename(log_file, log_file_new); log_init(log_get_filter()); free(log_file_new); free(log_file); log_info("Log has been rotated"); } void chat_log_init(void) { session_started = g_date_time_new_now_local(); log_info("Initialising chat logs"); logs = g_hash_table_new_full(g_str_hash, (GEqualFunc) _key_equals, free, (GDestroyNotify)_free_chat_log); } void groupchat_log_init(void) { log_info("Initialising groupchat logs"); groupchat_logs = g_hash_table_new_full(g_str_hash, (GEqualFunc) _key_equals, free, (GDestroyNotify)_free_chat_log); } void chat_log_msg_out(const char *const barejid, const char *const msg) { if (prefs_get_boolean(PREF_CHLOG)) { const char *jid = connection_get_fulljid(); Jid *jidp = jid_create(jid); _chat_log_chat(jidp->barejid, barejid, msg, PROF_OUT_LOG, NULL); jid_destroy(jidp); } } void chat_log_otr_msg_out(const char *const barejid, const char *const msg) { if (prefs_get_boolean(PREF_CHLOG)) { const char *jid = connection_get_fulljid(); Jid *jidp = jid_create(jid); char *pref_otr_log = prefs_get_string(PREF_OTR_LOG); if (strcmp(pref_otr_log, "on") == 0) { _chat_log_chat(jidp->barejid, barejid, msg, PROF_OUT_LOG, NULL); } else if (strcmp(pref_otr_log, "redact") == 0) { _chat_log_chat(jidp->barejid, barejid, "[redacted]", PROF_OUT_LOG, NULL); } prefs_free_string(pref_otr_log); jid_destroy(jidp); } } void chat_log_pgp_msg_out(const char *const barejid, const char *const msg) { if (prefs_get_boolean
/*
 * buffer.c
 *
 * Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
 *
 * This file is part of Profanity.
 *
 * Profanity is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Profanity is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Profanity.  If not, see <https://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give permission to
 * link the code of portions of this program with the OpenSSL library under
 * certain conditions as described in each individual source file, and
 * distribute linked combinations including the two.
 *
 * You must obey the GNU General Public License in all respects for all of the
 * code used other than OpenSSL. If you modify file(s) with this exception, you
 * may extend this exception to your version of the file(s), but you are not
 * obligated to do so. If you do not wish to do so, delete this exception
 * statement from your version. If you delete this exception statement from all
 * source files in the program, then also delete it here.
 *
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>

#include <glib.h>
#ifdef HAVE_NCURSESW_NCURSES_H
#include <ncursesw/ncurses.h>
#elif HAVE_NCURSES_H
#include <ncurses.h>
#endif

#include "ui/window.h"
#include "ui/buffer.h"

#define BUFF_SIZE 1200

struct prof_buff_t {
    GSList *entries;
};

static void _free_entry(ProfBuffEntry *entry);

ProfBuff
buffer_create(void)
{
    ProfBuff new_buff = malloc(sizeof(struct prof_buff_t));
    new_buff->entries = NULL;
    return new_buff;
}

int
buffer_size(ProfBuff buffer)
{
    return g_slist_length(buffer->entries);
}

void
buffer_free(ProfBuff buffer)
{
    g_slist_free_full(buffer->entries, (GDestroyNotify)_free_entry);
    free(buffer);
}

void
buffer_append(ProfBuff buffer, const char show_char, int pad_indent, GDateTime *time,
    int flags, theme_item_t theme_item, const char *const from, const