deltachat/
location.rs

1//! Location handling.
2//!
3//! Delta Chat handles two kind of locations.
4//!
5//! There are two kinds of locations:
6//! - Independent locations, also known as Points of Interest (POI).
7//! - Path locations.
8//!
9//! Locations are sent as KML attachments.
10//! Independent locations are sent in `message.kml` attachments
11//! and path locations are sent in `location.kml` attachments.
12
13use std::time::Duration;
14
15use anyhow::{Context as _, Result, ensure};
16use async_channel::Receiver;
17use quick_xml::events::{BytesEnd, BytesStart, BytesText};
18use tokio::time::timeout;
19
20use crate::chat::{self, ChatId};
21use crate::constants::DC_CHAT_ID_TRASH;
22use crate::contact::ContactId;
23use crate::context::Context;
24use crate::events::EventType;
25use crate::log::warn;
26use crate::message::{Message, MsgId, Viewtype};
27use crate::mimeparser::SystemMessage;
28use crate::tools::{duration_to_str, time};
29use crate::{chatlist_events, stock_str};
30
31/// Location record.
32#[derive(Debug, Clone, Default)]
33pub struct Location {
34    /// Row ID of the location.
35    pub location_id: u32,
36
37    /// Location latitude.
38    pub latitude: f64,
39
40    /// Location longitude.
41    pub longitude: f64,
42
43    /// Nonstandard `accuracy` attribute of the `coordinates` tag.
44    pub accuracy: f64,
45
46    /// Location timestamp in seconds.
47    pub timestamp: i64,
48
49    /// Contact ID.
50    pub contact_id: ContactId,
51
52    /// Message ID.
53    pub msg_id: u32,
54
55    /// Chat ID.
56    pub chat_id: ChatId,
57
58    /// A marker string, such as an emoji, to be displayed on top of the location.
59    pub marker: Option<String>,
60
61    /// Whether location is independent, i.e. not part of the path.
62    pub independent: u32,
63}
64
65impl Location {
66    /// Creates a new empty location.
67    pub fn new() -> Self {
68        Default::default()
69    }
70}
71
72/// KML document.
73///
74/// See <https://www.ogc.org/standards/kml/> for the standard and
75/// <https://developers.google.com/kml> for documentation.
76#[derive(Debug, Clone, Default)]
77pub struct Kml {
78    /// Nonstandard `addr` attribute of the `Document` tag storing the user email address.
79    pub addr: Option<String>,
80
81    /// Placemarks.
82    pub locations: Vec<Location>,
83
84    /// Currently parsed XML tag.
85    tag: KmlTag,
86
87    /// Currently parsed placemark.
88    pub curr: Location,
89}
90
91#[derive(Default, Debug, Clone, PartialEq, Eq)]
92enum KmlTag {
93    #[default]
94    Undefined,
95    Placemark,
96    PlacemarkTimestamp,
97    PlacemarkTimestampWhen,
98    PlacemarkPoint,
99    PlacemarkPointCoordinates,
100}
101
102impl Kml {
103    /// Creates a new empty KML document.
104    pub fn new() -> Self {
105        Default::default()
106    }
107
108    /// Parses a KML document.
109    pub fn parse(to_parse: &[u8]) -> Result<Self> {
110        ensure!(to_parse.len() <= 1024 * 1024, "kml-file is too large");
111
112        let mut reader = quick_xml::Reader::from_reader(to_parse);
113        reader.config_mut().trim_text(true);
114
115        let mut kml = Kml::new();
116        kml.locations = Vec::with_capacity(100);
117
118        let mut buf = Vec::new();
119
120        loop {
121            match reader.read_event_into(&mut buf).with_context(|| {
122                format!(
123                    "location parsing error at position {}",
124                    reader.buffer_position()
125                )
126            })? {
127                quick_xml::events::Event::Start(ref e) => kml.starttag_cb(e, &reader),
128                quick_xml::events::Event::End(ref e) => kml.endtag_cb(e),
129                quick_xml::events::Event::Text(ref e) => kml.text_cb(e),
130                quick_xml::events::Event::Eof => break,
131                _ => (),
132            }
133            buf.clear();
134        }
135
136        Ok(kml)
137    }
138
139    fn text_cb(&mut self, event: &BytesText) {
140        if self.tag == KmlTag::PlacemarkTimestampWhen
141            || self.tag == KmlTag::PlacemarkPointCoordinates
142        {
143            let val = event.xml_content().unwrap_or_default();
144
145            let val = val.replace(['\n', '\r', '\t', ' '], "");
146
147            if self.tag == KmlTag::PlacemarkTimestampWhen && val.len() >= 19 {
148                // YYYY-MM-DDTHH:MM:SSZ
149                // 0   4  7  10 13 16 19
150                match chrono::NaiveDateTime::parse_from_str(&val, "%Y-%m-%dT%H:%M:%SZ") {
151                    Ok(res) => {
152                        self.curr.timestamp = res.and_utc().timestamp();
153                        let now = time();
154                        if self.curr.timestamp > now {
155                            self.curr.timestamp = now;
156                        }
157                    }
158                    Err(_err) => {
159                        self.curr.timestamp = time();
160                    }
161                }
162            } else if self.tag == KmlTag::PlacemarkPointCoordinates {
163                let parts = val.splitn(2, ',').collect::<Vec<_>>();
164                if let [longitude, latitude] = &parts[..] {
165                    self.curr.longitude = longitude.parse().unwrap_or_default();
166                    self.curr.latitude = latitude.parse().unwrap_or_default();
167                }
168            }
169        }
170    }
171
172    fn endtag_cb(&mut self, event: &BytesEnd) {
173        let tag = String::from_utf8_lossy(event.name().as_ref())
174            .trim()
175            .to_lowercase();
176
177        match self.tag {
178            KmlTag::PlacemarkTimestampWhen => {
179                if tag == "when" {
180                    self.tag = KmlTag::PlacemarkTimestamp
181                }
182            }
183            KmlTag::PlacemarkTimestamp => {
184                if tag == "timestamp" {
185                    self.tag = KmlTag::Placemark
186                }
187            }
188            KmlTag::PlacemarkPointCoordinates => {
189                if tag == "coordinates" {
190                    self.tag = KmlTag::PlacemarkPoint
191                }
192            }
193            KmlTag::PlacemarkPoint => {
194                if tag == "point" {
195                    self.tag = KmlTag::Placemark
196                }
197            }
198            KmlTag::Placemark => {
199                if tag == "placemark" {
200                    if 0 != self.curr.timestamp
201                        && 0. != self.curr.latitude
202                        && 0. != self.curr.longitude
203                    {
204                        self.locations
205                            .push(std::mem::replace(&mut self.curr, Location::new()));
206                    }
207                    self.tag = KmlTag::Undefined;
208                }
209            }
210            KmlTag::Undefined => {}
211        }
212    }
213
214    fn starttag_cb<B: std::io::BufRead>(
215        &mut self,
216        event: &BytesStart,
217        reader: &quick_xml::Reader<B>,
218    ) {
219        let tag = String::from_utf8_lossy(event.name().as_ref())
220            .trim()
221            .to_lowercase();
222        if tag == "document" {
223            if let Some(addr) = event.attributes().filter_map(|a| a.ok()).find(|attr| {
224                String::from_utf8_lossy(attr.key.as_ref())
225                    .trim()
226                    .to_lowercase()
227                    == "addr"
228            }) {
229                self.addr = addr
230                    .decode_and_unescape_value(reader.decoder())
231                    .ok()
232                    .map(|a| a.into_owned());
233            }
234        } else if tag == "placemark" {
235            self.tag = KmlTag::Placemark;
236            self.curr.timestamp = 0;
237            self.curr.latitude = 0.0;
238            self.curr.longitude = 0.0;
239            self.curr.accuracy = 0.0
240        } else if tag == "timestamp" && self.tag == KmlTag::Placemark {
241            self.tag = KmlTag::PlacemarkTimestamp;
242        } else if tag == "when" && self.tag == KmlTag::PlacemarkTimestamp {
243            self.tag = KmlTag::PlacemarkTimestampWhen;
244        } else if tag == "point" && self.tag == KmlTag::Placemark {
245            self.tag = KmlTag::PlacemarkPoint;
246        } else if tag == "coordinates" && self.tag == KmlTag::PlacemarkPoint {
247            self.tag = KmlTag::PlacemarkPointCoordinates;
248            if let Some(acc) = event.attributes().find_map(|attr| {
249                attr.ok().filter(|a| {
250                    String::from_utf8_lossy(a.key.as_ref())
251                        .trim()
252                        .eq_ignore_ascii_case("accuracy")
253                })
254            }) {
255                let v = acc
256                    .decode_and_unescape_value(reader.decoder())
257                    .unwrap_or_default();
258
259                self.curr.accuracy = v.trim().parse().unwrap_or_default();
260            }
261        }
262    }
263}
264
265/// Enables location streaming in chat identified by `chat_id` for `seconds` seconds.
266#[expect(clippy::arithmetic_side_effects)]
267pub async fn send_locations_to_chat(
268    context: &Context,
269    chat_id: ChatId,
270    seconds: i64,
271) -> Result<()> {
272    ensure!(seconds >= 0);
273    ensure!(!chat_id.is_special());
274    let now = time();
275    let is_sending_locations_before = is_sending_locations_to_chat(context, Some(chat_id)).await?;
276    context
277        .sql
278        .execute(
279            "UPDATE chats    \
280         SET locations_send_begin=?,        \
281         locations_send_until=?  \
282         WHERE id=?",
283            (
284                if 0 != seconds { now } else { 0 },
285                if 0 != seconds { now + seconds } else { 0 },
286                chat_id,
287            ),
288        )
289        .await?;
290    if 0 != seconds && !is_sending_locations_before {
291        let mut msg = Message::new_text(stock_str::msg_location_enabled(context));
292        msg.param.set_cmd(SystemMessage::LocationStreamingEnabled);
293        chat::send_msg(context, chat_id, &mut msg)
294            .await
295            .unwrap_or_default();
296    } else if 0 == seconds && is_sending_locations_before {
297        let stock_str = stock_str::msg_location_disabled(context);
298        chat::add_info_msg(context, chat_id, &stock_str).await?;
299    }
300    context.emit_event(EventType::ChatModified(chat_id));
301    chatlist_events::emit_chatlist_item_changed(context, chat_id);
302    if 0 != seconds {
303        context.scheduler.interrupt_location().await;
304    }
305    Ok(())
306}
307
308/// Returns whether `chat_id` or any chat is sending locations.
309///
310/// If `chat_id` is `Some` only that chat is checked, otherwise returns `true` if any chat
311/// is sending locations.
312pub async fn is_sending_locations_to_chat(
313    context: &Context,
314    chat_id: Option<ChatId>,
315) -> Result<bool> {
316    let exists = match chat_id {
317        Some(chat_id) => {
318            context
319                .sql
320                .exists(
321                    "SELECT COUNT(id) FROM chats  WHERE id=?  AND locations_send_until>?;",
322                    (chat_id, time()),
323                )
324                .await?
325        }
326        None => {
327            context
328                .sql
329                .exists(
330                    "SELECT COUNT(id) FROM chats  WHERE locations_send_until>?;",
331                    (time(),),
332                )
333                .await?
334        }
335    };
336    Ok(exists)
337}
338
339/// Sets current location of the user device.
340pub async fn set(context: &Context, latitude: f64, longitude: f64, accuracy: f64) -> Result<bool> {
341    if latitude == 0.0 && longitude == 0.0 {
342        return Ok(true);
343    }
344    let mut continue_streaming = false;
345    let now = time();
346
347    let chats = context
348        .sql
349        .query_map_vec(
350            "SELECT id FROM chats WHERE locations_send_until>?;",
351            (now,),
352            |row| {
353                let id: i32 = row.get(0)?;
354                Ok(id)
355            },
356        )
357        .await?;
358
359    let mut stored_location = false;
360    for chat_id in chats {
361        context.sql.execute(
362                "INSERT INTO locations  \
363                 (latitude, longitude, accuracy, timestamp, chat_id, from_id) VALUES (?,?,?,?,?,?);",
364                 (
365                    latitude,
366                    longitude,
367                    accuracy,
368                    now,
369                    chat_id,
370                    ContactId::SELF,
371                )).await.context("Failed to store location")?;
372        stored_location = true;
373
374        info!(context, "Stored location for chat {chat_id}.");
375        continue_streaming = true;
376    }
377    if continue_streaming {
378        context.emit_location_changed(Some(ContactId::SELF)).await?;
379    };
380    if stored_location {
381        // Interrupt location loop so it may send a location-only message.
382        context.scheduler.interrupt_location().await;
383    }
384
385    Ok(continue_streaming)
386}
387
388/// Searches for locations in the given time range, optionally filtering by chat and contact IDs.
389#[expect(clippy::arithmetic_side_effects)]
390pub async fn get_range(
391    context: &Context,
392    chat_id: Option<ChatId>,
393    contact_id: Option<u32>,
394    timestamp_from: i64,
395    mut timestamp_to: i64,
396) -> Result<Vec<Location>> {
397    if timestamp_to == 0 {
398        timestamp_to = time() + 10;
399    }
400
401    let (disable_chat_id, chat_id) = match chat_id {
402        Some(chat_id) => (0, chat_id),
403        None => (1, ChatId::new(0)), // this ChatId is unused
404    };
405    let (disable_contact_id, contact_id) = match contact_id {
406        Some(contact_id) => (0, contact_id),
407        None => (1, 0), // this contact_id is unused
408    };
409    let list = context
410        .sql
411        .query_map_vec(
412            "SELECT l.id, l.latitude, l.longitude, l.accuracy, l.timestamp, l.independent, \
413             COALESCE(m.id, 0) AS msg_id, l.from_id, l.chat_id, COALESCE(m.txt, '') AS txt \
414             FROM locations l  LEFT JOIN msgs m ON l.id=m.location_id  WHERE (? OR l.chat_id=?) \
415             AND (? OR l.from_id=?) \
416             AND (l.independent=1 OR (l.timestamp>=? AND l.timestamp<=?)) \
417             ORDER BY l.timestamp DESC, l.id DESC, msg_id DESC;",
418            (
419                disable_chat_id,
420                chat_id,
421                disable_contact_id,
422                contact_id as i32,
423                timestamp_from,
424                timestamp_to,
425            ),
426            |row| {
427                let msg_id = row.get(6)?;
428                let txt: String = row.get(9)?;
429                let marker = if msg_id != 0 && is_marker(&txt) {
430                    Some(txt)
431                } else {
432                    None
433                };
434                let loc = Location {
435                    location_id: row.get(0)?,
436                    latitude: row.get(1)?,
437                    longitude: row.get(2)?,
438                    accuracy: row.get(3)?,
439                    timestamp: row.get(4)?,
440                    independent: row.get(5)?,
441                    msg_id,
442                    contact_id: row.get(7)?,
443                    chat_id: row.get(8)?,
444                    marker,
445                };
446                Ok(loc)
447            },
448        )
449        .await?;
450    Ok(list)
451}
452
453fn is_marker(txt: &str) -> bool {
454    let mut chars = txt.chars();
455    if let Some(c) = chars.next() {
456        !c.is_whitespace() && chars.next().is_none()
457    } else {
458        false
459    }
460}
461
462/// Deletes expired locations.
463///
464/// Only path locations are deleted.
465/// POIs should be deleted when corresponding message is deleted.
466pub(crate) async fn delete_expired(context: &Context, now: i64) -> Result<()> {
467    let Some(delete_device_after) = context.get_config_delete_device_after().await? else {
468        return Ok(());
469    };
470
471    let threshold_timestamp = now.saturating_sub(delete_device_after);
472    let deleted = context
473        .sql
474        .execute(
475            "DELETE FROM locations WHERE independent=0 AND timestamp < ?",
476            (threshold_timestamp,),
477        )
478        .await?
479        > 0;
480    if deleted {
481        info!(context, "Deleted {deleted} expired locations.");
482        context.emit_location_changed(None).await?;
483    }
484    Ok(())
485}
486
487/// Deletes location if it is an independent location.
488///
489/// This function is used when a message is deleted
490/// that has a corresponding `location_id`.
491pub(crate) async fn delete_poi_location(context: &Context, location_id: u32) -> Result<()> {
492    context
493        .sql
494        .execute(
495            "DELETE FROM locations WHERE independent = 1 AND id=?",
496            (location_id as i32,),
497        )
498        .await?;
499    Ok(())
500}
501
502/// Deletes POI locations that don't have corresponding message anymore.
503pub(crate) async fn delete_orphaned_poi_locations(context: &Context) -> Result<()> {
504    context.sql.execute("
505    DELETE FROM locations
506    WHERE independent=1 AND id NOT IN
507    (SELECT location_id from MSGS LEFT JOIN locations
508     ON locations.id=location_id
509     WHERE location_id>0 -- This check makes the query faster by not looking for locations with ID 0 that don't exist.
510     AND msgs.chat_id != ?)", (DC_CHAT_ID_TRASH,)).await?;
511    Ok(())
512}
513
514/// Returns `location.kml` contents.
515#[expect(clippy::arithmetic_side_effects)]
516pub async fn get_kml(context: &Context, chat_id: ChatId) -> Result<Option<(String, u32)>> {
517    let mut last_added_location_id = 0;
518
519    let self_addr = context.get_primary_self_addr().await?;
520
521    let (locations_send_begin, locations_send_until, locations_last_sent) = context.sql.query_row(
522        "SELECT locations_send_begin, locations_send_until, locations_last_sent  FROM chats  WHERE id=?;",
523        (chat_id,), |row| {
524            let send_begin: i64 = row.get(0)?;
525            let send_until: i64 = row.get(1)?;
526            let last_sent: i64 = row.get(2)?;
527
528            Ok((send_begin, send_until, last_sent))
529        })
530        .await?;
531
532    let now = time();
533    let mut location_count = 0;
534    let mut ret = String::new();
535    if locations_send_begin != 0 && now <= locations_send_until {
536        ret += &format!(
537            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
538            <kml xmlns=\"http://www.opengis.net/kml/2.2\">\n<Document addr=\"{self_addr}\">\n",
539        );
540
541        context
542            .sql
543            .query_map(
544                "SELECT id, latitude, longitude, accuracy, timestamp \
545             FROM locations  WHERE from_id=? \
546             AND timestamp>=? \
547             AND (timestamp>=? OR \
548                  timestamp=(SELECT MAX(timestamp) FROM locations WHERE from_id=?)) \
549             AND independent=0 \
550             GROUP BY timestamp \
551             ORDER BY timestamp;",
552             (
553                    ContactId::SELF,
554                    locations_send_begin,
555                    locations_last_sent,
556                    ContactId::SELF
557                ),
558                |row| {
559                    let location_id: i32 = row.get(0)?;
560                    let latitude: f64 = row.get(1)?;
561                    let longitude: f64 = row.get(2)?;
562                    let accuracy: f64 = row.get(3)?;
563                    let timestamp = get_kml_timestamp(row.get(4)?);
564
565                    Ok((location_id, latitude, longitude, accuracy, timestamp))
566                },
567                |rows| {
568                    for row in rows {
569                        let (location_id, latitude, longitude, accuracy, timestamp) = row?;
570                        ret += &format!(
571                            "<Placemark>\
572                <Timestamp><when>{timestamp}</when></Timestamp>\
573                <Point><coordinates accuracy=\"{accuracy}\">{longitude},{latitude}</coordinates></Point>\
574                </Placemark>\n"
575                        );
576                        location_count += 1;
577                        last_added_location_id = location_id as u32;
578                    }
579                    Ok(())
580                },
581            )
582            .await?;
583        ret += "</Document>\n</kml>";
584    }
585
586    if location_count > 0 {
587        Ok(Some((ret, last_added_location_id)))
588    } else {
589        Ok(None)
590    }
591}
592
593fn get_kml_timestamp(utc: i64) -> String {
594    // Returns a string formatted as YYYY-MM-DDTHH:MM:SSZ. The trailing `Z` indicates UTC.
595    chrono::DateTime::<chrono::Utc>::from_timestamp(utc, 0)
596        .unwrap()
597        .format("%Y-%m-%dT%H:%M:%SZ")
598        .to_string()
599}
600
601/// Returns a KML document containing a single location with the given timestamp and coordinates.
602pub fn get_message_kml(timestamp: i64, latitude: f64, longitude: f64) -> String {
603    format!(
604        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
605         <kml xmlns=\"http://www.opengis.net/kml/2.2\">\n\
606         <Document>\n\
607         <Placemark>\
608         <Timestamp><when>{}</when></Timestamp>\
609         <Point><coordinates>{},{}</coordinates></Point>\
610         </Placemark>\n\
611         </Document>\n\
612         </kml>",
613        get_kml_timestamp(timestamp),
614        longitude,
615        latitude,
616    )
617}
618
619/// Sets the timestamp of the last time location was sent in the chat.
620pub async fn set_kml_sent_timestamp(
621    context: &Context,
622    chat_id: ChatId,
623    timestamp: i64,
624) -> Result<()> {
625    context
626        .sql
627        .execute(
628            "UPDATE chats SET locations_last_sent=? WHERE id=?;",
629            (timestamp, chat_id),
630        )
631        .await?;
632    Ok(())
633}
634
635/// Sets the location of the message.
636pub async fn set_msg_location_id(context: &Context, msg_id: MsgId, location_id: u32) -> Result<()> {
637    context
638        .sql
639        .execute(
640            "UPDATE msgs SET location_id=? WHERE id=?;",
641            (location_id, msg_id),
642        )
643        .await?;
644
645    Ok(())
646}
647
648/// Saves given locations to the database.
649///
650/// Returns the database row ID of the location with the highest timestamp.
651pub(crate) async fn save(
652    context: &Context,
653    chat_id: ChatId,
654    contact_id: ContactId,
655    locations: &[Location],
656    independent: bool,
657) -> Result<Option<u32>> {
658    ensure!(!chat_id.is_special(), "Invalid chat id");
659
660    let mut newest_timestamp = 0;
661    let mut newest_location_id = None;
662
663    let stmt_insert = "INSERT INTO locations\
664             (timestamp, from_id, chat_id, latitude, longitude, accuracy, independent) \
665             VALUES (?,?,?,?,?,?,?);";
666
667    for location in locations {
668        let &Location {
669            timestamp,
670            latitude,
671            longitude,
672            accuracy,
673            ..
674        } = location;
675
676        context
677            .sql
678            .call_write(|conn| {
679                let mut stmt_test = conn
680                    .prepare_cached("SELECT id FROM locations WHERE timestamp=? AND from_id=?")?;
681                let mut stmt_insert = conn.prepare_cached(stmt_insert)?;
682
683                let exists = stmt_test.exists((timestamp, contact_id))?;
684
685                if independent || !exists {
686                    stmt_insert.execute((
687                        timestamp,
688                        contact_id,
689                        chat_id,
690                        latitude,
691                        longitude,
692                        accuracy,
693                        independent,
694                    ))?;
695
696                    if timestamp > newest_timestamp {
697                        newest_timestamp = timestamp;
698                        newest_location_id = Some(u32::try_from(conn.last_insert_rowid())?);
699                    }
700                }
701
702                Ok(())
703            })
704            .await?;
705    }
706
707    Ok(newest_location_id)
708}
709
710pub(crate) async fn location_loop(context: &Context, interrupt_receiver: Receiver<()>) {
711    loop {
712        let next_event = match maybe_send_locations(context).await {
713            Err(err) => {
714                warn!(context, "maybe_send_locations failed: {:#}", err);
715                Some(60) // Retry one minute later.
716            }
717            Ok(next_event) => next_event,
718        };
719
720        let duration = if let Some(next_event) = next_event {
721            Duration::from_secs(next_event)
722        } else {
723            Duration::from_secs(86400)
724        };
725
726        info!(
727            context,
728            "Location loop is waiting for {} or interrupt",
729            duration_to_str(duration)
730        );
731        match timeout(duration, interrupt_receiver.recv()).await {
732            Err(_err) => {
733                info!(context, "Location loop timeout.");
734            }
735            Ok(Err(err)) => {
736                warn!(
737                    context,
738                    "Interrupt channel closed, location loop exits now: {err:#}."
739                );
740                return;
741            }
742            Ok(Ok(())) => {
743                info!(context, "Location loop received interrupt.");
744            }
745        }
746    }
747}
748
749/// Returns number of seconds until the next time location streaming for some chat ends
750/// automatically.
751#[expect(clippy::arithmetic_side_effects)]
752async fn maybe_send_locations(context: &Context) -> Result<Option<u64>> {
753    let mut next_event: Option<u64> = None;
754
755    let now = time();
756    let rows = context
757        .sql
758        .query_map_vec(
759            "SELECT id, locations_send_begin, locations_send_until, locations_last_sent
760             FROM chats
761             WHERE locations_send_until>0",
762            [],
763            |row| {
764                let chat_id: ChatId = row.get(0)?;
765                let locations_send_begin: i64 = row.get(1)?;
766                let locations_send_until: i64 = row.get(2)?;
767                let locations_last_sent: i64 = row.get(3)?;
768                Ok((
769                    chat_id,
770                    locations_send_begin,
771                    locations_send_until,
772                    locations_last_sent,
773                ))
774            },
775        )
776        .await
777        .context("failed to query location streaming chats")?;
778
779    for (chat_id, locations_send_begin, locations_send_until, locations_last_sent) in rows {
780        if locations_send_begin > 0 && locations_send_until > now {
781            let can_send = now > locations_last_sent + 60;
782            let has_locations = context
783                .sql
784                .exists(
785                    "SELECT COUNT(id) \
786     FROM locations \
787     WHERE from_id=? \
788     AND timestamp>=? \
789     AND timestamp>? \
790     AND independent=0",
791                    (ContactId::SELF, locations_send_begin, locations_last_sent),
792                )
793                .await?;
794
795            next_event = next_event
796                .into_iter()
797                .chain(u64::try_from(locations_send_until - now))
798                .min();
799
800            if has_locations {
801                if can_send {
802                    // Send location-only message.
803                    // Pending locations are attached automatically to every message,
804                    // so also to this empty text message.
805                    info!(
806                        context,
807                        "Chat {} has pending locations, sending them.", chat_id
808                    );
809                    let mut msg = Message::new(Viewtype::Text);
810                    msg.hidden = true;
811                    msg.param.set_cmd(SystemMessage::LocationOnly);
812                    chat::send_msg(context, chat_id, &mut msg).await?;
813                } else {
814                    // Wait until pending locations can be sent.
815                    info!(
816                        context,
817                        "Chat {} has pending locations, but they can't be sent yet.", chat_id
818                    );
819                    next_event = next_event
820                        .into_iter()
821                        .chain(u64::try_from(locations_last_sent + 61 - now))
822                        .min();
823                }
824            } else {
825                info!(
826                    context,
827                    "Chat {} has location streaming enabled, but no pending locations.", chat_id
828                );
829            }
830        } else {
831            // Location streaming was either explicitly disabled (locations_send_begin = 0) or
832            // locations_send_until is in the past.
833            info!(
834                context,
835                "Disabling location streaming for chat {}.", chat_id
836            );
837            context
838                .sql
839                .execute(
840                    "UPDATE chats \
841                         SET locations_send_begin=0, locations_send_until=0 \
842                         WHERE id=?",
843                    (chat_id,),
844                )
845                .await
846                .context("failed to disable location streaming")?;
847
848            let stock_str = stock_str::msg_location_disabled(context);
849            chat::add_info_msg(context, chat_id, &stock_str).await?;
850            context.emit_event(EventType::ChatModified(chat_id));
851            chatlist_events::emit_chatlist_item_changed(context, chat_id);
852        }
853    }
854
855    Ok(next_event)
856}
857
858#[cfg(test)]
859mod tests {
860    use super::*;
861    use crate::config::Config;
862    use crate::message::MessageState;
863    use crate::receive_imf::receive_imf;
864    use crate::test_utils::{TestContext, TestContextManager};
865    use crate::tools::SystemTime;
866
867    #[test]
868    fn test_kml_parse() {
869        let xml =
870            b"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n<Document addr=\"user@example.org\">\n<Placemark><Timestamp><when>2019-03-06T21:09:57Z</when></Timestamp><Point><coordinates accuracy=\"32.000000\">9.423110,53.790302</coordinates></Point></Placemark>\n<PlaceMARK>\n<Timestamp><WHEN > \n\t2018-12-13T22:11:12Z\t</WHEN></Timestamp><Point><coordinates aCCuracy=\"2.500000\"> 19.423110 \t , \n 63.790302\n </coordinates></Point></PlaceMARK>\n</Document>\n</kml>";
871
872        let kml = Kml::parse(xml).expect("parsing failed");
873
874        assert!(kml.addr.is_some());
875        assert_eq!(kml.addr.as_ref().unwrap(), "user@example.org",);
876
877        let locations_ref = &kml.locations;
878        assert_eq!(locations_ref.len(), 2);
879
880        assert!(locations_ref[0].latitude > 53.6f64);
881        assert!(locations_ref[0].latitude < 53.8f64);
882        assert!(locations_ref[0].longitude > 9.3f64);
883        assert!(locations_ref[0].longitude < 9.5f64);
884        assert!(locations_ref[0].accuracy > 31.9f64);
885        assert!(locations_ref[0].accuracy < 32.1f64);
886        assert_eq!(locations_ref[0].timestamp, 1551906597);
887
888        assert!(locations_ref[1].latitude > 63.6f64);
889        assert!(locations_ref[1].latitude < 63.8f64);
890        assert!(locations_ref[1].longitude > 19.3f64);
891        assert!(locations_ref[1].longitude < 19.5f64);
892        assert!(locations_ref[1].accuracy > 2.4f64);
893        assert!(locations_ref[1].accuracy < 2.6f64);
894        assert_eq!(locations_ref[1].timestamp, 1544739072);
895    }
896
897    #[test]
898    fn test_kml_parse_error() {
899        let xml = b"<?><xmlversi\"\"\">?</document>";
900        assert!(Kml::parse(xml).is_err());
901    }
902
903    #[test]
904    fn test_get_message_kml() {
905        let timestamp = 1598490000;
906
907        let xml = get_message_kml(timestamp, 51.423723f64, 8.552556f64);
908        let kml = Kml::parse(xml.as_bytes()).expect("parsing failed");
909        let locations_ref = &kml.locations;
910        assert_eq!(locations_ref.len(), 1);
911
912        assert!(locations_ref[0].latitude >= 51.423723f64);
913        assert!(locations_ref[0].latitude < 51.423724f64);
914        assert!(locations_ref[0].longitude >= 8.552556f64);
915        assert!(locations_ref[0].longitude < 8.552557f64);
916        assert!(locations_ref[0].accuracy.abs() < f64::EPSILON);
917        assert_eq!(locations_ref[0].timestamp, timestamp);
918    }
919
920    #[test]
921    fn test_is_marker() {
922        assert!(is_marker("f"));
923        assert!(!is_marker("foo"));
924        assert!(is_marker("🏠"));
925        assert!(!is_marker(" "));
926        assert!(!is_marker("\t"));
927    }
928
929    /// Tests that location.kml is hidden.
930    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
931    async fn receive_location_kml() -> Result<()> {
932        let alice = TestContext::new_alice().await;
933
934        receive_imf(
935            &alice,
936            br#"Subject: Hello
937Message-ID: hello@example.net
938To: Alice <alice@example.org>
939From: Bob <bob@example.net>
940Date: Mon, 20 Dec 2021 00:00:00 +0000
941Chat-Version: 1.0
942Content-Type: text/plain; charset=utf-8; format=flowed; delsp=no
943
944Text message."#,
945            false,
946        )
947        .await?;
948        let received_msg = alice.get_last_msg().await;
949        assert_eq!(received_msg.text, "Text message.");
950
951        receive_imf(
952            &alice,
953            br#"Subject: locations
954MIME-Version: 1.0
955To: <alice@example.org>
956From: <bob@example.net>
957Date: Tue, 21 Dec 2021 00:00:00 +0000
958Chat-Version: 1.0
959Message-ID: <foobar@example.net>
960Content-Type: multipart/mixed; boundary="U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF"
961
962
963--U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF
964Content-Type: text/plain; charset=utf-8; format=flowed; delsp=no
965
966
967
968--U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF
969Content-Type: application/vnd.google-earth.kml+xml
970Content-Disposition: attachment; filename="location.kml"
971
972<?xml version="1.0" encoding="UTF-8"?>
973<kml xmlns="http://www.opengis.net/kml/2.2">
974<Document addr="bob@example.net">
975<Placemark><Timestamp><when>2021-11-21T00:00:00Z</when></Timestamp><Point><coordinates accuracy="1.0000000000000000">10.00000000000000,20.00000000000000</coordinates></Point></Placemark>
976</Document>
977</kml>
978
979--U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF--"#,
980            false,
981        )
982        .await?;
983
984        // Received location message is not visible, last message stays the same.
985        let received_msg2 = alice.get_last_msg().await;
986        assert_eq!(received_msg2.id, received_msg.id);
987
988        let locations = get_range(&alice, None, None, 0, 0).await?;
989        assert_eq!(locations.len(), 1);
990        Ok(())
991    }
992
993    /// Tests that `location.kml` is not hidden and not seen if it contains a message.
994    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
995    async fn receive_visible_location_kml() -> Result<()> {
996        let alice = TestContext::new_alice().await;
997
998        receive_imf(
999            &alice,
1000            br#"Subject: locations
1001MIME-Version: 1.0
1002To: <alice@example.org>
1003From: <bob@example.net>
1004Date: Tue, 21 Dec 2021 00:00:00 +0000
1005Chat-Version: 1.0
1006Message-ID: <foobar@localhost>
1007Content-Type: multipart/mixed; boundary="U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF"
1008
1009
1010--U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF
1011Content-Type: text/plain; charset=utf-8; format=flowed; delsp=no
1012
1013Text message.
1014
1015
1016--U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF
1017Content-Type: application/vnd.google-earth.kml+xml
1018Content-Disposition: attachment; filename="location.kml"
1019
1020<?xml version="1.0" encoding="UTF-8"?>
1021<kml xmlns="http://www.opengis.net/kml/2.2">
1022<Document addr="bob@example.net">
1023<Placemark><Timestamp><when>2021-11-21T00:00:00Z</when></Timestamp><Point><coordinates accuracy="1.0000000000000000">10.00000000000000,20.00000000000000</coordinates></Point></Placemark>
1024</Document>
1025</kml>
1026
1027--U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF--"#,
1028            false,
1029        )
1030        .await?;
1031
1032        let received_msg = alice.get_last_msg().await;
1033        assert_eq!(received_msg.text, "Text message.");
1034        assert_eq!(received_msg.state, MessageState::InFresh);
1035
1036        let locations = get_range(&alice, None, None, 0, 0).await?;
1037        assert_eq!(locations.len(), 1);
1038        Ok(())
1039    }
1040
1041    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1042    async fn test_send_locations_to_chat() -> Result<()> {
1043        let alice = TestContext::new_alice().await;
1044        let bob = TestContext::new_bob().await;
1045
1046        let alice_chat = alice.create_chat(&bob).await;
1047        send_locations_to_chat(&alice, alice_chat.id, 1000).await?;
1048        let sent = alice.pop_sent_msg().await;
1049        let msg = bob.recv_msg(&sent).await;
1050        assert_eq!(msg.text, "Location streaming enabled by alice@example.org.");
1051        let bob_chat_id = msg.chat_id;
1052
1053        assert_eq!(set(&alice, 10.0, 20.0, 1.0).await?, true);
1054
1055        // Send image without text.
1056        let file_name = "image.png";
1057        let bytes = include_bytes!("../test-data/image/logo.png");
1058        let file = alice.get_blobdir().join(file_name);
1059        tokio::fs::write(&file, bytes).await?;
1060        let mut msg = Message::new(Viewtype::Image);
1061        msg.set_file_and_deduplicate(&alice, &file, Some("logo.png"), None)?;
1062        let sent = alice.send_msg(alice_chat.id, &mut msg).await;
1063        let alice_msg = Message::load_from_db(&alice, sent.sender_msg_id).await?;
1064        assert_eq!(alice_msg.has_location(), false);
1065
1066        let msg = bob.recv_msg_opt(&sent).await.unwrap();
1067        assert!(msg.chat_id == bob_chat_id);
1068        assert_eq!(msg.msg_ids.len(), 1);
1069
1070        let bob_msg = Message::load_from_db(&bob, *msg.msg_ids.first().unwrap()).await?;
1071        assert_eq!(bob_msg.chat_id, bob_chat_id);
1072        assert_eq!(bob_msg.viewtype, Viewtype::Image);
1073        assert_eq!(bob_msg.has_location(), false);
1074
1075        let bob_locations = get_range(&bob, None, None, 0, 0).await?;
1076        assert_eq!(bob_locations.len(), 1);
1077
1078        Ok(())
1079    }
1080
1081    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1082    async fn test_delete_expired_locations() -> Result<()> {
1083        let mut tcm = TestContextManager::new();
1084        let alice = &tcm.alice().await;
1085        let bob = &tcm.bob().await;
1086
1087        // Alice enables deletion of messages from device after 1 week.
1088        alice
1089            .set_config(Config::DeleteDeviceAfter, Some("604800"))
1090            .await?;
1091        // Bob enables deletion of messages from device after 1 day.
1092        bob.set_config(Config::DeleteDeviceAfter, Some("86400"))
1093            .await?;
1094
1095        let alice_chat = alice.create_chat(bob).await;
1096
1097        // Alice enables location streaming.
1098        // Bob receives a message saying that Alice enabled location streaming.
1099        send_locations_to_chat(alice, alice_chat.id, 60).await?;
1100        bob.recv_msg(&alice.pop_sent_msg().await).await;
1101
1102        // Alice gets new location from GPS.
1103        assert_eq!(set(alice, 10.0, 20.0, 1.0).await?, true);
1104        assert_eq!(get_range(alice, None, None, 0, 0).await?.len(), 1);
1105
1106        // 10 seconds later location sending stream manages to send location.
1107        SystemTime::shift(Duration::from_secs(10));
1108        delete_expired(alice, time()).await?;
1109        maybe_send_locations(alice).await?;
1110        bob.recv_msg_opt(&alice.pop_sent_msg().await).await;
1111        assert_eq!(get_range(alice, None, None, 0, 0).await?.len(), 1);
1112        assert_eq!(get_range(bob, None, None, 0, 0).await?.len(), 1);
1113
1114        // Location-only messages are "auto-generated", but they mustn't make the contact a bot.
1115        let contact = bob.add_or_lookup_contact(alice).await;
1116        assert!(!contact.is_bot());
1117
1118        // Day later Bob removes location.
1119        SystemTime::shift(Duration::from_secs(86400));
1120        delete_expired(alice, time()).await?;
1121        delete_expired(bob, time()).await?;
1122        assert_eq!(get_range(alice, None, None, 0, 0).await?.len(), 1);
1123        assert_eq!(get_range(bob, None, None, 0, 0).await?.len(), 0);
1124
1125        // Week late Alice removes location.
1126        SystemTime::shift(Duration::from_secs(604800));
1127        delete_expired(alice, time()).await?;
1128        delete_expired(bob, time()).await?;
1129        assert_eq!(get_range(alice, None, None, 0, 0).await?.len(), 0);
1130        assert_eq!(get_range(bob, None, None, 0, 0).await?.len(), 0);
1131
1132        Ok(())
1133    }
1134}