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