agentzero-channels 0.3.0

AgentZero — modular AI-agent runtime and tool framework
Documentation
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#[cfg(feature = "channel-email")]
#[allow(dead_code)]
mod impl_ {
    use crate::channels::helpers;
    use crate::{Channel, ChannelMessage, SendMessage};
    use async_trait::async_trait;
    use std::time::Duration;
    use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
    use tokio::net::TcpStream;

    super::super::channel_meta!(EMAIL_DESCRIPTOR, "email", "Email");

    const POLL_INTERVAL_SECS: u64 = 30;
    const MAX_MESSAGE_LENGTH: usize = 50000;

    pub struct EmailConfig {
        pub smtp_host: String,
        pub smtp_port: u16,
        pub imap_host: String,
        pub imap_port: u16,
        pub username: String,
        pub password: String,
        pub from_address: String,
        pub allowed_senders: Vec<String>,
    }

    pub struct EmailChannel {
        smtp_host: String,
        smtp_port: u16,
        imap_host: String,
        imap_port: u16,
        username: String,
        password: String,
        from_address: String,
        allowed_senders: Vec<String>,
    }

    impl EmailChannel {
        pub fn new(config: EmailConfig) -> Self {
            Self {
                smtp_host: config.smtp_host,
                smtp_port: config.smtp_port,
                imap_host: config.imap_host,
                imap_port: config.imap_port,
                username: config.username,
                password: config.password,
                from_address: config.from_address,
                allowed_senders: config.allowed_senders,
            }
        }

        /// Send an email via raw SMTP.
        async fn smtp_send(
            &self,
            to: &str,
            subject: &str,
            body: &str,
        ) -> anyhow::Result<()> {
            let addr = format!("{}:{}", self.smtp_host, self.smtp_port);
            let stream = TcpStream::connect(&addr).await?;
            let (reader_half, mut writer) = tokio::io::split(stream);
            let mut reader = BufReader::new(reader_half);
            let mut line = String::new();

            // Read greeting
            Self::read_smtp_line(&mut reader, &mut line).await?;

            // EHLO
            Self::smtp_command(&mut writer, &mut reader, &mut line, "EHLO agentzero").await?;

            // AUTH LOGIN if credentials provided
            if !self.username.is_empty() {
                Self::smtp_command(&mut writer, &mut reader, &mut line, "AUTH LOGIN").await?;
                let user_b64 = base64_encode(self.username.as_bytes());
                Self::smtp_command(&mut writer, &mut reader, &mut line, &user_b64).await?;
                let pass_b64 = base64_encode(self.password.as_bytes());
                Self::smtp_command(&mut writer, &mut reader, &mut line, &pass_b64).await?;
            }

            // MAIL FROM
            Self::smtp_command(
                &mut writer,
                &mut reader,
                &mut line,
                &format!("MAIL FROM:<{}>", self.from_address),
            )
            .await?;

            // RCPT TO
            Self::smtp_command(
                &mut writer,
                &mut reader,
                &mut line,
                &format!("RCPT TO:<{to}>"),
            )
            .await?;

            // DATA
            Self::smtp_command(&mut writer, &mut reader, &mut line, "DATA").await?;

            // Message headers + body
            let message = format!(
                "From: {}\r\nTo: {}\r\nSubject: {}\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n{}\r\n.",
                self.from_address, to, subject, body
            );
            Self::smtp_command(&mut writer, &mut reader, &mut line, &message).await?;

            // QUIT
            let _ = Self::smtp_command(&mut writer, &mut reader, &mut line, "QUIT").await;

            Ok(())
        }

        async fn smtp_command(
            writer: &mut tokio::io::WriteHalf<TcpStream>,
            reader: &mut BufReader<tokio::io::ReadHalf<TcpStream>>,
            line: &mut String,
            command: &str,
        ) -> anyhow::Result<()> {
            writer.write_all(command.as_bytes()).await?;
            writer.write_all(b"\r\n").await?;
            writer.flush().await?;
            Self::read_smtp_line(reader, line).await
        }

        async fn read_smtp_line(
            reader: &mut BufReader<tokio::io::ReadHalf<TcpStream>>,
            line: &mut String,
        ) -> anyhow::Result<()> {
            line.clear();
            reader.read_line(line).await?;
            // Read continuation lines (multi-line responses: "250-..." then "250 ...")
            while line.len() >= 4 && line.as_bytes().get(3) == Some(&b'-') {
                let mut cont = String::new();
                reader.read_line(&mut cont).await?;
                line.push_str(&cont);
            }
            let code = line.get(..3).unwrap_or("");
            if code.starts_with('4') || code.starts_with('5') {
                anyhow::bail!("SMTP error: {}", line.trim());
            }
            Ok(())
        }

        /// Poll for new messages via basic IMAP.
        async fn imap_poll_unseen(&self) -> anyhow::Result<Vec<(String, String, String)>> {
            let addr = format!("{}:{}", self.imap_host, self.imap_port);
            let stream = TcpStream::connect(&addr).await?;
            let (reader_half, mut writer) = tokio::io::split(stream);
            let mut reader = BufReader::new(reader_half);
            let mut line = String::new();

            // Read greeting
            Self::imap_read_response(&mut reader, &mut line).await?;

            // LOGIN
            Self::imap_command(
                &mut writer,
                &mut reader,
                &mut line,
                &format!("A001 LOGIN {} {}", self.username, self.password),
            )
            .await?;

            // SELECT INBOX
            Self::imap_command(&mut writer, &mut reader, &mut line, "A002 SELECT INBOX").await?;

            // SEARCH UNSEEN
            Self::imap_command(&mut writer, &mut reader, &mut line, "A003 SEARCH UNSEEN").await?;

            let mut message_ids: Vec<String> = Vec::new();
            for response_line in line.lines() {
                if response_line.starts_with("* SEARCH") {
                    message_ids = response_line
                        .strip_prefix("* SEARCH")
                        .unwrap_or("")
                        .split_whitespace()
                        .map(String::from)
                        .collect();
                }
            }

            let mut results = Vec::new();

            // Fetch each unseen message (limited to newest 5)
            for msg_id in message_ids.iter().rev().take(5) {
                Self::imap_command(
                    &mut writer,
                    &mut reader,
                    &mut line,
                    &format!("A004 FETCH {msg_id} (BODY[HEADER.FIELDS (FROM SUBJECT)] BODY[TEXT])"),
                )
                .await?;

                let from = extract_header(&line, "From");
                let subject = extract_header(&line, "Subject");
                let body = extract_body(&line);

                if !from.is_empty() {
                    results.push((from, subject, body));
                }

                // Mark as seen
                Self::imap_command(
                    &mut writer,
                    &mut reader,
                    &mut line,
                    &format!("A005 STORE {msg_id} +FLAGS (\\Seen)"),
                )
                .await?;
            }

            // LOGOUT
            let _ = Self::imap_command(&mut writer, &mut reader, &mut line, "A006 LOGOUT").await;

            Ok(results)
        }

        async fn imap_command(
            writer: &mut tokio::io::WriteHalf<TcpStream>,
            reader: &mut BufReader<tokio::io::ReadHalf<TcpStream>>,
            line: &mut String,
            command: &str,
        ) -> anyhow::Result<()> {
            writer.write_all(command.as_bytes()).await?;
            writer.write_all(b"\r\n").await?;
            writer.flush().await?;
            Self::imap_read_response(reader, line).await
        }

        async fn imap_read_response(
            reader: &mut BufReader<tokio::io::ReadHalf<TcpStream>>,
            output: &mut String,
        ) -> anyhow::Result<()> {
            output.clear();
            loop {
                let mut line = String::new();
                let n = reader.read_line(&mut line).await?;
                if n == 0 {
                    break;
                }
                output.push_str(&line);
                // IMAP tagged responses end with "Axx OK/NO/BAD ..."
                let trimmed = line.trim();
                if trimmed.starts_with("* OK")
                    || trimmed.contains(" OK ")
                    || trimmed.contains(" NO ")
                    || trimmed.contains(" BAD ")
                {
                    break;
                }
            }
            Ok(())
        }
    }

    /// Simple base64 encoder (no padding variant handling needed for SMTP AUTH).
    fn base64_encode(input: &[u8]) -> String {
        const CHARS: &[u8] =
            b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        let mut result = String::new();
        for chunk in input.chunks(3) {
            let b0 = chunk[0] as u32;
            let b1 = chunk.get(1).copied().unwrap_or(0) as u32;
            let b2 = chunk.get(2).copied().unwrap_or(0) as u32;
            let triple = (b0 << 16) | (b1 << 8) | b2;
            result.push(CHARS[((triple >> 18) & 0x3F) as usize] as char);
            result.push(CHARS[((triple >> 12) & 0x3F) as usize] as char);
            if chunk.len() > 1 {
                result.push(CHARS[((triple >> 6) & 0x3F) as usize] as char);
            } else {
                result.push('=');
            }
            if chunk.len() > 2 {
                result.push(CHARS[(triple & 0x3F) as usize] as char);
            } else {
                result.push('=');
            }
        }
        result
    }

    /// Extract a header value from raw IMAP FETCH output.
    fn extract_header(raw: &str, header: &str) -> String {
        let prefix = format!("{header}:");
        for line in raw.lines() {
            let trimmed = line.trim();
            if let Some(value) = trimmed.strip_prefix(&prefix) {
                return value.trim().to_string();
            }
        }
        String::new()
    }

    /// Extract the body text from raw IMAP FETCH output.
    fn extract_body(raw: &str) -> String {
        // Look for empty line after headers (body separator in MIME)
        let mut in_body = false;
        let mut body_lines = Vec::new();

        for line in raw.lines() {
            if in_body {
                let trimmed = line.trim();
                // Stop at IMAP tagged response or closing paren
                if trimmed.starts_with("A00") || trimmed == ")" {
                    break;
                }
                body_lines.push(line);
            } else if line.trim().is_empty() {
                in_body = true;
            }
        }

        body_lines.join("\n").trim().to_string()
    }

    #[async_trait]
    impl Channel for EmailChannel {
        fn name(&self) -> &str {
            "email"
        }

        async fn send(&self, message: &SendMessage) -> anyhow::Result<()> {
            let subject = message
                .subject
                .as_deref()
                .unwrap_or("Message from AgentZero");

            let chunks = helpers::split_message(&message.content, MAX_MESSAGE_LENGTH);
            for (i, chunk) in chunks.iter().enumerate() {
                let subj = if chunks.len() > 1 {
                    format!("{subject} ({}/{})", i + 1, chunks.len())
                } else {
                    subject.to_string()
                };
                self.smtp_send(&message.recipient, &subj, chunk).await?;
            }
            Ok(())
        }

        async fn listen(
            &self,
            tx: tokio::sync::mpsc::Sender<ChannelMessage>,
        ) -> anyhow::Result<()> {
            loop {
                match self.imap_poll_unseen().await {
                    Ok(messages) => {
                        for (from, subject, body) in messages {
                            let sender_email = extract_email_address(&from);

                            if !helpers::is_user_allowed(&sender_email, &self.allowed_senders) {
                                tracing::debug!(sender = %sender_email, "email: ignoring from unallowed sender");
                                continue;
                            }

                            let content = if subject.is_empty() {
                                body
                            } else {
                                format!("[{subject}] {body}")
                            };

                            if content.is_empty() {
                                continue;
                            }

                            let msg = ChannelMessage {
                                id: helpers::new_message_id(),
                                sender: sender_email.clone(),
                                reply_target: sender_email,
                                content,
                                channel: "email".to_string(),
                                timestamp: helpers::now_epoch_secs(),
                                thread_ts: None,
                                privacy_boundary: String::new(),
                            };

                            if tx.send(msg).await.is_err() {
                                return Ok(());
                            }
                        }
                    }
                    Err(e) => {
                        tracing::error!(error = %e, "email imap poll failed");
                    }
                }

                tokio::time::sleep(Duration::from_secs(POLL_INTERVAL_SECS)).await;
            }
        }

        async fn health_check(&self) -> bool {
            // Try connecting to IMAP server
            let addr = format!("{}:{}", self.imap_host, self.imap_port);
            TcpStream::connect(&addr)
                .await
                .map(|_| true)
                .unwrap_or(false)
        }
    }

    /// Extract bare email address from "Name <email@example.com>" or "email@example.com".
    fn extract_email_address(from: &str) -> String {
        if let Some(start) = from.find('<') {
            if let Some(end) = from.find('>') {
                return from[start + 1..end].trim().to_string();
            }
        }
        from.trim().to_string()
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn email_channel_name() {
            let ch = EmailChannel::new(EmailConfig {
                smtp_host: "smtp.example.com".into(),
                smtp_port: 587,
                imap_host: "imap.example.com".into(),
                imap_port: 143,
                username: "user".into(),
                password: "pass".into(),
                from_address: "bot@example.com".into(),
                allowed_senders: vec![],
            });
            assert_eq!(ch.name(), "email");
        }

        #[test]
        fn extract_email_from_display_name() {
            assert_eq!(
                extract_email_address("Alice <alice@example.com>"),
                "alice@example.com"
            );
            assert_eq!(
                extract_email_address("bob@example.com"),
                "bob@example.com"
            );
            assert_eq!(
                extract_email_address("  <admin@example.com>  "),
                "admin@example.com"
            );
        }

        #[test]
        fn extract_header_parses_from() {
            let raw = "From: alice@example.com\r\nSubject: Hello\r\n\r\nBody text";
            assert_eq!(extract_header(raw, "From"), "alice@example.com");
            assert_eq!(extract_header(raw, "Subject"), "Hello");
        }

        #[test]
        fn base64_encode_standard() {
            assert_eq!(base64_encode(b"Hello"), "SGVsbG8=");
            assert_eq!(base64_encode(b"ab"), "YWI=");
            assert_eq!(base64_encode(b"abc"), "YWJj");
        }

        #[test]
        fn extract_body_from_imap_output() {
            let raw = "From: alice@example.com\r\nSubject: Test\r\n\r\nHello world\r\nSecond line\r\n)\r\nA004 OK";
            let body = extract_body(raw);
            assert!(body.contains("Hello world"));
        }
    }
}

#[cfg(feature = "channel-email")]
pub use impl_::*;

#[cfg(not(feature = "channel-email"))]
super::channel_stub!(EmailChannel, EMAIL_DESCRIPTOR, "email", "Email");