]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/mailbox.pm
factor out make_pair for debugging
[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 } # }}}
18 sub preprocess (@) { #{{{
19         my %params=@_;
20         
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);
34 } # }}}
37 ### The guts of the plugin
38 ### parameters 
39 sub format_mailbox(@){
40     my %params=@_;
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;
47 }
49 sub make_pair($$){
50     my $message=shift;
51     my $name=shift;
52     my $val=$message->header($_);
53     my $hash={'HEADERNAME'=>$name,'VAL'=>$val};
54     return $hash;
55 }
56 sub format_message(@){
57     my  %params=@_;
59     my $message=$params{message} || 
60         error gettext("missing parameter"). "message";
62     my $template= 
63         template("email.tmpl") || error gettext("missing template");
64     
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();
73     return $output;
74 }
76 ### Utilities
78 # From Arpit Jain
79 # http://ikiwiki.info/todo/Bestdir_along_with_bestlink_in_IkiWiki.pm/
80 # need to clarify license
81 sub bestdir ($$) { #{{{
82     my $page=shift;
83        my $link=shift;
84        my $cwd=$page;
86        if ($link=~s/^\/+//) {
87                $cwd="";
88        }
90        do {
91                my $l=$cwd;
92                $l.="/" if length $l;
93                $l.=$link;
94                if (-d "$config{srcdir}/$l") {
95                        return $l;
96                }
97        } while $cwd=~s!/?[^/]+$!!;
99        if (length $config{userdir}) {
100                my $l = "$config{userdir}/".lc($link);
102                if (-d $l) {
103                        return $l;
104                }
105        }
107        return "";
108 } #}}}
112 sub fill_template(@){
113     my  %params=@_;
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}}),
123                                               $params{$param}));
124         }
125         if ($template->query(name => "raw_$param")) {
126             $template->param("raw_$param" => $params{$param});
127         }
128     }