comparison libpurple/plugins/log_reader.c @ 18170:179bb0ea89c7

Using g_file_get_contents instead of common file operations; all DEBUG_MESSAGEs were removed
author Michael Shkutkov <mshkutkov@soc.pidgin.im>
date Wed, 20 Jun 2007 22:42:01 +0000
parents e9d751afc90b
children 6935c7bbd310
comparison
equal deleted inserted replaced
18169:e9d751afc90b 18170:179bb0ea89c7
1742 #define QIP_LOG_IN_MESSAGE (QIP_LOG_DELIMITER "<-") 1742 #define QIP_LOG_IN_MESSAGE (QIP_LOG_DELIMITER "<-")
1743 #define QIP_LOG_OUT_MESSAGE (QIP_LOG_DELIMITER ">-") 1743 #define QIP_LOG_OUT_MESSAGE (QIP_LOG_DELIMITER ">-")
1744 #define QIP_LOG_IN_MESSAGE_ESC (QIP_LOG_DELIMITER "&lt;-") 1744 #define QIP_LOG_IN_MESSAGE_ESC (QIP_LOG_DELIMITER "&lt;-")
1745 #define QIP_LOG_OUT_MESSAGE_ESC (QIP_LOG_DELIMITER "&gt;-") 1745 #define QIP_LOG_OUT_MESSAGE_ESC (QIP_LOG_DELIMITER "&gt;-")
1746 1746
1747 #define DEBUG_MESSAGE(var, sign, value, title) if(var sign value) \
1748 purple_debug(PURPLE_DEBUG_ERROR, title, \
1749 #var " " #sign " " #value "\n");
1750
1751 static PurpleLogLogger *qip_logger; 1747 static PurpleLogLogger *qip_logger;
1752 static void qip_logger_finalize(PurpleLog *log); 1748 static void qip_logger_finalize(PurpleLog *log);
1753 1749
1754 struct qip_logger_data { 1750 struct qip_logger_data {
1755 1751
1898 } 1894 }
1899 1895
1900 static char * qip_logger_read (PurpleLog *log, PurpleLogReadFlags *flags) 1896 static char * qip_logger_read (PurpleLog *log, PurpleLogReadFlags *flags)
1901 { 1897 {
1902 struct qip_logger_data *data; 1898 struct qip_logger_data *data;
1903 char *read;
1904 FILE *file;
1905 PurpleBuddy *buddy; 1899 PurpleBuddy *buddy;
1906 char *escaped; 1900 char *escaped;
1907 GString *formatted; 1901 GString *formatted;
1908 char *c; 1902 char *c;
1909 const char *line; 1903 const char *line;
1904 GError *error = NULL;
1905 gchar *contents = NULL;
1906 gsize length;
1910 1907
1911 purple_debug(PURPLE_DEBUG_INFO, "QIP logger read",
1912 "start\n");
1913
1914 DEBUG_MESSAGE(log, ==, NULL, "QIP logger read");
1915 g_return_val_if_fail(log != NULL, g_strdup("")); 1908 g_return_val_if_fail(log != NULL, g_strdup(""));
1916 1909
1917 data = log->logger_data; 1910 data = log->logger_data;
1918 1911
1919 DEBUG_MESSAGE(data->path, ==, NULL, "QIP logger read");
1920 g_return_val_if_fail(data->path != NULL, g_strdup("")); 1912 g_return_val_if_fail(data->path != NULL, g_strdup(""));
1921
1922 DEBUG_MESSAGE(data->length, <=, 0, "QIP logger read");
1923 g_return_val_if_fail(data->length > 0, g_strdup("")); 1913 g_return_val_if_fail(data->length > 0, g_strdup(""));
1924 1914
1925 1915
1926 purple_debug(PURPLE_DEBUG_INFO, "QIP logger read", 1916 purple_debug(PURPLE_DEBUG_INFO, "QIP logger read",
1927 "Reading %s\n", data->path); 1917 "Reading %s\n", data->path);
1928 1918
1929 read = g_malloc(data->length + 2); 1919 if (!g_file_get_contents(data->path, &contents, &length, &error))
1930 1920 if (error)
1931 file = g_fopen(data->path, "rb"); 1921 g_error_free(error);
1932 fseek(file, data->offset, SEEK_SET); 1922
1933 fread(read, data->length, 1, file); 1923 if (contents) {
1934 fclose(file); 1924 /* Load miscellaneous data. */
1935 1925 buddy = purple_find_buddy(log->account, log->name);
1936 if (read[data->length-1] == '\n') {
1937 read[data->length] = '\0';
1938 } else {
1939 read[data->length] = '\n';
1940 read[data->length+1] = '\0';
1941 }
1942
1943 /* Load miscellaneous data. */
1944 buddy = purple_find_buddy(log->account, log->name);
1945
1946 escaped = g_markup_escape_text(read, -1);
1947 g_free(read);
1948 read = escaped;
1949
1950 /* Apply formatting... */
1951 formatted = g_string_sized_new(strlen(read));
1952 c = read;
1953 line = read;
1954
1955 while (*c)
1956 {
1957 gboolean is_in_message = FALSE;
1958 1926
1959 if (purple_str_has_prefix(line, QIP_LOG_IN_MESSAGE_ESC) || 1927 escaped = g_markup_escape_text(contents, -1);
1960 purple_str_has_prefix(line, QIP_LOG_OUT_MESSAGE_ESC)) { 1928 g_free(contents);
1961 const char *buddy_name; 1929 contents = escaped;
1962 is_in_message = purple_str_has_prefix(line, QIP_LOG_IN_MESSAGE_ESC); 1930
1931 /* Apply formatting... */
1932 formatted = g_string_sized_new(strlen(contents));
1933 c = contents;
1934 line = contents;
1935
1936 while (*c)
1937 {
1938 gboolean is_in_message = FALSE;
1963 1939
1964 purple_debug(PURPLE_DEBUG_INFO, "QIP loggger read", 1940 if (purple_str_has_prefix(line, QIP_LOG_IN_MESSAGE_ESC) ||
1965 "%s message\n", (is_in_message) ? "incoming" : "Outgoing"); 1941 purple_str_has_prefix(line, QIP_LOG_OUT_MESSAGE_ESC)) {
1942 const char *buddy_name;
1943 is_in_message = purple_str_has_prefix(line, QIP_LOG_IN_MESSAGE_ESC);
1944
1945 purple_debug(PURPLE_DEBUG_INFO, "QIP loggger read",
1946 "%s message\n", (is_in_message) ? "incoming" : "Outgoing");
1947
1948 /* find EOL */
1949 c = strstr(c, "\n");
1950
1951 /* XXX: Do we need buddy_name when we have buddy->alias? */
1952 buddy_name = ++c;
1953
1954 /* searching '(' character from the end of the line */
1955 c = strstr(c, "\n");
1956 while (*c && *c != '(')
1957 --c;
1958
1959 if (*c == '(') {
1960 const char *timestamp = c;
1961 int hour;
1962 int min;
1963 int sec;
1964
1965 timestamp++;
1966
1967 /* Parse the time, day, month and year */
1968 if (sscanf(timestamp, "%u:%u:%u",
1969 &hour, &min, &sec) != 3)
1970 purple_debug(PURPLE_DEBUG_ERROR, "QIP logger read",
1971 "Parsing timestamp error\n");
1972 else {
1973 g_string_append(formatted, "<font size=\"2\">");
1974 g_string_append_printf(formatted,
1975 "(%u:%02u:%02u) %cM ", hour % 12,
1976 min, sec, (hour >= 12) ? 'P': 'A');
1977 g_string_append(formatted, "</font> ");
1978
1979 if (is_in_message) {
1980 if (buddy_name != NULL && buddy->alias) {
1981 g_string_append_printf(formatted,
1982 "<span style=\"color: #A82F2F;\">"
1983 "<b>%s</b></span>: ", buddy->alias);
1984 }
1985 } else {
1986 const char *acct_name;
1987 acct_name = purple_account_get_alias(log->account);
1988 if (!acct_name)
1989 acct_name = purple_account_get_username(log->account);
1990
1991 g_string_append_printf(formatted,
1992 "<span style=\"color: #16569E;\">"
1993 "<b>%s</b></span>: ", acct_name);
1994 }
1995
1996 /* find EOF */
1997 c = strstr(c, "\n");
1998
1999 line = ++c;
2000
2001 if ((c = strstr(c, "\n")))
2002 *c = '\0';
2003
2004 purple_debug(PURPLE_DEBUG_INFO, "QIP logger read",
2005 "writing message: \"%s\"\n", line);
2006
2007 g_string_append(formatted, line);
2008 line = ++c;
2009 g_string_append_c(formatted, '\n');
2010 }
2011 }
2012 } else {
2013 if ((c = strchr(c, '\n')))
2014 *c = '\0';
1966 2015
1967 /* find EOL */ 2016 if (line[0] != '\n' && line[0] != '\r') {
1968 c = strstr(c, "\n"); 2017 purple_debug(PURPLE_DEBUG_INFO, "QIP logger read",
1969 2018 "line is not delimiter \"%s\"\n", line);
1970 /* XXX: Do we need buddy_name when we have buddy->alias? */
1971 buddy_name = ++c;
1972
1973
1974 /* searching '(' character from the end of the line */
1975 c = strstr(c, "\n");
1976 while (*c && *c != '(')
1977 --c;
1978
1979 if (*c == '(') {
1980 const char *timestamp = c;
1981 int hour;
1982 int min;
1983 int sec;
1984
1985 timestamp++;
1986 2019
1987 /* Parse the time, day, month and year */
1988 if (sscanf(timestamp, "%u:%u:%u",
1989 &hour, &min, &sec) != 3)
1990 purple_debug(PURPLE_DEBUG_ERROR, "QIP logger read",
1991 "Parsing timestamp error\n");
1992 else {
1993 g_string_append(formatted, "<font size=\"2\">");
1994 g_string_append_printf(formatted,
1995 "(%u:%02u:%02u) %cM ", hour % 12,
1996 min, sec, (hour >= 12) ? 'P': 'A');
1997 g_string_append(formatted, "</font> ");
1998
1999 if (is_in_message) {
2000 if (buddy_name != NULL && buddy->alias) {
2001 g_string_append_printf(formatted,
2002 "<span style=\"color: #A82F2F;\">"
2003 "<b>%s</b></span>: ", buddy->alias);
2004 }
2005 } else {
2006 const char *acct_name;
2007 acct_name = purple_account_get_alias(log->account);
2008 if (!acct_name)
2009 acct_name = purple_account_get_username(log->account);
2010
2011 g_string_append_printf(formatted,
2012 "<span style=\"color: #16569E;\">"
2013 "<b>%s</b></span>: ", acct_name);
2014 }
2015
2016 /* find EOF */
2017 c = strstr(c, "\n");
2018
2019 line = ++c;
2020
2021 if ((c = strstr(c, "\n")))
2022 *c = '\0';
2023
2024 purple_debug(PURPLE_DEBUG_INFO, "QIP logger read",
2025 "writing message: \"%s\"\n", line);
2026
2027 g_string_append(formatted, line); 2020 g_string_append(formatted, line);
2028 line = ++c;
2029 g_string_append_c(formatted, '\n'); 2021 g_string_append_c(formatted, '\n');
2030 } 2022 }
2023 line = ++c;
2031 } 2024 }
2032 } else { 2025 }
2033 if ((c = strchr(c, '\n'))) 2026 }
2034 *c = '\0'; 2027 g_free(contents);
2035
2036 if (line[0] != '\n' && line[0] != '\r') {
2037 purple_debug(PURPLE_DEBUG_INFO, "QIP logger read",
2038 "line is not delimiter \"%s\"\n", line);
2039
2040 g_string_append(formatted, line);
2041 g_string_append_c(formatted, '\n');
2042 }
2043 line = ++c;
2044 }
2045 }
2046 g_free(read);
2047 /* XXX: TODO: Avoid this g_strchomp() */ 2028 /* XXX: TODO: Avoid this g_strchomp() */
2048 return g_strchomp(g_string_free(formatted, FALSE)); 2029 return g_strchomp(g_string_free(formatted, FALSE));
2049 } 2030 }
2050 2031
2051 static int qip_logger_size (PurpleLog *log) 2032 static int qip_logger_size (PurpleLog *log)