Skip to content

Instantly share code, notes, and snippets.

@seven1m
Created October 9, 2019 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seven1m/82ac194bc8c894a44bff54a16f43bf9b to your computer and use it in GitHub Desktop.
Save seven1m/82ac194bc8c894a44bff54a16f43bf9b to your computer and use it in GitHub Desktop.
poll mail.morgn.net protocol POP3
user "tim" with password "blablablabla" is tim here
* * * * * $HOME/myfetchmail.sh
require 'net/imap'
DEBUG = ARGV.include?('-d')
PASS = File.read(File.expand_path('.password', __dir__)).strip
imap = Net::IMAP.new('localhost')
imap.authenticate('LOGIN', 'tim', PASS)
imap.select('INBOX')
imap.search(['ALL']).each do |message_id|
headers = imap.fetch(message_id, 'BODY.PEEK[HEADER.FIELDS (X-Spam-Status Subject)]')[0].attr.values.first
if headers =~ /X-Spam-Status: Yes/
subject = headers.match(/Subject: (.*)/)[1] rescue nil
puts "moving to Spam: [#{message_id}] #{subject}" if DEBUG
imap.copy(message_id, 'Spam')
imap.store(message_id, '+FLAGS', [:Deleted])
end
end
imap.expunge
#!/bin/bash
#tim@morgn:~ $ fetchmail
#1 message for tim at mail.morgn.net (2316 octets).
#reading message tim@mail.morgn.net:1 of 1 (2316 octets) flushed
#tim@morgn:~ $ fetchmail
#fetchmail: No mail for tim at mail.morgn.net
out=$(fetchmail 2>&1)
if echo "$out" | egrep "[0-9]+ message" &>/dev/null; then
sleep 2 # not sure if this is really needed
ruby $HOME/move_spam.rb
elif echo "$out" | egrep "No mail" &>/dev/null; then
exit
else
echo "$out"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment