fetch2gmail 1.1.8__tar.gz → 1.1.9__tar.gz

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.
Files changed (24) hide show
  1. {fetch2gmail-1.1.8/src/fetch2gmail.egg-info → fetch2gmail-1.1.9}/PKG-INFO +1 -1
  2. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/pyproject.toml +1 -1
  3. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9/src/fetch2gmail.egg-info}/PKG-INFO +1 -1
  4. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/src/fetcher/cli.py +2 -0
  5. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/src/fetcher/imap_client.py +29 -5
  6. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/src/fetcher/run.py +23 -17
  7. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/src/fetcher/web_ui.py +27 -0
  8. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/LICENSE +0 -0
  9. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/README.md +0 -0
  10. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/setup.cfg +0 -0
  11. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/src/fetch2gmail.egg-info/SOURCES.txt +0 -0
  12. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/src/fetch2gmail.egg-info/dependency_links.txt +0 -0
  13. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/src/fetch2gmail.egg-info/entry_points.txt +0 -0
  14. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/src/fetch2gmail.egg-info/requires.txt +0 -0
  15. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/src/fetch2gmail.egg-info/top_level.txt +0 -0
  16. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/src/fetcher/__init__.py +0 -0
  17. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/src/fetcher/auth_server.py +0 -0
  18. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/src/fetcher/auth_ui.py +0 -0
  19. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/src/fetcher/config.py +0 -0
  20. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/src/fetcher/env_file.py +0 -0
  21. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/src/fetcher/gmail_client.py +0 -0
  22. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/src/fetcher/log_buffer.py +0 -0
  23. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/src/fetcher/state.py +0 -0
  24. {fetch2gmail-1.1.8 → fetch2gmail-1.1.9}/src/fetcher/ui_auth.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fetch2gmail
3
- Version: 1.1.8
3
+ Version: 1.1.9
4
4
  Summary: Self-hosted email fetcher: IMAP to Gmail API import with idempotent state tracking
5
5
  Author: Fetch2Gmail Contributors
6
6
  License-Expression: MIT
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "fetch2gmail"
7
- version = "1.1.8"
7
+ version = "1.1.9"
8
8
  description = "Self-hosted email fetcher: IMAP to Gmail API import with idempotent state tracking"
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fetch2gmail
3
- Version: 1.1.8
3
+ Version: 1.1.9
4
4
  Summary: Self-hosted email fetcher: IMAP to Gmail API import with idempotent state tracking
5
5
  Author: Fetch2Gmail Contributors
6
6
  License-Expression: MIT
@@ -284,6 +284,7 @@ def _write_default_config(dest: Path) -> None:
284
284
  "mailbox": "INBOX",
285
285
  "use_ssl": True,
286
286
  "delete_after_import": True,
287
+ "since_date": None,
287
288
  },
288
289
  # Canonical: gmail_accounts (multi-account). Single-account setups will have one entry.
289
290
  "gmail_accounts": [{"use_label": False, "label": "ISP Mail", "credentials_path": "credentials.json", "token_path": "token.json"}],
@@ -317,6 +318,7 @@ def config_wizard_interactive() -> None:
317
318
  "mailbox": mailbox,
318
319
  "use_ssl": True,
319
320
  "delete_after_import": True,
321
+ "since_date": None,
320
322
  },
321
323
  "gmail_accounts": [{"use_label": bool(label), "label": label or "ISP Mail", "credentials_path": cred_path, "token_path": token_path}],
322
324
  "state": {"db_path": db_path},
@@ -10,6 +10,7 @@ import hashlib
10
10
  import imaplib
11
11
  import logging
12
12
  import ssl
13
+ import datetime
13
14
  from dataclasses import dataclass
14
15
  from typing import Iterator
15
16
 
@@ -102,6 +103,22 @@ def get_uid_validity(
102
103
  pass
103
104
 
104
105
 
106
+ # IMAP date-month names must be English (RFC 3501); do not use locale-dependent %b.
107
+ _IMAP_MONTHS = (
108
+ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
109
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
110
+ )
111
+
112
+
113
+ def _format_imap_date(date_val: str | datetime.date) -> str:
114
+ """Convert an ISO date string or date object to IMAP DD-MMM-YYYY (English months)."""
115
+ if isinstance(date_val, str):
116
+ date_val = datetime.date.fromisoformat(date_val)
117
+ if not isinstance(date_val, datetime.date):
118
+ raise TypeError("since_date must be a date or ISO date string")
119
+ return f"{date_val.day:02d}-{_IMAP_MONTHS[date_val.month - 1]}-{date_val.year}"
120
+
121
+
105
122
  def fetch_messages(
106
123
  host: str,
107
124
  port: int,
@@ -110,11 +127,14 @@ def fetch_messages(
110
127
  mailbox: str = "INBOX",
111
128
  use_ssl: bool = True,
112
129
  last_processed_uid: int | None = None,
130
+ since: str | datetime.date | None = None,
113
131
  ) -> tuple[int, Iterator[FetchedMessage]]:
114
132
  """
115
133
  Connect via IMAPS, select mailbox, return (uid_validity, iterator of new messages).
116
134
 
117
- Messages are those with UID > last_processed_uid. Ordered by ascending UID.
135
+ Messages are those with UID > last_processed_uid and, if since is
136
+ provided, whose INTERNALDATE is on or after that date.
137
+
118
138
  Caller must handle UIDVALIDITY changes (e.g. reset state when it changes).
119
139
  """
120
140
  ssl_context = ssl.create_default_context() if use_ssl else None
@@ -131,12 +151,16 @@ def fetch_messages(
131
151
  status = conn.status(mailbox, "(UIDVALIDITY)")
132
152
  # e.g. STATUS INBOX (UIDVALIDITY 12345)
133
153
  uid_validity = int(status[1][0].decode().split("UIDVALIDITY")[1].strip(" ()"))
134
- # Search UIDs > last_processed_uid (all if last_processed_uid is None)
154
+ # Search UIDs > last_processed_uid (all if last_processed_uid is None) and optionally SINCE date.
155
+ criteria_parts: list[str] = []
135
156
  if last_processed_uid is not None:
136
- search_criteria = f"UID {last_processed_uid + 1}:*"
157
+ criteria_parts.append(f"UID {last_processed_uid + 1}:*")
137
158
  else:
138
- search_criteria = "UID 1:*"
139
- _, data = conn.uid("SEARCH", None, search_criteria)
159
+ criteria_parts.append("UID 1:*")
160
+ if since is not None:
161
+ imap_date = _format_imap_date(since)
162
+ criteria_parts.append(f"SINCE {imap_date}")
163
+ _, data = conn.uid("SEARCH", None, " ".join(criteria_parts))
140
164
  if not data or not data[0]:
141
165
  try:
142
166
  conn.logout()
@@ -92,6 +92,9 @@ def run_once(config_path: str | None = None, dry_run: bool = False) -> dict[str,
92
92
  db_path = config_dir / db_path
93
93
  mailbox = imap_cfg.get("mailbox", "INBOX")
94
94
  delete_after_import = imap_cfg.get("delete_after_import", True)
95
+ since_date = imap_cfg.get("since_date")
96
+ if since_date == "":
97
+ since_date = None
95
98
 
96
99
  state = StateStore(str(db_path))
97
100
  state.connect()
@@ -119,6 +122,7 @@ def run_once(config_path: str | None = None, dry_run: bool = False) -> dict[str,
119
122
  mailbox=mailbox,
120
123
  use_ssl=imap_cfg.get("use_ssl", True),
121
124
  last_processed_uid=last_uid,
125
+ since=since_date,
122
126
  )
123
127
  except Exception as e:
124
128
  logger.exception("IMAP fetch failed: %s", e)
@@ -129,13 +133,6 @@ def run_once(config_path: str | None = None, dry_run: bool = False) -> dict[str,
129
133
  deleted = 0
130
134
  gmail_contexts: list[tuple[Any, str | None, str, str]] = []
131
135
 
132
- if not dry_run:
133
- try:
134
- gmail_contexts = _build_gmail_contexts(gmail_accounts, config_dir)
135
- except Exception as e:
136
- logger.exception("Gmail API init failed: %s", e)
137
- return {"error": str(e), "imported": 0, "skipped_duplicate": 0, "deleted": 0}
138
-
139
136
  for msg in messages_iter:
140
137
  if state.seen_hash(msg.message_hash):
141
138
  logger.debug("Skipping duplicate (hash): uid=%s", msg.uid)
@@ -162,6 +159,13 @@ def run_once(config_path: str | None = None, dry_run: bool = False) -> dict[str,
162
159
  imported += 1
163
160
  continue
164
161
 
162
+ if not gmail_contexts:
163
+ try:
164
+ gmail_contexts = _build_gmail_contexts(gmail_accounts, config_dir)
165
+ except Exception as e:
166
+ logger.exception("Gmail API init failed: %s", e)
167
+ return {"error": str(e), "imported": 0, "skipped_duplicate": 0, "deleted": 0}
168
+
165
169
  # Import to all Gmail accounts. Record state when at least one import succeeds; only
166
170
  # delete from ISP when *all* imports succeed. This avoids duplicate imports when a
167
171
  # subset of accounts succeeded but others failed.
@@ -266,6 +270,9 @@ def run_copy_all(
266
270
  if not db_path.is_absolute():
267
271
  db_path = config_dir / db_path
268
272
  mailbox = imap_cfg.get("mailbox", "INBOX")
273
+ since_date = imap_cfg.get("since_date")
274
+ if since_date == "":
275
+ since_date = None
269
276
 
270
277
  state = StateStore(str(db_path))
271
278
  state.connect()
@@ -292,6 +299,7 @@ def run_copy_all(
292
299
  mailbox=mailbox,
293
300
  use_ssl=imap_cfg.get("use_ssl", True),
294
301
  last_processed_uid=None,
302
+ since=since_date,
295
303
  )
296
304
  except Exception as e:
297
305
  logger.exception("IMAP fetch failed: %s", e)
@@ -302,13 +310,6 @@ def run_copy_all(
302
310
  deleted = 0
303
311
  gmail_contexts: list[tuple[Any, str | None, str, str]] = []
304
312
 
305
- if not dry_run:
306
- try:
307
- gmail_contexts = _build_gmail_contexts(gmail_accounts, config_dir)
308
- except Exception as e:
309
- logger.exception("Gmail API init failed: %s", e)
310
- return {"error": str(e), "imported": 0, "skipped_duplicate": 0, "deleted": 0}
311
-
312
313
  for msg in messages_iter:
313
314
  # Copy-all is also used as a "repair" tool. With multiple Gmail accounts, we must not
314
315
  # skip a message just because one account already has it; we should import to the missing
@@ -346,9 +347,14 @@ def run_copy_all(
346
347
  imported += 1
347
348
  continue
348
349
 
349
- message_id_header = None
350
- if not dry_run:
351
- message_id_header = _parse_message_id_from_raw(msg.raw)
350
+ if not gmail_contexts:
351
+ try:
352
+ gmail_contexts = _build_gmail_contexts(gmail_accounts, config_dir)
353
+ except Exception as e:
354
+ logger.exception("Gmail API init failed: %s", e)
355
+ return {"error": str(e), "imported": 0, "skipped_duplicate": 0, "deleted": 0}
356
+
357
+ message_id_header = _parse_message_id_from_raw(msg.raw)
352
358
 
353
359
  first_gmail_id: str | None = None
354
360
  all_succeeded = True
@@ -11,6 +11,7 @@ import logging
11
11
  import os
12
12
  import threading
13
13
  import time
14
+ import datetime
14
15
  from contextlib import asynccontextmanager
15
16
  from pathlib import Path
16
17
  from typing import Any
@@ -227,6 +228,7 @@ class ImapConfigSafe(BaseModel):
227
228
  mailbox: str
228
229
  use_ssl: bool
229
230
  delete_after_import: bool
231
+ since_date: str | None = None
230
232
 
231
233
 
232
234
  class GmailConfigSafe(BaseModel):
@@ -439,6 +441,7 @@ def _default_config_response() -> ConfigResponse:
439
441
  mailbox="INBOX",
440
442
  use_ssl=True,
441
443
  delete_after_import=True,
444
+ since_date=None,
442
445
  ),
443
446
  gmail=GmailConfigSafe(
444
447
  use_label=False,
@@ -502,6 +505,7 @@ def api_config(request: Request) -> ConfigResponse:
502
505
  mailbox=imap.get("mailbox", "INBOX"),
503
506
  use_ssl=imap.get("use_ssl", True),
504
507
  delete_after_import=imap.get("delete_after_import", True),
508
+ since_date=imap.get("since_date"),
505
509
  ),
506
510
  gmail=GmailConfigSafe(
507
511
  use_label=gmail.get("use_label") if "use_label" in gmail else bool((gmail.get("label") or "").strip()),
@@ -530,6 +534,7 @@ class ConfigUpdate(BaseModel):
530
534
  imap_username: str | None = None
531
535
  imap_mailbox: str | None = None
532
536
  imap_use_ssl: bool | None = None
537
+ imap_since_date: str | None = None
533
538
  imap_password: str | None = None # stored in .env, not in config
534
539
  delete_after_import: bool | None = None
535
540
  gmail_use_label: bool | None = None
@@ -563,6 +568,11 @@ def api_setup(request: Request, body: SetupBody) -> dict[str, str]:
563
568
  path = _get_config_path()
564
569
  if path.exists():
565
570
  raise HTTPException(status_code=400, detail="Config already exists")
571
+ if body.imap_since_date:
572
+ try:
573
+ datetime.date.fromisoformat(body.imap_since_date)
574
+ except Exception as e:
575
+ raise HTTPException(status_code=400, detail="since_date must be YYYY-MM-DD")
566
576
  try:
567
577
  _verify_imap_credentials(
568
578
  host=body.imap_host,
@@ -582,6 +592,7 @@ def api_setup(request: Request, body: SetupBody) -> dict[str, str]:
582
592
  "username": body.imap_username,
583
593
  "password_env": "IMAP_PASSWORD",
584
594
  "mailbox": body.imap_mailbox,
595
+ "since_date": body.imap_since_date,
585
596
  "use_ssl": body.imap_use_ssl,
586
597
  "delete_after_import": body.delete_after_import,
587
598
  },
@@ -647,6 +658,17 @@ def api_config_update(request: Request, update: ConfigUpdate) -> dict[str, str]:
647
658
  imap["username"] = update.imap_username
648
659
  if update.imap_mailbox is not None:
649
660
  imap["mailbox"] = update.imap_mailbox
661
+ # UI always sends imap_since_date (string or null). null/"" clears the filter.
662
+ if "imap_since_date" in update.model_fields_set:
663
+ if update.imap_since_date:
664
+ try:
665
+ datetime.date.fromisoformat(update.imap_since_date)
666
+ except Exception:
667
+ raise HTTPException(status_code=400, detail="since_date must be YYYY-MM-DD")
668
+ cfg.setdefault("imap", {})["since_date"] = update.imap_since_date
669
+ else:
670
+ cfg.setdefault("imap", {})["since_date"] = None
671
+
650
672
  if update.imap_use_ssl is not None:
651
673
  imap["use_ssl"] = update.imap_use_ssl
652
674
  if update.delete_after_import is not None:
@@ -921,6 +943,7 @@ _HTML_PAGE = """
921
943
  <label>IMAP password <input id="s_imap_password" type="password" placeholder="Stored encrypted in .env"></label>
922
944
  <label>Mailbox <input id="s_imap_mailbox" type="text" value="INBOX" title="IMAP folder to fetch from. INBOX is the main inbox where new mail arrives."></label>
923
945
  <p class="hint" style="font-size:0.85rem; color:#666; margin-top:0;">Mailbox is the IMAP folder to fetch from. <strong>INBOX</strong> is the main inbox where new mail arrives at your ISP; leave as INBOX unless you use a different folder.</p>
946
+ <label>Only fetch mail on or after <input id="s_imap_since_date" type="date"></label>
924
947
  <label style="margin-top:1rem;"><input type="checkbox" id="s_delete_after_import" checked> Delete emails from ISP after importing to Gmail</label>
925
948
  <label><input type="checkbox" id="s_gmail_use_label"> Add a Gmail label to imported mail</label>
926
949
  <div id="s_gmail_label_row" style="display:none;"><label>Label name <input id="s_gmail_label" type="text" value="ISP Mail" placeholder="e.g. ISP Mail"></label></div>
@@ -992,6 +1015,7 @@ _HTML_PAGE = """
992
1015
  <label>IMAP password <input id="imap_password" type="password" placeholder="Leave blank to keep current (stored encrypted)"></label>
993
1016
  <label>Mailbox <input id="imap_mailbox" type="text" title="IMAP folder to fetch from. INBOX = main inbox."></label>
994
1017
  <p class="hint" style="font-size:0.85rem; color:#666; margin-top:0;">Mailbox is the IMAP folder to fetch from. <strong>INBOX</strong> = main inbox where new mail arrives.</p>
1018
+ <label>Only fetch mail on or after <input id="imap_since_date" type="date"></label>
995
1019
  <label style="margin-top:1rem;"><input type="checkbox" id="delete_after_import"> Delete emails from ISP after importing to Gmail</label>
996
1020
  <label><input type="checkbox" id="gmail_use_label"> Add a Gmail label to imported mail</label>
997
1021
  <div id="gmail_label_row" style="display:none;"><label>Label name <input id="gmail_label" type="text" placeholder="e.g. ISP Mail"></label></div>
@@ -1154,6 +1178,7 @@ _APP_JS = r"""
1154
1178
  document.getElementById('imap_use_ssl').checked = c.imap.use_ssl;
1155
1179
  document.getElementById('imap_username').value = c.imap.username;
1156
1180
  document.getElementById('imap_mailbox').value = c.imap.mailbox;
1181
+ document.getElementById('imap_since_date').value = c.imap.since_date || '';
1157
1182
  document.getElementById('delete_after_import').checked = c.imap.delete_after_import;
1158
1183
  document.getElementById('gmail_use_label').checked = c.gmail.use_label;
1159
1184
  document.getElementById('gmail_label_row').style.display = c.gmail.use_label ? 'block' : 'none';
@@ -1352,6 +1377,7 @@ _APP_JS = r"""
1352
1377
  imap_use_ssl: document.getElementById('imap_use_ssl').checked,
1353
1378
  imap_username: document.getElementById('imap_username').value,
1354
1379
  imap_mailbox: document.getElementById('imap_mailbox').value,
1380
+ imap_since_date: document.getElementById('imap_since_date').value || null,
1355
1381
  delete_after_import: document.getElementById('delete_after_import').checked,
1356
1382
  gmail_use_label: document.getElementById('gmail_use_label').checked,
1357
1383
  gmail_label: document.getElementById('gmail_label').value,
@@ -1401,6 +1427,7 @@ _APP_JS = r"""
1401
1427
  imap_username: document.getElementById('s_imap_username').value,
1402
1428
  imap_password: document.getElementById('s_imap_password').value,
1403
1429
  imap_mailbox: document.getElementById('s_imap_mailbox').value,
1430
+ imap_since_date: document.getElementById('s_imap_since_date').value || null,
1404
1431
  delete_after_import: document.getElementById('s_delete_after_import').checked,
1405
1432
  gmail_use_label: document.getElementById('s_gmail_use_label').checked,
1406
1433
  gmail_label: document.getElementById('s_gmail_label').value
File without changes
File without changes
File without changes