Guest User

Untitled

a guest
Apr 14th, 2018
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'net/imap'
  3.  
  4. # Source server connection info.
  5. SOURCE_HOST = 'mail.example.com'
  6. SOURCE_PORT = 143
  7. SOURCE_SSL = false
  8. SOURCE_USER = 'username'
  9. SOURCE_PASS = 'password'
  10.  
  11. # Destination server connection info.
  12. DEST_HOST = 'imap.gmail.com'
  13. DEST_PORT = 993
  14. DEST_SSL = true
  15. DEST_USER = 'username@gmail.com'
  16. DEST_PASS = 'password'
  17.  
  18. # Mapping of source folders to destination folders. The key is the name of the
  19. # folder on the source server, the value is the name on the destination server.
  20. # Any folder not specified here will be ignored. If a destination folder does
  21. # not exist, it will be created.
  22. FOLDERS = {
  23. 'INBOX' => 'INBOX',
  24. 'sourcefolder' => 'gmailfolder'
  25. }
  26.  
  27. # Utility methods.
  28. def dd(message)
  29. puts "[#{DEST_HOST}] #{message}"
  30. end
  31.  
  32. def ds(message)
  33. puts "[#{SOURCE_HOST}] #{message}"
  34. end
  35.  
  36. # Connect and log into both servers.
  37. ds 'connecting...'
  38. source = Net::IMAP.new(SOURCE_HOST, SOURCE_PORT, SOURCE_SSL)
  39.  
  40. ds 'logging in...'
  41. source.login(SOURCE_USER, SOURCE_PASS)
  42.  
  43. dd 'connecting...'
  44. dest = Net::IMAP.new(DEST_HOST, DEST_PORT, DEST_SSL)
  45.  
  46. dd 'logging in...'
  47. dest.login(DEST_USER, DEST_PASS)
  48.  
  49. # Loop through folders and copy messages.
  50. FOLDERS.each do |source_folder, dest_folder|
  51. # Open source folder in read-only mode.
  52. begin
  53. ds "selecting folder '#{source_folder}'..."
  54. source.examine(source_folder)
  55. rescue => e
  56. ds "error: select failed: #{e}"
  57. next
  58. end
  59.  
  60. # Open (or create) destination folder in read-write mode.
  61. begin
  62. dd "selecting folder '#{dest_folder}'..."
  63. dest.select(dest_folder)
  64. rescue => e
  65. begin
  66. dd "folder not found; creating..."
  67. dest.create(dest_folder)
  68. dest.select(dest_folder)
  69. rescue => ee
  70. dd "error: could not create folder: #{e}"
  71. next
  72. end
  73. end
  74.  
  75. # Build a lookup hash of all message ids present in the destination folder.
  76. dest_info = {}
  77.  
  78. dd 'analyzing existing messages...'
  79. uids = dest.uid_search(['ALL'])
  80. if uids.length > 0
  81. dest.uid_fetch(uids, ['ENVELOPE']).each do |data|
  82. dest_info[data.attr['ENVELOPE'].message_id] = true
  83. end
  84. end
  85.  
  86. # Loop through all messages in the source folder.
  87. uids = source.uid_search(['ALL'])
  88. if uids.length > 0
  89. source.uid_fetch(uids, ['ENVELOPE']).each do |data|
  90. mid = data.attr['ENVELOPE'].message_id
  91.  
  92. # If this message is already in the destination folder, skip it.
  93. next if dest_info[mid]
  94.  
  95. # Download the full message body from the source folder.
  96. ds "downloading message #{mid}..."
  97. msg = source.uid_fetch(data.attr['UID'], ['RFC822', 'FLAGS',
  98. 'INTERNALDATE']).first
  99.  
  100. # Append the message to the destination folder, preserving flags and
  101. # internal timestamp.
  102. dd "storing message #{mid}..."
  103. dest.append(dest_folder, msg.attr['RFC822'], msg.attr['FLAGS'],
  104. msg.attr['INTERNALDATE'])
  105. end
  106. end
  107.  
  108. source.close
  109. dest.close
  110. end
  111.  
  112. puts 'done'
Add Comment
Please, Sign In to add comment