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);
18 sub preprocess (@) { #{{{
21 my $page=$params{page};
22 my $type=$params{type} || 'maildir';
24 my $path=$params{path} || error gettext("missing parameter") . " path";
26 # note, mbox is not a directory, needs to be special cased
27 my $dir=bestdir($page,$params{path}) ||
28 error("could not find ".$params{path});
30 $params{path} = $config{srcdir} ."/" . $dir;
32 return format_mailbox(path=>$dir,%params);
37 ### The guts of the plugin
39 sub format_mailbox(@){
41 my $path=$params{path} || error("path parameter mandatory");
42 my $header_list=$params{headers} || "subject,from";
44 my $folder=Email::Folder->new($path) || error("mailbox could not be opened");
45 return join "\n", map { format_message(message=>$_) } $folder->messages;
52 my $val=$message->header($_);
53 my $hash={'HEADERNAME'=>$name,'VAL'=>$val};
56 sub format_message(@){
59 my $message=$params{message} ||
60 error gettext("missing parameter"). "message";
63 template("email.tmpl") || error gettext("missing template");
65 my @headers=map { make_pair($message,$_) }
66 $message->header_names;
68 $template->param(HEADERS=>[@headers]);
70 $template->param(body=>$message->body);
72 my $output=$template->output();
79 # http://ikiwiki.info/todo/Bestdir_along_with_bestlink_in_IkiWiki.pm/
80 # need to clarify license
81 sub bestdir ($$) { #{{{
86 if ($link=~s/^\/+//) {
94 if (-d "$config{srcdir}/$l") {
97 } while $cwd=~s!/?[^/]+$!!;
99 if (length $config{userdir}) {
100 my $l = "$config{userdir}/".lc($link);
112 sub fill_template(@){
114 my $template = $params{template} || error gettext("missing parameter");
116 $params{basename}=IkiWiki::basename($params{page});
118 foreach my $param (keys %params) {
119 if ($template->query(name => $param)) {
120 $template->param($param =>
121 IkiWiki::htmlize($params{page}, $params{destpage},
122 pagetype($pagesources{$params{page}}),
125 if ($template->query(name => "raw_$param")) {
126 $template->param("raw_$param" => $params{$param});