2 # based on Ikiwiki skeleton plugin.
4 # Copyright (c) 2008 David Bremner <bremner@unb.ca>
5 # This file is distributed under the Artistic License/GPL2+
8 package Email::MIMEFolder;
9 use base 'Email::Folder';
10 sub bless_message { return Email::MIME->new($_[1]) };
12 package IkiWiki::Plugin::mailbox;
23 hook(type => "preprocess", id => "mailbox", call => \&preprocess);
24 hook(type => "scan", id => "mailbox", call => \&scan);
25 hook(type => "pagetemplate", id=>"mailbox", call => \&pagetemplate);
30 my $page=$params{page};
32 push @{$metaheaders{$page}},
33 '<link rel="stylesheet" href="mailbox.css" type="text/css"/>'
36 sub preprocess (@) { #{{{
39 my $page=$params{page};
40 my $type=$params{type} || 'maildir';
42 my $path=$params{path} || error gettext("missing parameter") . " path";
44 # hmm, this should probably only be inserted once per page.
46 # note, mbox is not a directory, needs to be special cased
47 my $dir=bestdir($page,$params{path}) ||
48 error("could not find ".$params{path});
50 $params{path} = $config{srcdir} ."/" . $dir;
52 return format_mailbox(path=>$dir,%params);
57 ### The guts of the plugin
59 sub format_mailbox(@){
61 my $path=$params{path} || error("path parameter mandatory");
63 my $folder=Email::MIMEFolder->new($path) || error("mailbox could not be opened");
64 my $threader=new Email::Thread($folder->messages);
68 return join "\n", map { format_thread(thread=>$_) } $threader->rootset;
74 my $thread=$params{thread} || error gettext("missing parameter") . "thread";
78 if ($thread->message) {
79 $output .= format_message(message=>$thread->message);
81 $output .= sprintf gettext("Message %s not available"), $thread;
85 $output .= '<div class="emailthreadindent">' .
86 format_thread(thread=>$thread->child).
91 $output .= format_thread(thread=>$thread->next);
99 my $val=$message->header($_);
101 $val = escapeHTML($val);
103 my $hash={'HEADERNAME'=>$name,'VAL'=>$val};
106 sub format_message(@){
109 my $message=$params{message} ||
110 error gettext("missing parameter"). "message";
112 my $keep_headers=$params{headers} || qr/^(subject|from|date)/i;
115 template("email.tmpl") || error gettext("missing template");
119 my @names = grep {m/$keep_headers/;} ($message->header_names);
120 my @headers=map { make_pair($message,$_) } @names;
123 $template->param(HEADERS=>[@headers]);
126 my $body= join("\n", map { $_->body } $message->parts);
128 $template->param(body=>format_body($body));
130 $output .= $template->output();
137 # it is not completely clear to me the right way to go here.
138 # passing things straight to markdown is not working all that
140 return "<pre>".escapeHTML($body)."</pre>";
145 # http://ikiwiki.info/todo/Bestdir_along_with_bestlink_in_IkiWiki.pm/
146 # need to clarify license
147 sub bestdir ($$) { #{{{
152 if ($link=~s/^\/+//) {
158 $l.="/" if length $l;
160 if (-d "$config{srcdir}/$l") {
163 } while $cwd=~s!/?[^/]+$!!;
165 if (length $config{userdir}) {
166 my $l = "$config{userdir}/".lc($link);
176 sub pagetemplate (@) { #{{{
178 my $page=$params{page};
179 my $destpage=$params{destpage};
180 my $template=$params{template};
182 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
183 # avoid duplicate meta lines
185 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));