1 38232a0a 2023-05-07 op #!/usr/bin/env perl
3 38232a0a 2023-05-07 op # smingest was written by Omar Polo <op@openbsd.org> and is placed in the
4 38232a0a 2023-05-07 op # public domain. The author hereby disclaims copyright to this source
12 38232a0a 2023-05-07 op use Date::Parse;
13 38232a0a 2023-05-07 op use File::Basename;
15 38232a0a 2023-05-07 op die "usage: $0 dbpath\n" if @ARGV != 1;
16 38232a0a 2023-05-07 op my $dbpath = shift @ARGV;
18 e92cf939 2023-05-07 op open(my $sqlite, "|-", "sqlite3", $dbpath) or die "can't spawn sqlite3";
20 7b8263f5 2023-05-07 op if (`uname` =~ "OpenBSD") {
21 7b8263f5 2023-05-07 op use OpenBSD::Pledge;
22 7b8263f5 2023-05-07 op use OpenBSD::Unveil;
24 7b8263f5 2023-05-07 op unveil("/usr/local/bin/mshow", "rx") or die "unveil mshow: $!";
25 7b8263f5 2023-05-07 op pledge("stdio proc exec") or die "pledge: $!";
28 38232a0a 2023-05-07 op say $sqlite ".import --csv /dev/stdin email"
29 38232a0a 2023-05-07 op or die "can't speak to sqlite: $!";
34 e92cf939 2023-05-07 op open(my $fh, "-|", "mshow", "-Atext/plain", "-NF", $_)
35 38232a0a 2023-05-07 op or die "can't run mshow $_: $!";
37 38232a0a 2023-05-07 op my ($time, $id) = split /\./, basename $_;
38 38232a0a 2023-05-07 op my $mid = "$time.$id";
39 38232a0a 2023-05-07 op $mid =~ s/"/""/g;
41 38232a0a 2023-05-07 op my ($from, $subj, $date) = ('', '', undef);
42 38232a0a 2023-05-07 op while (<$fh>) {
46 38232a0a 2023-05-07 op $from = s/.*?: //r if /^From:/;
47 38232a0a 2023-05-07 op $subj = s/.*?: //r if /^Subject:/;
48 38232a0a 2023-05-07 op $date = str2time(s/.*?: //r) if /^Date:/;
50 38232a0a 2023-05-07 op $date //= time;
51 38232a0a 2023-05-07 op $from =~ s/ +<.*>//;
53 38232a0a 2023-05-07 op print $sqlite "\"$mid\",\"$from\",\"$date\",\"$subj\",\"";
54 38232a0a 2023-05-07 op while (<$fh>) {
56 38232a0a 2023-05-07 op print $sqlite $_;
58 c59125c8 2023-07-01 op print $sqlite "\"\r\n";
63 38232a0a 2023-05-07 op close $sqlite;
64 38232a0a 2023-05-07 op die "sqlite3 exited with $?\n" unless $? == 0;