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;
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");
37 my $page=$params{page};
39 my $linktext=$config{url}.'/mailbox.css';
41 push @{$metaheaders{$page}},
42 '<link rel="stylesheet" href="'.$linktext.'" type="text/css"/>'
45 sub preprocess (@) { #{{{
48 my $page=$params{page};
49 my $type=$params{type} || 'Maildir';
51 my $path=$params{path} || error gettext("missing parameter") . " path";
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;
61 return format_mailbox(%params);
68 my $path=$config{srcdir} . '/' . $params{page}.".mbox";
69 return format_mailbox(path=>$path,type=>'Mbox',destpage=>$params{page});
72 ### The guts of the plugin
74 sub format_mailbox(@){
76 my $path=$params{path} || error gettext("missing parameter "). 'path';
77 my $type=$params{type} || error gettext("missing paramater ")."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);
85 my @roots= sort { str2time($a->header('Date')) <=>
86 str2time($b->header('Date'))} ($threader->rootset);
88 return join "\n", map { format_thread(%params,thread=>$_) } @roots;
95 my $thread=$params{thread} || error gettext("missing parameter") . "thread";
99 if ($thread->message) {
100 $output .= format_message(%params,message=>$thread->message);
102 $output .= sprintf gettext("Message %s not available"), $thread->messageid;
106 $output .= '<div class="emailthreadindent">' .
107 format_thread(%params,thread=>$thread->child).
112 $output .= format_thread(%params,thread=>$thread->next);
120 my $val=$message->header($_);
122 $val = escapeHTML($val);
124 my $hash={'HEADERNAME'=>$name,'VAL'=>$val};
127 sub format_message(@){
130 my $message=$params{message} ||
131 error gettext("missing parameter"). "message";
134 my $dest=$params{destpage} ||
135 error gettext("missing parameter"). "destpage";
137 my $keep_headers=$params{headers} || qr/^(subject|from|date)[:]?$/i;
140 template("email.tmpl") || error gettext("missing template");
144 my @names = grep {m/$keep_headers/;} ($message->header_names);
145 my @headers=map { make_pair($message,$_) } @names;
148 $template->param(HEADERS=>[@headers]);
150 my $allowed_attachments=$params{allowed_attachments} ||
151 "maxsize(100kb) and mimetype(text/plain)";
153 my @parts=$message->parts;
157 #this sucks. But someone would need to modify filecheck to
158 #accept a blob of content. Or maybe hacking with IO::Scalar
159 my $tmpfile=File::Temp->new();
161 binmode $tmpfile,':utf8';
162 print $tmpfile $_->body();
164 my $allowed=pagespec_match($dest, $allowed_attachments, file=>$tmpfile);
167 debug("clobbering attachment $partcount");
168 $_->content_type_set('text/plain');
169 $_->body_set("[ omitting part $partcount: $allowed ]");
174 my $body= join("\n", map { $_->body } @parts);
176 $template->param(body=>format_body($body));
178 $output .= $template->output();
185 # it is not completely clear to me the right way to go here.
186 # passing things straight to markdown is not working all that
188 return "<pre>".escapeHTML($body)."</pre>";
192 # based on bestdir From Arpit Jain
193 # http://ikiwiki.info/todo/Bestdir_along_with_bestlink_in_IkiWiki.pm/
194 # need to clarify license
195 sub bestpath ($$) { #{{{
200 if ($link=~s/^\/+//) {
206 $l.="/" if length $l;
208 if (-d "$config{srcdir}/$l" || -f "$config{srcdir}/$l") {
211 } while $cwd=~s!/?[^/]+$!!;
213 if (length $config{userdir}) {
214 my $l = "$config{userdir}/".lc($link);
216 if (-d $l || -f $l) {
224 sub pagetemplate (@) { #{{{
226 my $page=$params{page};
227 my $destpage=$params{destpage};
228 my $template=$params{template};
230 if (exists $metaheaders{$page} && $template->query(name => "meta")) {
231 # avoid duplicate meta lines
233 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
239 package Email::FolderType::MH;
243 return 0 if (! -d $folder);
244 opendir DIR,$folder || error("opendir failed");
247 return 0 if (!m|\.| && !m|\.\.| && !m|\d+|);