Search code examples
dovecotsieve-language

How to replace a character by another in a variable


I want to know if there is a way to replace a character by another in a variable. For example replacing every dots with underscores in a string variable.


Solution

  • I haven't tried it, but based on the Variables specification, the way I'd try to approach this would be to try to match on the text before and after a dot, and then make new variables based on the matches. Something like:

    set "value" "abc.def";
    if string :matches "${value}" "*.*" {
        set "newvalue" "${1}_${2}
    }
    

    This will, of course, only match on a single period because Sieve doesn't include any looping structures. While there's a regex match option, I'm not aware of any regex replacement Sieve extensions.

    Another approach to complex mail filtering you can do with Dovecot (if you do need loops and have full access to the mail server) is their Dovecot-specific extensions like vnd.dovecot.pipe which allows the mail administrator to define full programs (written in whatever language one wishes) to process mail on its way through.