]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/mailbox.pm
remove htmllink
[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 Email::Thread;
14 use CGI 'escapeHTML';
15 use Data::Dumper;
17 my %metaheaders;
19 sub import { #{{{
20         hook(type => "preprocess", id => "mailbox", call => \&preprocess);
21         hook(type => "scan", id => "mailbox", call => \&scan);
22         hook(type => "pagetemplate", id=>"mailbox", call => \&pagetemplate);
23 } # }}}
25 sub scan(@){
26         my %params=@_;
27         my $page=$params{page};
29         debug('calling scan');
30         push @{$metaheaders{$page}}, 
31                '<link rel="stylesheet" href="mailbox.css" type="text/css"/>'
32 }
34 sub preprocess (@) { #{{{
35         my %params=@_;
36         
37         my $page=$params{page};
38         my $type=$params{type} || 'maildir';
40         my $path=$params{path} ||  error gettext("missing parameter") . " path";
41         
42         # hmm, this should probably only be inserted once per page.
44         # note, mbox is not a directory, needs to be special cased
45         my $dir=bestdir($page,$params{path}) || 
46             error("could not find ".$params{path});
48         $params{path} = $config{srcdir} ."/" . $dir;
50         return  format_mailbox(path=>$dir,%params);
52 } # }}}
55 ### The guts of the plugin
56 ### parameters 
57 sub format_mailbox(@){
58     my %params=@_;
59     my $path=$params{path} || error("path parameter mandatory");
61     my $folder=Email::Folder->new($path) || error("mailbox could not be opened");
62     my $threader=new Email::Thread($folder->messages);
64     $threader->thread();
66     return join "\n", map { format_thread(thread=>$_) } $threader->rootset;
67 }
69 sub format_thread(@){
70     my %params=@_;
71     
72     my $thread=$params{thread} || error gettext("missing parameter") . "thread";
74     my $output="";
76     if ($thread->message) {
77         $output .= format_message(message=>$thread->message);
78     } else {
79         $output .= sprintf gettext("Message %s not available"), $thread;
80     }
82     if ($thread->child){
83         $output .= '<div class="emailthreadindent">' .
84             format_thread(thread=>$thread->child).
85             '</div>';
86     }
88     if ($thread->next){
89         $output .= format_thread(thread=>$thread->next);
90     }
91     return $output;
92 }
94 sub make_pair($$){
95     my $message=shift;
96     my $name=shift;
97     my $val=$message->header($_);
98     
99     $val = escapeHTML($val);
101     my $hash={'HEADERNAME'=>$name,'VAL'=>$val};
102     return $hash;
104 sub format_message(@){
105     my  %params=@_;
107     my $message=$params{message} || 
108         error gettext("missing parameter"). "message";
110     my $keep_headers=$params{headers} || qr/^(subject|from|date)/i;
111     
112     my $template= 
113         template("email.tmpl") || error gettext("missing template");
114     
115     my @names = grep  {m/$keep_headers/;}  ($message->header_names);
116     my @headers=map { make_pair($message,$_) } @names;
117     
119     $template->param(HEADERS=>[@headers]);
120     $template->param(body=>format_body($message->body));
122     my $output=$template->output();
123     return $output;
126 sub format_body($){
127     my $body=shift;
129     # it is not completely clear to me the right way to go here.  
130     # passing things straight to markdown is not working all that
131     # well.
132     return "<pre>".escapeHTML($body)."</pre>";
134 ### Utilities
136 # From Arpit Jain
137 # http://ikiwiki.info/todo/Bestdir_along_with_bestlink_in_IkiWiki.pm/
138 # need to clarify license
139 sub bestdir ($$) { #{{{
140     my $page=shift;
141        my $link=shift;
142        my $cwd=$page;
144        if ($link=~s/^\/+//) {
145                $cwd="";
146        }
148        do {
149                my $l=$cwd;
150                $l.="/" if length $l;
151                $l.=$link;
152                if (-d "$config{srcdir}/$l") {
153                        return $l;
154                }
155        } while $cwd=~s!/?[^/]+$!!;
157        if (length $config{userdir}) {
158                my $l = "$config{userdir}/".lc($link);
160                if (-d $l) {
161                        return $l;
162                }
163        }
165        return "";
166 } #}}}
168 sub pagetemplate (@) { #{{{
169         my %params=@_;
170         my $page=$params{page};
171         my $destpage=$params{destpage};
172         my $template=$params{template};
174         if (exists $metaheaders{$page} && $template->query(name => "meta")) {
175                 # avoid duplicate meta lines
176                 my %seen;
177                 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
178         }
183 1;