2023-06-18 02:37:25 +00:00
|
|
|
# vim: set et nosta sw=4 ts=4 :
|
|
|
|
|
#
|
2023-06-18 17:04:02 +00:00
|
|
|
# A class that represents an individual Maildir, and a Nessage class to nanage
|
|
|
|
|
# files underneath them.
|
2023-06-18 02:37:25 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
|
|
#############################################################
|
|
|
|
|
# I M P O R T S
|
|
|
|
|
#############################################################
|
|
|
|
|
|
|
|
|
|
import
|
|
|
|
|
std/os,
|
2023-06-18 17:04:02 +00:00
|
|
|
std/osproc,
|
|
|
|
|
std/posix,
|
2023-06-18 02:37:25 +00:00
|
|
|
std/streams,
|
|
|
|
|
std/strformat,
|
|
|
|
|
std/times
|
|
|
|
|
|
|
|
|
|
import
|
|
|
|
|
util
|
|
|
|
|
|
|
|
|
|
|
2023-06-18 17:04:02 +00:00
|
|
|
#############################################################
|
|
|
|
|
# C O N S T A N T S
|
|
|
|
|
#############################################################
|
|
|
|
|
|
|
|
|
|
const
|
|
|
|
|
OWNERDIRPERMS = { fpUserExec, fpUserWrite, fpUserRead }
|
|
|
|
|
OWNERFILEPERMS = { fpUserWrite, fpUserRead }
|
2023-06-20 11:26:41 +00:00
|
|
|
# FILTERPROCOPTS = { poUsePath }
|
|
|
|
|
FILTERPROCOPTS = { poUsePath, poEvalCommand }
|
2023-06-18 17:04:02 +00:00
|
|
|
BUFSIZE = 8192 # reading and writing buffer size
|
|
|
|
|
|
|
|
|
|
|
2023-06-18 02:37:25 +00:00
|
|
|
#############################################################
|
|
|
|
|
# T Y P E S
|
|
|
|
|
#############################################################
|
|
|
|
|
|
|
|
|
|
# A Maildir object.
|
|
|
|
|
#
|
|
|
|
|
type Maildir* = ref object
|
|
|
|
|
path*: string # Absolute path to the encapsualting dir
|
|
|
|
|
cur: string
|
|
|
|
|
new: string
|
|
|
|
|
tmp: string
|
|
|
|
|
|
|
|
|
|
# An email message, under a specific Maildir.
|
|
|
|
|
#
|
|
|
|
|
type Message* = ref object
|
2023-06-18 17:04:02 +00:00
|
|
|
basename: string
|
|
|
|
|
dir: Maildir
|
|
|
|
|
headers: seq[ tuple[ header: string, value: seq[string] ] ]
|
|
|
|
|
path: string
|
|
|
|
|
stream: FileStream
|
2023-06-18 02:37:25 +00:00
|
|
|
|
|
|
|
|
|
2023-06-20 11:26:41 +00:00
|
|
|
# Count messages generated during a run.
|
|
|
|
|
var msgcount = 0
|
|
|
|
|
|
|
|
|
|
|
2023-06-18 02:37:25 +00:00
|
|
|
#############################################################
|
|
|
|
|
# M E T H O D S
|
|
|
|
|
#############################################################
|
|
|
|
|
|
2023-06-20 11:26:41 +00:00
|
|
|
#------------------------------------------------------------
|
|
|
|
|
# Maildir
|
|
|
|
|
#------------------------------------------------------------
|
|
|
|
|
|
2023-06-18 02:37:25 +00:00
|
|
|
proc newMaildir*( path: string ): Maildir =
|
|
|
|
|
## Create and return a new Maildir object, making it on-disk if necessary.
|
|
|
|
|
result = new Maildir
|
|
|
|
|
result.path = path
|
2023-06-20 11:26:41 +00:00
|
|
|
result.cur = joinPath( path, "cur" )
|
|
|
|
|
result.new = joinPath( path, "new" )
|
|
|
|
|
result.tmp = joinPath( path, "tmp" )
|
2023-06-18 02:37:25 +00:00
|
|
|
|
|
|
|
|
if not dirExists( path ):
|
2023-06-20 11:26:41 +00:00
|
|
|
debug "Creating new maildir at {path}".fmt
|
2023-06-18 02:37:25 +00:00
|
|
|
try:
|
|
|
|
|
for p in [ result.path, result.cur, result.new, result.tmp ]:
|
|
|
|
|
p.createDir
|
2023-06-18 17:04:02 +00:00
|
|
|
p.setFilePermissions( OWNERDIRPERMS )
|
2023-06-18 02:37:25 +00:00
|
|
|
|
|
|
|
|
except CatchableError as err:
|
|
|
|
|
deferral "Unable to create Maildir: ({err.msg}), deferring delivery.".fmt
|
|
|
|
|
|
|
|
|
|
|
2023-06-18 17:04:02 +00:00
|
|
|
proc subDir*( dir: Maildir, path: string ): Maildir =
|
|
|
|
|
## Creates a new Maildir relative to an existing one.
|
|
|
|
|
result = newMaildir( dir.path & "/" & path )
|
|
|
|
|
|
|
|
|
|
|
2023-06-20 11:26:41 +00:00
|
|
|
#------------------------------------------------------------
|
|
|
|
|
# Message
|
|
|
|
|
#------------------------------------------------------------
|
|
|
|
|
|
2023-06-18 02:37:25 +00:00
|
|
|
proc newMessage*( dir: Maildir ): Message =
|
|
|
|
|
## Create and return a Message - an open FileStream under a specific Maildir
|
|
|
|
|
## (in tmp)
|
|
|
|
|
result = new Message
|
|
|
|
|
|
|
|
|
|
let now = getTime()
|
2023-06-18 17:04:02 +00:00
|
|
|
var hostname = newString(256)
|
|
|
|
|
discard getHostname( cstring(hostname), cint(256) )
|
2023-06-18 02:37:25 +00:00
|
|
|
|
2023-06-20 11:26:41 +00:00
|
|
|
msgcount = msgcount + 1
|
2023-06-18 17:04:02 +00:00
|
|
|
result.dir = dir
|
2023-06-20 11:26:41 +00:00
|
|
|
result.basename = $now.toUnixFloat & '.' & $getCurrentProcessID() & '.' & $msgcount & '.' & $hostname
|
|
|
|
|
result.path = joinPath( result.dir.tmp, result.basename )
|
2023-06-18 17:04:02 +00:00
|
|
|
result.headers = @[]
|
|
|
|
|
|
|
|
|
|
try:
|
2023-06-20 11:26:41 +00:00
|
|
|
debug "Opening new message at {result.path}".fmt
|
2023-06-18 17:04:02 +00:00
|
|
|
result.stream = openFileStream( result.path, fmWrite )
|
|
|
|
|
result.path.setFilePermissions( OWNERFILEPERMS )
|
|
|
|
|
except CatchableError as err:
|
2023-06-20 11:26:41 +00:00
|
|
|
deferral "Unable to write file {result.path} {err.msg}".fmt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc open*( msg: Message ) =
|
|
|
|
|
## Open (or re-open) a Message file stream.
|
|
|
|
|
msg.stream = msg.path.openFileStream
|
2023-06-18 17:04:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
proc save*( msg: Message, dir=msg.dir ) =
|
|
|
|
|
## Move the message from tmp to new. Defaults to its current
|
|
|
|
|
## maildir, but can be provided a different one.
|
2023-06-20 11:26:41 +00:00
|
|
|
msg.stream.close
|
|
|
|
|
let newpath = joinPath( dir.new, msg.basename )
|
|
|
|
|
debug "Delivering message to {newpath}".fmt
|
2023-06-18 17:04:02 +00:00
|
|
|
msg.path.moveFile( newpath )
|
|
|
|
|
msg.dir = dir
|
|
|
|
|
msg.path = newpath
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc delete*( msg: Message ) =
|
|
|
|
|
## Remove a message from disk.
|
2023-06-20 11:26:41 +00:00
|
|
|
msg.stream.close
|
|
|
|
|
debug "Removing message at {msg.path}".fmt
|
2023-06-18 17:04:02 +00:00
|
|
|
msg.path.removeFile
|
|
|
|
|
msg.path = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc writeStdin*( msg: Message ) =
|
|
|
|
|
## Streams stdin to the message file, returning how
|
|
|
|
|
## many bytes were written.
|
2023-06-20 11:26:41 +00:00
|
|
|
let input = stdin.newFileStream
|
2023-06-18 17:04:02 +00:00
|
|
|
var buf = input.readStr( BUFSIZE )
|
|
|
|
|
var total = buf.len
|
|
|
|
|
msg.stream.write( buf )
|
|
|
|
|
|
|
|
|
|
while buf != "" and buf.len == BUFSIZE:
|
|
|
|
|
buf = input.readStr( BUFSIZE )
|
|
|
|
|
total = total + buf.len
|
|
|
|
|
msg.stream.write( buf )
|
2023-06-20 11:26:41 +00:00
|
|
|
msg.stream.flush
|
|
|
|
|
msg.stream.close
|
2023-06-18 17:04:02 +00:00
|
|
|
debug "Wrote {total} bytes from stdin".fmt
|
|
|
|
|
|
|
|
|
|
|
2023-06-20 11:26:41 +00:00
|
|
|
proc filter*( orig_msg: Message, cmd: string ): Message =
|
|
|
|
|
## Filter message content through an external program,
|
|
|
|
|
## returning a new Message if successful.
|
|
|
|
|
try:
|
|
|
|
|
var buf: string
|
|
|
|
|
|
|
|
|
|
# let command = cmd.split
|
|
|
|
|
# let process = command[0].startProcess(
|
|
|
|
|
# args = command[1..(command.len-1)],
|
|
|
|
|
# options = FILTERPROCOPTS
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
let process = cmd.startProcess( options = FILTERPROCOPTS )
|
|
|
|
|
|
|
|
|
|
# Read from the original message, write to the filter
|
|
|
|
|
# process in chunks.
|
|
|
|
|
#
|
|
|
|
|
orig_msg.open
|
|
|
|
|
buf = orig_msg.stream.readStr( BUFSIZE )
|
|
|
|
|
process.inputStream.write( buf )
|
|
|
|
|
process.inputStream.flush
|
|
|
|
|
while buf != "" and buf.len == BUFSIZE:
|
|
|
|
|
buf = orig_msg.stream.readStr( BUFSIZE )
|
|
|
|
|
process.inputStream.write( buf )
|
|
|
|
|
process.inputStream.flush
|
|
|
|
|
|
|
|
|
|
# Read from the filter process until EOF, send to the
|
|
|
|
|
# new message in chunks.
|
|
|
|
|
process.inputStream.close
|
|
|
|
|
let new_msg = newMessage( orig_msg.dir )
|
|
|
|
|
buf = process.outputStream.readStr( BUFSIZE )
|
|
|
|
|
new_msg.stream.write( buf )
|
|
|
|
|
new_msg.stream.flush
|
|
|
|
|
while buf != "" and buf.len == BUFSIZE:
|
|
|
|
|
buf = process.outputStream.readStr( BUFSIZE )
|
|
|
|
|
new_msg.stream.write( buf )
|
|
|
|
|
new_msg.stream.flush
|
|
|
|
|
|
|
|
|
|
let exitcode = process.waitForExit
|
|
|
|
|
debug "Filter exited: {exitcode}".fmt
|
|
|
|
|
process.close
|
|
|
|
|
orig_msg.delete
|
|
|
|
|
result = new_msg
|
|
|
|
|
|
|
|
|
|
except OSError as err:
|
|
|
|
|
debug "Unable to filter message: {err.msg}".fmt
|
|
|
|
|
result = orig_msg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-06-18 17:04:02 +00:00
|
|
|
# FIXME: header parsing to tuples
|
|
|
|
|
# - open file
|
|
|
|
|
# - skip lines that don't match headers
|
|
|
|
|
# - unwrap multiline headers
|
|
|
|
|
# - store header, add value to seq of strings
|
2023-06-18 02:37:25 +00:00
|
|
|
|
2023-06-20 11:26:41 +00:00
|
|
|
|