yopmail-client 0.3.2

Unofficial async client for YOPmail: list inboxes, fetch (HTML/raw), send, attachments, RSS
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
//! High-level async client for interacting with YOPmail via its web endpoints.
//!
//! The client is intentionally "browser-like": it maintains a cookie jar, sends default headers,
//! and extracts a `yp` token from the login page. Most operations will automatically call
//! [`YopmailClient::open_inbox`] the first time they need a session.
use crate::constants::*;
use crate::error::{Error, Result};
use crate::models::{Attachment, Message, MessageContent, RssItem};
use regex::Regex;
use reqwest::{
    cookie::Jar,
    header::{HeaderMap, HeaderName, HeaderValue},
    Client, ClientBuilder, StatusCode,
};
use rand::{distributions::Alphanumeric, Rng};
use scraper::{Html, Selector};
use std::collections::HashSet;
use std::sync::Arc;
use std::time::SystemTime;
use std::time::Duration;

fn parse_mailbox(mailbox: &str) -> (String, String) {
    if let Some((local, domain)) = mailbox.split_once('@') {
        (
            local.trim().to_lowercase(),
            domain.trim().to_lowercase(),
        )
    } else {
        (mailbox.trim().to_lowercase(), DEFAULT_DOMAIN.to_string())
    }
}

fn build_headers(base: &[(&str, &str)], extras: &[(&str, &str)]) -> HeaderMap {
    let mut headers = HeaderMap::new();
    for (k, v) in base.iter().chain(extras.iter()) {
        if let Ok(name) = HeaderName::from_bytes(k.as_bytes()) {
            if let Ok(val) = HeaderValue::from_str(v) {
                headers.insert(name, val);
            }
        }
    }
    headers
}

/// A stateful YOPmail client backed by `reqwest`.
///
/// The client maintains a cookie jar (to emulate the web UI) and an extracted `yp` token used by
/// inbox/mail endpoints. Methods take `&mut self` because session state is refreshed on demand.
pub struct YopmailClient {
    mailbox: String,
    domain: String,
    base_url: String,
    jar: Arc<Jar>,
    client: Client,
    yp_token: Option<String>,
}

/// Builder for [`YopmailClient`].
///
/// This configures the underlying HTTP client (base URL, timeout, optional proxy) and constructs a
/// `reqwest::Client` with a shared cookie jar.
pub struct YopmailClientBuilder {
    mailbox: String,
    base_url: String,
    timeout: Duration,
    proxy_url: Option<String>,
}

impl YopmailClientBuilder {
    /// Create a builder for `mailbox`.
    ///
    /// The mailbox may be provided as `local` or `local@domain`.
    /// - `local` and `domain` are trimmed and lowercased.
    /// - If no domain is provided, [`DEFAULT_DOMAIN`] is used.
    pub fn new(mailbox: impl AsRef<str>) -> Self {
        Self {
            mailbox: mailbox.as_ref().to_string(),
            base_url: BASE_URL.to_string(),
            timeout: default_timeout(),
            proxy_url: None,
        }
    }

    /// Override the base URL (defaults to [`BASE_URL`]).
    ///
    /// This is primarily useful for testing or for alternative frontends that mimic YOPmail's
    /// endpoints.
    pub fn base_url(mut self, base_url: impl Into<String>) -> Self {
        self.base_url = base_url.into();
        self
    }

    /// Override the request timeout (defaults to [`default_timeout`]).
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Configure an HTTP proxy URL for the underlying `reqwest` client.
    ///
    /// The value is passed to `reqwest::Proxy::all`.
    pub fn proxy_url(mut self, proxy_url: impl Into<String>) -> Self {
        self.proxy_url = Some(proxy_url.into());
        self
    }

    /// Build the [`YopmailClient`].
    ///
    /// This parses and normalizes the mailbox, builds a `reqwest::Client` with cookies enabled,
    /// and initializes session state (no network requests are made).
    pub fn build(self) -> Result<YopmailClient> {
        let (mailbox, domain) = parse_mailbox(&self.mailbox);
        let jar = Arc::new(Jar::default());

        let mut builder = ClientBuilder::new()
            .cookie_provider(jar.clone())
            .timeout(self.timeout)
            .default_headers(default_headers());

        if let Some(proxy) = &self.proxy_url {
            builder = builder.proxy(reqwest::Proxy::all(proxy).map_err(Error::Http)?);
        }

        let client = builder.build().map_err(Error::Http)?;

        Ok(YopmailClient {
            mailbox,
            domain,
            base_url: self.base_url,
            jar,
            client,
            yp_token: None,
        })
    }
}

impl YopmailClient {
    /// Start building a client for `mailbox`.
    ///
    /// Equivalent to [`YopmailClientBuilder::new`].
    pub fn builder(mailbox: impl AsRef<str>) -> YopmailClientBuilder {
        YopmailClientBuilder::new(mailbox)
    }

    /// Construct a client for `mailbox` with default settings.
    ///
    /// This does not perform any network requests. Most operations will implicitly call
    /// [`open_inbox`](Self::open_inbox) on first use.
    pub fn new(mailbox: impl AsRef<str>) -> Result<Self> {
        YopmailClientBuilder::new(mailbox).build()
    }

    /// Initialize the session by performing the same flow as the YOPmail web UI.
    ///
    /// This method:
    /// - sets default cookies (including `ytime` and `ywm`)
    /// - loads the login page to extract a `yp` token (falling back to [`FALLBACK_YP_TOKEN`])
    /// - posts the login form to establish the session cookies
    ///
    /// This is a best-effort initialization. The method does not validate the login response;
    /// subsequent operations may still fail with [`Error::Status`].
    ///
    /// # Errors
    /// - Returns [`Error::Http`] for transport failures and timeouts.
    /// - This method does not currently validate HTTP status codes, so a non-2xx response may not
    ///   be reported as [`Error::Status`].
    pub async fn open_inbox(&mut self) -> Result<()> {
        self.set_default_cookies();

        // Use the same flow as the web UI: load the login page (with yp token) then post the form
        let login_url = format!("{}/en/?login={}", self.base_url, self.mailbox);
        let resp = self.client.get(&login_url).send().await?;
        let body = resp.text().await?;

        self.yp_token = extract_yp_token(&body);
        if self.yp_token.is_none() {
            self.yp_token = Some(FALLBACK_YP_TOKEN.to_string());
        }

        // Submit the login form to establish the session (mirrors the hidden auto-submit form)
        if let Some(ref yp) = self.yp_token {
            let form = [
                ("login", self.mailbox.clone()),
                ("id", String::new()),
                ("yp", yp.clone()),
            ];
            let _ = self
                .client
                .post(format!("{}/en/", self.base_url))
                .headers(default_headers())
                .form(&form)
                .send()
                .await?;
        }
        Ok(())
    }

    /// List messages in the inbox for `page`.
    ///
    /// If the session is not yet initialized, this calls [`open_inbox`](Self::open_inbox)
    /// automatically.
    ///
    /// The returned [`Message`] values are derived from the YOPmail inbox HTML. In particular,
    /// `Message::date` is currently always `None`.
    ///
    /// # Errors
    /// - Returns [`Error::Http`] for transport failures and timeouts.
    /// - Returns [`Error::Status`] if the inbox endpoint responds with a non-success HTTP status.
    pub async fn list_messages(&mut self, page: i32) -> Result<Vec<Message>> {
        if self.yp_token.is_none() {
            self.open_inbox().await?;
        }

        let yp = self
            .yp_token
            .clone()
            .unwrap_or_else(|| FALLBACK_YP_TOKEN.to_string());

        let params = [
            ("login", self.mailbox.as_str()),
            ("p", &page.to_string()),
            ("d", ""),
            ("ctrl", ""),
            ("yp", yp.as_str()),
            ("yj", YJ_TOKEN),
            ("v", VERSION),
            ("r_c", ""),
            ("id", ""),
            ("ad", &AD_PARAM.to_string()),
        ];

        let headers = build_headers(DEFAULT_HEADERS, INBOX_HEADERS);
        let url = format!("{}/inbox", self.base_url);
        let resp = self
            .client
            .get(&url)
            .headers(headers)
            .query(&params)
            .send()
            .await?;
        let status = resp.status();
        let body = resp.text().await?;
        if !status.is_success() {
            return Err(Error::Status { status, body });
        }

        let messages = parse_messages(&body);
        Ok(messages)
    }

    /// Fetch the text body of a message.
    ///
    /// This is a convenience wrapper around [`fetch_message_full`](Self::fetch_message_full) that
    /// returns only [`MessageContent::text`].
    pub async fn fetch_message(&mut self, message_id: &str) -> Result<String> {
        let content = self.fetch_message_full(message_id).await?;
        Ok(content.text)
    }

    /// Fetch a message and return parsed text/HTML, raw HTML, and attachment links.
    ///
    /// `message_id` should typically be the [`Message::id`] returned by [`list_messages`].
    /// The implementation accepts several ID variants and will retry (on HTTP 400) using multiple
    /// common encodings used by YOPmail (for example `m_...`, `me_...`, and `e_...` prefixes).
    ///
    /// On success:
    /// - `text` is extracted and whitespace-normalized
    /// - `html` is extracted from the message container (best-effort)
    /// - `raw` is the full HTML response body
    /// - `attachments` is a best-effort list of unique download URLs
    ///
    /// # Retry behavior
    /// This method may retry the mail fetch on HTTP 400 by attempting a few different message ID
    /// encodings. It does not perform transport-level retries or backoff.
    ///
    /// # Errors
    /// - Returns [`Error::Http`] for transport failures and timeouts.
    /// - Returns [`Error::Status`] when all ID variants fail (the captured body is from the last
    ///   attempt).
    pub async fn fetch_message_full(&mut self, message_id: &str) -> Result<MessageContent> {
        if self.yp_token.is_none() {
            self.open_inbox().await?;
        }

        // Refresh cookies (ytime/ywm) just before the mail fetch
        self.set_default_cookies();

        let headers = build_headers(DEFAULT_HEADERS, MAIL_HEADERS);
        let mail_url = format!("{}/en/mail", self.base_url);
        let yp = self
            .yp_token
            .clone()
            .unwrap_or_else(|| FALLBACK_YP_TOKEN.to_string());
        let ad_param = AD_PARAM.to_string();

        let raw_id = message_id.trim();
        let main_id = if raw_id.starts_with('m') {
            raw_id.to_string()
        } else if raw_id.starts_with("e_") {
            format!("m{}", raw_id)
        } else {
            format!("m_{}", raw_id.trim_start_matches("m_"))
        };
        let alt_id = if raw_id.starts_with("e_")
            || raw_id.starts_with("me_")
            || raw_id.starts_with("m_")
        {
            raw_id.to_string()
        } else {
            format!("e_{raw_id}")
        };

        let variants = vec![
            (main_id, true),
            (alt_id, true),
            (raw_id.to_string(), false),
        ];

        let mut last_status = None;
        let mut last_body = None;
        for (id, use_full_params) in variants {
            let mut params_owned = vec![
                ("b".to_string(), self.mailbox.clone()),
                ("id".to_string(), id),
            ];
            if use_full_params {
                params_owned.extend_from_slice(&[
                    ("yp".to_string(), yp.clone()),
                    ("yj".to_string(), YJ_TOKEN.to_string()),
                    ("v".to_string(), VERSION.to_string()),
                    ("d".to_string(), "".to_string()),
                    ("ctrl".to_string(), "".to_string()),
                    ("r_c".to_string(), "".to_string()),
                    ("ad".to_string(), ad_param.clone()),
                ]);
            }

            let params: Vec<(&str, &str)> = params_owned
                .iter()
                .map(|(k, v)| (k.as_str(), v.as_str()))
                .collect();

            let resp = self
                .client
                .get(&mail_url)
                .headers(headers.clone())
                .query(&params)
                .send()
                .await?;
            let status = resp.status();
            let body = resp.text().await?;
            if status.is_success() {
                let attachments = extract_attachments(&body, &self.base_url);
                let html = extract_message_html(&body);
                return Ok(MessageContent {
                    text: extract_message_body(&body),
                    html,
                    raw: body,
                    attachments,
                });
            }

            last_status = Some(status);
            last_body = Some(body);
            if status != StatusCode::BAD_REQUEST {
                break;
            }
        }

        Err(Error::Status {
            status: last_status.unwrap_or(StatusCode::BAD_REQUEST),
            body: last_body.unwrap_or_else(|| "mail fetch failed".into()),
        })
    }

    /// Send a message from this mailbox.
    ///
    /// The recipient must be a YOPmail address ending with `@yopmail.com`, otherwise
    /// [`Error::InvalidRecipient`] is returned.
    ///
    /// Note: the current implementation also checks the recipient domain against [`ALT_DOMAINS`].
    /// Combined with the `@yopmail.com` suffix requirement, this effectively restricts recipients
    /// to `...@yopmail.com` only.
    ///
    /// Delivery and success are determined by a simple, case-insensitive substring check over the
    /// response body (for example `sent successfully` or `ok|`). If the HTTP request succeeds but
    /// no success marker is found, this returns [`Error::Auth`].
    ///
    /// # Errors
    /// - Returns [`Error::InvalidRecipient`] if `to` is not accepted by the current implementation.
    /// - Returns [`Error::Http`] for transport failures and timeouts.
    /// - Returns [`Error::Status`] if the write endpoint responds with a non-success HTTP status.
    /// - Returns [`Error::Auth`] if the response body does not contain a recognized success marker.
    pub async fn send_message(&mut self, to: &str, subject: &str, body: &str) -> Result<()> {
        if !to.ends_with("@yopmail.com") {
            return Err(Error::InvalidRecipient);
        }
        if self.yp_token.is_none() {
            self.open_inbox().await?;
        }

        let recipient_ok = ALT_DOMAINS.iter().any(|d| to.ends_with(d));
        if !recipient_ok {
            return Err(Error::InvalidRecipient);
        }
        let form = [
            ("msgfrom", format!("{}@{}", self.mailbox, self.domain)),
            ("msgto", to.to_string()),
            ("msgsubject", subject.to_string()),
            ("msgbody", body.to_string()),
        ];

        let headers = build_headers(DEFAULT_HEADERS, SEND_HEADERS);
        let url = format!("{}/writepost", self.base_url);
        let resp = self
            .client
            .post(&url)
            .headers(headers)
            .form(&form)
            .send()
            .await?;
        let status = resp.status();
        let text = resp.text().await?;
        if !status.is_success() {
            return Err(Error::Status {
                status,
                body: text.clone(),
            });
        }

        let lower = text.to_lowercase();
        let success = ["msgto|", "sent successfully", "message sent", "ok|"]
            .iter()
            .any(|needle| lower.contains(needle));
        if success {
            Ok(())
        } else {
            Err(Error::Auth(format!("send failed: {}", text)))
        }
    }

    /// Fetch the first page of the inbox and return `(count, messages)`.
    ///
    /// `count` is the number of messages returned by the parsed page, not a server-reported total.
    pub async fn get_inbox_info(&mut self) -> Result<(usize, Vec<Message>)> {
        let messages = self.list_messages(1).await?;
        let count = messages.len();
        Ok((count, messages))
    }

    /// Convenience wrapper for `list_messages(1)`.
    /// Convenience wrapper for `list_messages(1)`.
    pub async fn check_inbox(&mut self) -> Result<Vec<Message>> {
        self.list_messages(1).await
    }

    /// Return the first message from `list_messages(1)`, if any.
    pub async fn get_last_message(&mut self) -> Result<Option<Message>> {
        let messages = self.list_messages(1).await?;
        Ok(messages.into_iter().next())
    }

    /// Fetch and return the text of the first message from `list_messages(1)`, if any.
    pub async fn get_last_message_content(&mut self) -> Result<Option<String>> {
        let messages = self.list_messages(1).await?;
        if let Some(msg) = messages.first() {
            let content = self.fetch_message(&msg.id).await?;
            Ok(Some(content))
        } else {
            Ok(None)
        }
    }

    /// Return the number of messages in `list_messages(1)`.
    pub async fn get_inbox_count(&mut self) -> Result<usize> {
        let messages = self.list_messages(1).await?;
        Ok(messages.len())
    }

    /// Return the number of messages in `list_messages(page)`.
    pub async fn get_inbox_count_page(&mut self, page: i32) -> Result<usize> {
        let messages = self.list_messages(page).await?;
        Ok(messages.len())
    }

    /// Return `(count, latest)` for `list_messages(1)`.
    ///
    /// `latest` is the first message in the returned vector, if any.
    pub async fn get_inbox_summary(&mut self) -> Result<(usize, Option<Message>)> {
        let messages = self.list_messages(1).await?;
        let count = messages.len();
        let latest = messages.get(0).cloned();
        Ok((count, latest))
    }

    /// Return `(count, latest)` for `list_messages(page)`.
    ///
    /// `latest` is the first message in the returned vector, if any.
    pub async fn get_inbox_summary_page(&mut self, page: i32) -> Result<(usize, Option<Message>)> {
        let messages = self.list_messages(page).await?;
        let count = messages.len();
        let latest = messages.get(0).cloned();
        Ok((count, latest))
    }

    /// Download an attachment and return its raw bytes.
    ///
    /// The `attachment` URL is typically obtained from [`MessageContent::attachments`].
    /// If the URL is relative, it is resolved against the client's `base_url`.
    ///
    /// # Errors
    /// - Returns [`Error::Http`] for transport failures and timeouts.
    /// - Returns [`Error::Status`] if the download endpoint responds with a non-success HTTP status.
    pub async fn download_attachment(&mut self, attachment: &Attachment) -> Result<Vec<u8>> {
        if self.yp_token.is_none() {
            self.open_inbox().await?;
        }
        self.set_default_cookies();

        let headers = build_headers(DEFAULT_HEADERS, MAIL_HEADERS);
        let url = normalize_url(&attachment.url, &self.base_url);
        let resp = self.client.get(url).headers(headers).send().await?;
        let status = resp.status();
        let bytes = resp.bytes().await?;
        if !status.is_success() {
            return Err(Error::Status {
                status,
                body: format!("failed to download attachment: {}", status),
            });
        }
        Ok(bytes.to_vec())
    }

    /// Construct the RSS feed URL for a mailbox.
    ///
    /// If `mailbox` is `None`, this uses the mailbox configured on the client.
    pub fn get_rss_feed_url(&self, mailbox: Option<&str>) -> String {
        let target = mailbox.unwrap_or(&self.mailbox);
        format!("{}/rss?login={}", self.base_url, target)
    }

    /// Generate (or resolve) the RSS feed for a mailbox and return `(rss_url, items)`.
    ///
    /// This first requests the "gen-rss" endpoint to obtain a feed URL (including a hash parameter),
    /// then downloads that RSS XML and parses `<item>` entries.
    ///
    /// Parsing is best-effort:
    /// - `RssItem::sender` is inferred by scanning the item's description for an email address
    /// - missing fields fall back to placeholder strings (for example `"No Subject"`)
    ///
    /// # Errors
    /// - Returns [`Error::Http`] for transport failures and timeouts.
    /// - This method does not currently validate HTTP status codes for the RSS endpoints; if the
    ///   server returns a non-2xx response body that can be read as text, the method may still
    ///   return `Ok((rss_url, items))` with an empty or partial parse.
    pub async fn get_rss_feed_data(
        &mut self,
        mailbox: Option<&str>,
    ) -> Result<(String, Vec<RssItem>)> {
        let target = mailbox.unwrap_or(&self.mailbox);
        let gen_url = format!("{}/gen-rss?login={}", self.base_url, target);

        let resp = self.client.get(&gen_url).send().await?;
        let body = resp.text().await?;
        let rss_url = extract_rss_url(&body, &self.base_url, target);

        let rss_resp = self.client.get(&rss_url).send().await?;
        let rss_body = rss_resp.text().await?;
        let items = parse_rss_items(&rss_body);
        Ok((rss_url, items))
    }

    fn set_default_cookies(&self) {
        let base: reqwest::Url = self
            .base_url
            .parse()
            .expect("base URL should be valid");
        let time_now = current_time_cookie();
        self.jar
            .add_cookie_str(&format!("ytime={}; Domain=.yopmail.com; Path=/", time_now), &base);
        self.jar.add_cookie_str(
            &format!("ywm={}; Domain=.yopmail.com; Path=/", self.mailbox),
            &base,
        );
    }
}

fn current_time_cookie() -> String {
    use chrono::prelude::*;
    let now: DateTime<Utc> = SystemTime::now().into();
    now.format("%H:%M").to_string()
}

fn extract_yp_token(body: &str) -> Option<String> {
    let doc = Html::parse_document(body);
    let selector = Selector::parse("input#yp").ok()?;
    for node in doc.select(&selector) {
        if let Some(value) = node.value().attr("value") {
            return Some(value.to_string());
        }
    }
    None
}

fn parse_messages(body: &str) -> Vec<Message> {
    let doc = Html::parse_document(body);
    let message_sel = Selector::parse(".m").ok();
    let subject_sel = Selector::parse(".lsub, .lms").ok();
    let sender_sel = Selector::parse(".lmf").ok();
    let time_sel = Selector::parse(".lmh").ok();

    let mut messages = Vec::new();
    if let Some(msg_sel) = message_sel {
        for el in doc.select(&msg_sel) {
            let id = el
                .value()
                .id()
                .map(|s| s.to_string())
                .unwrap_or_else(|| "".into());
            if id.is_empty() {
                continue;
            }

            let subject = subject_sel
                .as_ref()
                .and_then(|sel| el.select(sel).next())
                .map(|n| n.text().collect::<String>().trim().to_string())
                .unwrap_or_default();

            let sender = sender_sel
                .as_ref()
                .and_then(|sel| el.select(sel).next())
                .map(|n| n.text().collect::<String>().trim().to_string());

            let time = time_sel
                .as_ref()
                .and_then(|sel| el.select(sel).next())
                .map(|n| n.text().collect::<String>().trim().to_string());

            messages.push(Message {
                id,
                subject,
                sender,
                date: None,
                time,
            });
        }
    }
    messages
}

fn extract_message_body(body: &str) -> String {
    let doc = Html::parse_document(body);
    let selectors = [
        "#mailctn #mail",
        "#mailctn",
        "#mail",
        "div.mail-body",
        "div.mail",
        "div.message",
        "div.content",
        "div.body",
    ];
    for sel in selectors {
        if let Ok(selector) = Selector::parse(sel) {
            if let Some(node) = doc.select(&selector).next() {
                let text = node.text().collect::<String>();
                if text.trim().len() > 5 {
                    return clean_text(&text);
                }
            }
        }
    }
    clean_text(body)
}

fn extract_message_html(body: &str) -> String {
    let doc = Html::parse_document(body);
    let selectors = [
        "#mailctn #mail",
        "#mailctn",
        "#mail",
        "div.mail-body",
        "div.mail",
        "div.message",
        "div.content",
        "div.body",
    ];
    for sel in selectors {
        if let Ok(selector) = Selector::parse(sel) {
            if let Some(node) = doc.select(&selector).next() {
                let html = node.inner_html();
                if html.trim().len() > 5 {
                    return html;
                }
            }
        }
    }
    body.to_string()
}

fn extract_attachments(body: &str, base: &str) -> Vec<Attachment> {
    let doc = Html::parse_document(body);
    let mut seen = HashSet::new();
    let mut attachments = Vec::new();
    if let Ok(sel) = Selector::parse("a.pj") {
        for node in doc.select(&sel) {
            if let Some(href) = node.value().attr("href") {
                let url = normalize_url(href, base);
                if seen.insert(url.clone()) {
                    let name = node
                        .value()
                        .attr("title")
                        .map(|s| s.to_string())
                        .or_else(|| {
                            let txt = node.text().collect::<String>().trim().to_string();
                            if txt.is_empty() {
                                None
                            } else {
                                Some(txt)
                            }
                        });
                    attachments.push(Attachment { name, url });
                }
            }
        }
    }

    if let Ok(re) = Regex::new(r#"(/downmail\?[^"' ]+)"#) {
        for cap in re.captures_iter(body) {
            if let Some(m) = cap.get(1) {
                let url = normalize_url(m.as_str(), base);
                if seen.insert(url.clone()) {
                    attachments.push(Attachment { name: None, url });
                }
            }
        }
    }

    attachments
}

fn normalize_url(href: &str, base: &str) -> String {
    if href.starts_with("http://") || href.starts_with("https://") {
        href.to_string()
    } else if href.starts_with('/') {
        format!("{}{}", base.trim_end_matches('/'), href)
    } else {
        format!("{}/{}", base.trim_end_matches('/'), href)
    }
}

fn clean_text(input: &str) -> String {
    let mut out = String::with_capacity(input.len());
    let mut last_ws = false;
    for c in input.chars() {
        if c.is_whitespace() {
            if !last_ws {
                out.push(' ');
            }
            last_ws = true;
        } else {
            out.push(c);
            last_ws = false;
        }
    }
    out.trim().to_string()
}

fn extract_rss_url(gen_body: &str, base: &str, mailbox: &str) -> String {
    let pattern = format!(r#"href="(/rss\?login={}&h=[^"]+)""#, regex::escape(mailbox));
    let re = Regex::new(&pattern).ok();
    if let Some(re) = re {
        if let Some(caps) = re.captures(gen_body) {
            if let Some(path) = caps.get(1) {
                return format!("{}{}", base, path.as_str());
            }
        }
    }
    format!("{}/rss?login={}", base, mailbox)
}

fn parse_rss_items(body: &str) -> Vec<RssItem> {
    let doc = Html::parse_document(body);
    let item_sel = Selector::parse("item").ok();
    let title_sel = Selector::parse("title").ok();
    let link_sel = Selector::parse("link").ok();
    let date_sel = Selector::parse("pubdate").ok();
    let desc_sel = Selector::parse("description").ok();

    let mut items = Vec::new();
    if let Some(item_sel) = item_sel {
        for node in doc.select(&item_sel) {
            let subject = title_sel
                .as_ref()
                .and_then(|sel| node.select(sel).next())
                .map(|n| n.text().collect::<String>().trim().to_string())
                .unwrap_or_else(|| "No Subject".into());
            let url = link_sel
                .as_ref()
                .and_then(|sel| node.select(sel).next())
                .map(|n| n.text().collect::<String>().trim().to_string())
                .unwrap_or_default();
            let date = date_sel
                .as_ref()
                .and_then(|sel| node.select(sel).next())
                .map(|n| n.text().collect::<String>().trim().to_string())
                .unwrap_or_else(|| "Unknown Date".into());
            let description = desc_sel
                .as_ref()
                .and_then(|sel| node.select(sel).next())
                .map(|n| n.text().collect::<String>().trim().to_string());
            let sender = description
                .as_ref()
                .and_then(|desc| find_email(desc))
                .unwrap_or_else(|| "Unknown".into());

            items.push(RssItem {
                subject,
                sender,
                date,
                url,
                description,
            });
        }
    }
    items
}

fn find_email(text: &str) -> Option<String> {
    let re = Regex::new(r"([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})").ok()?;
    re.captures(text)
        .and_then(|caps| caps.get(1))
        .map(|m| m.as_str().to_string())
}

/// Generate a random mailbox name.
///
/// The generated value:
/// - uses ASCII alphanumeric characters only
/// - is lowercased
/// - clamps `len` to the inclusive range `6..=32`
pub fn generate_random_mailbox(len: usize) -> String {
    let length = len.max(6).min(32);
    let mut rng = rand::thread_rng();
    let raw: String = (0..length)
        .map(|_| rng.sample(Alphanumeric) as char)
        .collect();
    raw.to_lowercase()
}