]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/mailbox.pm
document some limitations, update 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 use Email::MIME;
8 use Email::MIME::Modifier;
9 package Email::MIMEFolder;
10 use base 'Email::Folder';
11 sub bless_message { return  Email::MIME->new($_[1]) };
14 package IkiWiki::Plugin::mailbox;
16 use Email::FolderType qw(folder_type);
17 use IkiWiki 2.00;
18 use Email::Thread;
19 use CGI 'escapeHTML';
20 use File::Temp qw/tempfile/;
21 use File::MimeInfo::Magic;
24 my %metaheaders;
27 sub import { #{{{
28         hook(type => "preprocess", id => "mailbox", call => \&preprocess);
29         hook(type => "scan", id => "mailbox", call => \&scan);
30         hook(type => "pagetemplate", id=>"mailbox", call => \&pagetemplate);
31         hook(type => "htmlize",id=>"mbox",call => \&mbox_htmlize);
32         IkiWiki::loadplugin("filecheck");
33 } # }}}
35 sub scan(@){
36         my %params=@_;
37         my $page=$params{page};
38         
39         my $linktext=$config{url}.'/mailbox.css';
41         push @{$metaheaders{$page}}, 
42                '<link rel="stylesheet" href="'.$linktext.'" type="text/css"/>'
43 }
45 sub preprocess (@) { #{{{
46         my %params=@_;
47         
48         my $page=$params{page};
49         my $type=$params{type} || 'Maildir';
51         my $path=$params{path} ||  error gettext("missing parameter") . " path";
52         
53         # hmm, this should probably only be inserted once per page.
55         my $dir=bestpath($page,$params{path}) || 
56             error("could not find ".$params{path});
58         $params{path} = $config{srcdir} ."/" . $dir;
59         $params{type} = $type;
60         
61         return  format_mailbox(%params);
63 } # }}}
65 sub mbox_htmlize(@){
66     my %params=@_;
67     
68     my $path=$config{srcdir} . '/' . $params{page}.".mbox";
69     return format_mailbox(path=>$path,type=>'Mbox',destpage=>$params{page});
70 }
72 ### The guts of the plugin
73 ### parameters 
74 sub format_mailbox(@){
75     my %params=@_;
76     my $path=$params{path} || error gettext("missing parameter "). 'path';
77     my $type=$params{type} || error gettext("missing paramater ")."type";
79     debug('type='.$type);
80     my $folder=Email::MIMEFolder->new($path,reader=>'Email::Folder::'.$type) || error("mailbox could not be opened");
81     my $threader=new Email::Thread($folder->messages);
83     $threader->thread();
85     return join "\n", map { format_thread(%params,thread=>$_) } $threader->rootset;
86 }
88 sub format_thread(@){
89     my %params=@_;
90     
91     my $thread=$params{thread} || error gettext("missing parameter") . "thread";
93     my $output="";
95     if ($thread->message) {
96         $output .= format_message(%params,message=>$thread->message);
97     } else {
98         $output .= sprintf gettext("Message %s not available"), $thread->messageid;
99     }
101     if ($thread->child){
102         $output .= '<div class="emailthreadindent">' .
103             format_thread(%params,thread=>$thread->child).
104             '</div>';
105     }
107     if ($thread->next){
108         $output .= format_thread(%params,thread=>$thread->next);
109     }
110     return $output;
113 sub make_pair($$){
114     my $message=shift;
115     my $name=shift;
116     my $val=$message->header($_);
117     
118     $val = escapeHTML($val);
120     my $hash={'HEADERNAME'=>$name,'VAL'=>$val};
121     return $hash;
123 sub format_message(@){
124     my  %params=@_;
126     my $message=$params{message} || 
127         error gettext("missing parameter"). "message";
130     my $dest=$params{destpage} || 
131         error gettext("missing parameter"). "destpage";
133     my $keep_headers=$params{headers} || qr/^(subject|from|date)[:]?$/i;
134     
135     my $template= 
136         template("email.tmpl") || error gettext("missing template");
138     my $output="";
140     my @names = grep  {m/$keep_headers/;}  ($message->header_names);
141     my @headers=map { make_pair($message,$_) } @names;
142     
144     $template->param(HEADERS=>[@headers]);
146     my $allowed_attachments=$params{allowed_attachments} || 
147         "maxsize(100kb) and mimetype(text/plain)";
149     my @parts=$message->parts;
151     my $partcount=1;
152     foreach(@parts){
153         #this sucks. But someone would need to modify filecheck to
154         #accept a blob of content. Or maybe hacking with IO::Scalar
155         my $tmpfile=File::Temp->new();
157         binmode $tmpfile,':utf8';
158         print $tmpfile $_->body();
160         my $allowed=pagespec_match($dest, $allowed_attachments, file=>$tmpfile);
162         if (!$allowed) {
163             debug("clobbering attachment $partcount");
164             $_->content_type_set('text/plain');
165             $_->body_set("[ omitting part $partcount: $allowed ]");
167         }
168         $partcount++;
169     }
170     my $body= join("\n", map { $_->body }  @parts);
172     $template->param(body=>format_body($body));
174     $output .= $template->output();
175     return $output;
178 sub format_body($){
179     my $body=shift;
181     # it is not completely clear to me the right way to go here.  
182     # passing things straight to markdown is not working all that
183     # well.
184     return "<pre>".escapeHTML($body)."</pre>";
186 ### Utilities
188 # based on bestdir From Arpit Jain
189 # http://ikiwiki.info/todo/Bestdir_along_with_bestlink_in_IkiWiki.pm/
190 # need to clarify license
191 sub bestpath ($$) { #{{{
192     my $page=shift;
193        my $link=shift;
194        my $cwd=$page;
196        if ($link=~s/^\/+//) {
197                $cwd="";
198        }
200        do {
201                my $l=$cwd;
202                $l.="/" if length $l;
203                $l.=$link;
204                if (-d "$config{srcdir}/$l" || -f "$config{srcdir}/$l") {
205                        return $l;
206                }
207        } while $cwd=~s!/?[^/]+$!!;
209        if (length $config{userdir}) {
210                my $l = "$config{userdir}/".lc($link);
212                if (-d $l || -f $l) {
213                        return $l;
214                }
215        }
217        return "";
218 } #}}}
220 sub pagetemplate (@) { #{{{
221         my %params=@_;
222         my $page=$params{page};
223         my $destpage=$params{destpage};
224         my $template=$params{template};
226         if (exists $metaheaders{$page} && $template->query(name => "meta")) {
227                 # avoid duplicate meta lines
228                 my %seen;
229                 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
230         }
235 package Email::FolderType::MH;
237 sub match {
238     my $folder = shift;
239     return 0 if (! -d $folder);
240     opendir DIR,$folder || error("opendir failed");
242     while (<DIR>){
243       return 0 if (!m|\.| && !m|\.\.| && !m|\d+|);
244     }
245     return 1;
250 1;