Quotes various astring tokens as per RFC 3501

Some notable exceptions: messagebox names are defined as `INBOX / astring`, but with this change, INBOX is also quoted.
The `list-mailbox` token is not quoted, as it is not `astring`.
This commit is contained in:
rhn 2017-11-02 15:48:36 +01:00
parent 13316f33b8
commit 61485c9036

View file

@ -284,18 +284,18 @@ impl<T: Read + Write> Client<T> {
/// Log in to the IMAP server.
pub fn login(&mut self, username: &str, password: &str) -> Result<()> {
self.run_command_and_check_ok(&format!("LOGIN {} {}", username, quote!(password)))
self.run_command_and_check_ok(&format!("LOGIN {} {}", quote!(username), quote!(password)))
}
/// Selects a mailbox
pub fn select(&mut self, mailbox_name: &str) -> Result<Mailbox> {
let lines = try!(self.run_command_and_read_response(&format!("SELECT {}", mailbox_name)));
let lines = try!(self.run_command_and_read_response(&format!("SELECT {}", quote!(mailbox_name))));
parse_select_or_examine(lines)
}
/// Examine is identical to Select, but the selected mailbox is identified as read-only
pub fn examine(&mut self, mailbox_name: &str) -> Result<Mailbox> {
let lines = try!(self.run_command_and_read_response(&format!("EXAMINE {}", mailbox_name)));
let lines = try!(self.run_command_and_read_response(&format!("EXAMINE {}", quote!(mailbox_name))));
parse_select_or_examine(lines)
}
@ -320,33 +320,33 @@ impl<T: Read + Write> Client<T> {
/// Create creates a mailbox with the given name.
pub fn create(&mut self, mailbox_name: &str) -> Result<()> {
self.run_command_and_check_ok(&format!("CREATE {}", mailbox_name))
self.run_command_and_check_ok(&format!("CREATE {}", quote!(mailbox_name)))
}
/// Delete permanently removes the mailbox with the given name.
pub fn delete(&mut self, mailbox_name: &str) -> Result<()> {
self.run_command_and_check_ok(&format!("DELETE {}", mailbox_name))
self.run_command_and_check_ok(&format!("DELETE {}", quote!(mailbox_name)))
}
/// Rename changes the name of a mailbox.
pub fn rename(&mut self, current_mailbox_name: &str, new_mailbox_name: &str) -> Result<()> {
self.run_command_and_check_ok(&format!(
"RENAME {} {}",
current_mailbox_name,
new_mailbox_name
quote!(current_mailbox_name),
quote!(new_mailbox_name)
))
}
/// Subscribe adds the specified mailbox name to the server's set of "active" or "subscribed"
/// mailboxes as returned by the LSUB command.
pub fn subscribe(&mut self, mailbox: &str) -> Result<()> {
self.run_command_and_check_ok(&format!("SUBSCRIBE {}", mailbox))
self.run_command_and_check_ok(&format!("SUBSCRIBE {}", quote!(mailbox)))
}
/// Unsubscribe removes the specified mailbox name from the server's set of
/// "active" or "subscribed mailboxes as returned by the LSUB command.
pub fn unsubscribe(&mut self, mailbox: &str) -> Result<()> {
self.run_command_and_check_ok(&format!("UNSUBSCRIBE {}", mailbox))
self.run_command_and_check_ok(&format!("UNSUBSCRIBE {}", quote!(mailbox)))
}
/// Capability requests a listing of capabilities that the server supports.
@ -399,7 +399,7 @@ impl<T: Read + Write> Client<T> {
) -> Result<Vec<String>> {
self.run_command_and_parse(&format!(
"LIST {} {}",
reference_name,
quote!(reference_name),
mailbox_search_pattern
))
}
@ -413,7 +413,7 @@ impl<T: Read + Write> Client<T> {
) -> Result<Vec<String>> {
self.run_command_and_parse(&format!(
"LSUB {} {}",
reference_name,
quote!(reference_name),
mailbox_search_pattern
))
}
@ -607,7 +607,7 @@ mod tests {
let response = b"a1 OK Logged in\r\n".to_vec();
let username = "username";
let password = "password";
let command = format!("a1 LOGIN {} {}\r\n", username, quote!(password));
let command = format!("a1 LOGIN {} {}\r\n", quote!(username), quote!(password));
let mock_stream = MockStream::new(response);
let mut client = Client::new(mock_stream);
client.login(username, password).unwrap();
@ -637,8 +637,8 @@ mod tests {
let new_mailbox_name = "NEWINBOX";
let command = format!(
"a1 RENAME {} {}\r\n",
current_mailbox_name,
new_mailbox_name
quote!(current_mailbox_name),
quote!(new_mailbox_name)
);
let mock_stream = MockStream::new(response);
let mut client = Client::new(mock_stream);
@ -655,7 +655,7 @@ mod tests {
fn subscribe() {
let response = b"a1 OK SUBSCRIBE completed\r\n".to_vec();
let mailbox = "INBOX";
let command = format!("a1 SUBSCRIBE {}\r\n", mailbox);
let command = format!("a1 SUBSCRIBE {}\r\n", quote!(mailbox));
let mock_stream = MockStream::new(response);
let mut client = Client::new(mock_stream);
client.subscribe(mailbox).unwrap();
@ -669,7 +669,7 @@ mod tests {
fn unsubscribe() {
let response = b"a1 OK UNSUBSCRIBE completed\r\n".to_vec();
let mailbox = "INBOX";
let command = format!("a1 UNSUBSCRIBE {}\r\n", mailbox);
let command = format!("a1 UNSUBSCRIBE {}\r\n", quote!(mailbox));
let mock_stream = MockStream::new(response);
let mut client = Client::new(mock_stream);
client.unsubscribe(mailbox).unwrap();
@ -724,7 +724,7 @@ mod tests {
uid_validity: Some(1257842737),
};
let mailbox_name = "INBOX";
let command = format!("a1 EXAMINE {}\r\n", mailbox_name);
let command = format!("a1 EXAMINE {}\r\n", quote!(mailbox_name));
let mock_stream = MockStream::new(response);
let mut client = Client::new(mock_stream);
let mailbox = client.examine(mailbox_name).unwrap();
@ -758,7 +758,7 @@ mod tests {
uid_validity: Some(1257842737),
};
let mailbox_name = "INBOX";
let command = format!("a1 SELECT {}\r\n", mailbox_name);
let command = format!("a1 SELECT {}\r\n", quote!(mailbox_name));
let mock_stream = MockStream::new(response);
let mut client = Client::new(mock_stream);
let mailbox = client.select(mailbox_name).unwrap();
@ -792,7 +792,7 @@ mod tests {
fn create() {
let response = b"a1 OK CREATE completed\r\n".to_vec();
let mailbox_name = "INBOX";
let command = format!("a1 CREATE {}\r\n", mailbox_name);
let command = format!("a1 CREATE {}\r\n", quote!(mailbox_name));
let mock_stream = MockStream::new(response);
let mut client = Client::new(mock_stream);
client.create(mailbox_name).unwrap();
@ -806,7 +806,7 @@ mod tests {
fn delete() {
let response = b"a1 OK DELETE completed\r\n".to_vec();
let mailbox_name = "INBOX";
let command = format!("a1 DELETE {}\r\n", mailbox_name);
let command = format!("a1 DELETE {}\r\n", quote!(mailbox_name));
let mock_stream = MockStream::new(response);
let mut client = Client::new(mock_stream);
client.delete(mailbox_name).unwrap();
@ -916,4 +916,14 @@ mod tests {
"Invalid command"
);
}
#[test]
fn quote_backslash() {
assert_eq!("\"test\\\\text\"", quote!(r"test\text"));
}
#[test]
fn quote_dquote() {
assert_eq!("\"test\\\"text\"", quote!("test\"text"));
}
}