deltachat/
mimefactory.rs

1//! # MIME message production.
2
3use std::collections::HashSet;
4use std::io::Cursor;
5use std::path::Path;
6
7use anyhow::{bail, Context as _, Result};
8use base64::Engine as _;
9use chrono::TimeZone;
10use deltachat_contact_tools::sanitize_bidi_characters;
11use mail_builder::headers::address::{Address, EmailAddress};
12use mail_builder::headers::HeaderType;
13use mail_builder::mime::MimePart;
14use tokio::fs;
15
16use crate::aheader::{Aheader, EncryptPreference};
17use crate::blob::BlobObject;
18use crate::chat::{self, Chat};
19use crate::config::Config;
20use crate::constants::ASM_SUBJECT;
21use crate::constants::{Chattype, DC_FROM_HANDSHAKE};
22use crate::contact::{Contact, ContactId, Origin};
23use crate::context::Context;
24use crate::e2ee::EncryptHelper;
25use crate::ephemeral::Timer as EphemeralTimer;
26use crate::key::DcKey;
27use crate::location;
28use crate::message::{self, Message, MsgId, Viewtype};
29use crate::mimeparser::{is_hidden, SystemMessage};
30use crate::param::Param;
31use crate::peer_channels::create_iroh_header;
32use crate::peerstate::Peerstate;
33use crate::simplify::escape_message_footer_marks;
34use crate::stock_str;
35use crate::tools::IsNoneOrEmpty;
36use crate::tools::{
37    create_outgoing_rfc724_mid, create_smeared_timestamp, remove_subject_prefix, time,
38};
39use crate::webxdc::StatusUpdateSerial;
40
41// attachments of 25 mb brutto should work on the majority of providers
42// (brutto examples: web.de=50, 1&1=40, t-online.de=32, gmail=25, posteo=50, yahoo=25, all-inkl=100).
43// to get the netto sizes, we subtract 1 mb header-overhead and the base64-overhead.
44pub const RECOMMENDED_FILE_SIZE: u64 = 24 * 1024 * 1024 / 4 * 3;
45
46#[derive(Debug, Clone)]
47pub enum Loaded {
48    Message {
49        chat: Chat,
50        msg: Message,
51    },
52    Mdn {
53        rfc724_mid: String,
54        additional_msg_ids: Vec<String>,
55    },
56}
57
58/// Helper to construct mime messages.
59#[derive(Debug, Clone)]
60pub struct MimeFactory {
61    from_addr: String,
62    from_displayname: String,
63
64    /// Goes to the `Sender:`-header, if set.
65    /// For overridden names, `sender_displayname` is set to the
66    /// config-name while `from_displayname` is set to the overridden name.
67    /// From the perspective of the receiver,
68    /// a set `Sender:`-header is used as an indicator that the name is overridden;
69    /// names are alsways read from the `From:`-header.
70    sender_displayname: Option<String>,
71
72    selfstatus: String,
73
74    /// Vector of actual recipient addresses.
75    ///
76    /// This is the list of addresses the message should be sent to.
77    /// It is not the same as the `To` header,
78    /// because in case of "member removed" message
79    /// removed member is in the recipient list,
80    /// but not in the `To` header.
81    /// In case of broadcast lists there are multiple recipients,
82    /// but the `To` header has no members.
83    ///
84    /// If `bcc_self` configuration is enabled,
85    /// this list will be extended with own address later,
86    /// but `MimeFactory` is not responsible for this.
87    recipients: Vec<String>,
88
89    /// Vector of pairs of recipient name and address that goes into the `To` field.
90    ///
91    /// The list of actual message recipient addresses may be different,
92    /// e.g. if members are hidden for broadcast lists
93    /// or if the keys for some recipients are missing
94    /// and encrypted message cannot be sent to them.
95    to: Vec<(String, String)>,
96
97    /// Vector of pairs of past group member names and addresses.
98    past_members: Vec<(String, String)>,
99
100    /// Timestamps of the members in the same order as in the `to`
101    /// followed by `past_members`.
102    ///
103    /// If this is not empty, its length
104    /// should be the sum of `recipients` and `past_members` length.
105    member_timestamps: Vec<i64>,
106
107    timestamp: i64,
108    loaded: Loaded,
109    in_reply_to: String,
110
111    /// List of Message-IDs for `References` header.
112    references: Vec<String>,
113
114    /// True if the message requests Message Disposition Notification
115    /// using `Chat-Disposition-Notification-To` header.
116    req_mdn: bool,
117
118    last_added_location_id: Option<u32>,
119
120    /// If the created mime-structure contains sync-items,
121    /// the IDs of these items are listed here.
122    /// The IDs are returned via `RenderedEmail`
123    /// and must be deleted if the message is actually queued for sending.
124    sync_ids_to_delete: Option<String>,
125
126    /// True if the avatar should be attached.
127    pub attach_selfavatar: bool,
128}
129
130/// Result of rendering a message, ready to be submitted to a send job.
131#[derive(Debug, Clone)]
132pub struct RenderedEmail {
133    pub message: String,
134    // pub envelope: Envelope,
135    pub is_encrypted: bool,
136    pub last_added_location_id: Option<u32>,
137
138    /// A comma-separated string of sync-IDs that are used by the rendered email and must be deleted
139    /// from `multi_device_sync` once the message is actually queued for sending.
140    pub sync_ids_to_delete: Option<String>,
141
142    /// Message ID (Message in the sense of Email)
143    pub rfc724_mid: String,
144
145    /// Message subject.
146    pub subject: String,
147}
148
149fn new_address_with_name(name: &str, address: String) -> Address<'static> {
150    Address::new_address(
151        if name == address || name.is_empty() {
152            None
153        } else {
154            Some(name.to_string())
155        },
156        address,
157    )
158}
159
160impl MimeFactory {
161    pub async fn from_msg(context: &Context, msg: Message) -> Result<MimeFactory> {
162        let now = time();
163        let chat = Chat::load_from_db(context, msg.chat_id).await?;
164        let attach_profile_data = Self::should_attach_profile_data(&msg);
165        let undisclosed_recipients = chat.typ == Chattype::Broadcast;
166
167        let from_addr = context.get_primary_self_addr().await?;
168        let config_displayname = context
169            .get_config(Config::Displayname)
170            .await?
171            .unwrap_or_default();
172        let (from_displayname, sender_displayname) =
173            if let Some(override_name) = msg.param.get(Param::OverrideSenderDisplayname) {
174                (override_name.to_string(), Some(config_displayname))
175            } else {
176                let name = match attach_profile_data {
177                    true => config_displayname,
178                    false => "".to_string(),
179                };
180                (name, None)
181            };
182
183        let mut recipients = Vec::new();
184        let mut to = Vec::new();
185        let mut past_members = Vec::new();
186        let mut member_timestamps = Vec::new();
187        let mut recipient_ids = HashSet::new();
188        let mut req_mdn = false;
189
190        if chat.is_self_talk() {
191            to.push((from_displayname.to_string(), from_addr.to_string()));
192        } else if chat.is_mailing_list() {
193            let list_post = chat
194                .param
195                .get(Param::ListPost)
196                .context("Can't write to mailinglist without ListPost param")?;
197            to.push(("".to_string(), list_post.to_string()));
198            recipients.push(list_post.to_string());
199        } else {
200            let email_to_remove = if msg.param.get_cmd() == SystemMessage::MemberRemovedFromGroup {
201                msg.param.get(Param::Arg)
202            } else {
203                None
204            };
205
206            context
207                .sql
208                .query_map(
209                    "SELECT c.authname, c.addr, c.id, cc.add_timestamp, cc.remove_timestamp
210                     FROM chats_contacts cc
211                     LEFT JOIN contacts c ON cc.contact_id=c.id
212                     WHERE cc.chat_id=? AND (cc.contact_id>9 OR (cc.contact_id=1 AND ?))",
213                    (msg.chat_id, chat.typ == Chattype::Group),
214                    |row| {
215                        let authname: String = row.get(0)?;
216                        let addr: String = row.get(1)?;
217                        let id: ContactId = row.get(2)?;
218                        let add_timestamp: i64 = row.get(3)?;
219                        let remove_timestamp: i64 = row.get(4)?;
220                        Ok((authname, addr, id, add_timestamp, remove_timestamp))
221                    },
222                    |rows| {
223                        let mut past_member_timestamps = Vec::new();
224
225                        for row in rows {
226                            let (authname, addr, id, add_timestamp, remove_timestamp) = row?;
227                            let addr = if id == ContactId::SELF {
228                                from_addr.to_string()
229                            } else {
230                                addr
231                            };
232                            let name = match attach_profile_data {
233                                true => authname,
234                                false => "".to_string(),
235                            };
236                            if add_timestamp >= remove_timestamp {
237                                if !recipients_contain_addr(&to, &addr) {
238                                    recipients.push(addr.clone());
239                                    if !undisclosed_recipients {
240                                        to.push((name, addr));
241                                        member_timestamps.push(add_timestamp);
242                                    }
243                                }
244                                recipient_ids.insert(id);
245                            } else if remove_timestamp.saturating_add(60 * 24 * 3600) > now {
246                                // Row is a tombstone,
247                                // member is not actually part of the group.
248                                if !recipients_contain_addr(&past_members, &addr) {
249                                    if let Some(email_to_remove) = email_to_remove {
250                                        if email_to_remove == addr {
251                                            // This is a "member removed" message,
252                                            // we need to notify removed member
253                                            // that it was removed.
254                                            recipients.push(addr.clone());
255                                        }
256                                    }
257                                    if !undisclosed_recipients {
258                                        past_members.push((name, addr));
259                                        past_member_timestamps.push(remove_timestamp);
260                                    }
261                                }
262                            }
263                        }
264
265                        debug_assert!(member_timestamps.len() >= to.len());
266
267                        if to.len() > 1 {
268                            if let Some(position) = to.iter().position(|(_, x)| x == &from_addr) {
269                                to.remove(position);
270                                member_timestamps.remove(position);
271                            }
272                        }
273
274                        member_timestamps.extend(past_member_timestamps);
275                        Ok(())
276                    },
277                )
278                .await?;
279            let recipient_ids: Vec<_> = recipient_ids.into_iter().collect();
280            ContactId::scaleup_origin(context, &recipient_ids, Origin::OutgoingTo).await?;
281
282            if !msg.is_system_message()
283                && msg.param.get_int(Param::Reaction).unwrap_or_default() == 0
284                && context.should_request_mdns().await?
285            {
286                req_mdn = true;
287            }
288        }
289        let (in_reply_to, references) = context
290            .sql
291            .query_row(
292                "SELECT mime_in_reply_to, IFNULL(mime_references, '')
293                 FROM msgs WHERE id=?",
294                (msg.id,),
295                |row| {
296                    let in_reply_to: String = row.get(0)?;
297                    let references: String = row.get(1)?;
298
299                    Ok((in_reply_to, references))
300                },
301            )
302            .await?;
303        let references: Vec<String> = references
304            .trim()
305            .split_ascii_whitespace()
306            .map(|s| s.trim_start_matches('<').trim_end_matches('>').to_string())
307            .collect();
308        let selfstatus = match attach_profile_data {
309            true => context
310                .get_config(Config::Selfstatus)
311                .await?
312                .unwrap_or_default(),
313            false => "".to_string(),
314        };
315        let attach_selfavatar = Self::should_attach_selfavatar(context, &msg).await;
316
317        debug_assert!(
318            member_timestamps.is_empty()
319                || to.len() + past_members.len() == member_timestamps.len()
320        );
321        let factory = MimeFactory {
322            from_addr,
323            from_displayname,
324            sender_displayname,
325            selfstatus,
326            recipients,
327            to,
328            past_members,
329            member_timestamps,
330            timestamp: msg.timestamp_sort,
331            loaded: Loaded::Message { msg, chat },
332            in_reply_to,
333            references,
334            req_mdn,
335            last_added_location_id: None,
336            sync_ids_to_delete: None,
337            attach_selfavatar,
338        };
339        Ok(factory)
340    }
341
342    pub async fn from_mdn(
343        context: &Context,
344        from_id: ContactId,
345        rfc724_mid: String,
346        additional_msg_ids: Vec<String>,
347    ) -> Result<MimeFactory> {
348        let contact = Contact::get_by_id(context, from_id).await?;
349        let from_addr = context.get_primary_self_addr().await?;
350        let timestamp = create_smeared_timestamp(context);
351
352        let res = MimeFactory {
353            from_addr,
354            from_displayname: "".to_string(),
355            sender_displayname: None,
356            selfstatus: "".to_string(),
357            recipients: vec![contact.get_addr().to_string()],
358            to: vec![("".to_string(), contact.get_addr().to_string())],
359            past_members: vec![],
360            member_timestamps: vec![],
361            timestamp,
362            loaded: Loaded::Mdn {
363                rfc724_mid,
364                additional_msg_ids,
365            },
366            in_reply_to: String::default(),
367            references: Vec::new(),
368            req_mdn: false,
369            last_added_location_id: None,
370            sync_ids_to_delete: None,
371            attach_selfavatar: false,
372        };
373
374        Ok(res)
375    }
376
377    async fn peerstates_for_recipients(
378        &self,
379        context: &Context,
380    ) -> Result<Vec<(Option<Peerstate>, String)>> {
381        let self_addr = context.get_primary_self_addr().await?;
382
383        let mut res = Vec::new();
384        for addr in self.recipients.iter().filter(|&addr| *addr != self_addr) {
385            res.push((Peerstate::from_addr(context, addr).await?, addr.clone()));
386        }
387
388        Ok(res)
389    }
390
391    fn is_e2ee_guaranteed(&self) -> bool {
392        match &self.loaded {
393            Loaded::Message { chat, msg } => {
394                !msg.param
395                    .get_bool(Param::ForcePlaintext)
396                    .unwrap_or_default()
397                    && (chat.is_protected()
398                        || msg.param.get_bool(Param::GuaranteeE2ee).unwrap_or_default())
399            }
400            Loaded::Mdn { .. } => false,
401        }
402    }
403
404    fn verified(&self) -> bool {
405        match &self.loaded {
406            Loaded::Message { chat, msg } => {
407                chat.is_self_talk() ||
408                    // Securejoin messages are supposed to verify a key.
409                    // In order to do this, it is necessary that they can be sent
410                    // to a key that is not yet verified.
411                    // This has to work independently of whether the chat is protected right now.
412                    chat.is_protected() && msg.get_info_type() != SystemMessage::SecurejoinMessage
413            }
414            Loaded::Mdn { .. } => false,
415        }
416    }
417
418    fn should_force_plaintext(&self) -> bool {
419        match &self.loaded {
420            Loaded::Message { msg, .. } => msg
421                .param
422                .get_bool(Param::ForcePlaintext)
423                .unwrap_or_default(),
424            Loaded::Mdn { .. } => false,
425        }
426    }
427
428    fn should_skip_autocrypt(&self) -> bool {
429        match &self.loaded {
430            Loaded::Message { msg, .. } => {
431                msg.param.get_bool(Param::SkipAutocrypt).unwrap_or_default()
432            }
433            Loaded::Mdn { .. } => true,
434        }
435    }
436
437    fn should_attach_profile_data(msg: &Message) -> bool {
438        msg.param.get_cmd() != SystemMessage::SecurejoinMessage || {
439            let step = msg.param.get(Param::Arg).unwrap_or_default();
440            // Don't attach profile data at the earlier SecureJoin steps:
441            // - The corresponding messages, i.e. "v{c,g}-request" and "v{c,g}-auth-required" are
442            //   deleted right after processing, so other devices won't see the avatar etc.
443            // - It's also good for privacy because the contact isn't yet verified and these
444            //   messages are auto-sent unlike usual unencrypted messages.
445            step == "vg-request-with-auth"
446                || step == "vc-request-with-auth"
447                || step == "vg-member-added"
448                || step == "vc-contact-confirm"
449        }
450    }
451
452    async fn should_attach_selfavatar(context: &Context, msg: &Message) -> bool {
453        Self::should_attach_profile_data(msg)
454            && match chat::shall_attach_selfavatar(context, msg.chat_id).await {
455                Ok(should) => should,
456                Err(err) => {
457                    warn!(
458                        context,
459                        "should_attach_selfavatar: cannot get selfavatar state: {err:#}."
460                    );
461                    false
462                }
463            }
464    }
465
466    fn grpimage(&self) -> Option<String> {
467        match &self.loaded {
468            Loaded::Message { chat, msg } => {
469                let cmd = msg.param.get_cmd();
470
471                match cmd {
472                    SystemMessage::MemberAddedToGroup => {
473                        return chat.param.get(Param::ProfileImage).map(Into::into);
474                    }
475                    SystemMessage::GroupImageChanged => {
476                        return msg.param.get(Param::Arg).map(Into::into)
477                    }
478                    _ => {}
479                }
480
481                if msg
482                    .param
483                    .get_bool(Param::AttachGroupImage)
484                    .unwrap_or_default()
485                {
486                    return chat.param.get(Param::ProfileImage).map(Into::into);
487                }
488
489                None
490            }
491            Loaded::Mdn { .. } => None,
492        }
493    }
494
495    async fn subject_str(&self, context: &Context) -> Result<String> {
496        let subject = match &self.loaded {
497            Loaded::Message { ref chat, msg } => {
498                let quoted_msg_subject = msg.quoted_message(context).await?.map(|m| m.subject);
499
500                if !msg.subject.is_empty() {
501                    return Ok(msg.subject.clone());
502                }
503
504                if (chat.typ == Chattype::Group || chat.typ == Chattype::Broadcast)
505                    && quoted_msg_subject.is_none_or_empty()
506                {
507                    let re = if self.in_reply_to.is_empty() {
508                        ""
509                    } else {
510                        "Re: "
511                    };
512                    return Ok(format!("{}{}", re, chat.name));
513                }
514
515                let parent_subject = if quoted_msg_subject.is_none_or_empty() {
516                    chat.param.get(Param::LastSubject)
517                } else {
518                    quoted_msg_subject.as_deref()
519                };
520                if let Some(last_subject) = parent_subject {
521                    return Ok(format!("Re: {}", remove_subject_prefix(last_subject)));
522                }
523
524                let self_name = match Self::should_attach_profile_data(msg) {
525                    true => context.get_config(Config::Displayname).await?,
526                    false => None,
527                };
528                let self_name = &match self_name {
529                    Some(name) => name,
530                    None => context.get_config(Config::Addr).await?.unwrap_or_default(),
531                };
532                stock_str::subject_for_new_contact(context, self_name).await
533            }
534            Loaded::Mdn { .. } => "Receipt Notification".to_string(), // untranslated to no reveal sender's language
535        };
536
537        Ok(subject)
538    }
539
540    pub fn recipients(&self) -> Vec<String> {
541        self.recipients.clone()
542    }
543
544    /// Consumes a `MimeFactory` and renders it into a message which is then stored in
545    /// `smtp`-table to be used by the SMTP loop
546    pub async fn render(mut self, context: &Context) -> Result<RenderedEmail> {
547        let mut headers = Vec::<(&'static str, HeaderType<'static>)>::new();
548
549        let from = new_address_with_name(&self.from_displayname, self.from_addr.clone());
550
551        let mut to: Vec<Address<'static>> = Vec::new();
552        for (name, addr) in &self.to {
553            to.push(Address::new_address(
554                if name.is_empty() {
555                    None
556                } else {
557                    Some(name.to_string())
558                },
559                addr.clone(),
560            ));
561        }
562
563        let mut past_members: Vec<Address<'static>> = Vec::new(); // Contents of `Chat-Group-Past-Members` header.
564        for (name, addr) in &self.past_members {
565            past_members.push(Address::new_address(
566                if name.is_empty() {
567                    None
568                } else {
569                    Some(name.to_string())
570                },
571                addr.clone(),
572            ));
573        }
574
575        debug_assert!(
576            self.member_timestamps.is_empty()
577                || to.len() + past_members.len() == self.member_timestamps.len()
578        );
579        if to.is_empty() {
580            to.push(hidden_recipients());
581        }
582
583        // Start with Internet Message Format headers in the order of the standard example
584        // <https://datatracker.ietf.org/doc/html/rfc5322#appendix-A.1.1>.
585        headers.push(("From", from.into()));
586
587        if let Some(sender_displayname) = &self.sender_displayname {
588            let sender = new_address_with_name(sender_displayname, self.from_addr.clone());
589            headers.push(("Sender", sender.into()));
590        }
591        headers.push((
592            "To",
593            mail_builder::headers::address::Address::new_list(to.clone()).into(),
594        ));
595        if !past_members.is_empty() {
596            headers.push((
597                "Chat-Group-Past-Members",
598                mail_builder::headers::address::Address::new_list(past_members.clone()).into(),
599            ));
600        }
601
602        if let Loaded::Message { chat, .. } = &self.loaded {
603            if chat.typ == Chattype::Group
604                && !self.member_timestamps.is_empty()
605                && !chat.member_list_is_stale(context).await?
606            {
607                headers.push((
608                    "Chat-Group-Member-Timestamps",
609                    mail_builder::headers::raw::Raw::new(
610                        self.member_timestamps
611                            .iter()
612                            .map(|ts| ts.to_string())
613                            .collect::<Vec<String>>()
614                            .join(" "),
615                    )
616                    .into(),
617                ));
618            }
619        }
620
621        let subject_str = self.subject_str(context).await?;
622        headers.push((
623            "Subject",
624            mail_builder::headers::text::Text::new(subject_str.to_string()).into(),
625        ));
626
627        let date = chrono::DateTime::<chrono::Utc>::from_timestamp(self.timestamp, 0)
628            .unwrap()
629            .to_rfc2822();
630        headers.push(("Date", mail_builder::headers::raw::Raw::new(date).into()));
631
632        let rfc724_mid = match &self.loaded {
633            Loaded::Message { msg, .. } => msg.rfc724_mid.clone(),
634            Loaded::Mdn { .. } => create_outgoing_rfc724_mid(),
635        };
636        headers.push((
637            "Message-ID",
638            mail_builder::headers::message_id::MessageId::new(rfc724_mid.clone()).into(),
639        ));
640
641        // Reply headers as in <https://datatracker.ietf.org/doc/html/rfc5322#appendix-A.2>.
642        if !self.in_reply_to.is_empty() {
643            headers.push((
644                "In-Reply-To",
645                mail_builder::headers::message_id::MessageId::new(self.in_reply_to.clone()).into(),
646            ));
647        }
648        if !self.references.is_empty() {
649            headers.push((
650                "References",
651                mail_builder::headers::message_id::MessageId::<'static>::new_list(
652                    self.references.iter().map(|s| s.to_string()),
653                )
654                .into(),
655            ));
656        }
657
658        // Automatic Response headers <https://www.rfc-editor.org/rfc/rfc3834>
659        if let Loaded::Mdn { .. } = self.loaded {
660            headers.push((
661                "Auto-Submitted",
662                mail_builder::headers::raw::Raw::new("auto-replied".to_string()).into(),
663            ));
664        } else if context.get_config_bool(Config::Bot).await? {
665            headers.push((
666                "Auto-Submitted",
667                mail_builder::headers::raw::Raw::new("auto-generated".to_string()).into(),
668            ));
669        } else if let Loaded::Message { msg, .. } = &self.loaded {
670            if msg.param.get_cmd() == SystemMessage::SecurejoinMessage {
671                let step = msg.param.get(Param::Arg).unwrap_or_default();
672                if step != "vg-request" && step != "vc-request" {
673                    headers.push((
674                        "Auto-Submitted",
675                        mail_builder::headers::raw::Raw::new("auto-replied".to_string()).into(),
676                    ));
677                }
678            }
679        }
680
681        if let Loaded::Message { chat, .. } = &self.loaded {
682            if chat.typ == Chattype::Broadcast {
683                headers.push((
684                    "List-ID",
685                    mail_builder::headers::text::Text::new(format!(
686                        "{} <{}>",
687                        chat.name, chat.grpid
688                    ))
689                    .into(),
690                ));
691            }
692        }
693
694        if let Loaded::Message { msg, .. } = &self.loaded {
695            if let Some(original_rfc724_mid) = msg.param.get(Param::TextEditFor) {
696                headers.push((
697                    "Chat-Edit",
698                    mail_builder::headers::message_id::MessageId::new(
699                        original_rfc724_mid.to_string(),
700                    )
701                    .into(),
702                ));
703            } else if let Some(rfc724_mid_list) = msg.param.get(Param::DeleteRequestFor) {
704                headers.push((
705                    "Chat-Delete",
706                    mail_builder::headers::message_id::MessageId::new(rfc724_mid_list.to_string())
707                        .into(),
708                ));
709            }
710        }
711
712        // Non-standard headers.
713        headers.push((
714            "Chat-Version",
715            mail_builder::headers::raw::Raw::new("1.0").into(),
716        ));
717
718        if self.req_mdn {
719            // we use "Chat-Disposition-Notification-To"
720            // because replies to "Disposition-Notification-To" are weird in many cases
721            // eg. are just freetext and/or do not follow any standard.
722            headers.push((
723                "Chat-Disposition-Notification-To",
724                mail_builder::headers::raw::Raw::new(self.from_addr.clone()).into(),
725            ));
726        }
727
728        let verified = self.verified();
729        let grpimage = self.grpimage();
730        let skip_autocrypt = self.should_skip_autocrypt();
731        let e2ee_guaranteed = self.is_e2ee_guaranteed();
732        let encrypt_helper = EncryptHelper::new(context).await?;
733
734        if !skip_autocrypt {
735            // unless determined otherwise we add the Autocrypt header
736            let aheader = encrypt_helper.get_aheader().to_string();
737            headers.push((
738                "Autocrypt",
739                mail_builder::headers::raw::Raw::new(aheader).into(),
740            ));
741        }
742
743        // Add ephemeral timer for non-MDN messages.
744        // For MDNs it does not matter because they are not visible
745        // and ignored by the receiver.
746        if let Loaded::Message { msg, .. } = &self.loaded {
747            let ephemeral_timer = msg.chat_id.get_ephemeral_timer(context).await?;
748            if let EphemeralTimer::Enabled { duration } = ephemeral_timer {
749                headers.push((
750                    "Ephemeral-Timer",
751                    mail_builder::headers::raw::Raw::new(duration.to_string()).into(),
752                ));
753            }
754        }
755
756        let peerstates = self.peerstates_for_recipients(context).await?;
757        let is_encrypted = !self.should_force_plaintext()
758            && (e2ee_guaranteed || encrypt_helper.should_encrypt(context, &peerstates).await?);
759        let is_securejoin_message = if let Loaded::Message { msg, .. } = &self.loaded {
760            msg.param.get_cmd() == SystemMessage::SecurejoinMessage
761        } else {
762            false
763        };
764
765        let message: MimePart<'static> = match &self.loaded {
766            Loaded::Message { msg, .. } => {
767                let msg = msg.clone();
768                let (main_part, mut parts) = self
769                    .render_message(context, &mut headers, &grpimage, is_encrypted)
770                    .await?;
771                if parts.is_empty() {
772                    // Single part, render as regular message.
773                    main_part
774                } else {
775                    parts.insert(0, main_part);
776
777                    // Multiple parts, render as multipart.
778                    if msg.param.get_cmd() == SystemMessage::MultiDeviceSync {
779                        MimePart::new("multipart/report; report-type=multi-device-sync", parts)
780                    } else if msg.param.get_cmd() == SystemMessage::WebxdcStatusUpdate {
781                        MimePart::new("multipart/report; report-type=status-update", parts)
782                    } else {
783                        MimePart::new("multipart/mixed", parts)
784                    }
785                }
786            }
787            Loaded::Mdn { .. } => self.render_mdn()?,
788        };
789
790        // Split headers based on header confidentiality policy.
791
792        // Headers that must go into IMF header section.
793        //
794        // These are standard headers such as Date, In-Reply-To, References, which cannot be placed
795        // anywhere else according to the standard. Placing headers here also allows them to be fetched
796        // individually over IMAP without downloading the message body. This is why Chat-Version is
797        // placed here.
798        let mut unprotected_headers: Vec<(&'static str, HeaderType<'static>)> = Vec::new();
799
800        // Headers that MUST NOT (only) go into IMF header section:
801        // - Large headers which may hit the header section size limit on the server, such as
802        //   Chat-User-Avatar with a base64-encoded image inside.
803        // - Headers duplicated here that servers mess up with in the IMF header section, like
804        //   Message-ID.
805        // - Nonstandard headers that should be DKIM-protected because e.g. OpenDKIM only signs
806        //   known headers.
807        //
808        // The header should be hidden from MTA
809        // by moving it either into protected part
810        // in case of encrypted mails
811        // or unprotected MIME preamble in case of unencrypted mails.
812        let mut hidden_headers: Vec<(&'static str, HeaderType<'static>)> = Vec::new();
813
814        // Opportunistically protected headers.
815        //
816        // These headers are placed into encrypted part *if* the message is encrypted. Place headers
817        // which are not needed before decryption (e.g. Chat-Group-Name) or are not interesting if the
818        // message cannot be decrypted (e.g. Chat-Disposition-Notification-To) here.
819        //
820        // If the message is not encrypted, these headers are placed into IMF header section, so make
821        // sure that the message will be encrypted if you place any sensitive information here.
822        let mut protected_headers: Vec<(&'static str, HeaderType<'static>)> = Vec::new();
823
824        // MIME header <https://datatracker.ietf.org/doc/html/rfc2045>.
825        unprotected_headers.push((
826            "MIME-Version",
827            mail_builder::headers::raw::Raw::new("1.0").into(),
828        ));
829        for header @ (original_header_name, _header_value) in &headers {
830            let header_name = original_header_name.to_lowercase();
831            if header_name == "message-id" {
832                unprotected_headers.push(header.clone());
833                hidden_headers.push(header.clone());
834            } else if is_hidden(&header_name) {
835                hidden_headers.push(header.clone());
836            } else if header_name == "autocrypt"
837                && !context.get_config_bool(Config::ProtectAutocrypt).await?
838            {
839                unprotected_headers.push(header.clone());
840            } else if header_name == "from" {
841                // Unencrypted securejoin messages should _not_ include the display name:
842                if is_encrypted || !is_securejoin_message {
843                    protected_headers.push(header.clone());
844                }
845
846                unprotected_headers.push((
847                    original_header_name,
848                    Address::new_address(None::<&'static str>, self.from_addr.clone()).into(),
849                ));
850            } else if header_name == "to" {
851                protected_headers.push(header.clone());
852                if is_encrypted {
853                    let mut to_without_names = to
854                        .clone()
855                        .into_iter()
856                        .filter_map(|header| match header {
857                            Address::Address(mb) => Some(Address::Address(EmailAddress {
858                                name: None,
859                                email: mb.email,
860                            })),
861                            _ => None,
862                        })
863                        .collect::<Vec<_>>();
864                    if to_without_names.is_empty() {
865                        to_without_names.push(hidden_recipients());
866                    }
867                    unprotected_headers.push((
868                        original_header_name,
869                        Address::new_list(to_without_names).into(),
870                    ));
871                } else {
872                    unprotected_headers.push(header.clone());
873                }
874            } else if is_encrypted {
875                protected_headers.push(header.clone());
876
877                match header_name.as_str() {
878                    "subject" => {
879                        unprotected_headers.push((
880                            "Subject",
881                            mail_builder::headers::raw::Raw::new("[...]").into(),
882                        ));
883                    }
884                    "date"
885                    | "in-reply-to"
886                    | "references"
887                    | "auto-submitted"
888                    | "chat-version"
889                    | "autocrypt-setup-message" => {
890                        unprotected_headers.push(header.clone());
891                    }
892                    _ => {
893                        // Other headers are removed from unprotected part.
894                    }
895                }
896            } else {
897                // Copy the header to the protected headers
898                // in case of signed-only message.
899                // If the message is not signed, this value will not be used.
900                protected_headers.push(header.clone());
901                unprotected_headers.push(header.clone())
902            }
903        }
904
905        let outer_message = if is_encrypted {
906            // Store protected headers in the inner message.
907            let message = protected_headers
908                .into_iter()
909                .fold(message, |message, (header, value)| {
910                    message.header(header, value)
911                });
912
913            // Add hidden headers to encrypted payload.
914            let mut message: MimePart<'static> = hidden_headers
915                .into_iter()
916                .fold(message, |message, (header, value)| {
917                    message.header(header, value)
918                });
919
920            // Add gossip headers in chats with multiple recipients
921            let multiple_recipients =
922                peerstates.len() > 1 || context.get_config_bool(Config::BccSelf).await?;
923
924            let gossip_period = context.get_config_i64(Config::GossipPeriod).await?;
925            let now = time();
926
927            match &self.loaded {
928                Loaded::Message { chat, msg } => {
929                    if chat.typ != Chattype::Broadcast {
930                        for peerstate in peerstates.iter().filter_map(|(state, _)| state.as_ref()) {
931                            let Some(key) = peerstate.peek_key(verified) else {
932                                continue;
933                            };
934
935                            let fingerprint = key.dc_fingerprint().hex();
936                            let cmd = msg.param.get_cmd();
937                            let should_do_gossip = cmd == SystemMessage::MemberAddedToGroup
938                                || cmd == SystemMessage::SecurejoinMessage
939                                || multiple_recipients && {
940                                    let gossiped_timestamp: Option<i64> = context
941                                        .sql
942                                        .query_get_value(
943                                            "SELECT timestamp
944                                         FROM gossip_timestamp
945                                         WHERE chat_id=? AND fingerprint=?",
946                                            (chat.id, &fingerprint),
947                                        )
948                                        .await?;
949
950                                    // `gossip_period == 0` is a special case for testing,
951                                    // enabling gossip in every message.
952                                    //
953                                    // If current time is in the past compared to
954                                    // `gossiped_timestamp`, we also gossip because
955                                    // either the `gossiped_timestamp` or clock is wrong.
956                                    gossip_period == 0
957                                        || gossiped_timestamp
958                                            .is_none_or(|ts| now >= ts + gossip_period || now < ts)
959                                };
960
961                            if !should_do_gossip {
962                                continue;
963                            }
964
965                            let header = Aheader::new(
966                                peerstate.addr.clone(),
967                                key.clone(),
968                                // Autocrypt 1.1.0 specification says that
969                                // `prefer-encrypt` attribute SHOULD NOT be included.
970                                EncryptPreference::NoPreference,
971                            )
972                            .to_string();
973
974                            message = message.header(
975                                "Autocrypt-Gossip",
976                                mail_builder::headers::raw::Raw::new(header),
977                            );
978
979                            context
980                                .sql
981                                .execute(
982                                    "INSERT INTO gossip_timestamp (chat_id, fingerprint, timestamp)
983                                     VALUES                       (?, ?, ?)
984                                     ON CONFLICT                  (chat_id, fingerprint)
985                                     DO UPDATE SET timestamp=excluded.timestamp",
986                                    (chat.id, &fingerprint, now),
987                                )
988                                .await?;
989                        }
990                    }
991                }
992                Loaded::Mdn { .. } => {
993                    // Never gossip in MDNs.
994                }
995            }
996
997            // Set the appropriate Content-Type for the inner message.
998            for (h, ref mut v) in &mut message.headers {
999                if h == "Content-Type" {
1000                    if let mail_builder::headers::HeaderType::ContentType(ref mut ct) = v {
1001                        *ct = ct.clone().attribute("protected-headers", "v1");
1002                    }
1003                }
1004            }
1005
1006            // Disable compression for SecureJoin to ensure
1007            // there are no compression side channels
1008            // leaking information about the tokens.
1009            let compress = match &self.loaded {
1010                Loaded::Message { msg, .. } => {
1011                    msg.param.get_cmd() != SystemMessage::SecurejoinMessage
1012                }
1013                Loaded::Mdn { .. } => true,
1014            };
1015
1016            let (encryption_keyring, missing_key_addresses) =
1017                encrypt_helper.encryption_keyring(context, verified, &peerstates)?;
1018
1019            // XXX: additional newline is needed
1020            // to pass filtermail at
1021            // <https://github.com/deltachat/chatmail/blob/4d915f9800435bf13057d41af8d708abd34dbfa8/chatmaild/src/chatmaild/filtermail.py#L84-L86>
1022            let encrypted = encrypt_helper
1023                .encrypt(context, encryption_keyring, message, compress)
1024                .await?
1025                + "\n";
1026
1027            // Remove recipients for which the key is missing.
1028            if !missing_key_addresses.is_empty() {
1029                self.recipients
1030                    .retain(|addr| !missing_key_addresses.contains(addr));
1031            }
1032
1033            // Set the appropriate Content-Type for the outer message
1034            MimePart::new(
1035                "multipart/encrypted; protocol=\"application/pgp-encrypted\"",
1036                vec![
1037                    // Autocrypt part 1
1038                    MimePart::new("application/pgp-encrypted", "Version: 1\r\n").header(
1039                        "Content-Description",
1040                        mail_builder::headers::raw::Raw::new("PGP/MIME version identification"),
1041                    ),
1042                    // Autocrypt part 2
1043                    MimePart::new(
1044                        "application/octet-stream; name=\"encrypted.asc\"",
1045                        encrypted,
1046                    )
1047                    .header(
1048                        "Content-Description",
1049                        mail_builder::headers::raw::Raw::new("OpenPGP encrypted message"),
1050                    )
1051                    .header(
1052                        "Content-Disposition",
1053                        mail_builder::headers::raw::Raw::new("inline; filename=\"encrypted.asc\";"),
1054                    ),
1055                ],
1056            )
1057        } else if matches!(self.loaded, Loaded::Mdn { .. }) {
1058            // Never add outer multipart/mixed wrapper to MDN
1059            // as multipart/report Content-Type is used to recognize MDNs
1060            // by Delta Chat receiver and Chatmail servers
1061            // allowing them to be unencrypted and not contain Autocrypt header
1062            // without resetting Autocrypt encryption or triggering Chatmail filter
1063            // that normally only allows encrypted mails.
1064
1065            // Hidden headers are dropped.
1066            message
1067        } else {
1068            let message = hidden_headers
1069                .into_iter()
1070                .fold(message, |message, (header, value)| {
1071                    message.header(header, value)
1072                });
1073            let message = MimePart::new("multipart/mixed", vec![message]);
1074            let mut message = protected_headers
1075                .iter()
1076                .fold(message, |message, (header, value)| {
1077                    message.header(*header, value.clone())
1078                });
1079
1080            if skip_autocrypt || !context.get_config_bool(Config::SignUnencrypted).await? {
1081                // Deduplicate unprotected headers that also are in the protected headers:
1082                let protected: HashSet<&str> =
1083                    HashSet::from_iter(protected_headers.iter().map(|(header, _value)| *header));
1084                unprotected_headers.retain(|(header, _value)| !protected.contains(header));
1085
1086                message
1087            } else {
1088                for (h, ref mut v) in &mut message.headers {
1089                    if h == "Content-Type" {
1090                        if let mail_builder::headers::HeaderType::ContentType(ref mut ct) = v {
1091                            *ct = ct.clone().attribute("protected-headers", "v1");
1092                        }
1093                    }
1094                }
1095
1096                let signature = encrypt_helper.sign(context, &message).await?;
1097                MimePart::new(
1098                    "multipart/signed; protocol=\"application/pgp-signature\"; protected",
1099                    vec![
1100                        message,
1101                        MimePart::new(
1102                            "application/pgp-signature; name=\"signature.asc\"",
1103                            signature,
1104                        )
1105                        .header(
1106                            "Content-Description",
1107                            mail_builder::headers::raw::Raw::<'static>::new(
1108                                "OpenPGP digital signature",
1109                            ),
1110                        )
1111                        .attachment("signature"),
1112                    ],
1113                )
1114            }
1115        };
1116
1117        // Store the unprotected headers on the outer message.
1118        let outer_message = unprotected_headers
1119            .into_iter()
1120            .fold(outer_message, |message, (header, value)| {
1121                message.header(header, value)
1122            });
1123
1124        let MimeFactory {
1125            last_added_location_id,
1126            ..
1127        } = self;
1128
1129        let mut buffer = Vec::new();
1130        let cursor = Cursor::new(&mut buffer);
1131        outer_message.clone().write_part(cursor).ok();
1132        let message = String::from_utf8_lossy(&buffer).to_string();
1133
1134        Ok(RenderedEmail {
1135            message,
1136            // envelope: Envelope::new,
1137            is_encrypted,
1138            last_added_location_id,
1139            sync_ids_to_delete: self.sync_ids_to_delete,
1140            rfc724_mid,
1141            subject: subject_str,
1142        })
1143    }
1144
1145    /// Returns MIME part with a `message.kml` attachment.
1146    fn get_message_kml_part(&self) -> Option<MimePart<'static>> {
1147        let Loaded::Message { msg, .. } = &self.loaded else {
1148            return None;
1149        };
1150
1151        let latitude = msg.param.get_float(Param::SetLatitude)?;
1152        let longitude = msg.param.get_float(Param::SetLongitude)?;
1153
1154        let kml_file = location::get_message_kml(msg.timestamp_sort, latitude, longitude);
1155        let part = MimePart::new("application/vnd.google-earth.kml+xml", kml_file)
1156            .attachment("message.kml");
1157        Some(part)
1158    }
1159
1160    /// Returns MIME part with a `location.kml` attachment.
1161    async fn get_location_kml_part(
1162        &mut self,
1163        context: &Context,
1164    ) -> Result<Option<MimePart<'static>>> {
1165        let Loaded::Message { msg, .. } = &self.loaded else {
1166            return Ok(None);
1167        };
1168
1169        let Some((kml_content, last_added_location_id)) =
1170            location::get_kml(context, msg.chat_id).await?
1171        else {
1172            return Ok(None);
1173        };
1174
1175        let part = MimePart::new("application/vnd.google-earth.kml+xml", kml_content)
1176            .attachment("location.kml");
1177        if !msg.param.exists(Param::SetLatitude) {
1178            // otherwise, the independent location is already filed
1179            self.last_added_location_id = Some(last_added_location_id);
1180        }
1181        Ok(Some(part))
1182    }
1183
1184    async fn render_message(
1185        &mut self,
1186        context: &Context,
1187        headers: &mut Vec<(&'static str, HeaderType<'static>)>,
1188        grpimage: &Option<String>,
1189        is_encrypted: bool,
1190    ) -> Result<(MimePart<'static>, Vec<MimePart<'static>>)> {
1191        let Loaded::Message { chat, msg } = &self.loaded else {
1192            bail!("Attempt to render MDN as a message");
1193        };
1194        let chat = chat.clone();
1195        let msg = msg.clone();
1196        let command = msg.param.get_cmd();
1197        let mut placeholdertext = None;
1198
1199        let send_verified_headers = match chat.typ {
1200            Chattype::Single => true,
1201            Chattype::Group => true,
1202            // Mailinglists and broadcast lists can actually never be verified:
1203            Chattype::Mailinglist => false,
1204            Chattype::Broadcast => false,
1205        };
1206        if chat.is_protected() && send_verified_headers {
1207            headers.push((
1208                "Chat-Verified",
1209                mail_builder::headers::raw::Raw::new("1").into(),
1210            ));
1211        }
1212
1213        if chat.typ == Chattype::Group {
1214            // Send group ID unless it is an ad hoc group that has no ID.
1215            if !chat.grpid.is_empty() {
1216                headers.push((
1217                    "Chat-Group-ID",
1218                    mail_builder::headers::raw::Raw::new(chat.grpid.clone()).into(),
1219                ));
1220            }
1221
1222            headers.push((
1223                "Chat-Group-Name",
1224                mail_builder::headers::text::Text::new(chat.name.to_string()).into(),
1225            ));
1226            if let Some(ts) = chat.param.get_i64(Param::GroupNameTimestamp) {
1227                headers.push((
1228                    "Chat-Group-Name-Timestamp",
1229                    mail_builder::headers::text::Text::new(ts.to_string()).into(),
1230                ));
1231            }
1232
1233            match command {
1234                SystemMessage::MemberRemovedFromGroup => {
1235                    let email_to_remove = msg.param.get(Param::Arg).unwrap_or_default();
1236
1237                    if email_to_remove
1238                        == context
1239                            .get_config(Config::ConfiguredAddr)
1240                            .await?
1241                            .unwrap_or_default()
1242                    {
1243                        placeholdertext = Some(stock_str::msg_group_left_remote(context).await);
1244                    } else {
1245                        placeholdertext =
1246                            Some(stock_str::msg_del_member_remote(context, email_to_remove).await);
1247                    };
1248
1249                    if !email_to_remove.is_empty() {
1250                        headers.push((
1251                            "Chat-Group-Member-Removed",
1252                            mail_builder::headers::raw::Raw::new(email_to_remove.to_string())
1253                                .into(),
1254                        ));
1255                    }
1256                }
1257                SystemMessage::MemberAddedToGroup => {
1258                    let email_to_add = msg.param.get(Param::Arg).unwrap_or_default();
1259                    placeholdertext =
1260                        Some(stock_str::msg_add_member_remote(context, email_to_add).await);
1261
1262                    if !email_to_add.is_empty() {
1263                        headers.push((
1264                            "Chat-Group-Member-Added",
1265                            mail_builder::headers::raw::Raw::new(email_to_add.to_string()).into(),
1266                        ));
1267                    }
1268                    if 0 != msg.param.get_int(Param::Arg2).unwrap_or_default() & DC_FROM_HANDSHAKE {
1269                        info!(
1270                            context,
1271                            "Sending secure-join message {:?}.", "vg-member-added",
1272                        );
1273                        headers.push((
1274                            "Secure-Join",
1275                            mail_builder::headers::raw::Raw::new("vg-member-added".to_string())
1276                                .into(),
1277                        ));
1278                    }
1279                }
1280                SystemMessage::GroupNameChanged => {
1281                    let old_name = msg.param.get(Param::Arg).unwrap_or_default().to_string();
1282                    headers.push((
1283                        "Chat-Group-Name-Changed",
1284                        mail_builder::headers::text::Text::new(old_name).into(),
1285                    ));
1286                }
1287                SystemMessage::GroupImageChanged => {
1288                    headers.push((
1289                        "Chat-Content",
1290                        mail_builder::headers::text::Text::new("group-avatar-changed").into(),
1291                    ));
1292                    if grpimage.is_none() {
1293                        headers.push((
1294                            "Chat-Group-Avatar",
1295                            mail_builder::headers::raw::Raw::new("0").into(),
1296                        ));
1297                    }
1298                }
1299                _ => {}
1300            }
1301        }
1302
1303        match command {
1304            SystemMessage::LocationStreamingEnabled => {
1305                headers.push((
1306                    "Chat-Content",
1307                    mail_builder::headers::raw::Raw::new("location-streaming-enabled").into(),
1308                ));
1309            }
1310            SystemMessage::EphemeralTimerChanged => {
1311                headers.push((
1312                    "Chat-Content",
1313                    mail_builder::headers::raw::Raw::new("ephemeral-timer-changed").into(),
1314                ));
1315            }
1316            SystemMessage::LocationOnly
1317            | SystemMessage::MultiDeviceSync
1318            | SystemMessage::WebxdcStatusUpdate => {
1319                // This should prevent automatic replies,
1320                // such as non-delivery reports.
1321                //
1322                // See <https://tools.ietf.org/html/rfc3834>
1323                //
1324                // Adding this header without encryption leaks some
1325                // information about the message contents, but it can
1326                // already be easily guessed from message timing and size.
1327                headers.push((
1328                    "Auto-Submitted",
1329                    mail_builder::headers::raw::Raw::new("auto-generated").into(),
1330                ));
1331            }
1332            SystemMessage::AutocryptSetupMessage => {
1333                headers.push((
1334                    "Autocrypt-Setup-Message",
1335                    mail_builder::headers::raw::Raw::new("v1").into(),
1336                ));
1337
1338                placeholdertext = Some(ASM_SUBJECT.to_string());
1339            }
1340            SystemMessage::SecurejoinMessage => {
1341                let step = msg.param.get(Param::Arg).unwrap_or_default();
1342                if !step.is_empty() {
1343                    info!(context, "Sending secure-join message {step:?}.");
1344                    headers.push((
1345                        "Secure-Join",
1346                        mail_builder::headers::raw::Raw::new(step.to_string()).into(),
1347                    ));
1348
1349                    let param2 = msg.param.get(Param::Arg2).unwrap_or_default();
1350                    if !param2.is_empty() {
1351                        headers.push((
1352                            if step == "vg-request-with-auth" || step == "vc-request-with-auth" {
1353                                "Secure-Join-Auth"
1354                            } else {
1355                                "Secure-Join-Invitenumber"
1356                            },
1357                            mail_builder::headers::text::Text::new(param2.to_string()).into(),
1358                        ));
1359                    }
1360
1361                    let fingerprint = msg.param.get(Param::Arg3).unwrap_or_default();
1362                    if !fingerprint.is_empty() {
1363                        headers.push((
1364                            "Secure-Join-Fingerprint",
1365                            mail_builder::headers::raw::Raw::new(fingerprint.to_string()).into(),
1366                        ));
1367                    }
1368                    if let Some(id) = msg.param.get(Param::Arg4) {
1369                        headers.push((
1370                            "Secure-Join-Group",
1371                            mail_builder::headers::raw::Raw::new(id.to_string()).into(),
1372                        ));
1373                    };
1374                }
1375            }
1376            SystemMessage::ChatProtectionEnabled => {
1377                headers.push((
1378                    "Chat-Content",
1379                    mail_builder::headers::raw::Raw::new("protection-enabled").into(),
1380                ));
1381            }
1382            SystemMessage::ChatProtectionDisabled => {
1383                headers.push((
1384                    "Chat-Content",
1385                    mail_builder::headers::raw::Raw::new("protection-disabled").into(),
1386                ));
1387            }
1388            SystemMessage::IrohNodeAddr => {
1389                headers.push((
1390                    "Iroh-Node-Addr",
1391                    mail_builder::headers::text::Text::new(serde_json::to_string(
1392                        &context
1393                            .get_or_try_init_peer_channel()
1394                            .await?
1395                            .get_node_addr()
1396                            .await?,
1397                    )?)
1398                    .into(),
1399                ));
1400            }
1401            _ => {}
1402        }
1403
1404        if let Some(grpimage) = grpimage {
1405            info!(context, "setting group image '{}'", grpimage);
1406            let avatar = build_avatar_file(context, grpimage)
1407                .await
1408                .context("Cannot attach group image")?;
1409            headers.push((
1410                "Chat-Group-Avatar",
1411                mail_builder::headers::raw::Raw::new(format!("base64:{avatar}")).into(),
1412            ));
1413        }
1414
1415        if msg.viewtype == Viewtype::Sticker {
1416            headers.push((
1417                "Chat-Content",
1418                mail_builder::headers::raw::Raw::new("sticker").into(),
1419            ));
1420        } else if msg.viewtype == Viewtype::VideochatInvitation {
1421            headers.push((
1422                "Chat-Content",
1423                mail_builder::headers::raw::Raw::new("videochat-invitation").into(),
1424            ));
1425            headers.push((
1426                "Chat-Webrtc-Room",
1427                mail_builder::headers::raw::Raw::new(
1428                    msg.param
1429                        .get(Param::WebrtcRoom)
1430                        .unwrap_or_default()
1431                        .to_string(),
1432                )
1433                .into(),
1434            ));
1435        }
1436
1437        if msg.viewtype == Viewtype::Voice
1438            || msg.viewtype == Viewtype::Audio
1439            || msg.viewtype == Viewtype::Video
1440        {
1441            if msg.viewtype == Viewtype::Voice {
1442                headers.push((
1443                    "Chat-Voice-Message",
1444                    mail_builder::headers::raw::Raw::new("1").into(),
1445                ));
1446            }
1447            let duration_ms = msg.param.get_int(Param::Duration).unwrap_or_default();
1448            if duration_ms > 0 {
1449                let dur = duration_ms.to_string();
1450                headers.push((
1451                    "Chat-Duration",
1452                    mail_builder::headers::raw::Raw::new(dur).into(),
1453                ));
1454            }
1455        }
1456
1457        // add text part - we even add empty text and force a MIME-multipart-message as:
1458        // - some Apps have problems with Non-text in the main part (eg. "Mail" from stock Android)
1459        // - we can add "forward hints" this way
1460        // - it looks better
1461
1462        let afwd_email = msg.param.exists(Param::Forwarded);
1463        let fwdhint = if afwd_email {
1464            Some(
1465                "---------- Forwarded message ----------\r\n\
1466                 From: Delta Chat\r\n\
1467                 \r\n"
1468                    .to_string(),
1469            )
1470        } else {
1471            None
1472        };
1473
1474        let final_text = placeholdertext.as_deref().unwrap_or(&msg.text);
1475
1476        let mut quoted_text = None;
1477        if let Some(msg_quoted_text) = msg.quoted_text() {
1478            let mut some_quoted_text = String::new();
1479            for quoted_line in msg_quoted_text.split('\n') {
1480                some_quoted_text += "> ";
1481                some_quoted_text += quoted_line;
1482                some_quoted_text += "\r\n";
1483            }
1484            some_quoted_text += "\r\n";
1485            quoted_text = Some(some_quoted_text)
1486        }
1487
1488        if !is_encrypted && msg.param.get_bool(Param::ProtectQuote).unwrap_or_default() {
1489            // Message is not encrypted but quotes encrypted message.
1490            quoted_text = Some("> ...\r\n\r\n".to_string());
1491        }
1492        if quoted_text.is_none() && final_text.starts_with('>') {
1493            // Insert empty line to avoid receiver treating user-sent quote as topquote inserted by
1494            // Delta Chat.
1495            quoted_text = Some("\r\n".to_string());
1496        }
1497
1498        let is_reaction = msg.param.get_int(Param::Reaction).unwrap_or_default() != 0;
1499
1500        let footer = if is_reaction { "" } else { &self.selfstatus };
1501
1502        let message_text = format!(
1503            "{}{}{}{}{}{}",
1504            fwdhint.unwrap_or_default(),
1505            quoted_text.unwrap_or_default(),
1506            escape_message_footer_marks(final_text),
1507            if !final_text.is_empty() && !footer.is_empty() {
1508                "\r\n\r\n"
1509            } else {
1510                ""
1511            },
1512            if !footer.is_empty() { "-- \r\n" } else { "" },
1513            footer
1514        );
1515
1516        let mut main_part = MimePart::new("text/plain", message_text);
1517        if is_reaction {
1518            main_part = main_part.header(
1519                "Content-Disposition",
1520                mail_builder::headers::raw::Raw::new("reaction"),
1521            );
1522        }
1523
1524        let mut parts = Vec::new();
1525
1526        // add HTML-part, this is needed only if a HTML-message from a non-delta-client is forwarded;
1527        // for simplificity and to avoid conversion errors, we're generating the HTML-part from the original message.
1528        if msg.has_html() {
1529            let html = if let Some(orig_msg_id) = msg.param.get_int(Param::Forwarded) {
1530                MsgId::new(orig_msg_id.try_into()?)
1531                    .get_html(context)
1532                    .await?
1533            } else {
1534                msg.param.get(Param::SendHtml).map(|s| s.to_string())
1535            };
1536            if let Some(html) = html {
1537                main_part = MimePart::new(
1538                    "multipart/alternative",
1539                    vec![main_part, MimePart::new("text/html", html)],
1540                )
1541            }
1542        }
1543
1544        // add attachment part
1545        if msg.viewtype.has_file() {
1546            let file_part = build_body_file(context, &msg).await?;
1547            parts.push(file_part);
1548        }
1549
1550        if let Some(msg_kml_part) = self.get_message_kml_part() {
1551            parts.push(msg_kml_part);
1552        }
1553
1554        if location::is_sending_locations_to_chat(context, Some(msg.chat_id)).await? {
1555            if let Some(part) = self.get_location_kml_part(context).await? {
1556                parts.push(part);
1557            }
1558        }
1559
1560        // we do not piggyback sync-files to other self-sent-messages
1561        // to not risk files becoming too larger and being skipped by download-on-demand.
1562        if command == SystemMessage::MultiDeviceSync && self.is_e2ee_guaranteed() {
1563            let json = msg.param.get(Param::Arg).unwrap_or_default();
1564            let ids = msg.param.get(Param::Arg2).unwrap_or_default();
1565            parts.push(context.build_sync_part(json.to_string()));
1566            self.sync_ids_to_delete = Some(ids.to_string());
1567        } else if command == SystemMessage::WebxdcStatusUpdate {
1568            let json = msg.param.get(Param::Arg).unwrap_or_default();
1569            parts.push(context.build_status_update_part(json));
1570        } else if msg.viewtype == Viewtype::Webxdc {
1571            headers.push((
1572                "Iroh-Gossip-Topic",
1573                mail_builder::headers::raw::Raw::new(create_iroh_header(context, msg.id).await?)
1574                    .into(),
1575            ));
1576            if let (Some(json), _) = context
1577                .render_webxdc_status_update_object(
1578                    msg.id,
1579                    StatusUpdateSerial::MIN,
1580                    StatusUpdateSerial::MAX,
1581                    None,
1582                )
1583                .await?
1584            {
1585                parts.push(context.build_status_update_part(&json));
1586            }
1587        }
1588
1589        if self.attach_selfavatar {
1590            match context.get_config(Config::Selfavatar).await? {
1591                Some(path) => match build_avatar_file(context, &path).await {
1592                    Ok(avatar) => headers.push((
1593                        "Chat-User-Avatar",
1594                        mail_builder::headers::raw::Raw::new(format!("base64:{avatar}")).into(),
1595                    )),
1596                    Err(err) => warn!(context, "mimefactory: cannot attach selfavatar: {}", err),
1597                },
1598                None => headers.push((
1599                    "Chat-User-Avatar",
1600                    mail_builder::headers::raw::Raw::new("0").into(),
1601                )),
1602            }
1603        }
1604
1605        Ok((main_part, parts))
1606    }
1607
1608    /// Render an MDN
1609    fn render_mdn(&mut self) -> Result<MimePart<'static>> {
1610        // RFC 6522, this also requires the `report-type` parameter which is equal
1611        // to the MIME subtype of the second body part of the multipart/report
1612        //
1613        // currently, we do not send MDNs encrypted:
1614        // - in a multi-device-setup that is not set up properly, MDNs would disturb the communication as they
1615        //   are send automatically which may lead to spreading outdated Autocrypt headers.
1616        // - they do not carry any information but the Message-ID
1617        // - this save some KB
1618        // - in older versions, we did not encrypt messages to ourself when they to to SMTP - however, if these messages
1619        //   are forwarded for any reasons (eg. gmail always forwards to IMAP), we have no chance to decrypt them;
1620        //   this issue is fixed with 0.9.4
1621
1622        let Loaded::Mdn {
1623            rfc724_mid,
1624            additional_msg_ids,
1625        } = &self.loaded
1626        else {
1627            bail!("Attempt to render a message as MDN");
1628        };
1629
1630        // first body part: always human-readable, always REQUIRED by RFC 6522.
1631        // untranslated to no reveal sender's language.
1632        // moreover, translations in unknown languages are confusing, and clients may not display them at all
1633        let text_part = MimePart::new("text/plain", "This is a receipt notification.");
1634
1635        let mut message = MimePart::new(
1636            "multipart/report; report-type=disposition-notification",
1637            vec![text_part],
1638        );
1639
1640        // second body part: machine-readable, always REQUIRED by RFC 6522
1641        let message_text2 = format!(
1642            "Original-Recipient: rfc822;{}\r\n\
1643             Final-Recipient: rfc822;{}\r\n\
1644             Original-Message-ID: <{}>\r\n\
1645             Disposition: manual-action/MDN-sent-automatically; displayed\r\n",
1646            self.from_addr, self.from_addr, rfc724_mid
1647        );
1648
1649        let extension_fields = if additional_msg_ids.is_empty() {
1650            "".to_string()
1651        } else {
1652            "Additional-Message-IDs: ".to_string()
1653                + &additional_msg_ids
1654                    .iter()
1655                    .map(|mid| render_rfc724_mid(mid))
1656                    .collect::<Vec<String>>()
1657                    .join(" ")
1658                + "\r\n"
1659        };
1660
1661        message.add_part(MimePart::new(
1662            "message/disposition-notification",
1663            message_text2 + &extension_fields,
1664        ));
1665
1666        Ok(message)
1667    }
1668}
1669
1670fn hidden_recipients() -> Address<'static> {
1671    Address::new_group(Some("hidden-recipients".to_string()), Vec::new())
1672}
1673
1674async fn build_body_file(context: &Context, msg: &Message) -> Result<MimePart<'static>> {
1675    let file_name = msg.get_filename().context("msg has no file")?;
1676    let suffix = Path::new(&file_name)
1677        .extension()
1678        .and_then(|e| e.to_str())
1679        .unwrap_or("dat");
1680
1681    let blob = msg
1682        .param
1683        .get_file_blob(context)?
1684        .context("msg has no file")?;
1685
1686    // Get file name to use for sending.  For privacy purposes, we do
1687    // not transfer the original filenames eg. for images; these names
1688    // are normally not needed and contain timestamps, running numbers
1689    // etc.
1690    let filename_to_send: String = match msg.viewtype {
1691        Viewtype::Voice => format!(
1692            "voice-messsage_{}.{}",
1693            chrono::Utc
1694                .timestamp_opt(msg.timestamp_sort, 0)
1695                .single()
1696                .map_or_else(
1697                    || "YY-mm-dd_hh:mm:ss".to_string(),
1698                    |ts| ts.format("%Y-%m-%d_%H-%M-%S").to_string()
1699                ),
1700            &suffix
1701        ),
1702        Viewtype::Image | Viewtype::Gif => format!(
1703            "image_{}.{}",
1704            chrono::Utc
1705                .timestamp_opt(msg.timestamp_sort, 0)
1706                .single()
1707                .map_or_else(
1708                    || "YY-mm-dd_hh:mm:ss".to_string(),
1709                    |ts| ts.format("%Y-%m-%d_%H-%M-%S").to_string(),
1710                ),
1711            &suffix,
1712        ),
1713        Viewtype::Video => format!(
1714            "video_{}.{}",
1715            chrono::Utc
1716                .timestamp_opt(msg.timestamp_sort, 0)
1717                .single()
1718                .map_or_else(
1719                    || "YY-mm-dd_hh:mm:ss".to_string(),
1720                    |ts| ts.format("%Y-%m-%d_%H-%M-%S").to_string()
1721                ),
1722            &suffix
1723        ),
1724        _ => file_name,
1725    };
1726
1727    /* check mimetype */
1728    let mimetype = match msg.param.get(Param::MimeType) {
1729        Some(mtype) => mtype.to_string(),
1730        None => {
1731            if let Some((_viewtype, res)) = message::guess_msgtype_from_suffix(msg) {
1732                res.to_string()
1733            } else {
1734                "application/octet-stream".to_string()
1735            }
1736        }
1737    };
1738
1739    let body = fs::read(blob.to_abs_path()).await?;
1740
1741    // create mime part, for Content-Disposition, see RFC 2183.
1742    // `Content-Disposition: attachment` seems not to make a difference to `Content-Disposition: inline`
1743    // at least on tested Thunderbird and Gma'l in 2017.
1744    // But I've heard about problems with inline and outl'k, so we just use the attachment-type until we
1745    // run into other problems ...
1746    let mail =
1747        MimePart::new(mimetype, body).attachment(sanitize_bidi_characters(&filename_to_send));
1748
1749    Ok(mail)
1750}
1751
1752async fn build_avatar_file(context: &Context, path: &str) -> Result<String> {
1753    let blob = match path.starts_with("$BLOBDIR/") {
1754        true => BlobObject::from_name(context, path)?,
1755        false => BlobObject::from_path(context, path.as_ref())?,
1756    };
1757    let body = fs::read(blob.to_abs_path()).await?;
1758    let encoded_body = base64::engine::general_purpose::STANDARD
1759        .encode(&body)
1760        .chars()
1761        .enumerate()
1762        .fold(String::new(), |mut res, (i, c)| {
1763            if i % 78 == 77 {
1764                res.push(' ')
1765            }
1766            res.push(c);
1767            res
1768        });
1769    Ok(encoded_body)
1770}
1771
1772fn recipients_contain_addr(recipients: &[(String, String)], addr: &str) -> bool {
1773    let addr_lc = addr.to_lowercase();
1774    recipients
1775        .iter()
1776        .any(|(_, cur)| cur.to_lowercase() == addr_lc)
1777}
1778
1779fn render_rfc724_mid(rfc724_mid: &str) -> String {
1780    let rfc724_mid = rfc724_mid.trim().to_string();
1781
1782    if rfc724_mid.chars().next().unwrap_or_default() == '<' {
1783        rfc724_mid
1784    } else {
1785        format!("<{rfc724_mid}>")
1786    }
1787}
1788
1789#[cfg(test)]
1790mod mimefactory_tests;