2018-07-13 18:38:57 +03:00
/*
2019-06-28 19:34:40 +03:00
* meli - imap module .
2018-07-13 18:38:57 +03:00
*
2019-06-28 19:34:40 +03:00
* Copyright 2019 Manos Pitsidianakis
2018-07-13 18:38:57 +03:00
*
* This file is part of meli .
*
* meli is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* meli is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with meli . If not , see < http ://www.gnu.org/licenses/>.
* /
2020-02-06 01:49:18 +02:00
use crate ::get_path_hash ;
2020-01-08 17:04:44 +02:00
use smallvec ::SmallVec ;
2019-06-28 19:34:40 +03:00
#[ macro_use ]
mod protocol_parser ;
pub use protocol_parser ::{ UntaggedResponse ::* , * } ;
2020-02-26 10:54:10 +02:00
mod mailbox ;
pub use mailbox ::* ;
2019-06-28 19:34:40 +03:00
mod operations ;
pub use operations ::* ;
mod connection ;
pub use connection ::* ;
2019-09-06 12:54:12 +03:00
mod watch ;
pub use watch ::* ;
2020-02-28 15:47:07 +02:00
pub mod managesieve ;
2019-06-28 19:34:40 +03:00
2019-09-11 17:57:55 +03:00
use crate ::async_workers ::{ Async , AsyncBuilder , AsyncStatus , WorkContext } ;
2019-06-28 19:34:40 +03:00
use crate ::backends ::BackendOp ;
2020-02-26 10:54:10 +02:00
use crate ::backends ::MailboxHash ;
2019-06-28 19:34:40 +03:00
use crate ::backends ::RefreshEvent ;
use crate ::backends ::RefreshEventKind ::{ self , * } ;
2020-02-26 10:54:10 +02:00
use crate ::backends ::{ BackendMailbox , MailBackend , Mailbox , RefreshEventConsumer } ;
2019-06-28 19:34:40 +03:00
use crate ::conf ::AccountSettings ;
use crate ::email ::* ;
use crate ::error ::{ MeliError , Result } ;
use fnv ::{ FnvHashMap , FnvHashSet } ;
2019-12-02 01:09:23 +02:00
use std ::collections ::{ hash_map ::DefaultHasher , BTreeMap } ;
2019-09-08 10:27:10 +03:00
use std ::hash ::Hasher ;
2019-08-24 15:12:44 +03:00
use std ::str ::FromStr ;
2019-11-18 12:59:04 +02:00
use std ::sync ::{ Arc , Mutex , RwLock } ;
2019-12-14 18:46:12 +02:00
use std ::time ::Instant ;
2019-06-28 19:34:40 +03:00
pub type UID = usize ;
2019-11-23 17:48:52 +02:00
pub static SUPPORTED_CAPABILITIES : & 'static [ & 'static str ] =
& [ " IDLE " , " LOGIN " , " LOGINDISABLED " , " ENABLE " , " IMAP4REV1 " ] ;
2019-11-06 15:11:35 +02:00
2019-09-14 15:57:28 +03:00
#[ derive(Debug, Default) ]
pub struct EnvelopeCache {
bytes : Option < String > ,
headers : Option < String > ,
body : Option < String > ,
flags : Option < Flag > ,
}
2019-09-21 21:02:35 +03:00
#[ derive(Debug, Clone) ]
pub struct ImapServerConf {
pub server_hostname : String ,
pub server_username : String ,
pub server_password : String ,
pub server_port : u16 ,
pub use_starttls : bool ,
pub danger_accept_invalid_certs : bool ,
2020-02-28 15:47:07 +02:00
pub protocol : ImapProtocol ,
2019-09-21 21:02:35 +03:00
}
2019-10-24 20:30:17 +03:00
struct IsSubscribedFn ( Box < dyn Fn ( & str ) -> bool + Send + Sync > ) ;
impl std ::fmt ::Debug for IsSubscribedFn {
fn fmt ( & self , f : & mut std ::fmt ::Formatter ) -> std ::fmt ::Result {
write! ( f , " IsSubscribedFn Box " )
}
}
impl std ::ops ::Deref for IsSubscribedFn {
type Target = Box < dyn Fn ( & str ) -> bool + Send + Sync > ;
fn deref ( & self ) -> & Box < dyn Fn ( & str ) -> bool + Send + Sync > {
& self . 0
}
}
2019-09-14 15:57:28 +03:00
type Capabilities = FnvHashSet < Vec < u8 > > ;
2019-11-10 23:02:23 +02:00
2020-02-28 15:47:07 +02:00
#[ macro_export ]
2019-11-27 14:22:53 +02:00
macro_rules ! get_conf_val {
( $s :ident [ $var :literal ] ) = > {
$s . extra . get ( $var ) . ok_or_else ( | | {
MeliError ::new ( format! (
" Configuration error ({}): IMAP connection requires the field `{}` set " ,
$s . name . as_str ( ) ,
$var
) )
} )
} ;
( $s :ident [ $var :literal ] , $default :expr ) = > {
$s . extra
. get ( $var )
. map ( | v | {
< _ > ::from_str ( v ) . map_err ( | e | {
MeliError ::new ( format! (
" Configuration error ({}): Invalid value for field `{}`: {} \n {} " ,
$s . name . as_str ( ) ,
$var ,
v ,
e
) )
} )
} )
. unwrap_or_else ( | | Ok ( $default ) )
} ;
}
2019-11-10 23:02:23 +02:00
#[ derive(Debug) ]
pub struct UIDStore {
2020-02-26 10:54:10 +02:00
uidvalidity : Arc < Mutex < FnvHashMap < MailboxHash , UID > > > ,
hash_index : Arc < Mutex < FnvHashMap < EnvelopeHash , ( UID , MailboxHash ) > > > ,
2019-11-10 23:02:23 +02:00
uid_index : Arc < Mutex < FnvHashMap < UID , EnvelopeHash > > > ,
byte_cache : Arc < Mutex < FnvHashMap < UID , EnvelopeCache > > > ,
}
2019-06-28 19:34:40 +03:00
#[ derive(Debug) ]
pub struct ImapType {
account_name : String ,
2019-12-14 18:46:12 +02:00
online : Arc < Mutex < ( Instant , Result < ( ) > ) > > ,
2019-10-24 20:30:17 +03:00
is_subscribed : Arc < IsSubscribedFn > ,
2019-09-14 15:57:28 +03:00
connection : Arc < Mutex < ImapConnection > > ,
2019-09-21 21:02:35 +03:00
server_conf : ImapServerConf ,
2019-11-10 23:02:23 +02:00
uid_store : Arc < UIDStore > ,
2019-12-02 01:09:23 +02:00
can_create_flags : Arc < Mutex < bool > > ,
tag_index : Arc < RwLock < BTreeMap < u64 , String > > > ,
2019-08-24 15:12:44 +03:00
2020-02-26 10:54:10 +02:00
mailboxes : Arc < RwLock < FnvHashMap < MailboxHash , ImapMailbox > > > ,
2018-07-13 18:38:57 +03:00
}
2019-06-28 19:34:40 +03:00
impl MailBackend for ImapType {
2019-12-14 18:46:12 +02:00
fn is_online ( & self ) -> Result < ( ) > {
self . online . lock ( ) . unwrap ( ) . 1. clone ( )
2019-10-24 20:30:17 +03:00
}
2019-12-14 18:55:08 +02:00
fn connect ( & mut self ) {
if self . is_online ( ) . is_err ( ) {
if Instant ::now ( ) . duration_since ( self . online . lock ( ) . unwrap ( ) . 0 )
> = std ::time ::Duration ::new ( 2 , 0 )
{
2020-02-22 11:22:11 +02:00
let _ = self . connection . lock ( ) . unwrap ( ) . connect ( ) ;
2019-12-14 18:55:08 +02:00
}
}
}
2020-02-26 10:54:10 +02:00
fn get ( & mut self , mailbox : & Mailbox ) -> Async < Result < Vec < Envelope > > > {
2019-06-28 19:34:40 +03:00
let mut w = AsyncBuilder ::new ( ) ;
let handle = {
let tx = w . tx ( ) ;
2019-11-10 23:02:23 +02:00
let uid_store = self . uid_store . clone ( ) ;
2019-12-08 10:57:36 +02:00
let tag_index = self . tag_index . clone ( ) ;
2019-12-02 01:09:23 +02:00
let can_create_flags = self . can_create_flags . clone ( ) ;
2020-02-26 10:54:10 +02:00
let mailbox_hash = mailbox . hash ( ) ;
let ( permissions , mailbox_path , mailbox_exists , no_select , unseen ) = {
let f = & self . mailboxes . read ( ) . unwrap ( ) [ & mailbox_hash ] ;
2019-12-17 14:12:41 +02:00
(
f . permissions . clone ( ) ,
2020-02-06 01:49:18 +02:00
f . imap_path ( ) . to_string ( ) ,
2019-12-17 14:12:41 +02:00
f . exists . clone ( ) ,
f . no_select ,
f . unseen . clone ( ) ,
)
2019-11-11 00:47:23 +02:00
} ;
2019-09-06 12:54:12 +03:00
let connection = self . connection . clone ( ) ;
2019-09-11 17:57:55 +03:00
let closure = move | _work_context | {
2019-12-10 23:54:19 +02:00
if no_select {
tx . send ( AsyncStatus ::Payload ( Ok ( Vec ::new ( ) ) ) ) . unwrap ( ) ;
tx . send ( AsyncStatus ::Finished ) . unwrap ( ) ;
return ;
}
2020-01-15 12:33:05 +02:00
let _tx = tx . clone ( ) ;
if let Err ( err ) = ( move | | {
let tx = _tx ;
let mut response = String ::with_capacity ( 8 * 1024 ) ;
let mut conn = connection . lock ( ) ? ;
2020-02-26 10:54:10 +02:00
debug! ( " locked for get {} " , mailbox_path ) ;
2019-06-28 19:34:40 +03:00
2020-01-15 12:33:05 +02:00
/* first SELECT the mailbox to get READ / WRITE permissions (because EXAMINE only
* returns READ - ONLY for both cases ) * /
2020-02-26 10:54:10 +02:00
conn . send_command ( format! ( " SELECT \" {} \" " , mailbox_path ) . as_bytes ( ) ) ? ;
2020-01-15 12:33:05 +02:00
conn . read_response ( & mut response ) ? ;
let examine_response = protocol_parser ::select_response ( & response ) ? ;
* can_create_flags . lock ( ) . unwrap ( ) = examine_response . can_create_flags ;
debug! (
2020-02-26 10:54:10 +02:00
" mailbox: {} examine_response: {:?} " ,
mailbox_path , examine_response
2020-01-15 12:33:05 +02:00
) ;
let mut exists : usize = examine_response . uidnext - 1 ;
{
let mut uidvalidities = uid_store . uidvalidity . lock ( ) . unwrap ( ) ;
2019-11-10 23:02:23 +02:00
2020-01-15 12:33:05 +02:00
let v = uidvalidities
2020-02-26 10:54:10 +02:00
. entry ( mailbox_hash )
2020-01-15 12:33:05 +02:00
. or_insert ( examine_response . uidvalidity ) ;
* v = examine_response . uidvalidity ;
2019-11-11 00:47:23 +02:00
2020-01-15 12:33:05 +02:00
let mut permissions = permissions . lock ( ) . unwrap ( ) ;
permissions . create_messages = ! examine_response . read_only ;
permissions . remove_messages = ! examine_response . read_only ;
permissions . set_flags = ! examine_response . read_only ;
permissions . rename_messages = ! examine_response . read_only ;
permissions . delete_messages = ! examine_response . read_only ;
permissions . delete_messages = ! examine_response . read_only ;
2020-02-26 10:54:10 +02:00
let mut mailbox_exists = mailbox_exists . lock ( ) . unwrap ( ) ;
* mailbox_exists = exists ;
2020-01-15 12:33:05 +02:00
}
/* reselecting the same mailbox with EXAMINE prevents expunging it */
2020-02-26 10:54:10 +02:00
conn . send_command ( format! ( " EXAMINE \" {} \" " , mailbox_path ) . as_bytes ( ) ) ? ;
2020-01-15 12:33:05 +02:00
conn . read_response ( & mut response ) ? ;
2018-08-06 11:05:09 +03:00
2020-01-15 12:33:05 +02:00
let mut tag_lck = tag_index . write ( ) . unwrap ( ) ;
let mut our_unseen = 0 ;
while exists > 1 {
let mut envelopes = vec! [ ] ;
conn . send_command (
format! (
" UID FETCH {}:{} (UID FLAGS ENVELOPE BODYSTRUCTURE) " ,
std ::cmp ::max ( exists . saturating_sub ( 500 ) , 1 ) ,
exists
)
. as_bytes ( ) ,
) ? ;
conn . read_response ( & mut response ) ? ;
debug! (
" fetch response is {} bytes and {} lines " ,
response . len ( ) ,
response . lines ( ) . collect ::< Vec < & str > > ( ) . len ( )
) ;
let ( _ , v , _ ) = protocol_parser ::uid_fetch_responses ( & response ) ? ;
debug! ( " responses len is {} " , v . len ( ) ) ;
for UidFetchResponse {
uid ,
flags ,
envelope ,
..
} in v
{
let mut env = envelope . unwrap ( ) ;
let mut h = DefaultHasher ::new ( ) ;
h . write_usize ( uid ) ;
2020-02-26 10:54:10 +02:00
h . write ( mailbox_path . as_bytes ( ) ) ;
2020-01-15 12:33:05 +02:00
env . set_hash ( h . finish ( ) ) ;
if let Some ( ( flags , keywords ) ) = flags {
if ! flags . contains ( Flag ::SEEN ) {
our_unseen + = 1 ;
}
env . set_flags ( flags ) ;
for f in keywords {
let hash = tag_hash! ( f ) ;
if ! tag_lck . contains_key ( & hash ) {
tag_lck . insert ( hash , f ) ;
2019-12-08 10:57:36 +02:00
}
2020-01-15 12:33:05 +02:00
env . labels_mut ( ) . push ( hash ) ;
2019-09-14 15:57:28 +03:00
}
2019-06-28 19:34:40 +03:00
}
2020-01-15 12:33:05 +02:00
uid_store
. hash_index
. lock ( )
. unwrap ( )
2020-02-26 10:54:10 +02:00
. insert ( env . hash ( ) , ( uid , mailbox_hash ) ) ;
2020-01-15 12:33:05 +02:00
uid_store . uid_index . lock ( ) . unwrap ( ) . insert ( uid , env . hash ( ) ) ;
envelopes . push ( env ) ;
2019-06-28 19:34:40 +03:00
}
2020-01-15 12:33:05 +02:00
exists = std ::cmp ::max ( exists . saturating_sub ( 500 ) , 1 ) ;
debug! ( " sending payload " ) ;
2019-12-17 14:12:41 +02:00
2020-01-15 12:33:05 +02:00
* unseen . lock ( ) . unwrap ( ) = our_unseen ;
tx . send ( AsyncStatus ::Payload ( Ok ( envelopes ) ) ) . unwrap ( ) ;
}
drop ( conn ) ;
Ok ( ( ) )
} ) ( ) {
tx . send ( AsyncStatus ::Payload ( Err ( err ) ) ) . unwrap ( ) ;
2019-06-28 19:34:40 +03:00
}
2019-09-09 12:53:39 +03:00
tx . send ( AsyncStatus ::Finished ) . unwrap ( ) ;
2019-06-28 19:34:40 +03:00
} ;
Box ::new ( closure )
} ;
w . build ( handle )
2018-07-13 18:38:57 +03:00
}
2019-06-28 19:34:40 +03:00
2019-12-17 14:12:41 +02:00
fn refresh (
& mut self ,
2020-02-26 10:54:10 +02:00
mailbox_hash : MailboxHash ,
2020-02-22 11:24:36 +02:00
sender : RefreshEventConsumer ,
) -> Result < Async < ( ) > > {
self . connection . lock ( ) . unwrap ( ) . connect ( ) ? ;
let inbox = self
2020-02-26 10:54:10 +02:00
. mailboxes
2020-02-22 11:24:36 +02:00
. read ( )
. unwrap ( )
2020-02-26 10:54:10 +02:00
. get ( & mailbox_hash )
2020-02-22 11:24:36 +02:00
. map ( std ::clone ::Clone ::clone )
. unwrap ( ) ;
let tag_index = self . tag_index . clone ( ) ;
let main_conn = self . connection . clone ( ) ;
let uid_store = self . uid_store . clone ( ) ;
let account_name = self . account_name . clone ( ) ;
let w = AsyncBuilder ::new ( ) ;
let closure = move | work_context : WorkContext | {
let thread = std ::thread ::current ( ) ;
let mut conn = main_conn . lock ( ) . unwrap ( ) ;
work_context
. set_name
. send ( (
thread . id ( ) ,
format! ( " refreshing {} imap connection " , account_name . as_str ( ) , ) ,
) )
. unwrap ( ) ;
work_context
. set_status
. send ( ( thread . id ( ) , " refresh " . to_string ( ) ) )
. unwrap ( ) ;
watch ::examine_updates (
& inbox ,
& sender ,
& mut conn ,
& uid_store ,
& work_context ,
& tag_index ,
)
. ok ( )
. take ( ) ;
} ;
Ok ( w . build ( Box ::new ( closure ) ) )
2019-12-17 14:12:41 +02:00
}
2019-09-11 17:57:55 +03:00
fn watch (
& self ,
sender : RefreshEventConsumer ,
work_context : WorkContext ,
) -> Result < std ::thread ::ThreadId > {
2020-02-26 10:54:10 +02:00
let mailboxes = self . mailboxes . clone ( ) ;
2019-12-08 10:57:36 +02:00
let tag_index = self . tag_index . clone ( ) ;
2019-12-14 18:46:12 +02:00
let conn = ImapConnection ::new_connection ( & self . server_conf , self . online . clone ( ) ) ;
2019-09-06 12:54:12 +03:00
let main_conn = self . connection . clone ( ) ;
2019-10-24 20:30:17 +03:00
let is_online = self . online . clone ( ) ;
2019-11-10 23:02:23 +02:00
let uid_store = self . uid_store . clone ( ) ;
2019-09-11 17:57:55 +03:00
let handle = std ::thread ::Builder ::new ( )
2019-09-06 12:54:12 +03:00
. name ( format! ( " {} imap connection " , self . account_name . as_str ( ) , ) )
. spawn ( move | | {
2019-09-11 17:57:55 +03:00
let thread = std ::thread ::current ( ) ;
work_context
. set_status
. send ( ( thread . id ( ) , " watching " . to_string ( ) ) )
. unwrap ( ) ;
2019-12-11 01:36:04 +02:00
let has_idle : bool = main_conn
. lock ( )
. unwrap ( )
. capabilities
. iter ( )
. any ( | cap | cap . eq_ignore_ascii_case ( b " IDLE " ) ) ;
2019-09-06 12:54:12 +03:00
let kit = ImapWatchKit {
conn ,
2019-10-24 20:30:17 +03:00
is_online ,
2019-09-06 12:54:12 +03:00
main_conn ,
2019-11-10 23:02:23 +02:00
uid_store ,
2020-02-26 10:54:10 +02:00
mailboxes ,
2019-09-06 12:54:12 +03:00
sender ,
2019-09-11 17:57:55 +03:00
work_context ,
2019-12-08 10:57:36 +02:00
tag_index ,
2019-09-06 12:54:12 +03:00
} ;
if has_idle {
2019-11-07 19:12:06 +02:00
idle ( kit ) . ok ( ) . take ( ) ;
2019-09-06 12:54:12 +03:00
} else {
2019-11-07 19:12:06 +02:00
poll_with_examine ( kit ) . ok ( ) . take ( ) ;
2019-09-06 12:54:12 +03:00
}
} ) ? ;
2019-09-11 17:57:55 +03:00
Ok ( handle . thread ( ) . id ( ) )
2018-07-13 18:38:57 +03:00
}
2019-06-28 19:34:40 +03:00
2020-02-26 10:54:10 +02:00
fn mailboxes ( & self ) -> Result < FnvHashMap < MailboxHash , Mailbox > > {
2019-11-18 12:59:04 +02:00
{
2020-02-26 10:54:10 +02:00
let mailboxes = self . mailboxes . read ( ) . unwrap ( ) ;
if ! mailboxes . is_empty ( ) {
return Ok ( mailboxes
2019-11-18 12:59:04 +02:00
. iter ( )
2020-02-26 10:54:10 +02:00
. map ( | ( h , f ) | ( * h , Box ::new ( Clone ::clone ( f ) ) as Mailbox ) )
2019-11-23 17:47:24 +02:00
. collect ( ) ) ;
2019-11-18 12:59:04 +02:00
}
2019-06-28 19:34:40 +03:00
}
2020-02-26 10:54:10 +02:00
let mut mailboxes = self . mailboxes . write ( ) ? ;
* mailboxes = ImapType ::imap_mailboxes ( & self . connection ) ? ;
mailboxes . retain ( | _ , f | ( self . is_subscribed ) ( f . path ( ) ) ) ;
let keys = mailboxes
. keys ( )
. cloned ( )
. collect ::< FnvHashSet < MailboxHash > > ( ) ;
2019-11-27 01:45:10 +02:00
let mut uid_lock = self . uid_store . uidvalidity . lock ( ) . unwrap ( ) ;
2020-02-26 10:54:10 +02:00
for f in mailboxes . values_mut ( ) {
2019-11-27 01:45:10 +02:00
uid_lock . entry ( f . hash ( ) ) . or_default ( ) ;
2019-10-24 20:30:17 +03:00
f . children . retain ( | c | keys . contains ( c ) ) ;
2019-06-28 19:34:40 +03:00
}
2019-11-27 01:45:10 +02:00
drop ( uid_lock ) ;
2020-02-26 10:54:10 +02:00
Ok ( mailboxes
2019-06-28 19:34:40 +03:00
. iter ( )
2019-12-18 15:44:44 +02:00
. filter ( | ( _ , f ) | f . is_subscribed )
2020-02-26 10:54:10 +02:00
. map ( | ( h , f ) | ( * h , Box ::new ( Clone ::clone ( f ) ) as Mailbox ) )
2019-11-23 17:47:24 +02:00
. collect ( ) )
2018-07-13 18:38:57 +03:00
}
2019-06-28 19:34:40 +03:00
2019-11-03 13:12:28 +02:00
fn operation ( & self , hash : EnvelopeHash ) -> Box < dyn BackendOp > {
2020-02-26 10:54:10 +02:00
let ( uid , mailbox_hash ) = self . uid_store . hash_index . lock ( ) . unwrap ( ) [ & hash ] ;
2019-06-28 19:34:40 +03:00
Box ::new ( ImapOp ::new (
uid ,
2020-02-26 10:54:10 +02:00
self . mailboxes . read ( ) . unwrap ( ) [ & mailbox_hash ]
2020-02-06 01:49:18 +02:00
. imap_path ( )
2019-10-24 20:30:17 +03:00
. to_string ( ) ,
2019-06-28 19:34:40 +03:00
self . connection . clone ( ) ,
2019-11-10 23:02:23 +02:00
self . uid_store . clone ( ) ,
2019-12-08 10:57:36 +02:00
self . tag_index . clone ( ) ,
2019-06-28 19:34:40 +03:00
) )
2018-07-13 18:38:57 +03:00
}
2019-06-28 19:34:40 +03:00
2020-02-26 10:54:10 +02:00
fn save ( & self , bytes : & [ u8 ] , mailbox : & str , flags : Option < Flag > ) -> Result < ( ) > {
2019-10-24 20:30:17 +03:00
let path = {
2020-02-26 10:54:10 +02:00
let mailboxes = self . mailboxes . read ( ) . unwrap ( ) ;
2019-10-24 20:30:17 +03:00
2020-02-26 10:54:10 +02:00
let f_result = mailboxes
2019-11-27 14:22:53 +02:00
. values ( )
2020-02-26 10:54:10 +02:00
. find ( | v | v . path = = mailbox | | v . name = = mailbox ) ;
2019-11-11 00:47:23 +02:00
if f_result
. map ( | f | ! f . permissions . lock ( ) . unwrap ( ) . create_messages )
. unwrap_or ( false )
{
return Err ( MeliError ::new ( format! (
2020-02-26 10:54:10 +02:00
" You are not allowed to create messages in mailbox {} " ,
mailbox
2019-11-11 00:47:23 +02:00
) ) ) ;
}
f_result
2020-02-06 01:49:18 +02:00
. map ( | v | v . imap_path ( ) . to_string ( ) )
2019-11-11 00:47:23 +02:00
. ok_or ( MeliError ::new ( format! (
2020-02-26 10:54:10 +02:00
" Mailbox with name {} not found. " ,
mailbox
2019-11-11 00:47:23 +02:00
) ) ) ?
2019-10-24 20:30:17 +03:00
} ;
2019-06-28 19:34:40 +03:00
let mut response = String ::with_capacity ( 8 * 1024 ) ;
let mut conn = self . connection . lock ( ) . unwrap ( ) ;
let flags = flags . unwrap_or ( Flag ::empty ( ) ) ;
conn . send_command (
format! (
" APPEND \" {} \" ({}) {{{}}} " ,
2019-10-24 20:30:17 +03:00
& path ,
2019-06-28 19:34:40 +03:00
flags_to_imap_list! ( flags ) ,
bytes . len ( )
)
. as_bytes ( ) ,
) ? ;
// wait for "+ Ready for literal data" reply
conn . wait_for_continuation_request ( ) ? ;
conn . send_literal ( bytes ) ? ;
conn . read_response ( & mut response ) ? ;
Ok ( ( ) )
2018-07-13 18:38:57 +03:00
}
2019-08-26 20:04:02 +03:00
2019-11-06 14:53:12 +02:00
fn as_any ( & self ) -> & dyn ::std ::any ::Any {
self
}
2019-12-02 01:09:23 +02:00
2019-12-11 00:07:47 +02:00
fn as_any_mut ( & mut self ) -> & mut dyn ::std ::any ::Any {
self
}
2019-12-02 01:09:23 +02:00
fn tags ( & self ) -> Option < Arc < RwLock < BTreeMap < u64 , String > > > > {
if * self . can_create_flags . lock ( ) . unwrap ( ) {
Some ( self . tag_index . clone ( ) )
} else {
None
}
}
2020-01-15 12:31:49 +02:00
2020-02-26 10:54:10 +02:00
fn create_mailbox (
2020-02-22 10:57:59 +02:00
& mut self ,
mut path : String ,
2020-02-26 10:54:10 +02:00
) -> Result < ( MailboxHash , FnvHashMap < MailboxHash , Mailbox > ) > {
2020-02-06 01:49:18 +02:00
/* Must transform path to something the IMAP server will accept
*
* Each root mailbox has a hierarchy delimeter reported by the LIST entry . All paths
* must use this delimeter to indicate children of this mailbox .
*
* A new root mailbox should have the default delimeter , which can be found out by issuing
* an empty LIST command as described in RFC3501 :
* C : A101 LIST " " " "
* S : * LIST ( \ Noselect ) " / " " "
*
* The default delimiter for us is '/' just like UNIX paths . I apologise if this
* decision is unpleasant for you .
* /
2020-02-26 10:54:10 +02:00
let mut mailboxes = self . mailboxes . write ( ) . unwrap ( ) ;
for root_mailbox in mailboxes . values ( ) . filter ( | f | f . parent . is_none ( ) ) {
if path . starts_with ( & root_mailbox . name ) {
debug! ( " path starts with {:?} " , & root_mailbox ) ;
2020-02-06 01:49:18 +02:00
path = path . replace (
'/' ,
2020-02-26 10:54:10 +02:00
( root_mailbox . separator as char ) . encode_utf8 ( & mut [ 0 ; 4 ] ) ,
2020-02-06 01:49:18 +02:00
) ;
break ;
}
}
2020-02-26 10:54:10 +02:00
if mailboxes . values ( ) . any ( | f | f . path = = path ) {
2020-01-15 12:31:49 +02:00
return Err ( MeliError ::new ( format! (
2020-02-26 10:54:10 +02:00
" Mailbox named `{}` in account `{}` already exists. " ,
2020-01-15 12:31:49 +02:00
path , self . account_name ,
) ) ) ;
}
2020-02-06 01:49:18 +02:00
let mut response = String ::with_capacity ( 8 * 1024 ) ;
{
let mut conn_lck = self . connection . lock ( ) ? ;
conn_lck . send_command ( format! ( " CREATE \" {} \" " , path , ) . as_bytes ( ) ) ? ;
conn_lck . read_response ( & mut response ) ? ;
conn_lck . send_command ( format! ( " SUBSCRIBE \" {} \" " , path , ) . as_bytes ( ) ) ? ;
conn_lck . read_response ( & mut response ) ? ;
}
let ret : Result < ( ) > = ImapResponse ::from ( & response ) . into ( ) ;
ret ? ;
2020-02-22 10:57:59 +02:00
let new_hash = get_path_hash! ( path . as_str ( ) ) ;
2020-02-26 10:54:10 +02:00
mailboxes . clear ( ) ;
drop ( mailboxes ) ;
Ok ( ( new_hash , self . mailboxes ( ) . map_err ( | err | MeliError ::new ( format! ( " Mailbox create was succesful (returned ` {} `) but listing mailboxes afterwards returned ` {} ` " , response , err ) ) ) ? ) )
2020-02-06 01:49:18 +02:00
}
2020-02-26 10:54:10 +02:00
fn delete_mailbox (
& mut self ,
mailbox_hash : MailboxHash ,
) -> Result < FnvHashMap < MailboxHash , Mailbox > > {
let mut mailboxes = self . mailboxes . write ( ) . unwrap ( ) ;
let permissions = mailboxes [ & mailbox_hash ] . permissions ( ) ;
2020-02-06 01:49:18 +02:00
if ! permissions . delete_mailbox {
2020-02-26 10:54:10 +02:00
return Err ( MeliError ::new ( format! ( " You do not have permission to delete ` {} `. Set permissions for this mailbox are {} " , mailboxes [ & mailbox_hash ] . name ( ) , permissions ) ) ) ;
2020-02-06 01:49:18 +02:00
}
let mut response = String ::with_capacity ( 8 * 1024 ) ;
{
let mut conn_lck = self . connection . lock ( ) ? ;
2020-02-26 10:54:10 +02:00
if ! mailboxes [ & mailbox_hash ] . no_select {
2020-02-06 01:49:18 +02:00
/* make sure mailbox is not selected before it gets deleted, otherwise
* connection gets dropped by server * /
if conn_lck
. capabilities
. iter ( )
. any ( | cap | cap . eq_ignore_ascii_case ( b " UNSELECT " ) )
{
conn_lck . send_command (
2020-02-26 10:54:10 +02:00
format! ( " UNSELECT \" {} \" " , mailboxes [ & mailbox_hash ] . imap_path ( ) ) . as_bytes ( ) ,
2020-02-06 01:49:18 +02:00
) ? ;
conn_lck . read_response ( & mut response ) ? ;
} else {
conn_lck . send_command (
2020-02-26 10:54:10 +02:00
format! ( " SELECT \" {} \" " , mailboxes [ & mailbox_hash ] . imap_path ( ) ) . as_bytes ( ) ,
2020-02-06 01:49:18 +02:00
) ? ;
conn_lck . read_response ( & mut response ) ? ;
conn_lck . send_command (
2020-02-26 10:54:10 +02:00
format! ( " EXAMINE \" {} \" " , mailboxes [ & mailbox_hash ] . imap_path ( ) ) . as_bytes ( ) ,
2020-02-06 01:49:18 +02:00
) ? ;
conn_lck . read_response ( & mut response ) ? ;
}
}
2020-02-26 10:54:10 +02:00
if mailboxes [ & mailbox_hash ] . is_subscribed ( ) {
2020-02-22 11:22:11 +02:00
conn_lck . send_command (
2020-02-26 10:54:10 +02:00
format! ( " UNSUBSCRIBE \" {} \" " , mailboxes [ & mailbox_hash ] . imap_path ( ) ) . as_bytes ( ) ,
2020-02-22 11:22:11 +02:00
) ? ;
conn_lck . read_response ( & mut response ) ? ;
}
2020-02-06 01:49:18 +02:00
conn_lck . send_command (
2020-02-26 10:54:10 +02:00
debug! ( format! (
" DELETE \" {} \" " ,
mailboxes [ & mailbox_hash ] . imap_path ( )
) )
. as_bytes ( ) ,
2020-02-06 01:49:18 +02:00
) ? ;
conn_lck . read_response ( & mut response ) ? ;
}
let ret : Result < ( ) > = ImapResponse ::from ( & response ) . into ( ) ;
2020-02-22 10:57:59 +02:00
ret ? ;
2020-02-26 10:54:10 +02:00
mailboxes . clear ( ) ;
drop ( mailboxes ) ;
self . mailboxes ( ) . map_err ( | err | format! ( " Mailbox delete was succesful (returned ` {} `) but listing mailboxes afterwards returned ` {} ` " , response , err ) . into ( ) )
2020-02-06 01:49:18 +02:00
}
2020-02-26 10:54:10 +02:00
fn set_mailbox_subscription ( & mut self , mailbox_hash : MailboxHash , new_val : bool ) -> Result < ( ) > {
let mut mailboxes = self . mailboxes . write ( ) . unwrap ( ) ;
if mailboxes [ & mailbox_hash ] . is_subscribed ( ) = = new_val {
2020-02-06 01:49:18 +02:00
return Ok ( ( ) ) ;
}
let mut response = String ::with_capacity ( 8 * 1024 ) ;
{
let mut conn_lck = self . connection . lock ( ) ? ;
if new_val {
conn_lck . send_command (
2020-02-26 10:54:10 +02:00
format! ( " SUBSCRIBE \" {} \" " , mailboxes [ & mailbox_hash ] . imap_path ( ) ) . as_bytes ( ) ,
2020-02-06 01:49:18 +02:00
) ? ;
} else {
conn_lck . send_command (
2020-02-26 10:54:10 +02:00
format! ( " UNSUBSCRIBE \" {} \" " , mailboxes [ & mailbox_hash ] . imap_path ( ) ) . as_bytes ( ) ,
2020-02-06 01:49:18 +02:00
) ? ;
}
conn_lck . read_response ( & mut response ) ? ;
}
let ret : Result < ( ) > = ImapResponse ::from ( & response ) . into ( ) ;
if ret . is_ok ( ) {
2020-02-26 10:54:10 +02:00
mailboxes . entry ( mailbox_hash ) . and_modify ( | entry | {
2020-02-06 01:49:18 +02:00
let _ = entry . set_is_subscribed ( new_val ) ;
} ) ;
}
ret
}
2020-02-26 10:54:10 +02:00
fn rename_mailbox (
& mut self ,
mailbox_hash : MailboxHash ,
mut new_path : String ,
) -> Result < Mailbox > {
let mut mailboxes = self . mailboxes . write ( ) . unwrap ( ) ;
let permissions = mailboxes [ & mailbox_hash ] . permissions ( ) ;
2020-02-06 01:49:18 +02:00
if ! permissions . delete_mailbox {
2020-02-26 10:54:10 +02:00
return Err ( MeliError ::new ( format! ( " You do not have permission to rename mailbox ` {} ` (rename is equivalent to delete + create). Set permissions for this mailbox are {} " , mailboxes [ & mailbox_hash ] . name ( ) , permissions ) ) ) ;
2020-02-06 01:49:18 +02:00
}
let mut response = String ::with_capacity ( 8 * 1024 ) ;
2020-02-26 10:54:10 +02:00
if mailboxes [ & mailbox_hash ] . separator ! = b '/' {
2020-02-06 01:49:18 +02:00
new_path = new_path . replace (
'/' ,
2020-02-26 10:54:10 +02:00
( mailboxes [ & mailbox_hash ] . separator as char ) . encode_utf8 ( & mut [ 0 ; 4 ] ) ,
2020-02-06 01:49:18 +02:00
) ;
}
{
let mut conn_lck = self . connection . lock ( ) ? ;
conn_lck . send_command (
debug! ( format! (
" RENAME \" {} \" \" {} \" " ,
2020-02-26 10:54:10 +02:00
mailboxes [ & mailbox_hash ] . imap_path ( ) ,
2020-02-06 01:49:18 +02:00
new_path
2020-01-15 12:31:49 +02:00
) )
2020-02-06 01:49:18 +02:00
. as_bytes ( ) ,
) ? ;
conn_lck . read_response ( & mut response ) ? ;
}
let new_hash = get_path_hash! ( new_path . as_str ( ) ) ;
let ret : Result < ( ) > = ImapResponse ::from ( & response ) . into ( ) ;
ret ? ;
2020-02-26 10:54:10 +02:00
mailboxes . clear ( ) ;
drop ( mailboxes ) ;
self . mailboxes ( ) . map_err ( | err | format! ( " Mailbox rename was succesful (returned ` {} `) but listing mailboxes afterwards returned ` {} ` " , response , err ) ) ? ;
Ok ( BackendMailbox ::clone (
& self . mailboxes . read ( ) . unwrap ( ) [ & new_hash ] ,
2020-02-06 01:49:18 +02:00
) )
}
2020-02-26 10:54:10 +02:00
fn set_mailbox_permissions (
2020-02-06 01:49:18 +02:00
& mut self ,
2020-02-26 10:54:10 +02:00
mailbox_hash : MailboxHash ,
_val : crate ::backends ::MailboxPermissions ,
2020-02-06 01:49:18 +02:00
) -> Result < ( ) > {
2020-02-26 10:54:10 +02:00
let mailboxes = self . mailboxes . write ( ) . unwrap ( ) ;
let permissions = mailboxes [ & mailbox_hash ] . permissions ( ) ;
2020-02-06 01:49:18 +02:00
if ! permissions . change_permissions {
2020-02-26 10:54:10 +02:00
return Err ( MeliError ::new ( format! ( " You do not have permission to change permissions for mailbox ` {} `. Set permissions for this mailbox are {} " , mailboxes [ & mailbox_hash ] . name ( ) , permissions ) ) ) ;
2020-02-06 01:49:18 +02:00
}
Err ( MeliError ::new ( " Unimplemented. " ) )
2020-01-15 12:31:49 +02:00
}
2018-08-11 18:00:21 +03:00
}
2018-07-13 18:38:57 +03:00
impl ImapType {
2019-10-24 20:30:17 +03:00
pub fn new (
s : & AccountSettings ,
is_subscribed : Box < dyn Fn ( & str ) -> bool + Send + Sync > ,
2019-11-16 00:33:22 +02:00
) -> Result < Box < dyn MailBackend > > {
let server_hostname = get_conf_val! ( s [ " server_hostname " ] ) ? ;
let server_username = get_conf_val! ( s [ " server_username " ] ) ? ;
2020-03-12 09:45:18 +02:00
let server_password = if ! s . extra . contains_key ( " server_password_command " ) {
get_conf_val! ( s [ " server_password " ] ) ? . to_string ( )
} else {
let invocation = get_conf_val! ( s [ " server_password_command " ] ) ?
. split_whitespace ( )
. collect ::< Vec < & str > > ( ) ;
let output = std ::process ::Command ::new ( invocation [ 0 ] )
. args ( & invocation [ 1 .. ] )
. stdin ( std ::process ::Stdio ::piped ( ) )
. stdout ( std ::process ::Stdio ::piped ( ) )
. stderr ( std ::process ::Stdio ::piped ( ) )
. output ( ) ? ;
if ! output . status . success ( ) {
return Err ( MeliError ::new ( format! (
" ({}) server_password_command `{}` returned {}: {} " ,
s . name ,
get_conf_val! ( s [ " server_password_command " ] ) ? ,
output . status ,
String ::from_utf8_lossy ( & output . stderr )
) ) ) ;
}
std ::str ::from_utf8 ( & output . stdout ) ? . trim_end ( ) . to_string ( )
} ;
2019-11-16 00:33:22 +02:00
let server_port = get_conf_val! ( s [ " server_port " ] , 143 ) ? ;
2020-02-06 01:50:15 +02:00
let use_starttls = get_conf_val! ( s [ " use_starttls " ] , ! ( server_port = = 993 ) ) ? ;
2019-08-24 15:12:44 +03:00
let danger_accept_invalid_certs : bool =
2019-11-16 00:33:22 +02:00
get_conf_val! ( s [ " danger_accept_invalid_certs " ] , false ) ? ;
2019-09-21 21:02:35 +03:00
let server_conf = ImapServerConf {
server_hostname : server_hostname . to_string ( ) ,
server_username : server_username . to_string ( ) ,
2020-03-12 09:45:18 +02:00
server_password ,
2019-09-21 21:02:35 +03:00
server_port ,
use_starttls ,
2019-09-20 09:07:55 +03:00
danger_accept_invalid_certs ,
2020-02-28 15:47:07 +02:00
protocol : ImapProtocol ::IMAP ,
2019-09-21 21:02:35 +03:00
} ;
2019-12-14 18:46:12 +02:00
let online = Arc ::new ( Mutex ::new ( (
Instant ::now ( ) ,
Err ( MeliError ::new ( " Account is uninitialised. " ) ) ,
) ) ) ;
let connection = ImapConnection ::new_connection ( & server_conf , online . clone ( ) ) ;
2019-06-28 19:34:40 +03:00
2019-11-16 00:33:22 +02:00
Ok ( Box ::new ( ImapType {
2019-06-28 19:34:40 +03:00
account_name : s . name ( ) . to_string ( ) ,
2019-12-14 18:46:12 +02:00
online ,
2019-09-21 21:02:35 +03:00
server_conf ,
2019-10-24 20:30:17 +03:00
is_subscribed : Arc ::new ( IsSubscribedFn ( is_subscribed ) ) ,
2019-09-21 21:02:35 +03:00
2019-12-02 01:09:23 +02:00
can_create_flags : Arc ::new ( Mutex ::new ( false ) ) ,
tag_index : Arc ::new ( RwLock ::new ( Default ::default ( ) ) ) ,
2020-02-26 10:54:10 +02:00
mailboxes : Arc ::new ( RwLock ::new ( Default ::default ( ) ) ) ,
2019-09-14 15:59:53 +03:00
connection : Arc ::new ( Mutex ::new ( connection ) ) ,
2019-11-10 23:02:23 +02:00
uid_store : Arc ::new ( UIDStore {
uidvalidity : Default ::default ( ) ,
hash_index : Default ::default ( ) ,
uid_index : Default ::default ( ) ,
byte_cache : Default ::default ( ) ,
} ) ,
2019-11-16 00:33:22 +02:00
} ) )
2019-06-28 19:34:40 +03:00
}
pub fn shell ( & mut self ) {
2019-12-14 18:46:12 +02:00
let mut conn = ImapConnection ::new_connection ( & self . server_conf , self . online . clone ( ) ) ;
2020-02-22 11:22:11 +02:00
conn . connect ( ) . unwrap ( ) ;
2019-06-28 19:34:40 +03:00
let mut res = String ::with_capacity ( 8 * 1024 ) ;
2019-12-11 00:07:47 +02:00
conn . send_command ( b " NOOP " ) . unwrap ( ) ;
2019-09-14 15:59:53 +03:00
conn . read_response ( & mut res ) . unwrap ( ) ;
2019-06-28 19:34:40 +03:00
let mut input = String ::new ( ) ;
loop {
use std ::io ;
input . clear ( ) ;
match io ::stdin ( ) . read_line ( & mut input ) {
Ok ( _ ) = > {
2019-12-11 00:07:47 +02:00
if input . trim ( ) . eq_ignore_ascii_case ( " logout " ) {
break ;
}
2019-06-28 19:34:40 +03:00
conn . send_command ( input . as_bytes ( ) ) . unwrap ( ) ;
2019-09-14 15:57:28 +03:00
conn . read_lines ( & mut res , String ::new ( ) ) . unwrap ( ) ;
if input . trim ( ) = = " IDLE " {
let mut iter = ImapBlockingConnection ::from ( conn ) ;
while let Some ( line ) = iter . next ( ) {
debug! ( " out: {} " , unsafe { std ::str ::from_utf8_unchecked ( & line ) } ) ;
}
conn = iter . into_conn ( ) ;
}
2019-06-28 19:34:40 +03:00
debug! ( " out: {} " , & res ) ;
}
Err ( error ) = > debug! ( " error: {} " , error ) ,
}
}
2018-07-13 18:38:57 +03:00
}
2019-06-28 19:34:40 +03:00
2020-02-26 10:54:10 +02:00
pub fn imap_mailboxes (
2019-10-24 20:30:17 +03:00
connection : & Arc < Mutex < ImapConnection > > ,
2020-02-26 10:54:10 +02:00
) -> Result < FnvHashMap < MailboxHash , ImapMailbox > > {
let mut mailboxes : FnvHashMap < MailboxHash , ImapMailbox > = Default ::default ( ) ;
2019-06-28 19:34:40 +03:00
let mut res = String ::with_capacity ( 8 * 1024 ) ;
2019-10-24 20:30:17 +03:00
let mut conn = connection . lock ( ) . unwrap ( ) ;
2019-12-11 01:36:04 +02:00
conn . send_command ( b " LIST \" \" \" * \" " ) ? ;
conn . read_response ( & mut res ) ? ;
2019-06-28 19:34:40 +03:00
debug! ( " out: {} " , & res ) ;
2020-01-27 15:55:01 +02:00
let mut lines = res . lines ( ) ;
/* Remove "M__ OK .." line */
lines . next_back ( ) ;
for l in lines . map ( | l | l . trim ( ) ) {
2020-02-26 10:54:10 +02:00
if let Ok ( mut mailbox ) =
protocol_parser ::list_mailbox_result ( l . as_bytes ( ) ) . to_full_result ( )
2019-06-28 19:34:40 +03:00
{
2020-02-26 10:54:10 +02:00
if let Some ( parent ) = mailbox . parent {
if mailboxes . contains_key ( & parent ) {
mailboxes
2019-06-28 19:34:40 +03:00
. entry ( parent )
2020-02-26 10:54:10 +02:00
. and_modify ( | e | e . children . push ( mailbox . hash ) ) ;
2019-06-28 19:34:40 +03:00
} else {
/* Insert dummy parent entry, populating only the children field. Later
* when we encounter the parent entry we will swap its children with
* dummy ' s * /
2020-02-26 10:54:10 +02:00
mailboxes . insert (
2019-06-28 19:34:40 +03:00
parent ,
2020-02-26 10:54:10 +02:00
ImapMailbox {
children : vec ! [ mailbox . hash ] ,
.. ImapMailbox ::default ( )
2019-06-28 19:34:40 +03:00
} ,
) ;
}
}
2020-02-26 10:54:10 +02:00
if mailboxes . contains_key ( & mailbox . hash ) {
let entry = mailboxes . entry ( mailbox . hash ) . or_default ( ) ;
std ::mem ::swap ( & mut entry . children , & mut mailbox . children ) ;
* entry = mailbox ;
2019-06-28 19:34:40 +03:00
} else {
2020-02-26 10:54:10 +02:00
mailboxes . insert ( mailbox . hash , mailbox ) ;
2019-06-28 19:34:40 +03:00
}
} else {
debug! ( " parse error for {:?} " , l ) ;
2019-12-18 15:44:44 +02:00
}
}
conn . send_command ( b " LSUB \" \" \" * \" " ) ? ;
conn . read_response ( & mut res ) ? ;
debug! ( " out: {} " , & res ) ;
2020-01-27 15:55:01 +02:00
let mut lines = res . lines ( ) ;
/* Remove "M__ OK .." line */
lines . next_back ( ) ;
for l in lines . map ( | l | l . trim ( ) ) {
2019-12-18 15:44:44 +02:00
if let Ok ( subscription ) =
2020-02-26 10:54:10 +02:00
protocol_parser ::list_mailbox_result ( l . as_bytes ( ) ) . to_full_result ( )
2019-12-18 15:44:44 +02:00
{
2020-02-26 10:54:10 +02:00
if let Some ( f ) = mailboxes . get_mut ( & subscription . hash ( ) ) {
2019-12-18 15:44:44 +02:00
if subscription . no_select {
continue ;
}
f . is_subscribed = true ;
}
} else {
debug! ( " parse error for {:?} " , l ) ;
2019-06-28 19:34:40 +03:00
}
}
2020-02-26 10:54:10 +02:00
Ok ( debug! ( mailboxes ) )
2019-06-28 19:34:40 +03:00
}
2019-11-06 15:11:35 +02:00
pub fn capabilities ( & self ) -> Vec < String > {
self . connection
. lock ( )
. unwrap ( )
. capabilities
. iter ( )
. map ( | c | String ::from_utf8_lossy ( c ) . into ( ) )
. collect ::< Vec < String > > ( )
}
2019-11-07 20:26:56 +02:00
2019-11-07 22:34:24 +02:00
pub fn search (
& self ,
query : String ,
2020-02-26 10:54:10 +02:00
mailbox_hash : MailboxHash ,
2020-01-08 17:04:44 +02:00
) -> Result < SmallVec < [ EnvelopeHash ; 512 ] > > {
2020-02-26 10:54:10 +02:00
let mailboxes_lck = self . mailboxes . read ( ) ? ;
2019-11-07 20:26:56 +02:00
let mut response = String ::with_capacity ( 8 * 1024 ) ;
let mut conn = self . connection . lock ( ) ? ;
2020-02-06 01:49:18 +02:00
conn . send_command (
2020-02-26 10:54:10 +02:00
format! ( " EXAMINE \" {} \" " , mailboxes_lck [ & mailbox_hash ] . imap_path ( ) ) . as_bytes ( ) ,
2020-02-06 01:49:18 +02:00
) ? ;
2019-11-07 22:34:24 +02:00
conn . read_response ( & mut response ) ? ;
2019-11-07 20:26:56 +02:00
conn . send_command ( format! ( " UID SEARCH CHARSET UTF-8 {} " , query ) . as_bytes ( ) ) ? ;
conn . read_response ( & mut response ) ? ;
2019-11-07 22:34:24 +02:00
debug! ( & response ) ;
2019-11-07 20:26:56 +02:00
let mut lines = response . lines ( ) ;
for l in lines . by_ref ( ) {
if l . starts_with ( " * SEARCH " ) {
use std ::iter ::FromIterator ;
2019-11-10 23:02:23 +02:00
let uid_index = self . uid_store . uid_index . lock ( ) ? ;
2020-01-08 17:04:44 +02:00
return Ok ( SmallVec ::from_iter (
2019-11-07 20:26:56 +02:00
l [ " * SEARCH " . len ( ) .. ]
. trim ( )
. split_whitespace ( )
. map ( usize ::from_str )
. filter_map ( std ::result ::Result ::ok )
. filter_map ( | uid | uid_index . get ( & uid ) )
. map ( | env_hash_ref | * env_hash_ref ) ,
) ) ;
}
}
Err ( MeliError ::new ( response ) )
}
2019-11-27 14:22:53 +02:00
pub fn validate_config ( s : & AccountSettings ) -> Result < ( ) > {
get_conf_val! ( s [ " server_hostname " ] ) ? ;
get_conf_val! ( s [ " server_username " ] ) ? ;
2020-03-12 09:45:18 +02:00
if ! s . extra . contains_key ( " server_password_command " ) {
get_conf_val! ( s [ " server_password " ] ) ? ;
} else if s . extra . contains_key ( " server_password " ) {
return Err ( MeliError ::new ( format! (
" Configuration error ({}): both server_password and server_password_command are set, cannot choose " ,
s . name . as_str ( ) ,
) ) ) ;
}
2019-11-27 14:22:53 +02:00
get_conf_val! ( s [ " server_port " ] , 143 ) ? ;
get_conf_val! ( s [ " use_starttls " ] , false ) ? ;
get_conf_val! ( s [ " danger_accept_invalid_certs " ] , false ) ? ;
Ok ( ( ) )
}
2019-06-28 19:34:40 +03:00
}