Blob


1 #!/usr/bin/env perl
2 #
3 # smingest was written by Omar Polo <op@openbsd.org> and is placed in the
4 # public domain. The author hereby disclaims copyright to this source
5 # code.
7 use strict;
8 use warnings;
9 use v5.32;
10 use utf8;
12 use Date::Parse;
13 use File::Basename;
15 use OpenBSD::Pledge;
16 use OpenBSD::Unveil;
18 die "usage: $0 dbpath\n" if @ARGV != 1;
19 my $dbpath = shift @ARGV;
21 open(my $sqlite, "|-", "/usr/local/bin/sqlite3", $dbpath)
22 or die "can't spawn sqlite3";
24 unveil("/usr/local/bin/mshow", "rx") or die "unveil mshow: $!";
25 pledge("stdio proc exec") or die "pledge: $!";
27 say $sqlite ".import --csv /dev/stdin email"
28 or die "can't speak to sqlite: $!";
30 while (<>) {
31 chomp;
33 open(my $fh, "-|", "/usr/local/bin/mshow", "-Atext/plain", "-NF", $_)
34 or die "can't run mshow $_: $!";
36 my $f = $_;
37 my ($time, $id) = split /\./, basename $_;
38 my $mid = "$time.$id";
39 $mid =~ s/"/""/g;
41 my ($from, $subj, $date) = ('', '', undef);
42 while (<$fh>) {
43 chomp;
44 last if /^$/;
45 s/"/""/g;
46 $from = s/.*?: //r if /^From:/;
47 $subj = s/.*?: //r if /^Subject:/;
48 $date = str2time(s/.*?: //r) if /^Date:/;
49 }
50 $date //= time;
51 $from =~ s/ +<.*>//;
53 # leave open for the body
54 print $sqlite "\"$mid\",\"$from\",\"$date\",\"$subj\",\"";
56 while (<$fh>) {
57 s/"/""/g;
58 print $sqlite $_;
59 }
60 say $sqlite '"';
62 close $fh;
63 }
65 close $sqlite;
66 die "sqlite3 exited with $?\n" unless $? == 0;