/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2007 Hiroyuki Yamamoto & The Claws Mail Team
+ * Copyright (C) 1999-2012 Hiroyuki Yamamoto & The Claws Mail Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
+#include "claws-features.h"
#endif
+#define _GNU_SOURCE
+#include <stdio.h>
+
#include "defs.h"
-#include <stdio.h>
#include <glib.h>
#include <glib/gi18n.h>
-#include <stdio.h>
#include <string.h>
#if HAVE_LOCALE_H
# include <locale.h>
#include "prefs_common.h"
#include "prefs_gtk.h"
#include "alertpanel.h"
+#include "timing.h"
+#include "privacy.h"
static GHashTable *procmime_get_mime_type_table (void);
+static MimeInfo *procmime_scan_file_short(const gchar *filename);
+static MimeInfo *procmime_scan_queue_file_short(const gchar *filename);
+static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan);
MimeInfo *procmime_mimeinfo_new(void)
{
switch (mimeinfo->content) {
case MIMECONTENT_FILE:
if (mimeinfo->tmp)
- g_unlink(mimeinfo->data.filename);
+ claws_unlink(mimeinfo->data.filename);
g_free(mimeinfo->data.filename);
break;
MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
{
- g_return_val_if_fail(mimeinfo != NULL, NULL);
- g_return_val_if_fail(mimeinfo->node != NULL, NULL);
+ cm_return_val_if_fail(mimeinfo != NULL, NULL);
+ cm_return_val_if_fail(mimeinfo->node != NULL, NULL);
if (mimeinfo->node->parent == NULL)
return NULL;
MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
{
- g_return_val_if_fail(mimeinfo != NULL, NULL);
- g_return_val_if_fail(mimeinfo->node != NULL, NULL);
+ cm_return_val_if_fail(mimeinfo != NULL, NULL);
+ cm_return_val_if_fail(mimeinfo->node != NULL, NULL);
if (mimeinfo->node->children)
return (MimeInfo *) mimeinfo->node->children->data;
gchar *filename;
MimeInfo *mimeinfo;
- filename = procmsg_get_message_file_path(msginfo);
+ filename = procmsg_get_message_file_path(msginfo);
if (!filename || !is_file_exist(filename)) {
g_free(filename);
- filename = procmsg_get_message_file(msginfo);
- }
- if (!filename || !is_file_exist(filename))
return NULL;
+ }
if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
!folder_has_parent_of_type(msginfo->folder, F_DRAFT))
return mimeinfo;
}
+MimeInfo *procmime_scan_message_short(MsgInfo *msginfo)
+{
+ gchar *filename;
+ MimeInfo *mimeinfo;
+
+ filename = procmsg_get_message_file_path(msginfo);
+ if (!filename || !is_file_exist(filename)) {
+ g_free(filename);
+ return NULL;
+ }
+
+ if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
+ !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
+ mimeinfo = procmime_scan_file_short(filename);
+ else
+ mimeinfo = procmime_scan_queue_file_short(filename);
+ g_free(filename);
+
+ return mimeinfo;
+}
+
enum
{
H_CONTENT_TRANSFER_ENCODING = 0,
{
const gchar *value;
- g_return_val_if_fail(mimeinfo != NULL, NULL);
- g_return_val_if_fail(name != NULL, NULL);
+ cm_return_val_if_fail(mimeinfo != NULL, NULL);
+ cm_return_val_if_fail(name != NULL, NULL);
value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
if (value == NULL)
return value;
}
+#ifdef HAVE_FGETS_UNLOCKED
+#define SC_FGETS fgets_unlocked
+#define SC_FPUTS fputs_unlocked
+#define SC_FPUTC fputc_unlocked
+#define SC_FREAD fread_unlocked
+#define SC_FWRITE fwrite_unlocked
+#define SC_FEOF feof_unlocked
+#define SC_FERROR ferror_unlocked
+
+static FILE *procmime_fopen(const gchar *file, const gchar *mode)
+{
+ FILE *fp = g_fopen(file, mode);
+ if (!fp)
+ return NULL;
+ flockfile(fp);
+ return fp;
+}
+static int procmime_fclose(FILE *fp)
+{
+ funlockfile(fp);
+ return fclose(fp);
+}
+#else
+#define SC_FGETS fgets
+#define SC_FPUTS fputs
+#define SC_FPUTC fputc
+#define SC_FREAD fread
+#define SC_FWRITE fwrite
+#define SC_FEOF feof
+#define SC_FERROR ferror
+#define procmime_fopen g_fopen
+#define procmime_fclose fclose
+#endif
+
#define FLUSH_LASTLINE() { \
if (*lastline != '\0') { \
gint llen = 0; \
/* this is flowed */ \
if (delsp) \
lastline[llen-1] = '\0'; \
- fputs(lastline, outfp); \
+ if (SC_FPUTS(lastline, outfp) == EOF) \
+ err = TRUE; \
} else { \
- fputs(lastline, outfp); \
- fputs("\n", outfp); \
+ if (SC_FPUTS(lastline, outfp) == EOF) \
+ err = TRUE; \
+ if (SC_FPUTS("\n", outfp) == EOF) \
+ err = TRUE; \
} \
} \
strcpy(lastline, buf); \
gboolean tmp_file = FALSE;
gboolean flowed = FALSE;
gboolean delsp = FALSE;
+ gboolean err = FALSE;
+
EncodingType encoding = forced_encoding
? forced_encoding
: mimeinfo->encoding_type;
gchar lastline[BUFFSIZE];
memset(lastline, 0, BUFFSIZE);
-
- g_return_val_if_fail(mimeinfo != NULL, FALSE);
+
+ cm_return_val_if_fail(mimeinfo != NULL, FALSE);
if (prefs_common.respect_flowed_format &&
mimeinfo->type == MIMETYPE_TEXT &&
))
return TRUE;
- infp = g_fopen(mimeinfo->data.filename, "rb");
+ if (mimeinfo->type == MIMETYPE_MULTIPART || mimeinfo->type == MIMETYPE_MESSAGE)
+ return TRUE;
+
+ infp = procmime_fopen(mimeinfo->data.filename, "rb");
if (!infp) {
perror("fopen");
return FALSE;
outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
if (!outfp) {
perror("tmpfile");
- fclose(infp);
+ procmime_fclose(infp);
return FALSE;
}
tmp_file = TRUE;
readend = mimeinfo->offset + mimeinfo->length;
if (encoding == ENC_QUOTED_PRINTABLE) {
- while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
+ while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
gint len;
len = qp_decode_line(buf);
buf[len]='\0';
if (!flowed) {
- fwrite(buf, 1, len, outfp);
+ if (SC_FWRITE(buf, 1, len, outfp) < len)
+ err = TRUE;
} else {
FLUSH_LASTLINE();
}
FLUSH_LASTLINE();
} else if (encoding == ENC_BASE64) {
gchar outbuf[BUFFSIZE];
- gint len;
+ gint len, inlen, inread;
Base64Decoder *decoder;
gboolean got_error = FALSE;
gboolean uncanonicalize = FALSE;
FILE *tmpfp = outfp;
+ gboolean null_bytes = FALSE;
+ gboolean starting = TRUE;
if (mimeinfo->type == MIMETYPE_TEXT ||
mimeinfo->type == MIMETYPE_MESSAGE) {
tmpfp = my_tmpfile();
if (!tmpfp) {
perror("tmpfile");
- if (tmp_file) fclose(outfp);
- fclose(infp);
+ if (tmp_file)
+ procmime_fclose(outfp);
+ procmime_fclose(infp);
return FALSE;
}
+#ifdef HAVE_FGETS_UNLOCKED
+ flockfile(tmpfp);
+#endif
}
decoder = base64_decoder_new();
- while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
- len = base64_decoder_decode(decoder, buf, outbuf);
- if (len < 0 && !got_error) {
+ while ((inlen = MIN(readend - ftell(infp), sizeof(buf))) > 0 && !err) {
+ inread = SC_FREAD(buf, 1, inlen, infp);
+ len = base64_decoder_decode(decoder, buf, outbuf, inread);
+ if (uncanonicalize == TRUE && strlen(outbuf) < len && starting) {
+ uncanonicalize = FALSE;
+ null_bytes = TRUE;
+ }
+ starting = FALSE;
+ if (((inread != inlen) || len < 0) && !got_error) {
g_warning("Bad BASE64 content.\n");
- fwrite(_("[Error decoding BASE64]\n"),
+ if (SC_FWRITE(_("[Error decoding BASE64]\n"),
sizeof(gchar),
strlen(_("[Error decoding BASE64]\n")),
- tmpfp);
+ tmpfp) < strlen(_("[Error decoding BASE64]\n")))
+ g_warning("error decoding BASE64");
got_error = TRUE;
continue;
} else if (len >= 0) {
/* print out the error message only once
* per block */
- fwrite(outbuf, sizeof(gchar), len, tmpfp);
+ if (null_bytes) {
+ /* we won't uncanonicalize, output to outfp directly */
+ if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
+ err = TRUE;
+ } else {
+ if (SC_FWRITE(outbuf, sizeof(gchar), len, tmpfp) < len)
+ err = TRUE;
+ }
got_error = FALSE;
}
}
if (uncanonicalize) {
rewind(tmpfp);
- while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
+ while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
strcrchomp(buf);
- fputs(buf, outfp);
+ if (SC_FPUTS(buf, outfp) == EOF)
+ err = TRUE;
}
- fclose(tmpfp);
+ procmime_fclose(tmpfp);
}
} else if (encoding == ENC_X_UUENCODE) {
gchar outbuf[BUFFSIZE];
gint len;
gboolean flag = FALSE;
- while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
+ while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
if (!flag && strncmp(buf,"begin ", 6)) continue;
if (flag) {
g_warning("Bad UUENCODE content(%d)\n", len);
break;
}
- fwrite(outbuf, sizeof(gchar), len, outfp);
+ if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
+ err = TRUE;
} else
flag = TRUE;
}
} else {
- while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
+ while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
if (!flowed) {
- fputs(buf, outfp);
+ if (SC_FPUTS(buf, outfp) == EOF)
+ err = TRUE;
} else {
FLUSH_LASTLINE();
}
}
if (flowed)
FLUSH_LASTLINE();
+ if (err == TRUE)
+ g_warning("write error");
}
- fclose(outfp);
- fclose(infp);
+ procmime_fclose(outfp);
+ procmime_fclose(infp);
- stat(tmpfilename, &statbuf);
+ if (err == TRUE) {
+ return FALSE;
+ }
+
+ g_stat(tmpfilename, &statbuf);
if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
- g_unlink(mimeinfo->data.filename);
+ claws_unlink(mimeinfo->data.filename);
g_free(mimeinfo->data.filename);
mimeinfo->data.filename = tmpfilename;
mimeinfo->tmp = TRUE;
gint len;
gchar *tmpfilename;
struct stat statbuf;
+ gboolean err = FALSE;
if (mimeinfo->content == MIMECONTENT_EMPTY)
return TRUE;
perror("tmpfile");
return FALSE;
}
+#ifdef HAVE_FGETS_UNLOCKED
+ flockfile(outfp);
+#endif
- if (mimeinfo->content == MIMECONTENT_FILE) {
- if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+ if (mimeinfo->content == MIMECONTENT_FILE && mimeinfo->data.filename) {
+ if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
g_warning("Can't open file %s\n", mimeinfo->data.filename);
+ procmime_fclose(outfp);
return FALSE;
}
} else if (mimeinfo->content == MIMECONTENT_MEM) {
infp = str_open_as_stream(mimeinfo->data.mem);
- if (infp == NULL)
+ if (infp == NULL) {
+ procmime_fclose(outfp);
return FALSE;
+ }
+#ifdef HAVE_FGETS_UNLOCKED
+ flockfile(infp);
+#endif
+
}
if (encoding == ENC_BASE64) {
tmp_file = get_tmp_file();
if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
g_free(tmp_file);
- fclose(infp);
+ procmime_fclose(infp);
+ procmime_fclose(outfp);
return FALSE;
}
- if ((tmp_fp = g_fopen(tmp_file, "rb")) == NULL) {
+ if ((tmp_fp = procmime_fopen(tmp_file, "rb")) == NULL) {
FILE_OP_ERROR(tmp_file, "fopen");
- g_unlink(tmp_file);
+ claws_unlink(tmp_file);
g_free(tmp_file);
- fclose(infp);
+ procmime_fclose(infp);
+ procmime_fclose(outfp);
return FALSE;
}
} else {
gchar *out = canonicalize_str(mimeinfo->data.mem);
- fclose(infp);
+ procmime_fclose(infp);
infp = str_open_as_stream(out);
tmp_fp = infp;
g_free(out);
- if (infp == NULL)
+ if (infp == NULL) {
+ procmime_fclose(outfp);
return FALSE;
+ }
+#ifdef HAVE_FGETS_UNLOCKED
+ flockfile(infp);
+#endif
}
}
- while ((len = fread(inbuf, sizeof(gchar),
+ while ((len = SC_FREAD(inbuf, sizeof(gchar),
B64_LINE_SIZE, tmp_fp))
== B64_LINE_SIZE) {
base64_encode(outbuf, inbuf, B64_LINE_SIZE);
- fputs(outbuf, outfp);
- fputc('\n', outfp);
+ if (SC_FPUTS(outbuf, outfp) == EOF)
+ err = TRUE;
+ if (SC_FPUTC('\n', outfp) == EOF)
+ err = TRUE;
}
- if (len > 0 && feof(tmp_fp)) {
+ if (len > 0 && SC_FEOF(tmp_fp)) {
base64_encode(outbuf, inbuf, len);
- fputs(outbuf, outfp);
- fputc('\n', outfp);
+ if (SC_FPUTS(outbuf, outfp) == EOF)
+ err = TRUE;
+ if (SC_FPUTC('\n', outfp) == EOF)
+ err = TRUE;
}
if (tmp_file) {
- fclose(tmp_fp);
- g_unlink(tmp_file);
+ procmime_fclose(tmp_fp);
+ claws_unlink(tmp_file);
g_free(tmp_file);
}
} else if (encoding == ENC_QUOTED_PRINTABLE) {
gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
- while (fgets(inbuf, sizeof(inbuf), infp) != NULL) {
+ while (SC_FGETS(inbuf, sizeof(inbuf), infp) != NULL) {
qp_encode_line(outbuf, inbuf);
if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
tmpbuf += sizeof("From ")-1;
- fputs("=46rom ", outfp);
- fputs(tmpbuf, outfp);
- } else
- fputs(outbuf, outfp);
+ if (SC_FPUTS("=46rom ", outfp) == EOF)
+ err = TRUE;
+ if (SC_FPUTS(tmpbuf, outfp) == EOF)
+ err = TRUE;
+ } else {
+ if (SC_FPUTS(outbuf, outfp) == EOF)
+ err = TRUE;
+ }
}
} else {
gchar buf[BUFFSIZE];
- while (fgets(buf, sizeof(buf), infp) != NULL) {
+ while (SC_FGETS(buf, sizeof(buf), infp) != NULL) {
strcrchomp(buf);
- fputs(buf, outfp);
+ if (SC_FPUTS(buf, outfp) == EOF)
+ err = TRUE;
}
}
- fclose(outfp);
- fclose(infp);
+ procmime_fclose(outfp);
+ procmime_fclose(infp);
+
+ if (err == TRUE)
+ return FALSE;
if (mimeinfo->content == MIMECONTENT_FILE) {
if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
- g_unlink(mimeinfo->data.filename);
+ claws_unlink(mimeinfo->data.filename);
g_free(mimeinfo->data.filename);
} else if (mimeinfo->content == MIMECONTENT_MEM) {
if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
g_free(mimeinfo->data.mem);
}
- stat(tmpfilename, &statbuf);
+ g_stat(tmpfilename, &statbuf);
mimeinfo->content = MIMECONTENT_FILE;
mimeinfo->data.filename = tmpfilename;
mimeinfo->tmp = TRUE;
gint restlength, readlength;
gint saved_errno = 0;
- g_return_val_if_fail(outfile != NULL, -1);
- g_return_val_if_fail(mimeinfo != NULL, -1);
+ cm_return_val_if_fail(outfile != NULL, -1);
+ cm_return_val_if_fail(mimeinfo != NULL, -1);
if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
return -EINVAL;
- if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+ if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
saved_errno = errno;
FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
return -(saved_errno);
if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
saved_errno = errno;
FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
- fclose(infp);
+ procmime_fclose(infp);
return -(saved_errno);
}
- if ((outfp = g_fopen(outfile, "wb")) == NULL) {
+ if ((outfp = procmime_fopen(outfile, "wb")) == NULL) {
saved_errno = errno;
FILE_OP_ERROR(outfile, "fopen");
- fclose(infp);
+ procmime_fclose(infp);
return -(saved_errno);
}
restlength = mimeinfo->length;
- while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
- if (fwrite(buf, 1, readlength, outfp) != readlength) {
+ while ((restlength > 0) && ((readlength = SC_FREAD(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
+ if (SC_FWRITE(buf, 1, readlength, outfp) != readlength) {
saved_errno = errno;
- fclose(outfp);
+ procmime_fclose(infp);
+ procmime_fclose(outfp);
return -(saved_errno);
}
restlength -= readlength;
}
- fclose(infp);
- if (fclose(outfp) == EOF) {
+ procmime_fclose(infp);
+ if (procmime_fclose(outfp) == EOF) {
saved_errno = errno;
FILE_OP_ERROR(outfile, "fclose");
- g_unlink(outfile);
+ claws_unlink(outfile);
return -(saved_errno);
}
return 0;
}
-struct ContentRenderer {
- char * content_type;
- char * renderer;
-};
-
-static GList * renderer_list = NULL;
-
-static struct ContentRenderer *
-content_renderer_new(char * content_type, char * renderer)
-{
- struct ContentRenderer * cr;
-
- cr = g_new(struct ContentRenderer, 1);
- if (cr == NULL)
- return NULL;
-
- cr->content_type = g_strdup(content_type);
- cr->renderer = g_strdup(renderer);
-
- return cr;
-}
-
-static void content_renderer_free(struct ContentRenderer * cr)
-{
- g_free(cr->content_type);
- g_free(cr->renderer);
- g_free(cr);
-}
-
-void renderer_read_config(void)
-{
- gchar buf[BUFFSIZE];
- FILE * f;
- gchar * rcpath;
-
- g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
- renderer_list = NULL;
-
- rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
- f = g_fopen(rcpath, "rb");
- g_free(rcpath);
-
- if (f == NULL)
- return;
-
- while (fgets(buf, BUFFSIZE, f)) {
- char * p;
- struct ContentRenderer * cr;
-
- strretchomp(buf);
- p = strchr(buf, ' ');
- if (p == NULL)
- continue;
- * p = 0;
-
- cr = content_renderer_new(buf, p + 1);
- if (cr == NULL)
- continue;
-
- renderer_list = g_list_append(renderer_list, cr);
- }
-
- fclose(f);
-}
-
-void renderer_write_config(void)
-{
- gchar * rcpath;
- PrefFile *pfile;
- GList * cur;
-
- rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
-
- if ((pfile = prefs_write_open(rcpath)) == NULL) {
- g_warning("failed to write configuration to file\n");
- g_free(rcpath);
- return;
- }
-
- g_free(rcpath);
-
- for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
- struct ContentRenderer * renderer;
- renderer = cur->data;
- fprintf(pfile->fp, "%s %s\n", renderer->content_type,
- renderer->renderer);
- }
-
- if (prefs_file_close(pfile) < 0) {
- g_warning("failed to write configuration to file\n");
- return;
- }
-}
-
-FILE *procmime_get_text_content(MimeInfo *mimeinfo)
+gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
+ gboolean (*scan_callback)(const gchar *str, gpointer cb_data),
+ gpointer cb_data)
{
- FILE *tmpfp, *outfp;
+ FILE *tmpfp;
const gchar *src_codeset;
gboolean conv_fail = FALSE;
gchar buf[BUFFSIZE];
gchar *str;
- struct ContentRenderer * renderer;
- GList * cur;
- gchar *tmpfile, *content_type;
-
- g_return_val_if_fail(mimeinfo != NULL, NULL);
+ gchar *tmpfile;
+ gboolean scan_ret = FALSE;
+
+ cm_return_val_if_fail(mimeinfo != NULL, TRUE);
+ cm_return_val_if_fail(scan_callback != NULL, TRUE);
if (!procmime_decode_content(mimeinfo))
- return NULL;
+ return TRUE;
tmpfile = procmime_get_tmp_file_name(mimeinfo);
if (tmpfile == NULL)
- return NULL;
+ return TRUE;
if (procmime_get_part(tmpfile, mimeinfo) < 0) {
g_free(tmpfile);
- return NULL;
+ return TRUE;
}
- tmpfp = g_fopen(tmpfile, "rb");
+ tmpfp = procmime_fopen(tmpfile, "rb");
if (tmpfp == NULL) {
g_free(tmpfile);
- return NULL;
- }
-
- if ((outfp = my_tmpfile()) == NULL) {
- perror("tmpfile");
- fclose(tmpfp);
- g_free(tmpfile);
- return NULL;
+ return TRUE;
}
src_codeset = forced_charset
? forced_charset :
procmime_mimeinfo_get_parameter(mimeinfo, "charset");
- renderer = NULL;
-
- content_type = procmime_get_content_type_str(mimeinfo->type,
- mimeinfo->subtype);
- for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
- struct ContentRenderer * cr;
-
- cr = cur->data;
- if (g_ascii_strcasecmp(cr->content_type, content_type) == 0) {
- renderer = cr;
- break;
- }
- }
- g_free(content_type);
-
- if (renderer != NULL) {
- FILE * p;
- int oldout;
-
- oldout = dup(1);
-
- dup2(fileno(outfp), 1);
-
- p = popen(renderer->renderer, "w");
- if (p != NULL) {
- size_t count;
-
- while ((count =
- fread(buf, sizeof(char), sizeof(buf),
- tmpfp)) > 0)
- fwrite(buf, sizeof(char), count, p);
- pclose(p);
- }
-
- dup2(oldout, 1);
-/* CodeConverter seems to have no effect here */
- } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
+ /* use supersets transparently when possible */
+ if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_ISO_8859_1))
+ src_codeset = CS_WINDOWS_1252;
+ else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_X_GBK))
+ src_codeset = CS_GB18030;
+ else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GBK))
+ src_codeset = CS_GB18030;
+ else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GB2312))
+ src_codeset = CS_GB18030;
+ else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_X_VIET_VPS))
+ src_codeset = CS_WINDOWS_874;
+
+ if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
SC_HTMLParser *parser;
CodeConverter *conv;
conv = conv_code_converter_new(src_codeset);
parser = sc_html_parser_new(tmpfp, conv);
while ((str = sc_html_parse(parser)) != NULL) {
- fputs(str, outfp);
+ if ((scan_ret = scan_callback(str, cb_data)) == TRUE)
+ break;
}
sc_html_parser_destroy(parser);
conv_code_converter_destroy(conv);
conv = conv_code_converter_new(src_codeset);
parser = ertf_parser_new(tmpfp, conv);
while ((str = ertf_parse(parser)) != NULL) {
- fputs(str, outfp);
+ if ((scan_ret = scan_callback(str, cb_data)) == TRUE)
+ break;
}
ertf_parser_destroy(parser);
conv_code_converter_destroy(conv);
} else if (mimeinfo->type == MIMETYPE_TEXT) {
- while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
+ while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
if (str) {
- fputs(str, outfp);
+ if ((scan_ret = scan_callback(str, cb_data)) == TRUE) {
+ g_free(str);
+ break;
+ }
g_free(str);
} else {
conv_fail = TRUE;
- fputs(buf, outfp);
+ if ((scan_ret = scan_callback(buf, cb_data)) == TRUE)
+ break;
}
}
}
if (conv_fail)
g_warning("procmime_get_text_content(): Code conversion failed.\n");
- fclose(tmpfp);
+ procmime_fclose(tmpfp);
+ claws_unlink(tmpfile);
+ g_free(tmpfile);
+
+ return scan_ret;
+}
+
+static gboolean scan_fputs_cb(const gchar *str, gpointer fp)
+{
+ if (SC_FPUTS(str, (FILE *)fp) == EOF)
+ return TRUE;
+
+ return FALSE;
+}
+
+FILE *procmime_get_text_content(MimeInfo *mimeinfo)
+{
+ FILE *outfp;
+ gboolean err;
+
+ if ((outfp = my_tmpfile()) == NULL) {
+ perror("tmpfile");
+ return NULL;
+ }
+#ifdef HAVE_FGETS_UNLOCKED
+ flockfile(outfp);
+#endif
+
+ err = procmime_scan_text_content(mimeinfo, scan_fputs_cb, outfp);
+
rewind(outfp);
+ if (err == TRUE) {
+ procmime_fclose(outfp);
+ return NULL;
+ }
+#ifdef HAVE_FGETS_UNLOCKED
+ funlockfile(outfp);
+#endif
+ return outfp;
+
+}
+
+FILE *procmime_get_binary_content(MimeInfo *mimeinfo)
+{
+ FILE *outfp;
+ gchar *tmpfile;
+
+ cm_return_val_if_fail(mimeinfo != NULL, NULL);
+
+ if (!procmime_decode_content(mimeinfo))
+ return NULL;
+
+ tmpfile = procmime_get_tmp_file_name(mimeinfo);
+ if (tmpfile == NULL)
+ return NULL;
+
+ if (procmime_get_part(tmpfile, mimeinfo) < 0) {
+ g_free(tmpfile);
+ return NULL;
+ }
+
+ outfp = procmime_fopen(tmpfile, "rb");
+ if (outfp == NULL) {
+ g_unlink(tmpfile);
+ g_free(tmpfile);
+ return NULL;
+ }
+
g_unlink(tmpfile);
g_free(tmpfile);
+#ifdef HAVE_FGETS_UNLOCKED
+ funlockfile(outfp);
+#endif
return outfp;
}
{
FILE *outfp = NULL;
MimeInfo *mimeinfo, *partinfo;
+ gboolean empty_ok = FALSE, short_scan = TRUE;
- g_return_val_if_fail(msginfo != NULL, NULL);
+ cm_return_val_if_fail(msginfo != NULL, NULL);
- mimeinfo = procmime_scan_message(msginfo);
+ /* first we try to short-scan (for speed), refusing empty parts */
+scan_again:
+ if (short_scan)
+ mimeinfo = procmime_scan_message_short(msginfo);
+ else
+ mimeinfo = procmime_scan_message(msginfo);
if (!mimeinfo) return NULL;
partinfo = mimeinfo;
- while (partinfo && partinfo->type != MIMETYPE_TEXT) {
+ while (partinfo && (partinfo->type != MIMETYPE_TEXT ||
+ (partinfo->length == 0 && !empty_ok))) {
partinfo = procmime_mimeinfo_next(partinfo);
}
if (partinfo)
outfp = procmime_get_text_content(partinfo);
-
+ else if (!empty_ok && short_scan) {
+ /* if short scan didn't find a non-empty part, rescan
+ * fully for non-empty parts
+ */
+ short_scan = FALSE;
+ procmime_mimeinfo_free_all(mimeinfo);
+ goto scan_again;
+ } else if (!empty_ok && !short_scan) {
+ /* if full scan didn't find a non-empty part, rescan
+ * accepting empty parts
+ */
+ empty_ok = TRUE;
+ procmime_mimeinfo_free_all(mimeinfo);
+ goto scan_again;
+ }
procmime_mimeinfo_free_all(mimeinfo);
+ /* outfp already unlocked at this time */
return outfp;
}
FILE *outfp = NULL;
MimeInfo *mimeinfo, *partinfo, *encinfo;
- g_return_val_if_fail(msginfo != NULL, NULL);
+ cm_return_val_if_fail(msginfo != NULL, NULL);
mimeinfo = procmime_scan_message(msginfo);
if (!mimeinfo) {
partinfo = mimeinfo;
while (partinfo && partinfo->type != MIMETYPE_TEXT) {
partinfo = procmime_mimeinfo_next(partinfo);
+ if (privacy_mimeinfo_is_signed(partinfo))
+ procmsg_msginfo_set_flags(msginfo, 0, MSG_SIGNED);
}
if (partinfo)
procmime_mimeinfo_free_all(mimeinfo);
+ /* outfp already unlocked at this time */
return outfp;
}
MimeInfo *mimeinfo, *partinfo;
gboolean result = FALSE;
- g_return_val_if_fail(msginfo != NULL, FALSE);
+ cm_return_val_if_fail(msginfo != NULL, FALSE);
mimeinfo = procmime_scan_message(msginfo);
if (!mimeinfo) {
return result;
}
-gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
- const gchar *str, StrFindFunc find_func)
-{
- FILE *outfp;
- gchar buf[BUFFSIZE];
-
- g_return_val_if_fail(mimeinfo != NULL, FALSE);
- g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
- g_return_val_if_fail(str != NULL, FALSE);
- g_return_val_if_fail(find_func != NULL, FALSE);
-
- outfp = procmime_get_text_content(mimeinfo);
-
- if (!outfp)
- return FALSE;
-
- while (fgets(buf, sizeof(buf), outfp) != NULL) {
- strretchomp(buf);
- if (find_func(buf, str)) {
- fclose(outfp);
- return TRUE;
- }
- }
-
- fclose(outfp);
-
- return FALSE;
-}
-
-gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
- StrFindFunc find_func)
-{
- MimeInfo *mimeinfo;
- MimeInfo *partinfo;
- gchar *filename;
- gboolean found = FALSE;
-
- g_return_val_if_fail(msginfo != NULL, FALSE);
- g_return_val_if_fail(str != NULL, FALSE);
- g_return_val_if_fail(find_func != NULL, FALSE);
-
- filename = procmsg_get_message_file(msginfo);
- if (!filename) return FALSE;
- mimeinfo = procmime_scan_message(msginfo);
-
- for (partinfo = mimeinfo; partinfo != NULL;
- partinfo = procmime_mimeinfo_next(partinfo)) {
- if (partinfo->type == MIMETYPE_TEXT) {
- if (procmime_find_string_part
- (partinfo, filename, str, find_func) == TRUE) {
- found = TRUE;
- break;
- }
- }
- }
-
- procmime_mimeinfo_free_all(mimeinfo);
- g_free(filename);
-
- return found;
-}
-
gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
{
static guint32 id = 0;
gchar *filename;
gchar f_prefix[10];
- g_return_val_if_fail(mimeinfo != NULL, NULL);
+ cm_return_val_if_fail(mimeinfo != NULL, NULL);
g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
gchar *procmime_get_mime_type(const gchar *filename)
{
- static GHashTable *mime_type_table = NULL;
- MimeType *mime_type;
const gchar *p;
gchar *ext = NULL;
gchar *base;
+#ifndef G_OS_WIN32
+ static GHashTable *mime_type_table = NULL;
+ MimeType *mime_type;
if (!mime_type_table) {
mime_type_table = procmime_get_mime_type_table();
if (!mime_type_table) return NULL;
}
+#endif
+
+ if (filename == NULL)
+ return NULL;
base = g_path_get_basename(filename);
if ((p = strrchr(base, '.')) != NULL)
- Xstrdup_a(ext, p + 1, p = NULL );
+ ext = g_utf8_strdown(p + 1, -1);
g_free(base);
- if (!p) return NULL;
- g_strdown(ext);
+#ifndef G_OS_WIN32
mime_type = g_hash_table_lookup(mime_type_table, ext);
+
if (mime_type) {
gchar *str;
-
str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
NULL);
+ debug_print("got type %s for %s\n", str, ext);
+ g_free(ext);
return str;
- }
-
+ }
return NULL;
+#else
+ gchar *str = get_content_type_from_registry_with_ext(ext);
+
+ g_free(ext);
+ return str;
+#endif
}
static guint procmime_str_hash(gconstpointer gptr)
exts = g_strsplit(mime_type->extension, " ", 16);
for (i = 0; exts[i] != NULL; i++) {
- /* make the key case insensitive */
- g_strdown(exts[i]);
- /* use previously dup'd key on overwriting */
- if (g_hash_table_lookup(table, exts[i]))
- key = exts[i];
- else
+ /* Don't overwrite previously inserted extension */
+ if (!g_hash_table_lookup(table, exts[i])) {
key = g_strdup(exts[i]);
- g_hash_table_insert(table, key, mime_type);
+ g_hash_table_insert(table, key, mime_type);
+ }
}
g_strfreev(exts);
}
if (mime_type_list)
return mime_type_list;
-#if defined(__NetBSD__)
- if ((fp = g_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL)
+#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
+ if ((fp = procmime_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL)
#else
- if ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL)
+ if ((fp = procmime_fopen("/usr/share/mime/globs", "rb")) == NULL)
#endif
{
fp_is_glob_file = FALSE;
- if ((fp = g_fopen("/etc/mime.types", "rb")) == NULL) {
- if ((fp = g_fopen(SYSCONFDIR "/mime.types", "rb"))
+ if ((fp = procmime_fopen("/etc/mime.types", "rb")) == NULL) {
+ if ((fp = procmime_fopen(SYSCONFDIR "/mime.types", "rb"))
== NULL) {
FILE_OP_ERROR(SYSCONFDIR "/mime.types",
"fopen");
}
}
- while (fgets(buf, sizeof(buf), fp) != NULL) {
+ while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
p = strchr(buf, '#');
if (p) *p = '\0';
g_strstrip(buf);
}
if (*p)
- mime_type->extension = g_strdup(p);
+ mime_type->extension = g_utf8_strdown(p, -1);
else
mime_type->extension = NULL;
list = g_list_append(list, mime_type);
}
- fclose(fp);
+ procmime_fclose(fp);
if (!list)
g_warning("Can't read mime.types\n");
gfloat octet_percentage;
gboolean force_b64 = FALSE;
- if ((fp = g_fopen(file, "rb")) == NULL) {
+ if ((fp = procmime_fopen(file, "rb")) == NULL) {
FILE_OP_ERROR(file, "fopen");
return ENC_UNKNOWN;
}
- while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
+ while ((len = SC_FREAD(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
guchar *p;
gint i;
total_len += len;
}
- fclose(fp);
+ procmime_fclose(fp);
if (total_len > 0)
octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
octet_percentage = 0.0;
debug_print("procmime_get_encoding_for_text_file(): "
- "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
+ "8bit chars: %zd / %zd (%f%%)\n", octet_chars, total_len,
100.0 * octet_percentage);
if (octet_percentage > 0.20 || force_b64) {
gchar *content_id,
gchar *content_disposition,
gchar *content_location,
+ const gchar *original_msgid,
+ const gchar *disposition_notification_hdr,
const gchar *filename,
guint offset,
- guint length);
+ guint length,
+ gboolean short_scan);
-static void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
+static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_scan)
{
HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
{"Content-Transfer-Encoding:",
NULL, TRUE},
{"MIME-Version:",
NULL, TRUE},
+ {"Original-Message-ID:",
+ NULL, TRUE},
+ {"Disposition:",
+ NULL, TRUE},
{NULL, NULL, FALSE}};
guint content_start, i;
FILE *fp;
procmime_decode_content(mimeinfo);
- fp = g_fopen(mimeinfo->data.filename, "rb");
+ fp = procmime_fopen(mimeinfo->data.filename, "rb");
if (fp == NULL) {
FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
return;
fseek(fp, mimeinfo->offset, SEEK_SET);
procheader_get_header_fields(fp, hentry);
if (hentry[0].body != NULL) {
- tmp = conv_unmime_header(hentry[0].body, NULL);
+ tmp = conv_unmime_header(hentry[0].body, NULL, FALSE);
g_free(hentry[0].body);
hentry[0].body = tmp;
}
if (hentry[2].body != NULL) {
- tmp = conv_unmime_header(hentry[2].body, NULL);
+ tmp = conv_unmime_header(hentry[2].body, NULL, FALSE);
g_free(hentry[2].body);
hentry[2].body = tmp;
}
if (hentry[4].body != NULL) {
- tmp = conv_unmime_header(hentry[4].body, NULL);
+ tmp = conv_unmime_header(hentry[4].body, NULL, FALSE);
g_free(hentry[4].body);
hentry[4].body = tmp;
}
if (hentry[5].body != NULL) {
- tmp = conv_unmime_header(hentry[5].body, NULL);
+ tmp = conv_unmime_header(hentry[5].body, NULL, FALSE);
g_free(hentry[5].body);
hentry[5].body = tmp;
}
+ if (hentry[7].body != NULL) {
+ tmp = conv_unmime_header(hentry[7].body, NULL, FALSE);
+ g_free(hentry[7].body);
+ hentry[7].body = tmp;
+ }
+ if (hentry[8].body != NULL) {
+ tmp = conv_unmime_header(hentry[8].body, NULL, FALSE);
+ g_free(hentry[8].body);
+ hentry[8].body = tmp;
+ }
+
content_start = ftell(fp);
- fclose(fp);
+ procmime_fclose(fp);
len = mimeinfo->length - (content_start - mimeinfo->offset);
if (len < 0)
hentry[0].body, hentry[1].body,
hentry[2].body, hentry[3].body,
hentry[4].body, hentry[5].body,
+ hentry[7].body, hentry[8].body,
mimeinfo->data.filename, content_start,
- len);
+ len, short_scan);
+
+ for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
+ g_free(hentry[i].body);
+ hentry[i].body = NULL;
+ }
+}
+
+static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
+ const gchar *original_msgid, const gchar *disposition_notification_hdr,
+ gboolean short_scan)
+{
+ HeaderEntry hentry[] = {{"Original-Message-ID:", NULL, TRUE},
+ {"Disposition:", NULL, TRUE},
+ {NULL, NULL, FALSE}};
+ guint i;
+ FILE *fp;
+ gchar *orig_msg_id = NULL;
+ gchar *disp = NULL;
+
+ procmime_decode_content(mimeinfo);
+
+ debug_print("parse disposition notification\n");
+ fp = procmime_fopen(mimeinfo->data.filename, "rb");
+ if (fp == NULL) {
+ FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+ return;
+ }
+ fseek(fp, mimeinfo->offset, SEEK_SET);
+
+ if (original_msgid && disposition_notification_hdr) {
+ hentry[0].body = g_strdup(original_msgid);
+ hentry[1].body = g_strdup(disposition_notification_hdr);
+ } else {
+ procheader_get_header_fields(fp, hentry);
+ }
+
+ procmime_fclose(fp);
+
+ if (!hentry[0].body || !hentry[1].body) {
+ debug_print("MsgId %s, Disp %s\n",
+ hentry[0].body ? hentry[0].body:"(nil)",
+ hentry[1].body ? hentry[1].body:"(nil)");
+ goto bail;
+ }
+
+ orig_msg_id = g_strdup(hentry[0].body);
+ disp = g_strdup(hentry[1].body);
+
+ extract_parenthesis(orig_msg_id, '<', '>');
+ remove_space(orig_msg_id);
+ if (strstr(disp, "displayed")) {
+ /* find sent message, if possible */
+ MsgInfo *info = NULL;
+ GList *flist;
+ debug_print("%s has been displayed.\n", orig_msg_id);
+ for (flist = folder_get_list(); flist != NULL; flist = g_list_next(flist)) {
+ FolderItem *outbox = ((Folder *)(flist->data))->outbox;
+ if (!outbox) {
+ debug_print("skipping folder with no outbox...\n");
+ continue;
+ }
+ info = folder_item_get_msginfo_by_msgid(outbox, orig_msg_id);
+ debug_print("%s %s in %s\n", info?"found":"didn't find", orig_msg_id, outbox->path);
+ if (info) {
+ procmsg_msginfo_set_flags(info, MSG_RETRCPT_GOT, 0);
+ procmsg_msginfo_free(info);
+ }
+ }
+ }
+ g_free(orig_msg_id);
+ g_free(disp);
+bail:
for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
g_free(hentry[i].body);
hentry[i].body = NULL;
}
}
-static void procmime_parse_multipart(MimeInfo *mimeinfo)
+#define GET_HEADERS() { \
+ procheader_get_header_fields(fp, hentry); \
+ if (hentry[0].body != NULL) { \
+ tmp = conv_unmime_header(hentry[0].body, NULL, FALSE); \
+ g_free(hentry[0].body); \
+ hentry[0].body = tmp; \
+ } \
+ if (hentry[2].body != NULL) { \
+ tmp = conv_unmime_header(hentry[2].body, NULL, FALSE); \
+ g_free(hentry[2].body); \
+ hentry[2].body = tmp; \
+ } \
+ if (hentry[4].body != NULL) { \
+ tmp = conv_unmime_header(hentry[4].body, NULL, FALSE); \
+ g_free(hentry[4].body); \
+ hentry[4].body = tmp; \
+ } \
+ if (hentry[5].body != NULL) { \
+ tmp = conv_unmime_header(hentry[5].body, NULL, FALSE); \
+ g_free(hentry[5].body); \
+ hentry[5].body = tmp; \
+ } \
+ if (hentry[6].body != NULL) { \
+ tmp = conv_unmime_header(hentry[6].body, NULL, FALSE); \
+ g_free(hentry[6].body); \
+ hentry[6].body = tmp; \
+ } \
+ if (hentry[7].body != NULL) { \
+ tmp = conv_unmime_header(hentry[7].body, NULL, FALSE); \
+ g_free(hentry[7].body); \
+ hentry[7].body = tmp; \
+ } \
+}
+
+static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
{
HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
{"Content-Transfer-Encoding:",
NULL, TRUE},
{"Content-Location:",
NULL, TRUE},
+ {"Original-Message-ID:",
+ NULL, TRUE},
+ {"Disposition:",
+ NULL, TRUE},
{NULL, NULL, FALSE}};
gchar *p, *tmp;
gchar *boundary;
gchar buf[BUFFSIZE];
FILE *fp;
int result = 0;
+ gboolean start_found = FALSE;
+ gboolean end_found = FALSE;
boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
if (!boundary)
procmime_decode_content(mimeinfo);
- fp = g_fopen(mimeinfo->data.filename, "rb");
+ fp = procmime_fopen(mimeinfo->data.filename, "rb");
if (fp == NULL) {
FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
return;
}
+
fseek(fp, mimeinfo->offset, SEEK_SET);
- while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
+ while ((p = SC_FGETS(buf, sizeof(buf), fp)) != NULL && result == 0) {
if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
break;
if (IS_BOUNDARY(buf, boundary, boundary_len)) {
+ start_found = TRUE;
+
if (lastoffset != -1) {
gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
if (len < 0)
hentry[0].body, hentry[1].body,
hentry[2].body, hentry[3].body,
hentry[4].body, hentry[5].body,
+ hentry[6].body, hentry[7].body,
mimeinfo->data.filename, lastoffset,
- len);
- }
+ len, short_scan);
+ if (result == 1 && short_scan)
+ break;
+
+ }
if (buf[2 + boundary_len] == '-' &&
- buf[2 + boundary_len + 1] == '-')
+ buf[2 + boundary_len + 1] == '-') {
+ end_found = TRUE;
break;
-
+ }
for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
g_free(hentry[i].body);
hentry[i].body = NULL;
}
- procheader_get_header_fields(fp, hentry);
- if (hentry[0].body != NULL) {
- tmp = conv_unmime_header(hentry[0].body, NULL);
- g_free(hentry[0].body);
- hentry[0].body = tmp;
- }
- if (hentry[2].body != NULL) {
- tmp = conv_unmime_header(hentry[2].body, NULL);
- g_free(hentry[2].body);
- hentry[2].body = tmp;
- }
- if (hentry[4].body != NULL) {
- tmp = conv_unmime_header(hentry[4].body, NULL);
- g_free(hentry[4].body);
- hentry[4].body = tmp;
- }
- if (hentry[5].body != NULL) {
- tmp = conv_unmime_header(hentry[5].body, NULL);
- g_free(hentry[5].body);
- hentry[5].body = tmp;
- }
+ GET_HEADERS();
lastoffset = ftell(fp);
}
}
+
+ if (start_found && !end_found && lastoffset != -1) {
+ gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
+
+ if (len >= 0) {
+ result = procmime_parse_mimepart(mimeinfo,
+ hentry[0].body, hentry[1].body,
+ hentry[2].body, hentry[3].body,
+ hentry[4].body, hentry[5].body,
+ hentry[6].body, hentry[7].body,
+ mimeinfo->data.filename, lastoffset,
+ len, short_scan);
+ }
+ mimeinfo->broken = TRUE;
+ }
+
for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
g_free(hentry[i].body);
hentry[i].body = NULL;
}
- fclose(fp);
+ procmime_fclose(fp);
}
static void parse_parameters(const gchar *parameters, GHashTable *table)
param = params;
next = params;
for (; next != NULL; param = next) {
- gchar *attribute, *value, *tmp;
+ gchar *attribute, *value, *tmp, *down_attr, *orig_down_attr;
gint len;
gboolean convert = FALSE;
while (value[0] == ' ')
value++;
- g_strdown(attribute);
-
- len = strlen(attribute);
- if (attribute[len - 1] == '*') {
+ down_attr = g_utf8_strdown(attribute, -1);
+ orig_down_attr = down_attr;
+
+ len = down_attr ? strlen(down_attr):0;
+ if (len > 0 && down_attr[len - 1] == '*') {
gchar *srcpos, *dstpos, *endpos;
convert = TRUE;
- attribute[len - 1] = '\0';
+ down_attr[len - 1] = '\0';
srcpos = value;
dstpos = value;
dstpos++;
}
*dstpos = '\0';
+ if (value[0] == '"')
+ extract_quote(value, '"');
} else {
if (value[0] == '"')
extract_quote(value, '"');
*tmp = '\0';
}
- if (attribute) {
- while (attribute[0] == ' ')
- attribute++;
- while (attribute[strlen(attribute)-1] == ' ')
- attribute[strlen(attribute)-1] = '\0';
+ if (down_attr) {
+ while (down_attr[0] == ' ')
+ down_attr++;
+ while (down_attr[strlen(down_attr)-1] == ' ')
+ down_attr[strlen(down_attr)-1] = '\0';
}
if (value) {
while (value[0] == ' ')
while (value[strlen(value)-1] == ' ')
value[strlen(value)-1] = '\0';
}
- if (strrchr(attribute, '*') != NULL) {
+ if (down_attr && strrchr(down_attr, '*') != NULL) {
gchar *tmpattr;
- tmpattr = g_strdup(attribute);
+ tmpattr = g_strdup(down_attr);
tmp = strrchr(tmpattr, '*');
tmp[0] = '\0';
if ((tmp[1] == '0') && (tmp[2] == '\0') &&
- (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
+ (g_slist_find_custom(concatlist, down_attr, (GCompareFunc)g_strcmp0) == NULL))
concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
- if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
+ if (convert && (g_slist_find_custom(convlist, tmpattr, (GCompareFunc)g_strcmp0) == NULL))
convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
g_free(tmpattr);
} else if (convert) {
- if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
- convlist = g_slist_prepend(convlist, g_strdup(attribute));
+ if (g_slist_find_custom(convlist, down_attr, (GCompareFunc)g_strcmp0) == NULL)
+ convlist = g_slist_prepend(convlist, g_strdup(down_attr));
}
- if (g_hash_table_lookup(table, attribute) == NULL)
- g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
+ if (g_hash_table_lookup(table, down_attr) == NULL)
+ g_hash_table_insert(table, g_strdup(down_attr), g_strdup(value));
+ g_free(orig_down_attr);
}
for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
g_string_append(value, partvalue);
+ g_hash_table_remove(table, attrwnum);
g_free(attrwnum);
n++;
attrwnum = g_strdup_printf("%s*%d", attribute, n);
g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
g_string_free(value, TRUE);
}
- slist_free_strings(concatlist);
- g_slist_free(concatlist);
+ slist_free_strings_full(concatlist);
for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
gchar *attribute, *key, *value;
continue;
charset = value;
+ if (charset == NULL)
+ continue;
lang = strchr(charset, '\'');
if (lang == NULL)
continue;
g_hash_table_insert(table, g_strdup(attribute), newvalue);
}
- slist_free_strings(convlist);
- g_slist_free(convlist);
+ slist_free_strings_full(convlist);
g_free(params);
}
static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
{
- g_return_if_fail(content_type != NULL);
- g_return_if_fail(mimeinfo != NULL);
+ cm_return_if_fail(content_type != NULL);
+ cm_return_if_fail(mimeinfo != NULL);
/* RFC 2045, page 13 says that the mime subtype is MANDATORY;
* if it's not available we use the default Content-Type */
}
mimeinfo->type = procmime_get_media_type(type);
- mimeinfo->subtype = g_strdup(subtype);
+ mimeinfo->subtype = g_strstrip(g_strdup(subtype));
/* Get mimeinfo->typeparameters */
if (params != NULL)
{
gchar *tmp, *params;
- g_return_if_fail(content_disposition != NULL);
- g_return_if_fail(mimeinfo != NULL);
+ cm_return_if_fail(content_disposition != NULL);
+ cm_return_if_fail(mimeinfo != NULL);
tmp = g_strdup(content_disposition);
if ((params = strchr(tmp, ';')) != NULL) {
return;
}
+static GSList *registered_parsers = NULL;
+
+static MimeParser *procmime_get_mimeparser_for_type(MimeMediaType type, const gchar *sub_type)
+{
+ GSList *cur;
+ for (cur = registered_parsers; cur; cur = cur->next) {
+ MimeParser *parser = (MimeParser *)cur->data;
+ if (parser->type == type && !strcmp2(parser->sub_type, sub_type))
+ return parser;
+ }
+ return NULL;
+}
+
+void procmime_mimeparser_register(MimeParser *parser)
+{
+ if (!procmime_get_mimeparser_for_type(parser->type, parser->sub_type))
+ registered_parsers = g_slist_append(registered_parsers, parser);
+}
+
+
+void procmime_mimeparser_unregister(MimeParser *parser)
+{
+ registered_parsers = g_slist_remove(registered_parsers, parser);
+}
+
+static gboolean procmime_mimeparser_parse(MimeParser *parser, MimeInfo *mimeinfo)
+{
+ cm_return_val_if_fail(parser->parse != NULL, FALSE);
+ return parser->parse(parser, mimeinfo);
+}
+
static int procmime_parse_mimepart(MimeInfo *parent,
gchar *content_type,
gchar *content_encoding,
gchar *content_id,
gchar *content_disposition,
gchar *content_location,
+ const gchar *original_msgid,
+ const gchar *disposition_notification_hdr,
const gchar *filename,
guint offset,
- guint length)
+ guint length,
+ gboolean short_scan)
{
MimeInfo *mimeinfo;
+ MimeParser *parser = NULL;
+ gboolean parsed = FALSE;
+ int result = 0;
/* Create MimeInfo */
mimeinfo = procmime_mimeinfo_new();
mimeinfo->content = MIMECONTENT_FILE;
+
if (parent != NULL) {
if (g_node_depth(parent->node) > 32) {
/* 32 is an arbitrary value
mimeinfo->length = length;
if (content_type != NULL) {
+ remove_space(content_type);
procmime_parse_content_type(content_type, mimeinfo);
} else {
mimeinfo->type = MIMETYPE_TEXT;
}
if (content_encoding != NULL) {
+ remove_space(content_encoding);
procmime_parse_content_encoding(content_encoding, mimeinfo);
} else {
mimeinfo->encoding_type = ENC_UNKNOWN;
mimeinfo->location = NULL;
if (content_disposition != NULL)
+ remove_space(content_disposition);
procmime_parse_content_disposition(content_disposition, mimeinfo);
else
mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
/* Call parser for mime type */
- switch (mimeinfo->type) {
+ if ((parser = procmime_get_mimeparser_for_type(mimeinfo->type, mimeinfo->subtype)) != NULL) {
+ parsed = procmime_mimeparser_parse(parser, mimeinfo);
+ }
+ if (!parsed) {
+ switch (mimeinfo->type) {
+ case MIMETYPE_TEXT:
+ if (g_ascii_strcasecmp(mimeinfo->subtype, "plain") == 0 && short_scan) {
+ return 1;
+ }
+ break;
+
case MIMETYPE_MESSAGE:
if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
- procmime_parse_message_rfc822(mimeinfo);
+ procmime_parse_message_rfc822(mimeinfo, short_scan);
+ }
+ if (g_ascii_strcasecmp(mimeinfo->subtype, "disposition-notification") == 0) {
+ procmime_parse_disposition_notification(mimeinfo,
+ original_msgid, disposition_notification_hdr, short_scan);
}
break;
case MIMETYPE_MULTIPART:
- procmime_parse_multipart(mimeinfo);
+ procmime_parse_multipart(mimeinfo, short_scan);
+ break;
+
+ case MIMETYPE_APPLICATION:
+ if (g_ascii_strcasecmp(mimeinfo->subtype, "octet-stream") == 0
+ && original_msgid && *original_msgid
+ && disposition_notification_hdr && *disposition_notification_hdr) {
+ procmime_parse_disposition_notification(mimeinfo,
+ original_msgid, disposition_notification_hdr, short_scan);
+ }
break;
-
default:
break;
+ }
}
- return 0;
+ return result;
}
static gchar *typenames[] = {
depth = g_node_depth(node);
for (i = 0; i < depth; i++)
- printf(" ");
- printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
+ g_print(" ");
+ g_print("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
return FALSE;
}
g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
}
-static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
+static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset, gboolean short_scan)
{
MimeInfo *mimeinfo;
struct stat buf;
- stat(filename, &buf);
+ g_stat(filename, &buf);
mimeinfo = procmime_mimeinfo_new();
mimeinfo->content = MIMECONTENT_FILE;
mimeinfo->offset = offset;
mimeinfo->length = buf.st_size - offset;
- procmime_parse_message_rfc822(mimeinfo);
+ procmime_parse_message_rfc822(mimeinfo, short_scan);
if (debug_get_mode())
output_mime_structure(mimeinfo, 0);
return mimeinfo;
}
-MimeInfo *procmime_scan_file(const gchar *filename)
+static MimeInfo *procmime_scan_file_full(const gchar *filename, gboolean short_scan)
{
MimeInfo *mimeinfo;
- g_return_val_if_fail(filename != NULL, NULL);
+ cm_return_val_if_fail(filename != NULL, NULL);
- mimeinfo = procmime_scan_file_with_offset(filename, 0);
+ mimeinfo = procmime_scan_file_with_offset(filename, 0, short_scan);
return mimeinfo;
}
-MimeInfo *procmime_scan_queue_file(const gchar *filename)
+MimeInfo *procmime_scan_file(const gchar *filename)
+{
+ return procmime_scan_file_full(filename, FALSE);
+}
+
+static MimeInfo *procmime_scan_file_short(const gchar *filename)
+{
+ return procmime_scan_file_full(filename, TRUE);
+}
+
+static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan)
{
FILE *fp;
MimeInfo *mimeinfo;
gchar buf[BUFFSIZE];
gint offset = 0;
- g_return_val_if_fail(filename != NULL, NULL);
+ cm_return_val_if_fail(filename != NULL, NULL);
/* Open file */
- if ((fp = g_fopen(filename, "rb")) == NULL)
+ if ((fp = procmime_fopen(filename, "rb")) == NULL)
return NULL;
/* Skip queue header */
- while (fgets(buf, sizeof(buf), fp) != NULL) {
+ while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
/* new way */
if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
strlen("X-Claws-End-Special-Headers:"))) ||
}
}
offset = ftell(fp);
- fclose(fp);
+ procmime_fclose(fp);
- mimeinfo = procmime_scan_file_with_offset(filename, offset);
+ mimeinfo = procmime_scan_file_with_offset(filename, offset, short_scan);
return mimeinfo;
}
+MimeInfo *procmime_scan_queue_file(const gchar *filename)
+{
+ return procmime_scan_queue_file_full(filename, FALSE);
+}
+
+static MimeInfo *procmime_scan_queue_file_short(const gchar *filename)
+{
+ return procmime_scan_queue_file_full(filename, TRUE);
+}
+
typedef enum {
ENC_AS_TOKEN,
ENC_AS_QUOTED_STRING,
ENC_AS_EXTENDED,
- ENC_TO_ASCII,
+ ENC_AS_ENCWORD
} EncodeAs;
typedef struct _ParametersData {
FILE *fp;
guint len;
- guint ascii_only;
+ gint error;
} ParametersData;
static void write_parameters(gpointer key, gpointer value, gpointer user_data)
gchar *val = value, *valpos, *tmp;
ParametersData *pdata = (ParametersData *)user_data;
GString *buf = g_string_new("");
+ gint len;
EncodeAs encas = ENC_AS_TOKEN;
for (valpos = val; *valpos != 0; valpos++) {
- if (!IS_ASCII(*valpos) || *valpos == '"') {
- encas = ENC_AS_EXTENDED;
+ if (!IS_ASCII(*valpos)) {
+ encas = ENC_AS_ENCWORD;
break;
}
case ';':
case ':':
case '\\':
- case '\'':
+ case '"':
case '/':
case '[':
case ']':
}
}
- if (encas == ENC_AS_EXTENDED && pdata->ascii_only == TRUE)
- encas = ENC_TO_ASCII;
-
switch (encas) {
case ENC_AS_TOKEN:
g_string_append_printf(buf, "%s=%s", param, val);
break;
- case ENC_TO_ASCII:
- tmp = g_strdup(val);
- g_strcanon(tmp,
- " ()<>@,';:\\/[]?=.0123456789"
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- "abcdefghijklmnopqrstuvwxyz",
- '_');
- g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
- g_free(tmp);
- break;
-
case ENC_AS_QUOTED_STRING:
g_string_append_printf(buf, "%s=\"%s\"", param, val);
break;
+#if 0 /* we don't use that for now */
case ENC_AS_EXTENDED:
if (!g_utf8_validate(val, -1, NULL))
g_string_append_printf(buf, "%s*=%s''", param,
g_string_append_printf(buf, "%%%s", hexstr);
}
}
- break;
+ break;
+#else
+ case ENC_AS_EXTENDED:
+ debug_print("Unhandled ENC_AS_EXTENDED.");
+ break;
+#endif
+ case ENC_AS_ENCWORD:
+ len = MAX(strlen(val)*6, 512);
+ tmp = g_malloc(len+1);
+ codeconv_set_strict(TRUE);
+ conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
+ prefs_common.outgoing_charset);
+ codeconv_set_strict(FALSE);
+ if (!tmp || !*tmp) {
+ codeconv_set_strict(TRUE);
+ conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
+ conv_get_outgoing_charset_str());
+ codeconv_set_strict(FALSE);
+ }
+ if (!tmp || !*tmp) {
+ codeconv_set_strict(TRUE);
+ conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
+ CS_UTF_8);
+ codeconv_set_strict(FALSE);
+ }
+ if (!tmp || !*tmp) {
+ conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
+ CS_UTF_8);
+ }
+ g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
+ g_free(tmp);
+ break;
+
}
if (buf->str && strlen(buf->str)) {
- if (pdata->len + strlen(buf->str) + 2 > 76) {
- fprintf(pdata->fp, ";\n %s", buf->str);
+ tmp = strstr(buf->str, "\n");
+ if (tmp)
+ len = (tmp - buf->str);
+ else
+ len = strlen(buf->str);
+ if (pdata->len + len > 76) {
+ if (fprintf(pdata->fp, ";\n %s", buf->str) < 0)
+ pdata->error = TRUE;
pdata->len = strlen(buf->str) + 1;
} else {
- fprintf(pdata->fp, "; %s", buf->str);
+ if (fprintf(pdata->fp, "; %s", buf->str) < 0)
+ pdata->error = TRUE;
pdata->len += strlen(buf->str) + 2;
}
}
g_string_free(buf, TRUE);
}
-void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
+#define TRY(func) { \
+ if (!(func)) { \
+ return -1; \
+ } \
+}
+
+int procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
{
struct TypeTable *type_table;
ParametersData *pdata = g_new0(ParametersData, 1);
debug_print("procmime_write_mime_header\n");
pdata->fp = fp;
- pdata->ascii_only = FALSE;
-
+ pdata->error = FALSE;
for (type_table = mime_type_table; type_table->str != NULL; type_table++)
if (mimeinfo->type == type_table->type) {
gchar *buf = g_strdup_printf(
"Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
- fprintf(fp, "%s", buf);
+ if (fprintf(fp, "%s", buf) < 0) {
+ g_free(buf);
+ g_free(pdata);
+ return -1;
+ }
pdata->len = strlen(buf);
- pdata->ascii_only = TRUE;
g_free(buf);
break;
}
g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
+ if (pdata->error == TRUE) {
+ g_free(pdata);
+ return -1;
+ }
g_free(pdata);
- fprintf(fp, "\n");
+ TRY(fprintf(fp, "\n") >= 0);
if (mimeinfo->encoding_type != ENC_UNKNOWN)
- fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
+ TRY(fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type)) >= 0);
if (mimeinfo->description != NULL)
- fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
+ TRY(fprintf(fp, "Content-Description: %s\n", mimeinfo->description) >= 0);
if (mimeinfo->id != NULL)
- fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
+ TRY(fprintf(fp, "Content-ID: %s\n", mimeinfo->id) >= 0);
if (mimeinfo->location != NULL)
- fprintf(fp, "Content-Location: %s\n", mimeinfo->location);
+ TRY(fprintf(fp, "Content-Location: %s\n", mimeinfo->location) >= 0);
if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
ParametersData *pdata = g_new0(ParametersData, 1);
else
buf = g_strdup("Content-Disposition: unknown");
- fprintf(fp, "%s", buf);
+ if (fprintf(fp, "%s", buf) < 0) {
+ g_free(buf);
+ g_free(pdata);
+ return -1;
+ }
pdata->len = strlen(buf);
g_free(buf);
pdata->fp = fp;
- pdata->ascii_only = FALSE;
-
+ pdata->error = FALSE;
g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
+ if (pdata->error == TRUE) {
+ g_free(pdata);
+ return -1;
+ }
g_free(pdata);
- fprintf(fp, "\n");
+ TRY(fprintf(fp, "\n") >= 0);
}
- fprintf(fp, "\n");
+ TRY(fprintf(fp, "\n") >= 0);
+
+ return 0;
}
static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
MimeInfo *child;
gchar buf[BUFFSIZE];
gboolean skip = FALSE;;
+ size_t len;
debug_print("procmime_write_message_rfc822\n");
/* write header */
switch (mimeinfo->content) {
case MIMECONTENT_FILE:
- if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+ if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
return -1;
}
fseek(infp, mimeinfo->offset, SEEK_SET);
- while (fgets(buf, sizeof(buf), infp) == buf) {
+ while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
strcrchomp(buf);
if (buf[0] == '\n' && buf[1] == '\0')
break;
if (skip && (buf[0] == ' ' || buf[0] == '\t'))
continue;
- if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
+ if (g_ascii_strncasecmp(buf, "MIME-Version:", 13) == 0 ||
g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
skip = TRUE;
continue;
}
- fwrite(buf, sizeof(gchar), strlen(buf), fp);
+ len = strlen(buf);
+ if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
+ g_warning("failed to dump %zd bytes from file", len);
+ procmime_fclose(infp);
+ return -1;
+ }
skip = FALSE;
}
- fclose(infp);
+ procmime_fclose(infp);
break;
case MIMECONTENT_MEM:
- fwrite(mimeinfo->data.mem,
- sizeof(gchar),
- strlen(mimeinfo->data.mem),
- fp);
+ len = strlen(mimeinfo->data.mem);
+ if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len) {
+ g_warning("failed to dump %zd bytes from mem", len);
+ return -1;
+ }
break;
default:
return -1;
child = (MimeInfo *) childnode->data;
- fprintf(fp, "Mime-Version: 1.0\n");
- procmime_write_mime_header(child, fp);
+ if (fprintf(fp, "MIME-Version: 1.0\n") < 0) {
+ g_warning("failed to write mime version");
+ return -1;
+ }
+ if (procmime_write_mime_header(child, fp) < 0)
+ return -1;
return procmime_write_mimeinfo(child, fp);
}
gchar *boundary, *str, *str2;
gchar buf[BUFFSIZE];
gboolean firstboundary;
+ size_t len;
debug_print("procmime_write_multipart\n");
switch (mimeinfo->content) {
case MIMECONTENT_FILE:
- if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+ if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
return -1;
}
fseek(infp, mimeinfo->offset, SEEK_SET);
- while (fgets(buf, sizeof(buf), infp) == buf) {
+ while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
break;
- fwrite(buf, sizeof(gchar), strlen(buf), fp);
+ len = strlen(buf);
+ if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
+ g_warning("failed to write %zd", len);
+ procmime_fclose(infp);
+ return -1;
+ }
}
- fclose(infp);
+ procmime_fclose(infp);
break;
case MIMECONTENT_MEM:
if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
(*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
*(str2 - 2) = '\0';
- fwrite(str, sizeof(gchar), strlen(str), fp);
+ len = strlen(str);
+ if (SC_FWRITE(str, sizeof(gchar), len, fp) < len) {
+ g_warning("failed to write %zd from mem", len);
+ g_free(str);
+ return -1;
+ }
g_free(str);
break;
if (firstboundary)
firstboundary = FALSE;
else
- fprintf(fp, "\n");
- fprintf(fp, "--%s\n", boundary);
+ TRY(fprintf(fp, "\n") >= 0);
+
+ TRY(fprintf(fp, "--%s\n", boundary) >= 0);
- procmime_write_mime_header(child, fp);
+ if (procmime_write_mime_header(child, fp) < 0)
+ return -1;
if (procmime_write_mimeinfo(child, fp) < 0)
return -1;
childnode = g_node_next_sibling(childnode);
}
- fprintf(fp, "\n--%s--\n", boundary);
+ TRY(fprintf(fp, "\n--%s--\n", boundary) >= 0);
return 0;
}
gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
{
FILE *infp;
-
+ size_t len;
debug_print("procmime_write_mimeinfo\n");
if (G_NODE_IS_LEAF(mimeinfo->node)) {
switch (mimeinfo->content) {
case MIMECONTENT_FILE:
- if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+ if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
return -1;
}
copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
- fclose(infp);
+ procmime_fclose(infp);
return 0;
case MIMECONTENT_MEM:
- fwrite(mimeinfo->data.mem,
- sizeof(gchar),
- strlen(mimeinfo->data.mem),
- fp);
+ len = strlen(mimeinfo->data.mem);
+ if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len)
+ return -1;
return 0;
default: