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;
16 hook(type => "preprocess", id => "mailbox", call => \&preprocess);
19 sub preprocess (@) { #{{{
22 my $page=$params{page};
23 my $type=$params{type} || 'maildir';
25 my $path=$params{path} || error gettext("missing parameter") . " path";
27 # note, mbox is not a directory, needs to be special cased
28 my $dir=bestdir($page,$params{path}) ||
29 error("could not find ".$params{path});
31 $params{path} = $config{srcdir} ."/" . $dir;
33 return format_mailbox(path=>$dir,%params);
38 ### The guts of the plugin
40 sub format_mailbox(@){
42 my $path=$params{path} || error("path parameter mandatory");
43 my $header_list=$params{headers} || "subject,from";
45 my $folder=Email::Folder->new($path) || error("mailbox could not be opened");
46 return join "\n", map { format_message(message=>$_) } $folder->messages;
53 my $val=$message->header($_);
55 $val = escapeHTML($val);
57 my $hash={'HEADERNAME'=>$name,'VAL'=>$val};
60 sub format_message(@){
63 my $message=$params{message} ||
64 error gettext("missing parameter"). "message";
67 template("email.tmpl") || error gettext("missing template");
69 my @headers=map { make_pair($message,$_) }
70 $message->header_names;
72 $template->param(HEADERS=>[@headers]);
74 $template->param(body=>escapeHTML($message->body));
76 my $output=$template->output();
83 # http://ikiwiki.info/todo/Bestdir_along_with_bestlink_in_IkiWiki.pm/
84 # need to clarify license
85 sub bestdir ($$) { #{{{
90 if ($link=~s/^\/+//) {
98 if (-d "$config{srcdir}/$l") {
101 } while $cwd=~s!/?[^/]+$!!;
103 if (length $config{userdir}) {
104 my $l = "$config{userdir}/".lc($link);
116 sub fill_template(@){
118 my $template = $params{template} || error gettext("missing parameter");
120 $params{basename}=IkiWiki::basename($params{page});
122 foreach my $param (keys %params) {
123 if ($template->query(name => $param)) {
124 $template->param($param =>
125 IkiWiki::htmlize($params{page}, $params{destpage},
126 pagetype($pagesources{$params{page}}),
129 if ($template->query(name => "raw_$param")) {
130 $template->param("raw_$param" => $params{$param});