fetch2gmail 1.1.5__tar.gz → 1.1.6__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.5/src/fetch2gmail.egg-info → fetch2gmail-1.1.6}/PKG-INFO +10 -3
  2. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/README.md +9 -2
  3. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/pyproject.toml +1 -1
  4. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6/src/fetch2gmail.egg-info}/PKG-INFO +10 -3
  5. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/src/fetcher/cli.py +3 -2
  6. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/src/fetcher/config.py +15 -0
  7. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/src/fetcher/run.py +191 -122
  8. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/src/fetcher/web_ui.py +355 -35
  9. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/LICENSE +0 -0
  10. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/setup.cfg +0 -0
  11. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/src/fetch2gmail.egg-info/SOURCES.txt +0 -0
  12. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/src/fetch2gmail.egg-info/dependency_links.txt +0 -0
  13. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/src/fetch2gmail.egg-info/entry_points.txt +0 -0
  14. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/src/fetch2gmail.egg-info/requires.txt +0 -0
  15. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/src/fetch2gmail.egg-info/top_level.txt +0 -0
  16. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/src/fetcher/__init__.py +0 -0
  17. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/src/fetcher/auth_server.py +0 -0
  18. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/src/fetcher/auth_ui.py +0 -0
  19. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/src/fetcher/env_file.py +0 -0
  20. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/src/fetcher/gmail_client.py +0 -0
  21. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/src/fetcher/imap_client.py +0 -0
  22. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/src/fetcher/log_buffer.py +0 -0
  23. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/src/fetcher/state.py +0 -0
  24. {fetch2gmail-1.1.5 → fetch2gmail-1.1.6}/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.5
3
+ Version: 1.1.6
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
@@ -237,11 +237,18 @@ One instance = one IMAP mailbox → one Gmail account. To run **multiple account
237
237
  ```bash
238
238
  fetch2gmail install-service --user YOUR_USER --dir /opt/fetch2gmail2 -o /tmp/fetch2gmail2.service
239
239
  ```
240
- Edit `/tmp/fetch2gmail2.service`: in **ExecStart**, add **`--port 8766`** so this instance uses port 8766 (the first stays on 8765). Set **WorkingDirectory** and **Environment=FETCH2GMAIL_CONFIG** to the second directory (e.g. `/opt/fetch2gmail2`, `/opt/fetch2gmail2/config.json`).
241
240
 
242
- 3. Install and enable the second unit under a different name so both can run:
241
+ 3. Install the second unit under a different name so both can run, **then edit its ExecStart to add a port**:
243
242
  ```bash
244
243
  sudo mv /tmp/fetch2gmail2.service /etc/systemd/system/fetch2gmail2.service
244
+ sudo nano /etc/systemd/system/fetch2gmail2.service
245
+ ```
246
+ In the editor, find the `ExecStart=` line and make sure it includes a port flag, for example:
247
+ ```
248
+ ExecStart=/home/YOUR_USER/.local/pipx/venvs/fetch2gmail/bin/fetch2gmail serve --host 0.0.0.0 --port 8766
249
+ ```
250
+ Also set **WorkingDirectory** and **Environment=FETCH2GMAIL_CONFIG** to the second directory (e.g. `/opt/fetch2gmail2`, `/opt/fetch2gmail2/config.json`). Then reload and enable:
251
+ ```bash
245
252
  sudo systemctl daemon-reload
246
253
  sudo systemctl enable fetch2gmail2
247
254
  sudo systemctl start fetch2gmail2
@@ -206,11 +206,18 @@ One instance = one IMAP mailbox → one Gmail account. To run **multiple account
206
206
  ```bash
207
207
  fetch2gmail install-service --user YOUR_USER --dir /opt/fetch2gmail2 -o /tmp/fetch2gmail2.service
208
208
  ```
209
- Edit `/tmp/fetch2gmail2.service`: in **ExecStart**, add **`--port 8766`** so this instance uses port 8766 (the first stays on 8765). Set **WorkingDirectory** and **Environment=FETCH2GMAIL_CONFIG** to the second directory (e.g. `/opt/fetch2gmail2`, `/opt/fetch2gmail2/config.json`).
210
209
 
211
- 3. Install and enable the second unit under a different name so both can run:
210
+ 3. Install the second unit under a different name so both can run, **then edit its ExecStart to add a port**:
212
211
  ```bash
213
212
  sudo mv /tmp/fetch2gmail2.service /etc/systemd/system/fetch2gmail2.service
213
+ sudo nano /etc/systemd/system/fetch2gmail2.service
214
+ ```
215
+ In the editor, find the `ExecStart=` line and make sure it includes a port flag, for example:
216
+ ```
217
+ ExecStart=/home/YOUR_USER/.local/pipx/venvs/fetch2gmail/bin/fetch2gmail serve --host 0.0.0.0 --port 8766
218
+ ```
219
+ Also set **WorkingDirectory** and **Environment=FETCH2GMAIL_CONFIG** to the second directory (e.g. `/opt/fetch2gmail2`, `/opt/fetch2gmail2/config.json`). Then reload and enable:
220
+ ```bash
214
221
  sudo systemctl daemon-reload
215
222
  sudo systemctl enable fetch2gmail2
216
223
  sudo systemctl start fetch2gmail2
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "fetch2gmail"
7
- version = "1.1.5"
7
+ version = "1.1.6"
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.5
3
+ Version: 1.1.6
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
@@ -237,11 +237,18 @@ One instance = one IMAP mailbox → one Gmail account. To run **multiple account
237
237
  ```bash
238
238
  fetch2gmail install-service --user YOUR_USER --dir /opt/fetch2gmail2 -o /tmp/fetch2gmail2.service
239
239
  ```
240
- Edit `/tmp/fetch2gmail2.service`: in **ExecStart**, add **`--port 8766`** so this instance uses port 8766 (the first stays on 8765). Set **WorkingDirectory** and **Environment=FETCH2GMAIL_CONFIG** to the second directory (e.g. `/opt/fetch2gmail2`, `/opt/fetch2gmail2/config.json`).
241
240
 
242
- 3. Install and enable the second unit under a different name so both can run:
241
+ 3. Install the second unit under a different name so both can run, **then edit its ExecStart to add a port**:
243
242
  ```bash
244
243
  sudo mv /tmp/fetch2gmail2.service /etc/systemd/system/fetch2gmail2.service
244
+ sudo nano /etc/systemd/system/fetch2gmail2.service
245
+ ```
246
+ In the editor, find the `ExecStart=` line and make sure it includes a port flag, for example:
247
+ ```
248
+ ExecStart=/home/YOUR_USER/.local/pipx/venvs/fetch2gmail/bin/fetch2gmail serve --host 0.0.0.0 --port 8766
249
+ ```
250
+ Also set **WorkingDirectory** and **Environment=FETCH2GMAIL_CONFIG** to the second directory (e.g. `/opt/fetch2gmail2`, `/opt/fetch2gmail2/config.json`). Then reload and enable:
251
+ ```bash
245
252
  sudo systemctl daemon-reload
246
253
  sudo systemctl enable fetch2gmail2
247
254
  sudo systemctl start fetch2gmail2
@@ -270,7 +270,8 @@ def _write_default_config(dest: Path) -> None:
270
270
  "use_ssl": True,
271
271
  "delete_after_import": True,
272
272
  },
273
- "gmail": {"use_label": False, "label": "ISP Mail", "credentials_path": "credentials.json", "token_path": "token.json"},
273
+ # Canonical: gmail_accounts (multi-account). Single-account setups will have one entry.
274
+ "gmail_accounts": [{"use_label": False, "label": "ISP Mail", "credentials_path": "credentials.json", "token_path": "token.json"}],
274
275
  "state": {"db_path": "state.db"},
275
276
  "ui": {"host": "127.0.0.1", "port": 8765},
276
277
  "poll_interval_minutes": 5,
@@ -302,7 +303,7 @@ def config_wizard_interactive() -> None:
302
303
  "use_ssl": True,
303
304
  "delete_after_import": True,
304
305
  },
305
- "gmail": {"use_label": bool(label), "label": label or "ISP Mail", "credentials_path": cred_path, "token_path": token_path},
306
+ "gmail_accounts": [{"use_label": bool(label), "label": label or "ISP Mail", "credentials_path": cred_path, "token_path": token_path}],
306
307
  "state": {"db_path": db_path},
307
308
  "ui": {"host": "127.0.0.1", "port": 8765},
308
309
  "poll_interval_minutes": 5,
@@ -51,3 +51,18 @@ def get_config_path() -> Path:
51
51
  if env:
52
52
  return Path(env)
53
53
  return Path.cwd() / "config.json"
54
+
55
+
56
+ def get_gmail_accounts(cfg: dict[str, Any]) -> list[dict[str, Any]]:
57
+ """
58
+ Return list of Gmail account configs for import.
59
+ Supports multi-account: use "gmail_accounts" (array) or legacy "gmail" (single object).
60
+ If both present, "gmail_accounts" wins. Each entry has credentials_path, token_path, label, use_label.
61
+ """
62
+ accounts = cfg.get("gmail_accounts")
63
+ if isinstance(accounts, list) and len(accounts) > 0:
64
+ return accounts
65
+ gmail = cfg.get("gmail")
66
+ if gmail and isinstance(gmail, dict):
67
+ return [gmail]
68
+ return []
@@ -3,13 +3,15 @@ Main fetch run: IMAP -> Gmail import -> state update -> delete from ISP.
3
3
 
4
4
  Idempotency: UID + message SHA256 hash. On UIDVALIDITY change, hashes prevent duplicates.
5
5
  Only delete from ISP after Gmail confirms import. Dry-run skips Gmail and delete.
6
+ Supports multiple Gmail accounts: import to all in one run; delete from ISP only if all imports succeed.
6
7
  """
7
8
 
8
9
  import logging
9
10
  import os
11
+ from pathlib import Path
10
12
  from typing import Any
11
13
 
12
- from .config import load_config
14
+ from .config import get_gmail_accounts, load_config
13
15
  from .gmail_client import (
14
16
  SkipMessageError,
15
17
  _parse_message_id_from_raw,
@@ -40,6 +42,32 @@ def _ensure_label(service: Any, label_name: str) -> str:
40
42
  return created["id"]
41
43
 
42
44
 
45
+ def _build_gmail_contexts(
46
+ gmail_accounts: list[dict[str, Any]],
47
+ config_dir: Path,
48
+ ) -> list[tuple[Any, str | None, str, str]]:
49
+ """
50
+ Build list of (service, label_id, inbox_label_id, unread_label_id) for each account.
51
+ Paths in account dicts are resolved relative to config_dir.
52
+ """
53
+ contexts = []
54
+ for acct in gmail_accounts:
55
+ cred_path = Path(acct.get("credentials_path", "credentials.json"))
56
+ token_path = Path(acct.get("token_path", "token.json"))
57
+ if not cred_path.is_absolute():
58
+ cred_path = config_dir / cred_path
59
+ if not token_path.is_absolute():
60
+ token_path = config_dir / token_path
61
+ use_label = acct.get("use_label") if "use_label" in acct else bool((acct.get("label") or "").strip())
62
+ label_name = (acct.get("label") or "ISP Mail").strip() if use_label else None
63
+ service = get_gmail_service(cred_path, token_path)
64
+ inbox_label_id = get_inbox_label_id(service, USER_ID)
65
+ unread_label_id = get_unread_label_id(service, USER_ID)
66
+ label_id = _ensure_label(service, label_name) if label_name else None
67
+ contexts.append((service, label_id, inbox_label_id, unread_label_id))
68
+ return contexts
69
+
70
+
43
71
  def run_once(config_path: str | None = None, dry_run: bool = False) -> dict[str, Any]:
44
72
  """
45
73
  Run one fetch cycle: fetch NEW mail only (UID > last_processed), import to Gmail, update state, delete from ISP.
@@ -52,16 +80,20 @@ def run_once(config_path: str | None = None, dry_run: bool = False) -> dict[str,
52
80
  from .config import get_config_path
53
81
  config_path = str(get_config_path())
54
82
  cfg = load_config(config_path)
83
+ config_dir = Path(config_path).resolve().parent
55
84
  imap_cfg = cfg["imap"]
56
- gmail_cfg = cfg["gmail"]
85
+ gmail_accounts = get_gmail_accounts(cfg)
86
+ if not gmail_accounts:
87
+ return {"error": "No Gmail account configured (add 'gmail' or 'gmail_accounts' in config)", "imported": 0, "skipped_duplicate": 0, "deleted": 0}
57
88
  state_cfg = cfg.get("state", {})
58
- db_path = state_cfg.get("db_path", "state.db")
89
+ raw_db_path = state_cfg.get("db_path", "state.db")
90
+ db_path = Path(raw_db_path)
91
+ if not db_path.is_absolute():
92
+ db_path = config_dir / db_path
59
93
  mailbox = imap_cfg.get("mailbox", "INBOX")
60
94
  delete_after_import = imap_cfg.get("delete_after_import", True)
61
- use_label = gmail_cfg.get("use_label") if "use_label" in gmail_cfg else bool((gmail_cfg.get("label") or "").strip())
62
- label_name = (gmail_cfg.get("label") or "ISP Mail").strip() if use_label else None
63
95
 
64
- state = StateStore(db_path)
96
+ state = StateStore(str(db_path))
65
97
  state.connect()
66
98
 
67
99
  try:
@@ -95,22 +127,11 @@ def run_once(config_path: str | None = None, dry_run: bool = False) -> dict[str,
95
127
  imported = 0
96
128
  skipped_duplicate = 0
97
129
  deleted = 0
98
- service = None
99
- label_id = None
100
- inbox_label_id = None
101
- unread_label_id = None
130
+ gmail_contexts: list[tuple[Any, str | None, str, str]] = []
102
131
 
103
132
  if not dry_run:
104
133
  try:
105
- service = get_gmail_service(
106
- gmail_cfg["credentials_path"],
107
- gmail_cfg["token_path"],
108
- )
109
- inbox_label_id = get_inbox_label_id(service, USER_ID)
110
- unread_label_id = get_unread_label_id(service, USER_ID)
111
- label_id = _ensure_label(service, label_name) if label_name else None
112
- if label_name and not label_id:
113
- label_id = _ensure_label(service, label_name)
134
+ gmail_contexts = _build_gmail_contexts(gmail_accounts, config_dir)
114
135
  except Exception as e:
115
136
  logger.exception("Gmail API init failed: %s", e)
116
137
  return {"error": str(e), "imported": 0, "skipped_duplicate": 0, "deleted": 0}
@@ -119,18 +140,6 @@ def run_once(config_path: str | None = None, dry_run: bool = False) -> dict[str,
119
140
  if state.seen_hash(msg.message_hash):
120
141
  logger.debug("Skipping duplicate (hash): uid=%s", msg.uid)
121
142
  skipped_duplicate += 1
122
- # Still advance last_processed_uid after we would have deleted (see below)
123
- # Actually no: we don't delete duplicates from ISP. So we don't update last_uid for skipped.
124
- # So we'll see them again next run. To avoid re-checking forever, we could record "seen UID" without
125
- # Gmail ID - but then we'd want to delete from ISP too for duplicates. Per spec: "If a UID changes or
126
- # server resets UIDVALIDITY, use the hash to prevent duplicates." So we skip import but do we delete?
127
- # Spec says "Only delete from ISP after Gmail confirms successful import." So we don't delete if we
128
- # skipped (no Gmail import). So the message stays on ISP. We could still advance last_processed_uid
129
- # so we don't keep re-fetching it - but then we'd never delete it. So the right behavior is: skip
130
- # import, do NOT delete, do NOT advance last_uid - so next run we'll see it again and skip again.
131
- # That's wasteful. Alternative: when we skip by hash, we still delete from ISP (we know it's already
132
- # in Gmail from a previous run). That way we advance and delete. I'll do that: if seen_hash, delete
133
- # from ISP and advance last_uid, so we don't re-process.
134
143
  if not dry_run and delete_after_import:
135
144
  try:
136
145
  delete_and_expunge(
@@ -153,31 +162,57 @@ def run_once(config_path: str | None = None, dry_run: bool = False) -> dict[str,
153
162
  imported += 1
154
163
  continue
155
164
 
156
- try:
157
- label_ids = [label_id] if label_id else []
158
- # Fetch/polling only gets NEW mail (UID > last); always import as unread.
159
- gmail_id = import_message(
160
- service,
161
- USER_ID,
162
- msg.raw,
163
- label_ids=label_ids,
164
- inbox_label_id=inbox_label_id,
165
- unread_label_id=unread_label_id,
166
- mark_unread=True,
167
- )
168
- state.record_import(msg.message_hash, gmail_id, mailbox, uid_validity, msg.uid)
165
+ # Import to all Gmail accounts. Record state when at least one import succeeds; only
166
+ # delete from ISP when *all* imports succeed. This avoids duplicate imports when a
167
+ # subset of accounts succeeded but others failed.
168
+ first_gmail_id: str | None = None
169
+ all_succeeded = True
170
+ any_imported = False
171
+ skip_entire_message = False
172
+ for idx, (service, label_id, inbox_label_id, unread_label_id) in enumerate(gmail_contexts):
173
+ try:
174
+ label_ids = [label_id] if label_id else []
175
+ gmail_id = import_message(
176
+ service,
177
+ USER_ID,
178
+ msg.raw,
179
+ label_ids=label_ids,
180
+ inbox_label_id=inbox_label_id,
181
+ unread_label_id=unread_label_id,
182
+ mark_unread=True,
183
+ )
184
+ any_imported = True
185
+ if first_gmail_id is None:
186
+ first_gmail_id = gmail_id
187
+ logger.info("Imported uid=%s -> Gmail account %s id=%s", msg.uid, idx + 1, gmail_id)
188
+ except SkipMessageError as e:
189
+ logger.warning("Skipping uid=%s (no From/Sender/Reply-To): %s", msg.uid, e)
190
+ # Message is structurally invalid for Gmail (missing From/Sender/Reply-To). Treat it
191
+ # as permanently unimportable for *all* accounts: do not record a partial success,
192
+ # but do advance last_processed_uid so we don't get stuck retrying this UID forever.
193
+ skip_entire_message = True
194
+ all_succeeded = False
195
+ break
196
+ except Exception as e:
197
+ logger.exception("Gmail import failed for uid=%s account %s: %s", msg.uid, idx + 1, e)
198
+ all_succeeded = False
199
+ break
200
+
201
+ if skip_entire_message:
169
202
  state.set_last_processed_uid(mailbox, uid_validity, msg.uid)
170
- imported += 1
171
- logger.info("Imported uid=%s -> Gmail id=%s", msg.uid, gmail_id)
172
- except SkipMessageError as e:
173
- logger.warning("Skipping uid=%s (no From/Sender/Reply-To): %s", msg.uid, e)
203
+ # Do not record_import and do not delete from ISP; the message remains only on ISP.
174
204
  continue
175
- except Exception as e:
176
- logger.exception("Gmail import failed for uid=%s: %s", msg.uid, e)
177
- # Do not delete from ISP; do not advance last_uid. Next run will retry.
205
+
206
+ if not any_imported or first_gmail_id is None:
207
+ # No successful import this run; do not record state or delete from ISP.
208
+ # Next run will retry for all accounts.
178
209
  continue
179
210
 
180
- if delete_after_import:
211
+ state.record_import(msg.message_hash, first_gmail_id, mailbox, uid_validity, msg.uid)
212
+ state.set_last_processed_uid(mailbox, uid_validity, msg.uid)
213
+ imported += 1
214
+
215
+ if delete_after_import and all_succeeded:
181
216
  try:
182
217
  delete_and_expunge(
183
218
  imap_cfg["host"],
@@ -190,10 +225,8 @@ def run_once(config_path: str | None = None, dry_run: bool = False) -> dict[str,
190
225
  )
191
226
  deleted += 1
192
227
  except Exception as e:
228
+ # Count only successful deletions; failures are logged but not included.
193
229
  logger.exception("Delete failed for uid=%s (already in Gmail): %s", msg.uid, e)
194
- # State already updated; next run won't re-import (hash recorded). We can retry delete later
195
- # by scanning ISP and matching hashes - not implemented; manual cleanup or re-run delete-only.
196
- deleted += 1 # count as "handled"
197
230
 
198
231
  last_fetch = state.get_last_fetch_time(mailbox, uid_validity)
199
232
  state.close()
@@ -215,21 +248,26 @@ def run_copy_all(
215
248
  Copy all messages from ISP mailbox to Gmail (not just UID > last_processed).
216
249
  Preserves read/unread from IMAP \\Seen. Skips messages already in Gmail (by hash or Message-ID).
217
250
  delete_after_import: if True, delete from ISP after successful import (or after skip when already in Gmail).
251
+ Supports multiple Gmail accounts: import to all; delete from ISP only if all imports succeed.
218
252
  Returns same shape as run_once.
219
253
  """
220
254
  if config_path is None:
221
255
  from .config import get_config_path
222
256
  config_path = str(get_config_path())
223
257
  cfg = load_config(config_path)
258
+ config_dir = Path(config_path).resolve().parent
224
259
  imap_cfg = cfg["imap"]
225
- gmail_cfg = cfg["gmail"]
260
+ gmail_accounts = get_gmail_accounts(cfg)
261
+ if not gmail_accounts:
262
+ return {"error": "No Gmail account configured (add 'gmail' or 'gmail_accounts' in config)", "imported": 0, "skipped_duplicate": 0, "deleted": 0}
226
263
  state_cfg = cfg.get("state", {})
227
- db_path = state_cfg.get("db_path", "state.db")
264
+ raw_db_path = state_cfg.get("db_path", "state.db")
265
+ db_path = Path(raw_db_path)
266
+ if not db_path.is_absolute():
267
+ db_path = config_dir / db_path
228
268
  mailbox = imap_cfg.get("mailbox", "INBOX")
229
- use_label = gmail_cfg.get("use_label") if "use_label" in gmail_cfg else bool((gmail_cfg.get("label") or "").strip())
230
- label_name = (gmail_cfg.get("label") or "ISP Mail").strip() if use_label else None
231
269
 
232
- state = StateStore(db_path)
270
+ state = StateStore(str(db_path))
233
271
  state.connect()
234
272
 
235
273
  try:
@@ -262,26 +300,24 @@ def run_copy_all(
262
300
  imported = 0
263
301
  skipped_duplicate = 0
264
302
  deleted = 0
265
- service = None
266
- label_id = None
267
- inbox_label_id = None
268
- unread_label_id = None
303
+ gmail_contexts: list[tuple[Any, str | None, str, str]] = []
269
304
 
270
305
  if not dry_run:
271
306
  try:
272
- service = get_gmail_service(
273
- gmail_cfg["credentials_path"],
274
- gmail_cfg["token_path"],
275
- )
276
- inbox_label_id = get_inbox_label_id(service, USER_ID)
277
- unread_label_id = get_unread_label_id(service, USER_ID)
278
- label_id = _ensure_label(service, label_name) if label_name else None
307
+ gmail_contexts = _build_gmail_contexts(gmail_accounts, config_dir)
279
308
  except Exception as e:
280
309
  logger.exception("Gmail API init failed: %s", e)
281
310
  return {"error": str(e), "imported": 0, "skipped_duplicate": 0, "deleted": 0}
282
311
 
283
312
  for msg in messages_iter:
284
- if state.seen_hash(msg.message_hash):
313
+ # Copy-all is also used as a "repair" tool. With multiple Gmail accounts, we must not
314
+ # skip a message just because one account already has it; we should import to the missing
315
+ # accounts. Therefore:
316
+ # - For single-account setups, we keep the existing hash-based dedupe fast-path.
317
+ # - For multi-account setups, we rely on per-account Message-ID checks when available.
318
+ # IMPORTANT: use gmail_accounts length here (not gmail_contexts) so behavior is correct
319
+ # even in dry-run, where gmail_contexts is intentionally empty.
320
+ if len(gmail_accounts) <= 1 and state.seen_hash(msg.message_hash):
285
321
  logger.debug("Copy-all: skipping duplicate (hash): uid=%s", msg.uid)
286
322
  skipped_duplicate += 1
287
323
  if not dry_run and delete_after_import:
@@ -301,37 +337,6 @@ def run_copy_all(
301
337
  logger.warning("Delete (duplicate) failed for uid=%s: %s", msg.uid, e)
302
338
  continue
303
339
 
304
- if not dry_run:
305
- message_id_header = _parse_message_id_from_raw(msg.raw)
306
- if message_id_header and gmail_has_message_with_id(
307
- service, USER_ID, message_id_header
308
- ):
309
- logger.debug(
310
- "Copy-all: skipping (already in Gmail by Message-ID): uid=%s",
311
- msg.uid,
312
- )
313
- skipped_duplicate += 1
314
- if delete_after_import:
315
- try:
316
- delete_and_expunge(
317
- imap_cfg["host"],
318
- int(imap_cfg.get("port", 993)),
319
- imap_cfg["username"],
320
- imap_cfg["password"],
321
- mailbox,
322
- msg.uid,
323
- imap_cfg.get("use_ssl", True),
324
- )
325
- state.set_last_processed_uid(mailbox, uid_validity, msg.uid)
326
- deleted += 1
327
- except Exception as e:
328
- logger.warning(
329
- "Delete (already in Gmail) failed for uid=%s: %s",
330
- msg.uid,
331
- e,
332
- )
333
- continue
334
-
335
340
  if dry_run:
336
341
  logger.info(
337
342
  "[DRY-RUN] Copy-all would import uid=%s (hash=%s)",
@@ -341,29 +346,93 @@ def run_copy_all(
341
346
  imported += 1
342
347
  continue
343
348
 
344
- try:
345
- label_ids = [label_id] if label_id else []
346
- # Copy-all: preserve read/unread from ISP (\Seen).
347
- gmail_id = import_message(
348
- service,
349
- USER_ID,
350
- msg.raw,
351
- label_ids=label_ids,
352
- inbox_label_id=inbox_label_id,
353
- unread_label_id=unread_label_id,
354
- mark_unread=not msg.is_seen,
349
+ message_id_header = None
350
+ if not dry_run:
351
+ message_id_header = _parse_message_id_from_raw(msg.raw)
352
+
353
+ first_gmail_id: str | None = None
354
+ all_succeeded = True
355
+ any_imported = False
356
+ all_already_had = True
357
+
358
+ for idx, (service, label_id, inbox_label_id, unread_label_id) in enumerate(gmail_contexts):
359
+ try:
360
+ already_here = False
361
+ if message_id_header:
362
+ already_here = gmail_has_message_with_id(service, USER_ID, message_id_header)
363
+ if already_here:
364
+ logger.debug("Copy-all: uid=%s already in Gmail account %s (Message-ID)", msg.uid, idx + 1)
365
+ else:
366
+ all_already_had = False
367
+ label_ids = [label_id] if label_id else []
368
+ gmail_id = import_message(
369
+ service,
370
+ USER_ID,
371
+ msg.raw,
372
+ label_ids=label_ids,
373
+ inbox_label_id=inbox_label_id,
374
+ unread_label_id=unread_label_id,
375
+ mark_unread=not msg.is_seen,
376
+ )
377
+ any_imported = True
378
+ if first_gmail_id is None:
379
+ first_gmail_id = gmail_id
380
+ logger.info(
381
+ "Copy-all imported uid=%s -> Gmail account %s id=%s",
382
+ msg.uid,
383
+ idx + 1,
384
+ gmail_id,
385
+ )
386
+ except SkipMessageError as e:
387
+ logger.warning("Skipping uid=%s (no From/Sender/Reply-To): %s", msg.uid, e)
388
+ all_succeeded = False
389
+ break
390
+ except Exception as e:
391
+ logger.exception("Gmail import failed for uid=%s account %s: %s", msg.uid, idx + 1, e)
392
+ all_succeeded = False
393
+ break
394
+
395
+ if not all_succeeded:
396
+ continue
397
+
398
+ if all_already_had and message_id_header:
399
+ skipped_duplicate += 1
400
+ # Even when every Gmail account already has the message, we still consider it "handled"
401
+ # for copy-all bookkeeping so we don't re-process it on subsequent copy-all runs.
402
+ state.record_import(
403
+ msg.message_hash,
404
+ "already-present",
405
+ mailbox,
406
+ uid_validity,
407
+ msg.uid,
355
408
  )
356
- state.record_import(msg.message_hash, gmail_id, mailbox, uid_validity, msg.uid)
357
409
  state.set_last_processed_uid(mailbox, uid_validity, msg.uid)
358
- imported += 1
359
- logger.info("Copy-all imported uid=%s -> Gmail id=%s", msg.uid, gmail_id)
360
- except SkipMessageError as e:
361
- logger.warning("Skipping uid=%s (no From/Sender/Reply-To): %s", msg.uid, e)
410
+ if delete_after_import:
411
+ try:
412
+ delete_and_expunge(
413
+ imap_cfg["host"],
414
+ int(imap_cfg.get("port", 993)),
415
+ imap_cfg["username"],
416
+ imap_cfg["password"],
417
+ mailbox,
418
+ msg.uid,
419
+ imap_cfg.get("use_ssl", True),
420
+ )
421
+ deleted += 1
422
+ except Exception as e:
423
+ logger.warning("Delete (already in Gmail) failed for uid=%s: %s", msg.uid, e)
362
424
  continue
363
- except Exception as e:
364
- logger.exception("Gmail import failed for uid=%s: %s", msg.uid, e)
425
+
426
+ if not any_imported and first_gmail_id is None:
427
+ # No Message-ID to check, and no import happened (shouldn't occur); be safe.
365
428
  continue
366
429
 
430
+ # Consider the message handled when every account either already had it (by Message-ID)
431
+ # or we successfully imported it. Record a single hash marker for copy-all bookkeeping.
432
+ state.record_import(msg.message_hash, first_gmail_id or "already-present", mailbox, uid_validity, msg.uid)
433
+ state.set_last_processed_uid(mailbox, uid_validity, msg.uid)
434
+ imported += 1
435
+
367
436
  if delete_after_import:
368
437
  try:
369
438
  delete_and_expunge(
@@ -377,10 +446,10 @@ def run_copy_all(
377
446
  )
378
447
  deleted += 1
379
448
  except Exception as e:
449
+ # Only count successful deletions; failures are logged but not included.
380
450
  logger.exception(
381
451
  "Delete failed for uid=%s (already in Gmail): %s", msg.uid, e
382
452
  )
383
- deleted += 1
384
453
 
385
454
  last_fetch = state.get_last_fetch_time(mailbox, uid_validity)
386
455
  state.close()
@@ -22,7 +22,7 @@ from pydantic import BaseModel
22
22
 
23
23
  import imaplib
24
24
 
25
- from .config import get_config_path, load_config
25
+ from .config import get_config_path, get_gmail_accounts, load_config
26
26
  from .env_file import set_encrypted_env
27
27
  from .gmail_client import get_gmail_service
28
28
  from .imap_client import get_uid_validity
@@ -175,16 +175,22 @@ def _config_exists() -> bool:
175
175
 
176
176
 
177
177
  def _token_exists() -> bool:
178
- """True when config exists and the configured token file exists."""
178
+ """True when config exists and every Gmail account has its token file."""
179
179
  cfg_dir = _config_dir_safe()
180
180
  if not cfg_dir or not _config_exists():
181
181
  return False
182
182
  try:
183
183
  cfg = load_config(_get_config_path(), resolve_password=False)
184
- token_path = Path((cfg.get("gmail") or {}).get("token_path", "token.json"))
185
- if not token_path.is_absolute():
186
- token_path = cfg_dir / token_path
187
- return token_path.exists()
184
+ accounts = get_gmail_accounts(cfg)
185
+ if not accounts:
186
+ return False
187
+ for acct in accounts:
188
+ token_path = Path(acct.get("token_path", "token.json"))
189
+ if not token_path.is_absolute():
190
+ token_path = cfg_dir / token_path
191
+ if not token_path.exists():
192
+ return False
193
+ return True
188
194
  except Exception:
189
195
  return False
190
196
 
@@ -230,6 +236,13 @@ class GmailConfigSafe(BaseModel):
230
236
  token_path: str
231
237
 
232
238
 
239
+ class GmailAccountSafe(BaseModel):
240
+ use_label: bool
241
+ label: str
242
+ credentials_path: str
243
+ token_path: str
244
+
245
+
233
246
  class UIConfigSafe(BaseModel):
234
247
  host: str
235
248
  port: int
@@ -238,6 +251,7 @@ class UIConfigSafe(BaseModel):
238
251
  class ConfigResponse(BaseModel):
239
252
  imap: ImapConfigSafe
240
253
  gmail: GmailConfigSafe
254
+ gmail_accounts: list[GmailAccountSafe] = []
241
255
  ui: UIConfigSafe
242
256
  poll_interval_minutes: int
243
257
  state_db_path: str
@@ -336,27 +350,20 @@ def index(request: Request) -> str:
336
350
 
337
351
 
338
352
  def _gmail_connected() -> bool:
339
- path = _get_config_path()
340
- if not path.exists():
341
- return False
342
- try:
343
- cfg = load_config(path, resolve_password=False)
344
- token_path = (cfg.get("gmail") or {}).get("token_path", "token.json")
345
- tp = Path(token_path)
346
- if not tp.is_absolute():
347
- tp = _config_dir() / token_path
348
- return tp.exists()
349
- except Exception:
350
- return False
353
+ """True when all configured Gmail accounts have token files (and can be used for fetch)."""
354
+ return _token_exists()
351
355
 
352
356
 
353
357
  def _gmail_email() -> str | None:
354
- """Return the Gmail address for the current token, or None if not connected or error."""
358
+ """Return the Gmail address for the first account, or None if not connected or error."""
355
359
  if not _gmail_connected():
356
360
  return None
357
361
  try:
358
362
  cfg = load_config(_get_config_path(), resolve_password=False)
359
- gmail = cfg.get("gmail") or {}
363
+ accounts = get_gmail_accounts(cfg)
364
+ if not accounts:
365
+ return None
366
+ gmail = accounts[0]
360
367
  cred_path = Path(gmail.get("credentials_path", "credentials.json"))
361
368
  token_path = Path(gmail.get("token_path", "token.json"))
362
369
  if not cred_path.is_absolute():
@@ -439,6 +446,14 @@ def _default_config_response() -> ConfigResponse:
439
446
  credentials_path="credentials.json",
440
447
  token_path="token.json",
441
448
  ),
449
+ gmail_accounts=[
450
+ GmailAccountSafe(
451
+ use_label=False,
452
+ label="ISP Mail",
453
+ credentials_path="credentials.json",
454
+ token_path="token.json",
455
+ )
456
+ ],
442
457
  ui=UIConfigSafe(host="127.0.0.1", port=8765),
443
458
  poll_interval_minutes=5,
444
459
  state_db_path="state.db",
@@ -463,11 +478,22 @@ def api_config(request: Request) -> ConfigResponse:
463
478
  except ValueError as e:
464
479
  raise HTTPException(status_code=400, detail=str(e))
465
480
  imap = cfg.get("imap") or {}
466
- gmail = cfg.get("gmail") or {}
481
+ gmail_accounts = get_gmail_accounts(cfg)
482
+ gmail = gmail_accounts[0] if gmail_accounts else {}
467
483
  ui = cfg.get("ui") or {}
468
484
  state = cfg.get("state") or {}
469
485
  cred_path = gmail.get("credentials_path", "credentials.json")
470
486
  token_path = gmail.get("token_path", "token.json")
487
+ gmail_accounts_safe: list[GmailAccountSafe] = []
488
+ for acct in gmail_accounts:
489
+ gmail_accounts_safe.append(
490
+ GmailAccountSafe(
491
+ use_label=acct.get("use_label") if "use_label" in acct else bool((acct.get("label") or "").strip()),
492
+ label=acct.get("label", "ISP Mail"),
493
+ credentials_path=acct.get("credentials_path", "credentials.json"),
494
+ token_path=acct.get("token_path", "token.json"),
495
+ )
496
+ )
471
497
  return ConfigResponse(
472
498
  imap=ImapConfigSafe(
473
499
  host=imap.get("host", ""),
@@ -483,6 +509,7 @@ def api_config(request: Request) -> ConfigResponse:
483
509
  credentials_path=cred_path,
484
510
  token_path=token_path,
485
511
  ),
512
+ gmail_accounts=gmail_accounts_safe,
486
513
  ui=UIConfigSafe(
487
514
  host=ui.get("host", "127.0.0.1"),
488
515
  port=int(ui.get("port", 8765)),
@@ -507,6 +534,7 @@ class ConfigUpdate(BaseModel):
507
534
  delete_after_import: bool | None = None
508
535
  gmail_use_label: bool | None = None
509
536
  gmail_label: str | None = None
537
+ gmail_accounts: list[GmailAccountSafe] | None = None
510
538
  poll_interval_minutes: int | None = None
511
539
  state_db_path: str | None = None
512
540
 
@@ -557,12 +585,15 @@ def api_setup(request: Request, body: SetupBody) -> dict[str, str]:
557
585
  "use_ssl": body.imap_use_ssl,
558
586
  "delete_after_import": body.delete_after_import,
559
587
  },
560
- "gmail": {
561
- "use_label": body.gmail_use_label,
562
- "label": body.gmail_label,
563
- "credentials_path": body.credentials_path,
564
- "token_path": body.token_path,
565
- },
588
+ # Canonical: gmail_accounts. Keep legacy readers working via get_gmail_accounts().
589
+ "gmail_accounts": [
590
+ {
591
+ "use_label": body.gmail_use_label,
592
+ "label": body.gmail_label,
593
+ "credentials_path": body.credentials_path,
594
+ "token_path": body.token_path,
595
+ }
596
+ ],
566
597
  "state": {"db_path": body.state_db_path},
567
598
  "ui": {"host": "127.0.0.1", "port": 8765},
568
599
  "poll_interval_minutes": 5,
@@ -620,10 +651,35 @@ def api_config_update(request: Request, update: ConfigUpdate) -> dict[str, str]:
620
651
  imap["use_ssl"] = update.imap_use_ssl
621
652
  if update.delete_after_import is not None:
622
653
  cfg.setdefault("imap", {})["delete_after_import"] = update.delete_after_import
623
- if update.gmail_use_label is not None:
624
- cfg.setdefault("gmail", {})["use_label"] = update.gmail_use_label
625
- if update.gmail_label is not None:
626
- cfg.setdefault("gmail", {})["label"] = update.gmail_label
654
+ # Gmail accounts
655
+ if update.gmail_accounts is not None:
656
+ # Replace full list from UI (canonical).
657
+ cfg["gmail_accounts"] = [
658
+ {
659
+ "use_label": a.use_label,
660
+ "label": a.label,
661
+ "credentials_path": a.credentials_path,
662
+ "token_path": a.token_path,
663
+ }
664
+ for a in update.gmail_accounts
665
+ ]
666
+ # Do not keep legacy 'gmail' when explicitly using gmail_accounts.
667
+ if "gmail" in cfg:
668
+ cfg.pop("gmail", None)
669
+ else:
670
+ # Back-compat: UI may still send gmail_use_label/gmail_label for the first account.
671
+ if update.gmail_use_label is not None or update.gmail_label is not None:
672
+ existing = get_gmail_accounts(cfg)
673
+ if not existing:
674
+ existing = [{"credentials_path": "credentials.json", "token_path": "token.json"}]
675
+ first = dict(existing[0])
676
+ if update.gmail_use_label is not None:
677
+ first["use_label"] = update.gmail_use_label
678
+ if update.gmail_label is not None:
679
+ first["label"] = update.gmail_label
680
+ rest = existing[1:]
681
+ cfg["gmail_accounts"] = [first, *rest]
682
+ cfg.pop("gmail", None)
627
683
  if update.poll_interval_minutes is not None:
628
684
  cfg["poll_interval_minutes"] = update.poll_interval_minutes
629
685
  if update.state_db_path is not None:
@@ -633,6 +689,94 @@ def api_config_update(request: Request, update: ConfigUpdate) -> dict[str, str]:
633
689
  return {"status": "ok"}
634
690
 
635
691
 
692
+ class DiscoveredToken(BaseModel):
693
+ token_path: str
694
+ email: str | None = None
695
+ in_use: bool = False
696
+
697
+
698
+ @app.get("/api/gmail/discover-tokens", response_model=list[DiscoveredToken])
699
+ def api_gmail_discover_tokens(request: Request) -> list[DiscoveredToken]:
700
+ """List token*.json files in config dir and (best-effort) associated Gmail email."""
701
+ if not _require_auth(request):
702
+ raise HTTPException(status_code=401, detail="Not authenticated")
703
+ cfg_dir = _config_dir_safe()
704
+ if not cfg_dir:
705
+ return []
706
+ # tokens in directory
707
+ token_files = sorted([p.name for p in Path(cfg_dir).glob("token*.json") if p.is_file()])
708
+ in_use: set[str] = set()
709
+ try:
710
+ if _config_exists():
711
+ cfg = load_config(_get_config_path(), resolve_password=False)
712
+ for a in get_gmail_accounts(cfg):
713
+ # Normalize for comparison against filenames in this directory. Config may store
714
+ # relative paths ("token2.json") or absolute paths ("/.../token2.json").
715
+ tp = Path(str(a.get("token_path", "token.json")))
716
+ in_use.add(tp.name)
717
+ except Exception:
718
+ pass
719
+ out: list[DiscoveredToken] = []
720
+ # Resolve email addresses best-effort (credentials required)
721
+ cred_path = Path(cfg_dir) / "credentials.json"
722
+ for fname in token_files:
723
+ tok_path = Path(cfg_dir) / fname
724
+ email = None
725
+ try:
726
+ if cred_path.exists() and tok_path.exists():
727
+ service = get_gmail_service(cred_path, tok_path)
728
+ profile = service.users().getProfile(userId="me").execute()
729
+ email = profile.get("emailAddress")
730
+ except Exception:
731
+ email = None
732
+ out.append(DiscoveredToken(token_path=fname, email=email, in_use=(fname in in_use)))
733
+ return out
734
+
735
+
736
+ class DeleteGmailAccountBody(BaseModel):
737
+ index: int
738
+
739
+
740
+ @app.delete("/api/gmail/accounts")
741
+ def api_delete_gmail_account(request: Request, body: DeleteGmailAccountBody) -> dict[str, str]:
742
+ """Delete a secondary Gmail account entry and delete its token file (safety: index must be > 0)."""
743
+ if not _require_auth(request):
744
+ raise HTTPException(status_code=401, detail="Not authenticated")
745
+ if body.index <= 0:
746
+ raise HTTPException(status_code=400, detail="Cannot delete the primary Gmail account from the UI.")
747
+ path = _get_config_path()
748
+ if not path.exists():
749
+ raise HTTPException(status_code=404, detail="config.json not found")
750
+ with open(path, encoding="utf-8") as f:
751
+ cfg = json.load(f)
752
+ accounts = get_gmail_accounts(cfg)
753
+ if body.index >= len(accounts):
754
+ raise HTTPException(status_code=400, detail="Invalid account index")
755
+ token_path = str(accounts[body.index].get("token_path", "")).strip()
756
+ # Remove from config
757
+ new_accounts = [accounts[i] for i in range(len(accounts)) if i != body.index]
758
+ cfg["gmail_accounts"] = new_accounts
759
+ cfg.pop("gmail", None)
760
+ with open(path, "w", encoding="utf-8") as f:
761
+ json.dump(cfg, f, indent=2)
762
+ try:
763
+ # Delete token file (only if inside config dir and named token*.json)
764
+ cfg_dir = _config_dir()
765
+ tp = Path(token_path)
766
+ if not tp.is_absolute():
767
+ tp = cfg_dir / tp
768
+ tp = tp.resolve()
769
+ if tp.parent != cfg_dir.resolve():
770
+ return {"status": "ok", "message": "Account removed. Token file was not deleted (outside config dir)."}
771
+ if not tp.name.startswith("token") or not tp.name.endswith(".json"):
772
+ return {"status": "ok", "message": "Account removed. Token file was not deleted (unexpected filename)."}
773
+ if tp.exists():
774
+ tp.unlink()
775
+ return {"status": "ok", "message": f"Account removed. Deleted {tp.name}."}
776
+ except Exception:
777
+ return {"status": "ok", "message": "Account removed. Token file deletion failed (check permissions)."}
778
+
779
+
636
780
  class CopyAllBody(BaseModel):
637
781
  """Body for copy-all: copy every message from ISP to Gmail (with optional delete after)."""
638
782
  delete_after: bool = False
@@ -692,9 +836,13 @@ def api_status(request: Request) -> dict[str, Any]:
692
836
  cfg = load_config(_get_config_path(), resolve_password=False)
693
837
  from .state import StateStore
694
838
  state_cfg = cfg.get("state", {})
695
- db_path = state_cfg.get("db_path", "state.db")
839
+ raw_db_path = state_cfg.get("db_path", "state.db")
840
+ from pathlib import Path as _PathForStatus
841
+ db_path = _PathForStatus(raw_db_path)
842
+ if not db_path.is_absolute():
843
+ db_path = _PathForStatus(_get_config_path()).resolve().parent / db_path
696
844
  mailbox = (cfg.get("imap") or {}).get("mailbox", "INBOX")
697
- state = StateStore(db_path)
845
+ state = StateStore(str(db_path))
698
846
  state.connect()
699
847
  row = state.get_last_fetch_time_any(mailbox)
700
848
  state.close()
@@ -800,7 +948,24 @@ _HTML_PAGE = """
800
948
  <h2>Gmail</h2>
801
949
  <p id="gmailStatus"></p>
802
950
  <p id="gmailEmail" class="status" style="font-size:0.9rem"></p>
803
- <p class="hint" style="font-size:0.85rem; color:#666;">Token is from <code>fetch2gmail auth</code> (CLI). To switch accounts, run <code>fetch2gmail auth</code> again and replace token.json.</p>
951
+ <div id="gmailAccounts"></div>
952
+ <button type="button" id="btnAddGmailAccount">Add Gmail account</button>
953
+ <div id="addGmailAccountPanel" style="display:none; margin-top:0.75rem; padding:0.75rem; background:#f7f7f7; border:1px solid #e6e6e6;">
954
+ <div style="display:flex; gap:0.5rem; align-items:center; flex-wrap:wrap;">
955
+ <label style="margin:0; flex: 1 1 320px;">
956
+ Token to add
957
+ <select id="addGmailTokenSelect" style="width:100%; padding:0.4rem; margin-top:0.25rem;"></select>
958
+ </label>
959
+ <div style="display:flex; gap:0.5rem; align-items:flex-end; padding-top:1.35rem;">
960
+ <button type="button" id="btnConfirmAddGmailAccount">Add</button>
961
+ <button type="button" id="btnCancelAddGmailAccount">Cancel</button>
962
+ </div>
963
+ </div>
964
+ <p id="addGmailAccountMsg" class="status" style="margin-top:0.5rem;"></p>
965
+ </div>
966
+ <p class="hint" style="font-size:0.85rem; color:#666; margin-top:0.5rem;">
967
+ Tokens are created with <code>fetch2gmail auth</code> (CLI). To add a second account, run it again with a different token filename (e.g. <code>--token token2.json</code>).
968
+ </p>
804
969
  </section>
805
970
 
806
971
  <section id="changePasswordSection" style="display:none">
@@ -851,6 +1016,7 @@ _APP_JS = r"""
851
1016
  (function() {
852
1017
  const api = function(path) { return fetch(path).then(function(r) { if (r.status === 401) { location.href = '/'; throw new Error('Unauthorized'); } return r.ok ? r.json() : r.json().then(function(j) { return Promise.reject(j); }); }); };
853
1018
  const put = function(path, body) { return fetch(path, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }).then(function(r) { if (r.status === 401) { location.href = '/'; throw new Error('Unauthorized'); } return r.ok ? r.json() : r.json().then(function(j) { return Promise.reject(j); }); }); };
1019
+ const del = function(path, body) { return fetch(path, { method: 'DELETE', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }).then(function(r) { if (r.status === 401) { location.href = '/'; throw new Error('Unauthorized'); } return r.ok ? r.json() : r.json().then(function(j) { return Promise.reject(j); }); }); };
854
1020
 
855
1021
  fetch('/api/setup/status').then(function(r) { return r.json(); }).then(function(st) {
856
1022
  if (!st.credentials_exist) {
@@ -869,6 +1035,55 @@ _APP_JS = r"""
869
1035
  if (params.get('error') === 'no_credentials') document.getElementById('subtitle').innerHTML = '<span class="error">Put credentials.json in the app folder and run fetch2gmail auth for token.json.</span>';
870
1036
 
871
1037
  function hideLoading() { var el = document.getElementById('loadingMsg'); if (el) el.style.display = 'none'; }
1038
+ function ensureGmailAccounts(c) {
1039
+ if (c.gmail_accounts && c.gmail_accounts.length) return c.gmail_accounts;
1040
+ // Back-compat: synthesize from single c.gmail
1041
+ if (c.gmail) return [c.gmail];
1042
+ return [];
1043
+ }
1044
+ function renderGmailAccounts(c) {
1045
+ var wrap = document.getElementById('gmailAccounts');
1046
+ if (!wrap) return;
1047
+ var accounts = ensureGmailAccounts(c);
1048
+ if (!accounts.length) {
1049
+ wrap.innerHTML = '<p class="error">No Gmail accounts configured.</p>';
1050
+ return;
1051
+ }
1052
+ var html = '<div style="margin-top:0.5rem;">';
1053
+ html += '<table style="width:100%; border-collapse:collapse;">';
1054
+ html += '<tr><th style="text-align:left; font-weight:600; padding:4px 0;">Account</th><th style="text-align:left; font-weight:600; padding:4px 0;">Token</th><th style="text-align:left; font-weight:600; padding:4px 0;">Label</th><th></th></tr>';
1055
+ for (var i = 0; i < accounts.length; i++) {
1056
+ var a = accounts[i];
1057
+ var token = (a.token_path || '').replace(/</g,'&lt;').replace(/>/g,'&gt;');
1058
+ var label = (a.label || '').replace(/</g,'&lt;').replace(/>/g,'&gt;');
1059
+ html += '<tr style="border-top:1px solid #eee;">';
1060
+ html += '<td style="padding:6px 0;">#' + (i + 1) + (i === 0 ? ' (primary)' : '') + '</td>';
1061
+ html += '<td style="padding:6px 0;"><code>' + token + '</code></td>';
1062
+ html += '<td style="padding:6px 0;"><input type="text" data-glabel="' + i + '" value="' + label + '" style="width: 95%;"></td>';
1063
+ html += '<td style="padding:6px 0; text-align:right;">' + (i === 0 ? '' : '<button type="button" data-gdel="' + i + '">Delete</button>') + '</td>';
1064
+ html += '</tr>';
1065
+ }
1066
+ html += '</table>';
1067
+ html += '<p class="hint" style="font-size:0.85rem; color:#666; margin:0.5rem 0 0;">Deleting a secondary account also deletes its token file (e.g. <code>token2.json</code>).</p>';
1068
+ html += '</div>';
1069
+ wrap.innerHTML = html;
1070
+ // Wire delete buttons
1071
+ var btns = wrap.querySelectorAll('button[data-gdel]');
1072
+ for (var j = 0; j < btns.length; j++) {
1073
+ btns[j].onclick = function(ev) {
1074
+ var idx = parseInt(ev.target.getAttribute('data-gdel'), 10);
1075
+ if (!confirm('Delete Gmail account #' + (idx + 1) + '?\\n\\nThis will remove it from config and delete its token file.')) return;
1076
+ del('/api/gmail/accounts', { index: idx }).then(function(r) {
1077
+ document.getElementById('status').textContent = r.message || 'Account deleted.';
1078
+ document.getElementById('status').className = 'status';
1079
+ return loadConfig().then(function(cc) { if (cc) { renderGmailAccounts(cc); } });
1080
+ }).catch(function(e) {
1081
+ document.getElementById('status').textContent = 'Error: ' + (typeof e.detail === 'string' ? e.detail : (e.detail && e.detail.detail) || e.message || 'Delete failed');
1082
+ document.getElementById('status').className = 'error';
1083
+ });
1084
+ };
1085
+ }
1086
+ }
872
1087
  function loadConfig() {
873
1088
  return api('/api/config').then(function(c) {
874
1089
  hideLoading();
@@ -905,6 +1120,7 @@ _APP_JS = r"""
905
1120
  if (c.gmail_connected) {
906
1121
  api('/api/gmail/email').then(function(d) { if (d.email) document.getElementById('gmailEmail').textContent = 'Connected as ' + d.email; }).catch(function() {});
907
1122
  }
1123
+ renderGmailAccounts(c);
908
1124
  return c;
909
1125
  }).catch(function(e) {
910
1126
  hideLoading();
@@ -1052,7 +1268,22 @@ _APP_JS = r"""
1052
1268
  document.getElementById('gmail_use_label').onchange = function() {
1053
1269
  document.getElementById('gmail_label_row').style.display = this.checked ? 'block' : 'none';
1054
1270
  };
1271
+ // Keep legacy "Label name" and table row 0 in sync (table is source of truth on save).
1272
+ document.getElementById('gmail_label').addEventListener('input', function() {
1273
+ var first = document.querySelector('#gmailAccounts input[data-glabel="0"]');
1274
+ if (first) first.value = this.value;
1275
+ });
1276
+ document.getElementById('gmailAccounts').addEventListener('input', function(ev) {
1277
+ var el = ev.target;
1278
+ if (el.getAttribute('data-glabel') === '0') document.getElementById('gmail_label').value = el.value;
1279
+ });
1055
1280
  document.getElementById('btnSave').onclick = function() {
1281
+ // Read current accounts from UI (labels only editable here).
1282
+ var currentAccounts = null;
1283
+ try {
1284
+ // use last loaded config from DOM if present
1285
+ // We'll rebuild from /api/config right before save to avoid stale token list
1286
+ } catch (e) {}
1056
1287
  var body = {
1057
1288
  imap_host: document.getElementById('imap_host').value,
1058
1289
  imap_port: parseInt(document.getElementById('imap_port').value, 10),
@@ -1067,7 +1298,22 @@ _APP_JS = r"""
1067
1298
  };
1068
1299
  var pw = document.getElementById('imap_password').value;
1069
1300
  if (pw) body.imap_password = pw;
1070
- put('/api/config', body).then(function() {
1301
+ // Merge updated labels into gmail_accounts and save canonical list.
1302
+ api('/api/config').then(function(c) {
1303
+ var accounts = ensureGmailAccounts(c);
1304
+ var labelInputs = document.querySelectorAll('#gmailAccounts input[data-glabel]');
1305
+ for (var i = 0; i < labelInputs.length; i++) {
1306
+ var idx = parseInt(labelInputs[i].getAttribute('data-glabel'), 10);
1307
+ if (accounts[idx]) accounts[idx].label = labelInputs[i].value;
1308
+ }
1309
+ // Table is single source of truth (legacy field is synced into table row 0 via listener below).
1310
+ // also apply global label toggle to all accounts (simple, predictable)
1311
+ for (var k = 0; k < accounts.length; k++) {
1312
+ accounts[k].use_label = document.getElementById('gmail_use_label').checked;
1313
+ }
1314
+ body.gmail_accounts = accounts;
1315
+ return put('/api/config', body);
1316
+ }).then(function() {
1071
1317
  document.getElementById('status').textContent = 'Config saved.';
1072
1318
  document.getElementById('status').className = 'status';
1073
1319
  if (pw) document.getElementById('imap_password').value = '';
@@ -1107,6 +1353,80 @@ _APP_JS = r"""
1107
1353
  msg.className = 'error';
1108
1354
  });
1109
1355
  };
1356
+
1357
+ var addBtn = document.getElementById('btnAddGmailAccount');
1358
+ if (addBtn) {
1359
+ addBtn.onclick = function() {
1360
+ var panel = document.getElementById('addGmailAccountPanel');
1361
+ var sel = document.getElementById('addGmailTokenSelect');
1362
+ var msg = document.getElementById('addGmailAccountMsg');
1363
+ var btnOk = document.getElementById('btnConfirmAddGmailAccount');
1364
+ var btnCancel = document.getElementById('btnCancelAddGmailAccount');
1365
+ if (!panel || !sel || !msg || !btnOk || !btnCancel) return;
1366
+ msg.textContent = 'Loading tokens...';
1367
+ msg.className = 'status';
1368
+ panel.style.display = 'block';
1369
+ sel.innerHTML = '';
1370
+ btnOk.disabled = true;
1371
+
1372
+ function closePanel() {
1373
+ panel.style.display = 'none';
1374
+ msg.textContent = '';
1375
+ sel.innerHTML = '';
1376
+ }
1377
+ btnCancel.onclick = closePanel;
1378
+
1379
+ api('/api/gmail/discover-tokens').then(function(tokens) {
1380
+ var available = tokens.filter(function(t) { return !t.in_use && t.token_path; });
1381
+ if (!available.length) {
1382
+ msg.textContent = 'No unused token*.json files found in the app folder. Run: fetch2gmail auth --token token2.json';
1383
+ msg.className = 'error';
1384
+ btnOk.disabled = true;
1385
+ return;
1386
+ }
1387
+ for (var i = 0; i < available.length; i++) {
1388
+ var t = available[i];
1389
+ var opt = document.createElement('option');
1390
+ opt.value = t.token_path;
1391
+ opt.textContent = t.token_path + (t.email ? (' (' + t.email + ')') : '');
1392
+ sel.appendChild(opt);
1393
+ }
1394
+ msg.textContent = 'Select a token and click Add.';
1395
+ msg.className = 'status';
1396
+ btnOk.disabled = false;
1397
+
1398
+ btnOk.onclick = function() {
1399
+ var chosen = sel.value;
1400
+ if (!chosen) return;
1401
+ btnOk.disabled = true;
1402
+ msg.textContent = 'Adding account...';
1403
+ msg.className = 'status';
1404
+ api('/api/config').then(function(c) {
1405
+ var accounts = ensureGmailAccounts(c);
1406
+ accounts.push({
1407
+ use_label: document.getElementById('gmail_use_label').checked,
1408
+ label: document.getElementById('gmail_label').value || 'ISP Mail',
1409
+ credentials_path: (accounts[0] && accounts[0].credentials_path) ? accounts[0].credentials_path : 'credentials.json',
1410
+ token_path: chosen
1411
+ });
1412
+ return put('/api/config', { gmail_accounts: accounts });
1413
+ }).then(function() {
1414
+ msg.textContent = 'Account added.';
1415
+ msg.className = 'ok';
1416
+ setTimeout(function() { closePanel(); loadConfig(); }, 600);
1417
+ }).catch(function(e) {
1418
+ msg.textContent = 'Error: ' + (typeof e.detail === 'string' ? e.detail : (e.detail && e.detail.detail) || e.message || 'Failed');
1419
+ msg.className = 'error';
1420
+ btnOk.disabled = false;
1421
+ });
1422
+ };
1423
+ }).catch(function() {
1424
+ msg.textContent = 'Could not discover tokens.';
1425
+ msg.className = 'error';
1426
+ btnOk.disabled = true;
1427
+ });
1428
+ };
1429
+ }
1110
1430
  })();
1111
1431
  """
1112
1432
 
File without changes
File without changes