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