grr-cli 0.4.0

Google tools from the terminal, at maximum performance: zero-config Gmail, Calendar, Drive, Contacts, Chat and Forms over HTTP/3
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! Gmail API response models deserialized with serde

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Gmail message
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Message {
    pub id: String,
    pub thread_id: String,
    // Gmail omits labelIds on minimal-format messages (e.g. history entries).
    #[serde(default)]
    pub label_ids: Vec<String>,
    pub snippet: Option<String>,
    pub history_id: Option<String>,
    pub internal_date: Option<String>,
    pub payload: Option<MessagePayload>,
    pub size_estimate: Option<u64>,
    pub raw: Option<String>,
}

/// Message payload with headers and body
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MessagePayload {
    pub part_id: Option<String>,
    pub mime_type: String,
    pub filename: Option<String>,
    pub headers: Vec<Header>,
    pub body: MessageBody,
    pub parts: Option<Vec<MessagePayload>>,
}

/// Message body
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MessageBody {
    pub attachment_id: Option<String>,
    pub size: u64,
    pub data: Option<String>,
}

/// Email header
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Header {
    pub name: String,
    pub value: String,
}

/// Thread
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Thread {
    pub id: String,
    pub snippet: Option<String>,
    pub history_id: Option<String>,
    pub messages: Option<Vec<Message>>,
}

/// Label
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Label {
    pub id: String,
    pub name: String,
    pub message_list_visibility: Option<String>,
    pub label_list_visibility: Option<String>,
    pub r#type: Option<String>,
    pub messages_total: Option<u64>,
    pub messages_unread: Option<u64>,
    pub threads_total: Option<u64>,
    pub threads_unread: Option<u64>,
    pub color: Option<LabelColor>,
}

/// Label color
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelColor {
    pub text_color: String,
    pub background_color: String,
}

/// Draft
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Draft {
    pub id: String,
    pub message: Message,
}

/// Attachment
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Attachment {
    pub size: u64,
    pub data: String,
}

/// Send-as alias
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SendAs {
    pub send_as_email: String,
    pub display_name: Option<String>,
    pub reply_to_address: Option<String>,
    pub signature: Option<String>,
    // Gmail omits false booleans instead of sending `false`.
    #[serde(default)]
    pub is_primary: bool,
    #[serde(default)]
    pub is_default: bool,
    #[serde(default)]
    pub treat_as_alias: bool,
    pub verification_status: Option<String>,
}

/// History record
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct History {
    pub id: String,
    pub messages: Option<Vec<Message>>,
    pub messages_added: Option<Vec<HistoryMessageAdded>>,
    pub messages_deleted: Option<Vec<HistoryMessageDeleted>>,
    pub labels_added: Option<Vec<HistoryLabelAdded>>,
    pub labels_removed: Option<Vec<HistoryLabelRemoved>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HistoryMessageAdded {
    pub message: Message,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HistoryMessageDeleted {
    pub message: Message,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HistoryLabelAdded {
    pub message: Message,
    #[serde(default)]
    pub label_ids: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HistoryLabelRemoved {
    pub message: Message,
    #[serde(default)]
    pub label_ids: Vec<String>,
}

/// User profile
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Profile {
    pub email_address: String,
    pub messages_total: u64,
    pub threads_total: u64,
    pub history_id: String,
}

/// Watch response
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WatchResponse {
    pub history_id: String,
    pub expiration: String,
}

/// Search response
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SearchResponse {
    // Gmail omits the key instead of sending `[]` when there are no hits.
    #[serde(default)]
    pub messages: Vec<MessageRef>,
    pub next_page_token: Option<String>,
    pub result_size_estimate: Option<u64>,
}

/// Message reference (lightweight)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MessageRef {
    pub id: String,
    pub thread_id: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Filter {
    #[serde(skip_serializing_if = "String::is_empty")]
    pub id: String,
    #[serde(skip_serializing_if = "FilterCriteria::is_empty")]
    pub criteria: FilterCriteria,
    pub action: FilterAction,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct FilterCriteria {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subject: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub query: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub negated_query: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub has_attachment: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exclude_chats: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub size: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub size_comparison: Option<String>,
}

impl FilterCriteria {
    fn is_empty(&self) -> bool {
        self.from.is_none()
            && self.to.is_none()
            && self.subject.is_none()
            && self.query.is_none()
            && self.negated_query.is_none()
            && self.has_attachment.is_none()
            && self.exclude_chats.is_none()
            && self.size.is_none()
            && self.size_comparison.is_none()
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct FilterAction {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub add_label_ids: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub remove_label_ids: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub forward: Option<String>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct FilterList {
    #[serde(default)]
    pub filter: Vec<Filter>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_page_token: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result_size_estimate: Option<u64>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct ForwardingAddress {
    #[serde(skip_serializing_if = "String::is_empty")]
    pub forwarding_email: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub verification_status: Option<String>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct ForwardingAddressList {
    #[serde(default)]
    pub forwarding_addresses: Vec<ForwardingAddress>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct AutoForwarding {
    pub enabled: bool,
    #[serde(skip_serializing_if = "String::is_empty")]
    pub email_address: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disposition: Option<String>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct PopSettings {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub access_window: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disposition: Option<String>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct ImapSettings {
    pub enabled: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expunge_behavior: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auto_expunge: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_folder_size: Option<i64>,
}

/// List response wrappers.
///
/// Gmail keys list payloads by resource and omits the key instead of
/// sending an empty array, so each resource gets its own plain serde
/// struct with a defaulted vector — no aliases, no custom impls.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelList {
    #[serde(default)]
    pub labels: Vec<Label>,
    pub next_page_token: Option<String>,
    pub result_size_estimate: Option<u64>,
}

/// List response wrapper.
///
/// Gmail keys list payloads by resource and omits the key instead of
/// sending an empty array, so each resource gets its own plain serde
/// struct with a defaulted vector — no aliases, no custom impls.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DraftList {
    #[serde(default)]
    pub drafts: Vec<Draft>,
    pub next_page_token: Option<String>,
    pub result_size_estimate: Option<u64>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct ThreadList {
    pub threads: Vec<Thread>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_page_token: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result_size_estimate: Option<u64>,
}

/// List response wrapper.
///
/// Gmail keys list payloads by resource and omits the key instead of
/// sending an empty array, so each resource gets its own plain serde
/// struct with a defaulted vector — no aliases, no custom impls.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HistoryList {
    #[serde(default)]
    pub history: Vec<History>,
    pub next_page_token: Option<String>,
}

/// List response wrapper.
///
/// Gmail keys list payloads by resource and omits the key instead of
/// sending an empty array, so each resource gets its own plain serde
/// struct with a defaulted vector — no aliases, no custom impls.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SendAsList {
    #[serde(default)]
    pub send_as: Vec<SendAs>,
}

/// Batch request
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchRequest {
    pub requests: Vec<BatchRequestEntry>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchRequestEntry {
    pub id: String,
    pub method: String,
    pub path: String,
    pub body: Option<serde_json::Value>,
    pub headers: Option<HashMap<String, String>>,
}

/// Batch response
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchResponse {
    pub responses: Vec<BatchResponseEntry>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchResponseEntry {
    pub id: String,
    pub status: u16,
    pub headers: HashMap<String, String>,
    pub body: Option<serde_json::Value>,
}

/// Modify labels request
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ModifyLabelsRequest {
    pub add_label_ids: Vec<String>,
    pub remove_label_ids: Vec<String>,
}

/// Batch modify labels request
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchModifyLabelsRequest {
    pub ids: Vec<String>,
    pub add_label_ids: Vec<String>,
    pub remove_label_ids: Vec<String>,
}

/// Create label request
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateLabelRequest {
    pub name: String,
    pub label_list_visibility: Option<String>,
    pub message_list_visibility: Option<String>,
    pub color: Option<LabelColor>,
}

/// Update label request
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateLabelRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label_list_visibility: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message_list_visibility: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub color: Option<LabelColor>,
}

/// Watch request
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WatchRequest {
    pub topic_name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label_ids: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label_filter_action: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label_filter_behavior: Option<String>,
}

/// Import message request
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ImportMessageRequest {
    pub raw: String,
    pub internal_date_source: Option<String>,
    pub deleted: Option<bool>,
    pub never_spam: Option<bool>,
    pub process_for_calendar: Option<bool>,
}

/// Create draft request
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateDraftRequest {
    pub message: DraftMessage,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DraftMessage {
    pub raw: String,
    pub thread_id: Option<String>,
}

/// Send message request
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SendMessageRequest {
    pub raw: String,
    pub thread_id: Option<String>,
}

/// Attachment data for sending emails
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AttachmentData {
    pub filename: String,
    pub content: Vec<u8>,
    pub mime_type: String,
}

/// Options for creating a label
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateLabelOptions {
    pub label_list_visibility: Option<String>,
    pub message_list_visibility: Option<String>,
    pub color: Option<LabelColor>,
}

/// Options for updating a label
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateLabelOptions {
    pub name: Option<String>,
    pub label_list_visibility: Option<String>,
    pub message_list_visibility: Option<String>,
    pub color: Option<LabelColor>,
}

/// Options for creating a send-as alias
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateSendAsOptions {
    pub send_as_email: String,
    pub display_name: Option<String>,
    pub reply_to_address: Option<String>,
    pub signature: Option<String>,
    pub treat_as_alias: Option<bool>,
}

/// Options for updating a send-as alias
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateSendAsOptions {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub display_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reply_to_address: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub signature: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub treat_as_alias: Option<bool>,
}

/// Error response
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ErrorResponse {
    pub error: ErrorDetail,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ErrorDetail {
    pub code: u16,
    pub message: String,
    pub status: String,
    pub details: Option<Vec<serde_json::Value>>,
}

/// Helper to extract header value
impl MessagePayload {
    pub fn header(&self, name: &str) -> Option<&str> {
        self.headers
            .iter()
            .find(|h| h.name.eq_ignore_ascii_case(name))
            .map(|h| h.value.as_str())
    }

    pub fn subject(&self) -> Option<&str> {
        self.header("Subject")
    }

    pub fn from(&self) -> Option<&str> {
        self.header("From")
    }

    pub fn to(&self) -> Option<&str> {
        self.header("To")
    }

    pub fn date(&self) -> Option<&str> {
        self.header("Date")
    }

    pub fn message_id(&self) -> Option<&str> {
        self.header("Message-ID")
    }

    pub fn in_reply_to(&self) -> Option<&str> {
        self.header("In-Reply-To")
    }

    pub fn references(&self) -> Option<&str> {
        self.header("References")
    }
}

/// Extract the readable message body: first `text/plain` part found in a
/// DFS walk, falling back to `text/html` (returned as-is; agents strip
/// tags downstream). Attachment bodies are skipped.
pub fn extract_body(msg: &Message) -> Option<String> {
    let payload = msg.payload.as_ref()?;
    let plain = find_part_body(payload, "text/plain");
    if let Some(text) = plain {
        return Some(text);
    }
    find_part_body(payload, "text/html")
}

fn find_part_body(part: &MessagePayload, mime: &str) -> Option<String> {
    if part.mime_type == mime
        && let Some(encoded) = part.body.data.as_deref().filter(|d| !d.is_empty())
    {
        // Ignore decode failures: a corrupted part must not hide
        // readable parts deeper in the tree.
        if let Some(decoded) = decode_body_part(encoded) {
            return Some(decoded);
        }
    }
    let parts = part.parts.as_ref()?;
    for child in parts {
        if let Some(text) = find_part_body(child, mime) {
            return Some(text);
        }
    }
    None
}

fn decode_body_part(encoded: &str) -> Option<String> {
    use base64::Engine;
    // Gmail documents bodies as unpadded base64url, but parts also arrive
    // in the standard alphabet (with `/` and `+`). Normalize the alphabet
    // and re-pad before one canonical decode.
    let mut buf = encoded.replace('-', "+").replace('_', "/");
    while !buf.len().is_multiple_of(4) {
        buf.push('=');
    }
    let bytes = base64::engine::general_purpose::STANDARD.decode(buf).ok()?;
    Some(String::from_utf8_lossy(&bytes).into_owned())
}