+ mp_stream = tny_mime_part_get_decoded_stream (body);
+
+ is_html = (g_strcmp0 (tny_mime_part_get_content_type (body), "text/html") == 0);
+ if (is_html) {
+ input_stream = modest_stream_html_to_text_new (mp_stream);
+ } else {
+ input_stream = g_object_ref (mp_stream);
+ }
+
+ total = 0;
+ total_lines = 0;
+ line_chars = 0;
+ lines = 0;
+
+ forced_wrap = FALSE;
+ first_time = TRUE;
+ while (!tny_stream_is_eos (input_stream)) {
+ gchar buffer [128];
+ gchar *offset;
+ gint n_read;
+ gint next_read;
+
+ next_read = MIN (128, MAX_BODY_LENGTH - total);
+ if (next_read == 0)
+ break;
+ n_read = tny_stream_read (input_stream, buffer, next_read);
+
+ if (G_UNLIKELY (n_read < 0))
+ break;
+
+ offset = buffer;
+ while (offset < buffer + n_read) {
+
+ if (*offset == '\n' && !(forced_wrap && offset[1] == '\n')) {
+ total_lines ++;
+ line_chars = 0;
+ } else {
+ if (*offset == '\n') {
+ forced_wrap = FALSE;
+ }
+ line_chars ++;
+ if (line_chars >= LINE_WRAP) {
+ total_lines ++;
+ line_chars = 0;
+ forced_wrap = TRUE;
+ }
+ }
+ if (total_lines >= MAX_BODY_LINES)
+ break;
+ offset++;
+ }
+
+ if (offset - buffer > 0) {
+ gint n_write = 0, to_write = 0;
+
+ to_write = offset - buffer;
+ n_write = tny_stream_write (stream, buffer, to_write);
+ total += n_write;
+ } else if (n_read == -1) {
+ break;
+ }
+
+ if (total_lines >= MAX_BODY_LINES)
+ break;
+ }
+