Guest User

Untitled

a guest
Oct 10th, 2018
147
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 = 'imap.gmail.com'
  6. SOURCE_PORT = 993
  7. SOURCE_SSL = true
  8. SOURCE_USER = 'primary account'
  9. SOURCE_PASS = 'primary password'
  10.  
  11. # Destination server connection info.
  12. DEST_HOST = 'imap.gmail.com'
  13. DEST_PORT = 993
  14. DEST_SSL = true
  15. DEST_USER = 'secondary account'
  16. DEST_PASS = 'secondary 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. 'Sent Messages' => 'sentemail'
  24. }
  25.  
  26. # Utility methods.
  27. def dd(message)
  28. puts "[#{DEST_HOST}] #{message}"
  29. end
  30.  
  31. def ds(message)
  32. puts "[#{SOURCE_HOST}] #{message}"
  33. end
  34.  
  35. # Connect and log into both servers.
  36. ds 'connecting...'
  37. source = Net::IMAP.new(SOURCE_HOST, SOURCE_PORT, SOURCE_SSL)
  38.  
  39. ds 'logging in...'
  40. source.login(SOURCE_USER, SOURCE_PASS)
  41.  
  42. dd 'connecting...'
  43. dest = Net::IMAP.new(DEST_HOST, DEST_PORT, DEST_SSL)
  44.  
  45. dd 'logging in...'
  46. dest.login(DEST_USER, DEST_PASS)
  47.  
  48. # Loop through folders and copy messages.
  49. FOLDERS.each do |source_folder, dest_folder|
  50. # Open source folder in read-only mode.
  51. begin
  52. ds "selecting folder '#{source_folder}'..."
  53. source.examine(source_folder)
  54. rescue => e
  55. ds "error: select failed: #{e}"
  56. next
  57. end
  58.  
  59. # Open (or create) destination folder in read-write mode.
  60. begin
  61. dd "selecting folder '#{dest_folder}'..."
  62. dest.select(dest_folder)
  63. rescue => e
  64. begin
  65. dd "folder not found; creating..."
  66. dest.create(dest_folder)
  67. dest.select(dest_folder)
  68. rescue => ee
  69. dd "error: could not create folder: #{e}"
  70. next
  71. end
  72. end
  73.  
  74. # Build a lookup hash of all message ids present in the destination folder.
  75. dest_info = {}
  76.  
  77. dd 'analyzing existing messages...'
  78. uids = dest.uid_search(['ALL'])
  79. if uids.length > 0
  80. dest.uid_fetch(uids, ['ENVELOPE']).each do |data|
  81. dest_info[data.attr['ENVELOPE'].message_id] = true
  82. end
  83. end
  84.  
  85. # Loop through all messages in the source folder.
  86. uids = source.uid_search(['ALL'])
  87. if uids.length > 0
  88. source.uid_fetch(uids, ['ENVELOPE']).each do |data|
  89. mid = data.attr['ENVELOPE'].message_id
  90.  
  91. # If this message is already in the destination folder, skip it.
  92. next if dest_info[mid]
  93.  
  94. # Download the full message body from the source folder.
  95. ds "downloading message #{mid}..."
  96. msg = source.uid_fetch(data.attr['UID'], ['RFC822', 'FLAGS',
  97. 'INTERNALDATE']).first
  98.  
  99. # Append the message to the destination folder, preserving flags and
  100. # internal timestamp.
  101. dd "storing message #{mid}..."
  102. dest.append(dest_folder, msg.attr['RFC822'], msg.attr['FLAGS'],
  103. msg.attr['INTERNALDATE'])
  104. end
  105. end
  106.  
  107. source.close
  108. dest.close
  109. end
  110.  
  111. puts 'done'
Add Comment
Please, Sign In to add comment