Fossil SCM

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

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button