1#!/usr/local/bin/perl -w
2# Copyright (C) 2004,2005,2007 Olly Betts
3#
4# This program is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License as
6# published by the Free Software Foundation; either version 2 of the
7# License, or (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
17# USA
18
19use strict;
20
21if (grep {$_ eq '--help'} @ARGV) {
22    die <<EOT;
23Syntax: $0 [MBOX...]
24
25Run this script with one or more mailbox filenames on the command line (or
26pipe a mailbox in on stdin).  It produces output suitable for feeding to
27scriptindex using the mbox2omega.script index script.  For example:
28
29  $0 *.mbox | scriptindex /path/to/database mbox2omega.script
30
31The index script tells scriptindex how to process the dump file, so you can
32customise that to change how the indexing is done.
33
34Note that this script is mainly intended as a simple example of how you might
35generate scriptindex dump files from a data source, and its handling of mail
36messages is quite primitive - e.g. it doesn't handle MIME or character sets.
37EOT
38}
39
40my $hdr = 1;
41line: while (<>) {
42    if ($hdr) {
43	chomp;
44	while (1) {
45	    if (/^$/) {
46		print "body=\n";
47		$hdr = 0;
48		next line;
49	    }
50	    # Handle continuation lines
51	    my $line = $_;
52	    while (<>) {
53		chomp;
54		last unless /^[ \t]/;
55		$line .= $_;
56	    }
57	    if ($line =~ s/^Message-ID:\s*<?(.*?)>?\s*$/$1/i) {
58		print "id=$line\n" if length $line;
59	    } elsif ($line =~ s/^Subject:\s*(.*?)\s*$/$1/i) {
60		print "title=$line\n" if length $line;
61	    }
62	}
63    }
64
65    if (/^From /) {
66	print "\n";
67	$hdr = 1;
68	next;
69    }
70    if ($_ !~ /^\s+$/) {
71	print "=$_";
72    }
73}
74