GREYLIST_ENABLED_LOG		= yes
GREYLIST_INITIAL_DELAY		= 5 MINUTES
GREYLIST_INITIAL_LIFETIME	= 4 HOURS
GREYLIST_WHITE_LIFETIME		= 36 DAY
GREYLIST_BOUNCE_LIFETIME	= 7 DAY
GREYLIST_RECORD_LIFETIME	= 3 DAY
GREYLIST_LOG_LIFETIME		= 1 MONTH
GREYLIST_TABLE			= greylist
GREYLIST_LOG_TABLE		= greylist_log

GREYLIST_TEST = SELECT CASE WHEN now() > block_expires THEN 'accepted' \
	ELSE 'deferred' END AS result, id FROM GREYLIST_TABLE \
    WHERE (record_expires > now() OR manual) AND \
	    ${if def:sender_address_domain{NOT}{}} bounce AND \
	sender = lower('${quote_pgsql:$sender_address_domain}') AND \
	recipient = lower('${quote_pgsql:$domain}') AND \
	relay_ip >> inet '${quote_pgsql:$sender_host_address}' \
    ORDER BY result DESC LIMIT 1

GREYLIST_ADD = INSERT INTO \
	GREYLIST_TABLE (relay_ip, bounce, sender, recipient, \
	    block_expires, record_expires) \
    VALUES ('${quote_pgsql:${mask:$sender_host_address/24}}', \
	${if def:sender_address_domain{FALSE}{TRUE}}, \
	lower('${quote_pgsql:$sender_address_domain}'), \
	lower('${quote_pgsql:$domain}'), \
	now() + INTERVAL 'GREYLIST_INITIAL_DELAY', \
	now() + INTERVAL 'GREYLIST_INITIAL_LIFETIME')

GREYLIST_DEFER_HIT = UPDATE GREYLIST_TABLE \
	SET blockcount = blockcount + 1, last_block = now() WHERE id = $acl_m9

GREYLIST_OK_COUNT = UPDATE GREYLIST_TABLE \
	SET passcount = passcount + 1, last_pass = now() WHERE id = $acl_m9

GREYLIST_OK_NEWTIME = UPDATE GREYLIST_TABLE SET \
	record_expires = now() + INTERVAL 'GREYLIST_WHITE_LIFETIME' \
	WHERE id = $acl_m9 AND NOT MANUAL

GREYLIST_OK_BOUNCE = UPDATE GREYLIST_TABLE SET \
	record_expires = now() + INTERVAL 'GREYLIST_BOUNCE_LIFETIME' \
	WHERE id = $acl_m9 AND NOT MANUAL

GREYLIST_CLEAN = DELETE FROM GREYLIST_TABLE WHERE \
	record_expires < now() AND NOT manual

GREYLIST_LOG_CLEAN = DELETE FROM GREYLIST_LOG_TABLE WHERE \
	date < now() - INTERVAL 'GREYLIST_LOG_LIFETIME'

GREYLIST_LOG = INSERT INTO GREYLIST_LOG_TABLE \
	(relay_ip, sender, recipient, deferred, id) \
     VALUES ('${quote_pgsql:$sender_host_address}', \
	lower('${quote_pgsql:$sender_address_domain}'), \
	lower('${quote_pgsql:$domain}'), \
	${if eq{$acl_m8}{accepted}{FALSE}{TRUE}}, \
	CASE WHEN $acl_m9 < 0 THEN NULL ELSE $acl_m9 END)