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;
15 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;
50 sub format_message(@){
53 my $message=$params{message} ||
54 error gettext("missing parameter"). "message";
57 template("email.tmpl") || error gettext("missing template");
60 my @headers=map { {'HEADERNAME'=>$_,'VAL'=>$message->header($_)} }
61 $message->header_names;
63 $template->param(HEADERS=>[@headers]);
65 $template->param(body=>$message->body);
67 my $output=$template->output();
75 # http://ikiwiki.info/todo/Bestdir_along_with_bestlink_in_IkiWiki.pm/
76 # need to clarify license
77 sub bestdir ($$) { #{{{
82 if ($link=~s/^\/+//) {
90 if (-d "$config{srcdir}/$l") {
93 } while $cwd=~s!/?[^/]+$!!;
95 if (length $config{userdir}) {
96 my $l = "$config{userdir}/".lc($link);
108 sub fill_template(@){
110 my $template = $params{template} || error gettext("missing parameter");
112 $params{basename}=IkiWiki::basename($params{page});
114 foreach my $param (keys %params) {
115 if ($template->query(name => $param)) {
116 $template->param($param =>
117 IkiWiki::htmlize($params{page}, $params{destpage},
118 pagetype($pagesources{$params{page}}),
121 if ($template->query(name => "raw_$param")) {
122 $template->param("raw_$param" => $params{$param});