Mail!!!

Seto Elkahfi committed May 3, 2026 at 16:20 UTC be1c149279e5d9aaa8a31af040373f05afbcd738
5 files changed +47 -288
IMAP_IMPLEMENTATION.md deleted
-193
@@ -1,193 +0,0 @@
1 -# IMAP Implementation Documentation
2 -
3 -This document describes the IMAP (Internet Message Access Protocol) implementation in the smbcloud-mail server.
4 -
5 -## Overview
6 -
7 -The IMAP implementation provides a complete RFC 3501-compliant IMAP4rev1 server that works alongside the existing SMTP implementation. Messages received via SMTP are stored in mailboxes that can be accessed through IMAP.
8 -
9 -## Architecture
10 -
11 -### Core Components
12 -
13 -1. **Message Module** (`src/imap/message.rs`)
14 - - `Message`: Represents an email message with metadata
15 - - `MessageFlag`: Enum for standard IMAP flags (Seen, Answered, Flagged, Deleted, Draft, Recent)
16 - - Supports UID and sequence number addressing
17 - - Parses subject and body from raw email content
18 -
19 -2. **Mailbox Module** (`src/imap/mailbox.rs`)
20 - - `Mailbox`: Container for messages with thread-safe access
21 - - `MailboxStore`: Central storage for multiple mailboxes
22 - - Default mailboxes: INBOX, Sent, Drafts, Trash
23 - - Provides message counting (exists, recent, unseen)
24 - - UID management with uidvalidity and uidnext
25 -
26 -3. **State Module** (`src/imap/state.rs`)
27 - - `ImapState`: State machine for IMAP protocol
28 - - States: NotAuthenticated, Authenticated, Selected, Logout
29 - - Enforces proper command sequencing
30 -
31 -4. **Commands Module** (`src/imap/commands.rs`)
32 - - `ImapCommand`: Parser for IMAP commands
33 - - `ImapResponse`: Formatter for IMAP responses
34 - - `ImapCommandHandler`: Command execution logic
35 - - Implements all essential IMAP commands
36 -
37 -5. **Session Module** (`src/imap/session.rs`)
38 - - `ImapSession`: Async session handler
39 - - Manages client connections
40 - - Processes commands and generates responses
41 - - Uses Tokio's async I/O
42 -
43 -6. **Listen Module** (`src/imap/listen.rs`)
44 - - TCP listener for IMAP connections
45 - - Spawns session handlers for each connection
46 - - Runs on port 1143 by default
47 -
48 -7. **Storage Module** (`src/storage.rs`)
49 - - `MessageStorage`: Bridge between SMTP and IMAP
50 - - Shared mailbox store
51 - - Thread-safe message storage
52 -
53 -## Implemented Commands
54 -
55 -### Authentication Commands
56 -- **CAPABILITY**: Lists server capabilities
57 -- **LOGIN**: Authenticates user (accepts any credentials for demo)
58 -- **LOGOUT**: Closes connection
59 -
60 -### Mailbox Commands
61 -- **LIST/LSUB**: Lists available mailboxes
62 -- **SELECT**: Selects a mailbox in read-write mode
63 -- **EXAMINE**: Selects a mailbox in read-only mode
64 -- **STATUS**: Gets mailbox status without selecting
65 -
66 -### Message Commands
67 -- **FETCH**: Retrieves message data (FLAGS, UID, BODY, ENVELOPE, etc.)
68 -- **SEARCH**: Searches for messages (simplified implementation)
69 -- **NOOP**: No operation (keepalive)
70 -
71 -## Protocol State Machine
72 -
73 -```
74 -┌─────────────────────┐
75 -│ NotAuthenticated │
76 -└──────────┬──────────┘
77 - │ LOGIN
78 - ▼
79 -┌─────────────────────┐
80 -│ Authenticated │
81 -└──────────┬──────────┘
82 - │ SELECT/EXAMINE
83 - ▼
84 -┌─────────────────────┐
85 -│ Selected │
86 -└──────────┬──────────┘
87 - │ LOGOUT
88 - ▼
89 -┌─────────────────────┐
90 -│ Logout │
91 -└─────────────────────┘
92 -```
93 -
94 -## Integration with SMTP
95 -
96 -1. **Shared Storage**: Both SMTP and IMAP use the same `MessageStorage` instance
97 -2. **Message Flow**:
98 - - Email received via SMTP → Stored in `MessageStorage` → Accessible via IMAP INBOX
99 -3. **Concurrent Operation**: SMTP (port 2525) and IMAP (port 1143) run simultaneously
100 -
101 -## Usage Examples
102 -
103 -### Basic IMAP Session
104 -```bash
105 -# Connect and list mailboxes
106 -printf "a1 LOGIN user pass\r\na2 LIST \"\" \"*\"\r\na3 LOGOUT\r\n" | nc localhost 1143
107 -```
108 -
109 -### Check INBOX Status
110 -```bash
111 -printf "a1 LOGIN user pass\r\na2 STATUS INBOX (MESSAGES RECENT UNSEEN)\r\na3 LOGOUT\r\n" | nc localhost 1143
112 -```
113 -
114 -### Read Messages
115 -```bash
116 -printf "a1 LOGIN user pass\r\na2 SELECT INBOX\r\na3 FETCH 1:* (FLAGS BODY[])\r\na4 LOGOUT\r\n" | nc localhost 1143
117 -```
118 -
119 -## Testing
120 -
121 -### Run Unit Tests
122 -```bash
123 -cargo test
124 -```
125 -
126 -### Run Example Client
127 -```bash
128 -cargo run --example imap_client
129 -```
130 -
131 -### Manual Testing
132 -1. Start the server: `cargo run`
133 -2. Connect with netcat: `nc localhost 1143`
134 -3. Send IMAP commands interactively
135 -
136 -## Limitations and Future Enhancements
137 -
138 -### Current Limitations
139 -- No persistent storage (messages lost on restart)
140 -- Simplified authentication (accepts any credentials)
141 -- No SSL/TLS support for IMAP
142 -- Limited SEARCH criteria support
143 -- No support for mailbox hierarchy separators
144 -- No IDLE, APPEND, or COPY commands
145 -
146 -### Potential Enhancements
147 -- Persistent storage (database or file system)
148 -- Real authentication system
149 -- STARTTLS/SSL support for IMAP
150 -- Full SEARCH command support
151 -- IDLE command for push notifications
152 -- APPEND command for storing messages
153 -- COPY/MOVE commands for message management
154 -- Quota management
155 -- Access control lists (ACLs)
156 -
157 -## Security Considerations
158 -
159 -1. **Authentication**: Current implementation accepts any credentials - implement proper authentication for production
160 -2. **Encryption**: No TLS support yet - add STARTTLS for secure connections
161 -3. **Input Validation**: Basic validation implemented - add more robust checks
162 -4. **Resource Limits**: No limits on mailbox size or connection count - add limits for production
163 -
164 -## Performance Notes
165 -
166 -- Thread-safe using `Arc<Mutex<>>` for shared state
167 -- Async I/O with Tokio for efficient connection handling
168 -- Messages stored in memory for fast access
169 -- Supports concurrent SMTP and IMAP operations
170 -
171 -## Compliance
172 -
173 -The implementation follows RFC 3501 (IMAP4rev1) for core functionality:
174 -- Protocol state management ✓
175 -- Command syntax and responses ✓
176 -- Message flags ✓
177 -- UID support ✓
178 -- Mailbox operations ✓
179 -
180 -Not yet implemented:
181 -- Full SEARCH criteria
182 -- All FETCH data items
183 -- Mailbox subscriptions
184 -- Message sequence number updates after expunge
185 -
186 -## Contributing
187 -
188 -When extending the IMAP implementation:
189 -1. Follow existing code patterns
190 -2. Add unit tests for new functionality
191 -3. Update this documentation
192 -4. Test with standard IMAP clients (Thunderbird, Mail.app, etc.)
193 -5. Ensure thread safety for concurrent access
README.md
+31 -88
@@ -1,99 +1,42 @@
1 -# SMTP and IMAP Mail Server
1 +<!-- LOGO -->
2 +<h1>
3 +<p align="center">
4 + <img src="https://avatars.githubusercontent.com/u/89791739?s=200&v=4" alt="smbCloud Logo" width="128">
5 + <br>smbCloud Mail
6 +</h1>
7 + <p align="center">
8 + IMAP server for smbCloud Mail.
9 + <br />
10 + <br />
11 + <a href="https://smbcloud.xyz/">Website</a>
12 + ·
13 + <a href="./docs">Documentation</a>
14 + ·
15 + <a href="https://smbcloud.xyz/services">Services</a>
16 + </p>
17 +</p>
18
3 -A basic SMTP and IMAP server implementation in Rust with TLS/STARTTLS support.
19 +## About
20
5 -## Features
21 +**`smbcloud-mail-imap`** is the IMAP server for [smbCloud Mail](https://smbcloud.xyz/services).
22
7 -### SMTP
8 -- ✅ Basic SMTP protocol support (HELO/EHLO, MAIL FROM, RCPT TO, DATA, QUIT)
9 -- ✅ STARTTLS support for secure connections
10 -- ✅ Self-signed certificate generation for TLS
11 -- ✅ Multiple recipient support
12 -- ✅ Command validation and error handling
23 +<details>
24 + <summary>Mail</summary>
25
14 -### IMAP
15 -- ✅ IMAP4rev1 protocol implementation
16 -- ✅ Essential commands (LOGIN, SELECT, EXAMINE, FETCH, SEARCH, LIST, STATUS, LOGOUT, CAPABILITY)
17 -- ✅ Mailbox management (INBOX, Sent, Drafts, Trash)
18 -- ✅ Message storage and retrieval
19 -- ✅ UID and sequence number support
20 -- ✅ Message flags (Seen, Answered, Flagged, Deleted, Draft, Recent)
21 -- ✅ Protocol state management
26 + <p align="center">
27 + <img src="./assets/Mail-is-Mail.png" alt="smbCloud Mail preview" width="860">
28 + </p>
29
23 -### General
24 -- ✅ Async/await with Tokio runtime
25 -- ✅ Structured logging with tracing
26 -- ✅ Shared message storage between SMTP and IMAP
30 +</details>
31
28 -## Running the Server
32 +## Contrib
33
30 -```bash
31 -cargo run
32 -```
34 +Contributions are welcome.
35
34 -The SMTP server listens on `127.0.0.1:2525` and the IMAP server listens on `127.0.0.1:1143` by default.
36 +## License
37
36 -## Testing
38 +[Apache-2.0](LICENSE)
39
38 -### Testing SMTP
40 +## Copyright
41
40 -You can test the SMTP server using `nc` (netcat) or `telnet`:
41 -
42 -```bash
43 -echo -e "EHLO test.com\r\nMAIL FROM: <sender@example.com>\r\nRCPT TO: <recipient@example.com>\r\nDATA\r\nSubject: Test\r\n\r\nTest message\r\n.\r\nQUIT\r\n" | nc localhost 2525
44 -```
45 -
46 -### Testing IMAP
47 -
48 -You can test the IMAP server using `nc` (netcat) or `telnet`:
49 -
50 -```bash
51 -# Connect and login
52 -echo -e "a1 CAPABILITY\r\na2 LOGIN user pass\r\na3 LIST \"\" \"*\"\r\na4 SELECT INBOX\r\na5 LOGOUT\r\n" | nc localhost 1143
53 -
54 -# Fetch messages
55 -echo -e "a1 LOGIN user pass\r\na2 SELECT INBOX\r\na3 FETCH 1 (FLAGS BODY[])\r\na4 LOGOUT\r\n" | nc localhost 1143
56 -```
57 -
58 -## Supported Commands
59 -
60 -### SMTP Commands
61 -
62 -- `HELO/EHLO` - Initiate SMTP session
63 -- `MAIL FROM` - Specify sender email address
64 -- `RCPT TO` - Specify recipient email address (supports multiple)
65 -- `DATA` - Begin email message content
66 -- `RSET` - Reset the current transaction
67 -- `NOOP` - No operation (keepalive)
68 -- `QUIT` - Close connection
69 -- `STARTTLS` - Upgrade connection to TLS
70 -
71 -### IMAP Commands
72 -
73 -- `CAPABILITY` - List server capabilities
74 -- `LOGIN` - Authenticate user
75 -- `LIST/LSUB` - List available mailboxes
76 -- `SELECT` - Select a mailbox (read-write)
77 -- `EXAMINE` - Select a mailbox (read-only)
78 -- `FETCH` - Retrieve message data
79 -- `SEARCH` - Search for messages
80 -- `STATUS` - Get mailbox status
81 -- `LOGOUT` - Close connection
82 -- `NOOP` - No operation (keepalive)
83 -
84 -## Building
85 -
86 -```bash
87 -cargo build --release
88 -```
89 -
90 -## Dependencies
91 -
92 -- `tokio` - Async runtime
93 -- `anyhow` - Error handling
94 -- `tracing` / `tracing-subscriber` - Logging
95 -- `regex` - SMTP command parsing
96 -- `native-tls` / `tokio-native-tls` - TLS support
97 -- `openssl` - Certificate generation
98 -- `futures` - Stream handling
99 -- `tokio-util` - Codec utilities
42 +© 2026 [smbCloud](https://smbcloud.xyz) (Splitfire AB).
assets/Mail-is-Mail.png
Binary files /dev/null and b/assets/Mail-is-Mail.png differ
docs/readme.md
+15 -6
@@ -1,14 +1,23 @@
1 -# Run on server
1 +# Running on a server
2 +
3 +These are quick notes for running the IMAP service manually while you are still in the prototype stage.
4 +
5 +## Run in the background
6
7 ```bash
4 -$ cargo run --release -p nginx_auth_http > output.log 2>&1 &
5 -$ cargo run --release > output.log 2>&1 &
8 +cargo run --release > output.log 2>&1 &
9 ```
10
8 -## Find all TCP processes
11 +## Check the listener
12
10 -Find all running processes:
13 +For a local IMAP listener on port `1143`:
14
15 ```bash
13 -$ sudo lsof -nP -iTCP:2525 -sTCP:LISTEN
16 +sudo lsof -nP -iTCP:1143 -sTCP:LISTEN
17 ```
18 +
19 +If you change the port, update the command to match.
20 +
21 +## Note
22 +
23 +This is only a rough operator note for development and manual testing. It is not a deployment guide yet.
docs/scripts/myfile.txt
+1 -1
@@ -1 +1 @@
1 -this is my testtttt
1 +scratch file for local testing