Para transferir o meu email para o GMail tive que arranjar uma forma de copiar os 512MB de mail do meu servidor para o GMail. Após uma pesquisa rápida encontrei este script que fazia quase o que eu queria, quase porque não sincronizava todos as pastas mas apenas aquelas que nós indicávamos.
Decidi então alterar o script e saiu isto:
[source:ruby]#!/usr/bin/env ruby
require ‘net/imap’
# Source server connection info.
SOURCE_HOST = ‘sourceserver.domain’
SOURCE_PORT = 143
SOURCE_SSL = false
SOURCE_USER = ‘username’
SOURCE_PASS = ‘password’
# Destination server connection info.
DEST_HOST = ‘imap.gmail.com’
DEST_PORT = 993
DEST_SSL = true
DEST_USER = ‘username’
DEST_PASS = ‘password’
# Utility methods.
def dd(message)
puts “[#{DEST_HOST}] #{message}”
end
def ds(message)
puts “[#{SOURCE_HOST}] #{message}”
end
# Connect and log into both servers.
ds ‘connecting…’
source = Net::IMAP.new(SOURCE_HOST, SOURCE_PORT, SOURCE_SSL)
ds ‘logging in…’
source.login(SOURCE_USER, SOURCE_PASS)
# Get the list of folder on the source server
SOURCEFOLDERS = source.lsub(”, ‘*’)
dd ‘connecting…’
dest = Net::IMAP.new(DEST_HOST, DEST_PORT, DEST_SSL)
dd ‘logging in…’
dest.login(DEST_USER, DEST_PASS)
# Loop through folders and copy messages.
SOURCEFOLDERS.each do |net_folder|
source_folder = net_folder.name
dest_folder = net_folder.name.gsub(/\./, “/”)
# Open source folder in read-only mode.
begin
ds “selecting folder ‘#{source_folder}’…”
source.examine(source_folder)
rescue => e
ds “error: select failed: #{e}”
next
end
# Open (or create) destination folder in read-write mode.
begin
dd “selecting dest folder ‘#{dest_folder}’…”
dest.select(dest_folder)
rescue => e
begin
dd “folder not found; creating…”
dest.create(dest_folder)
dest.select(dest_folder)
rescue => ee
dd “error: could not create folder: #{e}”
next
end
end
# Build a lookup hash of all message ids present in the dest folder.
dest_info = {}
dd ‘analyzing existing messages…’
uids = dest.uid_search([‘ALL’])
if uids.length > 0
dest.uid_fetch(uids, [‘ENVELOPE’]).each do |data|
dest_info[data.attr[‘ENVELOPE’].message_id] = true
end
end
# Loop through all messages in the source folder.
uids = source.uid_search([‘ALL’])
if uids.length > 0
source.uid_fetch(uids, [‘ENVELOPE’]).each do |data|
mid = data.attr[‘ENVELOPE’].message_id
# If this message is already in the destination folder, skip it.
next if dest_info[mid]
# Download the full message body from the source folder.
ds “downloading message #{mid}…”
msg = source.uid_fetch(data.attr[‘UID’], [‘RFC822’, ‘FLAGS’,
‘INTERNALDATE’]).first
# Append the message to the destination folder, preserving flags
# and internal timestamp.
dd “storing message #{mid}…”
dest.append(dest_folder, msg.attr[‘RFC822’], msg.attr[‘FLAGS’],
msg.attr[‘INTERNALDATE’])
end
end
source.close
dest.close
end
puts ‘done’
[/source]