aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / spark-mail-rs / src / main.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | use clap::{Parser, Subcommand}; |
| 17 | use serde::Deserialize; |
| 18 | use spark_mail::api::{ApiState, create_router}; |
| 19 | use spark_mail::cortex_sync::CortexSync; |
| 20 | use spark_mail::gandi::{self, Account}; |
| 21 | use spark_mail::models::SendEmailRequest; |
| 22 | use spark_mail::relay::MailRelay; |
| 23 | use spark_mail::smtp_server::run_smtp_server; |
| 24 | use spark_mail::store::MailStore; |
| 25 | use std::fs; |
| 26 | use std::io::Read; |
| 27 | use std::net::SocketAddr; |
| 28 | use std::path::PathBuf; |
| 29 | use std::sync::Arc; |
| 30 | use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; |
| 31 | |
| 32 | #[derive(Parser, Debug)] |
| 33 | #[command( |
| 34 | name = "spark-mail", |
| 35 | about = "Sovereign Native Mail Engine & Atlas Cortex Bridge" |
| 36 | )] |
| 37 | struct Cli { |
| 38 | /// SMTP ingest listener: off, unix:<absolute path> (owner uid only), |
| 39 | /// or host:port (plain TCP, no authentication). |
| 40 | #[arg(long, default_value = "127.0.0.1:2525")] |
| 41 | smtp_addr: String, |
| 42 | |
| 43 | #[arg(long, default_value = "127.0.0.1:18092")] |
| 44 | api_addr: String, |
| 45 | |
| 46 | #[arg(long)] |
| 47 | mail_dir: Option<PathBuf>, |
| 48 | |
| 49 | #[command(subcommand)] |
| 50 | command: Option<Commands>, |
| 51 | } |
| 52 | |
| 53 | #[derive(Subcommand, Debug)] |
| 54 | enum Commands { |
| 55 | Run, |
| 56 | Status, |
| 57 | List { |
| 58 | #[arg(default_value = "inbox")] |
| 59 | folder: String, |
| 60 | }, |
| 61 | Send { |
| 62 | #[arg(long)] |
| 63 | to: String, |
| 64 | #[arg(long)] |
| 65 | subject: String, |
| 66 | #[arg(long)] |
| 67 | body: String, |
| 68 | #[arg(long)] |
| 69 | from: Option<String>, |
| 70 | }, |
| 71 | IngestTest, |
| 72 | GandiList { |
| 73 | #[arg(long, value_enum)] |
| 74 | account: Account, |
| 75 | #[arg(long, default_value_t = 20)] |
| 76 | limit: usize, |
| 77 | }, |
| 78 | GandiRead { |
| 79 | #[arg(long, value_enum)] |
| 80 | account: Account, |
| 81 | #[arg(long)] |
| 82 | uid: u32, |
| 83 | }, |
| 84 | GandiSend { |
| 85 | #[arg(long)] |
| 86 | to: String, |
| 87 | #[arg(long)] |
| 88 | subject: String, |
| 89 | }, |
| 90 | } |
| 91 | |
| 92 | #[derive(Deserialize, Default)] |
| 93 | struct OperatorConfig { |
| 94 | operator: Option<OperatorSection>, |
| 95 | } |
| 96 | |
| 97 | #[derive(Deserialize, Default)] |
| 98 | struct OperatorSection { |
| 99 | email: Option<String>, |
| 100 | } |
| 101 | |
| 102 | fn load_operator_email() -> String { |
| 103 | let p = dirs_or_home().join(".config/sovereign/operator.toml"); |
| 104 | if p.exists() |
| 105 | && let Ok(content) = fs::read_to_string(&p) |
| 106 | && let Ok(cfg) = toml::from_str::<OperatorConfig>(&content) |
| 107 | && let Some(op) = cfg.operator |
| 108 | && let Some(email) = op.email |
| 109 | { |
| 110 | return email; |
| 111 | } |
| 112 | "aien@aienos.com".to_string() |
| 113 | } |
| 114 | |
| 115 | fn dirs_or_home() -> PathBuf { |
| 116 | std::env::var("HOME") |
| 117 | .or_else(|_| std::env::var("USERPROFILE")) |
| 118 | .map(PathBuf::from) |
| 119 | .unwrap_or_else(|_| PathBuf::from(".")) |
| 120 | } |
| 121 | |
| 122 | #[tokio::main] |
| 123 | async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { |
| 124 | tracing_subscriber::registry() |
| 125 | .with( |
| 126 | tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| "info".into()), |
| 127 | ) |
| 128 | .with(tracing_subscriber::fmt::layer()) |
| 129 | .init(); |
| 130 | |
| 131 | let cli = Cli::parse(); |
| 132 | let command = cli.command.unwrap_or(Commands::Run); |
| 133 | match command { |
| 134 | Commands::GandiList { account, limit } => { |
| 135 | println!("{}", serde_json::to_string(&gandi::list(account, limit)?)?); |
| 136 | return Ok(()); |
| 137 | } |
| 138 | Commands::GandiRead { account, uid } => { |
| 139 | println!("{}", serde_json::to_string(&gandi::read(account, uid)?)?); |
| 140 | return Ok(()); |
| 141 | } |
| 142 | Commands::GandiSend { to, subject } => { |
| 143 | let mut body = String::new(); |
| 144 | std::io::stdin().read_to_string(&mut body)?; |
| 145 | let receipt = gandi::send_as_aien(&to, &subject, &body)?; |
| 146 | println!("{}", serde_json::to_string(&receipt)?); |
| 147 | println!("Gandi SMTP accepted the message from aien@aienos.com"); |
| 148 | return Ok(()); |
| 149 | } |
| 150 | _ => {} |
| 151 | } |
| 152 | let store = Arc::new(MailStore::new(cli.mail_dir)); |
| 153 | let cortex = Arc::new(CortexSync::new(None)); |
| 154 | let operator_email = load_operator_email(); |
| 155 | |
| 156 | let relay = Arc::new(MailRelay::new( |
| 157 | "127.0.0.1", |
| 158 | 1025, |
| 159 | Arc::clone(&store), |
| 160 | Arc::clone(&cortex), |
| 161 | &operator_email, |
| 162 | )); |
| 163 | |
| 164 | match command { |
| 165 | Commands::Run => { |
| 166 | // A bad --smtp-addr stops the service before anything listens. |
| 167 | let smtp_port = spark_mail::smtp_server::SmtpBind::parse(&cli.smtp_addr)?.port(); |
| 168 | println!("📬 AIEN Sovereign Mail Engine starting..."); |
| 169 | println!(" Local NVMe Storage: {}", store.base_path.display()); |
| 170 | println!(" SMTP Listener: {}", cli.smtp_addr); |
| 171 | println!(" API Gateway: http://{}", cli.api_addr); |
| 172 | println!(" Operator Identity: {}", operator_email); |
| 173 | |
| 174 | let store_smtp = Arc::clone(&store); |
| 175 | let cortex_smtp = Arc::clone(&cortex); |
| 176 | let smtp_addr = cli.smtp_addr.clone(); |
| 177 | |
| 178 | let smtp_handle = |
| 179 | tokio::spawn( |
| 180 | async move { run_smtp_server(&smtp_addr, store_smtp, cortex_smtp).await }, |
| 181 | ); |
| 182 | |
| 183 | let api_port: u16 = cli |
| 184 | .api_addr |
| 185 | .split(':') |
| 186 | .next_back() |
| 187 | .and_then(|p| p.parse().ok()) |
| 188 | .unwrap_or(18092); |
| 189 | |
| 190 | let api_state = ApiState { |
| 191 | store: Arc::clone(&store), |
| 192 | cortex: Arc::clone(&cortex), |
| 193 | relay: Arc::clone(&relay), |
| 194 | smtp_port, |
| 195 | api_port, |
| 196 | operator_email, |
| 197 | }; |
| 198 | |
| 199 | let app = create_router(api_state); |
| 200 | let api_socket: SocketAddr = cli.api_addr.parse()?; |
| 201 | let listener = tokio::net::TcpListener::bind(api_socket).await?; |
| 202 | |
| 203 | let api_handle = tokio::spawn(async move { |
| 204 | let _ = axum::serve(listener, app).await; |
| 205 | }); |
| 206 | |
| 207 | tokio::select! { |
| 208 | // An SMTP listener that cannot start ends the process with an error. |
| 209 | res = smtp_handle => res??, |
| 210 | _ = api_handle => {}, |
| 211 | } |
| 212 | } |
| 213 | Commands::Status => { |
| 214 | let cortex_ok = cortex.check_health().await; |
| 215 | let bridge_ok = relay.check_bridge().await; |
| 216 | let smtp_port = spark_mail::smtp_server::SmtpBind::parse(&cli.smtp_addr) |
| 217 | .map(|bind| bind.port()) |
| 218 | .unwrap_or(0); |
| 219 | let status = store.get_status(&operator_email, smtp_port, 18092, cortex_ok, bridge_ok); |
| 220 | println!("{:#?}", status); |
| 221 | } |
| 222 | Commands::List { folder } => { |
| 223 | let messages = store.list_folder(&folder); |
| 224 | println!("Found {} messages in [{}]:", messages.len(), folder); |
| 225 | for m in messages { |
| 226 | println!( |
| 227 | "- [{}] {} | From: {} | Subject: {}", |
| 228 | &m.id[..8], |
| 229 | m.received_at, |
| 230 | m.from, |
| 231 | m.subject |
| 232 | ); |
| 233 | } |
| 234 | } |
| 235 | Commands::Send { |
| 236 | to, |
| 237 | subject, |
| 238 | body, |
| 239 | from, |
| 240 | } => { |
| 241 | let req = SendEmailRequest { |
| 242 | to: vec![to], |
| 243 | subject, |
| 244 | body, |
| 245 | from, |
| 246 | }; |
| 247 | let sent = relay.send_email(req).await.map_err(std::io::Error::other)?; |
| 248 | println!( |
| 249 | "SMTP relay accepted: {} (Cortex indexed: {})", |
| 250 | sent.id, sent.cortex_indexed |
| 251 | ); |
| 252 | } |
| 253 | Commands::IngestTest => { |
| 254 | let req = SendEmailRequest { |
| 255 | to: vec![operator_email.clone()], |
| 256 | subject: "Sovereign Mail Node Online".to_string(), |
| 257 | body: "Your sovereign mail server is running directly on Spark hardware. Messages are stored on local NVMe disk with zero cloud telemetry and synced to Atlas Cortex memory.".to_string(), |
| 258 | from: Some("aien@aienos.com".to_string()), |
| 259 | }; |
| 260 | let sent = relay.send_email(req).await.map_err(std::io::Error::other)?; |
| 261 | println!("Test message saved and indexed into Cortex: {}", sent.id); |
| 262 | } |
| 263 | Commands::GandiList { .. } | Commands::GandiRead { .. } | Commands::GandiSend { .. } => { |
| 264 | unreachable!() |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | Ok(()) |
| 269 | } |