joeyates / imap-backup

Backup and Migrate IMAP Email Accounts
MIT License
1.37k stars 75 forks source link

How is the length/offset calculated? #197

Closed witchent closed 7 months ago

witchent commented 7 months ago

I am sorry to ask this in the issues (because it is more of a question), but I didn't find any link where I could ask this.

How is the length and the offset in the .imap file calculated? If I run imap-backup (on a prepared folder) and look at the resulting files as an example, I get the following .imap file:

{"version":3,"uid_validity":1478344591,"messages":[{"uid":3,"offset":0,"length":98129,"flags":["Seen"]},{"uid":4,"offset":98129,"length":244490,"flags":["Seen"]}]}

If I copy the first message out of the mbox file and run wc on it I get 1523 1705 98116, so 98116 chars. If I use my text editor it says 96593 chars, and 1523 lines, which together also results in 98116 chars, but not 98129 as seen in the .imap file.

It's also not a constant offset, the word count of the second message is off by 46 chars (instead of the 13 chars of the first message).

The reason I'm asking this (in case there is an easier solution I did not find) is the following: I want to automatically get my encrypted mails from IMAP Server A, decrypt them locally, and upload them to Server B (which I host myself so I trust it). Everything works fine except the uploading part, because obviously after decrypting the messages the length of it changes and thus the upload does not work anymore. I did not find any other tool which allows me to easily upload my mbox file to my IMAP server so I figured just correcting the .imap file would be easiest but I can't seem to figure out how the length/offset is calculated. Notice also that I do not want to sync the folders - Instead I want to download mails from a folder in Server A, decrypt them, upload and add them to the folder on Server B (and leave all other messages on Server B intact).

joeyates commented 7 months ago

Hi @witchent

Offsets and lengths relate directly to positions in the mailbox file.

I've added a script to the contrib directory as an example of how to extract email text.

witchent commented 7 months ago

Thanks a lot for the quick answer, will dive into this and see if I can figure it out :)