Mercurial > pidgin
comparison libpurple/plugins/log_reader.c @ 18247:97671eb4991e
Avoid some extra nesting.
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Sun, 24 Jun 2007 23:37:53 +0000 |
parents | e236b3bd4542 |
children | ab761acbc614 |
comparison
equal
deleted
inserted
replaced
18246:e236b3bd4542 | 18247:97671eb4991e |
---|---|
1749 { | 1749 { |
1750 GList *list = NULL; | 1750 GList *list = NULL; |
1751 const char *logdir; | 1751 const char *logdir; |
1752 PurplePlugin *plugin; | 1752 PurplePlugin *plugin; |
1753 PurplePluginProtocolInfo *prpl_info; | 1753 PurplePluginProtocolInfo *prpl_info; |
1754 const char *buddy_name; | |
1755 char *username; | 1754 char *username; |
1756 char *filename; | 1755 char *filename; |
1757 char *path; | 1756 char *path; |
1758 gsize length; | 1757 gsize length; |
1759 GError *error = NULL; | 1758 GError *error = NULL; |
1760 gchar *contents = NULL; | 1759 char *contents = NULL; |
1760 struct qip_logger_data *data = NULL; | |
1761 char *utf8_string = NULL; | |
1762 struct tm prev_tm; | |
1763 gboolean prev_tm_init = FALSE; | |
1764 char *c; | |
1765 char *start_log; | |
1766 char *escaped; | |
1767 int offset = 0; | |
1761 | 1768 |
1762 g_return_val_if_fail(sn != NULL, list); | 1769 g_return_val_if_fail(sn != NULL, list); |
1763 g_return_val_if_fail(account != NULL, list); | 1770 g_return_val_if_fail(account != NULL, list); |
1764 | 1771 |
1765 /* QIP is ICQ messenger. Should we add prpl-aim? */ | 1772 /* QIP only supports ICQ. */ |
1766 if (strcmp(account->protocol_id, "prpl-icq")) | 1773 if (strcmp(account->protocol_id, "prpl-icq")) |
1767 return list; | 1774 return list; |
1768 | 1775 |
1769 logdir = purple_prefs_get_string("/plugins/core/log_reader/qip/log_directory"); | 1776 logdir = purple_prefs_get_string("/plugins/core/log_reader/qip/log_directory"); |
1770 | 1777 |
1778 | 1785 |
1779 prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(plugin); | 1786 prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(plugin); |
1780 if (!prpl_info->list_icon) | 1787 if (!prpl_info->list_icon) |
1781 return NULL; | 1788 return NULL; |
1782 | 1789 |
1783 buddy_name = g_strdup(purple_normalize(account, sn)); | |
1784 | |
1785 username = g_strdup(purple_normalize(account, account->username)); | 1790 username = g_strdup(purple_normalize(account, account->username)); |
1786 | 1791 filename = g_strdup_printf("%s.txt", purple_normalize(account, sn)); |
1787 filename = g_strdup_printf("%s.txt", buddy_name); | 1792 path = g_build_filename(logdir, username, "History", filename, NULL); |
1788 path = g_build_filename( | 1793 g_free(username); |
1789 logdir, username, "History", filename, NULL); | 1794 g_free(filename); |
1790 | 1795 |
1791 purple_debug_info("QIP logger list", "Reading %s\n", path); | 1796 purple_debug_info("QIP logger list", "Reading %s\n", path); |
1792 | 1797 |
1793 error = NULL; | 1798 error = NULL; |
1794 if (!g_file_get_contents(path, &contents, &length, &error)) { | 1799 if (!g_file_get_contents(path, &contents, &length, &error)) { |
1795 if (error) { | 1800 purple_debug_error("QIP logger list", |
1796 purple_debug_error("QIP logger list", | 1801 "Couldn't read file %s: %s \n", path, error->message); |
1797 "Couldn't read file %s \n", path); | 1802 g_error_free(error); |
1798 | 1803 |
1799 g_error_free(error); | 1804 g_free(path); |
1800 } | 1805 return list; |
1801 } else if (contents) { | 1806 } |
1802 struct qip_logger_data *data = NULL; | 1807 |
1803 gchar * utf8_string = NULL; | 1808 g_return_val_if_fail(contents != NULL, list); |
1804 | 1809 |
1805 purple_debug_info("QIP logger list", "File %s is found\n", filename); | 1810 purple_debug_info("QIP logger list", "File %s is found\n", path); |
1811 | |
1812 /* Convert file contents from Cp1251 to UTF-8 codeset */ | |
1813 error = NULL; | |
1814 if (!(utf8_string = g_convert(contents, length, "UTF-8", "Cp1251", NULL, NULL, &error))) { | |
1815 purple_debug_error("QIP logger list", | |
1816 "Couldn't convert file %s to UTF-8: %s\n", path, error->message); | |
1817 g_error_free(error); | |
1818 | |
1819 g_free(path); | |
1820 g_free(contents); | |
1821 return list; | |
1822 } | |
1823 | |
1824 g_free(contents); | |
1825 contents = g_markup_escape_text(utf8_string, -1); | |
1826 g_free(utf8_string); | |
1827 | |
1828 c = contents; | |
1829 start_log = contents; | |
1830 while (*c) { | |
1831 if (purple_str_has_prefix(c, QIP_LOG_IN_MESSAGE_ESC) || | |
1832 purple_str_has_prefix(c, QIP_LOG_OUT_MESSAGE_ESC)) { | |
1833 | |
1834 gchar *new_line = c; | |
1806 | 1835 |
1807 /* We should convert file contents from Cp1251 to UTF-8 codeset */ | 1836 /* find EOL */ |
1808 error = NULL; | 1837 c = strstr(c, "\n"); |
1809 if (!(utf8_string = g_convert(contents, length, "UTF-8", "Cp1251", NULL, NULL, &error))) { | 1838 c++; |
1810 if (error) { | 1839 |
1811 purple_debug_error("QIP logger list", | 1840 /* Find the last '(' character. */ |
1812 "Couldn't convert file %s to UTF-8\n", filename); | 1841 if ((tmp = strstr(c, "\n")) != NULL) |
1813 g_error_free(error); | 1842 c = g_strrstr(tmp, "("); |
1843 else { | |
1844 while (*c) | |
1845 c++; | |
1846 c--; | |
1847 c = g_strrstr(c, "("); | |
1814 } | 1848 } |
1815 } else { | 1849 |
1816 struct tm prev_tm; | 1850 if (c != NULL) { |
1817 gboolean prev_tm_init = FALSE; | 1851 const char *timestamp = ++c; |
1818 gchar *c; | 1852 struct tm tm; |
1819 gchar *start_log; | 1853 |
1820 gchar *escaped; | 1854 /* Parse the time, day, month and year */ |
1821 int offset = 0; | 1855 if (sscanf(timestamp, "%u:%u:%u %u/%u/%u", |
1822 | 1856 &tm.tm_hour, &tm.tm_min, &tm.tm_sec, |
1823 purple_debug_info("QIP logger list", | 1857 &tm.tm_mday, &tm.tm_mon, &tm.tm_year) != 6) { |
1824 "File %s converted successfully\n", filename); | 1858 |
1825 | 1859 purple_debug_error("QIP logger list", |
1826 g_free(contents); | 1860 "Parsing timestamp error\n"); |
1827 escaped = g_markup_escape_text(utf8_string, -1); | 1861 } else { |
1828 contents = escaped; | 1862 tm.tm_mon -= 1; |
1829 | 1863 tm.tm_year -= 1900; |
1830 c = contents; | 1864 |
1831 start_log = contents; | 1865 /* Let the C library deal with |
1832 while (*c) { | 1866 * daylight savings time. */ |
1833 if (purple_str_has_prefix(c, QIP_LOG_IN_MESSAGE_ESC) || | 1867 tm.tm_isdst = -1; |
1834 purple_str_has_prefix(c, QIP_LOG_OUT_MESSAGE_ESC)) { | 1868 |
1835 gchar *new_line = c; | 1869 if (!prev_tm_init) { |
1836 | 1870 prev_tm = tm; |
1837 purple_debug_info("QIP logger list", | 1871 prev_tm_init = TRUE; |
1838 "Find message\n", filename); | 1872 } else { |
1839 | 1873 double time_diff = difftime(mktime(&tm), mktime(&prev_tm)); |
1840 /* find EOL */ | 1874 |
1841 c = strstr(c, "\n"); | 1875 if (time_diff > QIP_LOG_TIMEOUT) { |
1842 c++; | 1876 PurpleLog *log; |
1843 /* searching '(' character from the end of the line */ | |
1844 c = strstr(c, "\n"); | |
1845 while (*c && *c != '(') | |
1846 --c; | |
1847 | |
1848 if (*c == '(') { | |
1849 const char *timestamp = ++c; | |
1850 struct tm tm; | |
1851 purple_debug_info("QIP logger list", "Timestap found\n"); | |
1852 | |
1853 /* Parse the time, day, month and year */ | |
1854 if (sscanf(timestamp, "%u:%u:%u %u/%u/%u", | |
1855 &tm.tm_hour, &tm.tm_min, &tm.tm_sec, | |
1856 &tm.tm_mday, &tm.tm_mon, &tm.tm_year) != 6) { | |
1857 purple_debug_error("QIP logger list", | |
1858 "Parsing timestamp error\n"); | |
1859 } else { | |
1860 /* cos month of year in [0,11] */ | |
1861 tm.tm_mon -= 1; | |
1862 /* cos years since 1900 */ | |
1863 tm.tm_year -= 1900; | |
1864 | |
1865 /* Let the C library deal with | |
1866 * daylight savings time. */ | |
1867 tm.tm_isdst = -1; | |
1868 | |
1869 if (!prev_tm_init) { | |
1870 prev_tm = tm; | |
1871 prev_tm_init = TRUE; | |
1872 | |
1873 } else { | |
1874 double time_diff = 0; | |
1875 | |
1876 time_diff = difftime(mktime(&tm), mktime(&prev_tm)); | |
1877 | |
1878 if (time_diff > QIP_LOG_TIMEOUT) { | |
1879 PurpleLog *log; | |
1880 | |
1881 /* filling data */ | |
1882 data = g_new0( | |
1883 struct qip_logger_data, 1); | |
1884 data->path = g_strdup(path); | |
1885 data->length = new_line - start_log; | |
1886 data->offset = offset; | |
1887 offset += data->length; | |
1888 | |
1889 purple_debug_error("QIP logger list", | |
1890 "Creating log: path = (%s); length = (%d); offset = (%d)\n", data->path, data->length, data->offset); | |
1891 | |
1892 /* XXX: Look into this later... Should we pass in a struct tm? */ | |
1893 log = purple_log_new(PURPLE_LOG_IM, | |
1894 sn, account, NULL, mktime(&prev_tm), NULL); | |
1895 | |
1896 log->logger = qip_logger; | |
1897 log->logger_data = data; | |
1898 | |
1899 list = g_list_append(list, log); | |
1900 | |
1901 prev_tm = tm; | |
1902 start_log = new_line; | |
1903 } | |
1904 } | |
1905 | 1877 |
1906 /* find EOF */ | 1878 /* filling data */ |
1907 c = strstr(c, "\n"); | 1879 data = g_new0(struct qip_logger_data, 1); |
1908 c++; | 1880 data->path = g_strdup(path); |
1881 data->length = new_line - start_log; | |
1882 data->offset = offset; | |
1883 offset += data->length; | |
1884 | |
1885 purple_debug_error("QIP logger list", | |
1886 "Creating log: path = (%s); length = (%d); offset = (%d)\n", data->path, data->length, data->offset); | |
1887 | |
1888 /* XXX: Look into this later... Should we pass in a struct tm? */ | |
1889 log = purple_log_new(PURPLE_LOG_IM, sn, account, | |
1890 NULL, mktime(&prev_tm), NULL); | |
1891 | |
1892 log->logger = qip_logger; | |
1893 log->logger_data = data; | |
1894 | |
1895 list = g_list_append(list, log); | |
1896 | |
1897 prev_tm = tm; | |
1898 start_log = new_line; | |
1909 } | 1899 } |
1910 } | 1900 } |
1911 } else { | 1901 |
1902 /* find EOF */ | |
1912 c = strstr(c, "\n"); | 1903 c = strstr(c, "\n"); |
1913 c++; | 1904 c++; |
1914 } | 1905 } |
1915 } | 1906 } |
1916 | 1907 } else { |
1917 /* adding last log */ | 1908 c = strstr(c, "\n"); |
1918 if (prev_tm_init) { | 1909 c++; |
1919 PurpleLog *log; | 1910 } |
1920 | 1911 } |
1921 /* filling data */ | 1912 |
1922 data = g_new0( | 1913 /* adding last log */ |
1923 struct qip_logger_data, 1); | 1914 if (prev_tm_init) { |
1924 data->path = g_strdup(path); | 1915 PurpleLog *log; |
1925 data->length = c - start_log; | 1916 |
1926 data->offset = offset; | 1917 /* filling data */ |
1927 offset += data->length; | 1918 data = g_new0( |
1928 purple_debug_error("QIP logger list", | 1919 struct qip_logger_data, 1); |
1929 "Creating log: path = (%s); length = (%d); offset = (%d)\n", data->path, data->length, data->offset); | 1920 data->path = g_strdup(path); |
1930 | 1921 data->length = c - start_log; |
1931 /* XXX: Look into this later... Should we pass in a struct tm? */ | 1922 data->offset = offset; |
1932 log = purple_log_new(PURPLE_LOG_IM, sn, account, | 1923 offset += data->length; |
1933 NULL, mktime(&prev_tm), NULL); | 1924 purple_debug_error("QIP logger list", |
1934 | 1925 "Creating log: path = (%s); length = (%d); offset = (%d)\n", data->path, data->length, data->offset); |
1935 log->logger = qip_logger; | 1926 |
1936 log->logger_data = data; | 1927 /* XXX: Look into this later... Should we pass in a struct tm? */ |
1937 | 1928 log = purple_log_new(PURPLE_LOG_IM, sn, account, |
1938 list = g_list_append(list, log); | 1929 NULL, mktime(&prev_tm), NULL); |
1939 } | 1930 |
1940 } | 1931 log->logger = qip_logger; |
1941 g_free(contents); | 1932 log->logger_data = data; |
1942 } | 1933 |
1943 | 1934 list = g_list_append(list, log); |
1944 g_free(username); | 1935 } |
1936 | |
1937 g_free(contents); | |
1945 g_free(path); | 1938 g_free(path); |
1946 g_free(filename); | |
1947 | |
1948 return list; | 1939 return list; |
1949 } | 1940 } |
1950 | 1941 |
1951 static char * qip_logger_read (PurpleLog *log, PurpleLogReadFlags *flags) | 1942 static char * qip_logger_read (PurpleLog *log, PurpleLogReadFlags *flags) |
1952 { | 1943 { |