1. Linux

1.1. Installation

  • Enter the following commands at a Command Line.

    sudo apt-get install imapfilter

1.2. Configuration

  • Crontab entry.

    */2 * * * * /home/daniel/bin/cleanimap.sh
  • Contents of the ~/bin/cleanimap.sh file.

    #!/bin/bash
    mkdir -p "$HOME/tmp"
    PIDFILE="$HOME/tmp/imapfilter.pid"
    
    if [ -e "${PIDFILE}" ] && (ps -u $(whoami) -opid= |
                               grep -P "^\s*$(cat ${PIDFILE})$" &> /dev/null); then
      echo "Already running."
      exit 99
    fi
    
    /usr/bin/imapfilter -c /home/daniel/.imapfilter/config.lua > $HOME/tmp/imapfilter.log &
    
    echo $! > "${PIDFILE}"
    chmod 644 "${PIDFILE}"
  • Contents of the ~/.imapfilter/config.lua file.

    -- The following four options and contents were taken from
    -- https://raymii.org/s/blog/Filtering_IMAP_mail_with_imapfilter.html
    -- The time in seconds for the program to wait for a mail server's response (default 60)
    options.timeout = 120
    -- According to the IMAP specification, when trying to write a message to a
    -- non-existent mailbox, the server must send a hint to the client,
    -- whether it should create the mailbox and try again or not.
    -- However some IMAP servers don't follow the specification and don't
    -- send the correct response code to the client.
    -- By enabling this option the client tries to create the mailbox,
    -- despite of the server's response.
    options.create = true
    -- By enabling this option new mailboxes that were automatically created,
    -- get also subscribed; they are set active in order for IMAP clients to recognize them
    options.subscribe = true
    -- Normally, messages are marked for deletion and are actually
    -- deleted when the mailbox is closed. When this option is enabled,
    -- messages are expunged immediately after being marked deleted.
    options.expunge = true
    
    -- server configuration settings
    workserver = IMAP {
      server = "new-work-server.com",
      username = "daniel",
      password = "hunter2",
      port = 993,
      ssl = "tls1"
    }
    
    -- rules for mails regarding CHASE workshop
    -- Either the subject contains "chase", or the
    -- to: field contains "chaseresearch.org" or "chase2016"
    chase = (
        workserver.INBOX:contain_subject("chase") +
        workserver.INBOX:contain_to("chaseresearch.org") +
        workserver.INBOX:contain_to("chase2016")
    )
    -- move the messages identified by the rule into the CHASE folder
    chase:move_messages(workserver.CHASE)
    
    -- rules for mails regarding JORS journal
    -- Either the subject contains "JORS", or the
    -- mail text contains "JORS", or the mail comes
    -- from a domain associated to JORS
    jors = (
        workserver.INBOX:contain_subject("JORS") +
        workserver.INBOX:contain_body("JORS") +
        workserver.INBOX:contain_from("software.ac.uk") +
        workserver.INBOX:contain_from("ubiquitypress.com")
    )
    -- move the messages identified by the rule into the EDITORIAL folder
    jors:move_messages(workserver.EDITORIAL)
    
    -- move those e-mails related to call for papers to a CFP folder
    cfp = (
        workserver.INBOX:contain_subject("AISWorld") +
        workserver.INBOX:contain_subject("SEworld") +
        workserver.INBOX:contain_subject("CFP") +
        workserver.INBOX:contain_subject("Call for papers")
    )
    cfp:move_messages(workserver.CFP)
    
    -- handle the tons of mails from ACM and IEEE unless they
    -- mention a conference I am often associated with (note the -)
    societies = (
        workserver.INBOX:contain_to("acm.org") +
        workserver.INBOX:contain_to("ieee.org") -
        workserver.INBOX:contain_message("PROFES")
    )
    societies:move_messages(workserver.SOCIETIES)
    
    -- all mails received by the old work's account (redirected to the new one), that
    -- are not directed directly to me are moved to OLDWORK
    oldworknotdirect = (
        workserver.INBOX:contain_to("somemail@oldwork.it") +
        workserver.INBOX:contain_to("distribution_list@oldwork.it")
    )
    oldworknotdirect:move_messages(workserver.OLDWORK)