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::{info, 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.
266pub async fn send_locations_to_chat(
267    context: &Context,
268    chat_id: ChatId,
269    seconds: i64,
270) -> Result<()> {
271    ensure!(seconds >= 0);
272    ensure!(!chat_id.is_special());
273    let now = time();
274    let is_sending_locations_before = is_sending_locations_to_chat(context, Some(chat_id)).await?;
275    context
276        .sql
277        .execute(
278            "UPDATE chats    \
279         SET locations_send_begin=?,        \
280         locations_send_until=?  \
281         WHERE id=?",
282            (
283                if 0 != seconds { now } else { 0 },
284                if 0 != seconds { now + seconds } else { 0 },
285                chat_id,
286            ),
287        )
288        .await?;
289    if 0 != seconds && !is_sending_locations_before {
290        let mut msg = Message::new_text(stock_str::msg_location_enabled(context).await);
291        msg.param.set_cmd(SystemMessage::LocationStreamingEnabled);
292        chat::send_msg(context, chat_id, &mut msg)
293            .await
294            .unwrap_or_default();
295    } else if 0 == seconds && is_sending_locations_before {
296        let stock_str = stock_str::msg_location_disabled(context).await;
297        chat::add_info_msg(context, chat_id, &stock_str, now).await?;
298    }
299    context.emit_event(EventType::ChatModified(chat_id));
300    chatlist_events::emit_chatlist_item_changed(context, chat_id);
301    if 0 != seconds {
302        context.scheduler.interrupt_location().await;
303    }
304    Ok(())
305}
306
307/// Returns whether `chat_id` or any chat is sending locations.
308///
309/// If `chat_id` is `Some` only that chat is checked, otherwise returns `true` if any chat
310/// is sending locations.
311pub async fn is_sending_locations_to_chat(
312    context: &Context,
313    chat_id: Option<ChatId>,
314) -> Result<bool> {
315    let exists = match chat_id {
316        Some(chat_id) => {
317            context
318                .sql
319                .exists(
320                    "SELECT COUNT(id) FROM chats  WHERE id=?  AND locations_send_until>?;",
321                    (chat_id, time()),
322                )
323                .await?
324        }
325        None => {
326            context
327                .sql
328                .exists(
329                    "SELECT COUNT(id) FROM chats  WHERE locations_send_until>?;",
330                    (time(),),
331                )
332                .await?
333        }
334    };
335    Ok(exists)
336}
337
338/// Sets current location of the user device.
339pub async fn set(context: &Context, latitude: f64, longitude: f64, accuracy: f64) -> Result<bool> {
340    if latitude == 0.0 && longitude == 0.0 {
341        return Ok(true);
342    }
343    let mut continue_streaming = false;
344    let now = time();
345
346    let chats = context
347        .sql
348        .query_map_vec(
349            "SELECT id FROM chats WHERE locations_send_until>?;",
350            (now,),
351            |row| row.get::<_, i32>(0),
352        )
353        .await?;
354
355    let mut stored_location = false;
356    for chat_id in chats {
357        context.sql.execute(
358                "INSERT INTO locations  \
359                 (latitude, longitude, accuracy, timestamp, chat_id, from_id) VALUES (?,?,?,?,?,?);",
360                 (
361                    latitude,
362                    longitude,
363                    accuracy,
364                    now,
365                    chat_id,
366                    ContactId::SELF,
367                )).await.context("Failed to store location")?;
368        stored_location = true;
369
370        info!(context, "Stored location for chat {chat_id}.");
371        continue_streaming = true;
372    }
373    if continue_streaming {
374        context.emit_location_changed(Some(ContactId::SELF)).await?;
375    };
376    if stored_location {
377        // Interrupt location loop so it may send a location-only message.
378        context.scheduler.interrupt_location().await;
379    }
380
381    Ok(continue_streaming)
382}
383
384/// Searches for locations in the given time range, optionally filtering by chat and contact IDs.
385pub async fn get_range(
386    context: &Context,
387    chat_id: Option<ChatId>,
388    contact_id: Option<u32>,
389    timestamp_from: i64,
390    mut timestamp_to: i64,
391) -> Result<Vec<Location>> {
392    if timestamp_to == 0 {
393        timestamp_to = time() + 10;
394    }
395
396    let (disable_chat_id, chat_id) = match chat_id {
397        Some(chat_id) => (0, chat_id),
398        None => (1, ChatId::new(0)), // this ChatId is unused
399    };
400    let (disable_contact_id, contact_id) = match contact_id {
401        Some(contact_id) => (0, contact_id),
402        None => (1, 0), // this contact_id is unused
403    };
404    let list = context
405        .sql
406        .query_map_vec(
407            "SELECT l.id, l.latitude, l.longitude, l.accuracy, l.timestamp, l.independent, \
408             COALESCE(m.id, 0) AS msg_id, l.from_id, l.chat_id, COALESCE(m.txt, '') AS txt \
409             FROM locations l  LEFT JOIN msgs m ON l.id=m.location_id  WHERE (? OR l.chat_id=?) \
410             AND (? OR l.from_id=?) \
411             AND (l.independent=1 OR (l.timestamp>=? AND l.timestamp<=?)) \
412             ORDER BY l.timestamp DESC, l.id DESC, msg_id DESC;",
413            (
414                disable_chat_id,
415                chat_id,
416                disable_contact_id,
417                contact_id as i32,
418                timestamp_from,
419                timestamp_to,
420            ),
421            |row| {
422                let msg_id = row.get(6)?;
423                let txt: String = row.get(9)?;
424                let marker = if msg_id != 0 && is_marker(&txt) {
425                    Some(txt)
426                } else {
427                    None
428                };
429                let loc = Location {
430                    location_id: row.get(0)?,
431                    latitude: row.get(1)?,
432                    longitude: row.get(2)?,
433                    accuracy: row.get(3)?,
434                    timestamp: row.get(4)?,
435                    independent: row.get(5)?,
436                    msg_id,
437                    contact_id: row.get(7)?,
438                    chat_id: row.get(8)?,
439                    marker,
440                };
441                Ok(loc)
442            },
443        )
444        .await?;
445    Ok(list)
446}
447
448fn is_marker(txt: &str) -> bool {
449    let mut chars = txt.chars();
450    if let Some(c) = chars.next() {
451        !c.is_whitespace() && chars.next().is_none()
452    } else {
453        false
454    }
455}
456
457/// Deletes all locations from the database.
458pub async fn delete_all(context: &Context) -> Result<()> {
459    context.sql.execute("DELETE FROM locations;", ()).await?;
460    context.emit_location_changed(None).await?;
461    Ok(())
462}
463
464/// Deletes expired locations.
465///
466/// Only path locations are deleted.
467/// POIs should be deleted when corresponding message is deleted.
468pub(crate) async fn delete_expired(context: &Context, now: i64) -> Result<()> {
469    let Some(delete_device_after) = context.get_config_delete_device_after().await? else {
470        return Ok(());
471    };
472
473    let threshold_timestamp = now.saturating_sub(delete_device_after);
474    let deleted = context
475        .sql
476        .execute(
477            "DELETE FROM locations WHERE independent=0 AND timestamp < ?",
478            (threshold_timestamp,),
479        )
480        .await?
481        > 0;
482    if deleted {
483        info!(context, "Deleted {deleted} expired locations.");
484        context.emit_location_changed(None).await?;
485    }
486    Ok(())
487}
488
489/// Deletes location if it is an independent location.
490///
491/// This function is used when a message is deleted
492/// that has a corresponding `location_id`.
493pub(crate) async fn delete_poi_location(context: &Context, location_id: u32) -> Result<()> {
494    context
495        .sql
496        .execute(
497            "DELETE FROM locations WHERE independent = 1 AND id=?",
498            (location_id as i32,),
499        )
500        .await?;
501    Ok(())
502}
503
504/// Deletes POI locations that don't have corresponding message anymore.
505pub(crate) async fn delete_orphaned_poi_locations(context: &Context) -> Result<()> {
506    context.sql.execute("
507    DELETE FROM locations
508    WHERE independent=1 AND id NOT IN
509    (SELECT location_id from MSGS LEFT JOIN locations
510     ON locations.id=location_id
511     WHERE location_id>0 -- This check makes the query faster by not looking for locations with ID 0 that don't exist.
512     AND msgs.chat_id != ?)", (DC_CHAT_ID_TRASH,)).await?;
513    Ok(())
514}
515
516/// Returns `location.kml` contents.
517pub async fn get_kml(context: &Context, chat_id: ChatId) -> Result<Option<(String, u32)>> {
518    let mut last_added_location_id = 0;
519
520    let self_addr = context.get_primary_self_addr().await?;
521
522    let (locations_send_begin, locations_send_until, locations_last_sent) = context.sql.query_row(
523        "SELECT locations_send_begin, locations_send_until, locations_last_sent  FROM chats  WHERE id=?;",
524        (chat_id,), |row| {
525            let send_begin: i64 = row.get(0)?;
526            let send_until: i64 = row.get(1)?;
527            let last_sent: i64 = row.get(2)?;
528
529            Ok((send_begin, send_until, last_sent))
530        })
531        .await?;
532
533    let now = time();
534    let mut location_count = 0;
535    let mut ret = String::new();
536    if locations_send_begin != 0 && now <= locations_send_until {
537        ret += &format!(
538            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
539            <kml xmlns=\"http://www.opengis.net/kml/2.2\">\n<Document addr=\"{self_addr}\">\n",
540        );
541
542        context
543            .sql
544            .query_map(
545                "SELECT id, latitude, longitude, accuracy, timestamp \
546             FROM locations  WHERE from_id=? \
547             AND timestamp>=? \
548             AND (timestamp>=? OR \
549                  timestamp=(SELECT MAX(timestamp) FROM locations WHERE from_id=?)) \
550             AND independent=0 \
551             GROUP BY timestamp \
552             ORDER BY timestamp;",
553             (
554                    ContactId::SELF,
555                    locations_send_begin,
556                    locations_last_sent,
557                    ContactId::SELF
558                ),
559                |row| {
560                    let location_id: i32 = row.get(0)?;
561                    let latitude: f64 = row.get(1)?;
562                    let longitude: f64 = row.get(2)?;
563                    let accuracy: f64 = row.get(3)?;
564                    let timestamp = get_kml_timestamp(row.get(4)?);
565
566                    Ok((location_id, latitude, longitude, accuracy, timestamp))
567                },
568                |rows| {
569                    for row in rows {
570                        let (location_id, latitude, longitude, accuracy, timestamp) = row?;
571                        ret += &format!(
572                            "<Placemark>\
573                <Timestamp><when>{timestamp}</when></Timestamp>\
574                <Point><coordinates accuracy=\"{accuracy}\">{longitude},{latitude}</coordinates></Point>\
575                </Placemark>\n"
576                        );
577                        location_count += 1;
578                        last_added_location_id = location_id as u32;
579                    }
580                    Ok(())
581                },
582            )
583            .await?;
584        ret += "</Document>\n</kml>";
585    }
586
587    if location_count > 0 {
588        Ok(Some((ret, last_added_location_id)))
589    } else {
590        Ok(None)
591    }
592}
593
594fn get_kml_timestamp(utc: i64) -> String {
595    // Returns a string formatted as YYYY-MM-DDTHH:MM:SSZ. The trailing `Z` indicates UTC.
596    chrono::DateTime::<chrono::Utc>::from_timestamp(utc, 0)
597        .unwrap()
598        .format("%Y-%m-%dT%H:%M:%SZ")
599        .to_string()
600}
601
602/// Returns a KML document containing a single location with the given timestamp and coordinates.
603pub fn get_message_kml(timestamp: i64, latitude: f64, longitude: f64) -> String {
604    format!(
605        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
606         <kml xmlns=\"http://www.opengis.net/kml/2.2\">\n\
607         <Document>\n\
608         <Placemark>\
609         <Timestamp><when>{}</when></Timestamp>\
610         <Point><coordinates>{},{}</coordinates></Point>\
611         </Placemark>\n\
612         </Document>\n\
613         </kml>",
614        get_kml_timestamp(timestamp),
615        longitude,
616        latitude,
617    )
618}
619
620/// Sets the timestamp of the last time location was sent in the chat.
621pub async fn set_kml_sent_timestamp(
622    context: &Context,
623    chat_id: ChatId,
624    timestamp: i64,
625) -> Result<()> {
626    context
627        .sql
628        .execute(
629            "UPDATE chats SET locations_last_sent=? WHERE id=?;",
630            (timestamp, chat_id),
631        )
632        .await?;
633    Ok(())
634}
635
636/// Sets the location of the message.
637pub async fn set_msg_location_id(context: &Context, msg_id: MsgId, location_id: u32) -> Result<()> {
638    context
639        .sql
640        .execute(
641            "UPDATE msgs SET location_id=? WHERE id=?;",
642            (location_id, msg_id),
643        )
644        .await?;
645
646    Ok(())
647}
648
649/// Saves given locations to the database.
650///
651/// Returns the database row ID of the location with the highest timestamp.
652pub(crate) async fn save(
653    context: &Context,
654    chat_id: ChatId,
655    contact_id: ContactId,
656    locations: &[Location],
657    independent: bool,
658) -> Result<Option<u32>> {
659    ensure!(!chat_id.is_special(), "Invalid chat id");
660
661    let mut newest_timestamp = 0;
662    let mut newest_location_id = None;
663
664    let stmt_insert = "INSERT INTO locations\
665             (timestamp, from_id, chat_id, latitude, longitude, accuracy, independent) \
666             VALUES (?,?,?,?,?,?,?);";
667
668    for location in locations {
669        let &Location {
670            timestamp,
671            latitude,
672            longitude,
673            accuracy,
674            ..
675        } = location;
676
677        context
678            .sql
679            .call_write(|conn| {
680                let mut stmt_test = conn
681                    .prepare_cached("SELECT id FROM locations WHERE timestamp=? AND from_id=?")?;
682                let mut stmt_insert = conn.prepare_cached(stmt_insert)?;
683
684                let exists = stmt_test.exists((timestamp, contact_id))?;
685
686                if independent || !exists {
687                    stmt_insert.execute((
688                        timestamp,
689                        contact_id,
690                        chat_id,
691                        latitude,
692                        longitude,
693                        accuracy,
694                        independent,
695                    ))?;
696
697                    if timestamp > newest_timestamp {
698                        newest_timestamp = timestamp;
699                        newest_location_id = Some(u32::try_from(conn.last_insert_rowid())?);
700                    }
701                }
702
703                Ok(())
704            })
705            .await?;
706    }
707
708    Ok(newest_location_id)
709}
710
711pub(crate) async fn location_loop(context: &Context, interrupt_receiver: Receiver<()>) {
712    loop {
713        let next_event = match maybe_send_locations(context).await {
714            Err(err) => {
715                warn!(context, "maybe_send_locations failed: {:#}", err);
716                Some(60) // Retry one minute later.
717            }
718            Ok(next_event) => next_event,
719        };
720
721        let duration = if let Some(next_event) = next_event {
722            Duration::from_secs(next_event)
723        } else {
724            Duration::from_secs(86400)
725        };
726
727        info!(
728            context,
729            "Location loop is waiting for {} or interrupt",
730            duration_to_str(duration)
731        );
732        match timeout(duration, interrupt_receiver.recv()).await {
733            Err(_err) => {
734                info!(context, "Location loop timeout.");
735            }
736            Ok(Err(err)) => {
737                warn!(
738                    context,
739                    "Interrupt channel closed, location loop exits now: {err:#}."
740                );
741                return;
742            }
743            Ok(Ok(())) => {
744                info!(context, "Location loop received interrupt.");
745            }
746        }
747    }
748}
749
750/// Returns number of seconds until the next time location streaming for some chat ends
751/// automatically.
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).await;
849            chat::add_info_msg(context, chat_id, &stock_str, now).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}