gws-cli 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
gws/commands/gmail.py ADDED
@@ -0,0 +1,885 @@
1
+ """Gmail CLI commands."""
2
+
3
+ import sys
4
+ import typer
5
+ from typing import Annotated, Optional
6
+
7
+ from gws.commands._account import account_callback
8
+ from gws.exceptions import ExitCode
9
+ from gws.output import output_error
10
+ from gws.services.gmail import GmailService
11
+
12
+ app = typer.Typer(
13
+ name="gmail",
14
+ help="Gmail email operations.",
15
+ no_args_is_help=True,
16
+ callback=account_callback,
17
+ )
18
+
19
+
20
+ @app.command("list")
21
+ def list_messages(
22
+ query: Annotated[
23
+ Optional[str],
24
+ typer.Option("--query", "-q", help="Gmail search query (e.g., 'from:someone')."),
25
+ ] = None,
26
+ max_results: Annotated[
27
+ int,
28
+ typer.Option("--max", "-n", help="Maximum messages to return."),
29
+ ] = 10,
30
+ labels: Annotated[
31
+ Optional[str],
32
+ typer.Option("--labels", "-l", help="Comma-separated label IDs."),
33
+ ] = None,
34
+ ) -> None:
35
+ """List recent messages."""
36
+ label_ids = labels.split(",") if labels else None
37
+
38
+ service = GmailService()
39
+ service.list_messages(
40
+ query=query,
41
+ max_results=max_results,
42
+ label_ids=label_ids,
43
+ )
44
+
45
+
46
+ @app.command("read")
47
+ def read_message(
48
+ message_id: Annotated[str, typer.Argument(help="Message ID to read.")],
49
+ full: Annotated[
50
+ bool,
51
+ typer.Option("--full", "-f", help="Get full message format."),
52
+ ] = True,
53
+ ) -> None:
54
+ """Read a message by ID."""
55
+ format_type = "full" if full else "metadata"
56
+
57
+ service = GmailService()
58
+ service.read_message(message_id=message_id, format_type=format_type)
59
+
60
+
61
+ @app.command("send")
62
+ def send_message(
63
+ to: Annotated[str, typer.Argument(help="Recipient email address.")],
64
+ subject: Annotated[str, typer.Argument(help="Email subject.")],
65
+ body: Annotated[
66
+ Optional[str],
67
+ typer.Argument(help="Email body text."),
68
+ ] = None,
69
+ stdin: Annotated[
70
+ bool,
71
+ typer.Option("--stdin", help="Read body from stdin."),
72
+ ] = False,
73
+ cc: Annotated[
74
+ Optional[str],
75
+ typer.Option("--cc", help="CC recipients."),
76
+ ] = None,
77
+ bcc: Annotated[
78
+ Optional[str],
79
+ typer.Option("--bcc", help="BCC recipients."),
80
+ ] = None,
81
+ plain_text: Annotated[
82
+ bool,
83
+ typer.Option("--plain", help="Send as plain text instead of HTML."),
84
+ ] = False,
85
+ from_name: Annotated[
86
+ Optional[str],
87
+ typer.Option("--from-name", "-n", help="Sender display name."),
88
+ ] = None,
89
+ signature: Annotated[
90
+ Optional[str],
91
+ typer.Option("--signature", "-s", help="Email signature to append."),
92
+ ] = None,
93
+ ) -> None:
94
+ """Send an email message (HTML by default)."""
95
+ if stdin:
96
+ body = sys.stdin.read()
97
+ elif body is None:
98
+ output_error(
99
+ error_code="INVALID_ARGS",
100
+ operation="gmail.send",
101
+ message="Either provide body argument or use --stdin",
102
+ )
103
+ raise typer.Exit(ExitCode.INVALID_ARGS)
104
+
105
+ service = GmailService()
106
+ service.send_message(
107
+ to=to,
108
+ subject=subject,
109
+ body=body,
110
+ cc=cc,
111
+ bcc=bcc,
112
+ html=not plain_text,
113
+ from_name=from_name,
114
+ signature=signature,
115
+ )
116
+
117
+
118
+ @app.command("reply")
119
+ def reply_to_message(
120
+ message_id: Annotated[str, typer.Argument(help="Message ID to reply to.")],
121
+ body: Annotated[
122
+ Optional[str],
123
+ typer.Argument(help="Reply body text."),
124
+ ] = None,
125
+ stdin: Annotated[
126
+ bool,
127
+ typer.Option("--stdin", help="Read body from stdin."),
128
+ ] = False,
129
+ plain_text: Annotated[
130
+ bool,
131
+ typer.Option("--plain", help="Send as plain text instead of HTML."),
132
+ ] = False,
133
+ from_name: Annotated[
134
+ Optional[str],
135
+ typer.Option("--from-name", "-n", help="Sender display name."),
136
+ ] = None,
137
+ signature: Annotated[
138
+ Optional[str],
139
+ typer.Option("--signature", "-s", help="Email signature to append."),
140
+ ] = None,
141
+ ) -> None:
142
+ """Reply to an existing message (HTML by default)."""
143
+ if stdin:
144
+ body = sys.stdin.read()
145
+ elif body is None:
146
+ output_error(
147
+ error_code="INVALID_ARGS",
148
+ operation="gmail.reply",
149
+ message="Either provide body argument or use --stdin",
150
+ )
151
+ raise typer.Exit(ExitCode.INVALID_ARGS)
152
+
153
+ service = GmailService()
154
+ service.reply_to_message(
155
+ message_id=message_id,
156
+ body=body,
157
+ html=not plain_text,
158
+ from_name=from_name,
159
+ signature=signature,
160
+ )
161
+
162
+
163
+ @app.command("search")
164
+ def search_messages(
165
+ query: Annotated[str, typer.Argument(help="Gmail search query.")],
166
+ max_results: Annotated[
167
+ int,
168
+ typer.Option("--max", "-n", help="Maximum messages to return."),
169
+ ] = 10,
170
+ ) -> None:
171
+ """Search messages using Gmail query syntax.
172
+
173
+ Examples:
174
+ from:someone@example.com
175
+ subject:meeting
176
+ is:unread
177
+ has:attachment
178
+ after:2024/01/01
179
+ """
180
+ service = GmailService()
181
+ service.search_messages(query=query, max_results=max_results)
182
+
183
+
184
+ @app.command("delete")
185
+ def delete_message(
186
+ message_id: Annotated[str, typer.Argument(help="Message ID to delete.")],
187
+ permanent: Annotated[
188
+ bool,
189
+ typer.Option("--permanent", "-p", help="Permanently delete (skip trash)."),
190
+ ] = False,
191
+ ) -> None:
192
+ """Delete a message (move to trash by default)."""
193
+ service = GmailService()
194
+ service.delete_message(message_id=message_id, permanent=permanent)
195
+
196
+
197
+ @app.command("mark-read")
198
+ def mark_read(
199
+ message_id: Annotated[str, typer.Argument(help="Message ID to mark as read.")],
200
+ ) -> None:
201
+ """Mark a message as read."""
202
+ service = GmailService()
203
+ service.mark_as_read(message_id=message_id)
204
+
205
+
206
+ @app.command("mark-unread")
207
+ def mark_unread(
208
+ message_id: Annotated[str, typer.Argument(help="Message ID to mark as unread.")],
209
+ ) -> None:
210
+ """Mark a message as unread."""
211
+ service = GmailService()
212
+ service.mark_as_unread(message_id=message_id)
213
+
214
+
215
+ # ===== Label Commands =====
216
+
217
+
218
+ @app.command("labels")
219
+ def list_labels() -> None:
220
+ """List all labels in the mailbox."""
221
+ service = GmailService()
222
+ service.list_labels()
223
+
224
+
225
+ @app.command("create-label")
226
+ def create_label(
227
+ name: Annotated[str, typer.Argument(help="Label name to create.")],
228
+ hide_in_message_list: Annotated[
229
+ bool,
230
+ typer.Option("--hide-in-list", help="Hide label in message list."),
231
+ ] = False,
232
+ visibility: Annotated[
233
+ str,
234
+ typer.Option("--visibility", "-v", help="Label list visibility (labelShow, labelShowIfUnread, labelHide)."),
235
+ ] = "labelShow",
236
+ ) -> None:
237
+ """Create a new label."""
238
+ message_visibility = "hide" if hide_in_message_list else "show"
239
+
240
+ service = GmailService()
241
+ service.create_label(
242
+ name=name,
243
+ message_list_visibility=message_visibility,
244
+ label_list_visibility=visibility,
245
+ )
246
+
247
+
248
+ @app.command("delete-label")
249
+ def delete_label(
250
+ label_id: Annotated[str, typer.Argument(help="Label ID to delete.")],
251
+ ) -> None:
252
+ """Delete a label."""
253
+ service = GmailService()
254
+ service.delete_label(label_id=label_id)
255
+
256
+
257
+ @app.command("add-labels")
258
+ def add_labels(
259
+ message_id: Annotated[str, typer.Argument(help="Message ID.")],
260
+ label_ids: Annotated[str, typer.Argument(help="Comma-separated label IDs to add.")],
261
+ ) -> None:
262
+ """Add labels to a message."""
263
+ labels = [label.strip() for label in label_ids.split(",")]
264
+
265
+ service = GmailService()
266
+ service.add_labels(message_id=message_id, label_ids=labels)
267
+
268
+
269
+ @app.command("remove-labels")
270
+ def remove_labels(
271
+ message_id: Annotated[str, typer.Argument(help="Message ID.")],
272
+ label_ids: Annotated[str, typer.Argument(help="Comma-separated label IDs to remove.")],
273
+ ) -> None:
274
+ """Remove labels from a message."""
275
+ labels = [label.strip() for label in label_ids.split(",")]
276
+
277
+ service = GmailService()
278
+ service.remove_labels(message_id=message_id, label_ids=labels)
279
+
280
+
281
+ # ===== Draft Commands =====
282
+
283
+
284
+ @app.command("drafts")
285
+ def list_drafts(
286
+ max_results: Annotated[
287
+ int,
288
+ typer.Option("--max", "-n", help="Maximum drafts to return."),
289
+ ] = 10,
290
+ ) -> None:
291
+ """List drafts in the mailbox."""
292
+ service = GmailService()
293
+ service.list_drafts(max_results=max_results)
294
+
295
+
296
+ @app.command("get-draft")
297
+ def get_draft(
298
+ draft_id: Annotated[str, typer.Argument(help="Draft ID to read.")],
299
+ ) -> None:
300
+ """Read a draft by ID."""
301
+ service = GmailService()
302
+ service.get_draft(draft_id=draft_id)
303
+
304
+
305
+ @app.command("create-draft")
306
+ def create_draft(
307
+ to: Annotated[str, typer.Argument(help="Recipient email address.")],
308
+ subject: Annotated[str, typer.Argument(help="Email subject.")],
309
+ body: Annotated[
310
+ Optional[str],
311
+ typer.Argument(help="Email body text."),
312
+ ] = None,
313
+ stdin: Annotated[
314
+ bool,
315
+ typer.Option("--stdin", help="Read body from stdin."),
316
+ ] = False,
317
+ cc: Annotated[
318
+ Optional[str],
319
+ typer.Option("--cc", help="CC recipients."),
320
+ ] = None,
321
+ bcc: Annotated[
322
+ Optional[str],
323
+ typer.Option("--bcc", help="BCC recipients."),
324
+ ] = None,
325
+ plain_text: Annotated[
326
+ bool,
327
+ typer.Option("--plain", help="Create as plain text instead of HTML."),
328
+ ] = False,
329
+ ) -> None:
330
+ """Create a new draft (HTML by default)."""
331
+ if stdin:
332
+ body = sys.stdin.read()
333
+ elif body is None:
334
+ output_error(
335
+ error_code="INVALID_ARGS",
336
+ operation="gmail.create_draft",
337
+ message="Either provide body argument or use --stdin",
338
+ )
339
+ raise typer.Exit(ExitCode.INVALID_ARGS)
340
+
341
+ service = GmailService()
342
+ service.create_draft(
343
+ to=to,
344
+ subject=subject,
345
+ body=body,
346
+ cc=cc,
347
+ bcc=bcc,
348
+ html=not plain_text,
349
+ )
350
+
351
+
352
+ @app.command("update-draft")
353
+ def update_draft(
354
+ draft_id: Annotated[str, typer.Argument(help="Draft ID to update.")],
355
+ to: Annotated[
356
+ Optional[str],
357
+ typer.Option("--to", help="New recipient."),
358
+ ] = None,
359
+ subject: Annotated[
360
+ Optional[str],
361
+ typer.Option("--subject", "-s", help="New subject."),
362
+ ] = None,
363
+ body: Annotated[
364
+ Optional[str],
365
+ typer.Option("--body", "-b", help="New body content."),
366
+ ] = None,
367
+ stdin: Annotated[
368
+ bool,
369
+ typer.Option("--stdin", help="Read body from stdin."),
370
+ ] = False,
371
+ cc: Annotated[
372
+ Optional[str],
373
+ typer.Option("--cc", help="New CC recipients."),
374
+ ] = None,
375
+ bcc: Annotated[
376
+ Optional[str],
377
+ typer.Option("--bcc", help="New BCC recipients."),
378
+ ] = None,
379
+ plain_text: Annotated[
380
+ bool,
381
+ typer.Option("--plain", help="Update as plain text instead of HTML."),
382
+ ] = False,
383
+ ) -> None:
384
+ """Update an existing draft."""
385
+ if stdin:
386
+ body = sys.stdin.read()
387
+
388
+ service = GmailService()
389
+ service.update_draft(
390
+ draft_id=draft_id,
391
+ to=to,
392
+ subject=subject,
393
+ body=body,
394
+ cc=cc,
395
+ bcc=bcc,
396
+ html=not plain_text,
397
+ )
398
+
399
+
400
+ @app.command("send-draft")
401
+ def send_draft(
402
+ draft_id: Annotated[str, typer.Argument(help="Draft ID to send.")],
403
+ ) -> None:
404
+ """Send a draft."""
405
+ service = GmailService()
406
+ service.send_draft(draft_id=draft_id)
407
+
408
+
409
+ @app.command("delete-draft")
410
+ def delete_draft(
411
+ draft_id: Annotated[str, typer.Argument(help="Draft ID to delete.")],
412
+ ) -> None:
413
+ """Delete a draft."""
414
+ service = GmailService()
415
+ service.delete_draft(draft_id=draft_id)
416
+
417
+
418
+ # ===== Attachment Commands =====
419
+
420
+
421
+ @app.command("send-with-attachment")
422
+ def send_with_attachment(
423
+ to: Annotated[str, typer.Argument(help="Recipient email address.")],
424
+ subject: Annotated[str, typer.Argument(help="Email subject.")],
425
+ body: Annotated[str, typer.Argument(help="Email body text.")],
426
+ attachments: Annotated[str, typer.Argument(help="Comma-separated file paths to attach.")],
427
+ cc: Annotated[
428
+ Optional[str],
429
+ typer.Option("--cc", help="CC recipients."),
430
+ ] = None,
431
+ bcc: Annotated[
432
+ Optional[str],
433
+ typer.Option("--bcc", help="BCC recipients."),
434
+ ] = None,
435
+ plain_text: Annotated[
436
+ bool,
437
+ typer.Option("--plain", help="Send as plain text instead of HTML."),
438
+ ] = False,
439
+ from_name: Annotated[
440
+ Optional[str],
441
+ typer.Option("--from-name", "-n", help="Sender display name."),
442
+ ] = None,
443
+ ) -> None:
444
+ """Send an email with file attachments (HTML by default)."""
445
+ attachment_list = [p.strip() for p in attachments.split(",")]
446
+
447
+ service = GmailService()
448
+ service.send_with_attachment(
449
+ to=to,
450
+ subject=subject,
451
+ body=body,
452
+ attachment_paths=attachment_list,
453
+ cc=cc,
454
+ bcc=bcc,
455
+ html=not plain_text,
456
+ from_name=from_name,
457
+ )
458
+
459
+
460
+ @app.command("list-attachments")
461
+ def list_attachments(
462
+ message_id: Annotated[str, typer.Argument(help="Message ID.")],
463
+ ) -> None:
464
+ """List attachments in a message."""
465
+ service = GmailService()
466
+ service.list_attachments(message_id=message_id)
467
+
468
+
469
+ @app.command("download-attachment")
470
+ def download_attachment(
471
+ message_id: Annotated[str, typer.Argument(help="Message ID containing the attachment.")],
472
+ attachment_id: Annotated[str, typer.Argument(help="Attachment ID.")],
473
+ output_path: Annotated[str, typer.Argument(help="Path to save the downloaded file.")],
474
+ ) -> None:
475
+ """Download an attachment to a file."""
476
+ service = GmailService()
477
+ service.download_attachment(
478
+ message_id=message_id,
479
+ attachment_id=attachment_id,
480
+ output_path=output_path,
481
+ )
482
+
483
+
484
+ # ===== Thread Commands =====
485
+
486
+
487
+ @app.command("threads")
488
+ def list_threads(
489
+ query: Annotated[
490
+ Optional[str],
491
+ typer.Option("--query", "-q", help="Gmail search query."),
492
+ ] = None,
493
+ max_results: Annotated[
494
+ int,
495
+ typer.Option("--max", "-n", help="Maximum threads to return."),
496
+ ] = 10,
497
+ labels: Annotated[
498
+ Optional[str],
499
+ typer.Option("--labels", "-l", help="Comma-separated label IDs."),
500
+ ] = None,
501
+ ) -> None:
502
+ """List email threads."""
503
+ label_ids = labels.split(",") if labels else None
504
+
505
+ service = GmailService()
506
+ service.list_threads(
507
+ query=query,
508
+ max_results=max_results,
509
+ label_ids=label_ids,
510
+ )
511
+
512
+
513
+ @app.command("get-thread")
514
+ def get_thread(
515
+ thread_id: Annotated[str, typer.Argument(help="Thread ID.")],
516
+ full: Annotated[
517
+ bool,
518
+ typer.Option("--full", "-f", help="Get full message format."),
519
+ ] = False,
520
+ ) -> None:
521
+ """Get a thread with all its messages."""
522
+ format_type = "full" if full else "metadata"
523
+
524
+ service = GmailService()
525
+ service.get_thread(thread_id=thread_id, format_type=format_type)
526
+
527
+
528
+ @app.command("trash-thread")
529
+ def trash_thread(
530
+ thread_id: Annotated[str, typer.Argument(help="Thread ID to trash.")],
531
+ ) -> None:
532
+ """Move a thread to trash."""
533
+ service = GmailService()
534
+ service.trash_thread(thread_id=thread_id)
535
+
536
+
537
+ @app.command("untrash-thread")
538
+ def untrash_thread(
539
+ thread_id: Annotated[str, typer.Argument(help="Thread ID to restore.")],
540
+ ) -> None:
541
+ """Restore a thread from trash."""
542
+ service = GmailService()
543
+ service.untrash_thread(thread_id=thread_id)
544
+
545
+
546
+ @app.command("modify-thread-labels")
547
+ def modify_thread_labels(
548
+ thread_id: Annotated[str, typer.Argument(help="Thread ID.")],
549
+ add_labels: Annotated[
550
+ Optional[str],
551
+ typer.Option("--add", "-a", help="Comma-separated label IDs to add."),
552
+ ] = None,
553
+ remove_labels: Annotated[
554
+ Optional[str],
555
+ typer.Option("--remove", "-r", help="Comma-separated label IDs to remove."),
556
+ ] = None,
557
+ ) -> None:
558
+ """Add or remove labels from all messages in a thread."""
559
+ add_label_ids = [label.strip() for label in add_labels.split(",")] if add_labels else None
560
+ remove_label_ids = [label.strip() for label in remove_labels.split(",")] if remove_labels else None
561
+
562
+ service = GmailService()
563
+ service.modify_thread_labels(
564
+ thread_id=thread_id,
565
+ add_label_ids=add_label_ids,
566
+ remove_label_ids=remove_label_ids,
567
+ )
568
+
569
+
570
+ # ===== Settings Commands =====
571
+
572
+
573
+ @app.command("get-vacation")
574
+ def get_vacation_settings() -> None:
575
+ """Get vacation responder settings."""
576
+ service = GmailService()
577
+ service.get_vacation_settings()
578
+
579
+
580
+ @app.command("set-vacation")
581
+ def set_vacation_settings(
582
+ enable: Annotated[bool, typer.Argument(help="Enable (true) or disable (false) vacation responder.")],
583
+ subject: Annotated[
584
+ Optional[str],
585
+ typer.Option("--subject", "-s", help="Response subject line."),
586
+ ] = None,
587
+ message: Annotated[
588
+ Optional[str],
589
+ typer.Option("--message", "-m", help="Plain text response message."),
590
+ ] = None,
591
+ html_message: Annotated[
592
+ Optional[str],
593
+ typer.Option("--html", help="HTML response message."),
594
+ ] = None,
595
+ start_time: Annotated[
596
+ Optional[int],
597
+ typer.Option("--start", help="Start time (Unix timestamp in milliseconds)."),
598
+ ] = None,
599
+ end_time: Annotated[
600
+ Optional[int],
601
+ typer.Option("--end", help="End time (Unix timestamp in milliseconds)."),
602
+ ] = None,
603
+ restrict_to_contacts: Annotated[
604
+ bool,
605
+ typer.Option("--contacts-only", help="Only respond to contacts."),
606
+ ] = False,
607
+ restrict_to_domain: Annotated[
608
+ bool,
609
+ typer.Option("--domain-only", help="Only respond to same domain."),
610
+ ] = False,
611
+ ) -> None:
612
+ """Set vacation responder settings."""
613
+ service = GmailService()
614
+ service.set_vacation_settings(
615
+ enable=enable,
616
+ subject=subject,
617
+ message=message,
618
+ html_message=html_message,
619
+ start_time=start_time,
620
+ end_time=end_time,
621
+ restrict_to_contacts=restrict_to_contacts,
622
+ restrict_to_domain=restrict_to_domain,
623
+ )
624
+
625
+
626
+ @app.command("get-signature")
627
+ def get_signature(
628
+ send_as_email: Annotated[
629
+ Optional[str],
630
+ typer.Option("--email", "-e", help="Send-as email address (default: primary)."),
631
+ ] = None,
632
+ ) -> None:
633
+ """Get email signature."""
634
+ service = GmailService()
635
+ service.get_signature(send_as_email=send_as_email)
636
+
637
+
638
+ @app.command("set-signature")
639
+ def set_signature(
640
+ signature: Annotated[str, typer.Argument(help="HTML signature content.")],
641
+ send_as_email: Annotated[
642
+ Optional[str],
643
+ typer.Option("--email", "-e", help="Send-as email address (default: primary)."),
644
+ ] = None,
645
+ ) -> None:
646
+ """Set email signature."""
647
+ service = GmailService()
648
+ service.set_signature(signature=signature, send_as_email=send_as_email)
649
+
650
+
651
+ # ===== Filters =====
652
+
653
+
654
+ @app.command("filters")
655
+ def list_filters() -> None:
656
+ """List all mail filters."""
657
+ service = GmailService()
658
+ service.list_filters()
659
+
660
+
661
+ @app.command("get-filter")
662
+ def get_filter(
663
+ filter_id: Annotated[str, typer.Argument(help="Filter ID to retrieve.")],
664
+ ) -> None:
665
+ """Get a specific mail filter."""
666
+ service = GmailService()
667
+ service.get_filter(filter_id=filter_id)
668
+
669
+
670
+ @app.command("create-filter")
671
+ def create_filter(
672
+ from_address: Annotated[
673
+ Optional[str],
674
+ typer.Option("--from", "-f", help="Match messages from this address."),
675
+ ] = None,
676
+ to_address: Annotated[
677
+ Optional[str],
678
+ typer.Option("--to", "-t", help="Match messages to this address."),
679
+ ] = None,
680
+ subject: Annotated[
681
+ Optional[str],
682
+ typer.Option("--subject", "-s", help="Match messages with this subject."),
683
+ ] = None,
684
+ query: Annotated[
685
+ Optional[str],
686
+ typer.Option("--query", "-q", help="Gmail search query to match."),
687
+ ] = None,
688
+ has_attachment: Annotated[
689
+ Optional[bool],
690
+ typer.Option("--has-attachment", help="Match messages with attachments."),
691
+ ] = None,
692
+ add_labels: Annotated[
693
+ Optional[str],
694
+ typer.Option("--add-labels", help="Comma-separated label IDs to add."),
695
+ ] = None,
696
+ remove_labels: Annotated[
697
+ Optional[str],
698
+ typer.Option("--remove-labels", help="Comma-separated label IDs to remove."),
699
+ ] = None,
700
+ archive: Annotated[
701
+ bool,
702
+ typer.Option("--archive", help="Skip inbox (archive) matching messages."),
703
+ ] = False,
704
+ star: Annotated[
705
+ bool,
706
+ typer.Option("--star", help="Star matching messages."),
707
+ ] = False,
708
+ mark_important: Annotated[
709
+ Optional[bool],
710
+ typer.Option("--important/--not-important", help="Mark as important or not."),
711
+ ] = None,
712
+ never_spam: Annotated[
713
+ bool,
714
+ typer.Option("--never-spam", help="Never send to spam."),
715
+ ] = False,
716
+ trash: Annotated[
717
+ bool,
718
+ typer.Option("--trash", help="Delete matching messages."),
719
+ ] = False,
720
+ forward_to: Annotated[
721
+ Optional[str],
722
+ typer.Option("--forward", help="Forward to this email address."),
723
+ ] = None,
724
+ ) -> None:
725
+ """Create a mail filter.
726
+
727
+ Example: Create filter to archive newsletters:
728
+ gws-cli gmail create-filter --from newsletter@example.com --archive --add-labels Label_123
729
+ """
730
+ add_label_ids = [label.strip() for label in add_labels.split(",")] if add_labels else None
731
+ remove_label_ids = [label.strip() for label in remove_labels.split(",")] if remove_labels else None
732
+
733
+ service = GmailService()
734
+ service.create_filter(
735
+ from_address=from_address,
736
+ to_address=to_address,
737
+ subject=subject,
738
+ query=query,
739
+ has_attachment=has_attachment,
740
+ add_label_ids=add_label_ids,
741
+ remove_label_ids=remove_label_ids,
742
+ archive=archive,
743
+ star=star,
744
+ mark_important=mark_important,
745
+ never_spam=never_spam,
746
+ trash=trash,
747
+ forward_to=forward_to,
748
+ )
749
+
750
+
751
+ @app.command("delete-filter")
752
+ def delete_filter(
753
+ filter_id: Annotated[str, typer.Argument(help="Filter ID to delete.")],
754
+ ) -> None:
755
+ """Delete a mail filter."""
756
+ service = GmailService()
757
+ service.delete_filter(filter_id=filter_id)
758
+
759
+
760
+ # ===== Additional Message Operations =====
761
+
762
+
763
+ @app.command("untrash")
764
+ def untrash_message(
765
+ message_id: Annotated[str, typer.Argument(help="Message ID to restore from trash.")],
766
+ ) -> None:
767
+ """Remove a message from trash."""
768
+ service = GmailService()
769
+ service.untrash_message(message_id=message_id)
770
+
771
+
772
+ @app.command("batch-modify")
773
+ def batch_modify_messages(
774
+ message_ids: Annotated[str, typer.Argument(help="Comma-separated message IDs.")],
775
+ add_labels: Annotated[
776
+ Optional[str],
777
+ typer.Option("--add-labels", "-a", help="Comma-separated label IDs to add."),
778
+ ] = None,
779
+ remove_labels: Annotated[
780
+ Optional[str],
781
+ typer.Option("--remove-labels", "-r", help="Comma-separated label IDs to remove."),
782
+ ] = None,
783
+ ) -> None:
784
+ """Modify labels on multiple messages at once."""
785
+ ids = [m.strip() for m in message_ids.split(",")]
786
+ add_label_ids = [label.strip() for label in add_labels.split(",")] if add_labels else None
787
+ remove_label_ids = [label.strip() for label in remove_labels.split(",")] if remove_labels else None
788
+
789
+ service = GmailService()
790
+ service.batch_modify_messages(
791
+ message_ids=ids,
792
+ add_label_ids=add_label_ids,
793
+ remove_label_ids=remove_label_ids,
794
+ )
795
+
796
+
797
+ # ===== Label Operations (extended) =====
798
+
799
+
800
+ @app.command("get-label")
801
+ def get_label(
802
+ label_id: Annotated[str, typer.Argument(help="Label ID.")],
803
+ ) -> None:
804
+ """Get details of a specific label."""
805
+ service = GmailService()
806
+ service.get_label(label_id=label_id)
807
+
808
+
809
+ @app.command("update-label")
810
+ def update_label(
811
+ label_id: Annotated[str, typer.Argument(help="Label ID to update.")],
812
+ name: Annotated[
813
+ Optional[str],
814
+ typer.Option("--name", "-n", help="New label name."),
815
+ ] = None,
816
+ message_list_visibility: Annotated[
817
+ Optional[str],
818
+ typer.Option("--message-visibility", help="'show' or 'hide' in message list."),
819
+ ] = None,
820
+ label_list_visibility: Annotated[
821
+ Optional[str],
822
+ typer.Option("--label-visibility", help="'labelShow', 'labelShowIfUnread', or 'labelHide'."),
823
+ ] = None,
824
+ text_color: Annotated[
825
+ Optional[str],
826
+ typer.Option("--text-color", help="Text color (hex, e.g., '#000000')."),
827
+ ] = None,
828
+ background_color: Annotated[
829
+ Optional[str],
830
+ typer.Option("--background-color", help="Background color (hex)."),
831
+ ] = None,
832
+ ) -> None:
833
+ """Update a label's name, visibility, or colors."""
834
+ service = GmailService()
835
+ service.update_label(
836
+ label_id=label_id,
837
+ name=name,
838
+ message_list_visibility=message_list_visibility,
839
+ label_list_visibility=label_list_visibility,
840
+ text_color=text_color,
841
+ background_color=background_color,
842
+ )
843
+
844
+
845
+ # ===== Thread Operations (extended) =====
846
+
847
+
848
+ @app.command("delete-thread")
849
+ def delete_thread(
850
+ thread_id: Annotated[str, typer.Argument(help="Thread ID to permanently delete.")],
851
+ ) -> None:
852
+ """Permanently delete a thread (cannot be undone)."""
853
+ service = GmailService()
854
+ service.delete_thread(thread_id=thread_id)
855
+
856
+
857
+ # ===== History =====
858
+
859
+
860
+ @app.command("history")
861
+ def list_history(
862
+ start_history_id: Annotated[str, typer.Argument(help="History ID to start from.")],
863
+ max_results: Annotated[
864
+ int,
865
+ typer.Option("--max", "-m", help="Maximum number of history records."),
866
+ ] = 100,
867
+ history_types: Annotated[
868
+ Optional[str],
869
+ typer.Option("--types", "-t", help="Comma-separated types (messageAdded,messageDeleted,labelAdded,labelRemoved)."),
870
+ ] = None,
871
+ label_id: Annotated[
872
+ Optional[str],
873
+ typer.Option("--label", "-l", help="Only return changes to messages with this label."),
874
+ ] = None,
875
+ ) -> None:
876
+ """List changes to the mailbox since a given history ID."""
877
+ types = [t.strip() for t in history_types.split(",")] if history_types else None
878
+
879
+ service = GmailService()
880
+ service.list_history(
881
+ start_history_id=start_history_id,
882
+ max_results=max_results,
883
+ history_types=types,
884
+ label_id=label_id,
885
+ )