]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/mailbox.pm
escape html, add test data
[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;
13 use CGI 'escapeHTML';
15 sub import { #{{{
16         hook(type => "preprocess", id => "mailbox", call => \&preprocess);
17 } # }}}
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 make_pair($$){
51     my $message=shift;
52     my $name=shift;
53     my $val=$message->header($_);
54     
55     $val = escapeHTML($val);
57     my $hash={'HEADERNAME'=>$name,'VAL'=>$val};
58     return $hash;
59 }
60 sub format_message(@){
61     my  %params=@_;
63     my $message=$params{message} || 
64         error gettext("missing parameter"). "message";
66     my $template= 
67         template("email.tmpl") || error gettext("missing template");
68     
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();
77     return $output;
78 }
80 ### Utilities
82 # From Arpit Jain
83 # http://ikiwiki.info/todo/Bestdir_along_with_bestlink_in_IkiWiki.pm/
84 # need to clarify license
85 sub bestdir ($$) { #{{{
86     my $page=shift;
87        my $link=shift;
88        my $cwd=$page;
90        if ($link=~s/^\/+//) {
91                $cwd="";
92        }
94        do {
95                my $l=$cwd;
96                $l.="/" if length $l;
97                $l.=$link;
98                if (-d "$config{srcdir}/$l") {
99                        return $l;
100                }
101        } while $cwd=~s!/?[^/]+$!!;
103        if (length $config{userdir}) {
104                my $l = "$config{userdir}/".lc($link);
106                if (-d $l) {
107                        return $l;
108                }
109        }
111        return "";
112 } #}}}
116 sub fill_template(@){
117     my  %params=@_;
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}}),
127                                               $params{$param}));
128         }
129         if ($template->query(name => "raw_$param")) {
130             $template->param("raw_$param" => $params{$param});
131         }
132     }