hippox-drivers 0.3.3

🦛All indivisible atomic driver units in Hippox.
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
use anyhow::Result;
use serde_json::{Value, json};
use std::collections::HashMap;
use std::time::Duration;

use crate::{
    DriverCallback, DriverCategory, DriverContext,
    types::{Driver, DriverParameter},
};

fn get_param_string(params: &HashMap<String, Value>, name: &str) -> Result<String> {
    params
        .get(name)
        .and_then(|v| v.as_str())
        .map(|s| s.to_string())
        .ok_or_else(|| anyhow::anyhow!("Missing parameter: {}", name))
}

fn get_param_bool(params: &HashMap<String, Value>, name: &str, default: bool) -> bool {
    params
        .get(name)
        .and_then(|v| v.as_bool())
        .unwrap_or(default)
}

fn get_param_u64(params: &HashMap<String, Value>, name: &str, default: u64) -> u64 {
    params.get(name).and_then(|v| v.as_u64()).unwrap_or(default)
}

fn strip_html_tags(html: &str) -> String {
    let mut result = html.to_string();
    result = result.replace("<br>", "\n");
    result = result.replace("<br/>", "\n");
    result = result.replace("<br />", "\n");
    result = result.replace("</p>", "\n\n");
    result = result.replace("</div>", "\n");
    result = result.replace("</h1>", "\n");
    result = result.replace("</h2>", "\n");
    result = result.replace("</h3>", "\n");
    result = result.replace("</h4>", "\n");
    result = result.replace("</h5>", "\n");
    result = result.replace("</h6>", "\n");
    result = result.replace("</li>", "\n");
    result = result.replace("<li>", "• ");
    let mut in_tag = false;
    let mut cleaned = String::with_capacity(result.len());
    for c in result.chars() {
        if c == '<' {
            in_tag = true;
        } else if c == '>' {
            in_tag = false;
        } else if !in_tag {
            cleaned.push(c);
        }
    }
    cleaned
        .lines()
        .map(|line| line.trim())
        .filter(|line| !line.is_empty())
        .collect::<Vec<_>>()
        .join("\n")
}

#[derive(Debug)]
pub struct SendEmailDriver;

#[async_trait::async_trait]
impl Driver for SendEmailDriver {
    fn name(&self) -> &str {
        "send_email"
    }

    fn description(&self) -> &str {
        "Send an email via SMTP server"
    }

    fn usage_hint(&self) -> &str {
        "Use this driver when the user wants to send an email, notify someone via email"
    }

    fn category(&self) -> DriverCategory {
        DriverCategory::Email
    }

    fn parameters(&self) -> Vec<DriverParameter> {
        vec![
            DriverParameter {
                name: "smtp_host".to_string(),
                param_type: "string".to_string(),
                description: "SMTP server host".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("smtp.gmail.com".to_string())),
                enum_values: None,
            },
            DriverParameter {
                name: "smtp_port".to_string(),
                param_type: "integer".to_string(),
                description: "SMTP server port".to_string(),
                required: false,
                default: Some(Value::Number(587.into())),
                example: Some(Value::Number(587.into())),
                enum_values: None,
            },
            DriverParameter {
                name: "username".to_string(),
                param_type: "string".to_string(),
                description: "SMTP username".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("user@gmail.com".to_string())),
                enum_values: None,
            },
            DriverParameter {
                name: "password".to_string(),
                param_type: "string".to_string(),
                description: "SMTP password".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("your_password".to_string())),
                enum_values: None,
            },
            DriverParameter {
                name: "from".to_string(),
                param_type: "string".to_string(),
                description: "Sender email address".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("bot@example.com".to_string())),
                enum_values: None,
            },
            DriverParameter {
                name: "to".to_string(),
                param_type: "string".to_string(),
                description: "Recipient email address".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("user@example.com".to_string())),
                enum_values: None,
            },
            DriverParameter {
                name: "subject".to_string(),
                param_type: "string".to_string(),
                description: "Email subject line".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("Hello from Hippo".to_string())),
                enum_values: None,
            },
            DriverParameter {
                name: "body".to_string(),
                param_type: "string".to_string(),
                description: "Email body content".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("This is a test email".to_string())),
                enum_values: None,
            },
            DriverParameter {
                name: "cc".to_string(),
                param_type: "string".to_string(),
                description: "CC recipient".to_string(),
                required: false,
                default: None,
                example: Some(Value::String("cc@example.com".to_string())),
                enum_values: None,
            },
            DriverParameter {
                name: "bcc".to_string(),
                param_type: "string".to_string(),
                description: "BCC recipient".to_string(),
                required: false,
                default: None,
                example: Some(Value::String("bcc@example.com".to_string())),
                enum_values: None,
            },
            DriverParameter {
                name: "is_html".to_string(),
                param_type: "boolean".to_string(),
                description: "Whether the body is HTML".to_string(),
                required: false,
                default: Some(Value::Bool(true)),
                example: Some(Value::Bool(false)),
                enum_values: None,
            },
            DriverParameter {
                name: "timeout".to_string(),
                param_type: "integer".to_string(),
                description: "Connection timeout in seconds".to_string(),
                required: false,
                default: Some(Value::Number(30.into())),
                example: Some(Value::Number(10.into())),
                enum_values: None,
            },
        ]
    }

    fn example_call(&self) -> Value {
        json!({ "action": "send_email", "parameters": { "smtp_host": "smtp.gmail.com", "username": "user@gmail.com", "password": "password", "from": "bot@example.com", "to": "user@example.com", "subject": "Hello", "body": "Test email" } })
    }

    fn example_output(&self) -> String {
        "Email sent successfully to user@example.com".to_string()
    }

    async fn execute(
        &self,
        parameters: &HashMap<String, Value>,
        callback: Option<&dyn DriverCallback>,
        context: Option<&DriverContext>,
    ) -> Result<String> {
        let task_id = context.as_ref().and_then(|c| c.task_id()).map(String::from);
        let driver_index = context.as_ref().and_then(|c| c.driver_index());
        let step_name = context
            .as_ref()
            .and_then(|c| c.driver_name())
            .map(String::from);
        let cb = callback;
        if let Some(cb) = cb {
            cb.on_start(task_id.clone(), driver_index, step_name);
            cb.on_log(
                task_id.clone(),
                driver_index,
                Some("Starting email send".to_string()),
            );
            cb.on_progress(task_id.clone(), driver_index, Some(5), None);
        }
        let smtp_host = get_param_string(parameters, "smtp_host")?;
        if let Some(cb) = cb {
            cb.on_log(
                task_id.clone(),
                driver_index,
                Some(format!("SMTP host: {}", smtp_host)),
            );
            cb.on_progress(task_id.clone(), driver_index, Some(10), None);
        }
        let smtp_port = get_param_u64(parameters, "smtp_port", 587) as u16;
        if let Some(cb) = cb {
            cb.on_log(
                task_id.clone(),
                driver_index,
                Some(format!("SMTP port: {}", smtp_port)),
            );
            cb.on_progress(task_id.clone(), driver_index, Some(15), None);
        }
        let username = get_param_string(parameters, "username")?;
        if let Some(cb) = cb {
            cb.on_log(
                task_id.clone(),
                driver_index,
                Some(format!("Username: {}", username)),
            );
            cb.on_progress(task_id.clone(), driver_index, Some(20), None);
        }
        let password = get_param_string(parameters, "password")?;
        if let Some(cb) = cb {
            cb.on_log(
                task_id.clone(),
                driver_index,
                Some("Password provided".to_string()),
            );
            cb.on_progress(task_id.clone(), driver_index, Some(25), None);
        }
        let from_addr = get_param_string(parameters, "from")?;
        if let Some(cb) = cb {
            cb.on_log(
                task_id.clone(),
                driver_index,
                Some(format!("From: {}", from_addr)),
            );
            cb.on_progress(task_id.clone(), driver_index, Some(30), None);
        }
        let to_addr = get_param_string(parameters, "to")?;
        if let Some(cb) = cb {
            cb.on_log(
                task_id.clone(),
                driver_index,
                Some(format!("To: {}", to_addr)),
            );
            cb.on_progress(task_id.clone(), driver_index, Some(35), None);
        }
        let subject = get_param_string(parameters, "subject")?;
        if let Some(cb) = cb {
            cb.on_log(
                task_id.clone(),
                driver_index,
                Some(format!("Subject: {}", subject)),
            );
            cb.on_progress(task_id.clone(), driver_index, Some(40), None);
        }
        let body = get_param_string(parameters, "body")?;
        if let Some(cb) = cb {
            let body_preview = if body.len() > 50 {
                format!("{}...", &body[..50])
            } else {
                body.clone()
            };
            cb.on_log(
                task_id.clone(),
                driver_index,
                Some(format!("Body: {}", body_preview)),
            );
            cb.on_progress(task_id.clone(), driver_index, Some(45), None);
        }
        let cc = parameters.get("cc").and_then(|v| v.as_str());
        if let Some(cb) = cb {
            if let Some(cc_addr) = cc {
                cb.on_log(
                    task_id.clone(),
                    driver_index,
                    Some(format!("CC: {}", cc_addr)),
                );
            }
            cb.on_progress(task_id.clone(), driver_index, Some(50), None);
        }
        let bcc = parameters.get("bcc").and_then(|v| v.as_str());
        if let Some(cb) = cb {
            if let Some(bcc_addr) = bcc {
                cb.on_log(
                    task_id.clone(),
                    driver_index,
                    Some(format!("BCC: {}", bcc_addr)),
                );
            }
            cb.on_progress(task_id.clone(), driver_index, Some(55), None);
        }
        let is_html = get_param_bool(parameters, "is_html", true);
        if let Some(cb) = cb {
            cb.on_log(
                task_id.clone(),
                driver_index,
                Some(format!("HTML format: {}", is_html)),
            );
            cb.on_progress(task_id.clone(), driver_index, Some(60), None);
        }
        let timeout_secs = get_param_u64(parameters, "timeout", 30);
        if let Some(cb) = cb {
            cb.on_log(
                task_id.clone(),
                driver_index,
                Some(format!("Timeout: {}s", timeout_secs)),
            );
            cb.on_progress(task_id.clone(), driver_index, Some(65), None);
        }
        use lettre::message::Mailbox;
        use lettre::{
            AsyncSmtpTransport, AsyncTransport, Message, Tokio1Executor,
            transport::smtp::authentication::Credentials,
        };
        if let Some(cb) = cb {
            cb.on_log(
                task_id.clone(),
                driver_index,
                Some("Building email message".to_string()),
            );
            cb.on_progress(task_id.clone(), driver_index, Some(70), None);
        }
        let to_parsed: Mailbox = to_addr.parse()?;
        let from_parsed: Mailbox = from_addr.parse()?;
        let mut email_builder = Message::builder()
            .from(from_parsed)
            .to(to_parsed)
            .subject(subject);
        if let Some(cc_addr) = cc {
            email_builder = email_builder.cc(cc_addr.parse()?);
        }
        if let Some(bcc_addr) = bcc {
            email_builder = email_builder.bcc(bcc_addr.parse()?);
        }
        let email = if is_html {
            let plain_body = strip_html_tags(&body);
            email_builder.multipart(lettre::message::MultiPart::alternative_plain_html(
                plain_body, body,
            ))?
        } else {
            email_builder.body(body)?
        };
        if let Some(cb) = cb {
            cb.on_log(
                task_id.clone(),
                driver_index,
                Some("Connecting to SMTP server".to_string()),
            );
            cb.on_progress(task_id.clone(), driver_index, Some(75), None);
        }
        let creds = Credentials::new(username, password);
        let mailer = AsyncSmtpTransport::<Tokio1Executor>::starttls_relay(&smtp_host)?
            .port(smtp_port)
            .credentials(creds)
            .build();
        if let Some(cb) = cb {
            cb.on_log(
                task_id.clone(),
                driver_index,
                Some("Sending email".to_string()),
            );
            cb.on_progress(task_id.clone(), driver_index, Some(80), None);
        }
        tokio::time::timeout(Duration::from_secs(timeout_secs), mailer.send(email))
            .await
            .map_err(|_| anyhow::anyhow!("Email send timeout after {} seconds", timeout_secs))?
            .map_err(|e| anyhow::anyhow!("SMTP error: {}", e))?;
        if let Some(cb) = cb {
            cb.on_log(
                task_id.clone(),
                driver_index,
                Some(format!("Email sent to {}", to_addr)),
            );
            cb.on_progress(task_id.clone(), driver_index, Some(100), None);
            cb.on_complete(
                task_id.clone(),
                driver_index,
                Some("send_email".to_string()),
                Some(format!("Email sent successfully to {}", to_addr)),
            );
        }
        Ok(format!("Email sent successfully to {}", to_addr))
    }
}