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>
30
#include <glib/gstdio.h>
32
#include <telepathy-glib/account-manager.h>
33
#include <telepathy-glib/util.h>
34
#include <telepathy-glib/defs.h>
36
#include "empathy-log-store.h"
37
#include "empathy-log-store-empathy.h"
38
#include "empathy-log-manager.h"
39
#include "empathy-contact.h"
40
#include "empathy-time.h"
41
#include "empathy-utils.h"
43
#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
44
#include "empathy-debug.h"
46
#define LOG_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
47
#define LOG_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
48
#define LOG_DIR_CHATROOMS "chatrooms"
49
#define LOG_FILENAME_SUFFIX ".log"
50
#define LOG_TIME_FORMAT_FULL "%Y%m%dT%H:%M:%S"
51
#define LOG_TIME_FORMAT "%Y%m%d"
53
"<?xml version='1.0' encoding='utf-8'?>\n" \
54
"<?xml-stylesheet type=\"text/xsl\" href=\"empathy-log.xsl\"?>\n" \
61
#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLogStoreEmpathy)
66
TpAccountManager *account_manager;
67
} EmpathyLogStoreEmpathyPriv;
69
static void log_store_iface_init (gpointer g_iface,gpointer iface_data);
71
G_DEFINE_TYPE_WITH_CODE (EmpathyLogStoreEmpathy, empathy_log_store_empathy,
72
G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_LOG_STORE,
73
log_store_iface_init));
76
log_store_empathy_finalize (GObject *object)
78
EmpathyLogStoreEmpathy *self = EMPATHY_LOG_STORE_EMPATHY (object);
79
EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
81
g_object_unref (priv->account_manager);
82
g_free (priv->basedir);
87
empathy_log_store_empathy_class_init (EmpathyLogStoreEmpathyClass *klass)
89
GObjectClass *object_class = G_OBJECT_CLASS (klass);
91
object_class->finalize = log_store_empathy_finalize;
93
g_type_class_add_private (object_class, sizeof (EmpathyLogStoreEmpathyPriv));
97
empathy_log_store_empathy_init (EmpathyLogStoreEmpathy *self)
99
EmpathyLogStoreEmpathyPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
100
EMPATHY_TYPE_LOG_STORE_EMPATHY, EmpathyLogStoreEmpathyPriv);
104
priv->basedir = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (),
105
PACKAGE_NAME, "logs", NULL);
107
priv->name = g_strdup ("Empathy");
108
priv->account_manager = tp_account_manager_dup ();
112
log_store_account_to_dirname (TpAccount *account)
116
name = tp_proxy_get_object_path (account);
117
if (g_str_has_prefix (name, TP_ACCOUNT_OBJECT_PATH_BASE))
118
name += strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
120
return g_strdelimit (g_strdup (name), "/", '_');
125
log_store_empathy_get_dir (EmpathyLogStore *self,
127
const gchar *chat_id,
132
EmpathyLogStoreEmpathyPriv *priv;
134
priv = GET_PRIV (self);
136
escaped = log_store_account_to_dirname (account);
139
basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir, escaped,
140
LOG_DIR_CHATROOMS, chat_id, NULL);
142
basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir,
143
escaped, chat_id, NULL);
151
log_store_empathy_get_timestamp_filename (void)
157
t = empathy_time_get_current ();
158
time_str = empathy_time_to_string_local (t, LOG_TIME_FORMAT);
159
filename = g_strconcat (time_str, LOG_FILENAME_SUFFIX, NULL);
167
log_store_empathy_get_timestamp_from_message (EmpathyMessage *message)
171
t = empathy_message_get_timestamp (message);
173
/* We keep the timestamps in the messages as UTC. */
174
return empathy_time_to_string_utc (t, LOG_TIME_FORMAT_FULL);
178
log_store_empathy_get_filename (EmpathyLogStore *self,
180
const gchar *chat_id,
187
basedir = log_store_empathy_get_dir (self, account, chat_id, chatroom);
188
timestamp = log_store_empathy_get_timestamp_filename ();
189
filename = g_build_filename (basedir, timestamp, NULL);
198
log_store_empathy_add_message (EmpathyLogStore *self,
199
const gchar *chat_id,
201
EmpathyMessage *message,
206
EmpathyContact *sender;
207
const gchar *body_str;
209
EmpathyAvatar *avatar;
210
gchar *avatar_token = NULL;
217
TpChannelTextMessageType msg_type;
219
g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), FALSE);
220
g_return_val_if_fail (chat_id != NULL, FALSE);
221
g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
223
sender = empathy_message_get_sender (message);
224
account = empathy_contact_get_account (sender);
225
body_str = empathy_message_get_body (message);
226
msg_type = empathy_message_get_tptype (message);
228
if (EMP_STR_EMPTY (body_str))
231
filename = log_store_empathy_get_filename (self, account, chat_id, chatroom);
232
basedir = g_path_get_dirname (filename);
233
if (!g_file_test (basedir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
235
DEBUG ("Creating directory:'%s'", basedir);
236
g_mkdir_with_parents (basedir, LOG_DIR_CREATE_MODE);
240
DEBUG ("Adding message: '%s' to file: '%s'", body_str, filename);
242
if (!g_file_test (filename, G_FILE_TEST_EXISTS))
244
file = g_fopen (filename, "w+");
246
g_fprintf (file, LOG_HEADER);
248
g_chmod (filename, LOG_FILE_CREATE_MODE);
252
file = g_fopen (filename, "r+");
254
fseek (file, - strlen (LOG_FOOTER), SEEK_END);
257
body = g_markup_escape_text (body_str, -1);
258
timestamp = log_store_empathy_get_timestamp_from_message (message);
260
str = empathy_contact_get_name (sender);
261
contact_name = g_markup_escape_text (str, -1);
263
str = empathy_contact_get_id (sender);
264
contact_id = g_markup_escape_text (str, -1);
266
avatar = empathy_contact_get_avatar (sender);
268
avatar_token = g_markup_escape_text (avatar->token, -1);
271
"<message time='%s' cm_id='%d' id='%s' name='%s' token='%s' isuser='%s' type='%s'>"
272
"%s</message>\n" LOG_FOOTER, timestamp,
273
empathy_message_get_id (message),
274
contact_id, contact_name,
275
avatar_token ? avatar_token : "",
276
empathy_contact_is_user (sender) ? "true" : "false",
277
empathy_message_type_to_str (msg_type), body);
282
g_free (contact_name);
285
g_free (avatar_token);
291
log_store_empathy_exists (EmpathyLogStore *self,
293
const gchar *chat_id,
299
dir = log_store_empathy_get_dir (self, account, chat_id, chatroom);
300
exists = g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
307
log_store_empathy_get_dates (EmpathyLogStore *self,
309
const gchar *chat_id,
316
const gchar *filename;
319
g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
320
g_return_val_if_fail (chat_id != NULL, NULL);
322
directory = log_store_empathy_get_dir (self, account, chat_id, chatroom);
323
dir = g_dir_open (directory, 0, NULL);
326
DEBUG ("Could not open directory:'%s'", directory);
331
DEBUG ("Collating a list of dates in:'%s'", directory);
333
while ((filename = g_dir_read_name (dir)) != NULL)
335
if (!g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
338
p = strstr (filename, LOG_FILENAME_SUFFIX);
339
date = g_strndup (filename, p - filename);
344
if (!g_regex_match_simple ("\\d{8}", date, 0, 0))
347
dates = g_list_insert_sorted (dates, date, (GCompareFunc) strcmp);
353
DEBUG ("Parsed %d dates", g_list_length (dates));
359
log_store_empathy_get_filename_for_date (EmpathyLogStore *self,
361
const gchar *chat_id,
369
basedir = log_store_empathy_get_dir (self, account, chat_id, chatroom);
370
timestamp = g_strconcat (date, LOG_FILENAME_SUFFIX, NULL);
371
filename = g_build_filename (basedir, timestamp, NULL);
379
static EmpathyLogSearchHit *
380
log_store_empathy_search_hit_new (EmpathyLogStore *self,
381
const gchar *filename)
383
EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
384
EmpathyLogSearchHit *hit;
391
if (!g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
394
strv = g_strsplit (filename, G_DIR_SEPARATOR_S, -1);
395
len = g_strv_length (strv);
397
hit = g_slice_new0 (EmpathyLogSearchHit);
399
end = strstr (strv[len-1], LOG_FILENAME_SUFFIX);
400
hit->date = g_strndup (strv[len-1], end - strv[len-1]);
401
hit->chat_id = g_strdup (strv[len-2]);
402
hit->is_chatroom = (strcmp (strv[len-3], LOG_DIR_CHATROOMS) == 0);
404
if (hit->is_chatroom)
405
account_name = strv[len-4];
407
account_name = strv[len-3];
409
/* FIXME: This assumes the account manager is prepared, but the
410
* synchronous API forces this. See bug #599189. */
411
accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
413
for (l = accounts; l != NULL; l = g_list_next (l))
415
TpAccount *account = TP_ACCOUNT (l->data);
418
name = log_store_account_to_dirname (account);
419
if (!tp_strdiff (name, account_name))
421
g_assert (hit->account == NULL);
422
hit->account = account;
423
g_object_ref (account);
427
g_list_free (accounts);
429
hit->filename = g_strdup (filename);
437
log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
439
const gchar *filename)
441
GList *messages = NULL;
442
xmlParserCtxtPtr ctxt;
447
g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
448
g_return_val_if_fail (filename != NULL, NULL);
450
DEBUG ("Attempting to parse filename:'%s'...", filename);
452
if (!g_file_test (filename, G_FILE_TEST_EXISTS))
454
DEBUG ("Filename:'%s' does not exist", filename);
459
ctxt = xmlNewParserCtxt ();
461
/* Parse and validate the file. */
462
doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
465
g_warning ("Failed to parse file:'%s'", filename);
466
xmlFreeParserCtxt (ctxt);
470
/* The root node, presets. */
471
log_node = xmlDocGetRootElement (doc);
475
xmlFreeParserCtxt (ctxt);
479
/* Now get the messages. */
480
for (node = log_node->children; node; node = node->next)
482
EmpathyMessage *message;
483
EmpathyContact *sender;
488
gchar *sender_avatar_token;
491
gboolean is_user = FALSE;
495
TpChannelTextMessageType msg_type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
497
if (strcmp ((const gchar *) node->name, "message") != 0)
500
body = (gchar *) xmlNodeGetContent (node);
501
time_ = (gchar *) xmlGetProp (node, (const xmlChar *) "time");
502
sender_id = (gchar *) xmlGetProp (node, (const xmlChar *) "id");
503
sender_name = (gchar *) xmlGetProp (node, (const xmlChar *) "name");
504
sender_avatar_token = (gchar *) xmlGetProp (node,
505
(const xmlChar *) "token");
506
is_user_str = (gchar *) xmlGetProp (node, (const xmlChar *) "isuser");
507
msg_type_str = (gchar *) xmlGetProp (node, (const xmlChar *) "type");
508
cm_id_str = (gchar *) xmlGetProp (node, (const xmlChar *) "cm_id");
511
is_user = strcmp (is_user_str, "true") == 0;
514
msg_type = empathy_message_type_from_str (msg_type_str);
517
cm_id = atoi (cm_id_str);
519
t = empathy_time_parse (time_);
521
sender = empathy_contact_new_for_log (account, sender_id, sender_name,
524
if (!EMP_STR_EMPTY (sender_avatar_token))
525
empathy_contact_load_avatar_cache (sender,
526
sender_avatar_token);
528
message = empathy_message_new (body);
529
empathy_message_set_sender (message, sender);
530
empathy_message_set_timestamp (message, t);
531
empathy_message_set_tptype (message, msg_type);
532
empathy_message_set_is_backlog (message, TRUE);
535
empathy_message_set_id (message, cm_id);
537
messages = g_list_append (messages, message);
539
g_object_unref (sender);
542
xmlFree (sender_name);
544
xmlFree (is_user_str);
545
xmlFree (msg_type_str);
547
xmlFree (sender_avatar_token);
550
DEBUG ("Parsed %d messages", g_list_length (messages));
553
xmlFreeParserCtxt (ctxt);
559
log_store_empathy_get_all_files (EmpathyLogStore *self,
565
const gchar *basedir;
566
EmpathyLogStoreEmpathyPriv *priv;
568
priv = GET_PRIV (self);
570
basedir = dir ? dir : priv->basedir;
572
gdir = g_dir_open (basedir, 0, NULL);
576
while ((name = g_dir_read_name (gdir)) != NULL)
580
filename = g_build_filename (basedir, name, NULL);
581
if (g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
583
files = g_list_prepend (files, filename);
587
if (g_file_test (filename, G_FILE_TEST_IS_DIR))
589
/* Recursively get all log files */
590
files = g_list_concat (files,
591
log_store_empathy_get_all_files (self, filename));
603
log_store_empathy_search_new (EmpathyLogStore *self,
608
gchar *text_casefold;
610
g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
611
g_return_val_if_fail (!EMP_STR_EMPTY (text), NULL);
613
text_casefold = g_utf8_casefold (text, -1);
615
files = log_store_empathy_get_all_files (self, NULL);
616
DEBUG ("Found %d log files in total", g_list_length (files));
618
for (l = files; l; l = g_list_next (l))
623
gchar *contents = NULL;
624
gchar *contents_casefold = NULL;
628
file = g_mapped_file_new (filename, FALSE, NULL);
630
goto drinking_island;
632
length = g_mapped_file_get_length (file);
633
contents = g_mapped_file_get_contents (file);
635
if (length == 0 || contents == NULL)
636
goto drinking_island;
638
contents_casefold = g_utf8_casefold (contents, length);
640
if (strstr (contents_casefold, text_casefold))
642
EmpathyLogSearchHit *hit;
644
hit = log_store_empathy_search_hit_new (self, filename);
648
hits = g_list_prepend (hits, hit);
649
DEBUG ("Found text:'%s' in file:'%s' on date:'%s'",
650
text, hit->filename, hit->date);
656
g_mapped_file_unref (file);
658
g_free (contents_casefold);
663
g_free (text_casefold);
669
log_store_empathy_get_chats_for_dir (EmpathyLogStore *self,
671
gboolean is_chatroom)
676
GError *error = NULL;
678
gdir = g_dir_open (dir, 0, &error);
681
DEBUG ("Failed to open directory: %s, error: %s", dir, error->message);
682
g_error_free (error);
686
while ((name = g_dir_read_name (gdir)) != NULL)
688
EmpathyLogSearchHit *hit;
690
if (!is_chatroom && strcmp (name, LOG_DIR_CHATROOMS) == 0)
692
gchar *filename = g_build_filename (dir, name, NULL);
693
hits = g_list_concat (hits, log_store_empathy_get_chats_for_dir (
694
self, filename, TRUE));
698
hit = g_slice_new0 (EmpathyLogSearchHit);
699
hit->chat_id = g_strdup (name);
700
hit->is_chatroom = is_chatroom;
702
hits = g_list_prepend (hits, hit);
712
log_store_empathy_get_messages_for_date (EmpathyLogStore *self,
714
const gchar *chat_id,
721
g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
722
g_return_val_if_fail (chat_id != NULL, NULL);
723
g_return_val_if_fail (account != NULL, NULL);
725
filename = log_store_empathy_get_filename_for_date (self, account,
726
chat_id, chatroom, date);
727
messages = log_store_empathy_get_messages_for_file (self, account,
735
log_store_empathy_get_chats (EmpathyLogStore *self,
740
EmpathyLogStoreEmpathyPriv *priv;
742
priv = GET_PRIV (self);
744
dir = log_store_empathy_get_dir (self, account, NULL, FALSE);
746
hits = log_store_empathy_get_chats_for_dir (self, dir, FALSE);
754
log_store_empathy_get_name (EmpathyLogStore *self)
756
EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
762
log_store_empathy_get_filtered_messages (EmpathyLogStore *self,
764
const gchar *chat_id,
767
EmpathyLogMessageFilter filter,
770
GList *dates, *l, *messages = NULL;
773
dates = log_store_empathy_get_dates (self, account, chat_id, chatroom);
775
for (l = g_list_last (dates); l && i < num_messages; l = g_list_previous (l))
777
GList *new_messages, *n, *next;
779
/* FIXME: We should really restrict the message parsing to get only
780
* the newest num_messages. */
781
new_messages = log_store_empathy_get_messages_for_date (self, account,
782
chat_id, chatroom, l->data);
787
next = g_list_next (n);
788
if (!filter (n->data, user_data))
790
g_object_unref (n->data);
791
new_messages = g_list_delete_link (new_messages, n);
799
messages = g_list_concat (messages, new_messages);
802
g_list_foreach (dates, (GFunc) g_free, NULL);
809
log_store_iface_init (gpointer g_iface,
812
EmpathyLogStoreInterface *iface = (EmpathyLogStoreInterface *) g_iface;
814
iface->get_name = log_store_empathy_get_name;
815
iface->exists = log_store_empathy_exists;
816
iface->add_message = log_store_empathy_add_message;
817
iface->get_dates = log_store_empathy_get_dates;
818
iface->get_messages_for_date = log_store_empathy_get_messages_for_date;
819
iface->get_chats = log_store_empathy_get_chats;
820
iface->search_new = log_store_empathy_search_new;
821
iface->ack_message = NULL;
822
iface->get_filtered_messages = log_store_empathy_get_filtered_messages;