about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/database.c154
-rw-r--r--src/database.h4
-rw-r--r--src/event/server_events.c14
3 files changed, 87 insertions, 85 deletions
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,30 +206,81 @@ 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);
 }
 
+GSList*
+log_database_get_previous_chat(const gchar *const contact_barejid)
+{
+	sqlite3_stmt *stmt = NULL;
+    char *query;
+
+    if (asprintf(&query, "SELECT `message`, `timestamp`, `from_jid` from `ChatLogs` WHERE `from_jid` = '%s' OR `to_jid` = '%s' ORDER BY `id` ASC LIMIT 10", contact_barejid, contact_barejid) == -1) {
+        log_error("log_database_get_previous_chat(): SQL query. could not allocate memory");
+        return NULL;
+    }
+
+	int rc = sqlite3_prepare_v2(g_chatlog_database, query, -1, &stmt, NULL);
+	if( rc!=SQLITE_OK ) {
+        log_error("log_database_get_previous_chat(): unknown SQLite error");
+        return NULL;
+    }
+
+    GSList *history = NULL;
+
+	while( sqlite3_step(stmt) == SQLITE_ROW ) {
+		char *message = (char*)sqlite3_column_text(stmt, 0);
+		char *date = (char*)sqlite3_column_text(stmt, 1);
+		char *from = (char*)sqlite3_column_text(stmt, 2);
+
+        ProfMessage *msg = message_init();
+        msg->jid = jid_create(from);
+        msg->plain = strdup(message);
+        msg->timestamp = g_date_time_new_from_iso8601(date, NULL);
+        // TODO: later we can get more fields like 'enc'. then we can display the history like regular chats with all info the user enabled.
+
+        history = g_slist_append(history, msg);
+	}
+	sqlite3_finalize(stmt);
+    free(query);
+
+    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, const char * const type, const Jid * const from_jid, const Jid * const to_jid)
+_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 *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;
@@ -255,6 +291,12 @@ _add_to_db(ProfMessage *message, const char * const type, const Jid * const from
         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 : "",
@@ -281,41 +323,3 @@ _add_to_db(ProfMessage *message, const char * const type, const Jid * const from
     }
     free(query);
 }
-
-GSList*
-log_database_get_previous_chat(const gchar *const contact_barejid)
-{
-	sqlite3_stmt *stmt = NULL;
-    char *query;
-
-    if (asprintf(&query, "SELECT `message`, `timestamp`, `from_jid` from `ChatLogs` WHERE `from_jid` = '%s' OR `to_jid` = '%s' ORDER BY `id` ASC LIMIT 10", contact_barejid, contact_barejid) == -1) {
-        log_error("log_database_get_previous_chat(): SQL query. could not allocate memory");
-        return NULL;
-    }
-
-	int rc = sqlite3_prepare_v2(g_chatlog_database, query, -1, &stmt, NULL);
-	if( rc!=SQLITE_OK ) {
-        log_error("log_database_get_previous_chat(): unknown SQLite error");
-        return NULL;
-    }
-
-    GSList *history = NULL;
-
-	while( sqlite3_step(stmt) == SQLITE_ROW ) {
-		char *message = (char*)sqlite3_column_text(stmt, 0);
-		char *date = (char*)sqlite3_column_text(stmt, 1);
-		char *from = (char*)sqlite3_column_text(stmt, 2);
-
-        ProfMessage *msg = message_init();
-        msg->jid = jid_create(from);
-        msg->plain = strdup(message);
-        msg->timestamp = g_date_time_new_from_iso8601(date, NULL);
-        // TODO: later we can get more fields like 'enc'. then we can display the history like regular chats with all info the user enabled.
-
-        history = g_slist_append(history, msg);
-	}
-	sqlite3_finalize(stmt);
-    free(query);
-
-    return history;
-}
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);