Permalink
Browse files

Parse and display message/rfc822 parts.

  • Loading branch information...
wangp committed Mar 27, 2012
1 parent d8de8e3 commit 5e0488a5c6ee90c209d85f34ab9de0de06d0c7c8
Showing with 80 additions and 3 deletions.
  1. +23 −2 callout.m
  2. +6 −1 compose.m
  3. +14 −0 copious_output.m
  4. +7 −0 data.m
  5. +30 −0 pager.m
View
@@ -209,8 +209,14 @@
notmuch_json_error
)
; strcase_equal(ContentType, "message/rfc822") ->
- Content = unsupported,
- MaybeFilename = no
+ ( JSON/"content" = array(List) ->
+ list.map(parse_encapsulated_message(MessageId), List,
+ EncapMessages),
+ Content = encapsulated_messages(EncapMessages),
+ MaybeFilename = no
+ ;
+ notmuch_json_error
+ )
;
% Leaf part.
( JSON/"filename" = unesc_string(Filename) ->
@@ -231,6 +237,21 @@
notmuch_json_error
).
+:- pred parse_encapsulated_message(message_id::in, json::in,
+ encapsulated_message::out) is det.
+
+parse_encapsulated_message(MessageId, JSON, EncapMessage) :-
+ (
+ JSON/"headers" = map(HeaderMap),
+ map.foldl(parse_header, HeaderMap, init_headers, Headers),
+ JSON/"body" = array(BodyList),
+ list.map(parse_part(MessageId), BodyList, Body)
+ ->
+ EncapMessage = encapsulated_message(Headers, Body)
+ ;
+ notmuch_json_error
+ ).
+
%-----------------------------------------------------------------------------%
parse_threads_list(Json, Threads) :-
View
@@ -317,7 +317,9 @@
PartContent = subparts(SubParts),
first_text_part(SubParts, Text, AttachmentParts)
;
- PartContent = unsupported,
+ ( PartContent = encapsulated_messages(_)
+ ; PartContent = unsupported
+ ),
first_text_part(Parts, Text, AttachmentParts0),
AttachmentParts = [Part | AttachmentParts0]
).
@@ -1410,6 +1412,9 @@ not separator_char(C).
;
Content = subparts(_),
unexpected($module, $pred, "nested part")
+ ;
+ Content = encapsulated_messages(_),
+ unexpected($module, $pred, "encapsulated_messages")
),
MaybeFileName = Part ^ pt_filename
;
View
@@ -44,6 +44,12 @@
list.map_foldl(expand_part, SubParts0, SubParts, !IO),
Content = subparts(SubParts),
Part = part(MessageId, PartId, Type, Content, MaybeFilename)
+ ;
+ Content0 = encapsulated_messages(EncapMessages0),
+ list.map_foldl(expand_encapsulated_message, EncapMessages0,
+ EncapMessages, !IO),
+ Content = encapsulated_messages(EncapMessages),
+ Part = part(MessageId, PartId, Type, Content, MaybeFilename)
;
Content0 = unsupported,
(
@@ -58,6 +64,14 @@
)
).
+:- pred expand_encapsulated_message(encapsulated_message::in,
+ encapsulated_message::out, io::di, io::uo) is det.
+
+expand_encapsulated_message(EncapMessage0, EncapMessage, !IO) :-
+ EncapMessage0 = encapsulated_message(Headers, SubParts0),
+ list.map_foldl(expand_part, SubParts0, SubParts, !IO),
+ EncapMessage = encapsulated_message(Headers, SubParts).
+
:- pred expand_html(message_id::in, int::in, part_content::out,
io::di, io::uo) is det.
View
7 data.m
@@ -69,8 +69,15 @@
:- type part_content
---> text(string)
; subparts(list(part))
+ ; encapsulated_messages(list(encapsulated_message))
; unsupported.
+:- type encapsulated_message
+ ---> encapsulated_message(
+ em_headers :: headers,
+ em_body :: list(part)
+ ).
+
%-----------------------------------------------------------------------------%
:- func thread_id_to_search_term(thread_id) = string.
View
30 pager.m
@@ -230,6 +230,11 @@
;
list.foldl2(append_part(Cols), SubParts, !Lines, !IsFirst)
)
+ ;
+ Content = encapsulated_messages(EncapMessages),
+ snoc(attachment(Part), !Lines),
+ list.foldl(append_encapsulated_message(Cols), EncapMessages, !Lines),
+ !:IsFirst = no
;
Content = unsupported,
snoc(blank_line, !Lines),
@@ -391,6 +396,31 @@
I = I0
).
+:- pred append_encapsulated_message(int::in, encapsulated_message::in,
+ cord(pager_line)::in, cord(pager_line)::out) is det.
+
+append_encapsulated_message(Cols, EncapMessage, !Lines) :-
+ EncapMessage = encapsulated_message(Headers, Body),
+ append_encapsulated_header("Date", Headers ^ h_date, !Lines),
+ append_encapsulated_header("From", Headers ^ h_from, !Lines),
+ append_encapsulated_header("Subject", Headers ^ h_subject, !Lines),
+ append_encapsulated_header("To", Headers ^ h_to, !Lines),
+ append_encapsulated_header("Cc", Headers ^ h_cc, !Lines),
+ append_encapsulated_header("Reply-To", Headers ^ h_replyto, !Lines),
+ snoc(blank_line, !Lines),
+ list.foldl2(append_part(Cols), Body, !Lines, yes, _IsFirst).
+
+:- pred append_encapsulated_header(string::in, string::in,
+ cord(pager_line)::in, cord(pager_line)::out) is det.
+
+append_encapsulated_header(Header, Value, !Lines) :-
+ ( Value = "" ->
+ true
+ ;
+ Line = text(Header ++ ": " ++ Value, no_url),
+ snoc(Line, !Lines)
+ ).
+
:- func blank_line = pager_line.
blank_line = text("", no_url).

0 comments on commit 5e0488a

Please sign in to comment.