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.
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).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| {
352                let id: i32 = row.get(0)?;
353                Ok(id)
354            },
355        )
356        .await?;
357
358    let mut stored_location = false;
359    for chat_id in chats {
360        context.sql.execute(
361                "INSERT INTO locations  \
362                 (latitude, longitude, accuracy, timestamp, chat_id, from_id) VALUES (?,?,?,?,?,?);",
363                 (
364                    latitude,
365                    longitude,
366                    accuracy,
367                    now,
368                    chat_id,
369                    ContactId::SELF,
370                )).await.context("Failed to store location")?;
371        stored_location = true;
372
373        info!(context, "Stored location for chat {chat_id}.");
374        continue_streaming = true;
375    }
376    if continue_streaming {
377        context.emit_location_changed(Some(ContactId::SELF)).await?;
378    };
379    if stored_location {
380        // Interrupt location loop so it may send a location-only message.
381        context.scheduler.interrupt_location().await;
382    }
383
384    Ok(continue_streaming)
385}
386
387/// Searches for locations in the given time range, optionally filtering by chat and contact IDs.
388pub async fn get_range(
389    context: &Context,
390    chat_id: Option<ChatId>,
391    contact_id: Option<u32>,
392    timestamp_from: i64,
393    mut timestamp_to: i64,
394) -> Result<Vec<Location>> {
395    if timestamp_to == 0 {
396        timestamp_to = time() + 10;
397    }
398
399    let (disable_chat_id, chat_id) = match chat_id {
400        Some(chat_id) => (0, chat_id),
401        None => (1, ChatId::new(0)), // this ChatId is unused
402    };
403    let (disable_contact_id, contact_id) = match contact_id {
404        Some(contact_id) => (0, contact_id),
405        None => (1, 0), // this contact_id is unused
406    };
407    let list = context
408        .sql
409        .query_map_vec(
410            "SELECT l.id, l.latitude, l.longitude, l.accuracy, l.timestamp, l.independent, \
411             COALESCE(m.id, 0) AS msg_id, l.from_id, l.chat_id, COALESCE(m.txt, '') AS txt \
412             FROM locations l  LEFT JOIN msgs m ON l.id=m.location_id  WHERE (? OR l.chat_id=?) \
413             AND (? OR l.from_id=?) \
414             AND (l.independent=1 OR (l.timestamp>=? AND l.timestamp<=?)) \
415             ORDER BY l.timestamp DESC, l.id DESC, msg_id DESC;",
416            (
417                disable_chat_id,
418                chat_id,
419                disable_contact_id,
420                contact_id as i32,
421                timestamp_from,
422                timestamp_to,
423            ),
424            |row| {
425                let msg_id = row.get(6)?;
426                let txt: String = row.get(9)?;
427                let marker = if msg_id != 0 && is_marker(&txt) {
428                    Some(txt)
429                } else {
430                    None
431                };
432                let loc = Location {
433                    location_id: row.get(0)?,
434                    latitude: row.get(1)?,
435                    longitude: row.get(2)?,
436                    accuracy: row.get(3)?,
437                    timestamp: row.get(4)?,
438                    independent: row.get(5)?,
439                    msg_id,
440                    contact_id: row.get(7)?,
441                    chat_id: row.get(8)?,
442                    marker,
443                };
444                Ok(loc)
445            },
446        )
447        .await?;
448    Ok(list)
449}
450
451fn is_marker(txt: &str) -> bool {
452    let mut chars = txt.chars();
453    if let Some(c) = chars.next() {
454        !c.is_whitespace() && chars.next().is_none()
455    } else {
456        false
457    }
458}
459
460/// Deletes all locations from the database.
461pub async fn delete_all(context: &Context) -> Result<()> {
462    context.sql.execute("DELETE FROM locations;", ()).await?;
463    context.emit_location_changed(None).await?;
464    Ok(())
465}
466
467/// Deletes expired locations.
468///
469/// Only path locations are deleted.
470/// POIs should be deleted when corresponding message is deleted.
471pub(crate) async fn delete_expired(context: &Context, now: i64) -> Result<()> {
472    let Some(delete_device_after) = context.get_config_delete_device_after().await? else {
473        return Ok(());
474    };
475
476    let threshold_timestamp = now.saturating_sub(delete_device_after);
477    let deleted = context
478        .sql
479        .execute(
480            "DELETE FROM locations WHERE independent=0 AND timestamp < ?",
481            (threshold_timestamp,),
482        )
483        .await?
484        > 0;
485    if deleted {
486        info!(context, "Deleted {deleted} expired locations.");
487        context.emit_location_changed(None).await?;
488    }
489    Ok(())
490}
491
492/// Deletes location if it is an independent location.
493///
494/// This function is used when a message is deleted
495/// that has a corresponding `location_id`.
496pub(crate) async fn delete_poi_location(context: &Context, location_id: u32) -> Result<()> {
497    context
498        .sql
499        .execute(
500            "DELETE FROM locations WHERE independent = 1 AND id=?",
501            (location_id as i32,),
502        )
503        .await?;
504    Ok(())
505}
506
507/// Deletes POI locations that don't have corresponding message anymore.
508pub(crate) async fn delete_orphaned_poi_locations(context: &Context) -> Result<()> {
509    context.sql.execute("
510    DELETE FROM locations
511    WHERE independent=1 AND id NOT IN
512    (SELECT location_id from MSGS LEFT JOIN locations
513     ON locations.id=location_id
514     WHERE location_id>0 -- This check makes the query faster by not looking for locations with ID 0 that don't exist.
515     AND msgs.chat_id != ?)", (DC_CHAT_ID_TRASH,)).await?;
516    Ok(())
517}
518
519/// Returns `location.kml` contents.
520pub async fn get_kml(context: &Context, chat_id: ChatId) -> Result<Option<(String, u32)>> {
521    let mut last_added_location_id = 0;
522
523    let self_addr = context.get_primary_self_addr().await?;
524
525    let (locations_send_begin, locations_send_until, locations_last_sent) = context.sql.query_row(
526        "SELECT locations_send_begin, locations_send_until, locations_last_sent  FROM chats  WHERE id=?;",
527        (chat_id,), |row| {
528            let send_begin: i64 = row.get(0)?;
529            let send_until: i64 = row.get(1)?;
530            let last_sent: i64 = row.get(2)?;
531
532            Ok((send_begin, send_until, last_sent))
533        })
534        .await?;
535
536    let now = time();
537    let mut location_count = 0;
538    let mut ret = String::new();
539    if locations_send_begin != 0 && now <= locations_send_until {
540        ret += &format!(
541            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
542            <kml xmlns=\"http://www.opengis.net/kml/2.2\">\n<Document addr=\"{self_addr}\">\n",
543        );
544
545        context
546            .sql
547            .query_map(
548                "SELECT id, latitude, longitude, accuracy, timestamp \
549             FROM locations  WHERE from_id=? \
550             AND timestamp>=? \
551             AND (timestamp>=? OR \
552                  timestamp=(SELECT MAX(timestamp) FROM locations WHERE from_id=?)) \
553             AND independent=0 \
554             GROUP BY timestamp \
555             ORDER BY timestamp;",
556             (
557                    ContactId::SELF,
558                    locations_send_begin,
559                    locations_last_sent,
560                    ContactId::SELF
561                ),
562                |row| {
563                    let location_id: i32 = row.get(0)?;
564                    let latitude: f64 = row.get(1)?;
565                    let longitude: f64 = row.get(2)?;
566                    let accuracy: f64 = row.get(3)?;
567                    let timestamp = get_kml_timestamp(row.get(4)?);
568
569                    Ok((location_id, latitude, longitude, accuracy, timestamp))
570                },
571                |rows| {
572                    for row in rows {
573                        let (location_id, latitude, longitude, accuracy, timestamp) = row?;
574                        ret += &format!(
575                            "<Placemark>\
576                <Timestamp><when>{timestamp}</when></Timestamp>\
577                <Point><coordinates accuracy=\"{accuracy}\">{longitude},{latitude}</coordinates></Point>\
578                </Placemark>\n"
579                        );
580                        location_count += 1;
581                        last_added_location_id = location_id as u32;
582                    }
583                    Ok(())
584                },
585            )
586            .await?;
587        ret += "</Document>\n</kml>";
588    }
589
590    if location_count > 0 {
591        Ok(Some((ret, last_added_location_id)))
592    } else {
593        Ok(None)
594    }
595}
596
597fn get_kml_timestamp(utc: i64) -> String {
598    // Returns a string formatted as YYYY-MM-DDTHH:MM:SSZ. The trailing `Z` indicates UTC.
599    chrono::DateTime::<chrono::Utc>::from_timestamp(utc, 0)
600        .unwrap()
601        .format("%Y-%m-%dT%H:%M:%SZ")
602        .to_string()
603}
604
605/// Returns a KML document containing a single location with the given timestamp and coordinates.
606pub fn get_message_kml(timestamp: i64, latitude: f64, longitude: f64) -> String {
607    format!(
608        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
609         <kml xmlns=\"http://www.opengis.net/kml/2.2\">\n\
610         <Document>\n\
611         <Placemark>\
612         <Timestamp><when>{}</when></Timestamp>\
613         <Point><coordinates>{},{}</coordinates></Point>\
614         </Placemark>\n\
615         </Document>\n\
616         </kml>",
617        get_kml_timestamp(timestamp),
618        longitude,
619        latitude,
620    )
621}
622
623/// Sets the timestamp of the last time location was sent in the chat.
624pub async fn set_kml_sent_timestamp(
625    context: &Context,
626    chat_id: ChatId,
627    timestamp: i64,
628) -> Result<()> {
629    context
630        .sql
631        .execute(
632            "UPDATE chats SET locations_last_sent=? WHERE id=?;",
633            (timestamp, chat_id),
634        )
635        .await?;
636    Ok(())
637}
638
639/// Sets the location of the message.
640pub async fn set_msg_location_id(context: &Context, msg_id: MsgId, location_id: u32) -> Result<()> {
641    context
642        .sql
643        .execute(
644            "UPDATE msgs SET location_id=? WHERE id=?;",
645            (location_id, msg_id),
646        )
647        .await?;
648
649    Ok(())
650}
651
652/// Saves given locations to the database.
653///
654/// Returns the database row ID of the location with the highest timestamp.
655pub(crate) async fn save(
656    context: &Context,
657    chat_id: ChatId,
658    contact_id: ContactId,
659    locations: &[Location],
660    independent: bool,
661) -> Result<Option<u32>> {
662    ensure!(!chat_id.is_special(), "Invalid chat id");
663
664    let mut newest_timestamp = 0;
665    let mut newest_location_id = None;
666
667    let stmt_insert = "INSERT INTO locations\
668             (timestamp, from_id, chat_id, latitude, longitude, accuracy, independent) \
669             VALUES (?,?,?,?,?,?,?);";
670
671    for location in locations {
672        let &Location {
673            timestamp,
674            latitude,
675            longitude,
676            accuracy,
677            ..
678        } = location;
679
680        context
681            .sql
682            .call_write(|conn| {
683                let mut stmt_test = conn
684                    .prepare_cached("SELECT id FROM locations WHERE timestamp=? AND from_id=?")?;
685                let mut stmt_insert = conn.prepare_cached(stmt_insert)?;
686
687                let exists = stmt_test.exists((timestamp, contact_id))?;
688
689                if independent || !exists {
690                    stmt_insert.execute((
691                        timestamp,
692                        contact_id,
693                        chat_id,
694                        latitude,
695                        longitude,
696                        accuracy,
697                        independent,
698                    ))?;
699
700                    if timestamp > newest_timestamp {
701                        newest_timestamp = timestamp;
702                        newest_location_id = Some(u32::try_from(conn.last_insert_rowid())?);
703                    }
704                }
705
706                Ok(())
707            })
708            .await?;
709    }
710
711    Ok(newest_location_id)
712}
713
714pub(crate) async fn location_loop(context: &Context, interrupt_receiver: Receiver<()>) {
715    loop {
716        let next_event = match maybe_send_locations(context).await {
717            Err(err) => {
718                warn!(context, "maybe_send_locations failed: {:#}", err);
719                Some(60) // Retry one minute later.
720            }
721            Ok(next_event) => next_event,
722        };
723
724        let duration = if let Some(next_event) = next_event {
725            Duration::from_secs(next_event)
726        } else {
727            Duration::from_secs(86400)
728        };
729
730        info!(
731            context,
732            "Location loop is waiting for {} or interrupt",
733            duration_to_str(duration)
734        );
735        match timeout(duration, interrupt_receiver.recv()).await {
736            Err(_err) => {
737                info!(context, "Location loop timeout.");
738            }
739            Ok(Err(err)) => {
740                warn!(
741                    context,
742                    "Interrupt channel closed, location loop exits now: {err:#}."
743                );
744                return;
745            }
746            Ok(Ok(())) => {
747                info!(context, "Location loop received interrupt.");
748            }
749        }
750    }
751}
752
753/// Returns number of seconds until the next time location streaming for some chat ends
754/// automatically.
755async fn maybe_send_locations(context: &Context) -> Result<Option<u64>> {
756    let mut next_event: Option<u64> = None;
757
758    let now = time();
759    let rows = context
760        .sql
761        .query_map_vec(
762            "SELECT id, locations_send_begin, locations_send_until, locations_last_sent
763             FROM chats
764             WHERE locations_send_until>0",
765            [],
766            |row| {
767                let chat_id: ChatId = row.get(0)?;
768                let locations_send_begin: i64 = row.get(1)?;
769                let locations_send_until: i64 = row.get(2)?;
770                let locations_last_sent: i64 = row.get(3)?;
771                Ok((
772                    chat_id,
773                    locations_send_begin,
774                    locations_send_until,
775                    locations_last_sent,
776                ))
777            },
778        )
779        .await
780        .context("failed to query location streaming chats")?;
781
782    for (chat_id, locations_send_begin, locations_send_until, locations_last_sent) in rows {
783        if locations_send_begin > 0 && locations_send_until > now {
784            let can_send = now > locations_last_sent + 60;
785            let has_locations = context
786                .sql
787                .exists(
788                    "SELECT COUNT(id) \
789     FROM locations \
790     WHERE from_id=? \
791     AND timestamp>=? \
792     AND timestamp>? \
793     AND independent=0",
794                    (ContactId::SELF, locations_send_begin, locations_last_sent),
795                )
796                .await?;
797
798            next_event = next_event
799                .into_iter()
800                .chain(u64::try_from(locations_send_until - now))
801                .min();
802
803            if has_locations {
804                if can_send {
805                    // Send location-only message.
806                    // Pending locations are attached automatically to every message,
807                    // so also to this empty text message.
808                    info!(
809                        context,
810                        "Chat {} has pending locations, sending them.", chat_id
811                    );
812                    let mut msg = Message::new(Viewtype::Text);
813                    msg.hidden = true;
814                    msg.param.set_cmd(SystemMessage::LocationOnly);
815                    chat::send_msg(context, chat_id, &mut msg).await?;
816                } else {
817                    // Wait until pending locations can be sent.
818                    info!(
819                        context,
820                        "Chat {} has pending locations, but they can't be sent yet.", chat_id
821                    );
822                    next_event = next_event
823                        .into_iter()
824                        .chain(u64::try_from(locations_last_sent + 61 - now))
825                        .min();
826                }
827            } else {
828                info!(
829                    context,
830                    "Chat {} has location streaming enabled, but no pending locations.", chat_id
831                );
832            }
833        } else {
834            // Location streaming was either explicitly disabled (locations_send_begin = 0) or
835            // locations_send_until is in the past.
836            info!(
837                context,
838                "Disabling location streaming for chat {}.", chat_id
839            );
840            context
841                .sql
842                .execute(
843                    "UPDATE chats \
844                         SET locations_send_begin=0, locations_send_until=0 \
845                         WHERE id=?",
846                    (chat_id,),
847                )
848                .await
849                .context("failed to disable location streaming")?;
850
851            let stock_str = stock_str::msg_location_disabled(context).await;
852            chat::add_info_msg(context, chat_id, &stock_str).await?;
853            context.emit_event(EventType::ChatModified(chat_id));
854            chatlist_events::emit_chatlist_item_changed(context, chat_id);
855        }
856    }
857
858    Ok(next_event)
859}
860
861#[cfg(test)]
862mod tests {
863    use super::*;
864    use crate::config::Config;
865    use crate::message::MessageState;
866    use crate::receive_imf::receive_imf;
867    use crate::test_utils::{TestContext, TestContextManager};
868    use crate::tools::SystemTime;
869
870    #[test]
871    fn test_kml_parse() {
872        let xml =
873            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>";
874
875        let kml = Kml::parse(xml).expect("parsing failed");
876
877        assert!(kml.addr.is_some());
878        assert_eq!(kml.addr.as_ref().unwrap(), "user@example.org",);
879
880        let locations_ref = &kml.locations;
881        assert_eq!(locations_ref.len(), 2);
882
883        assert!(locations_ref[0].latitude > 53.6f64);
884        assert!(locations_ref[0].latitude < 53.8f64);
885        assert!(locations_ref[0].longitude > 9.3f64);
886        assert!(locations_ref[0].longitude < 9.5f64);
887        assert!(locations_ref[0].accuracy > 31.9f64);
888        assert!(locations_ref[0].accuracy < 32.1f64);
889        assert_eq!(locations_ref[0].timestamp, 1551906597);
890
891        assert!(locations_ref[1].latitude > 63.6f64);
892        assert!(locations_ref[1].latitude < 63.8f64);
893        assert!(locations_ref[1].longitude > 19.3f64);
894        assert!(locations_ref[1].longitude < 19.5f64);
895        assert!(locations_ref[1].accuracy > 2.4f64);
896        assert!(locations_ref[1].accuracy < 2.6f64);
897        assert_eq!(locations_ref[1].timestamp, 1544739072);
898    }
899
900    #[test]
901    fn test_kml_parse_error() {
902        let xml = b"<?><xmlversi\"\"\">?</document>";
903        assert!(Kml::parse(xml).is_err());
904    }
905
906    #[test]
907    fn test_get_message_kml() {
908        let timestamp = 1598490000;
909
910        let xml = get_message_kml(timestamp, 51.423723f64, 8.552556f64);
911        let kml = Kml::parse(xml.as_bytes()).expect("parsing failed");
912        let locations_ref = &kml.locations;
913        assert_eq!(locations_ref.len(), 1);
914
915        assert!(locations_ref[0].latitude >= 51.423723f64);
916        assert!(locations_ref[0].latitude < 51.423724f64);
917        assert!(locations_ref[0].longitude >= 8.552556f64);
918        assert!(locations_ref[0].longitude < 8.552557f64);
919        assert!(locations_ref[0].accuracy.abs() < f64::EPSILON);
920        assert_eq!(locations_ref[0].timestamp, timestamp);
921    }
922
923    #[test]
924    fn test_is_marker() {
925        assert!(is_marker("f"));
926        assert!(!is_marker("foo"));
927        assert!(is_marker("🏠"));
928        assert!(!is_marker(" "));
929        assert!(!is_marker("\t"));
930    }
931
932    /// Tests that location.kml is hidden.
933    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
934    async fn receive_location_kml() -> Result<()> {
935        let alice = TestContext::new_alice().await;
936
937        receive_imf(
938            &alice,
939            br#"Subject: Hello
940Message-ID: hello@example.net
941To: Alice <alice@example.org>
942From: Bob <bob@example.net>
943Date: Mon, 20 Dec 2021 00:00:00 +0000
944Chat-Version: 1.0
945Content-Type: text/plain; charset=utf-8; format=flowed; delsp=no
946
947Text message."#,
948            false,
949        )
950        .await?;
951        let received_msg = alice.get_last_msg().await;
952        assert_eq!(received_msg.text, "Text message.");
953
954        receive_imf(
955            &alice,
956            br#"Subject: locations
957MIME-Version: 1.0
958To: <alice@example.org>
959From: <bob@example.net>
960Date: Tue, 21 Dec 2021 00:00:00 +0000
961Chat-Version: 1.0
962Message-ID: <foobar@example.net>
963Content-Type: multipart/mixed; boundary="U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF"
964
965
966--U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF
967Content-Type: text/plain; charset=utf-8; format=flowed; delsp=no
968
969
970
971--U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF
972Content-Type: application/vnd.google-earth.kml+xml
973Content-Disposition: attachment; filename="location.kml"
974
975<?xml version="1.0" encoding="UTF-8"?>
976<kml xmlns="http://www.opengis.net/kml/2.2">
977<Document addr="bob@example.net">
978<Placemark><Timestamp><when>2021-11-21T00:00:00Z</when></Timestamp><Point><coordinates accuracy="1.0000000000000000">10.00000000000000,20.00000000000000</coordinates></Point></Placemark>
979</Document>
980</kml>
981
982--U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF--"#,
983            false,
984        )
985        .await?;
986
987        // Received location message is not visible, last message stays the same.
988        let received_msg2 = alice.get_last_msg().await;
989        assert_eq!(received_msg2.id, received_msg.id);
990
991        let locations = get_range(&alice, None, None, 0, 0).await?;
992        assert_eq!(locations.len(), 1);
993        Ok(())
994    }
995
996    /// Tests that `location.kml` is not hidden and not seen if it contains a message.
997    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
998    async fn receive_visible_location_kml() -> Result<()> {
999        let alice = TestContext::new_alice().await;
1000
1001        receive_imf(
1002            &alice,
1003            br#"Subject: locations
1004MIME-Version: 1.0
1005To: <alice@example.org>
1006From: <bob@example.net>
1007Date: Tue, 21 Dec 2021 00:00:00 +0000
1008Chat-Version: 1.0
1009Message-ID: <foobar@localhost>
1010Content-Type: multipart/mixed; boundary="U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF"
1011
1012
1013--U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF
1014Content-Type: text/plain; charset=utf-8; format=flowed; delsp=no
1015
1016Text message.
1017
1018
1019--U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF
1020Content-Type: application/vnd.google-earth.kml+xml
1021Content-Disposition: attachment; filename="location.kml"
1022
1023<?xml version="1.0" encoding="UTF-8"?>
1024<kml xmlns="http://www.opengis.net/kml/2.2">
1025<Document addr="bob@example.net">
1026<Placemark><Timestamp><when>2021-11-21T00:00:00Z</when></Timestamp><Point><coordinates accuracy="1.0000000000000000">10.00000000000000,20.00000000000000</coordinates></Point></Placemark>
1027</Document>
1028</kml>
1029
1030--U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF--"#,
1031            false,
1032        )
1033        .await?;
1034
1035        let received_msg = alice.get_last_msg().await;
1036        assert_eq!(received_msg.text, "Text message.");
1037        assert_eq!(received_msg.state, MessageState::InFresh);
1038
1039        let locations = get_range(&alice, None, None, 0, 0).await?;
1040        assert_eq!(locations.len(), 1);
1041        Ok(())
1042    }
1043
1044    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1045    async fn test_send_locations_to_chat() -> Result<()> {
1046        let alice = TestContext::new_alice().await;
1047        let bob = TestContext::new_bob().await;
1048
1049        let alice_chat = alice.create_chat(&bob).await;
1050        send_locations_to_chat(&alice, alice_chat.id, 1000).await?;
1051        let sent = alice.pop_sent_msg().await;
1052        let msg = bob.recv_msg(&sent).await;
1053        assert_eq!(msg.text, "Location streaming enabled by alice@example.org.");
1054        let bob_chat_id = msg.chat_id;
1055
1056        assert_eq!(set(&alice, 10.0, 20.0, 1.0).await?, true);
1057
1058        // Send image without text.
1059        let file_name = "image.png";
1060        let bytes = include_bytes!("../test-data/image/logo.png");
1061        let file = alice.get_blobdir().join(file_name);
1062        tokio::fs::write(&file, bytes).await?;
1063        let mut msg = Message::new(Viewtype::Image);
1064        msg.set_file_and_deduplicate(&alice, &file, Some("logo.png"), None)?;
1065        let sent = alice.send_msg(alice_chat.id, &mut msg).await;
1066        let alice_msg = Message::load_from_db(&alice, sent.sender_msg_id).await?;
1067        assert_eq!(alice_msg.has_location(), false);
1068
1069        let msg = bob.recv_msg_opt(&sent).await.unwrap();
1070        assert!(msg.chat_id == bob_chat_id);
1071        assert_eq!(msg.msg_ids.len(), 1);
1072
1073        let bob_msg = Message::load_from_db(&bob, *msg.msg_ids.first().unwrap()).await?;
1074        assert_eq!(bob_msg.chat_id, bob_chat_id);
1075        assert_eq!(bob_msg.viewtype, Viewtype::Image);
1076        assert_eq!(bob_msg.has_location(), false);
1077
1078        let bob_locations = get_range(&bob, None, None, 0, 0).await?;
1079        assert_eq!(bob_locations.len(), 1);
1080
1081        Ok(())
1082    }
1083
1084    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1085    async fn test_delete_expired_locations() -> Result<()> {
1086        let mut tcm = TestContextManager::new();
1087        let alice = &tcm.alice().await;
1088        let bob = &tcm.bob().await;
1089
1090        // Alice enables deletion of messages from device after 1 week.
1091        alice
1092            .set_config(Config::DeleteDeviceAfter, Some("604800"))
1093            .await?;
1094        // Bob enables deletion of messages from device after 1 day.
1095        bob.set_config(Config::DeleteDeviceAfter, Some("86400"))
1096            .await?;
1097
1098        let alice_chat = alice.create_chat(bob).await;
1099
1100        // Alice enables location streaming.
1101        // Bob receives a message saying that Alice enabled location streaming.
1102        send_locations_to_chat(alice, alice_chat.id, 60).await?;
1103        bob.recv_msg(&alice.pop_sent_msg().await).await;
1104
1105        // Alice gets new location from GPS.
1106        assert_eq!(set(alice, 10.0, 20.0, 1.0).await?, true);
1107        assert_eq!(get_range(alice, None, None, 0, 0).await?.len(), 1);
1108
1109        // 10 seconds later location sending stream manages to send location.
1110        SystemTime::shift(Duration::from_secs(10));
1111        delete_expired(alice, time()).await?;
1112        maybe_send_locations(alice).await?;
1113        bob.recv_msg_opt(&alice.pop_sent_msg().await).await;
1114        assert_eq!(get_range(alice, None, None, 0, 0).await?.len(), 1);
1115        assert_eq!(get_range(bob, None, None, 0, 0).await?.len(), 1);
1116
1117        // Location-only messages are "auto-generated", but they mustn't make the contact a bot.
1118        let contact = bob.add_or_lookup_contact(alice).await;
1119        assert!(!contact.is_bot());
1120
1121        // Day later Bob removes location.
1122        SystemTime::shift(Duration::from_secs(86400));
1123        delete_expired(alice, time()).await?;
1124        delete_expired(bob, time()).await?;
1125        assert_eq!(get_range(alice, None, None, 0, 0).await?.len(), 1);
1126        assert_eq!(get_range(bob, None, None, 0, 0).await?.len(), 0);
1127
1128        // Week late Alice removes location.
1129        SystemTime::shift(Duration::from_secs(604800));
1130        delete_expired(alice, time()).await?;
1131        delete_expired(bob, time()).await?;
1132        assert_eq!(get_range(alice, None, None, 0, 0).await?.len(), 0);
1133        assert_eq!(get_range(bob, None, None, 0, 0).await?.len(), 0);
1134
1135        Ok(())
1136    }
1137}