Skip to content

need help with config.lua please (for loops instead of hardcoded values) #307

@prankousky

Description

@prankousky

Hi everybody,

I am currently using imapfilter with three different email accounts and with a large number of rules.

So instead of hardcoding all the values (which I want/need for all three accounts, anyway) I was trying to create for loops to iterate through them for each account, but it does not work.

I am not a professional programmer, my little lua knowledge is self-tought. I don't understand what I did wrong here, can you please help me fix the configuration?

options.create = true
options.expunge = true
options.subscribe = true
options.timeout = 30

acc0 = IMAP {
    server = 'imap.strato.de',
    username = 'yes',
    password = 'okay',

}

acc1 = IMAP {
    server = 'imap.gmail.com',
    username = 'maybe',
    password = 'notsure',
    ssl = "tls1"
}

acc2 = IMAP {
    server = 'imap.gmail.com',
    username = 'rathernot',
    password = 'danngehdochzunetto',
    ssl = "tls1"
}

results = (
    acc0.INBOX:contain_from('FlyerTalk.com') +
    acc0.INBOX:contain_from('Pinterest') +
    acc0.INBOX:contain_from('[email protected]') +
    acc1.INBOX:contain_from('FlyerTalk.com') +
    acc1.INBOX:contain_from('Pinterest') +
    acc1.INBOX:contain_from('[email protected]') +
    acc2.INBOX:contain_from('FlyerTalk.com') +
    acc2.INBOX:contain_from('Pinterest') +
    acc2.INBOX:contain_from('[email protected]') +
    --
    acc0.INBOX:contain_body("Werbung") +
    acc1.INBOX:contain_body("Werbung") +
    acc2.INBOX:contain_body("Werbung") +
    acc0.INBOX:contain_subject("Wichtig") +
    acc1.INBOX:contain_subject("Wichtig") +
    acc2.INBOX:contain_subject("Wichtig") +
    -- etc. etc.
)


results:delete_messages()

This works fine, but I don't like using the same contain_from, contain_body, etc. for everything manually; while body and subject aren't that big of a deal for me, the contain_from list is huge. This runs on my old email accounts that I don't use any longer, so 98%+ are spam from certain accounts. (I could handle this differently, I guess, but please let us focus on still achieving it with imapfilter and loops if possible)

I then tried this (code first, then error)

options.create = true
options.expunge = true
options.subscribe = true
options.timeout = 30

acc0 = IMAP {
    server = 'imap.strato.de',
    username = 'yes',
    password = 'okay',
}

acc1 = IMAP {
    server = 'imap.gmail.com',
    username = 'maybe',
    password = 'notsure',
    ssl = "tls1"
}

acc2 = IMAP {
    server = 'imap.gmail.com',
    username = 'rathernot',
    password = 'danngehdochzunetto',
    ssl = "tls1"
}


results = {}

my_accounts = {
    "acc0",
    "acc1",
    "acc2"
}

my_contain_body = { "Werbung" }
my_contain_subject = { "Wichtig" }
my_contain_from = {
    "[email protected]",
    "Discord",
    "My Friend Dahmer",
    "[email protected]"
}

for _, account in ipairs(my_accounts) do
    -- FROM
    for value in pairs(my_contain_from) do
        table.insert(results, string.format("%s.INBOX:contain_from('%s')", my_accounts[each], my_contain_from[value]))
    end
    -- BODY
    for value in pairs(my_contain_body) do
        table.insert(results, string.format("%s.INBOX:contain_body('%s')", my_accounts[each], my_contain_body[value]))
    end
    -- SUBJECT
    for value in pairs(my_contain_subject) do
        table.insert(results,
            string.format("%s.INBOX:contain_subject('%s')", my_accounts[each], my_contain_subject[value]))
    end
end


results:delete_messages()

error

imapfilter: config_test.lua:142: attempt to call a nil value (method 'delete_messages')
stack traceback:
        [C]: in method 'delete_messages'
        config_test.lua:142: in main chunk

What I don't understand is that I can run this file on it's own fine; I added these lines below at the end, then ran it via lua test.lua (without the imapfilter related part)

for each in pairs(results) do
    print(results[each])
end

This will return something like

acc0.INBOX:contain_from('[email protected]')
acc0.INBOX:contain_from('Discord')
acc0.INBOX:contain_from('My Friend Dahmer')
acc0.INBOX:contain_from('[email protected]')
acc0.INBOX:contain_body('Werbung')
acc0.INBOX:contain_subject('Wichtig')

acc1.INBOX:contain_from('[email protected]')
acc1.INBOX:contain_from('Discord')
acc1.INBOX:contain_from('My Friend Dahmer')
acc1.INBOX:contain_from('[email protected]')
acc1.INBOX:contain_body('Werbung')
acc1.INBOX:contain_subject('Wichtig')

acc2.INBOX:contain_from('[email protected]')
acc2.INBOX:contain_from('Discord')
acc2.INBOX:contain_from('My Friend Dahmer')
acc2.INBOX:contain_from('[email protected]')
acc2.INBOX:contain_body('Werbung')
acc2.INBOX:contain_subject('Wichtig')

Please consider I don't know much about lua or programming in general, so this is all trial end error. But the output above, which I created through the loop in my test script is exactly what I want as the contents of result.

It is just that imapfilter does not accept this result variable. It should not be a nil value as the error says, as my test script clearly outputs all these lines. But I am sure this can easily be fixed by somebody that knows what they are doing (unlike me) 😆

Thank you in advance for your help :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions