diff --git a/src/database.c b/src/database.c index c48f2f95..e3026ea1 100644 --- a/src/database.c +++ b/src/database.c @@ -45,7 +45,7 @@ static sqlite3 *g_chatlog_database; -static void _add_to_db(ProfMessage *message, const char * const type, const Jid * const from_jid, const Jid * const to_jid); +static void _add_to_db(ProfMessage *message, char *type, const Jid * const from_jid, const Jid * const to_jid); static char* _get_db_filename(ProfAccount *account); static char* @@ -156,34 +156,19 @@ log_database_close(void) } } -static void -_log_database_add_incoming(ProfMessage *message, const char * const type) +void +log_database_add_incoming(ProfMessage *message) { const char *jid = connection_get_fulljid(); Jid *myjid = jid_create(jid); - _add_to_db(message, type, message->jid, myjid); + _add_to_db(message, NULL, message->jid, myjid); jid_destroy(myjid); } -void -log_database_add_incoming_chat(ProfMessage *message) { - _log_database_add_incoming(message, "chat"); -} - -void -log_database_add_incoming_muc(ProfMessage *message) { - _log_database_add_incoming(message, "muc"); -} - -void -log_database_add_incoming_muc_pm(ProfMessage *message) { - _log_database_add_incoming(message, "mucpm"); -} - static void -_log_database_add_outgoing(const char * const type, const char * const id, const char * const barejid, const char * const message, const char *const replace_id, prof_enc_t enc) +_log_database_add_outgoing(char *type, const char * const id, const char * const barejid, const char * const message, const char *const replace_id, prof_enc_t enc) { ProfMessage *msg = message_init(); @@ -221,67 +206,6 @@ log_database_add_outgoing_muc_pm(const char * const id, const char * const barej _log_database_add_outgoing("mucpm", id, barejid, message, replace_id, enc); } -static void -_add_to_db(ProfMessage *message, const char * const type, const Jid * const from_jid, const Jid * const to_jid) -{ - if (!g_chatlog_database) { - log_debug("log_database_add() called but db is not initialized"); - return; - } - - char *enc; - switch (message->enc) { - case PROF_MSG_ENC_PGP: - enc = "pgp"; - break; - case PROF_MSG_ENC_OTR: - enc = "otr"; - break; - case PROF_MSG_ENC_OMEMO: - enc = "omemo"; - break; - case PROF_MSG_ENC_NONE: - default: - enc = "none"; - } - - char *err_msg; - char *query; - gchar *date_fmt; - - if (message->timestamp) { - date_fmt = g_date_time_format_iso8601(message->timestamp); - } else { - date_fmt = g_date_time_format_iso8601(g_date_time_new_now_local()); - } - - if (asprintf(&query, "INSERT INTO `ChatLogs` (`from_jid`, `from_resource`, `to_jid`, `to_resource`, `message`, `timestamp`, `stanza_id`, `replace_id`, `type`, `encryption`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", - from_jid->barejid, - from_jid->resourcepart ? from_jid->resourcepart : "", - to_jid->barejid, - to_jid->resourcepart ? to_jid->resourcepart : "", - message->plain, - date_fmt, - message->id ? message->id : "", - message->replace_id ? message->replace_id : "", - type, - enc) == -1) { - log_error("log_database_add(): SQL query. could not allocate memory"); - return; - } - g_free(date_fmt); - - if( SQLITE_OK != sqlite3_exec(g_chatlog_database, query, NULL, 0, &err_msg)) { - if (err_msg) { - log_error("SQLite error: %s", err_msg); - sqlite3_free(err_msg); - } else { - log_error("Unknown SQLite error"); - } - } - free(query); -} - GSList* log_database_get_previous_chat(const gchar *const contact_barejid) { @@ -319,3 +243,83 @@ log_database_get_previous_chat(const gchar *const contact_barejid) return history; } + +static const char* _get_message_type_str(prof_msg_type_t type) { + switch (type) { + case PROF_MSG_TYPE_CHAT: + return "chat"; + case PROF_MSG_TYPE_MUC: + return "muc"; + case PROF_MSG_TYPE_MUCPM: + return "mucpm"; + case PROF_MSG_TYPE_UNINITIALIZED: + return NULL; + } + return NULL; +} + +static const char* _get_message_enc_str(prof_enc_t enc) { + switch (enc) { + case PROF_MSG_ENC_PGP: + return "pgp"; + case PROF_MSG_ENC_OTR: + return "otr"; + case PROF_MSG_ENC_OMEMO: + return "omemo"; + case PROF_MSG_ENC_NONE: + return "none"; + } + + return "none"; +} + +static void +_add_to_db(ProfMessage *message, char *type, const Jid * const from_jid, const Jid * const to_jid) +{ + if (!g_chatlog_database) { + log_debug("log_database_add() called but db is not initialized"); + return; + } + + char *err_msg; + char *query; + gchar *date_fmt; + + if (message->timestamp) { + date_fmt = g_date_time_format_iso8601(message->timestamp); + } else { + date_fmt = g_date_time_format_iso8601(g_date_time_new_now_local()); + } + + const char *enc = _get_message_enc_str(message->enc); + + if (!type) { + type = (char*)_get_message_type_str(message->type); + } + + if (asprintf(&query, "INSERT INTO `ChatLogs` (`from_jid`, `from_resource`, `to_jid`, `to_resource`, `message`, `timestamp`, `stanza_id`, `replace_id`, `type`, `encryption`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", + from_jid->barejid, + from_jid->resourcepart ? from_jid->resourcepart : "", + to_jid->barejid, + to_jid->resourcepart ? to_jid->resourcepart : "", + message->plain, + date_fmt, + message->id ? message->id : "", + message->replace_id ? message->replace_id : "", + type, + enc) == -1) { + log_error("log_database_add(): SQL query. could not allocate memory"); + return; + } + g_free(date_fmt); + + if( SQLITE_OK != sqlite3_exec(g_chatlog_database, query, NULL, 0, &err_msg)) { + if (err_msg) { + log_error("SQLite error: %s", err_msg); + sqlite3_free(err_msg); + } else { + log_error("Unknown SQLite error"); + } + } + free(query); +} diff --git a/src/database.h b/src/database.h index 4401e430..4ad3fc9c 100644 --- a/src/database.h +++ b/src/database.h @@ -41,9 +41,7 @@ #include "xmpp/xmpp.h" gboolean log_database_init(ProfAccount *account); -void log_database_add_incoming_chat(ProfMessage *message); -void log_database_add_incoming_muc(ProfMessage *message); -void log_database_add_incoming_muc_pm(ProfMessage *message); +void log_database_add_incoming(ProfMessage *message); void log_database_add_outgoing_chat(const char * const id, const char * const barejid, const char * const message, const char *const replace_id, prof_enc_t enc); void log_database_add_outgoing_muc(const char * const id, const char * const barejid, const char * const message, const char *const replace_id, prof_enc_t enc); void log_database_add_outgoing_muc_pm(const char * const id, const char * const barejid, const char * const message, const char *const replace_id, prof_enc_t enc); diff --git a/src/event/server_events.c b/src/event/server_events.c index e75db88e..b63af3b6 100644 --- a/src/event/server_events.c +++ b/src/event/server_events.c @@ -301,7 +301,7 @@ static void _log_muc(ProfMessage *message) } else { groupchat_log_msg_in(message->jid->barejid, message->jid->resourcepart, message->plain); } - log_database_add_incoming_muc(message); + log_database_add_incoming(message); } void @@ -401,7 +401,7 @@ sv_ev_incoming_private_message(ProfMessage *message) } _clean_incoming_message(message); - log_database_add_incoming_muc_pm(message); + log_database_add_incoming(message); privwin_incoming_msg(privatewin, message); chat_log_msg_in(message); @@ -537,7 +537,7 @@ _sv_ev_incoming_pgp(ProfChatWin *chatwin, gboolean new_win, ProfMessage *message if (message->plain) { message->enc = PROF_MSG_ENC_PGP; _clean_incoming_message(message); - log_database_add_incoming_chat(message); + log_database_add_incoming(message); chatwin_incoming_msg(chatwin, message, new_win); if (logit) { chat_log_pgp_msg_in(message); @@ -553,7 +553,7 @@ _sv_ev_incoming_pgp(ProfChatWin *chatwin, gboolean new_win, ProfMessage *message message->enc = PROF_MSG_ENC_NONE; message->plain = strdup(message->body); _clean_incoming_message(message); - log_database_add_incoming_chat(message); + log_database_add_incoming(message); chatwin_incoming_msg(chatwin, message, new_win); chat_log_msg_in(message); chatwin->pgp_recv = FALSE; @@ -576,7 +576,7 @@ _sv_ev_incoming_otr(ProfChatWin *chatwin, gboolean new_win, ProfMessage *message } _clean_incoming_message(message); - log_database_add_incoming_chat(message); + log_database_add_incoming(message); chatwin_incoming_msg(chatwin, message, new_win); chat_log_otr_msg_in(message); @@ -592,7 +592,7 @@ static void _sv_ev_incoming_omemo(ProfChatWin *chatwin, gboolean new_win, ProfMessage *message, gboolean logit) { _clean_incoming_message(message); - log_database_add_incoming_chat(message); + log_database_add_incoming(message); chatwin_incoming_msg(chatwin, message, new_win); if (logit) { chat_log_omemo_msg_in(message); @@ -608,7 +608,7 @@ _sv_ev_incoming_plain(ProfChatWin *chatwin, gboolean new_win, ProfMessage *messa message->enc = PROF_MSG_ENC_NONE; message->plain = strdup(message->body); _clean_incoming_message(message); - log_database_add_incoming_chat(message); + log_database_add_incoming(message); chatwin_incoming_msg(chatwin, message, new_win); if (logit) { chat_log_msg_in(message);