1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#[cfg(feature = "serde_derive")]
#[macro_use]
extern crate serde_derive;

#[cfg(feature = "serde_derive")]
include!("serde_types.in.rs");

#[cfg(feature = "serde_codegen")]
include!(concat!(env!("OUT_DIR"), "/serde_types.rs"));

/******************************************************************/

#[macro_use]
extern crate log;
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate lazy_static;
extern crate openssl;
extern crate regex;

use std::io::BufReader;
use openssl::ssl::{SslMethod, SslConnectorBuilder};
use std::net::TcpStream;
use std::path::PathBuf;
use regex::Regex;

pub mod errors {
    error_chain! {
        foreign_links {
            Io(::std::io::Error);
            SslStack(::openssl::error::ErrorStack);
            SslHandshake(::openssl::ssl::HandshakeError<::std::net::TcpStream>);
            UTF8Error(::std::string::FromUtf8Error);
            RegexError(::regex::Error);
        }
    }
}
use errors::*;

mod tcpstream;
pub mod pop3result;
mod pop3resultimpl;
mod utils;
use tcpstream::TCPStreamType;
use pop3result::{POP3Stat, POP3List, POP3Retr, POP3Uidl};

#[derive(PartialEq)]
#[derive(Debug)]
enum POP3State {
    BEGIN,
    AUTHORIZATION,
    TRANSACTION,
    // UPDATE, // State unused in a Client
    END,
}

pub struct POP3Connection {
    account: AccountConfig,
    stream: TCPStreamType,
    state: POP3State,
    timestamp: String,
}

impl POP3Connection {
    pub fn new(account: AccountConfig) -> Result<POP3Connection> {
        trace!("Initiate POP3 Connection");
        let tcp_stream = TcpStream::connect((&account.host[..], account.port))?;
        let stream = match account.auth.as_ref() {
            "Plain" => {
                debug!("Creating a Plain TCP Connection");
                TCPStreamType::Plain(BufReader::new(tcp_stream.try_clone()?))
            }
            "SSL" => {
                debug!("Creating a SSL Connection");
                let connector = SslConnectorBuilder::new(SslMethod::tls())?.build();
                TCPStreamType::SSL(BufReader::new(
                        connector.connect(&account.host[..], tcp_stream)?))
            }
            _ => return Err("Unknown auth type".into()),
        };

        let mut ctx = POP3Connection {
            account: account,
            stream: stream,
            state: POP3State::BEGIN,
            timestamp: String::new(),
        };
        trace!("Connection Established");
        debug!("POP3State::{:?}", ctx.state);
        ctx.read_greeting()?;
        ctx.state = POP3State::AUTHORIZATION;
        debug!("POP3State::{:?}", ctx.state);
        Ok(ctx)
    }

    pub fn login(&mut self) -> Result<()> {
        assert!(self.state == POP3State::AUTHORIZATION);
        trace!("Attempting to Login");
        let username = &self.account.username.clone();
        let auth_user_cmd = self.send_command("USER", Some(username));
        let auth_response = match auth_user_cmd {
            Ok(_) => {
                debug!("Plain USER/PASS authentication");
                let password = &self.account.password.clone();
                self.send_command("PASS", Some(password))
            }
            Err(_) => {
                debug!("Authenticating using APOP");
                let digest = utils::get_apop_digest(&self.timestamp, &self.account.password);
                let apop_param = &format!("{} {}", self.account.username, digest);
                self.send_command("APOP", Some(apop_param))
            }
        };

        // Switch the current state to TRANSACTION on a successful authentication
        match auth_response {
            Ok(_) => {
                self.state = POP3State::TRANSACTION;
                debug!("POP3State::{:?}", self.state);
                Ok(())
            }
            Err(e) => Err(e),
        }
    }

    pub fn stat(&mut self) -> Result<POP3Stat> {
        assert!(self.state == POP3State::TRANSACTION);
        trace!("Cmd: STAT");
        self.send_command("STAT", None)
            .map(|msg| POP3Stat::parse(&msg[0]))
    }

    pub fn list(&mut self, msgnum: Option<u32>) -> Result<POP3List> {
        assert!(self.state == POP3State::TRANSACTION);
        trace!("Cmd: LIST");
        let msgnumval;
        let msgnum = match msgnum {
            Some(x) => {
                msgnumval = x.to_string();
                Some(msgnumval.as_ref())
            }
            None => None,
        };
        self.send_command("LIST", msgnum)
            .map(|msg| POP3List::parse(&msg))
    }

    pub fn retr(&mut self, msgnum: u32) -> Result<POP3Retr> {
        assert!(self.state == POP3State::TRANSACTION);
        trace!("Cmd: RETR");
        self.send_command("RETR", Some(&msgnum.to_string()))
            .map(|msg| POP3Retr::parse(&msg))
    }

    pub fn dele(&mut self, msgnum: u32) -> Result<()> {
        assert!(self.state == POP3State::TRANSACTION);
        trace!("Cmd: DELE");
        let _ = self.send_command("DELE", Some(&msgnum.to_string()))?;
        Ok(())
    }

    pub fn noop(&mut self) -> Result<()> {
        assert!(self.state == POP3State::TRANSACTION);
        trace!("Cmd: NOOP");
        let _ = self.send_command("NOOP", None)?;
        Ok(())
    }

    pub fn rset(&mut self) -> Result<()> {
        assert!(self.state == POP3State::TRANSACTION);
        trace!("Cmd: RSET");
        let _ = self.send_command("RSET", None)?;
        Ok(())
    }

    pub fn quit(&mut self) -> Result<()> {
        assert!(self.state == POP3State::AUTHORIZATION || self.state == POP3State::TRANSACTION);
        trace!("Cmd: QUIT");
        let _ = self.send_command("QUIT", None)?;
        self.state = POP3State::END;
        debug!("POP3State::{:?}", self.state);
        Ok(())
    }

    pub fn top(&mut self, msgnum: u32, lines: u32) -> Result<POP3Retr> {
        assert!(self.state == POP3State::TRANSACTION);
        trace!("Cmd: TOP {} {}", msgnum, lines);
        let args = &format!("{} {}", msgnum, lines);
        self.send_command("TOP", Some(args))
            .map(|msg| POP3Retr::parse(&msg))
    }

    pub fn uidl(&mut self, msgnum: Option<u32>) -> Result<POP3Uidl> {
        assert!(self.state == POP3State::TRANSACTION);
        trace!("Cmd: UIDL");
        let msgnumval;
        let msgnum = match msgnum {
            Some(x) => {
                msgnumval = x.to_string();
                Some(msgnumval.as_ref())
            }
            None => None,
        };
        self.send_command("UIDL", msgnum)
            .map(|msg| POP3Uidl::parse(&msg))
    }

    fn read_greeting(&mut self) -> Result<()> {
        trace!("Reading Greeting from Server");
        let greeting = &self.read_response(false)?[0];
        let re = Regex::new(r"(<.*>)\r\n$")?;
        for cap in re.captures_iter(greeting) {
            self.timestamp = cap[1].to_string();
        }
        Ok(())
    }

    fn send_command(&mut self, command: &str, param: Option<&str>) -> Result<Vec<String>> {
        // Identify if the command is a multiline command
        let is_multiline = match command {
            "LIST" | "UIDL" => param.is_none(),
            "RETR" => true,
            _ => false,
        };

        // Create the actual POP3 Command by appending the parameters
        let command = match param {
            Some(x) => format!("{} {}", command, x),
            None => command.to_string(),
        };

        info!("C: {}", command);
        self.stream.write_string(&command)?;

        self.read_response(is_multiline)
    }

    fn read_response(&mut self, is_multiline: bool) -> Result<Vec<String>> {

        lazy_static!{
            static ref RESPONSE: Regex =
                Regex::new(r"^(?P<status>\+OK|-ERR) (?P<statustext>.*)").unwrap();
        }
        const LF: u8 = 0x0a;
        let mut response_data: Vec<String> = Vec::new();
        let mut buff = Vec::new();
        let mut complete;

        //First read the status line
        self.stream.read_until(LF, &mut buff)?;
        response_data.push(String::from_utf8(buff.clone())?);
        info!("S: {}", response_data[0]);

        // Test if the response is positive. Else exit early.
        let status_line = response_data[0].clone();
        let response_groups = RESPONSE.captures(&status_line).unwrap();
        match response_groups.name("status").ok_or("Regex match failed")?.as_str() {
            "+OK" => complete = false,
            "-ERR" => return Err(response_groups["statustext"].to_string().into()),
            _ => return Err("Un-parseable Response".into()),
        };

        while !complete && is_multiline {
            buff.clear();
            self.stream.read_until(LF, &mut buff)?;
            let line = String::from_utf8(buff.clone())?;
            if line == ".\r\n" {
                complete = true;
            } else {
                // Don't add the final .CRLF to the response.
                // It's useless for us
                response_data.push(line);
            }
        }
        Ok(response_data)
    }
}