/soc/2015/igor.gajowiak/chatlog: 48b7ccc5444a: Fixed parsing mes...
Igor Gajowiak
igor.gajowiak at gmail.com
Thu Aug 6 14:27:39 EDT 2015
Changeset: 48b7ccc5444ac72c1539676158fdf1453ba8322b
Author: Igor Gajowiak <igor.gajowiak at gmail.com>
Date: 2015-08-06 20:27 +0200
Branch: default
URL: https://hg.pidgin.im/soc/2015/igor.gajowiak/chatlog/rev/48b7ccc5444a
Description:
Fixed parsing message timestamp from the legacy log.
diffstat:
libpurple/plugins/log/legacylog.c | 108 +++++++++++++++++++++++++------------
1 files changed, 72 insertions(+), 36 deletions(-)
diffs (181 lines):
diff --git a/libpurple/plugins/log/legacylog.c b/libpurple/plugins/log/legacylog.c
--- a/libpurple/plugins/log/legacylog.c
+++ b/libpurple/plugins/log/legacylog.c
@@ -37,6 +37,10 @@
/* PurpleGenericLog API implementation */
/**************************************************************************/
+static GRegex *msg_regex = NULL;
+static GRegex *date_regex = NULL;
+static GRegex *time_regex = NULL;
+
typedef enum
{
DIR_DEPTH_PROTOCOL = 1,
@@ -302,37 +306,54 @@ normalize_protocol_name(const gchar *pro
return protocol;
}
-/**
- * TODO: Use regex
- *
- * date "YYYY-MM-DD.hhmmss"
- * time "HH:MM:SS (A|P)M"
- */
-static guint64
-make_timestamp(const gchar *date, const gchar *msg_time)
+static gboolean
+parse_timestamp(const gchar *date, const gchar *time, guint64 *result)
{
- int year = atoi(date);
- date += 5;
- int month = atoi(date);
- date += 3;
- int day = atoi(date);
- date += 3;
+ g_assert(date);
+ g_assert(time);
+ g_assert(result);
- int hour = 0;
- for(int i = 0; i < 2; ++i, ++msg_time)
- hour = hour * 10 + (*msg_time - '0');
- ++msg_time;
- int min = 0;
- for(int i = 0; i < 2; ++i, ++msg_time)
- min = min * 10 + (*msg_time - '0');
- ++msg_time;
- int sec = 0;
- for(int i = 0; i < 2; ++i, ++msg_time)
- sec = sec * 10 + (*msg_time - '0');
- if(*(++msg_time) == 'P')
+ GMatchInfo *info = NULL;
+ gint begin, end;
+
+ if (!g_regex_match(date_regex, date, 0, &info)) {
+ g_match_info_free(info);
+ return FALSE;
+ }
+
+ g_match_info_fetch_pos(info, 1, &begin, &end);
+ guint64 year = g_ascii_strtoull(&date[begin], NULL, 10);
+
+ g_match_info_fetch_pos(info, 2, &begin, &end);
+ guint64 month = g_ascii_strtoull(&date[begin], NULL, 10);
+
+ g_match_info_fetch_pos(info, 3, &begin, &end);
+ guint64 day = g_ascii_strtoull(&date[begin], NULL, 10);
+
+ g_match_info_free(info);
+
+ if (!g_regex_match(time_regex, time, 0, &info)) {
+ g_match_info_free(info);
+ return FALSE;
+ }
+
+ g_match_info_fetch_pos(info, 1, &begin, &end);
+ guint64 hour = g_ascii_strtoull(&time[begin], NULL, 10);
+
+ g_match_info_fetch_pos(info, 2, &begin, &end);
+ guint64 min = g_ascii_strtoull(&time[begin], NULL, 10);
+
+ g_match_info_fetch_pos(info, 3, &begin, &end);
+ guint64 sec = g_ascii_strtoull(&time[begin], NULL, 10);
+
+ g_match_info_fetch_pos(info, 4, &begin, &end);
+ if (g_strcmp0(&time[begin], "PM") == 0)
hour += 12;
+
+ g_match_info_free(info);
- return purple_time_build(year, month, day, hour, min, sec);
+ *result = purple_time_build(year, month, day, hour, min, sec);
+ return TRUE;
}
static PurpleAccount*
@@ -353,6 +374,10 @@ make_message(PurpleAccount *account, con
if (!buddy)
return NULL;
+ guint64 timestamp;
+ if(!parse_timestamp(iter->log_file_name, msg_time, ×tamp))
+ return NULL;
+
PurpleMessage *msg;
if (strcmp(color, "#A82F2F") == 0) {
msg = purple_message_new_incoming(purple_buddy_get_name(buddy),
@@ -363,7 +388,6 @@ make_message(PurpleAccount *account, con
contents, PURPLE_MESSAGE_SEND);
}
- guint64 timestamp = make_timestamp(iter->log_file_name, msg_time);
purple_message_set_time(msg, timestamp);
return msg;
@@ -377,7 +401,6 @@ remove_divs(gchar *text)
for (size_t i = 0; i < len && text[i] != '\0';) {
if (len - i >= 5 && g_str_has_prefix(&text[i], "<div>")) {
- g_string_append_c(result, '\n');
i += 5;
}
else if (len - i >= 6 && g_str_has_prefix(&text[i], "</div>")) {
@@ -396,13 +419,8 @@ remove_divs(gchar *text)
static int
fill_arrays(gchar *text, GArray *accounts, GArray *messages, const Iter *iter)
{
- GRegex *regex = g_regex_new(
- "<font color=\"(.*?)\"><font size=.*?>\\((.*?)\\)</font> .*?"
- "</b></font>(.*?)<br/>",
- 0, 0, NULL);
-
GMatchInfo *info;
- g_regex_match(regex, text, 0, &info);
+ g_regex_match(msg_regex, text, 0, &info);
int msgs_count = 0;
while (g_match_info_matches(info)) {
@@ -435,7 +453,6 @@ fill_arrays(gchar *text, GArray *account
}
g_match_info_free(info);
- g_regex_unref(regex);
return msgs_count;
}
@@ -630,15 +647,34 @@ plugin_load(PurplePlugin *plugin, GError
PURPLE_GENERICLOG_PROPERTY_NAME, LEGACYLOG_NAME,
NULL);
purple_genericlog_register(PURPLE_GENERICLOG(legacylog_instance));
+
+ msg_regex = g_regex_new(
+ "<font color=\"(.*?)\"><font size=.*?>\\((.*?)\\)</font> .*?"
+ "</b></font>(.*?)<br/>",
+ 0, 0, NULL);
+ g_assert(msg_regex);
+
+ date_regex = g_regex_new("^([\\d]{4})-([\\d]{2})-([\\d]{2})\\.",0, 0, NULL);
+ g_assert(date_regex);
+
+ time_regex = g_regex_new("^([\\d]{2}):([\\d]{2}):([\\d]{2}) (AM|PM)",
+ 0, 0, NULL);
+ g_assert(time_regex);
+
return TRUE;
}
static gboolean
plugin_unload(PurplePlugin *plugin, GError **error)
{
+ g_regex_unref(msg_regex);
+ g_regex_unref(date_regex);
+ g_regex_unref(time_regex);
+
purple_genericlog_unregister(PURPLE_GENERICLOG(legacylog_instance));
g_object_unref(legacylog_instance);
legacylog_instance = NULL;
+
return TRUE;
}
More information about the Commits
mailing list