Sample code for 30+ languages & platforms
Zig

Test for an IMAP Server Capability

See more IMAP Examples

Demonstrates the Chilkat Imap.HasCapability method, which tests whether a named capability appears in a raw capability response. The second argument is typically the string returned by the Capability method. This example checks whether the server supports the IDLE extension.

Background: Rather than scanning the raw CAPABILITY text yourself, HasCapability parses it for a specific token. This is the right guard before using any optional feature — for example, only entering an IDLE wait loop, or using the MOVE command, when the server actually advertises it. Fetch the capability string once and test it as many times as needed.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // Demonstrates the Imap.HasCapability method, which tests whether a named capability appears
    // in a raw capability response.  The 2nd argument is typically the string returned by the
    // Capability method.

    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;
    };

    // Get the raw capability response.
    const capabilities = imap.capability(alloc) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Test whether the server supports the IDLE extension.
    const has_idle = imap.hasCapability("IDLE", capabilities);
    if (has_idle) {
        std.debug.print("The server supports IDLE.\n", .{});
    } else {
        std.debug.print("The server does not support IDLE.\n", .{});
    }

    imap.disconnect() catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };
}