public
Last active

Dreamcatcher - An "mm_" checker for Gmail - deletes emails with links to sites that use Dreamweaver!

  • Download Gist
dreamcatcher.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
#! /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[3].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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.