1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-09-22 19:45:54 -04:00

Move common chat logging code to log.c

This commit is contained in:
James Booth 2015-03-15 23:18:50 +00:00
parent 6f1119d225
commit 8944a3b5bb
6 changed files with 167 additions and 174 deletions

View File

@ -1956,43 +1956,19 @@ _cmd_execute_default(const char * inp)
if (encrypted != NULL) {
char *id = message_send_chat_encrypted(chatwin->barejid, encrypted);
otr_free_message(encrypted);
if (prefs_get_boolean(PREF_CHLOG)) {
const char *jid = jabber_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, chatwin->barejid, inp, PROF_OUT_LOG, NULL);
} else if (strcmp(pref_otr_log, "redact") == 0) {
chat_log_chat(jidp->barejid, chatwin->barejid, "[redacted]", PROF_OUT_LOG, NULL);
}
prefs_free_string(pref_otr_log);
jid_destroy(jidp);
}
chat_log_otr_msg_out(chatwin->barejid, inp);
ui_outgoing_chat_msg(chatwin->barejid, inp, id);
} else {
cons_show_error("Failed to send message.");
}
} else {
char *id = message_send_chat(chatwin->barejid, inp);
if (prefs_get_boolean(PREF_CHLOG)) {
const char *jid = jabber_get_fulljid();
Jid *jidp = jid_create(jid);
chat_log_chat(jidp->barejid, chatwin->barejid, inp, PROF_OUT_LOG, NULL);
jid_destroy(jidp);
}
chat_log_msg_out(chatwin->barejid, inp);
ui_outgoing_chat_msg(chatwin->barejid, inp, id);
}
#else
char *id = message_send_chat(chatwin->barejid, inp);
if (prefs_get_boolean(PREF_CHLOG)) {
const char *jid = jabber_get_fulljid();
Jid *jidp = jid_create(jid);
chat_log_chat(jidp->barejid, chatwin->barejid, inp, PROF_OUT_LOG, NULL);
jid_destroy(jidp);
}
chat_log_msg_out(chatwin->barejid, inp);
ui_outgoing_chat_msg(chatwin->barejid, inp, id);
#endif
}

View File

@ -1224,114 +1224,100 @@ cmd_msg(gchar **args, struct cmd_help_t help)
char *msg = args[1];
jabber_conn_status_t conn_status = jabber_get_connection_status();
win_type_t win_type = ui_current_win_type();
if (conn_status != JABBER_CONNECTED) {
cons_show("You are not currently connected.");
return TRUE;
}
if (win_type == WIN_MUC) {
ProfMucWin *mucwin = wins_get_current_muc();
if (muc_roster_contains_nick(mucwin->roomjid, usr)) {
GString *full_jid = g_string_new(mucwin->roomjid);
g_string_append(full_jid, "/");
g_string_append(full_jid, usr);
win_type_t win_type = ui_current_win_type();
switch (win_type) {
case WIN_MUC: {
ProfMucWin *mucwin = wins_get_current_muc();
if (muc_roster_contains_nick(mucwin->roomjid, usr)) {
GString *full_jid = g_string_new(mucwin->roomjid);
g_string_append(full_jid, "/");
g_string_append(full_jid, usr);
if (msg) {
message_send_private(full_jid->str, msg);
ui_outgoing_private_msg(full_jid->str, msg);
} else {
ui_new_private_win(full_jid->str);
}
g_string_free(full_jid, TRUE);
if (msg != NULL) {
message_send_private(full_jid->str, msg);
ui_outgoing_private_msg(full_jid->str, msg);
} else {
ui_new_private_win(full_jid->str);
ui_current_print_line("No such participant \"%s\" in room.", usr);
}
g_string_free(full_jid, TRUE);
} else {
ui_current_print_line("No such participant \"%s\" in room.", usr);
return TRUE;
}
default: {
char *barejid = roster_barejid_from_name(usr);
if (barejid == NULL) {
barejid = usr;
}
return TRUE;
} else {
// get barejid
char *barejid = roster_barejid_from_name(usr);
if (barejid == NULL) {
barejid = usr;
}
if (msg != NULL) {
if (msg) {
#ifdef HAVE_LIBOTR
if (otr_is_secure(barejid)) {
char *encrypted = otr_encrypt_message(barejid, msg);
if (encrypted != NULL) {
char *id = message_send_chat_encrypted(barejid, encrypted);
otr_free_message(encrypted);
ui_outgoing_chat_msg(barejid, msg, id);
if (((win_type == WIN_CHAT) || (win_type == WIN_CONSOLE)) && prefs_get_boolean(PREF_CHLOG)) {
const char *jid = jabber_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);
if (otr_is_secure(barejid)) {
char *encrypted = otr_encrypt_message(barejid, msg);
if (encrypted) {
char *id = message_send_chat_encrypted(barejid, encrypted);
otr_free_message(encrypted);
ui_outgoing_chat_msg(barejid, msg, id);
if (win_type == WIN_CHAT || win_type == WIN_CONSOLE) {
chat_log_otr_msg_out(barejid, msg);
}
prefs_free_string(pref_otr_log);
jid_destroy(jidp);
} else {
cons_show_error("Failed to encrypt and send message,");
}
} else {
cons_show_error("Failed to encrypt and send message,");
}
} else {
prof_otrpolicy_t policy = otr_get_policy(barejid);
char *id = NULL;
prof_otrpolicy_t policy = otr_get_policy(barejid);
char *id = NULL;
if (policy == PROF_OTRPOLICY_ALWAYS) {
cons_show_error("Failed to send message. Please check OTR policy");
return TRUE;
} else if (policy == PROF_OTRPOLICY_OPPORTUNISTIC) {
GString *otr_message = g_string_new(msg);
g_string_append(otr_message, OTRL_MESSAGE_TAG_BASE);
g_string_append(otr_message, OTRL_MESSAGE_TAG_V2);
id = message_send_chat_encrypted(barejid, otr_message->str);
if (policy == PROF_OTRPOLICY_ALWAYS) {
cons_show_error("Failed to send message. Please check OTR policy");
return TRUE;
} else if (policy == PROF_OTRPOLICY_OPPORTUNISTIC) {
GString *otr_message = g_string_new(msg);
g_string_append(otr_message, OTRL_MESSAGE_TAG_BASE);
g_string_append(otr_message, OTRL_MESSAGE_TAG_V2);
id = message_send_chat_encrypted(barejid, otr_message->str);
g_string_free(otr_message, TRUE);
} else {
id = message_send_chat(barejid, msg);
g_string_free(otr_message, TRUE);
} else {
id = message_send_chat(barejid, msg);
}
ui_outgoing_chat_msg(barejid, msg, id);
if (win_type == WIN_CHAT || win_type == WIN_CONSOLE) {
chat_log_msg_out(barejid, msg);
}
}
return TRUE;
#else
char *id = message_send_chat(barejid, msg);
ui_outgoing_chat_msg(barejid, msg, id);
if (((win_type == WIN_CHAT) || (win_type == WIN_CONSOLE)) && prefs_get_boolean(PREF_CHLOG)) {
const char *jid = jabber_get_fulljid();
Jid *jidp = jid_create(jid);
chat_log_chat(jidp->barejid, barejid, msg, PROF_OUT_LOG, NULL);
jid_destroy(jidp);
if (win_type == WIN_CHAT || win_type == WIN_CONSOLE) {
chat_log_msg_out(barejid, msg);
}
}
return TRUE;
#else
char *id = message_send_chat(barejid, msg);
ui_outgoing_chat_msg(barejid, msg, id);
if (((win_type == WIN_CHAT) || (win_type == WIN_CONSOLE)) && prefs_get_boolean(PREF_CHLOG)) {
const char *jid = jabber_get_fulljid();
Jid *jidp = jid_create(jid);
chat_log_chat(jidp->barejid, barejid, msg, PROF_OUT_LOG, NULL);
jid_destroy(jidp);
}
return TRUE;
return TRUE;
#endif
} else { // msg == NULL
ui_new_chat_win(barejid);
} else { // msg == NULL
ui_new_chat_win(barejid);
#ifdef HAVE_LIBOTR
if (otr_is_secure(barejid)) {
ui_gone_secure(barejid, otr_is_trusted(barejid));
}
if (otr_is_secure(barejid)) {
ui_gone_secure(barejid, otr_is_trusted(barejid));
}
#endif
return TRUE;
return TRUE;
}
}
}
}
@ -3076,43 +3062,19 @@ cmd_tiny(gchar **args, struct cmd_help_t help)
if (encrypted != NULL) {
char *id = message_send_chat_encrypted(chatwin->barejid, encrypted);
otr_free_message(encrypted);
if (prefs_get_boolean(PREF_CHLOG)) {
const char *jid = jabber_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, chatwin->barejid, tiny, PROF_OUT_LOG, NULL);
} else if (strcmp(pref_otr_log, "redact") == 0) {
chat_log_chat(jidp->barejid, chatwin->barejid, "[redacted]", PROF_OUT_LOG, NULL);
}
prefs_free_string(pref_otr_log);
jid_destroy(jidp);
}
chat_log_otr_msg_out(chatwin->barejid, tiny);
ui_outgoing_chat_msg(chatwin->barejid, tiny, id);
} else {
cons_show_error("Failed to send message.");
}
} else {
char *id = message_send_chat(chatwin->barejid, tiny);
if (prefs_get_boolean(PREF_CHLOG)) {
const char *jid = jabber_get_fulljid();
Jid *jidp = jid_create(jid);
chat_log_chat(jidp->barejid, chatwin->barejid, tiny, PROF_OUT_LOG, NULL);
jid_destroy(jidp);
}
chat_log_msg_out(chatwin->barejid, tiny);
ui_outgoing_chat_msg(chatwin->barejid, tiny, id);
}
#else
char *id = message_send_chat(chatwin->barejid, tiny);
if (prefs_get_boolean(PREF_CHLOG)) {
const char *jid = jabber_get_fulljid();
Jid *jidp = jid_create(jid);
chat_log_chat(jidp->barejid, chatwin->barejid, tiny, PROF_OUT_LOG, NULL);
jid_destroy(jidp);
}
chat_log_msg_out(chatwin->barejid, tiny);
ui_outgoing_chat_msg(chatwin->barejid, tiny, id);
#endif
} else if (win_type == WIN_PRIVATE) {

View File

@ -46,6 +46,7 @@
#include "common.h"
#include "config/preferences.h"
#include "xmpp/xmpp.h"
#define PROF "prof"
@ -66,7 +67,7 @@ struct dated_chat_log {
};
static gboolean _log_roll_needed(struct dated_chat_log *dated_log);
static struct dated_chat_log * _create_log(char *other, const char * const login);
static struct dated_chat_log * _create_log(const char * const other, const char * const login);
static struct dated_chat_log * _create_groupchat_log(char *room, const char * const login);
static void _free_chat_log(struct dated_chat_log *dated_log);
static gboolean _key_equals(void *key1, void *key2);
@ -78,6 +79,8 @@ static gchar * _get_chatlog_dir(void);
static gchar * _get_main_log_file(void);
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, GTimeVal *tv_stamp);
void
log_debug(const char * const msg, ...)
@ -249,8 +252,75 @@ groupchat_log_init(void)
}
void
chat_log_chat(const gchar * const login, gchar *other,
const gchar * const msg, chat_log_direction_t direction, GTimeVal *tv_stamp)
chat_log_msg_out(const char * const barejid, const char * const msg)
{
if (prefs_get_boolean(PREF_CHLOG)) {
const char *jid = jabber_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 = jabber_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_otr_msg_in(const char * const barejid, const char * const msg, gboolean was_decrypted)
{
if (prefs_get_boolean(PREF_CHLOG)) {
const char *jid = jabber_get_fulljid();
Jid *jidp = jid_create(jid);
char *pref_otr_log = prefs_get_string(PREF_OTR_LOG);
if (!was_decrypted || (strcmp(pref_otr_log, "on") == 0)) {
_chat_log_chat(jidp->barejid, barejid, msg, PROF_IN_LOG, NULL);
} else if (strcmp(pref_otr_log, "redact") == 0) {
_chat_log_chat(jidp->barejid, barejid, "[redacted]", PROF_IN_LOG, NULL);
}
prefs_free_string(pref_otr_log);
jid_destroy(jidp);
}
}
void
chat_log_msg_in(const char * const barejid, const char * const msg)
{
if (prefs_get_boolean(PREF_CHLOG)) {
const char *jid = jabber_get_fulljid();
Jid *jidp = jid_create(jid);
_chat_log_chat(jidp->barejid, barejid, msg, PROF_IN_LOG, NULL);
jid_destroy(jidp);
}
}
void
chat_log_msg_in_delayed(const char * const barejid, const char * msg, GTimeVal *tv_stamp)
{
if (prefs_get_boolean(PREF_CHLOG)) {
const char *jid = jabber_get_fulljid();
Jid *jidp = jid_create(jid);
_chat_log_chat(jidp->barejid, barejid, msg, PROF_IN_LOG, tv_stamp);
jid_destroy(jidp);
}
}
static void
_chat_log_chat(const char * const login, const char * const other,
const char * const msg, chat_log_direction_t direction, GTimeVal *tv_stamp)
{
struct dated_chat_log *dated_log = g_hash_table_lookup(logs, other);
@ -402,7 +472,7 @@ chat_log_close(void)
}
static struct dated_chat_log *
_create_log(char *other, const char * const login)
_create_log(const char * const other, const char * const login)
{
GDateTime *now = g_date_time_new_now_local();
char *filename = _get_log_filename(other, login, now, TRUE);

View File

@ -64,8 +64,14 @@ void log_msg(log_level_t level, const char * const area,
log_level_t log_level_from_string(char *log_level);
void chat_log_init(void);
void chat_log_chat(const gchar * const login, gchar *other,
const gchar * const msg, chat_log_direction_t direction, GTimeVal *tv_stamp);
void chat_log_msg_out(const char * const barejid, const char * const msg);
void chat_log_otr_msg_out(const char * const barejid, const char * const msg);
void chat_log_msg_in(const char * const barejid, const char * const msg);
void chat_log_msg_in_delayed(const char * const barejid, const char * msg, GTimeVal *tv_stamp);
void chat_log_otr_msg_in(const char * const barejid, const char * const msg, gboolean was_decrypted);
void chat_log_close(void);
GSList * chat_log_get_previous(const gchar * const login,
const gchar * const recipient);

View File

@ -355,32 +355,11 @@ handle_incoming_message(char *barejid, char *resource, char *message)
}
ui_incoming_msg(barejid, resource, newmessage, NULL);
if (prefs_get_boolean(PREF_CHLOG)) {
const char *jid = jabber_get_fulljid();
Jid *jidp = jid_create(jid);
char *pref_otr_log = prefs_get_string(PREF_OTR_LOG);
if (!was_decrypted || (strcmp(pref_otr_log, "on") == 0)) {
chat_log_chat(jidp->barejid, barejid, newmessage, PROF_IN_LOG, NULL);
} else if (strcmp(pref_otr_log, "redact") == 0) {
chat_log_chat(jidp->barejid, barejid, "[redacted]", PROF_IN_LOG, NULL);
}
prefs_free_string(pref_otr_log);
jid_destroy(jidp);
}
chat_log_otr_msg_in(barejid, newmessage, was_decrypted);
otr_free_message(newmessage);
#else
ui_incoming_msg(barejid, resource, message, NULL);
if (prefs_get_boolean(PREF_CHLOG)) {
const char *jid = jabber_get_fulljid();
Jid *jidp = jid_create(jid);
chat_log_chat(jidp->barejid, barejid, message, PROF_IN_LOG, NULL);
jid_destroy(jidp);
}
chat_log_msg_in(barejid, message);
#endif
}
@ -394,13 +373,7 @@ void
handle_delayed_message(char *barejid, char *message, GTimeVal tv_stamp)
{
ui_incoming_msg(barejid, NULL, message, &tv_stamp);
if (prefs_get_boolean(PREF_CHLOG)) {
const char *jid = jabber_get_fulljid();
Jid *jidp = jid_create(jid);
chat_log_chat(jidp->barejid, barejid, message, PROF_IN_LOG, &tv_stamp);
jid_destroy(jidp);
}
chat_log_msg_in_delayed(barejid, message, &tv_stamp);
}
void

View File

@ -50,8 +50,14 @@ log_level_t log_level_from_string(char *log_level)
}
void chat_log_init(void) {}
void chat_log_chat(const gchar * const login, gchar *other,
const gchar * const msg, chat_log_direction_t direction, GTimeVal *tv_stamp) {}
void chat_log_msg_out(const char * const barejid, const char * const msg) {}
void chat_log_otr_msg_out(const char * const barejid, const char * const msg) {}
void chat_log_msg_in(const char * const barejid, const char * const msg) {}
void chat_log_msg_in_delayed(const char * const barejid, const char * msg, GTimeVal *tv_stamp) {}
void chat_log_otr_msg_in(const char * const barejid, const char * const msg, gboolean was_decrypted) {}
void chat_log_close(void) {}
GSList * chat_log_get_previous(const gchar * const login,
const gchar * const recipient)