Revision 3245 libsylph/procmsg.c

View differences:

procmsg.c
1 1
/*
2 2
 * LibSylph -- E-Mail client library
3
 * Copyright (C) 1999-2011 Hiroyuki Yamamoto
3
 * Copyright (C) 1999-2013 Hiroyuki Yamamoto
4 4
 *
5 5
 * This library is free software; you can redistribute it and/or
6 6
 * modify it under the terms of the GNU Lesser General Public
......
1725 1725
	return 0;
1726 1726
}
1727 1727

  
1728
static void procmsg_write_headers(MsgInfo *msginfo, MimeInfo *partinfo,
1729
				  FILE *fp, FILE *dest_fp,
1730
				  const gchar *encoding, gboolean all_headers)
1731
{
1732
	GPtrArray *headers;
1733
	gint i;
1734

  
1735
	if (all_headers)
1736
		headers = procheader_get_header_array_asis(fp, NULL);
1737
	else
1738
		headers = procheader_get_header_array_for_display(fp, NULL);
1739

  
1740
	for (i = 0; i < headers->len; i++) {
1741
		Header *hdr;
1742
		gchar *file_str;
1743
		const gchar *body;
1744

  
1745
		hdr = g_ptr_array_index(headers, i);
1746

  
1747
		if (partinfo) {
1748
			if (!g_ascii_strcasecmp(hdr->name, "Subject") ||
1749
			    !g_ascii_strcasecmp(hdr->name, "From") ||
1750
			    !g_ascii_strcasecmp(hdr->name, "To") ||
1751
			    !g_ascii_strcasecmp(hdr->name, "Cc")) {
1752
				unfold_line(hdr->body);
1753
			}
1754

  
1755
			body = hdr->body;
1756
			while (g_ascii_isspace(*body))
1757
				body++;
1758
		} else {
1759
			if (!g_ascii_strcasecmp(hdr->name, "Subject"))
1760
				body = msginfo->subject;
1761
			else if (!g_ascii_strcasecmp(hdr->name, "From"))
1762
				body = msginfo->from;
1763
			else if (!g_ascii_strcasecmp(hdr->name, "To"))
1764
				body = msginfo->to;
1765
			else if (!g_ascii_strcasecmp(hdr->name, "Cc")) {
1766
				unfold_line(hdr->body);
1767
				body = hdr->body;
1768
				while (g_ascii_isspace(*body))
1769
					body++;
1770
			} else {
1771
				body = hdr->body;
1772
				while (g_ascii_isspace(*body))
1773
					body++;
1774
			}
1775
		}
1776

  
1777
		if (body && *body != '\0') {
1778
			file_str = conv_codeset_strdup
1779
				(body, CS_INTERNAL, encoding);
1780
			fprintf(dest_fp, "%s: %s\n", hdr->name,
1781
				file_str ? file_str : body);
1782
			g_free(file_str);
1783
		} else {
1784
			fprintf(dest_fp, "%s: (none)\n", hdr->name);
1785
		}
1786
	}
1787

  
1788
	procheader_header_array_destroy(headers);
1789
}
1790

  
1728 1791
void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline,
1729 1792
			   gboolean all_headers)
1730 1793
{
1731 1794
	gchar *prtmp;
1732 1795
	FILE *msgfp, *tmpfp, *prfp;
1733
	GPtrArray *headers;
1734
	gint i;
1735 1796
	gchar buf[BUFFSIZE];
1736 1797

  
1737 1798
	g_return_if_fail(msginfo != NULL);
......
1760 1821
		return;
1761 1822
	}
1762 1823

  
1763
	if (all_headers)
1764
		headers = procheader_get_header_array_asis(msgfp, NULL);
1765
	else
1766
		headers = procheader_get_header_array_for_display(msgfp, NULL);
1824
	procmsg_write_headers(msginfo, NULL, msgfp, prfp,
1825
			      conv_get_locale_charset_str(), all_headers);
1767 1826

  
1768 1827
	fclose(msgfp);
1769 1828

  
1770
	for (i = 0; i < headers->len; i++) {
1771
		Header *hdr;
1772
		gchar *locale_str;
1773
		const gchar *body;
1774

  
1775
		hdr = g_ptr_array_index(headers, i);
1776

  
1777
		if (!g_ascii_strcasecmp(hdr->name, "Subject"))
1778
			body = msginfo->subject;
1779
		else if (!g_ascii_strcasecmp(hdr->name, "From"))
1780
			body = msginfo->from;
1781
		else if (!g_ascii_strcasecmp(hdr->name, "To"))
1782
			body = msginfo->to;
1783
		else if (!g_ascii_strcasecmp(hdr->name, "Cc")) {
1784
			unfold_line(hdr->body);
1785
			body = hdr->body;
1786
			while (g_ascii_isspace(*body))
1787
				body++;
1788
		} else {
1789
			body = hdr->body;
1790
			while (g_ascii_isspace(*body))
1791
				body++;
1792
		}
1793

  
1794
		if (body && *body != '\0') {
1795
			locale_str = conv_codeset_strdup
1796
				(body, CS_INTERNAL,
1797
				 conv_get_locale_charset_str());
1798
			fprintf(prfp, "%s: %s\n", hdr->name,
1799
				locale_str ? locale_str : body);
1800
			g_free(locale_str);
1801
		} else {
1802
			fprintf(prfp, "%s: (none)\n", hdr->name);
1803
		}
1804
	}
1805

  
1806
	procheader_header_array_destroy(headers);
1807

  
1808 1829
	fputc('\n', prfp);
1809 1830

  
1810 1831
	while (fgets(buf, sizeof(buf), tmpfp) != NULL)
......
1857 1878
	g_free(prtmp);
1858 1879
}
1859 1880

  
1881
gint procmsg_save_message_as_text(MsgInfo *msginfo, const gchar *dest,
1882
				  const gchar *encoding, gboolean all_headers)
1883
{
1884
	MimeInfo *mimeinfo, *partinfo;
1885
	FILE *fp;
1886
	FILE *tmpfp;
1887
	FILE *destfp;
1888
	gchar buf[BUFFSIZE];
1889

  
1890
	g_return_val_if_fail(msginfo != NULL, -1);
1891
	g_return_val_if_fail(dest != NULL, -1);
1892

  
1893
	mimeinfo = procmime_scan_message(msginfo);
1894
	if (!mimeinfo)
1895
		return -1;
1896
	if ((fp = procmsg_open_message(msginfo)) == NULL) {
1897
		procmime_mimeinfo_free_all(mimeinfo);
1898
		return -1;
1899
	}
1900
	if ((destfp = g_fopen(dest, "wb")) == NULL) {
1901
		fclose(fp);
1902
		procmime_mimeinfo_free_all(mimeinfo);
1903
		return -1;
1904
	}
1905
	procmsg_write_headers(msginfo, mimeinfo, fp, destfp, encoding, all_headers);
1906
	fputc('\n', destfp);
1907

  
1908
	partinfo = mimeinfo;
1909

  
1910
	while (partinfo != NULL) {
1911
		if (fseek(fp, partinfo->fpos, SEEK_SET) < 0)
1912
			break;
1913

  
1914
		if (partinfo->filename || partinfo->name)
1915
			g_snprintf(buf, sizeof(buf), "\n[%s  %s (%s)]\n",
1916
				   partinfo->filename ? partinfo->filename :
1917
				   partinfo->name,
1918
				   partinfo->content_type,
1919
				   to_human_readable(partinfo->content_size));
1920
		else
1921
			g_snprintf(buf, sizeof(buf), "\n[%s (%s)]\n",
1922
				   partinfo->content_type,
1923
				   to_human_readable(partinfo->content_size));
1924

  
1925
		if (partinfo->mime_type == MIME_TEXT ||
1926
		    partinfo->mime_type == MIME_TEXT_HTML) {
1927
			if (!partinfo->main &&
1928
			    partinfo->parent &&
1929
			    partinfo->parent->children != partinfo) {
1930
				fputs(buf, destfp);
1931
			}
1932

  
1933
			if ((tmpfp = procmime_get_text_content(partinfo, fp, encoding)) == NULL)
1934
				break;
1935
			if (copy_file_stream(tmpfp, destfp) < 0) {
1936
				fclose(tmpfp);
1937
				break;
1938
			}
1939

  
1940
			fclose(tmpfp);
1941
		} else if (partinfo->mime_type == MIME_MESSAGE_RFC822) {
1942
			fputs(buf, destfp);
1943
			while (fgets(buf, sizeof(buf), fp) != NULL)
1944
				if (buf[0] == '\r' || buf[0] == '\n') break;
1945
			procmsg_write_headers(msginfo, partinfo, fp, destfp, encoding, all_headers);
1946
			fputc('\n', destfp);
1947
		} else if (partinfo->mime_type != MIME_MULTIPART) {
1948
			fputs(buf, destfp);
1949
		}
1950

  
1951
		if (partinfo->parent && partinfo->parent->content_type &&
1952
		    !g_ascii_strcasecmp(partinfo->parent->content_type,
1953
					"multipart/alternative"))
1954
			partinfo = partinfo->parent->next;
1955
		else
1956
			partinfo = procmime_mimeinfo_next(partinfo);
1957
	}
1958

  
1959
	fclose(destfp);
1960
	fclose(fp);
1961
	procmime_mimeinfo_free_all(mimeinfo);
1962

  
1963
	return 0;
1964
}
1965

  
1860 1966
/**
1861 1967
 * procmsg_concat_partial_messages:
1862 1968
 * @mlist: list of MsgInfo* including message/partial messages.

Also available in: Unified diff