imessage_database/message_types/
app.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*!
  App messages are messages that developers can generate with their apps.
  Some built-in functionality also uses App Messages, like Apple Pay or Handwriting.
*/

use std::collections::HashMap;

use plist::Value;

use crate::{
    error::plist::PlistParseError,
    message_types::variants::BalloonProvider,
    util::plist::{get_string_from_dict, get_string_from_nested_dict},
};

/// This struct represents Apple's [`MSMessageTemplateLayout`](https://developer.apple.com/documentation/messages/msmessagetemplatelayout).
#[derive(Debug, PartialEq, Eq)]
pub struct AppMessage<'a> {
    /// An image used to represent the message in the transcript
    pub image: Option<&'a str>,
    /// A URL pointing to a media file used to represent the message in the transcript
    pub url: Option<&'a str>,
    /// The title for the image or media file
    pub title: Option<&'a str>,
    /// The subtitle for the image or media file
    pub subtitle: Option<&'a str>,
    /// A left-aligned caption for the message bubble
    pub caption: Option<&'a str>,
    /// A left-aligned subcaption for the message bubble
    pub subcaption: Option<&'a str>,
    /// A right-aligned caption for the message bubble
    pub trailing_caption: Option<&'a str>,
    /// A right-aligned subcaption for the message bubble
    pub trailing_subcaption: Option<&'a str>,
    /// The name of the app that created this message
    pub app_name: Option<&'a str>,
    /// This property is set only for Apple system messages,
    /// it represents the text that displays in the center of the bubble
    pub ldtext: Option<&'a str>,
}

impl<'a> BalloonProvider<'a> for AppMessage<'a> {
    fn from_map(payload: &'a Value) -> Result<Self, PlistParseError> {
        let user_info = payload
            .as_dictionary()
            .ok_or_else(|| {
                PlistParseError::InvalidType("root".to_string(), "dictionary".to_string())
            })?
            .get("userInfo")
            .ok_or_else(|| PlistParseError::MissingKey("userInfo".to_string()))?;
        Ok(AppMessage {
            image: get_string_from_dict(payload, "image"),
            url: get_string_from_nested_dict(payload, "URL"),
            title: get_string_from_dict(user_info, "image-title"),
            subtitle: get_string_from_dict(user_info, "image-subtitle"),
            caption: get_string_from_dict(user_info, "caption"),
            subcaption: get_string_from_dict(user_info, "subcaption"),
            trailing_caption: get_string_from_dict(user_info, "secondary-subcaption"),
            trailing_subcaption: get_string_from_dict(user_info, "tertiary-subcaption"),
            app_name: get_string_from_dict(payload, "an"),
            ldtext: get_string_from_dict(payload, "ldtext"),
        })
    }
}

impl<'a> AppMessage<'a> {
    /// Parse key/value pairs from the query string in the balloon's a URL
    pub fn parse_query_string(&self) -> HashMap<&str, &str> {
        let mut map = HashMap::new();

        if let Some(url) = self.url {
            if url.starts_with('?') {
                let parts = url.strip_prefix('?').unwrap_or(url).split('&');
                for part in parts {
                    let key_val_split: Vec<&str> = part.split('=').collect();
                    if key_val_split.len() == 2 {
                        map.insert(key_val_split[0], key_val_split[1]);
                    }
                }
            }
        }

        map
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        message_types::{app::AppMessage, variants::BalloonProvider},
        util::plist::parse_plist,
    };
    use plist::Value;
    use std::fs::File;
    use std::{collections::HashMap, env::current_dir};

    #[test]
    fn test_parse_apple_pay_sent_265() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/app_message/Sent265.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = AppMessage::from_map(&parsed).unwrap();
        let expected = AppMessage {
            image: None,
            url: Some("data:application/vnd.apple.pkppm;base64,FAKE_BASE64_DATA="),
            title: None,
            subtitle: None,
            caption: Some("Apple\u{a0}Cash"),
            subcaption: Some("$265\u{a0}Payment"),
            trailing_caption: None,
            trailing_subcaption: None,
            app_name: Some("Apple\u{a0}Pay"),
            ldtext: Some("Sent $265 with Apple\u{a0}Pay."),
        };

        assert_eq!(balloon, expected);
    }

    #[test]
    fn test_parse_apple_pay_recurring_1() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/app_message/ApplePayRecurring.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = AppMessage::from_map(&parsed).unwrap();
        let expected = AppMessage {
            image: None,
            url: Some("data:application/vnd.apple.pkppm;base64,FAKEDATA"),
            title: None,
            subtitle: None,
            caption: None,
            subcaption: None,
            trailing_caption: None,
            trailing_subcaption: None,
            app_name: Some("Apple\u{a0}Cash"),
            ldtext: Some("Sending you $1 weekly starting Nov 18, 2023"),
        };

        assert_eq!(balloon, expected);
    }

    #[test]
    fn test_parse_opentable_invite() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/app_message/OpenTableInvited.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = AppMessage::from_map(&parsed).unwrap();
        let expected = AppMessage {
            image: None,
            url: Some("https://www.opentable.com/book/view?rid=0000000&confnumber=00000&invitationId=1234567890-abcd-def-ghij-4u5t1sv3ryc00l"),
            title: Some("Rusty Grill - Boise"),
            subtitle: Some("Reservation Confirmed"),
            caption: Some("Table for 4 people\nSunday, October 17 at 7:45 PM"),
            subcaption: Some("You're invited! Tap to accept."),
            trailing_caption: None,
            trailing_subcaption: None,
            app_name: Some("OpenTable"),
            ldtext: None,
        };

        assert_eq!(balloon, expected);
    }

    #[test]
    fn test_parse_slideshow() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/app_message/Slideshow.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = AppMessage::from_map(&parsed).unwrap();
        let expected = AppMessage {
            image: None,
            url: Some("https://share.icloud.com/photos/1337h4x0r_jk#Home"),
            title: None,
            subtitle: None,
            caption: Some("Home"),
            subcaption: Some("37 Photos"),
            trailing_caption: None,
            trailing_subcaption: None,
            app_name: Some("Photos"),
            ldtext: Some("Home - 37 Photos"),
        };

        assert_eq!(balloon, expected);
    }

    #[test]
    fn test_parse_game() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/app_message/Game.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = AppMessage::from_map(&parsed).unwrap();
        let expected = AppMessage {
            image: None,
            url: Some("data:?ver=48&data=pr3t3ndth3r3154b10b0fd4t4h3re=3"),
            title: None,
            subtitle: None,
            caption: Some("Your move."),
            subcaption: None,
            trailing_caption: None,
            trailing_subcaption: None,
            app_name: Some("GamePigeon"),
            ldtext: Some("Dots & Boxes"),
        };

        assert_eq!(balloon, expected);
    }

    #[test]
    fn test_parse_business() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/app_message/Business.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = AppMessage::from_map(&parsed).unwrap();
        let expected = AppMessage {
            image: None,
            url: Some("?receivedMessage=33c309ab520bc2c76e99c493157ed578&replyMessage=6a991da615f2e75d4aa0de334e529024"),
            title: None,
            subtitle: None,
            caption: Some("Yes, connect me with Goldman Sachs."),
            subcaption: None,
            trailing_caption: None,
            trailing_subcaption: None,
            app_name: Some("Business"),
            ldtext: Some("Yes, connect me with Goldman Sachs."),
        };

        assert_eq!(balloon, expected);
    }

    #[test]
    fn test_parse_business_query_string() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/app_message/Business.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = AppMessage::from_map(&parsed).unwrap();
        let mut expected = HashMap::new();
        expected.insert("receivedMessage", "33c309ab520bc2c76e99c493157ed578");
        expected.insert("replyMessage", "6a991da615f2e75d4aa0de334e529024");

        assert_eq!(balloon.parse_query_string(), expected);
    }

    #[test]
    fn test_parse_check_in_timer() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/app_message/CheckinTimer.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = AppMessage::from_map(&parsed).unwrap();

        let expected = AppMessage {
            image: None,
            url: Some("?messageType=1&interfaceVersion=1&sendDate=1697316869.688709"),
            title: None,
            subtitle: None,
            caption: Some("Check In: Timer Started"),
            subcaption: None,
            trailing_caption: None,
            trailing_subcaption: None,
            app_name: Some("Check In"),
            ldtext: Some("Check In: Timer Started"),
        };

        assert_eq!(balloon, expected);
    }

    #[test]
    fn test_parse_check_in_timer_late() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/app_message/CheckinLate.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = AppMessage::from_map(&parsed).unwrap();

        let expected = AppMessage {
            image: None,
            url: Some("?messageType=1&interfaceVersion=1&sendDate=1697316869.688709"),
            title: None,
            subtitle: None,
            caption: Some("Check In: Has not checked in when expected, location shared"),
            subcaption: None,
            trailing_caption: None,
            trailing_subcaption: None,
            app_name: Some("Check In"),
            ldtext: Some("Check In: Has not checked in when expected, location shared"),
        };

        assert_eq!(balloon, expected);
    }

    #[test]
    fn test_parse_check_in_location() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/app_message/CheckinLocation.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = AppMessage::from_map(&parsed).unwrap();

        let expected = AppMessage {
            image: None,
            url: Some("?messageType=1&interfaceVersion=1&sendDate=1697316869.688709"),
            title: None,
            subtitle: None,
            caption: Some("Check In: Fake Location"),
            subcaption: None,
            trailing_caption: None,
            trailing_subcaption: None,
            app_name: Some("Check In"),
            ldtext: Some("Check In: Fake Location"),
        };

        assert_eq!(balloon, expected);
    }

    #[test]
    fn test_parse_check_in_query_string() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/app_message/CheckinTimer.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = AppMessage::from_map(&parsed).unwrap();
        let mut expected = HashMap::new();
        expected.insert("messageType", "1");
        expected.insert("interfaceVersion", "1");
        expected.insert("sendDate", "1697316869.688709");

        assert_eq!(balloon.parse_query_string(), expected);
    }

    #[test]
    fn test_parse_find_my() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/app_message/FindMy.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = AppMessage::from_map(&parsed).unwrap();
        let expected = AppMessage {
            image: None,
            url: Some(
                "?FindMyMessagePayloadVersionKey=v0&FindMyMessagePayloadZippedDataKey=FAKEDATA",
            ),
            title: None,
            subtitle: None,
            caption: None,
            subcaption: None,
            trailing_caption: None,
            trailing_subcaption: None,
            app_name: Some("Find My"),
            ldtext: Some("Started Sharing Location"),
        };

        assert_eq!(balloon, expected);
    }
}