deltachat/
qr.rs

1//! # QR code module.
2
3mod dclogin_scheme;
4use std::collections::BTreeMap;
5use std::sync::LazyLock;
6
7use anyhow::{Context as _, Result, anyhow, bail, ensure};
8pub use dclogin_scheme::LoginOptions;
9pub(crate) use dclogin_scheme::login_param_from_login_qr;
10use deltachat_contact_tools::{ContactAddress, addr_normalize, may_be_valid_addr};
11use percent_encoding::{NON_ALPHANUMERIC, percent_decode_str, percent_encode};
12use rand::TryRngCore as _;
13use rand::distr::{Alphanumeric, SampleString};
14use serde::Deserialize;
15
16use crate::config::Config;
17use crate::contact::{Contact, ContactId, Origin};
18use crate::context::Context;
19use crate::key::Fingerprint;
20use crate::login_param::{EnteredCertificateChecks, EnteredLoginParam, EnteredServerLoginParam};
21use crate::net::http::post_empty;
22use crate::net::proxy::{DEFAULT_SOCKS_PORT, ProxyConfig};
23use crate::token;
24use crate::tools::{time, validate_id};
25
26const OPENPGP4FPR_SCHEME: &str = "OPENPGP4FPR:"; // yes: uppercase
27const IDELTACHAT_SCHEME: &str = "https://i.delta.chat/#";
28const IDELTACHAT_NOSLASH_SCHEME: &str = "https://i.delta.chat#";
29const DCACCOUNT_SCHEME: &str = "DCACCOUNT:";
30pub(super) const DCLOGIN_SCHEME: &str = "DCLOGIN:";
31const TG_SOCKS_SCHEME: &str = "https://t.me/socks";
32const MAILTO_SCHEME: &str = "mailto:";
33const MATMSG_SCHEME: &str = "MATMSG:";
34const VCARD_SCHEME: &str = "BEGIN:VCARD";
35const SMTP_SCHEME: &str = "SMTP:";
36const HTTPS_SCHEME: &str = "https://";
37const SHADOWSOCKS_SCHEME: &str = "ss://";
38
39/// Backup transfer based on iroh-net.
40pub(crate) const DCBACKUP_SCHEME_PREFIX: &str = "DCBACKUP";
41
42/// Version written to Backups and Backup-QR-Codes.
43/// Imports will fail when they have a larger version.
44pub(crate) const DCBACKUP_VERSION: i32 = 4;
45
46/// Scanned QR code.
47#[derive(Debug, Clone, PartialEq, Eq)]
48pub enum Qr {
49    /// Ask the user whether to verify the contact.
50    ///
51    /// If the user agrees, pass this QR code to [`crate::securejoin::join_securejoin`].
52    AskVerifyContact {
53        /// ID of the contact.
54        contact_id: ContactId,
55
56        /// Fingerprint of the contact key as scanned from the QR code.
57        fingerprint: Fingerprint,
58
59        /// Invite number.
60        invitenumber: String,
61
62        /// Authentication code.
63        authcode: String,
64
65        /// Whether the inviter supports the new Securejoin v3 protocol
66        is_v3: bool,
67    },
68
69    /// Ask the user whether to join the group.
70    AskVerifyGroup {
71        /// Group name.
72        grpname: String,
73
74        /// Group ID.
75        grpid: String,
76
77        /// ID of the contact.
78        contact_id: ContactId,
79
80        /// Fingerprint of the contact key as scanned from the QR code.
81        fingerprint: Fingerprint,
82
83        /// Invite number.
84        invitenumber: String,
85
86        /// Authentication code.
87        authcode: String,
88
89        /// Whether the inviter supports the new Securejoin v3 protocol
90        is_v3: bool,
91    },
92
93    /// Ask whether to join the broadcast channel.
94    AskJoinBroadcast {
95        /// The user-visible name of this broadcast channel
96        name: String,
97
98        /// A string of random characters,
99        /// uniquely identifying this broadcast channel across all databases/clients.
100        /// Called `grpid` for historic reasons:
101        /// The id of multi-user chats is always called `grpid` in the database
102        /// because groups were once the only multi-user chats.
103        grpid: String,
104
105        /// ID of the contact who owns the channel and created the QR code.
106        contact_id: ContactId,
107
108        /// Fingerprint of the contact's key as scanned from the QR code.
109        fingerprint: Fingerprint,
110
111        /// Invite number.
112        invitenumber: String,
113        /// Authentication code.
114        authcode: String,
115
116        /// Whether the inviter supports the new Securejoin v3 protocol
117        is_v3: bool,
118    },
119
120    /// Contact fingerprint is verified.
121    ///
122    /// Ask the user if they want to start chatting.
123    FprOk {
124        /// Contact ID.
125        contact_id: ContactId,
126    },
127
128    /// Scanned fingerprint does not match the last seen fingerprint.
129    FprMismatch {
130        /// Contact ID.
131        contact_id: Option<ContactId>,
132    },
133
134    /// The scanned QR code contains a fingerprint but no e-mail address.
135    FprWithoutAddr {
136        /// Key fingerprint.
137        fingerprint: String,
138    },
139
140    /// Ask the user if they want to create an account on the given domain.
141    Account {
142        /// Server domain name.
143        domain: String,
144    },
145
146    /// Provides a backup that can be retrieved using iroh-net based backup transfer protocol.
147    Backup2 {
148        /// Iroh node address.
149        node_addr: iroh::NodeAddr,
150
151        /// Authentication token.
152        auth_token: String,
153    },
154
155    /// The QR code is a backup, but it is too new. The user has to update its Delta Chat.
156    BackupTooNew {},
157
158    /// Ask the user if they want to use the given proxy.
159    ///
160    /// Note that HTTP(S) URLs without a path
161    /// and query parameters are treated as HTTP(S) proxy URL.
162    /// UI may want to still offer to open the URL
163    /// in the browser if QR code contents
164    /// starts with `http://` or `https://`
165    /// and the QR code was not scanned from
166    /// the proxy configuration screen.
167    Proxy {
168        /// Proxy URL.
169        ///
170        /// This is the URL that is going to be added.
171        url: String,
172
173        /// Host extracted from the URL to display in the UI.
174        host: String,
175
176        /// Port extracted from the URL to display in the UI.
177        port: u16,
178    },
179
180    /// Contact address is scanned.
181    ///
182    /// Optionally, a draft message could be provided.
183    /// Ask the user if they want to start chatting.
184    Addr {
185        /// Contact ID.
186        contact_id: ContactId,
187
188        /// Draft message.
189        draft: Option<String>,
190    },
191
192    /// URL scanned.
193    ///
194    /// Ask the user if they want to open a browser or copy the URL to clipboard.
195    Url {
196        /// URL.
197        url: String,
198    },
199
200    /// Text scanned.
201    ///
202    /// Ask the user if they want to copy the text to clipboard.
203    Text {
204        /// Scanned text.
205        text: String,
206    },
207
208    /// Ask the user if they want to withdraw their own QR code.
209    WithdrawVerifyContact {
210        /// Contact ID.
211        contact_id: ContactId,
212
213        /// Fingerprint of the contact key as scanned from the QR code.
214        fingerprint: Fingerprint,
215
216        /// Invite number.
217        invitenumber: String,
218
219        /// Authentication code.
220        authcode: String,
221    },
222
223    /// Ask the user if they want to withdraw their own group invite QR code.
224    WithdrawVerifyGroup {
225        /// Group name.
226        grpname: String,
227
228        /// Group ID.
229        grpid: String,
230
231        /// Contact ID.
232        contact_id: ContactId,
233
234        /// Fingerprint of the contact key as scanned from the QR code.
235        fingerprint: Fingerprint,
236
237        /// Invite number.
238        invitenumber: String,
239
240        /// Authentication code.
241        authcode: String,
242    },
243
244    /// Ask the user if they want to withdraw their own broadcast channel invite QR code.
245    WithdrawJoinBroadcast {
246        /// The user-visible name of this broadcast channel
247        name: String,
248
249        /// A string of random characters,
250        /// uniquely identifying this broadcast channel across all databases/clients.
251        /// Called `grpid` for historic reasons:
252        /// The id of multi-user chats is always called `grpid` in the database
253        /// because groups were once the only multi-user chats.
254        grpid: String,
255
256        /// Contact ID. Always `ContactId::SELF`.
257        contact_id: ContactId,
258
259        /// Fingerprint of the contact's key as scanned from the QR code.
260        fingerprint: Fingerprint,
261
262        /// Invite number.
263        invitenumber: String,
264
265        /// Authentication code.
266        authcode: String,
267    },
268
269    /// Ask the user if they want to revive their own QR code.
270    ReviveVerifyContact {
271        /// Contact ID.
272        contact_id: ContactId,
273
274        /// Fingerprint of the contact key as scanned from the QR code.
275        fingerprint: Fingerprint,
276
277        /// Invite number.
278        invitenumber: String,
279
280        /// Authentication code.
281        authcode: String,
282    },
283
284    /// Ask the user if they want to revive their own group invite QR code.
285    ReviveVerifyGroup {
286        /// Group name.
287        grpname: String,
288
289        /// Group ID.
290        grpid: String,
291
292        /// Contact ID.
293        contact_id: ContactId,
294
295        /// Fingerprint of the contact key as scanned from the QR code.
296        fingerprint: Fingerprint,
297
298        /// Invite number.
299        invitenumber: String,
300
301        /// Authentication code.
302        authcode: String,
303    },
304
305    /// Ask the user if they want to revive their own broadcast channel invite QR code.
306    ReviveJoinBroadcast {
307        /// The user-visible name of this broadcast channel
308        name: String,
309
310        /// A string of random characters,
311        /// uniquely identifying this broadcast channel across all databases/clients.
312        /// Called `grpid` for historic reasons:
313        /// The id of multi-user chats is always called `grpid` in the database
314        /// because groups were once the only multi-user chats.
315        grpid: String,
316
317        /// Contact ID. Always `ContactId::SELF`.
318        contact_id: ContactId,
319
320        /// Fingerprint of the contact's key as scanned from the QR code.
321        fingerprint: Fingerprint,
322
323        /// Invite number.
324        invitenumber: String,
325
326        /// Authentication code.
327        authcode: String,
328    },
329
330    /// `dclogin:` scheme parameters.
331    ///
332    /// Ask the user if they want to login with the email address.
333    Login {
334        /// Email address.
335        address: String,
336
337        /// Login parameters.
338        options: LoginOptions,
339    },
340}
341
342// hack around the changed JSON accidentally used by an iroh upgrade, see #6518 for more details and for code snippet.
343// this hack is mainly needed to give ppl time to upgrade and can be removed after some months (added 2025-02)
344fn fix_add_second_device_qr(qr: &str) -> String {
345    qr.replacen(r#","info":{"relay_url":"#, r#","relay_url":"#, 1)
346        .replacen(r#""]}}"#, r#""]}"#, 1)
347}
348
349fn starts_with_ignore_case(string: &str, pattern: &str) -> bool {
350    string.to_lowercase().starts_with(&pattern.to_lowercase())
351}
352
353/// Checks a scanned QR code.
354///
355/// The function should be called after a QR code is scanned.
356/// The function takes the raw text scanned and checks what can be done with it.
357pub async fn check_qr(context: &Context, qr: &str) -> Result<Qr> {
358    let qr = qr.trim();
359    let qrcode = if starts_with_ignore_case(qr, OPENPGP4FPR_SCHEME) {
360        decode_openpgp(context, qr)
361            .await
362            .context("failed to decode OPENPGP4FPR QR code")?
363    } else if qr.starts_with(IDELTACHAT_SCHEME) {
364        decode_ideltachat(context, IDELTACHAT_SCHEME, qr).await?
365    } else if qr.starts_with(IDELTACHAT_NOSLASH_SCHEME) {
366        decode_ideltachat(context, IDELTACHAT_NOSLASH_SCHEME, qr).await?
367    } else if starts_with_ignore_case(qr, DCACCOUNT_SCHEME) {
368        decode_account(qr)?
369    } else if starts_with_ignore_case(qr, DCLOGIN_SCHEME) {
370        dclogin_scheme::decode_login(qr)?
371    } else if starts_with_ignore_case(qr, TG_SOCKS_SCHEME) {
372        decode_tg_socks_proxy(context, qr)?
373    } else if qr.starts_with(SHADOWSOCKS_SCHEME) {
374        decode_shadowsocks_proxy(qr)?
375    } else if starts_with_ignore_case(qr, DCBACKUP_SCHEME_PREFIX) {
376        let qr_fixed = fix_add_second_device_qr(qr);
377        decode_backup2(&qr_fixed)?
378    } else if qr.starts_with(MAILTO_SCHEME) {
379        decode_mailto(context, qr).await?
380    } else if qr.starts_with(SMTP_SCHEME) {
381        decode_smtp(context, qr).await?
382    } else if qr.starts_with(MATMSG_SCHEME) {
383        decode_matmsg(context, qr).await?
384    } else if qr.starts_with(VCARD_SCHEME) {
385        decode_vcard(context, qr).await?
386    } else if let Ok(url) = url::Url::parse(qr) {
387        match url.scheme() {
388            "socks5" => Qr::Proxy {
389                url: qr.to_string(),
390                host: url.host_str().context("URL has no host")?.to_string(),
391                port: url.port().unwrap_or(DEFAULT_SOCKS_PORT),
392            },
393            "http" | "https" => {
394                // Parsing with a non-standard scheme
395                // is a hack to work around the `url` crate bug
396                // <https://github.com/servo/rust-url/issues/957>.
397                let url = if let Some(rest) = qr.strip_prefix("http://") {
398                    url::Url::parse(&format!("foobarbaz://{rest}"))?
399                } else if let Some(rest) = qr.strip_prefix("https://") {
400                    url::Url::parse(&format!("foobarbaz://{rest}"))?
401                } else {
402                    // Should not happen.
403                    url
404                };
405
406                if url.port().is_none() | (url.path() != "") | url.query().is_some() {
407                    // URL without a port, with a path or query cannot be a proxy URL.
408                    Qr::Url {
409                        url: qr.to_string(),
410                    }
411                } else {
412                    Qr::Proxy {
413                        url: qr.to_string(),
414                        host: url.host_str().context("URL has no host")?.to_string(),
415                        port: url
416                            .port_or_known_default()
417                            .context("HTTP(S) URLs are guaranteed to return Some port")?,
418                    }
419                }
420            }
421            _ => Qr::Url {
422                url: qr.to_string(),
423            },
424        }
425    } else {
426        Qr::Text {
427            text: qr.to_string(),
428        }
429    };
430    Ok(qrcode)
431}
432
433/// Formats the text of the [`Qr::Backup2`] variant.
434///
435/// This is the inverse of [`check_qr`] for that variant only.
436///
437/// TODO: Refactor this so all variants have a correct [`Display`] and transform `check_qr`
438/// into `FromStr`.
439pub fn format_backup(qr: &Qr) -> Result<String> {
440    match qr {
441        Qr::Backup2 {
442            node_addr,
443            auth_token,
444        } => {
445            let node_addr = serde_json::to_string(node_addr)?;
446            Ok(format!(
447                "{DCBACKUP_SCHEME_PREFIX}{DCBACKUP_VERSION}:{auth_token}&{node_addr}"
448            ))
449        }
450        _ => Err(anyhow!("Not a backup QR code")),
451    }
452}
453
454/// scheme: `OPENPGP4FPR:FINGERPRINT#a=ADDR&n=NAME&i=INVITENUMBER&s=AUTH`
455///     or: `OPENPGP4FPR:FINGERPRINT#a=ADDR&g=GROUPNAME&x=GROUPID&i=INVITENUMBER&s=AUTH`
456///     or: `OPENPGP4FPR:FINGERPRINT#a=ADDR&b=BROADCAST_NAME&x=BROADCAST_ID&j=INVITENUMBER&s=AUTH`
457///     or: `OPENPGP4FPR:FINGERPRINT#a=ADDR`
458async fn decode_openpgp(context: &Context, qr: &str) -> Result<Qr> {
459    let payload = qr
460        .get(OPENPGP4FPR_SCHEME.len()..)
461        .context("Invalid OPENPGP4FPR scheme")?;
462
463    // macOS and iOS sometimes replace the # with %23 (uri encode it), we should be able to parse this wrong format too.
464    // see issue https://github.com/deltachat/deltachat-core-rust/issues/1969 for more info
465    let (fingerprint, fragment) = match payload
466        .split_once('#')
467        .or_else(|| payload.split_once("%23"))
468    {
469        Some(pair) => pair,
470        None => (payload, ""),
471    };
472    let fingerprint: Fingerprint = fingerprint
473        .parse()
474        .context("Failed to parse fingerprint in the QR code")?;
475
476    let param: BTreeMap<&str, &str> = fragment
477        .split('&')
478        .filter_map(|s| {
479            if let [key, value] = s.splitn(2, '=').collect::<Vec<_>>()[..] {
480                Some((key, value))
481            } else {
482                None
483            }
484        })
485        .collect();
486
487    let addr = if let Some(addr) = param.get("a") {
488        Some(normalize_address(addr)?)
489    } else {
490        None
491    };
492
493    let name = decode_name(&param, "n")?.unwrap_or_default();
494
495    let mut invitenumber = param
496        .get("i")
497        // For historic reansons, broadcasts currently use j instead of i for the invitenumber:
498        .or_else(|| param.get("j"))
499        .filter(|&s| validate_id(s))
500        .map(|s| s.to_string());
501    let authcode = param
502        .get("s")
503        .filter(|&s| validate_id(s))
504        .map(|s| s.to_string());
505    let grpid = param
506        .get("x")
507        .filter(|&s| validate_id(s))
508        .map(|s| s.to_string());
509
510    let grpname = decode_name(&param, "g")?;
511    let broadcast_name = decode_name(&param, "b")?;
512
513    let mut is_v3 = param.get("v") == Some(&"3");
514
515    if authcode.is_some() && invitenumber.is_none() {
516        // Securejoin v3 doesn't need an invitenumber.
517        // We want to remove the invitenumber and the `v=3` parameter eventually;
518        // therefore, we accept v3 QR codes without an invitenumber.
519        is_v3 = true;
520        invitenumber = Some("".to_string());
521    }
522
523    if let (Some(addr), Some(invitenumber), Some(authcode)) = (&addr, invitenumber, authcode) {
524        let addr = ContactAddress::new(addr)?;
525        let (contact_id, _) = Contact::add_or_lookup_ex(
526            context,
527            &name,
528            &addr,
529            &fingerprint.hex(),
530            Origin::UnhandledSecurejoinQrScan,
531        )
532        .await
533        .with_context(|| format!("failed to add or lookup contact for address {addr:?}"))?;
534
535        if let (Some(grpid), Some(grpname)) = (grpid.clone(), grpname) {
536            if context
537                .is_self_addr(&addr)
538                .await
539                .with_context(|| format!("can't check if address {addr:?} is our address"))?
540            {
541                if token::exists(context, token::Namespace::Auth, &authcode).await? {
542                    Ok(Qr::WithdrawVerifyGroup {
543                        grpname,
544                        grpid,
545                        contact_id,
546                        fingerprint,
547                        invitenumber,
548                        authcode,
549                    })
550                } else {
551                    Ok(Qr::ReviveVerifyGroup {
552                        grpname,
553                        grpid,
554                        contact_id,
555                        fingerprint,
556                        invitenumber,
557                        authcode,
558                    })
559                }
560            } else {
561                Ok(Qr::AskVerifyGroup {
562                    grpname,
563                    grpid,
564                    contact_id,
565                    fingerprint,
566                    invitenumber,
567                    authcode,
568                    is_v3,
569                })
570            }
571        } else if let (Some(grpid), Some(name)) = (grpid, broadcast_name) {
572            if context
573                .is_self_addr(&addr)
574                .await
575                .with_context(|| format!("Can't check if {addr:?} is our address"))?
576            {
577                if token::exists(context, token::Namespace::Auth, &authcode).await? {
578                    Ok(Qr::WithdrawJoinBroadcast {
579                        name,
580                        grpid,
581                        contact_id,
582                        fingerprint,
583                        invitenumber,
584                        authcode,
585                    })
586                } else {
587                    Ok(Qr::ReviveJoinBroadcast {
588                        name,
589                        grpid,
590                        contact_id,
591                        fingerprint,
592                        invitenumber,
593                        authcode,
594                    })
595                }
596            } else {
597                Ok(Qr::AskJoinBroadcast {
598                    name,
599                    grpid,
600                    contact_id,
601                    fingerprint,
602                    invitenumber,
603                    authcode,
604                    is_v3,
605                })
606            }
607        } else if context.is_self_addr(&addr).await? {
608            if token::exists(context, token::Namespace::Auth, &authcode).await? {
609                Ok(Qr::WithdrawVerifyContact {
610                    contact_id,
611                    fingerprint,
612                    invitenumber,
613                    authcode,
614                })
615            } else {
616                Ok(Qr::ReviveVerifyContact {
617                    contact_id,
618                    fingerprint,
619                    invitenumber,
620                    authcode,
621                })
622            }
623        } else {
624            Ok(Qr::AskVerifyContact {
625                contact_id,
626                fingerprint,
627                invitenumber,
628                authcode,
629                is_v3,
630            })
631        }
632    } else if let Some(addr) = addr {
633        let fingerprint = fingerprint.hex();
634        let (contact_id, _) =
635            Contact::add_or_lookup_ex(context, "", &addr, &fingerprint, Origin::UnhandledQrScan)
636                .await?;
637        let contact = Contact::get_by_id(context, contact_id).await?;
638
639        if contact.public_key(context).await?.is_some() {
640            Ok(Qr::FprOk { contact_id })
641        } else {
642            Ok(Qr::FprMismatch {
643                contact_id: Some(contact_id),
644            })
645        }
646    } else {
647        Ok(Qr::FprWithoutAddr {
648            fingerprint: fingerprint.to_string(),
649        })
650    }
651}
652
653fn decode_name(param: &BTreeMap<&str, &str>, key: &str) -> Result<Option<String>> {
654    if let Some(encoded_name) = param.get(key) {
655        let encoded_name = encoded_name.replace('+', "%20"); // sometimes spaces are encoded as `+`
656        let mut name = match percent_decode_str(&encoded_name).decode_utf8() {
657            Ok(name) => name.to_string(),
658            Err(err) => bail!("Invalid QR param {key}: {err}"),
659        };
660        if let Some(n) = name.strip_suffix('_') {
661            name = format!("{n}…");
662        }
663        Ok(Some(name))
664    } else {
665        Ok(None)
666    }
667}
668
669/// scheme: `https://i.delta.chat[/]#FINGERPRINT&a=ADDR[&OPTIONAL_PARAMS]`
670async fn decode_ideltachat(context: &Context, prefix: &str, qr: &str) -> Result<Qr> {
671    let qr = qr.replacen(prefix, OPENPGP4FPR_SCHEME, 1);
672    let qr = qr.replacen('&', "#", 1);
673    decode_openpgp(context, &qr)
674        .await
675        .with_context(|| format!("failed to decode {prefix} QR code"))
676}
677
678/// scheme: `DCACCOUNT:example.org`
679/// or `DCACCOUNT:https://example.org/new`
680/// or `DCACCOUNT:https://example.org/new_email?t=1w_7wDjgjelxeX884x96v3`
681fn decode_account(qr: &str) -> Result<Qr> {
682    let payload = qr
683        .get(DCACCOUNT_SCHEME.len()..)
684        .context("Invalid DCACCOUNT payload")?;
685    if payload.starts_with("https://") {
686        let url = url::Url::parse(payload).context("Invalid account URL")?;
687        if url.scheme() == "https" {
688            Ok(Qr::Account {
689                domain: url
690                    .host_str()
691                    .context("can't extract account setup domain")?
692                    .to_string(),
693            })
694        } else {
695            bail!("Bad scheme for account URL: {:?}.", url.scheme());
696        }
697    } else {
698        Ok(Qr::Account {
699            domain: payload.to_string(),
700        })
701    }
702}
703
704/// scheme: `https://t.me/socks?server=foo&port=123` or `https://t.me/socks?server=1.2.3.4&port=123`
705#[expect(clippy::arithmetic_side_effects)]
706fn decode_tg_socks_proxy(_context: &Context, qr: &str) -> Result<Qr> {
707    let url = url::Url::parse(qr).context("Invalid t.me/socks url")?;
708
709    let mut host: Option<String> = None;
710    let mut port: u16 = DEFAULT_SOCKS_PORT;
711    let mut user: Option<String> = None;
712    let mut pass: Option<String> = None;
713    for (key, value) in url.query_pairs() {
714        if key == "server" {
715            host = Some(value.to_string());
716        } else if key == "port" {
717            port = value.parse().unwrap_or(DEFAULT_SOCKS_PORT);
718        } else if key == "user" {
719            user = Some(value.to_string());
720        } else if key == "pass" {
721            pass = Some(value.to_string());
722        }
723    }
724
725    let Some(host) = host else {
726        bail!("Bad t.me/socks url: {url:?}");
727    };
728
729    let mut url = "socks5://".to_string();
730    if let Some(pass) = pass {
731        url += &percent_encode(user.unwrap_or_default().as_bytes(), NON_ALPHANUMERIC).to_string();
732        url += ":";
733        url += &percent_encode(pass.as_bytes(), NON_ALPHANUMERIC).to_string();
734        url += "@";
735    };
736    url += &host;
737    url += ":";
738    url += &port.to_string();
739
740    Ok(Qr::Proxy { url, host, port })
741}
742
743/// Decodes `ss://` URLs for Shadowsocks proxies.
744fn decode_shadowsocks_proxy(qr: &str) -> Result<Qr> {
745    let server_config = shadowsocks::config::ServerConfig::from_url(qr)?;
746    let addr = server_config.addr();
747    let host = addr.host().to_string();
748    let port = addr.port();
749    Ok(Qr::Proxy {
750        url: qr.to_string(),
751        host,
752        port,
753    })
754}
755
756/// Decodes a `DCBACKUP` QR code.
757fn decode_backup2(qr: &str) -> Result<Qr> {
758    let version_and_payload = qr
759        .strip_prefix(DCBACKUP_SCHEME_PREFIX)
760        .ok_or_else(|| anyhow!("Invalid DCBACKUP scheme"))?;
761    let (version, payload) = version_and_payload
762        .split_once(':')
763        .context("DCBACKUP scheme separator missing")?;
764    let version: i32 = version.parse().context("Not a valid number")?;
765    if version > DCBACKUP_VERSION {
766        return Ok(Qr::BackupTooNew {});
767    }
768
769    let (auth_token, node_addr) = payload
770        .split_once('&')
771        .context("Backup QR code has no separator")?;
772    let auth_token = auth_token.to_string();
773    let node_addr = serde_json::from_str::<iroh::NodeAddr>(node_addr)
774        .context("Invalid node addr in backup QR code")?;
775
776    Ok(Qr::Backup2 {
777        node_addr,
778        auth_token,
779    })
780}
781
782#[derive(Debug, Deserialize)]
783struct CreateAccountSuccessResponse {
784    /// Email address.
785    email: String,
786
787    /// Password.
788    password: String,
789}
790#[derive(Debug, Deserialize)]
791struct CreateAccountErrorResponse {
792    /// Reason for the failure to create account returned by the server.
793    reason: String,
794}
795
796/// Takes a QR with `DCACCOUNT:` scheme, parses its parameters,
797/// downloads additional information from the contained URL
798/// and returns the login parameters.
799pub(crate) async fn login_param_from_account_qr(
800    context: &Context,
801    qr: &str,
802) -> Result<EnteredLoginParam> {
803    let payload = qr
804        .get(DCACCOUNT_SCHEME.len()..)
805        .context("Invalid DCACCOUNT scheme")?;
806
807    if !payload.starts_with(HTTPS_SCHEME) {
808        let rng = &mut rand::rngs::OsRng.unwrap_err();
809        let username = Alphanumeric.sample_string(rng, 9);
810        let addr = username + "@" + payload;
811        let password = Alphanumeric.sample_string(rng, 50);
812
813        let param = EnteredLoginParam {
814            addr,
815            imap: EnteredServerLoginParam {
816                password,
817                ..Default::default()
818            },
819            smtp: Default::default(),
820            certificate_checks: EnteredCertificateChecks::Strict,
821            oauth2: false,
822        };
823        return Ok(param);
824    }
825
826    let (response_text, response_success) = post_empty(context, payload).await?;
827    if response_success {
828        let CreateAccountSuccessResponse { password, email } = serde_json::from_str(&response_text)
829            .with_context(|| {
830                format!("Cannot create account, response is malformed:\n{response_text:?}")
831            })?;
832
833        let param = EnteredLoginParam {
834            addr: email,
835            imap: EnteredServerLoginParam {
836                password,
837                ..Default::default()
838            },
839            smtp: Default::default(),
840            certificate_checks: EnteredCertificateChecks::Strict,
841            oauth2: false,
842        };
843
844        Ok(param)
845    } else {
846        match serde_json::from_str::<CreateAccountErrorResponse>(&response_text) {
847            Ok(error) => Err(anyhow!(error.reason)),
848            Err(parse_error) => {
849                error!(
850                    context,
851                    "Cannot create account, server response could not be parsed:\n{parse_error:#}\nraw response:\n{response_text}"
852                );
853                bail!("Cannot create account, unexpected server response:\n{response_text:?}")
854            }
855        }
856    }
857}
858
859/// Sets configuration values from a QR code.
860/// "DCACCOUNT:" and "DCLOGIN:" QR codes configure `context`, but I/O mustn't be started for such QR
861/// codes, consider using [`Context::add_transport_from_qr`] which also restarts I/O.
862pub async fn set_config_from_qr(context: &Context, qr: &str) -> Result<()> {
863    match check_qr(context, qr).await? {
864        Qr::Account { .. } => {
865            let mut param = login_param_from_account_qr(context, qr).await?;
866            context.add_transport_inner(&mut param).await?
867        }
868        Qr::Proxy { url, .. } => {
869            let old_proxy_url_value = context
870                .get_config(Config::ProxyUrl)
871                .await?
872                .unwrap_or_default();
873
874            // Normalize the URL.
875            let url = ProxyConfig::from_url(&url)?.to_url();
876
877            let proxy_urls: Vec<&str> = std::iter::once(url.as_str())
878                .chain(
879                    old_proxy_url_value
880                        .split('\n')
881                        .filter(|s| !s.is_empty() && *s != url),
882                )
883                .collect();
884            context
885                .set_config(Config::ProxyUrl, Some(&proxy_urls.join("\n")))
886                .await?;
887            context.set_config_bool(Config::ProxyEnabled, true).await?;
888        }
889        Qr::WithdrawVerifyContact {
890            invitenumber,
891            authcode,
892            ..
893        } => {
894            token::delete(context, "").await?;
895            context
896                .sync_qr_code_token_deletion(invitenumber, authcode)
897                .await?;
898        }
899        Qr::WithdrawVerifyGroup {
900            grpid,
901            invitenumber,
902            authcode,
903            ..
904        }
905        | Qr::WithdrawJoinBroadcast {
906            grpid,
907            invitenumber,
908            authcode,
909            ..
910        } => {
911            token::delete(context, &grpid).await?;
912            context
913                .sync_qr_code_token_deletion(invitenumber, authcode)
914                .await?;
915        }
916        Qr::ReviveVerifyContact {
917            invitenumber,
918            authcode,
919            ..
920        } => {
921            let timestamp = time();
922            token::save(
923                context,
924                token::Namespace::InviteNumber,
925                None,
926                &invitenumber,
927                timestamp,
928            )
929            .await?;
930            token::save(context, token::Namespace::Auth, None, &authcode, timestamp).await?;
931            context.sync_qr_code_tokens(None).await?;
932            context.scheduler.interrupt_smtp().await;
933        }
934        Qr::ReviveVerifyGroup {
935            invitenumber,
936            authcode,
937            grpid,
938            ..
939        }
940        | Qr::ReviveJoinBroadcast {
941            invitenumber,
942            authcode,
943            grpid,
944            ..
945        } => {
946            let timestamp = time();
947            token::save(
948                context,
949                token::Namespace::InviteNumber,
950                Some(&grpid),
951                &invitenumber,
952                timestamp,
953            )
954            .await?;
955            token::save(
956                context,
957                token::Namespace::Auth,
958                Some(&grpid),
959                &authcode,
960                timestamp,
961            )
962            .await?;
963            context.sync_qr_code_tokens(Some(&grpid)).await?;
964            context.scheduler.interrupt_smtp().await;
965        }
966        Qr::Login { address, options } => {
967            let mut param = login_param_from_login_qr(&address, options)?;
968            context.add_transport_inner(&mut param).await?
969        }
970        _ => bail!("QR code does not contain config"),
971    }
972
973    Ok(())
974}
975
976/// Extract address for the mailto scheme.
977///
978/// Scheme: `mailto:addr...?subject=...&body=..`
979async fn decode_mailto(context: &Context, qr: &str) -> Result<Qr> {
980    let payload = qr
981        .get(MAILTO_SCHEME.len()..)
982        .context("Invalid mailto: scheme")?;
983
984    let (addr, query) = payload.split_once('?').unwrap_or((payload, ""));
985
986    let param: BTreeMap<&str, &str> = query
987        .split('&')
988        .filter_map(|s| {
989            if let [key, value] = s.splitn(2, '=').collect::<Vec<_>>()[..] {
990                Some((key, value))
991            } else {
992                None
993            }
994        })
995        .collect();
996
997    let subject = if let Some(subject) = param.get("subject") {
998        subject.to_string()
999    } else {
1000        "".to_string()
1001    };
1002    let draft = if let Some(body) = param.get("body") {
1003        if subject.is_empty() {
1004            body.to_string()
1005        } else {
1006            subject + "\n" + body
1007        }
1008    } else {
1009        subject
1010    };
1011    let draft = draft.replace('+', "%20"); // sometimes spaces are encoded as `+`
1012    let draft = match percent_decode_str(&draft).decode_utf8() {
1013        Ok(decoded_draft) => decoded_draft.to_string(),
1014        Err(_err) => draft,
1015    };
1016
1017    let addr = normalize_address(addr)?;
1018    let name = "";
1019    Qr::from_address(
1020        context,
1021        name,
1022        &addr,
1023        if draft.is_empty() { None } else { Some(draft) },
1024    )
1025    .await
1026}
1027
1028/// Extract address for the smtp scheme.
1029///
1030/// Scheme: `SMTP:addr...:subject...:body...`
1031async fn decode_smtp(context: &Context, qr: &str) -> Result<Qr> {
1032    let payload = qr.get(SMTP_SCHEME.len()..).context("Invalid SMTP scheme")?;
1033
1034    let (addr, _rest) = payload
1035        .split_once(':')
1036        .context("Invalid SMTP scheme payload")?;
1037    let addr = normalize_address(addr)?;
1038    let name = "";
1039    Qr::from_address(context, name, &addr, None).await
1040}
1041
1042/// Extract address for the matmsg scheme.
1043///
1044/// Scheme: `MATMSG:TO:addr...;SUB:subject...;BODY:body...;`
1045///
1046/// There may or may not be linebreaks after the fields.
1047#[expect(clippy::arithmetic_side_effects)]
1048async fn decode_matmsg(context: &Context, qr: &str) -> Result<Qr> {
1049    // Does not work when the text `TO:` is used in subject/body _and_ TO: is not the first field.
1050    // we ignore this case.
1051    let addr = if let Some(to_index) = qr.find("TO:") {
1052        let addr = qr.get(to_index + 3..).unwrap_or_default().trim();
1053        if let Some(semi_index) = addr.find(';') {
1054            addr.get(..semi_index).unwrap_or_default().trim()
1055        } else {
1056            addr
1057        }
1058    } else {
1059        bail!("Invalid MATMSG found");
1060    };
1061
1062    let addr = normalize_address(addr)?;
1063    let name = "";
1064    Qr::from_address(context, name, &addr, None).await
1065}
1066
1067static VCARD_NAME_RE: LazyLock<regex::Regex> =
1068    LazyLock::new(|| regex::Regex::new(r"(?m)^N:([^;]*);([^;\n]*)").unwrap());
1069static VCARD_EMAIL_RE: LazyLock<regex::Regex> =
1070    LazyLock::new(|| regex::Regex::new(r"(?m)^EMAIL([^:\n]*):([^;\n]*)").unwrap());
1071
1072/// Extract address for the vcard scheme.
1073///
1074/// Scheme: `VCARD:BEGIN\nN:last name;first name;...;\nEMAIL;<type>:addr...;`
1075async fn decode_vcard(context: &Context, qr: &str) -> Result<Qr> {
1076    let name = VCARD_NAME_RE
1077        .captures(qr)
1078        .and_then(|caps| {
1079            let last_name = caps.get(1)?.as_str().trim();
1080            let first_name = caps.get(2)?.as_str().trim();
1081
1082            Some(format!("{first_name} {last_name}"))
1083        })
1084        .unwrap_or_default();
1085
1086    let addr = if let Some(cap) = VCARD_EMAIL_RE.captures(qr).and_then(|caps| caps.get(2)) {
1087        normalize_address(cap.as_str().trim())?
1088    } else {
1089        bail!("Bad e-mail address");
1090    };
1091
1092    Qr::from_address(context, &name, &addr, None).await
1093}
1094
1095impl Qr {
1096    /// Creates a new scanned QR code of a contact address.
1097    ///
1098    /// May contain a message draft.
1099    pub async fn from_address(
1100        context: &Context,
1101        name: &str,
1102        addr: &str,
1103        draft: Option<String>,
1104    ) -> Result<Self> {
1105        let addr = ContactAddress::new(addr)?;
1106        let (contact_id, _) =
1107            Contact::add_or_lookup(context, name, &addr, Origin::UnhandledQrScan).await?;
1108        Ok(Qr::Addr { contact_id, draft })
1109    }
1110}
1111
1112/// URL decodes a given address, does basic email validation on the result.
1113fn normalize_address(addr: &str) -> Result<String> {
1114    // urldecoding is needed at least for OPENPGP4FPR but should not hurt in the other cases
1115    let new_addr = percent_decode_str(addr).decode_utf8()?;
1116    let new_addr = addr_normalize(&new_addr);
1117
1118    ensure!(may_be_valid_addr(&new_addr), "Bad e-mail address");
1119
1120    Ok(new_addr.to_string())
1121}
1122
1123#[cfg(test)]
1124mod qr_tests;