Spaces:
Running
Running
File size: 13,618 Bytes
c47ec10 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | from __future__ import annotations
import argparse
import imaplib
import json
import re
import sys
import urllib.error
import urllib.parse
import urllib.request
from dataclasses import dataclass
from email import message_from_bytes, policy
from email.header import decode_header, make_header
from email.message import Message
from email.utils import parsedate_to_datetime
from pathlib import Path
from typing import Any
TOKEN_URL = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
GRAPH_MESSAGES_URL = "https://graph.microsoft.com/v1.0/me/messages"
GRAPH_SCOPE = "offline_access https://graph.microsoft.com/Mail.Read"
IMAP_SCOPE = "offline_access https://outlook.office.com/IMAP.AccessAsUser.All"
DEFAULT_IMAP_HOST = "outlook.office365.com"
@dataclass(frozen=True)
class OutlookCredential:
email: str
password: str
client_id: str
refresh_token: str
line_number: int
@dataclass(frozen=True)
class HttpResponse:
status_code: int
text: str
def json(self) -> Any:
return json.loads(self.text)
def _clean(value: str) -> str:
return value.replace("\ufeff", "").replace("\u00a0", " ").strip()
def _redact_email(email: str) -> str:
local, sep, domain = email.partition("@")
if not sep:
return "***"
if len(local) <= 2:
masked = local[:1] + "***"
else:
masked = local[:2] + "***" + local[-1:]
return f"{masked}@{domain}"
def parse_credentials(path: Path) -> list[OutlookCredential]:
credentials: list[OutlookCredential] = []
for line_number, raw_line in enumerate(path.read_text(encoding="utf-8-sig").splitlines(), start=1):
line = _clean(raw_line)
if not line or "----" not in line:
continue
parts = [_clean(part) for part in line.split("----", 3)]
if len(parts) != 4:
continue
email, password, client_id, refresh_token = parts
if "@" not in email or not client_id or not refresh_token:
continue
credentials.append(
OutlookCredential(
email=email,
password=password,
client_id=client_id,
refresh_token=refresh_token,
line_number=line_number,
)
)
return credentials
def _http_request(
method: str,
url: str,
*,
headers: dict[str, str] | None = None,
data: dict[str, str] | None = None,
params: dict[str, Any] | None = None,
timeout: float = 30,
) -> HttpResponse:
target = url
if params:
query = urllib.parse.urlencode(params)
target = f"{url}?{query}"
body: bytes | None = None
request_headers = dict(headers or {})
if data is not None:
body = urllib.parse.urlencode(data).encode("utf-8")
request_headers.setdefault("Content-Type", "application/x-www-form-urlencoded")
request = urllib.request.Request(target, data=body, headers=request_headers, method=method.upper())
try:
with urllib.request.urlopen(request, timeout=timeout) as response:
return HttpResponse(
status_code=int(response.status),
text=response.read().decode("utf-8", errors="replace"),
)
except urllib.error.HTTPError as error:
return HttpResponse(
status_code=int(error.code),
text=error.read().decode("utf-8", errors="replace"),
)
def exchange_refresh_token(credential: OutlookCredential, scope: str, timeout: float) -> str:
response = _http_request(
"POST",
TOKEN_URL,
data={
"client_id": credential.client_id,
"grant_type": "refresh_token",
"refresh_token": credential.refresh_token,
"scope": scope,
},
timeout=timeout,
)
try:
data = response.json()
except Exception:
data = {}
if response.status_code != 200:
detail = data.get("error_description") or data.get("error") or response.text[:300]
raise RuntimeError(f"token refresh failed: HTTP {response.status_code}, {detail}")
access_token = str(data.get("access_token") or "").strip()
if not access_token:
raise RuntimeError("token refresh response did not include access_token")
return access_token
def _graph_sender(message: dict[str, Any]) -> str:
sender = message.get("from") or {}
if isinstance(sender, dict):
address = sender.get("emailAddress") or {}
if isinstance(address, dict):
return str(address.get("address") or address.get("name") or "")
return ""
def read_graph_messages(access_token: str, limit: int, timeout: float) -> list[dict[str, str]]:
response = _http_request(
"GET",
GRAPH_MESSAGES_URL,
headers={"Authorization": f"Bearer {access_token}", "Accept": "application/json"},
params={
"$top": max(1, min(limit, 50)),
"$orderby": "receivedDateTime desc",
"$select": "subject,receivedDateTime,from,bodyPreview",
},
timeout=timeout,
)
try:
data = response.json()
except Exception:
data = {}
if response.status_code != 200:
detail = data.get("error", {}).get("message") if isinstance(data.get("error"), dict) else response.text[:300]
raise RuntimeError(f"graph messages failed: HTTP {response.status_code}, {detail}")
items = data.get("value") if isinstance(data, dict) else None
if not isinstance(items, list):
raise RuntimeError("graph messages response did not include value[]")
return [
{
"received": str(item.get("receivedDateTime") or ""),
"from": _graph_sender(item),
"subject": str(item.get("subject") or ""),
"preview": str(item.get("bodyPreview") or ""),
}
for item in items
if isinstance(item, dict)
]
def _decode_header(value: str | None) -> str:
if not value:
return ""
try:
return str(make_header(decode_header(value)))
except Exception:
return value
def _message_text_preview(message: Message, limit: int = 240) -> str:
text_parts: list[str] = []
parts = message.walk() if message.is_multipart() else [message]
for part in parts:
if part.get_content_maintype() == "multipart":
continue
content_type = part.get_content_type()
if content_type not in {"text/plain", "text/html"}:
continue
try:
payload = part.get_content()
except Exception:
continue
if payload:
text_parts.append(str(payload))
if content_type == "text/plain" and text_parts:
break
text = "\n".join(text_parts)
text = re.sub(r"<[^>]+>", " ", text)
text = re.sub(r"\s+", " ", text).strip()
return text[:limit]
def _parse_imap_message(raw: bytes, include_preview: bool) -> dict[str, str]:
message = message_from_bytes(raw, policy=policy.default)
received = ""
try:
parsed = parsedate_to_datetime(str(message.get("Date") or ""))
received = parsed.isoformat()
except Exception:
received = str(message.get("Date") or "")
return {
"received": received,
"from": _decode_header(str(message.get("From") or "")),
"subject": _decode_header(str(message.get("Subject") or "")),
"preview": _message_text_preview(message) if include_preview else "",
}
def read_imap_messages(access_token: str, email: str, host: str, limit: int, include_preview: bool) -> list[dict[str, str]]:
auth_string = f"user={email}\x01auth=Bearer {access_token}\x01\x01"
mailbox = imaplib.IMAP4_SSL(host)
try:
mailbox.authenticate("XOAUTH2", lambda _: auth_string.encode("utf-8"))
status, _ = mailbox.select("INBOX", readonly=True)
if status != "OK":
raise RuntimeError("imap select INBOX failed")
status, data = mailbox.uid("search", None, "ALL")
if status != "OK" or not data or not data[0]:
return []
uids = data[0].split()[-max(1, limit) :]
messages: list[dict[str, str]] = []
for uid in reversed(uids):
status, fetched = mailbox.uid("fetch", uid, "(RFC822)")
if status != "OK":
continue
raw_payload = next((item[1] for item in fetched if isinstance(item, tuple) and isinstance(item[1], bytes)), b"")
if raw_payload:
messages.append(_parse_imap_message(raw_payload, include_preview))
return messages
finally:
try:
mailbox.logout()
except Exception:
pass
def print_messages(messages: list[dict[str, str]], include_preview: bool) -> None:
if not messages:
print(" no recent messages")
return
for index, message in enumerate(messages, start=1):
print(f" {index}. {message['received']} | {message['from']} | {message['subject']}")
if include_preview and message.get("preview"):
print(f" preview: {message['preview']}")
def test_credential(
credential: OutlookCredential,
mode: str,
limit: int,
timeout: float,
imap_host: str,
include_preview: bool,
show_email: bool,
) -> bool:
label = credential.email if show_email else _redact_email(credential.email)
print(f"[line {credential.line_number}] {label}")
errors: list[str] = []
if mode in {"graph", "auto"}:
try:
access_token = exchange_refresh_token(credential, GRAPH_SCOPE, timeout)
messages = read_graph_messages(access_token, limit, timeout)
print(" graph: ok")
print_messages(messages, include_preview)
return True
except Exception as error:
errors.append(f"graph: {error}")
if mode == "graph":
print(f" graph: failed - {error}")
return False
if mode in {"imap", "auto"}:
try:
access_token = exchange_refresh_token(credential, IMAP_SCOPE, timeout)
messages = read_imap_messages(access_token, credential.email, imap_host, limit, include_preview)
print(" imap: ok")
print_messages(messages, include_preview)
return True
except Exception as error:
errors.append(f"imap: {error}")
if mode == "imap":
print(f" imap: failed - {error}")
return False
print(" failed")
for error in errors:
print(f" - {error}")
return False
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="Test Outlook/Hotmail mailbox access from lines formatted as email----password----client_id----refresh_token.",
)
parser.add_argument("--file", default=r"D:\Desktop\yx.txt", help="Credential text file path.")
parser.add_argument("--mode", choices=("auto", "graph", "imap"), default="auto", help="Mailbox read method.")
parser.add_argument("--limit-accounts", type=int, default=1, help="How many accounts to test.")
parser.add_argument("--message-limit", type=int, default=5, help="How many recent messages to list per account.")
parser.add_argument("--timeout", type=float, default=30, help="HTTP request timeout in seconds.")
parser.add_argument("--imap-host", default=DEFAULT_IMAP_HOST, help="IMAP host for XOAUTH2 mode.")
parser.add_argument("--preview", action="store_true", help="Print body preview/snippet. Disabled by default.")
parser.add_argument("--show-email", action="store_true", help="Print full email address. Secrets are never printed.")
parser.add_argument("--json", action="store_true", help="Only parse the file and print non-secret account metadata as JSON.")
return parser
def main() -> int:
args = build_parser().parse_args()
path = Path(args.file)
if not path.exists():
print(f"file not found: {path}", file=sys.stderr)
return 2
credentials = parse_credentials(path)
if not credentials:
print(f"no valid credentials parsed from: {path}", file=sys.stderr)
return 2
limit_accounts = max(1, int(args.limit_accounts or 1))
selected = credentials[:limit_accounts]
if args.json:
print(
json.dumps(
[
{
"line": item.line_number,
"email": item.email if args.show_email else _redact_email(item.email),
"client_id": item.client_id,
"has_password": bool(item.password),
"has_refresh_token": bool(item.refresh_token),
}
for item in selected
],
ensure_ascii=False,
indent=2,
)
)
return 0
print(f"parsed {len(credentials)} credential(s), testing {len(selected)}")
success = 0
for credential in selected:
if test_credential(
credential=credential,
mode=str(args.mode),
limit=max(1, int(args.message_limit or 1)),
timeout=max(1.0, float(args.timeout or 30)),
imap_host=str(args.imap_host),
include_preview=bool(args.preview),
show_email=bool(args.show_email),
):
success += 1
print(f"summary: {success}/{len(selected)} account(s) readable")
return 0 if success == len(selected) else 1
if __name__ == "__main__":
raise SystemExit(main())
|