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
279
280
281
282
283
284
285
286
287
288
289
290
//! # Synapse: Neural Communi/// This means you can send messages using simple, human-readable names, and Synapseation Network
//!
//! **A revolutionary neural communication network for AI entities, distributed systems, and modern applications
//! with federated identity management, dual trust systems, and privacy-respecting discovery.**
//!
//! ## 🌟 What Makes Synapse Special?
//!
//! Synapse transforms how entities communicate by providing:
//!
//! - **🧠 Neural Identity**: Contextual participant discovery with natural language
//! - **🌍 Federated Network**: Cross-organizational communication and trust
//! - **⚡ Intelligent Routing**: Multi-speed communication with smart transport selection
//! - **🔒 Privacy First**: Advanced privacy controls with stealth and unlisted modes
//! - **🤖 AI-Native**: Designed for AI-to-AI and human-to-AI interaction
//! - **🏛️ Dual Trust**: Entity-to-entity and blockchain-verified network trust
//! - **⛓️ Blockchain Trust**: Staking, verification, and decay mechanisms
//!
//! ## 🎯 The Neural Identity System
//!
//! One of Synapse's most powerful features is its **contextual identity system**:
//!
//! ```text
//! Simple Name → Global ID → Network Discovery → Smart Transport Selection
//!
//! "Alice" → alice@ai-lab.example.com → 192.168.1.100:8080 → TCP (real-time)
//! "Claude" → claude@anthropic.com → [external] → Email (reliable)
//! "LocalBot" → bot@localhost → 127.0.0.1:9090 → UDP (fast local)
//! ```
//!
//! This means you can send messages using simple, human-readable names, and Synapse
//! automatically figures out:
//! - Where the recipient is located (email address, IP, domain)
//! - How to reach them (direct connection, relay, email)
//! - What transport to use (TCP for speed, email for reliability)
//! - What security to apply (encryption level, authentication)
//!
//! ## 🏗️ Architecture Overview
//!
//! Synapse operates on multiple intelligent layers:
//!
//! ### Layer 1: Message Layer
//! - Simple message types: Direct, Broadcast, Conversation, Notification
//! - Automatic serialization and security wrapping
//! - Metadata handling and routing information
//!
//! ### Layer 2: Identity Resolution
//! - **Local Names**: `"Alice"`, `"Bob"`, `"MyAI"` (human-friendly)
//! - **Global IDs**: `"alice@company.com"` (globally unique)
//! - **Network Addresses**: IP addresses, ports, service endpoints
//! - **Capability Discovery**: What transports and features each peer supports
//!
//! ### Layer 3: Multi-Transport Intelligence
//! - **Real-time Transports**: TCP, UDP, WebSocket for <100ms messaging
//! - **Local Discovery**: mDNS, LAN scanning, Bluetooth for peer finding
//! - **Email Backbone**: SMTP/IMAP for reliable, federated communication
//! - **NAT Traversal**: STUN/TURN/UPnP for firewall penetration
//!
//! ### Layer 4: Email Server Integration
//! - **Full Server Mode**: Run your own SMTP/IMAP when externally accessible
//! - **Relay Mode**: Outgoing only when behind firewall/NAT
//! - **External Provider**: Fall back to Gmail, Outlook, etc. when restricted
//!
//! ## 🚀 Quick Start Example
//!
//! ```rust
//! use message_routing_system::*;
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//! // Initialize with your identity
//! let config = Config::default();
//! let router = EnhancedSynapseRouter::new(config, "MyBot@example.com".to_string()).await?;
//!
//! // Register some contacts (or use auto-discovery)
//! router.register_peer("Alice", "alice@ai-lab.example.com").await?;
//!
//! // Start all services
//! router.start().await?;
//!
//! // Send messages using simple names!
//! router.send_message_smart(
//! "Alice", // Just the name
//! "Hello from Synapse!", // Your message
//! MessageType::Direct, // Type of communication
//! SecurityLevel::Authenticated, // Security level
//! MessageUrgency::Interactive, // Speed vs reliability preference
//! ).await?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## 🎛️ Advanced Features
//!
//! ### Automatic Transport Selection
//!
//! Synapse intelligently chooses the best transport based on:
//! - **Message urgency**: Real-time vs. reliable delivery
//! - **Network conditions**: Latency, bandwidth, connectivity
//! - **Security requirements**: Encryption levels, authentication
//! - **Peer capabilities**: What transports the recipient supports
//!
//! ```rust
//! // Real-time collaboration (prefers TCP/UDP)
//! router.send_message_smart(
//! "AI-Partner",
//! "Quick question about the algorithm...",
//! MessageType::Conversation,
//! SecurityLevel::Public,
//! MessageUrgency::RealTime, // <100ms preferred
//! ).await?;
//!
//! // Reliable file sharing (uses email for guaranteed delivery)
//! router.send_file_with_urgency(
//! "ResearchTeam",
//! "important_results.pdf",
//! MessageUrgency::Background, // Reliability over speed
//! ).await?;
//! ```
//!
//! ### Email Server Capabilities
//!
//! When your system is externally accessible, EMRP can run a full email server:
//!
//! ```rust
//! let router = EnhancedSynapseRouter::new(config, "bot@mydomain.com".to_string()).await?;
//!
//! if router.is_running_email_server() {
//! // You can receive emails directly at bot@mydomain.com
//! // Other EMRP systems can send messages to your domain
//! // Supports both EMRP messages and standard emails
//! println!("🏃 Running full email infrastructure");
//! } else {
//! // Falls back to external providers (Gmail, etc.)
//! println!("🌐 Using external email services");
//! }
//! ```
//!
//! ### Security by Design
//!
//! All messages are automatically secured:
//! - **🔐 PGP Encryption**: Messages encrypted with recipient's public key
//! - **✍️ Digital Signatures**: Verify sender authenticity
//! - **🛡️ TLS Transport**: Encrypted connections for real-time transports
//! - **🔑 Automatic Key Management**: Keys generated and distributed automatically
//!
//! ## 📚 Module Overview
//!
//! - [`router_enhanced`]: Main interface - start here for most use cases
//! - [`identity`]: Name resolution and identity management
//! - [`transport`]: Multi-transport layer and intelligent routing
//! - [`email_server`]: SMTP/IMAP server implementation
//! - [`types`]: Core message types and data structures
//! - [`crypto`]: Encryption, signatures, and key management
//! - [`config`]: Configuration and setup
//!
//! ## 🎯 Use Cases
//!
//! ### AI & Machine Learning
//! - Multi-agent AI systems coordinating in real-time
//! - AI-human collaboration with natural addressing
//! - Federated learning with secure model sharing
//! - Research collaboration between AI entities worldwide
//!
//! ### Enterprise & Distributed Systems
//! - Microservice communication with email-based service discovery
//! - Cross-organization messaging leveraging existing email infrastructure
//! - Reliable async processing with email-based job queuing
//! - Legacy system integration through email gateways
//!
//! ### IoT & Edge Computing
//! - Device-to-cloud communication using email when internet is limited
//! - Peer-to-peer IoT networks with automatic discovery
//! - Edge AI coordination across distributed deployments
//! - Resilient communication in unstable network conditions
//!
//! ## 🔧 Getting Started
//!
//! 1. **Add to Cargo.toml**: `message_routing_system = "0.1.0"`
//! 2. **See examples**: `cargo run --example email_integration_test`
//! 3. **Read the docs**: Full API documentation and guides
//! 4. **Join the community**: Contribute and get support
//!
//! The combination of email's universal reach with modern real-time transports
//! creates a communication system that's both globally federated and
//! performance-optimized - perfect for the AI-driven future.
// Core modules
// Platform-specific modules - not available in WASM
// Re-export commonly used types
pub use CryptoManager;
pub use EmailTransport;
// Re-export transport types needed for tests
pub use ;
pub use ;
pub use Config;
// Synapse Neural Communication Network
// WebAssembly support
// Re-export key types for convenience
pub use SynapseError;
pub use *;
// Re-export transport types for tests and external usage
pub use ;
pub use MessageUrgency;
// Re-export router types for tests and external usage
pub use SynapseRouter;
pub use EnhancedSynapseRouter;
pub use MultiTransportRouter;
// Re-export Synapse key types (only on non-WASM platforms)
pub use ;
pub use ;
// Re-export synapse submodules
pub use api;
pub use blockchain;
pub use models;
pub use services;
pub use storage;
// Auth integration module (conditional based on auth feature)
use tracing_subscriber;
/// Initialize the Synapse system with logging (not available on WASM)
/// Current protocol version
pub const PROTOCOL_VERSION: &str = "1.0.0";
/// Standard email headers for Synapse