Last active
May 6, 2020 16:55
-
-
Save Xunnamius/db1d13df511a846e909c37f47e432c25 to your computer and use it in GitHub Desktop.
Epic SQL: DIGEST_FORWARDING (used in my Postfix instances)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP FUNCTION DIGEST_FORWARDING; | |
DELIMITER // | |
CREATE FUNCTION DIGEST_FORWARDING (s VARCHAR(80)) | |
RETURNS TEXT | |
DETERMINISTIC | |
LANGUAGE SQL | |
CONTAINS SQL | |
SQL SECURITY INVOKER | |
COMMENT 'Takes an email address, potentially an alias, and returns a real mailbox' | |
BEGIN | |
DECLARE addr TEXT; | |
SET addr = NULL; | |
SELECT (CASE WHEN f.allow_wildcard_goto = 0 THEN f.goto WHEN f.allow_wildcard_goto != 0 THEN REPLACE(REPLACE(f.goto, '%@', SUBSTRING(s, 1, LOCATE('@', s))), '@%', SUBSTRING(s, LOCATE('@', s))) END) INTO addr FROM forwardings f WHERE (f.goto != s OR f.recursive != 0) AND s LIKE f.address AND (f.allow_wildcard_goto != 0 OR f.allow_restricted_goto != 0 OR 1 IN (SELECT 1 FROM domains d JOIN mailboxes m WHERE f.goto = CONCAT(m.email, '@', d.domain) AND d.active + m.active + f.active = 3)) ORDER BY f.priority DESC LIMIT 1; | |
RETURN addr; | |
END// | |
DELIMITER ; | |
SELECT DIGEST_FORWARDING('[email protected]'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment