Zig
Zig
List and Inspect IMAP Mailboxes
See more IMAP Examples
Demonstrates the Chilkat Mailboxes class, a read-only result collection populated by Imap.MbxList. This example connects to an IMAP server, lists all mailboxes into a Mailboxes object, then loops from 0 to Count - 1 and inspects each mailbox: its name (GetName), whether it is selectable (IsSelectable) or marked (IsMarked), whether it may have child mailboxes (HasInferiors), a specific flag (HasFlag), and all of its flags both as a string (GetFlags) and individually (GetNumFlags with GetNthFlag). Finally it looks up a mailbox by name with GetMailboxIndex.
Background:
Mailboxes is how an application renders an IMAP folder tree. Because it is read-only, you never construct it directly — you receive it from MbxList and read its entries by index. Mailbox names come back as normal Unicode text (Chilkat handles the modified UTF-7 / IMAP UTF-8 encoding at the protocol level), so a name from GetName can be passed straight to methods like SelectMailbox. Note that HasInferiors is often true: it means children exist or may be created, which is not the same as \HasChildren.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// Demonstrates using Chilkat.Imap to populate a Mailboxes object (via MbxList), then looping
// over the collection to inspect each mailbox's name and attributes. Mailboxes is a read-only
// result collection -- applications inspect the entries but do not add, remove, or modify them.
const imap = try chilkat.Imap.init();
defer imap.deinit();
imap.setSsl(true);
imap.setPort(993);
imap.connect("imap.example.com") catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
imap.login("user@example.com", "myPassword") catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Populate a Mailboxes object with the full list of mailboxes. The first argument requests
// all mailboxes (not subscribed-only) and "*" matches any name.
const subscribed_only = false;
const mailboxes = try chilkat.Mailboxes.init();
defer mailboxes.deinit();
imap.mbxList(subscribed_only, "", "*", mailboxes) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Loop over each mailbox in the returned collection.
const n = mailboxes.getCount();
std.debug.print("Number of mailboxes: {d}\n", .{n});
var i: i32 = 0;
i = 0;
while (i < n) : (i += 1) {
const name = try mailboxes.getName(alloc, i);
std.debug.print("Mailbox: {s}\n", .{name});
// Whether the mailbox can be selected or examined for message access.
if (mailboxes.isSelectable(i)) {
std.debug.print(" Selectable: yes\n", .{});
} else {
std.debug.print(" Selectable: no\n", .{});
}
// Whether the server marked the mailbox as possibly having new messages.
if (mailboxes.isMarked(i)) {
std.debug.print(" Marked: yes\n", .{});
}
// Whether the mailbox has, or may contain, child mailboxes.
if (mailboxes.hasInferiors(i)) {
std.debug.print(" May have child mailboxes\n", .{});
}
// Check for a specific flag by name. Include the leading backslash for a system flag;
// in CkScript a literal backslash in a string is written as "\\".
if (mailboxes.hasFlag(i, "\\HasChildren")) {
std.debug.print(" Has child mailboxes\n", .{});
}
// Get all of the mailbox's flags as a comma-separated string.
const flags = try mailboxes.getFlags(alloc, i);
std.debug.print(" Flags: {s}\n", .{flags});
// Iterate the flags individually.
const num_flags = mailboxes.getNumFlags(i);
var j: i32 = 0;
j = 0;
while (j < num_flags) : (j += 1) {
const flag = try mailboxes.getNthFlag(alloc, i, j);
std.debug.print(" Flag {d}: {s}\n", .{ j, flag });
}
}
// Look up a mailbox by name. Returns -1 if no mailbox with that name is present.
const sent_index = mailboxes.getMailboxIndex("Sent");
if (sent_index >= 0) {
std.debug.print("The Sent mailbox is at index {d}\n", .{sent_index});
}
imap.disconnect() catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
}