1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2003-2007 Imendio AB
4 * Copyright (C) 2007-2008 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Xavier Claessens <xclaesse@gmail.com>
22 * Jonny Lamb <jonny.lamb@collabora.co.uk>
31 /* FIXME: g_mapped_file_free has been deprecated in GLib 2.22, but the
32 * replacement symbol, g_mapped_file_unref is not available in older Glib
33 * and we're not ready to bump our version requirement just for this. When
34 * we're ready to bump our version requirement, just revert this patch. */
35 #undef G_DISABLE_DEPRECATED
36 #include <glib/gstdio.h>
37 #define G_DISABLE_DEPRECATED
39 #include <telepathy-glib/util.h>
40 #include <telepathy-glib/defs.h>
42 #include "empathy-log-store.h"
43 #include "empathy-log-store-empathy.h"
44 #include "empathy-log-manager.h"
45 #include "empathy-account-manager.h"
46 #include "empathy-contact.h"
47 #include "empathy-time.h"
48 #include "empathy-utils.h"
50 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
51 #include "empathy-debug.h"
53 #define LOG_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
54 #define LOG_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
55 #define LOG_DIR_CHATROOMS "chatrooms"
56 #define LOG_FILENAME_SUFFIX ".log"
57 #define LOG_TIME_FORMAT_FULL "%Y%m%dT%H:%M:%S"
58 #define LOG_TIME_FORMAT "%Y%m%d"
60 "<?xml version='1.0' encoding='utf-8'?>\n" \
61 "<?xml-stylesheet type=\"text/xsl\" href=\"empathy-log.xsl\"?>\n" \
68 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLogStoreEmpathy)
73 EmpathyAccountManager *account_manager;
74 } EmpathyLogStoreEmpathyPriv;
76 static void log_store_iface_init (gpointer g_iface,gpointer iface_data);
78 G_DEFINE_TYPE_WITH_CODE (EmpathyLogStoreEmpathy, empathy_log_store_empathy,
79 G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_LOG_STORE,
80 log_store_iface_init));
83 log_store_empathy_finalize (GObject *object)
85 EmpathyLogStoreEmpathy *self = EMPATHY_LOG_STORE_EMPATHY (object);
86 EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
88 g_object_unref (priv->account_manager);
89 g_free (priv->basedir);
94 empathy_log_store_empathy_class_init (EmpathyLogStoreEmpathyClass *klass)
96 GObjectClass *object_class = G_OBJECT_CLASS (klass);
98 object_class->finalize = log_store_empathy_finalize;
100 g_type_class_add_private (object_class, sizeof (EmpathyLogStoreEmpathyPriv));
104 empathy_log_store_empathy_init (EmpathyLogStoreEmpathy *self)
106 EmpathyLogStoreEmpathyPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
107 EMPATHY_TYPE_LOG_STORE_EMPATHY, EmpathyLogStoreEmpathyPriv);
111 priv->basedir = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (),
112 PACKAGE_NAME, "logs", NULL);
114 priv->name = g_strdup ("Empathy");
115 priv->account_manager = empathy_account_manager_dup_singleton ();
119 log_store_account_to_dirname (EmpathyAccount *account)
123 name = empathy_account_get_unique_name (account);
124 if (g_str_has_prefix (name, TP_ACCOUNT_OBJECT_PATH_BASE))
125 name += strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
127 return g_strdelimit (g_strdup (name), "/", '_');
132 log_store_empathy_get_dir (EmpathyLogStore *self,
133 EmpathyAccount *account,
134 const gchar *chat_id,
139 EmpathyLogStoreEmpathyPriv *priv;
141 priv = GET_PRIV (self);
143 escaped = log_store_account_to_dirname (account);
146 basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir, escaped,
147 LOG_DIR_CHATROOMS, chat_id, NULL);
149 basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir,
150 escaped, chat_id, NULL);
158 log_store_empathy_get_timestamp_filename (void)
164 t = empathy_time_get_current ();
165 time_str = empathy_time_to_string_local (t, LOG_TIME_FORMAT);
166 filename = g_strconcat (time_str, LOG_FILENAME_SUFFIX, NULL);
174 log_store_empathy_get_timestamp_from_message (EmpathyMessage *message)
178 t = empathy_message_get_timestamp (message);
180 /* We keep the timestamps in the messages as UTC. */
181 return empathy_time_to_string_utc (t, LOG_TIME_FORMAT_FULL);
185 log_store_empathy_get_filename (EmpathyLogStore *self,
186 EmpathyAccount *account,
187 const gchar *chat_id,
194 basedir = log_store_empathy_get_dir (self, account, chat_id, chatroom);
195 timestamp = log_store_empathy_get_timestamp_filename ();
196 filename = g_build_filename (basedir, timestamp, NULL);
205 log_store_empathy_add_message (EmpathyLogStore *self,
206 const gchar *chat_id,
208 EmpathyMessage *message,
212 EmpathyAccount *account;
213 EmpathyContact *sender;
214 const gchar *body_str;
216 EmpathyAvatar *avatar;
217 gchar *avatar_token = NULL;
224 TpChannelTextMessageType msg_type;
226 g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), FALSE);
227 g_return_val_if_fail (chat_id != NULL, FALSE);
228 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
230 sender = empathy_message_get_sender (message);
231 account = empathy_contact_get_account (sender);
232 body_str = empathy_message_get_body (message);
233 msg_type = empathy_message_get_tptype (message);
235 if (EMP_STR_EMPTY (body_str))
238 filename = log_store_empathy_get_filename (self, account, chat_id, chatroom);
239 basedir = g_path_get_dirname (filename);
240 if (!g_file_test (basedir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
242 DEBUG ("Creating directory:'%s'", basedir);
243 g_mkdir_with_parents (basedir, LOG_DIR_CREATE_MODE);
247 DEBUG ("Adding message: '%s' to file: '%s'", body_str, filename);
249 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
251 file = g_fopen (filename, "w+");
253 g_fprintf (file, LOG_HEADER);
255 g_chmod (filename, LOG_FILE_CREATE_MODE);
259 file = g_fopen (filename, "r+");
261 fseek (file, - strlen (LOG_FOOTER), SEEK_END);
264 body = g_markup_escape_text (body_str, -1);
265 timestamp = log_store_empathy_get_timestamp_from_message (message);
267 str = empathy_contact_get_name (sender);
268 contact_name = g_markup_escape_text (str, -1);
270 str = empathy_contact_get_id (sender);
271 contact_id = g_markup_escape_text (str, -1);
273 avatar = empathy_contact_get_avatar (sender);
275 avatar_token = g_markup_escape_text (avatar->token, -1);
278 "<message time='%s' cm_id='%d' id='%s' name='%s' token='%s' isuser='%s' type='%s'>"
279 "%s</message>\n" LOG_FOOTER, timestamp,
280 empathy_message_get_id (message),
281 contact_id, contact_name,
282 avatar_token ? avatar_token : "",
283 empathy_contact_is_user (sender) ? "true" : "false",
284 empathy_message_type_to_str (msg_type), body);
289 g_free (contact_name);
292 g_free (avatar_token);
298 log_store_empathy_exists (EmpathyLogStore *self,
299 EmpathyAccount *account,
300 const gchar *chat_id,
306 dir = log_store_empathy_get_dir (self, account, chat_id, chatroom);
307 exists = g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
314 log_store_empathy_get_dates (EmpathyLogStore *self,
315 EmpathyAccount *account,
316 const gchar *chat_id,
323 const gchar *filename;
326 g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
327 g_return_val_if_fail (chat_id != NULL, NULL);
329 directory = log_store_empathy_get_dir (self, account, chat_id, chatroom);
330 dir = g_dir_open (directory, 0, NULL);
333 DEBUG ("Could not open directory:'%s'", directory);
338 DEBUG ("Collating a list of dates in:'%s'", directory);
340 while ((filename = g_dir_read_name (dir)) != NULL)
342 if (!g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
345 p = strstr (filename, LOG_FILENAME_SUFFIX);
346 date = g_strndup (filename, p - filename);
351 if (!g_regex_match_simple ("\\d{8}", date, 0, 0))
354 dates = g_list_insert_sorted (dates, date, (GCompareFunc) strcmp);
360 DEBUG ("Parsed %d dates", g_list_length (dates));
366 log_store_empathy_get_filename_for_date (EmpathyLogStore *self,
367 EmpathyAccount *account,
368 const gchar *chat_id,
376 basedir = log_store_empathy_get_dir (self, account, chat_id, chatroom);
377 timestamp = g_strconcat (date, LOG_FILENAME_SUFFIX, NULL);
378 filename = g_build_filename (basedir, timestamp, NULL);
386 static EmpathyLogSearchHit *
387 log_store_empathy_search_hit_new (EmpathyLogStore *self,
388 const gchar *filename)
390 EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
391 EmpathyLogSearchHit *hit;
398 if (!g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
401 strv = g_strsplit (filename, G_DIR_SEPARATOR_S, -1);
402 len = g_strv_length (strv);
404 hit = g_slice_new0 (EmpathyLogSearchHit);
406 end = strstr (strv[len-1], LOG_FILENAME_SUFFIX);
407 hit->date = g_strndup (strv[len-1], end - strv[len-1]);
408 hit->chat_id = g_strdup (strv[len-2]);
409 hit->is_chatroom = (strcmp (strv[len-3], LOG_DIR_CHATROOMS) == 0);
411 if (hit->is_chatroom)
412 account_name = strv[len-4];
414 account_name = strv[len-3];
416 accounts = empathy_account_manager_dup_accounts (priv->account_manager);
418 for (l = accounts; l != NULL; l = g_list_next (l))
420 EmpathyAccount *account = EMPATHY_ACCOUNT (l->data);
423 name = log_store_account_to_dirname (account);
424 if (!tp_strdiff (name, account_name))
426 g_assert (hit->account == NULL);
427 hit->account = account;
428 g_object_ref (account);
430 g_object_unref (account);
433 g_list_free (accounts);
435 hit->filename = g_strdup (filename);
443 log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
444 const gchar *filename)
446 GList *messages = NULL;
447 xmlParserCtxtPtr ctxt;
451 EmpathyLogSearchHit *hit;
452 EmpathyAccount *account;
454 g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
455 g_return_val_if_fail (filename != NULL, NULL);
457 DEBUG ("Attempting to parse filename:'%s'...", filename);
459 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
461 DEBUG ("Filename:'%s' does not exist", filename);
465 /* Get the account from the filename */
466 hit = log_store_empathy_search_hit_new (self, filename);
468 if (hit->account != NULL)
469 account = g_object_ref (hit->account);
471 empathy_log_manager_search_hit_free (hit);
473 if (hit->account == NULL)
477 ctxt = xmlNewParserCtxt ();
479 /* Parse and validate the file. */
480 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
483 g_warning ("Failed to parse file:'%s'", filename);
484 xmlFreeParserCtxt (ctxt);
488 /* The root node, presets. */
489 log_node = xmlDocGetRootElement (doc);
493 xmlFreeParserCtxt (ctxt);
497 /* Now get the messages. */
498 for (node = log_node->children; node; node = node->next)
500 EmpathyMessage *message;
501 EmpathyContact *sender;
506 gchar *sender_avatar_token;
509 gboolean is_user = FALSE;
513 TpChannelTextMessageType msg_type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
515 if (strcmp (node->name, "message") != 0)
518 body = xmlNodeGetContent (node);
519 time = xmlGetProp (node, "time");
520 sender_id = xmlGetProp (node, "id");
521 sender_name = xmlGetProp (node, "name");
522 sender_avatar_token = xmlGetProp (node, "token");
523 is_user_str = xmlGetProp (node, "isuser");
524 msg_type_str = xmlGetProp (node, "type");
525 cm_id_str = xmlGetProp (node, "cm_id");
528 is_user = strcmp (is_user_str, "true") == 0;
531 msg_type = empathy_message_type_from_str (msg_type_str);
534 cm_id = atoi (cm_id_str);
536 t = empathy_time_parse (time);
538 sender = empathy_contact_new_for_log (account, sender_id, sender_name,
541 if (!EMP_STR_EMPTY (sender_avatar_token))
542 empathy_contact_load_avatar_cache (sender,
543 sender_avatar_token);
545 message = empathy_message_new (body);
546 empathy_message_set_sender (message, sender);
547 empathy_message_set_timestamp (message, t);
548 empathy_message_set_tptype (message, msg_type);
549 empathy_message_set_is_backlog (message, TRUE);
552 empathy_message_set_id (message, cm_id);
554 messages = g_list_append (messages, message);
556 g_object_unref (sender);
559 xmlFree (sender_name);
561 xmlFree (is_user_str);
562 xmlFree (msg_type_str);
564 xmlFree (sender_avatar_token);
567 DEBUG ("Parsed %d messages", g_list_length (messages));
570 xmlFreeParserCtxt (ctxt);
576 log_store_empathy_get_all_files (EmpathyLogStore *self,
582 const gchar *basedir;
583 EmpathyLogStoreEmpathyPriv *priv;
585 priv = GET_PRIV (self);
587 basedir = dir ? dir : priv->basedir;
589 gdir = g_dir_open (basedir, 0, NULL);
593 while ((name = g_dir_read_name (gdir)) != NULL)
597 filename = g_build_filename (basedir, name, NULL);
598 if (g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
600 files = g_list_prepend (files, filename);
604 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
606 /* Recursively get all log files */
607 files = g_list_concat (files,
608 log_store_empathy_get_all_files (self, filename));
620 log_store_empathy_search_new (EmpathyLogStore *self,
625 gchar *text_casefold;
627 g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
628 g_return_val_if_fail (!EMP_STR_EMPTY (text), NULL);
630 text_casefold = g_utf8_casefold (text, -1);
632 files = log_store_empathy_get_all_files (self, NULL);
633 DEBUG ("Found %d log files in total", g_list_length (files));
635 for (l = files; l; l = g_list_next (l))
641 gchar *contents_casefold;
645 file = g_mapped_file_new (filename, FALSE, NULL);
649 length = g_mapped_file_get_length (file);
650 contents = g_mapped_file_get_contents (file);
651 contents_casefold = g_utf8_casefold (contents, length);
653 g_mapped_file_free (file);
655 if (strstr (contents_casefold, text_casefold))
657 EmpathyLogSearchHit *hit;
659 hit = log_store_empathy_search_hit_new (self, filename);
663 hits = g_list_prepend (hits, hit);
664 DEBUG ("Found text:'%s' in file:'%s' on date:'%s'",
665 text, hit->filename, hit->date);
669 g_free (contents_casefold);
674 g_free (text_casefold);
680 log_store_empathy_get_chats_for_dir (EmpathyLogStore *self,
682 gboolean is_chatroom)
687 GError *error = NULL;
689 gdir = g_dir_open (dir, 0, &error);
692 DEBUG ("Failed to open directory: %s, error: %s", dir, error->message);
693 g_error_free (error);
697 while ((name = g_dir_read_name (gdir)) != NULL)
699 EmpathyLogSearchHit *hit;
701 if (!is_chatroom && strcmp (name, LOG_DIR_CHATROOMS) == 0)
703 gchar *filename = g_build_filename (dir, name, NULL);
704 hits = g_list_concat (hits, log_store_empathy_get_chats_for_dir (
705 self, filename, TRUE));
709 hit = g_slice_new0 (EmpathyLogSearchHit);
710 hit->chat_id = g_strdup (name);
711 hit->is_chatroom = is_chatroom;
713 hits = g_list_prepend (hits, hit);
723 log_store_empathy_get_messages_for_date (EmpathyLogStore *self,
724 EmpathyAccount *account,
725 const gchar *chat_id,
732 g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
733 g_return_val_if_fail (chat_id != NULL, NULL);
735 filename = log_store_empathy_get_filename_for_date (self, account,
736 chat_id, chatroom, date);
737 messages = log_store_empathy_get_messages_for_file (self, filename);
744 log_store_empathy_get_chats (EmpathyLogStore *self,
745 EmpathyAccount *account)
749 EmpathyLogStoreEmpathyPriv *priv;
751 priv = GET_PRIV (self);
753 dir = log_store_empathy_get_dir (self, account, NULL, FALSE);
755 hits = log_store_empathy_get_chats_for_dir (self, dir, FALSE);
763 log_store_empathy_get_name (EmpathyLogStore *self)
765 EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
771 log_store_empathy_get_filtered_messages (EmpathyLogStore *self,
772 EmpathyAccount *account,
773 const gchar *chat_id,
776 EmpathyLogMessageFilter filter,
779 GList *dates, *l, *messages = NULL;
782 dates = log_store_empathy_get_dates (self, account, chat_id, chatroom);
784 for (l = g_list_last (dates); l && i < num_messages; l = g_list_previous (l))
786 GList *new_messages, *n, *next;
788 /* FIXME: We should really restrict the message parsing to get only
789 * the newest num_messages. */
790 new_messages = log_store_empathy_get_messages_for_date (self, account,
791 chat_id, chatroom, l->data);
796 next = g_list_next (n);
797 if (!filter (n->data, user_data))
799 g_object_unref (n->data);
800 new_messages = g_list_delete_link (new_messages, n);
808 messages = g_list_concat (messages, new_messages);
811 g_list_foreach (dates, (GFunc) g_free, NULL);
818 log_store_iface_init (gpointer g_iface,
821 EmpathyLogStoreInterface *iface = (EmpathyLogStoreInterface *) g_iface;
823 iface->get_name = log_store_empathy_get_name;
824 iface->exists = log_store_empathy_exists;
825 iface->add_message = log_store_empathy_add_message;
826 iface->get_dates = log_store_empathy_get_dates;
827 iface->get_messages_for_date = log_store_empathy_get_messages_for_date;
828 iface->get_chats = log_store_empathy_get_chats;
829 iface->search_new = log_store_empathy_search_new;
830 iface->ack_message = NULL;
831 iface->get_filtered_messages = log_store_empathy_get_filtered_messages;