aboutsummaryrefslogtreecommitdiff
path: root/src/server.rs
blob: fe4f2ec6d664916da778d88d6ac349ed52dd5372 (plain) (blame)
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
use anyhow::{bail, Result};
use std::sync::Arc;

use rusoto_signature::Region;

use crate::config::*;
use crate::login::{ldap_provider::*, static_provider::*, *};
use crate::mailbox::Mailbox;

use boitalettres::proto::{Request, Response};
use boitalettres::server::accept::addr::{AddrIncoming, AddrStream};
use boitalettres::server::Server as ImapServer;

use std::pin::Pin;
use std::task::{Context, Poll};
use tower::Service;
use futures::future::BoxFuture;

pub struct Connection {
    pub mailstore: Arc<Mailstore>,
}
impl Connection {
  pub fn new(mailstore: Arc<Mailstore>) -> Self {
    Self { mailstore }
  }
}
impl Service<Request> for Connection {
    type Response = Response;
    type Error = boitalettres::errors::Error;
    type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: Request) -> Self::Future {
        tracing::debug!("Got request: {:#?}", req);
        let mailstore = self.mailstore.clone();
        Box::pin(async move {
            use imap_codec::types::{
                command::CommandBody,
                response::{Capability, Data},
            };

            let r = match req.body {
                CommandBody::Capability => {
                    use tokio::time::{sleep, Duration};
                    sleep(Duration::from_millis(100)).await;
                    let capabilities = vec![Capability::Imap4Rev1, Capability::Idle];
                    let body = vec![Data::Capability(capabilities)];
                    Response::ok(
                        "Pre-login capabilities listed, post-login capabilities have more.",
                    )?
                    .with_body(body)
                }
                CommandBody::Login {
                    username,
                    password,
                } => {
                    let (u, p) = match (String::try_from(username), String::try_from(password)) {
                      (Ok(u), Ok(p)) => (u, p),
                      _ => { return Response::bad("Invalid characters") }
                    };

                    tracing::debug!(user = %u, "command.login");
                    let creds = match mailstore.login_provider.login(&u, &p).await {
                        Err(_) => { return Response::no("[AUTHENTICATIONFAILED] Authentication failed.") }
                        Ok(c) => c,
                    };

                    Response::ok("Logged in")?
                }
                _ => Response::bad("Error in IMAP command received by server.")?,
            };

            Ok(r)
        })
    }
}

pub struct Instance {
    pub mailstore: Arc<Mailstore>
}
impl Instance {
   pub fn new(mailstore: Arc<Mailstore>) -> Self {
      Self { mailstore }
   }
}
impl<'a> Service<&'a AddrStream> for Instance {
    type Response = Connection;
    type Error = anyhow::Error;
    type Future = BoxFuture<'static, Result<Self::Response>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, addr: &'a AddrStream) -> Self::Future {
        tracing::info!(remote_addr = %addr.remote_addr, local_addr = %addr.local_addr, "accept");
        let ms = self.mailstore.clone();
        Box::pin(async { 
            Ok(Connection::new(ms)) 
        })
    }
}




pub struct Mailstore {
    pub login_provider: Box<dyn LoginProvider + Send + Sync>,
}
impl Mailstore {
    pub fn new(config: Config) -> Result<Arc<Self>> {
        let s3_region = Region::Custom {
            name: config.aws_region.clone(),
            endpoint: config.s3_endpoint,
        };
        let k2v_region = Region::Custom {
            name: config.aws_region,
            endpoint: config.k2v_endpoint,
        };
        let login_provider: Box<dyn LoginProvider + Send + Sync> = match (config.login_static, config.login_ldap)
        {
            (Some(st), None) => Box::new(StaticLoginProvider::new(st, k2v_region, s3_region)?),
            (None, Some(ld)) => Box::new(LdapLoginProvider::new(ld, k2v_region, s3_region)?),
            (Some(_), Some(_)) => bail!("A single login provider must be set up in config file"),
            (None, None) => bail!("No login provider is set up in config file"),
        };
        Ok(Arc::new(Self { login_provider }))
    }
}



pub struct Server {
    pub incoming: AddrIncoming,
    pub mailstore: Arc<Mailstore>,
}
impl Server {
    pub async fn new(config: Config) -> Result<Self> {
        Ok(Self { 
            incoming: AddrIncoming::new("127.0.0.1:4567").await?,
            mailstore: Mailstore::new(config)?,
        })
    }

    pub async fn run(self: Self) -> Result<()> {
        tracing::info!("Starting server on {:#}", self.incoming.local_addr);


        let creds = self.mailstore.login_provider.login("quentin", "poupou").await?;
        //let mut mailbox = Mailbox::new(&creds, "TestMailbox".to_string()).await?;
        //mailbox.test().await?;

        let server = ImapServer::new(self.incoming).serve(Instance::new(self.mailstore.clone()));
        let _ = server.await?;


        Ok(())
    }
}