-
-
Save andrew/150350 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/ruby | |
## Elliott Kember's Delete MM_ Script. | |
# I bet you thought I wouldn't write this! | |
## Usage: | |
# $ ruby dreamcatcher.rb emailaddress password | |
## dreamcatcher.rb | |
# What it does: | |
# It checks all your new emails for links which contain "MM_" in their HTML. | |
# That means they use Dreamweaver. | |
# That means you don't want their emails, so we delete them. | |
# We need some things from the grocery store | |
require 'net/imap' | |
require 'net/http' | |
require 'rubygems' | |
require 'mms2r' | |
require 'activesupport' # For TMail. | |
# And some settings | |
config = {} | |
config['username'] = ARGV[0] # => 'elliott.kember@gmail.com' | |
config['password'] = ARGV[1] # => 'secret' | |
config['timeout'] = ARGV[2].nil? ? 60 : ARGV[2].to_i # seconds | |
config['host'] = ARGV[3] || 'imap.gmail.com' | |
config['port'] = ARGV[4] || '993' | |
latest_uid = nil | |
# Starting time to check from | |
t = (Time.new - 120) | |
# Keep doing this forever. | |
while true do | |
# Let em know what we're doing. | |
puts "Begin MM_ scanning..." | |
# Connect and log in to imap | |
imap = Net::IMAP.new(config['host'], config['port'], true) | |
imap.login(config['username'], config['password']) | |
imap.select('Inbox') | |
# Search for not deleted | |
if !latest_uid.nil? | |
puts "Using latest uid" | |
uids = imap.uid_search("#{latest_uid}:*, NOT SEEN") | |
uids.delete latest_uid | |
else | |
uids = imap.uid_search(["NOT", "SEEN", "SINCE", t]) | |
end | |
latest_uid = uids.max if uids.length > 0 | |
puts "Latest uid: #{latest_uid}" | |
t = Time.new # Next time, we'll go from now. | |
puts "Checking emails: #{uids.join(',')}" | |
if uids.length > 0 | |
uids.each do |uid| | |
# Has to be an integer | |
uid = uid.to_i | |
# Find the email | |
source = imap.uid_fetch(uid, 'RFC822') | |
if source.nil? | |
next # It's gone, forget it | |
else | |
# Get it | |
source = source.first.attr['RFC822'] | |
# Parse it | |
mail = TMail::Mail.parse(source) # TMail object for some stuff | |
# This is where MMS2R comes in handy - plain text body content. | |
mms = MMS2R::Media.new(mail) # MMS object for plain-text content | |
plain_body_text = mms.body # See? | |
# Regex for finding URLs | |
url_regexp = /http[s]?:\/\/\w/ | |
# Find 'em | |
urls = plain_body_text.split.grep(url_regexp) | |
# Check each one | |
delete_this_email = false | |
# Check each url for MM_ | |
urls.each do |url| | |
url = url.gsub(/<|>/, '') | |
# Downloadify it | |
r = Net::HTTP.get_response(URI.parse(url).host, URI.parse(url).path) | |
# Check it for MM_ | |
if r.body.include? 'MM_' | |
puts "Found #{uid} - #{mail.subject} from #{mail.from} - deleting that sucker." | |
delete_this_email = true | |
# I've seen enough. GTFO. | |
break | |
else | |
# Good, clean email. | |
puts "#{url} is clean." | |
end | |
end | |
# If we're deleting this email, delete it! What a worthless comment. | |
if delete_this_email | |
imap.uid_store(uid, "+FLAGS", [:Deleted]) | |
puts "Deleting that email. Spammers!" | |
else | |
puts "Email was clean. Next!" | |
end | |
end | |
end | |
else | |
puts "Nothing found. For now. Trying again in #{config['timeout']} seconds." | |
end | |
puts "Done for now." | |
# Log out so we can reconnect, otherwise this gets cached. | |
# This seems really awful, but keeping the connection open got messy. | |
# GMail seems to cache things between searches which makes things hard. | |
imap.logout | |
# Sleep for x seconds so GMail doesn't hate us. | |
sleep config['timeout'] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment