1+ #[ macro_use]
2+ extern crate lazy_static;
13use indicatif:: ProgressStyle ;
4+ use regex:: Regex ;
25use std:: fs:: { self } ;
36use std:: io:: { Error , ErrorKind } ;
47use std:: path:: { Path , PathBuf } ;
@@ -45,13 +48,18 @@ fn list_eml_file(
4548}
4649
4750fn randomize_message_id ( eml : & String ) -> Result < String , String > {
51+ lazy_static ! {
52+ static ref MID_RE : Regex = Regex :: new( r"(?imu)^message-id:.+$" ) . unwrap( ) ;
53+ }
54+
4855 let mut new_eml = String :: new ( ) ;
4956
50- let header_pos = eml . find ( "Message-ID:" ) ;
57+ let header_pos = MID_RE . find ( eml ) ;
5158 if header_pos. is_none ( ) {
5259 return Err ( "Could not find Message-ID in the EML." . to_string ( ) ) ;
5360 }
54- let ( fpart, lpart) = eml. split_at ( header_pos. unwrap ( ) ) ;
61+
62+ let ( fpart, lpart) = eml. split_at ( header_pos. unwrap ( ) . start ( ) ) ;
5563 new_eml. push_str ( fpart) ;
5664
5765 let ( _mid, rest) = lpart. split_at ( lpart. find ( '\n' ) . expect ( "Malformed Message-ID." ) ) ;
@@ -95,6 +103,7 @@ impl Config {
95103 let recursive = matches. is_present ( "recursive" ) ;
96104 let symlink = matches. is_present ( "symlink" ) ;
97105 let random_id = matches. is_present ( "random-message-id" ) ;
106+
98107 Config {
99108 server,
100109 port,
@@ -192,6 +201,10 @@ fn main() {
192201 println ! ( "- {}" , path. to_str( ) . unwrap_or( "" ) ) ;
193202 }
194203
204+ if conf. random_id {
205+ println ! ( "Randomizing Message-IDs." )
206+ }
207+
195208 let tls = native_tls:: TlsConnector :: builder ( ) . build ( ) . unwrap ( ) ;
196209 let client = imap:: connect ( ( conf. server . clone ( ) , conf. port ) , conf. server , & tls) . unwrap ( ) ;
197210 let mut session = client. login ( conf. login , conf. password ) . unwrap ( ) ;
@@ -204,10 +217,17 @@ fn main() {
204217 for eml in & emls_files {
205218 let rfc822 = fs:: read_to_string ( eml) . expect ( "Failed to read eml file." ) ;
206219 if conf. random_id {
207- let randomize_id = randomize_message_id ( & rfc822) . unwrap ( ) ;
208- session
209- . append ( & conf. folder , & randomize_id)
210- . expect ( "Could not copy eml file to inbox." ) ;
220+ let randomize_id = randomize_message_id ( & rfc822) ;
221+ if randomize_id. is_err ( ) {
222+ println ! (
223+ "Could not find Message-ID for file {}, skipping." ,
224+ eml. to_string_lossy( )
225+ ) ;
226+ } else {
227+ session
228+ . append ( & conf. folder , & randomize_id. unwrap ( ) )
229+ . expect ( "Could not copy eml file to inbox." ) ;
230+ }
211231 } else {
212232 session
213233 . append ( & conf. folder , & rfc822)
0 commit comments