Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- #
- # usage:
- # ------
- # empty the trash
- # ruby g.rb --deletetrash user@gmail.com password
- #
- #
- # notes:
- # ------
- # load modules
- require 'net/imap'
- require 'date'
- # declare globals
- result = nil
- user = ''
- pass = ''
- deletetrash = false # default: make user manually 'Empty Trash now' via web
- deletespam = false # default: we don't care about the 'Spam' folder much
- # process command line arguments
- if(ARGV.size > 1)
- if(ARGV[0] == '--deletetrash')
- deletetrash = true
- ARGV.shift
- end
- if(ARGV[0] == '--deletetrashspam')
- deletetrash = true
- deletespam = true
- ARGV.shift
- end
- user = ARGV[0]
- pass = ARGV[1]
- if(ARGV.size == 3)
- label = ARGV[2]
- end
- else
- puts "Usage: ruby gmail.rb [--deletetrash|--deletetrashspam] <user@gmail.com> <password>"
- exit(1)
- end
- # connect to the server
- puts "Started at #{Time.now}"
- imap = Net::IMAP.new('imap.gmail.com', '993', true)
- imap.login(user, pass)
- # do we have to empty the trash, too?
- if deletetrash
- imap.select('[Gmail]/Trash')
- result = imap.search(["SINCE", '1-Jan-1900'])
- #result = imap.search(["FROM", "Somebody"])
- puts "Attempting to delete all #{result.length} messages in trash..."
- imap.store(1..result.length, "+FLAGS", [:Deleted])
- imap.expunge
- end
- # did the user want to clean out the spam folder, too?
- if deletespam
- imap.select('[Gmail]/Spam')
- result = imap.search(["SINCE", '1-Jan-1900'])
- puts "Attempting to delete all #{result.length} messages in spam folder..."
- imap.store(1..result.length, "+FLAGS", [:Deleted])
- imap.expunge
- end
- # end imap session
- imap.logout
- # tell the user we finished
- puts "Ended at #{Time.now}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement