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 die "usage: $0 dbpath\n" if @ARGV != 1;
16 my $dbpath = shift @ARGV;
18 open(my $sqlite, "|-", "/usr/local/bin/sqlite3", $dbpath)
19 or die "can't spawn sqlite3";
21 if (`uname` =~ "OpenBSD") {
22 use OpenBSD::Pledge;
23 use OpenBSD::Unveil;
25 unveil("/usr/local/bin/mshow", "rx") or die "unveil mshow: $!";
26 pledge("stdio proc exec") or die "pledge: $!";
27 }
29 say $sqlite ".import --csv /dev/stdin email"
30 or die "can't speak to sqlite: $!";
32 while (<>) {
33 chomp;
35 open(my $fh, "-|", "/usr/local/bin/mshow", "-Atext/plain", "-NF", $_)
36 or die "can't run mshow $_: $!";
38 my $f = $_;
39 my ($time, $id) = split /\./, basename $_;
40 my $mid = "$time.$id";
41 $mid =~ s/"/""/g;
43 my ($from, $subj, $date) = ('', '', undef);
44 while (<$fh>) {
45 chomp;
46 last if /^$/;
47 s/"/""/g;
48 $from = s/.*?: //r if /^From:/;
49 $subj = s/.*?: //r if /^Subject:/;
50 $date = str2time(s/.*?: //r) if /^Date:/;
51 }
52 $date //= time;
53 $from =~ s/ +<.*>//;
55 # leave open for the body
56 print $sqlite "\"$mid\",\"$from\",\"$date\",\"$subj\",\"";
58 while (<$fh>) {
59 s/"/""/g;
60 print $sqlite $_;
61 }
62 say $sqlite '"';
64 close $fh;
65 }
67 close $sqlite;
68 die "sqlite3 exited with $?\n" unless $? == 0;