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+
7 package IkiWiki::Plugin::mailbox;
20 hook(type => "preprocess", id => "mailbox", call => \&preprocess);
21 hook(type => "scan", id => "mailbox", call => \&scan);
22 hook(type => "pagetemplate", id=>"mailbox", call => \&pagetemplate);
27 my $page=$params{page};
29 debug('calling scan');
30 push @{$metaheaders{$page}},
31 '<link rel="stylesheet" href="mailbox.css" type="text/css"/>'
34 sub preprocess (@) { #{{{
37 my $page=$params{page};
38 my $type=$params{type} || 'maildir';
40 my $path=$params{path} || error gettext("missing parameter") . " path";
42 # hmm, this should probably only be inserted once per page.
44 # note, mbox is not a directory, needs to be special cased
45 my $dir=bestdir($page,$params{path}) ||
46 error("could not find ".$params{path});
48 $params{path} = $config{srcdir} ."/" . $dir;
50 return format_mailbox(path=>$dir,%params);
55 ### The guts of the plugin
57 sub format_mailbox(@){
59 my $path=$params{path} || error("path parameter mandatory");
61 my $folder=Email::Folder->new($path) || error("mailbox could not be opened");
62 my $threader=new Email::Thread($folder->messages);
66 return join "\n", map { format_thread(thread=>$_) } $threader->rootset;
72 my $thread=$params{thread} || error gettext("missing parameter") . "thread";
76 if ($thread->message) {
77 $output .= format_message(message=>$thread->message);
79 $output .= sprintf gettext("Message %s not available"), $thread;
83 $output .= '<div class="emailthreadindent">' .
84 format_thread(thread=>$thread->child).
89 $output .= format_thread(thread=>$thread->next);
97 my $val=$message->header($_);
99 $val = escapeHTML($val);
101 my $hash={'HEADERNAME'=>$name,'VAL'=>$val};
104 sub format_message(@){
107 my $message=$params{message} ||
108 error gettext("missing parameter"). "message";
110 my $keep_headers=$params{headers} || qr/^(subject|from|date)/i;
113 template("email.tmpl") || error gettext("missing template");
115 my @names = grep {m/$keep_headers/;} ($message->header_names);
116 my @headers=map { make_pair($message,$_) } @names;
119 $template->param(HEADERS=>[@headers]);
120 $template->param(body=>format_body($message->body));
122 my $output=$template->output();
129 # it is not completely clear to me the right way to go here.
130 # passing things straight to markdown is not working all that
132 return "<pre>".escapeHTML($body)."</pre>";
137 # http://ikiwiki.info/todo/Bestdir_along_with_bestlink_in_IkiWiki.pm/
138 # need to clarify license
139 sub bestdir ($$) { #{{{
144 if ($link=~s/^\/+//) {
150 $l.="/" if length $l;
152 if (-d "$config{srcdir}/$l") {
155 } while $cwd=~s!/?[^/]+$!!;
157 if (length $config{userdir}) {
158 my $l = "$config{userdir}/".lc($link);
168 sub pagetemplate (@) { #{{{
170 my $page=$params{page};
171 my $destpage=$params{destpage};
172 my $template=$params{template};
174 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
175 # avoid duplicate meta lines
177 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));