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 return join "\n", map { format_thread(%params,thread=>$_) } $threader->rootset;
91 my $thread=$params{thread} || error gettext("missing parameter") . "thread";
95 if ($thread->message) {
96 $output .= format_message(%params,message=>$thread->message);
98 $output .= sprintf gettext("Message %s not available"), $thread->messageid;
102 $output .= '<div class="emailthreadindent">' .
103 format_thread(%params,thread=>$thread->child).
108 $output .= format_thread(%params,thread=>$thread->next);
116 my $val=$message->header($_);
118 $val = escapeHTML($val);
120 my $hash={'HEADERNAME'=>$name,'VAL'=>$val};
123 sub format_message(@){
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;
136 template("email.tmpl") || error gettext("missing template");
140 my @names = grep {m/$keep_headers/;} ($message->header_names);
141 my @headers=map { make_pair($message,$_) } @names;
144 $template->param(HEADERS=>[@headers]);
146 my $allowed_attachments=$params{allowed_attachments} ||
147 "maxsize(100kb) and mimetype(text/plain)";
149 my @parts=$message->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);
163 debug("clobbering attachment $partcount");
164 $_->content_type_set('text/plain');
165 $_->body_set("[ omitting part $partcount: $allowed ]");
170 my $body= join("\n", map { $_->body } @parts);
172 $template->param(body=>format_body($body));
174 $output .= $template->output();
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
184 return "<pre>".escapeHTML($body)."</pre>";
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 ($$) { #{{{
196 if ($link=~s/^\/+//) {
202 $l.="/" if length $l;
204 if (-d "$config{srcdir}/$l" || -f "$config{srcdir}/$l") {
207 } while $cwd=~s!/?[^/]+$!!;
209 if (length $config{userdir}) {
210 my $l = "$config{userdir}/".lc($link);
212 if (-d $l || -f $l) {
220 sub pagetemplate (@) { #{{{
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
229 $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}}));
235 package Email::FolderType::MH;
239 return 0 if (! -d $folder);
240 opendir DIR,$folder || error("opendir failed");
243 return 0 if (!m|\.| && !m|\.\.| && !m|\d+|);