Reworded unsafe to dangerous/risky. Updated the body parsing bit. Moved warning comments to .login call.

This commit is contained in:
641i130 2022-10-30 12:23:43 -05:00
parent 4b75957a7c
commit 1fac43882d

View file

@ -1,14 +1,14 @@
use std::net::TcpStream; use std::net::TcpStream;
fn main() { fn main() {
// REMINDER this is unsafe, the credentials are sent over the connection in CLEARTEXT // REMINDER this is dangerous, the credentials are sent over the connection in CLEARTEXT!
// Anyone or anything between this connection and the server can read your login creds! // Anyone or anything between this connection and the server can read your login creds!
// Please oh please do not use this where this is even a possibility. // Please oh please do not use this where this is even a possibility.
match plaintext() { match plaintext() {
Ok(conn) => { Ok(conn) => {
eprintln!("Connection successful!"); eprintln!("Connection successful!");
println!("{:?}", conn); println!("{:?}", conn);
}, }
Err(e) => { Err(e) => {
eprintln!("Connection error!"); eprintln!("Connection error!");
eprintln!("{:?}", e); eprintln!("{:?}", e);
@ -17,13 +17,14 @@ fn main() {
} }
fn plaintext() -> imap::error::Result<Option<String>> { fn plaintext() -> imap::error::Result<Option<String>> {
// Make a raw TCP connection to an UNSAFE IMAP server
let stream = TcpStream::connect("imap.example.com:143").unwrap(); // This is unsafe. let stream = TcpStream::connect("imap.example.com:143").unwrap(); // This is unsafe.
let mut client = imap::Client::new(stream); let mut client = imap::Client::new(stream);
client.read_greeting()?; client.read_greeting()?;
eprintln!("\nUNENCRYPTED connection made!!!!\n"); eprintln!("\nUNENCRYPTED connection made!!!!\n");
eprintln!("This is highly not recommended.\n"); eprintln!("This is highly not recommended.\n");
// to do anything useful with the e-mails, we need to log in // to do anything useful with the e-mails, we need to log in
// Makes an unencrypted login message to the IMAP server. This is risky business!
let mut imap_session = client.login("user", "pass").unwrap(); let mut imap_session = client.login("user", "pass").unwrap();
// we want to fetch the first email in the INBOX mailbox // we want to fetch the first email in the INBOX mailbox
@ -39,15 +40,11 @@ fn plaintext() -> imap::error::Result<Option<String>> {
}; };
// extract the message's body // extract the message's body
let mut body; let body = message
match message.body() { .body()
Some(msg) => { .map(|body| String::from_utf8(body).expect("message was not valid utf-8"))
body = std::str::from_utf8(msg) .unwrap_or_else(String::new);
.expect("message was not valid utf-8")
.to_string();
}
None => body = "".to_string(),
}
// be nice to the server and log out // be nice to the server and log out
imap_session.logout()?; imap_session.logout()?;
Ok(Some(body)) Ok(Some(body))