]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/mailbox.pm
Scan a maildir and output each message via a template. Currently output is escaped...
[git.ikiwiki.info.git] / IkiWiki / Plugin / mailbox.pm
1 #!/usr/bin/perl
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;
9 use warnings;
10 use strict;
11 use IkiWiki 2.00;
12 use Email::Folder;
14 sub import { #{{{
15         hook(type => "preprocess", id => "mailbox", call => \&preprocess);
16 } # }}}
19 sub preprocess (@) { #{{{
20         my %params=@_;
21         
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);
35 } # }}}
38 ### The guts of the plugin
39 ### parameters 
40 sub format_mailbox(@){
41     my %params=@_;
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;
48 }
50 sub format_message(@){
51     my  %params=@_;
53     my $message=$params{message} || 
54         error gettext("missing parameter"). "message";
56     my $template= 
57         template("email.tmpl") || error gettext("missing template");
58     
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();
68     print STDERR $output;
69     return $output;
70 }
72 ### Utilities
74 # From Arpit Jain
75 # http://ikiwiki.info/todo/Bestdir_along_with_bestlink_in_IkiWiki.pm/
76 # need to clarify license
77 sub bestdir ($$) { #{{{
78     my $page=shift;
79        my $link=shift;
80        my $cwd=$page;
82        if ($link=~s/^\/+//) {
83                $cwd="";
84        }
86        do {
87                my $l=$cwd;
88                $l.="/" if length $l;
89                $l.=$link;
90                if (-d "$config{srcdir}/$l") {
91                        return $l;
92                }
93        } while $cwd=~s!/?[^/]+$!!;
95        if (length $config{userdir}) {
96                my $l = "$config{userdir}/".lc($link);
98                if (-d $l) {
99                        return $l;
100                }
101        }
103        return "";
104 } #}}}
108 sub fill_template(@){
109     my  %params=@_;
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}}),
119                                               $params{$param}));
120         }
121         if ($template->query(name => "raw_$param")) {
122             $template->param("raw_$param" => $params{$param});
123         }
124     }