Deleting those pesky RFC-822 in-compatible emails with Ruby

If you have landed on this post from Google you already know what I am talking about, if not read the below intro.

I work for an “Enterprisy” company, so the standard here is using the exchange server and I am on Linux using thunder bird. Every once in a while I get those emails that the exchange server cannot convert to an RFC-822 compatible format and thunder bird chokes on em and even the web mail cannot handle these, the only solution so far has been going to the web mail and selecting the mail and moving it to trash.

Finally I wrote a script to automate the whole thing, it telnets into the server checks each message to see if it is RFC-822 compatible and if not moves it into Deleted Items, you can find the script here. (It’s on GitHub feel free to fork it)

Usage ruby telnet-imap.rb <server> <username> <password> <dryrun>

All arguments are self explanatory, dry run can be y/n

PS : if you mange you blow up your inbox you are on your own!!

UPDATE (2010/03/12): Looks like this has taken a life of its own, please see the comments to find a better/newer versions in different languages.

Use Ruby to Prefix all lines /Postfix all lines/Remove empty lines/Replace strings in all lines of a file

For some reason or the other I had to do bulk operations on files, like prefix/postfix all lines or remove empty lines
, yeah I could do it with some unix command, but I am already using ruby to massage the data just wanted to extand that.So here
is what I did wrote a ruby script to do it now I can do stuff like

prefix ( ‘c:\tmp\zip.txt’,’some-prefix’ )

The script, hmmm I wonder if I could submit this to ruby people.

require 'fileutils'

module DevFileutils
include FileUtils::Verbose

# Opens a file and adds prefix to each line
def prefix file_name, prefix
temp = File.new(file_name+".tmp", 'a+')
IO.foreach(file_name) do |line|
temp.puts prefix + line
end
temp.close
mv file_name+'.tmp', file_name
end

# Opens a file and adds postfix to each line
def postfix file_name, postfix
temp = File.new(file_name+".tmp", 'a+')
IO.foreach(file_name) do |line|
temp.puts line.chomp + postfix
end
temp.close
mv file_name+'.tmp', file_name
end

#removes all empty lines from a file
def remove_empty_lines file_name
temp = File.new(file_name+".tmp", 'a+')
IO.foreach(file_name) do |line|
temp.puts line if !line.chomp.empty?
end
temp.close
mv file_name+'.tmp', file_name
end

def replace file_name,string,with_string
temp = File.new(file_name+".tmp", 'a+')
IO.foreach(file_name) do |line|
temp.puts line.gsub(string,with_string)
end
temp.close
mv file_name+'.tmp', file_name
end
end