X-Git-Url: http://git.vanrenterghem.biz/git.ikiwiki.info.git/blobdiff_plain/1bff0dbe20367dd6e07f428b35637acbc201a17e..de5b1b004578c44dcc7d33748941108a88c6eb9d:/IkiWiki/Plugin/mailbox.pm diff --git a/IkiWiki/Plugin/mailbox.pm b/IkiWiki/Plugin/mailbox.pm index 7c8e4a5db..8f501ea40 100644 --- a/IkiWiki/Plugin/mailbox.pm +++ b/IkiWiki/Plugin/mailbox.pm @@ -4,18 +4,35 @@ # Copyright (c) 2008 David Bremner # This file is distributed under the Artistic License/GPL2+ +use Email::MIME; +package Email::MIMEFolder; +use base 'Email::Folder'; +sub bless_message { return Email::MIME->new($_[1]) }; + package IkiWiki::Plugin::mailbox; -use warnings; -use strict; use IkiWiki 2.00; use Email::Folder; +use Email::Thread; use CGI 'escapeHTML'; +use Data::Dumper; + +my %metaheaders; sub import { #{{{ hook(type => "preprocess", id => "mailbox", call => \&preprocess); + hook(type => "scan", id => "mailbox", call => \&scan); + hook(type => "pagetemplate", id=>"mailbox", call => \&pagetemplate); } # }}} +sub scan(@){ + my %params=@_; + my $page=$params{page}; + + push @{$metaheaders{$page}}, + '' +} + sub preprocess (@) { #{{{ my %params=@_; @@ -23,6 +40,8 @@ sub preprocess (@) { #{{{ my $type=$params{type} || 'maildir'; my $path=$params{path} || error gettext("missing parameter") . " path"; + + # hmm, this should probably only be inserted once per page. # note, mbox is not a directory, needs to be special cased my $dir=bestdir($page,$params{path}) || @@ -40,11 +59,38 @@ sub preprocess (@) { #{{{ sub format_mailbox(@){ my %params=@_; my $path=$params{path} || error("path parameter mandatory"); - my $header_list=$params{headers} || "subject,from"; - my $folder=Email::Folder->new($path) || error("mailbox could not be opened"); - return join "\n", map { format_message(message=>$_) } $folder->messages; + my $folder=Email::MIMEFolder->new($path) || error("mailbox could not be opened"); + my $threader=new Email::Thread($folder->messages); + + $threader->thread(); + + return join "\n", map { format_thread(thread=>$_) } $threader->rootset; +} + +sub format_thread(@){ + my %params=@_; + + my $thread=$params{thread} || error gettext("missing parameter") . "thread"; + + my $output=""; + + if ($thread->message) { + $output .= format_message(message=>$thread->message); + } else { + $output .= sprintf gettext("Message %s not available"), $thread; + } + + if ($thread->child){ + $output .= '
' . + format_thread(thread=>$thread->child). + '
'; + } + if ($thread->next){ + $output .= format_thread(thread=>$thread->next); + } + return $output; } sub make_pair($$){ @@ -63,20 +109,36 @@ sub format_message(@){ my $message=$params{message} || error gettext("missing parameter"). "message"; + my $keep_headers=$params{headers} || qr/^(subject|from|date)/i; + my $template= template("email.tmpl") || error gettext("missing template"); + + my $output=""; + + my @names = grep {m/$keep_headers/;} ($message->header_names); + my @headers=map { make_pair($message,$_) } @names; - my @headers=map { make_pair($message,$_) } - $message->header_names; $template->param(HEADERS=>[@headers]); - $template->param(body=>escapeHTML($message->body)); - my $output=$template->output(); + my $body= join("\n", map { $_->body } $message->parts); + + $template->param(body=>format_body($body)); + + $output .= $template->output(); return $output; } +sub format_body($){ + my $body=shift; + + # it is not completely clear to me the right way to go here. + # passing things straight to markdown is not working all that + # well. + return "
".escapeHTML($body)."
"; +} ### Utilities # From Arpit Jain @@ -111,25 +173,19 @@ sub bestdir ($$) { #{{{ return ""; } #}}} - - -sub fill_template(@){ - my %params=@_; - my $template = $params{template} || error gettext("missing parameter"); - - $params{basename}=IkiWiki::basename($params{page}); - - foreach my $param (keys %params) { - if ($template->query(name => $param)) { - $template->param($param => - IkiWiki::htmlize($params{page}, $params{destpage}, - pagetype($pagesources{$params{page}}), - $params{$param})); - } - if ($template->query(name => "raw_$param")) { - $template->param("raw_$param" => $params{$param}); +sub pagetemplate (@) { #{{{ + my %params=@_; + my $page=$params{page}; + my $destpage=$params{destpage}; + my $template=$params{template}; + + if (exists $metaheaders{$page} && $template->query(name => "meta")) { + # avoid duplicate meta lines + my %seen; + $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}})); } - } } -1 + + +1;