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 unescaped = g_strdup_printf ("/%s", g_strdelimit (account_name, "%", '/'));
418 hit->account = empathy_account_manager_get_account (priv->account_manager,
420 if (hit->account != NULL)
421 g_object_ref (hit->account);
422 hit->filename = g_strdup (filename);
431 log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
432 const gchar *filename)
434 GList *messages = NULL;
435 xmlParserCtxtPtr ctxt;
439 EmpathyLogSearchHit *hit;
440 EmpathyAccount *account;
442 g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
443 g_return_val_if_fail (filename != NULL, NULL);
445 DEBUG ("Attempting to parse filename:'%s'...", filename);
447 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
449 DEBUG ("Filename:'%s' does not exist", filename);
453 /* Get the account from the filename */
454 hit = log_store_empathy_search_hit_new (self, filename);
456 if (hit->account != NULL)
457 account = g_object_ref (hit->account);
459 empathy_log_manager_search_hit_free (hit);
461 if (hit->account == NULL)
465 ctxt = xmlNewParserCtxt ();
467 /* Parse and validate the file. */
468 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
471 g_warning ("Failed to parse file:'%s'", filename);
472 xmlFreeParserCtxt (ctxt);
476 /* The root node, presets. */
477 log_node = xmlDocGetRootElement (doc);
481 xmlFreeParserCtxt (ctxt);
485 /* Now get the messages. */
486 for (node = log_node->children; node; node = node->next)
488 EmpathyMessage *message;
489 EmpathyContact *sender;
494 gchar *sender_avatar_token;
497 gboolean is_user = FALSE;
501 TpChannelTextMessageType msg_type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
503 if (strcmp (node->name, "message") != 0)
506 body = xmlNodeGetContent (node);
507 time = xmlGetProp (node, "time");
508 sender_id = xmlGetProp (node, "id");
509 sender_name = xmlGetProp (node, "name");
510 sender_avatar_token = xmlGetProp (node, "token");
511 is_user_str = xmlGetProp (node, "isuser");
512 msg_type_str = xmlGetProp (node, "type");
513 cm_id_str = xmlGetProp (node, "cm_id");
516 is_user = strcmp (is_user_str, "true") == 0;
519 msg_type = empathy_message_type_from_str (msg_type_str);
522 cm_id = atoi (cm_id_str);
524 t = empathy_time_parse (time);
526 sender = empathy_contact_new_for_log (account, sender_id, sender_name,
529 if (!EMP_STR_EMPTY (sender_avatar_token))
530 empathy_contact_load_avatar_cache (sender,
531 sender_avatar_token);
533 message = empathy_message_new (body);
534 empathy_message_set_sender (message, sender);
535 empathy_message_set_timestamp (message, t);
536 empathy_message_set_tptype (message, msg_type);
537 empathy_message_set_is_backlog (message, TRUE);
540 empathy_message_set_id (message, cm_id);
542 messages = g_list_append (messages, message);
544 g_object_unref (sender);
547 xmlFree (sender_name);
549 xmlFree (is_user_str);
550 xmlFree (msg_type_str);
552 xmlFree (sender_avatar_token);
555 DEBUG ("Parsed %d messages", g_list_length (messages));
558 xmlFreeParserCtxt (ctxt);
564 log_store_empathy_get_all_files (EmpathyLogStore *self,
570 const gchar *basedir;
571 EmpathyLogStoreEmpathyPriv *priv;
573 priv = GET_PRIV (self);
575 basedir = dir ? dir : priv->basedir;
577 gdir = g_dir_open (basedir, 0, NULL);
581 while ((name = g_dir_read_name (gdir)) != NULL)
585 filename = g_build_filename (basedir, name, NULL);
586 if (g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
588 files = g_list_prepend (files, filename);
592 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
594 /* Recursively get all log files */
595 files = g_list_concat (files,
596 log_store_empathy_get_all_files (self, filename));
608 log_store_empathy_search_new (EmpathyLogStore *self,
613 gchar *text_casefold;
615 g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
616 g_return_val_if_fail (!EMP_STR_EMPTY (text), NULL);
618 text_casefold = g_utf8_casefold (text, -1);
620 files = log_store_empathy_get_all_files (self, NULL);
621 DEBUG ("Found %d log files in total", g_list_length (files));
623 for (l = files; l; l = g_list_next (l))
629 gchar *contents_casefold;
633 file = g_mapped_file_new (filename, FALSE, NULL);
637 length = g_mapped_file_get_length (file);
638 contents = g_mapped_file_get_contents (file);
639 contents_casefold = g_utf8_casefold (contents, length);
641 g_mapped_file_free (file);
643 if (strstr (contents_casefold, text_casefold))
645 EmpathyLogSearchHit *hit;
647 hit = log_store_empathy_search_hit_new (self, filename);
651 hits = g_list_prepend (hits, hit);
652 DEBUG ("Found text:'%s' in file:'%s' on date:'%s'",
653 text, hit->filename, hit->date);
657 g_free (contents_casefold);
662 g_free (text_casefold);
668 log_store_empathy_get_chats_for_dir (EmpathyLogStore *self,
670 gboolean is_chatroom)
675 GError *error = NULL;
677 gdir = g_dir_open (dir, 0, &error);
680 DEBUG ("Failed to open directory: %s, error: %s", dir, error->message);
681 g_error_free (error);
685 while ((name = g_dir_read_name (gdir)) != NULL)
687 EmpathyLogSearchHit *hit;
689 if (!is_chatroom && strcmp (name, LOG_DIR_CHATROOMS) == 0)
691 gchar *filename = g_build_filename (dir, name, NULL);
692 hits = g_list_concat (hits, log_store_empathy_get_chats_for_dir (
693 self, filename, TRUE));
697 hit = g_slice_new0 (EmpathyLogSearchHit);
698 hit->chat_id = g_strdup (name);
699 hit->is_chatroom = is_chatroom;
701 hits = g_list_prepend (hits, hit);
711 log_store_empathy_get_messages_for_date (EmpathyLogStore *self,
712 EmpathyAccount *account,
713 const gchar *chat_id,
720 g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
721 g_return_val_if_fail (chat_id != NULL, NULL);
723 filename = log_store_empathy_get_filename_for_date (self, account,
724 chat_id, chatroom, date);
725 messages = log_store_empathy_get_messages_for_file (self, filename);
732 log_store_empathy_get_chats (EmpathyLogStore *self,
733 EmpathyAccount *account)
737 EmpathyLogStoreEmpathyPriv *priv;
739 priv = GET_PRIV (self);
741 dir = g_build_filename (priv->basedir,
742 empathy_account_get_unique_name (account), NULL);
744 hits = log_store_empathy_get_chats_for_dir (self, dir, FALSE);
752 log_store_empathy_get_name (EmpathyLogStore *self)
754 EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
760 log_store_empathy_get_filtered_messages (EmpathyLogStore *self,
761 EmpathyAccount *account,
762 const gchar *chat_id,
765 EmpathyLogMessageFilter filter,
768 GList *dates, *l, *messages = NULL;
771 dates = log_store_empathy_get_dates (self, account, chat_id, chatroom);
773 for (l = g_list_last (dates); l && i < num_messages; l = g_list_previous (l))
775 GList *new_messages, *n, *next;
777 /* FIXME: We should really restrict the message parsing to get only
778 * the newest num_messages. */
779 new_messages = log_store_empathy_get_messages_for_date (self, account,
780 chat_id, chatroom, l->data);
785 next = g_list_next (n);
786 if (!filter (n->data, user_data))
788 g_object_unref (n->data);
789 new_messages = g_list_delete_link (new_messages, n);
797 messages = g_list_concat (messages, new_messages);
800 g_list_foreach (dates, (GFunc) g_free, NULL);
807 log_store_iface_init (gpointer g_iface,
810 EmpathyLogStoreInterface *iface = (EmpathyLogStoreInterface *) g_iface;
812 iface->get_name = log_store_empathy_get_name;
813 iface->exists = log_store_empathy_exists;
814 iface->add_message = log_store_empathy_add_message;
815 iface->get_dates = log_store_empathy_get_dates;
816 iface->get_messages_for_date = log_store_empathy_get_messages_for_date;
817 iface->get_chats = log_store_empathy_get_chats;
818 iface->search_new = log_store_empathy_search_new;
819 iface->ack_message = NULL;
820 iface->get_filtered_messages = log_store_empathy_get_filtered_messages;