Something went wrong. Try again.
A Microsoft 365 mail client library for Zig, over Microsoft Graph
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! A Microsoft 365 mail client for Zig, over Microsoft Graph: folders,//! messages, the RFC 5322 bytes behind them, and the delta queries that turn a//! second run into an update rather than another full download.//!//! ## The short version//!//! ```zig//! const o365 = @import("o365");//!//! var client: o365.Client = .{ .gpa = gpa, .oauth = oauth, .tokens = &tokens };//! var d: o365.Client.Diagnostics = .init(client.gpa);//! defer d.deinit();//!//! var it = try client.messageIterator(gpa, .{//! .folder = "inbox",//! .select = o365.Client.walk_select,//! });//! defer it.deinit();//!//! while (try it.next(io, &d)) |stub| {//! const mime = try client.getMime(io, gpa, stub.id, &d);//! defer gpa.free(mime);//! // `mime` is the whole message, exactly as it arrived.//! }//! ```//!//! ## What is here//!//! - `Client` -- the API: the user, the folders, the message listing and fetch,//! the MIME content, the attachments, and the delta query. It refreshes the//! access token on its own.//! - `Message`, `MailFolder`, `User`, `Attachment` -- the resources, parsed//! leniently, because a Graph response carries only what `$select` asked for//! and Microsoft adds new members without warning.//! - `Batch` -- `$batch`: up to twenty requests in one round trip, which is//! what turns a forty-thousand-message walk from forty thousand round trips//! into two thousand. It retries the entries a batch reports as throttled,//! which nothing else can: inside a batch a 429 is a field in a *successful*//! response, so every layer below it is satisfied and the request is lost.//! - `ApiError` -- Graph's error object, and the questions worth asking of it://! is this worth retrying, is this the token, is this a permission, has a//! delta token aged out.//! - `tenant` -- where a tenant's OAuth endpoints are, since the identity//! platform puts the tenant in the path rather than having one pair of URLs.//! - `TokenStore` -- a refresh token in a 0600 file, so that a program works//! twice.//! - `scopes` -- the Graph permissions, spelled correctly, and a reminder that//! `offline_access` is not optional.//! - `iso8601`, `odata`, `json`, `http` -- the plumbing, exported because a//! caller building its own queries needs the first two.//!//! ## What is not//!//! **No OAuth.** Signing in is [zig-oidc]'s job, and this takes one of its//! `Client`s and one of its `TokenSet`s already filled in. Keeping them apart//! is what lets the flow be tested against something that is not Microsoft.//!//! **No writing.** No sending, no moving, no deleting, no drafts, no folder//! creation, no mailbox settings, no rules, no subscriptions. Reading a mailbox//! is what this does, and every request it makes is a GET -- which means a//! token scoped to `Mail.Read` is all it can use, and that is the point.//!//! No calendar and no contacts, though both are the same API shape and the//! plumbing here would serve them.//!//! ## How it differs from the Gmail sibling//!//! [zig-gmail] is the same idea against the other service, and four things are//! genuinely different rather than merely spelled differently://!//! - **A message is in one folder**, not carrying a set of labels. Moving it//! between folders **changes its id**; `internetMessageId` is what survives.//! - **Change tracking is per folder.** There is no mailbox-wide history feed,//! so a full sync is one delta walk and one stored token per folder.//! - **Pagination is by URL**, not by token: a page carries the complete URL of//! the next one, which is checked against `base_url` before it is followed.//! See `odata.sameOrigin` for why that check is not optional.//! - **Throttling carries `Retry-After`**, and Microsoft's guidance is that//! obeying it is the fastest way out, because usage keeps being logged//! against a client that comes back early. `http.zig` does.//!//! [zig-oidc]: https://git.jcollie.dev/jeff/zig-oidc//! [zig-gmail]: https://git.jcollie.dev/jeff/zig-gmail
const std = @import("std");
pub const ApiError = @import("ApiError.zig");pub const Attachment = @import("Attachment.zig");pub const Batch = @import("Batch.zig");pub const Client = @import("Client.zig");pub const MailFolder = @import("MailFolder.zig");pub const Message = @import("Message.zig");pub const TokenStore = @import("TokenStore.zig");pub const User = @import("User.zig");pub const http = @import("http.zig");pub const iso8601 = @import("iso8601.zig");pub const json = @import("json.zig");pub const odata = @import("odata.zig");pub const scopes = @import("scopes.zig");pub const tenant = @import("tenant.zig");
/// The `common` tenant -- work, school and personal accounts -- so that a/// caller need not import zig-oidc merely to name the provider these tokens/// have to come from.////// A single-tenant application wants `tenant.init` with its own tenant id/// instead; read the issuer caveat in `tenant.zig` before reading an ID token/// from either.pub const provider = tenant.common;
test { // One test executable covers one module, and a test is only compiled if // something references the file it is in. Without this, everything below // the root would silently never run. _ = ApiError; _ = Attachment; _ = Batch; _ = Client; _ = MailFolder; _ = Message; _ = TokenStore; _ = User; _ = http; _ = iso8601; _ = json; _ = odata; _ = scopes; _ = tenant;}