]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/mailbox.pm
initial MIME handling
[git.ikiwiki.info.git] / IkiWiki / Plugin / mailbox.pm
index 7c8e4a5dbc0df093f812c0d077b46d7ae2d908ff..8f501ea40021e3ae6b76dcdb275cd9441b5261c1 100644 (file)
@@ -4,18 +4,35 @@
 # Copyright (c) 2008 David Bremner <bremner@unb.ca>
 # 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}}, 
+              '<link rel="stylesheet" href="mailbox.css" type="text/css"/>'
+}
+
 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 .= '<div class="emailthreadindent">' .
+           format_thread(thread=>$thread->child).
+           '</div>';
+    }
 
+    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 "<pre>".escapeHTML($body)."</pre>";
+}
 ### 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;