|
1
|
#!/usr/bin/tcl |
|
2
|
# |
|
3
|
# Monitor the database file named on the command line for |
|
4
|
# incoming email messages. Print the "To:" line of each |
|
5
|
# email on standard output as it is received. |
|
6
|
# |
|
7
|
# It should be relatively easy to modify this scribe to actually |
|
8
|
# deliver the emails to a real email transfer agent such as |
|
9
|
# Postfix. |
|
10
|
# |
|
11
|
# For long-term use, set the polling interval to something |
|
12
|
# greater than the default 100 milliseconds. Polling once |
|
13
|
# every 10 seconds is probably sufficient. |
|
14
|
# |
|
15
|
set POLLING_INTERVAL 100 ;# milliseconds |
|
16
|
|
|
17
|
set dbfile [lindex $argv 0] |
|
18
|
if {[llength $argv]!=1} { |
|
19
|
puts stderr "Usage: $argv0 DBFILE" |
|
20
|
exit 1 |
|
21
|
} |
|
22
|
package require sqlite3 |
|
23
|
puts "SQLite version [sqlite3 -version]" |
|
24
|
sqlite3 db $dbfile |
|
25
|
db timeout 2000 |
|
26
|
catch {db eval {PRAGMA journal_mode=WAL}} |
|
27
|
db eval { |
|
28
|
CREATE TABLE IF NOT EXISTS email( |
|
29
|
emailid INTEGER PRIMARY KEY, |
|
30
|
msg TXT |
|
31
|
); |
|
32
|
} |
|
33
|
while {1} { |
|
34
|
db transaction immediate { |
|
35
|
set n 0 |
|
36
|
db eval {SELECT msg FROM email} { |
|
37
|
set email ??? |
|
38
|
regexp {To: \S*} $msg to |
|
39
|
puts "$to ([string length $msg] bytes)" |
|
40
|
incr n |
|
41
|
} |
|
42
|
if {$n>0} { |
|
43
|
db eval {DELETE FROM email} |
|
44
|
} |
|
45
|
# Hold the write lock a little longer in order to exercise |
|
46
|
# the SQLITE_BUSY handling logic on the writing inside of |
|
47
|
# Fossil. Probably comment-out this line for production use. |
|
48
|
after 100 |
|
49
|
} |
|
50
|
after $POLLING_INTERVAL |
|
51
|
} |
|
52
|
|