Changing IMAPMailbox to Mailbox

This commit is contained in:
Matt McCoy 2016-06-21 15:46:43 -04:00
parent 7f92a1429e
commit fb89e4a50a
2 changed files with 21 additions and 16 deletions

View file

@ -2,8 +2,7 @@ extern crate imap;
extern crate openssl; extern crate openssl;
use openssl::ssl::{SslContext, SslMethod}; use openssl::ssl::{SslContext, SslMethod};
use imap::client::Client; use imap::client::{Client, Mailbox};
use imap::client::IMAPMailbox;
fn main() { fn main() {
let mut imap_socket = match Client::secure_connect(("imap.gmail.com", 993), SslContext::new(SslMethod::Sslv23).unwrap()) { let mut imap_socket = match Client::secure_connect(("imap.gmail.com", 993), SslContext::new(SslMethod::Sslv23).unwrap()) {
@ -25,7 +24,7 @@ fn main() {
}; };
match imap_socket.select("INBOX") { match imap_socket.select("INBOX") {
Ok(IMAPMailbox{flags, exists, recent, unseen, permanent_flags, uid_next, uid_validity}) => { Ok(Mailbox{flags, exists, recent, unseen, permanent_flags, uid_next, uid_validity}) => {
println!("flags: {}, exists: {}, recent: {}, unseen: {:?}, permanent_flags: {:?}, uid_next: {:?}, uid_validity: {:?}", flags, exists, recent, unseen, permanent_flags, uid_next, uid_validity); println!("flags: {}, exists: {}, recent: {}, unseen: {:?}, permanent_flags: {:?}, uid_next: {:?}, uid_validity: {:?}", flags, exists, recent, unseen, permanent_flags, uid_next, uid_validity);
}, },
Err(_) => println!("Error selecting INBOX") Err(_) => println!("Error selecting INBOX")

View file

@ -12,7 +12,7 @@ pub struct Client<T> {
tag: u32 tag: u32
} }
pub struct IMAPMailbox { pub struct Mailbox {
pub flags: String, pub flags: String,
pub exists: u32, pub exists: u32,
pub recent: u32, pub recent: u32,
@ -22,6 +22,20 @@ pub struct IMAPMailbox {
pub uid_validity: Option<u32> pub uid_validity: Option<u32>
} }
impl Default for Mailbox {
fn default() -> Mailbox {
Mailbox {
flags: "".to_string(),
exists: 0,
recent: 0,
unseen: None,
permanent_flags: None,
uid_next: None,
uid_validity: None
}
}
}
impl Client<TcpStream> { impl Client<TcpStream> {
/// Creates a new client. /// Creates a new client.
pub fn connect<A: ToSocketAddrs>(addr: A) -> Result<Client<TcpStream>> { pub fn connect<A: ToSocketAddrs>(addr: A) -> Result<Client<TcpStream>> {
@ -66,14 +80,14 @@ impl<T: Read+Write> Client<T> {
} }
/// Selects a mailbox /// Selects a mailbox
pub fn select(&mut self, mailbox_name: &str) -> Result<IMAPMailbox> { pub fn select(&mut self, mailbox_name: &str) -> Result<Mailbox> {
match self.run_command(&format!("SELECT {}", mailbox_name).to_string()) { match self.run_command(&format!("SELECT {}", mailbox_name).to_string()) {
Ok(lines) => self.parse_select_or_examine(lines), Ok(lines) => self.parse_select_or_examine(lines),
Err(e) => Err(e) Err(e) => Err(e)
} }
} }
fn parse_select_or_examine(&mut self, lines: Vec<String>) -> Result<IMAPMailbox> { fn parse_select_or_examine(&mut self, lines: Vec<String>) -> Result<Mailbox> {
let exists_regex = Regex::new(r"^\* (\d+) EXISTS\r\n").unwrap(); let exists_regex = Regex::new(r"^\* (\d+) EXISTS\r\n").unwrap();
let recent_regex = Regex::new(r"^\* (\d+) RECENT\r\n").unwrap(); let recent_regex = Regex::new(r"^\* (\d+) RECENT\r\n").unwrap();
@ -94,15 +108,7 @@ impl<T: Read+Write> Client<T> {
Err(e) => return Err(e) Err(e) => return Err(e)
}; };
let mut mailbox = IMAPMailbox{ let mut mailbox = Mailbox::default();
flags: "".to_string(),
exists: 0,
recent: 0,
unseen: None,
permanent_flags: None,
uid_next: None,
uid_validity: None
};
for line in lines.iter() { for line in lines.iter() {
if exists_regex.is_match(line) { if exists_regex.is_match(line) {
@ -133,7 +139,7 @@ impl<T: Read+Write> Client<T> {
} }
/// Examine is identical to Select, but the selected mailbox is identified as read-only /// Examine is identical to Select, but the selected mailbox is identified as read-only
pub fn examine(&mut self, mailbox_name: &str) -> Result<IMAPMailbox> { pub fn examine(&mut self, mailbox_name: &str) -> Result<Mailbox> {
match self.run_command(&format!("EXAMINE {}", mailbox_name).to_string()) { match self.run_command(&format!("EXAMINE {}", mailbox_name).to_string()) {
Ok(lines) => self.parse_select_or_examine(lines), Ok(lines) => self.parse_select_or_examine(lines),
Err(e) => Err(e) Err(e) => Err(e)