Skip to content

Instantly share code, notes, and snippets.

@z-image
Created February 9, 2025 09:10
Show Gist options
  • Save z-image/4e29da1f01e040da80f2edd1c7e63b4f to your computer and use it in GitHub Desktop.
Save z-image/4e29da1f01e040da80f2edd1c7e63b4f to your computer and use it in GitHub Desktop.
function vdelivermail(..., env_vars):
// get_arguments(): Parse EXT, split at *first* hyphen
EXT = env_vars["EXT"] // e.g., "no-reply-office-sofia@domain.com"
TheUser = EXT
TheUserExt, TheExt = split_at_first_hyphen(EXT) // "no", "reply-office-sofia"
replace_all(TheExt, ".", ":")
// Main Logic: Lookup user (full name first, then base name)
if not vauth_getpw(TheUser, ...): // Try full username "no-reply-office-sofia"
if QMAIL_EXT and TheUser != TheUserExt:
if not vauth_getpw(TheUserExt, ...): // Try base username "no"
usernotfound()
exit
else:
usernotfound()
exit
checkuser()
exit()
function checkuser():
//...
check_forward_deliver(vpw.pw_dir)
//...
function check_forward_deliver(dir):
// check_forward_deliver(): Recursive .qmail lookup
change_dir(dir)
if TheExt: // Extension exists?
if exists(".qmail-" + TheExt): // Try full extension ".qmail-reply-office-sofia"
process(".qmail-" + TheExt)
return 1
for i = length(TheExt) - 1 downto 0: // Loop backwards
if i == 0 or TheExt[i-1] == '-': // Hyphen or start?
prefix = substring(TheExt, 0, i - 1)
if exists(".qmail-" + prefix + "-default"):
process(".qmail-" + prefix + "-default")
return 1
if exists(".qmail"): // Try standard .qmail
process(".qmail")
return 1
return -1 // No .qmail file
// Helper functions (simplified names)
function split_at_first_hyphen(str):
// Returns (prefix, suffix) split at first '-', or (str, "") if no '-'
index = find_first(str, '-')
if index != -1:
return (substring(str, 0, index - 1), substring(str, index + 1))
return (str, "")
@z-image
Copy link
Author

z-image commented Feb 9, 2025

graph TD
    subgraph get_arguments
        A["Start: vdelivermail receives email: no-reply-office-sofia\@domain.com"] --> B["Get EXT and HOST"]
        B --> C{"EXT contains '-'?"}
        C -->|Yes| D["Split EXT at *first* '-' - TheUser = no-reply-office-sofia"]
        D --> E["TheUserExt = no"]
        E --> F["TheExt = reply-office-sofia"]
        F --> G["Replace '.' with ':' in TheExt"]
        C -->|No| H["TheUserExt = EXT"]
        H --> I["TheExt = ''"]
        G --> J["Continue"]
        I --> J
    end
    J --> K{"User no-reply-office-sofia found?"}
    K -->|No| M{"QMAIL_EXT & no-reply-office-sofia != no?"}
    M -->|Yes| N{"vauth_getpw?"}
    N -->|Yes| L["checkuser() TheUserExt=no"]
    N -->|No| O["usernotfound()"]
    M -->|No| O
    L --> P["check_forward_deliver"]
    subgraph check_forward_deliver
        P --> Q{"TheExt (reply-office-sofia) is not empty?"}
        Q -->|Yes| R["Try .qmail-reply-office-sofia"]
        R --> S{"Opened?"}
        S -->|Yes| T1["Process .qmail-reply-office-sofia"]
        S -->|No| U["Loop backwards through TheExt"]
        U --> V{"Current/Previous char is '-'?"}
        V -->|Yes, i=16| W1["Try .qmail-reply-office-default"]
        W1 --> X1{"Opened?"}
        X1 -->|Yes| T2["Process .qmail-reply-office-default"]
        X1 -->|No| U
        V -->|Yes, i=6| W2["Try .qmail-reply-default"]
        W2 --> X2{"Opened?"}
        X2 -->|Yes| T3["Process .qmail-reply-default"]
        X2 -->|No| U
        V -->|Yes, i=0| W3["Try .qmail-default"]
        W3 --> X3{"Opened?"}
        X3 -->|Yes| T4["Process .qmail-default"]
        X3 -->|No| U
        Q -->|No| Y["Try .qmail"]
        Y --> Z{"Opened?"}
        Z -->|Yes| T5["Process .qmail"]
        Z -->|No| AA["Return -1"]
        T1 --> AB["Return 1"]
        T2 --> AB
        T3 --> AB
        T4 --> AB
        T5 --> AB
    end
Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment