|
1
|
#!/usr/bin/tcl |
|
2
|
# |
|
3
|
# Monitor the database file named by the DBFILE variable |
|
4
|
# looking for email messages sent by Fossil. Forward each |
|
5
|
# to /usr/sbin/sendmail. |
|
6
|
# |
|
7
|
set POLLING_INTERVAL 10000 ;# milliseconds |
|
8
|
set DBFILE /home/www/data/emailqueue.db |
|
9
|
set PIPE "/usr/sbin/sendmail -ti" |
|
10
|
|
|
11
|
package require sqlite3 |
|
12
|
# puts "SQLite version [sqlite3 -version]" |
|
13
|
sqlite3 db $DBFILE |
|
14
|
db timeout 5000 |
|
15
|
catch {db eval {PRAGMA journal_mode=WAL}} |
|
16
|
db eval { |
|
17
|
CREATE TABLE IF NOT EXISTS email( |
|
18
|
emailid INTEGER PRIMARY KEY, |
|
19
|
msg TXT |
|
20
|
); |
|
21
|
CREATE TABLE IF NOT EXISTS sentlog( |
|
22
|
mtime INT, |
|
23
|
xto TEXT, |
|
24
|
xfrom TEXT, |
|
25
|
xsubject TEXT, |
|
26
|
xsize INT |
|
27
|
); |
|
28
|
} |
|
29
|
set ctr 0 |
|
30
|
while {1} { |
|
31
|
set n 0 |
|
32
|
db transaction immediate { |
|
33
|
set emailid 0 |
|
34
|
db eval {SELECT emailid, msg FROM email LIMIT 1} { |
|
35
|
set pipe $PIPE |
|
36
|
set to unk |
|
37
|
set subject none |
|
38
|
set size [string length $msg] |
|
39
|
regexp {To:[^\n]*<([^>]+)>} $msg all to |
|
40
|
regexp {\nSubject:[ ]*([^\r\n]+)} $msg all subject |
|
41
|
set subject [string trim $subject] |
|
42
|
if {[regexp {\nFrom:[^\n]*<([^>]+)>} $msg all from]} { |
|
43
|
append pipe " -f $from" |
|
44
|
} |
|
45
|
set out [open |$pipe w] |
|
46
|
puts -nonewline $out $msg |
|
47
|
flush $out |
|
48
|
close $out |
|
49
|
incr n |
|
50
|
incr ctr |
|
51
|
} |
|
52
|
if {$n>0} { |
|
53
|
db eval {DELETE FROM email WHERE emailid=$emailid} |
|
54
|
db eval {INSERT INTO sentlog(mtime,xto,xfrom,xsubject,xsize) |
|
55
|
VALUES(unixepoch(),$to,$from,$subject,$size)} |
|
56
|
} |
|
57
|
} |
|
58
|
if {$n==0} { |
|
59
|
if {$ctr>100} { |
|
60
|
db eval {DELETE FROM sentlog WHERE mtime<unixepoch('now','-30 days')} |
|
61
|
set ctr 0 |
|
62
|
} |
|
63
|
after $POLLING_INTERVAL |
|
64
|
} |
|
65
|
} |
|
66
|
|