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+
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);
20 use File::Temp qw/tempfile/;
21 use File::MimeInfo::Magic;
29 hook(type => "preprocess", id => "mailbox", call => \&preprocess);
30 hook(type => "scan", id => "mailbox", call => \&scan);
31 hook(type => "pagetemplate", id=>"mailbox", call => \&pagetemplate);
32 hook(type => "htmlize",id=>"mbox",call => \&mbox_htmlize);
33 IkiWiki::loadplugin("filecheck");
38 my $page=$params{page};
40 my $linktext=$config{url}.'/mailbox.css';
42 push @{$metaheaders{$page}},
43 '<link rel="stylesheet" href="'.$linktext.'" type="text/css"/>'
46 sub preprocess (@) { #{{{
49 my $page=$params{page};
50 my $type=$params{type} || 'Maildir';
52 my $path=$params{path} || error gettext("missing parameter") . " path";
54 # hmm, this should probably only be inserted once per page.
56 my $dir=bestpath($page,$params{path}) ||
57 error("could not find ".$params{path});
59 $params{path} = $config{srcdir} ."/" . $dir;
60 $params{type} = $type;
62 return format_mailbox(%params);
69 my $path=$config{srcdir} . '/' . $params{page}.".mbox";
70 return format_mailbox(path=>$path,type=>'Mbox',destpage=>$params{page});
73 ### The guts of the plugin
75 sub format_mailbox(@){
77 my $path=$params{path} || error gettext("missing parameter "). 'path';
78 my $type=$params{type} || error gettext("missing paramater ")."type";
81 my $folder=Email::MIMEFolder->new($path,reader=>'Email::Folder::'.$type) || error("mailbox could not be opened");
82 my $threader=new Email::Thread($folder->messages);
86 my @roots= sort { str2time($a->header('Date')) <=>
87 str2time($b->header('Date'))} ($threader->rootset);
89 return join "\n", map { format_thread(%params,thread=>$_) } @roots;
96 my $thread=$params{thread} || error gettext("missing parameter") . "thread";
100 if ($thread->message) {
101 $output .= format_message(%params,message=>$thread->message);
103 $output .= sprintf gettext("Message %s not available"), $thread->messageid;
107 $output .= '<div class="emailthreadindent">' .
108 format_thread(%params,thread=>$thread->child).
113 $output .= format_thread(%params,thread=>$thread->next);
118 sub sanitize_address($$){
121 my $strategy= $config{mailbox_obfuscation_strategy} || "delete";
123 return $val if ($strategy eq "none");
125 if ($hdrname =~ qr/From|To|Reply-To|CC/){
126 my @addrs=Email::Address->parse($val);
127 foreach my $addr (@addrs){
128 if ($strategy eq "rot13"){
129 my $orig=$addr->address;
130 $orig =~ y/A-Za-z/N-ZA-Mn-za-m/;
131 $addr->address($orig);
133 $addr->address(gettext("address deleted"));
136 $val=join(",",map {$_->format;} @addrs);
144 my $val=$message->header($name);
146 $val = sanitize_address($name,$val);
148 $val = escapeHTML($val);
150 my $hash={'HEADERNAME'=>$name,'VAL'=>$val};
153 sub format_message(@){
156 my $message=$params{message} ||
157 error gettext("missing parameter"). "message";
160 my $dest=$params{destpage} ||
161 error gettext("missing parameter"). "destpage";
163 my $keep_headers=$params{headers} || qr/^(subject|from|date)[:]?$/i;
166 template("email.tmpl") || error gettext("missing template");
170 my @names = grep {m/$keep_headers/;} ($message->header_names);
172 my @headers=map { make_pair($message,$_) } @names;
176 $template->param(HEADERS=>[@headers]);
178 my $allowed_attachments=$params{allowed_attachments} ||
179 "maxsize(100kb) and mimetype(text/plain)";
181 my @parts=$message->parts;
185 #this sucks. But someone would need to modify filecheck to
186 #accept a blob of content. Or maybe hacking with IO::Scalar
187 my $tmpfile=File::Temp->new();
189 binmode $tmpfile,':utf8';
190 print $tmpfile $_->body();
192 my $allowed=pagespec_match($dest, $allowed_attachments, file=>$tmpfile);
195 debug("clobbering attachment $partcount");
196 $_->content_type_set('text/plain');
197 $_->body_set("[ omitting part $partcount: $allowed ]");
202 my $body= join("\n", map { $_->body } @parts);
204 $template->param(body=>format_body($body));
206 $output .= $template->output();
213 # it is not completely clear to me the right way to go here.
214 # passing things straight to markdown is not working all that
216 return "<pre>".escapeHTML($body)."</pre>";
220 # based on bestdir From Arpit Jain
221 # http://ikiwiki.info/todo/Bestdir_along_with_bestlink_in_IkiWiki.pm/
222 # need to clarify license
223 sub bestpath ($$) { #{{{
228 if ($link=~s/^\/+//) {
234 $l.="/" if length $l;
236 if (-d "$config{srcdir}/$l" || -f "$config{srcdir}/$l") {
239 } while $cwd=~s!/?[^/]+$!!;
241 if (length $config{userdir}) {
242 my $l = "$config{userdir}/".lc($link);
244 if (-d $l || -f $l) {
252 sub pagetemplate (@) { #{{{
254 my $page=$params{page};
255 my $destpage=$params{destpage};
256 my $template=$params{template};
259 if (defined($config{mailbox_copyright})){
260 $template->param(COPYRIGHT=>$config{mailbox_copyright});
263 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
264 # avoid duplicate meta lines
266 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
272 package Email::FolderType::MH;
276 return 0 if (! -d $folder);
277 opendir DIR,$folder || error("opendir failed");
280 return 0 if (!m|\.| && !m|\.\.| && !m|\d+|);