| 1 | #!/opt/bin/perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | mcp-imap - inspect and manage one IMAP account through MCP | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | mcp-imap [OPTION]... | ||||||||||||||||||||||||||||||||||||||||||
| 12 | mcp-imap [OPTION]... login [method password|oauth2] [server HOST] | ||||||||||||||||||||||||||||||||||||||||||
| 13 | [user USER] [tls ssl|starttls] [port PORT] [oauth-KEY VALUE]... | ||||||||||||||||||||||||||||||||||||||||||
| 14 | mcp-imap [OPTION]... providers [SEARCH] | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | Each C<--name> selects one IMAP account and an independent MCP permission | ||||||||||||||||||||||||||||||||||||||||||
| 19 | profile. Configure the account with C<login>, then run the MCP server with | ||||||||||||||||||||||||||||||||||||||||||
| 20 | C<--stdio>, C<--connect>, or an HTTP listener. Login takes passwords from | ||||||||||||||||||||||||||||||||||||||||||
| 21 | the controlling terminal without echo, never from command-line arguments | ||||||||||||||||||||||||||||||||||||||||||
| 22 | or MCP tools. | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | The account is stored as C<imap-NAME.ini> under | ||||||||||||||||||||||||||||||||||||||||||
| 25 | C<$XDG_CONFIG_HOME/anyevent-mcp> or C<~/.config/anyevent-mcp>, mode 0600. | ||||||||||||||||||||||||||||||||||||||||||
| 26 | Password data is Base64 in that private file, not encrypted. Protect the | ||||||||||||||||||||||||||||||||||||||||||
| 27 | file like a password. Repeating C<login> replaces the saved password after | ||||||||||||||||||||||||||||||||||||||||||
| 28 | a successful connection. For C<login method oauth2>, it prints a browser | ||||||||||||||||||||||||||||||||||||||||||
| 29 | authorization URL and accepts the resulting redirect URL from the terminal, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | without listening on a callback port. The private file stores the refresh | ||||||||||||||||||||||||||||||||||||||||||
| 31 | token; later IMAP sessions refresh access tokens automatically. Neither | ||||||||||||||||||||||||||||||||||||||||||
| 32 | passwords nor OAuth tokens are accepted as command-line values. | ||||||||||||||||||||||||||||||||||||||||||
| 33 | Unencrypted IMAP authentication is not supported. | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | C<providers> searches Thunderbird's ISPDB domain index and OAuth provider | ||||||||||||||||||||||||||||||||||||||||||
| 36 | definitions. With no argument it lists known domains. A domain, substring, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | or full email address selects a provider and looks up provider-hosted HTTPS | ||||||||||||||||||||||||||||||||||||||||||
| 38 | autoconfig (preferred) or Thunderbird's ISPDB as a fallback. It prints | ||||||||||||||||||||||||||||||||||||||||||
| 39 | shell-quoted password login commands for supported IMAP/TLS alternatives, | ||||||||||||||||||||||||||||||||||||||||||
| 40 | provider setup instructions, and OAuth commands if a matching public OAuth | ||||||||||||||||||||||||||||||||||||||||||
| 41 | registration is known. An email address fills in the username. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | The commands are NOT executed. Thunderbird's client registrations may not | ||||||||||||||||||||||||||||||||||||||||||
| 43 | be usable by unrelated applications. OAuth authorization requests need | ||||||||||||||||||||||||||||||||||||||||||
| 44 | registered redirect URIs and may require provider-specific optional | ||||||||||||||||||||||||||||||||||||||||||
| 45 | C<oauth-auth-NAME VALUE> arguments (e.g. C<oauth-auth-access_type offline>). | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | OAuth2 login accepts C<oauth-client-id>, optional C<oauth-client-secret>, | ||||||||||||||||||||||||||||||||||||||||||
| 48 | C<oauth-authorization-endpoint>, C<oauth-token-endpoint>, C<oauth-scope>, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | and C<oauth-redirect-uri>. Optional C<oauth-client-auth> selects | ||||||||||||||||||||||||||||||||||||||||||
| 50 | C<none>, C<client_secret_post> (default when a secret is supplied) or | ||||||||||||||||||||||||||||||||||||||||||
| 51 | C<client_secret_basic>. C<oauth-pkce> defaults to C<1> (S256), or C<0> | ||||||||||||||||||||||||||||||||||||||||||
| 52 | when a provider registration does not support PKCE. C<oauth-sasl> selects | ||||||||||||||||||||||||||||||||||||||||||
| 53 | C<xoauth2> (default) or C<oauthbearer>, depending on the IMAP server. | ||||||||||||||||||||||||||||||||||||||||||
| 54 | Additional authorization query parameters use C<oauth-auth-NAME VALUE>. | ||||||||||||||||||||||||||||||||||||||||||
| 55 | Successful OAuth2 login requires a refresh token; provider-specific offline | ||||||||||||||||||||||||||||||||||||||||||
| 56 | access parameters may be necessary. Authorization is human-initiated only, | ||||||||||||||||||||||||||||||||||||||||||
| 57 | never exposed through an MCP tool. After authorization, the browser will | ||||||||||||||||||||||||||||||||||||||||||
| 58 | normally show an error page because no server is listening on the loopback | ||||||||||||||||||||||||||||||||||||||||||
| 59 | redirect URI. B<This is expected; copy the full redirect URL from the address | ||||||||||||||||||||||||||||||||||||||||||
| 60 | bar anyway.> Port 1 works with Chromium's error-page behavior, but provider | ||||||||||||||||||||||||||||||||||||||||||
| 61 | redirect registrations and other browsers may differ. | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | IMAP runs in a separate persistent synchronous worker, so an IMAP command | ||||||||||||||||||||||||||||||||||||||||||
| 64 | does not block the MCP event loop. One request at a time is processed by the | ||||||||||||||||||||||||||||||||||||||||||
| 65 | worker. A dropped connection during a write can leave an unknown outcome; | ||||||||||||||||||||||||||||||||||||||||||
| 66 | do not blindly retry such writes. | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | Message references consist of mailbox, UIDVALIDITY and UID. Read operations | ||||||||||||||||||||||||||||||||||||||||||
| 69 | use BODY.PEEK, never mark messages Seen. Deleted flags do not automatically | ||||||||||||||||||||||||||||||||||||||||||
| 70 | expunge. EXPUNGE is explicit. Selecting another mailbox or logging out does | ||||||||||||||||||||||||||||||||||||||||||
| 71 | not expunge. Search is bounded by UID windows, potentially returning a | ||||||||||||||||||||||||||||||||||||||||||
| 72 | partial/empty page with a continuation cursor when matches are sparse. | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | =head1 FIRST USE | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | Discover your provider's settings before logging in: | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | mcp-imap --name personal providers alice@example.net | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | Replace the example address. This reports possible IMAP settings and | ||||||||||||||||||||||||||||||||||||||||||
| 81 | login commands; provider-specific OAuth client registrations may be | ||||||||||||||||||||||||||||||||||||||||||
| 82 | restricted to their original application. For a password-based provider, | ||||||||||||||||||||||||||||||||||||||||||
| 83 | for example: | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | mcp-imap --name personal login method password \ | ||||||||||||||||||||||||||||||||||||||||||
| 86 | server imap.example.net user alice@example.net tls ssl port 993 | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | The password is read interactively. For OAuth2, run C<login method oauth2> | ||||||||||||||||||||||||||||||||||||||||||
| 89 | with your provider-approved client ID, authorization/token endpoints, | ||||||||||||||||||||||||||||||||||||||||||
| 90 | scope and redirect URI. For example, the structure of such a command | ||||||||||||||||||||||||||||||||||||||||||
| 91 | (the provider URLs and registration below are placeholders) is: | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | mcp-imap --name personal login method oauth2 \ | ||||||||||||||||||||||||||||||||||||||||||
| 94 | server imap.example.net user alice@example.net tls ssl port 993 \ | ||||||||||||||||||||||||||||||||||||||||||
| 95 | oauth-client-id REGISTERED_CLIENT_ID \ | ||||||||||||||||||||||||||||||||||||||||||
| 96 | oauth-authorization-endpoint https://login.example.net/authorize \ | ||||||||||||||||||||||||||||||||||||||||||
| 97 | oauth-token-endpoint https://login.example.net/token \ | ||||||||||||||||||||||||||||||||||||||||||
| 98 | oauth-scope MAIL_SCOPE \ | ||||||||||||||||||||||||||||||||||||||||||
| 99 | oauth-redirect-uri http://127.0.0.1:1 | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | The redirect URI must be allowed by the OAuth client registration. Some | ||||||||||||||||||||||||||||||||||||||||||
| 102 | providers additionally require C<oauth-auth-access_type offline> or | ||||||||||||||||||||||||||||||||||||||||||
| 103 | other authorization parameters to issue a refresh token. After opening | ||||||||||||||||||||||||||||||||||||||||||
| 104 | the printed authorization URL, authorize the intended account and paste | ||||||||||||||||||||||||||||||||||||||||||
| 105 | the B<full> redirect URL from the browser's address bar even if the browser | ||||||||||||||||||||||||||||||||||||||||||
| 106 | reports an unreachable loopback page. Login checks IMAP authentication | ||||||||||||||||||||||||||||||||||||||||||
| 107 | before saving the account. | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | Then configure your MCP client to start: | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | mcp-imap --name personal --stdio | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | Or expose it through a multibroker: | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | mcp-multibroker --stdio mail --- mcp-imap --name personal --stdio --- | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | Ask the assistant: | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | List my subscribed mailboxes, then show the ten newest subjects | ||||||||||||||||||||||||||||||||||||||||||
| 120 | and senders in INBOX. Do not modify flags or move messages. | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | For example, to subscribe a folder called C<Receipts>, invoke C<mailbox> | ||||||||||||||||||||||||||||||||||||||||||
| 123 | with C<{"operation":"subscribe","mailbox":"Receipts"}>, then call C<lsub> | ||||||||||||||||||||||||||||||||||||||||||
| 124 | to see subscribed folders. Read operations request C<read> permission; | ||||||||||||||||||||||||||||||||||||||||||
| 125 | changing subscriptions requires C<manage>. A mailbox-specific permission | ||||||||||||||||||||||||||||||||||||||||||
| 126 | prompt can allow just that mailbox or all mailboxes. | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | To let a separately started server join an existing multibroker with | ||||||||||||||||||||||||||||||||||||||||||
| 129 | an HTTP/extension listener, run: | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | mcp-imap --name personal --connect http://127.0.0.1:31740/TOKEN/mail | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | Replace C<TOKEN> with the token obtained using the multibroker's | ||||||||||||||||||||||||||||||||||||||||||
| 134 | C<--print-url>. Use C<https://> with a trusted, matching certificate. | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | The C<lsub> tool lists subscribed mailboxes; C<mailboxes> lists all | ||||||||||||||||||||||||||||||||||||||||||
| 137 | visible mailboxes. Subscriptions affect folder selection in clients, | ||||||||||||||||||||||||||||||||||||||||||
| 138 | not message delivery or permissions. See | ||||||||||||||||||||||||||||||||||||||||||
| 139 | L<AnyEvent::MCP::Intro/Read and organize mail> for search and safety | ||||||||||||||||||||||||||||||||||||||||||
| 140 | examples. | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | =head1 TOOLS | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | =item mailboxes | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | List visible mailboxes. The C<mailboxes> result contains triples of | ||||||||||||||||||||||||||||||||||||||||||
| 149 | C<[attributes, hierarchy_separator, name]>. One entry might be | ||||||||||||||||||||||||||||||||||||||||||
| 150 | C<[["\\HasNoChildren"], "/", "INBOX"]>. Mailboxes marked C<\\Noselect> | ||||||||||||||||||||||||||||||||||||||||||
| 151 | are containers, not selectable message folders. | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | =item lsub | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | List subscribed mailboxes (IMAP LSUB). Uses the same result format as C<mailboxes>. | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | =item messages | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | List at most 100 message summaries, newest UID first. Optional SEARCH | ||||||||||||||||||||||||||||||||||||||||||
| 160 | filter, summary fields, header names and UIDVALIDITY-aware cursor. | ||||||||||||||||||||||||||||||||||||||||||
| 161 | C<incomplete> means the search reached its 20000-UID scan budget: | ||||||||||||||||||||||||||||||||||||||||||
| 162 | continue with C<next_cursor> even if the page contains no messages. | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | C<filter> combines its keys with AND. Text search keys are C<from>, C<to>, | ||||||||||||||||||||||||||||||||||||||||||
| 165 | C<cc>, C<bcc>, C<subject>, C<body> and C<text> (headers and body). | ||||||||||||||||||||||||||||||||||||||||||
| 166 | C<header> maps header names to search strings. Date keys are C<since>, | ||||||||||||||||||||||||||||||||||||||||||
| 167 | C<before>, C<on>, C<sent_since>, C<sent_before> and C<sent_on>, with dates | ||||||||||||||||||||||||||||||||||||||||||
| 168 | as C<D-Mon-YYYY>. C<since>/C<before>/C<on> use the IMAP internal date; | ||||||||||||||||||||||||||||||||||||||||||
| 169 | C<sent_*> keys use the message's Date header. C<larger> and C<smaller> | ||||||||||||||||||||||||||||||||||||||||||
| 170 | filter by message size. Boolean flags include C<seen>, C<unseen>, | ||||||||||||||||||||||||||||||||||||||||||
| 171 | C<answered>, C<unanswered>, C<flagged>, C<unflagged>, C<deleted>, | ||||||||||||||||||||||||||||||||||||||||||
| 172 | C<undeleted>, C<draft>, C<undraft>, C<recent>, C<old> and C<new>. | ||||||||||||||||||||||||||||||||||||||||||
| 173 | C<all> and C<any> contain arrays of nested filters (AND and OR); | ||||||||||||||||||||||||||||||||||||||||||
| 174 | C<not> contains one nested filter. For example: | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | { | ||||||||||||||||||||||||||||||||||||||||||
| 177 | "mailbox": "INBOX", | ||||||||||||||||||||||||||||||||||||||||||
| 178 | "limit": 20, | ||||||||||||||||||||||||||||||||||||||||||
| 179 | "filter": { | ||||||||||||||||||||||||||||||||||||||||||
| 180 | "since": "1-Nov-2026", | ||||||||||||||||||||||||||||||||||||||||||
| 181 | "any": [ | ||||||||||||||||||||||||||||||||||||||||||
| 182 | {"from": "alice@example.net"}, | ||||||||||||||||||||||||||||||||||||||||||
| 183 | {"subject": "invoice", "body": "overdue"} | ||||||||||||||||||||||||||||||||||||||||||
| 184 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 185 | "not": {"flagged": true} | ||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | The example finds messages with an internal date since November 1 from | ||||||||||||||||||||||||||||||||||||||||||
| 190 | Alice or containing both invoice in the subject and overdue in the body, | ||||||||||||||||||||||||||||||||||||||||||
| 191 | excluding flagged mail. Filter nesting and size are bounded; search | ||||||||||||||||||||||||||||||||||||||||||
| 192 | values are explicitly quoted as IMAP arguments, never interpolated into | ||||||||||||||||||||||||||||||||||||||||||
| 193 | raw SEARCH commands. | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | =item message | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | Read ENVELOPE, flags, dates, size and MIME BODYSTRUCTURE for a UID. | ||||||||||||||||||||||||||||||||||||||||||
| 198 | Use the C<uidvalidity> and C<uid> returned by C<messages> for the selected | ||||||||||||||||||||||||||||||||||||||||||
| 199 | mailbox. For example, if the current UIDVALIDITY is 13 and the UID is 42: | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | {"mailbox": "INBOX", "uidvalidity": "13", "uid": 42} | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | =item part | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | Read a MIME part using BODY.PEEK with a byte ceiling. Returns Base64 | ||||||||||||||||||||||||||||||||||||||||||
| 206 | raw transferred bytes; C<decode_text> additionally decodes complete text | ||||||||||||||||||||||||||||||||||||||||||
| 207 | parts using MIME transfer encoding and charset. | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | =item raw | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | Read raw RFC 5322 message bytes (bounded, Base64). Never sets Seen. | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | =item flags | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | Add/remove a set of IMAP flags, including Deleted (but never expunge). | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | =item copy, move | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | Copy or move named UIDs. MOVE requires server MOVE capability and has no | ||||||||||||||||||||||||||||||||||||||||||
| 220 | unsafe emulation. | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | =item append | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | APPEND a Base64-encoded RFC 5322 message up to 1 MiB. Use CRLF line | ||||||||||||||||||||||||||||||||||||||||||
| 225 | endings. Reject unescaped mbox From_ separator-like lines in the raw | ||||||||||||||||||||||||||||||||||||||||||
| 226 | message unless C<--allow-mbox-from> is set. Refusal identifies the raw | ||||||||||||||||||||||||||||||||||||||||||
| 227 | line number and explains the mboxo hazard. This uploads bytes unchanged; | ||||||||||||||||||||||||||||||||||||||||||
| 228 | it is not an SMTP send operation. | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | =item expunge | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | Permanently expunge explicitly named deleted UIDs when UIDPLUS is available, | ||||||||||||||||||||||||||||||||||||||||||
| 233 | or all Deleted messages in the selected mailbox when C<uids> is omitted. | ||||||||||||||||||||||||||||||||||||||||||
| 234 | Never fall back from UID EXPUNGE to whole-mailbox EXPUNGE. | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | =item mailbox | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | Create, rename, delete, subscribe or unsubscribe a mailbox. | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | =head1 PERMISSIONS | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | The independent profile is C<imap-NAME>, with C<read>, C<modify>, | ||||||||||||||||||||||||||||||||||||||||||
| 245 | C<manage> and C<expunge> actions. Each tool checks permission before | ||||||||||||||||||||||||||||||||||||||||||
| 246 | network activity. Mailbox-specific operations can be approved for one | ||||||||||||||||||||||||||||||||||||||||||
| 247 | mailbox or all mailboxes; C<mailboxes> and C<lsub> list folders without | ||||||||||||||||||||||||||||||||||||||||||
| 248 | selecting a mailbox. This worker is NOT filesystem- or network-sandboxed; | ||||||||||||||||||||||||||||||||||||||||||
| 249 | permissions authorize operations, not OS-level confinement. | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | =head1 OPTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | Shared switches are documented in | ||||||||||||||||||||||||||||||||||||||||||
| 254 | L<AnyEvent::MCP::Server::Simple/COMMON COMMAND-LINE OPTIONS>. | ||||||||||||||||||||||||||||||||||||||||||
| 255 | The following switches are specific to C<mcp-imap>. | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | =item --ask MODE | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | Choose where permission prompts appear: C<helper> (default when run | ||||||||||||||||||||||||||||||||||||||||||
| 262 | independently) uses a local helper; C<tty> and C<yad> force those | ||||||||||||||||||||||||||||||||||||||||||
| 263 | local interfaces without fallback. C<mcp> delegates prompts to the | ||||||||||||||||||||||||||||||||||||||||||
| 264 | MCP client; C<auto> uses client elicitation when advertised and | ||||||||||||||||||||||||||||||||||||||||||
| 265 | otherwise falls back to the helper. A server spawned by a multibroker | ||||||||||||||||||||||||||||||||||||||||||
| 266 | defaults to C<mcp> so the broker can present its prompts. | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | =item --allow-mbox-from | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | Allow APPEND messages with unescaped mbox From_ separator-like lines. | ||||||||||||||||||||||||||||||||||||||||||
| 271 | Unsafe with mboxo servers which fail to escape these lines. Off by default; | ||||||||||||||||||||||||||||||||||||||||||
| 272 | can only be enabled by the server operator, not through an MCP tool. | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | =item --provider-file FILE | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | Use FILE as a local Thunderbird OAuth JavaScript source with the | ||||||||||||||||||||||||||||||||||||||||||
| 277 | C<providers> command. Invalid for normal server startup or C<login>. | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | =item --ispdb-file FILE | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | Use FILE as a local Thunderbird autoconfiguration XML source with the | ||||||||||||||||||||||||||||||||||||||||||
| 282 | C<providers> command. Invalid for normal server startup or C<login>. | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | 6 | ▶ | use common::sense; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 6 | ▶ | use MIME::Base64 (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 6 | ▶ | use AnyEvent (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | 6 | ▶ | use AnyEvent::Fork::Early; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | 3 | ▶ | use Term::ReadKey (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 3 | ▶ | use AnyEvent::Fork (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 3 | ▶ | use AnyEvent::Fork::RPC (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | 3 | ▶ | use AnyEvent::MCP (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | 3 | ▶ | use AnyEvent::MCP::File (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | 3 | ▶ | use AnyEvent::MCP::Config (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | 3 | ▶ | use AnyEvent::MCP::Server::Permissions (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 3 | ▶ | use JSON::XS (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | 3 | ▶ | use AnyEvent::MCP::Server::Simple; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | 3 | my $options = mcp_getoptions | |||||||||||||||||||||||||||||||||||||||||
| 306 | { | ||||||||||||||||||||||||||||||||||||||||||
| 307 | usage => "Usage: mcp-imap [OPTION]... [login [key value ...] | providers [SEARCH]]\n", | ||||||||||||||||||||||||||||||||||||||||||
| 308 | description => "Serve one named IMAP account; 'login' configures password or OAuth2; 'providers' discovers mail setup.", | ||||||||||||||||||||||||||||||||||||||||||
| 309 | options => [["--ask MODE", "Permission prompts: helper (default), tty, yad, mcp, auto"], | ||||||||||||||||||||||||||||||||||||||||||
| 310 | ["--allow-mbox-from", "Allow potentially unsafe unescaped mbox From_ lines in APPEND"], | ||||||||||||||||||||||||||||||||||||||||||
| 311 | ["--provider-file FILE", "Use local Thunderbird OAuth source with providers"], | ||||||||||||||||||||||||||||||||||||||||||
| 312 | ["--ispdb-file FILE", "Use local autoconfig XML with providers"]], | ||||||||||||||||||||||||||||||||||||||||||
| 313 | examples => " mcp-imap --name doom login server doom user root tls starttls\n" | ||||||||||||||||||||||||||||||||||||||||||
| 314 | . " mcp-imap --name gmail providers gmail\n" | ||||||||||||||||||||||||||||||||||||||||||
| 315 | . " mcp-imap --name doom --stdio", | ||||||||||||||||||||||||||||||||||||||||||
| 316 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 317 | "ask=s" => \my $ask, | ||||||||||||||||||||||||||||||||||||||||||
| 318 | "allow-mbox-from!" => \my $allow_mbox_from, | ||||||||||||||||||||||||||||||||||||||||||
| 319 | "provider-file=s" => \my $provider_file, | ||||||||||||||||||||||||||||||||||||||||||
| 320 | "ispdb-file=s" => \my $ispdb_file, | ||||||||||||||||||||||||||||||||||||||||||
| 321 | ; | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | 3 | my $name = $options->{name}; | |||||||||||||||||||||||||||||||||||||||||
| 324 | 3 | ▶ | if (@ARGV && $ARGV[0] eq "providers") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@ARGV and $ARGV[0] eq "providers"
MC/DC0%0/2
@ARGV and $ARGV[0] eq "providers"
@ARGV
$ARGV[0] eq "providers"
Truth table33%1/3
A and B
A= @ARGVB= $ARGV[0] eq "providers"
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | 3 | shift @ARGV; | |||||||||||||||||||||||||||||||||||||||||
| 326 | 3 | require AnyEvent::MCP::IMAP::Providers; | |||||||||||||||||||||||||||||||||||||||||
| 327 | 3 | AnyEvent::MCP::IMAP::Providers::run ($name, $provider_file, $ispdb_file, @ARGV); | |||||||||||||||||||||||||||||||||||||||||
| 328 | 3 | exit; | |||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | ▶ | (defined $provider_file || defined $ispdb_file) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $provider_file or defined $ispdb_file
MC/DC0%0/2
defined $provider_file or defined $ispdb_file
defined $provider_file
defined $ispdb_file
Truth table0%0/3
A or B
A= defined $provider_fileB= defined $ispdb_file
| |||||||||||||||||||||||||||||||||||||||||||
| 331 | and die "--provider-file and --ispdb-file are only for providers\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | ▶ | my $directory = ($ENV{XDG_CONFIG_HOME} || ($ENV{HOME} || die "HOME is not set\n") . "/.config") | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/4
$ENV{'XDG_CONFIG_HOME'} || ($ENV{'HOME'} || die("HOME is not set\n")) . '/.config'
$ENV{'HOME'} || die("HOME is not set\n")
MC/DC0%0/1
$ENV{'XDG_CONFIG_HOME'} || ($ENV{'HOME'} || die("HOME is not set\n")) . '/.config'
$ENV{'XDG_CONFIG_HOME'}
Truth table0%0/2
A
A= $ENV{'XDG_CONFIG_HOME'}
| |||||||||||||||||||||||||||||||||||||||||||
| 334 | . "/anyevent-mcp"; | ||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | my $file = "$directory/imap-$name.ini"; | |||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | ▶ | sub load_account { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load_account: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | ▶ | return {} unless -e $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | ▶ | open my $fh, "<:raw", $file or die "$file: $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | my $text = <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 343 | 0 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | my %kv; | |||||||||||||||||||||||||||||||||||||||||
| 345 | 0 | ▶ | AnyEvent::MCP::parse_ini $text, sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | my ($section, $values) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 347 | 0 | ▶ | $section eq "" || $section eq "account" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$section eq "" or $section eq "account"
MC/DC0%0/2
$section eq "" or $section eq "account"
$section eq ""
$section eq "account"
Truth table0%0/3
A or B
A= $section eq ""B= $section eq "account"
| |||||||||||||||||||||||||||||||||||||||||||
| 348 | or die "$file: unexpected section $section\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | %kv = (%kv, %$values); | |||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | }, $file; | |||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | \%kv | |||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | ▶ | sub settings { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
settings: not called CC=20 SCAR=60.4
| |||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | my ($kv) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | 0 | for (qw(server user tls port)) { | |||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | ▶ | defined $kv->{$_} && length $kv->{$_} or die "$file: missing $_; run mcp-imap --name $name login\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $kv->{$_} and length $kv->{$_}
MC/DC0%0/2
defined $kv->{$_} and length $kv->{$_}
defined $kv->{$_}
length $kv->{$_}
Truth table0%0/3
A and B
A= defined $kv->{$_}B= length $kv->{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | ▶ | $kv->{tls} =~ /\A(?:ssl|starttls)\z/ or die "$file: invalid tls mode\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | ▶ | $kv->{port} =~ /\A[0-9]+\z/ && $kv->{port} >= 1 && $kv->{port} <= 65535 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$kv->{"port"} =~ /\A[0-9]+\z/u and $kv->{"port"} >= 1 and $kv->{"port"} <= 65535
$kv->{"port"} =~ /\A[0-9]+\z/u and $kv->{"port"} >= 1
MC/DC0%0/3
$kv->{"port"} =~ /\A[0-9]+\z/u and $kv->{"port"} >= 1 and $kv->{"port"} <= 65535
$kv->{"port"} =~ /\A[0-9]+\z/u
$kv->{"port"} >= 1
$kv->{"port"} <= 65535
Truth table0%0/4
A and B and C
A= $kv->{"port"} =~ /\A[0-9]+\z/uB= $kv->{"port"} >= 1C= $kv->{"port"} <= 65535
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | or die "$file: invalid port\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 364 | 0 | ▶ | $kv->{server} !~ /[\s\x00-\x1f]/ && length $kv->{server} <= 255 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $kv->{"server"} =~ /[\s\x00-\x1f]/u and length $kv->{"server"} <= 255
MC/DC0%0/2
not $kv->{"server"} =~ /[\s\x00-\x1f]/u and length $kv->{"server"} <= 255
not $kv->{"server"} =~ /[\s\x00-\x1f]/u
length $kv->{"server"} <= 255
Truth table0%0/3
not(A) and B
A= not $kv->{"server"} =~ /[\s\x00-\x1f]/uB= length $kv->{"server"} <= 255
| |||||||||||||||||||||||||||||||||||||||||||
| 365 | or die "$file: invalid server\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | ▶ | $kv->{user} !~ /[\r\n\x00]/ or die "$file: invalid user\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | 0 | my %connection = map +($_ => $kv->{$_}), qw(server user port tls); | |||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | ▶ | my $method = $kv->{method} || "password"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$kv->{'method'} || 'password'
MC/DC0%0/1
$kv->{'method'} || 'password'
$kv->{'method'}
Truth table0%0/2
A
A= $kv->{'method'}
| |||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | ▶ | if ($method eq "oauth2") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | require AnyEvent::MCP::IMAP::OAuth2; | |||||||||||||||||||||||||||||||||||||||||
| 372 | 0 | AnyEvent::MCP::IMAP::OAuth2::validate ($kv); | |||||||||||||||||||||||||||||||||||||||||
| 373 | defined $kv->{"oauth-refresh-token-base64"} | ||||||||||||||||||||||||||||||||||||||||||
| 374 | && $kv->{"oauth-refresh-token-base64"} =~ /\A[A-Za-z0-9+\/]*={0,2}\z/ | ||||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | ▶ | && length $kv->{"oauth-refresh-token-base64"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $kv->{"oauth-refresh-token-base64"} and $kv->{"oauth-refresh-token-base64"} =~ m[\A[A-Za-z0-9+/]*={0,2}\z]u and length $kv->{"oauth-refresh-token-base64"}
defined $kv->{"oauth-refresh-token-base64"} and $kv->{"oauth-refresh-token-base64"} =~ m[\A[A-Za-z0-9+/]*={0,2}\z]u
MC/DC0%0/3
defined $kv->{"oauth-refresh-token-base64"} and $kv->{"oauth-refresh-token-base64"} =~ m[\A[A-Za-z0-9+/]*={0,2}\z]u and length $kv->{"oauth-refresh-token-base64"}
defined $kv->{"oauth-refresh-token-base64"}
$kv->{"oauth-refresh-token-base64"} =~ m[\A[A-Za-z0-9+/]*={0,2}\z]u
length $kv->{"oauth-refresh-token-base64"}
Truth table0%0/4
A and B and C
A= defined $kv->{"oauth-refresh-token-base64"}B= $kv->{"oauth-refresh-token-base64"} =~ m[\A[A-Za-z0-9+/]*={0,2}\z]uC= length $kv->{"oauth-refresh-token-base64"}
| |||||||||||||||||||||||||||||||||||||||||||
| 376 | or die "$file: missing or invalid OAuth refresh token; run mcp-imap login\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 377 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | %connection, method => "oauth2", account_file => $file, oauth => { %$kv }, | |||||||||||||||||||||||||||||||||||||||||
| 379 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | ▶ | $method eq "password" or die "$file: invalid login method\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | defined $kv->{"password-base64"} | ||||||||||||||||||||||||||||||||||||||||||
| 384 | && $kv->{"password-base64"} =~ /\A[A-Za-z0-9+\/]*={0,2}\z/ | ||||||||||||||||||||||||||||||||||||||||||
| 385 | 0 | ▶ | && length $kv->{"password-base64"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $kv->{"password-base64"} and $kv->{"password-base64"} =~ m[\A[A-Za-z0-9+/]*={0,2}\z]u and length $kv->{"password-base64"}
defined $kv->{"password-base64"} and $kv->{"password-base64"} =~ m[\A[A-Za-z0-9+/]*={0,2}\z]u
MC/DC0%0/3
defined $kv->{"password-base64"} and $kv->{"password-base64"} =~ m[\A[A-Za-z0-9+/]*={0,2}\z]u and length $kv->{"password-base64"}
defined $kv->{"password-base64"}
$kv->{"password-base64"} =~ m[\A[A-Za-z0-9+/]*={0,2}\z]u
length $kv->{"password-base64"}
Truth table0%0/4
A and B and C
A= defined $kv->{"password-base64"}B= $kv->{"password-base64"} =~ m[\A[A-Za-z0-9+/]*={0,2}\z]uC= length $kv->{"password-base64"}
| |||||||||||||||||||||||||||||||||||||||||||
| 386 | or die "$file: missing or invalid password encoding\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 387 | +{ | ||||||||||||||||||||||||||||||||||||||||||
| 388 | %connection, method => "password", | ||||||||||||||||||||||||||||||||||||||||||
| 389 | 0 | password => MIME::Base64::decode_base64 ($kv->{"password-base64"}), | |||||||||||||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | 0 | ▶ | sub login { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
login: not called CC=27 SCAR=66.3
| |||||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | ▶ | @ARGV % 2 == 0 or die "login expects key/value pairs\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 395 | 0 | my %input; | |||||||||||||||||||||||||||||||||||||||||
| 396 | 0 | ▶ | while (@ARGV) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 397 | 0 | my ($key, $value) = splice @ARGV, 0, 2; | |||||||||||||||||||||||||||||||||||||||||
| 398 | 0 | ▶ | $key =~ /\A(?:method|server|user|tls|port|oauth-(?:client-id|client-secret|client-auth|authorization-endpoint|token-endpoint|scope|pkce|redirect-uri|sasl|auth-[A-Za-z0-9_]+))\z/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 399 | or die "Unknown login key '$key' (passwords and tokens cannot be given on argv)\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 400 | 0 | ▶ | $value !~ /[\r\n\x00]/ or die "Invalid $key\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 401 | 0 | $input{$key} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||||||||||
| 403 | 0 | my $old = load_account (); | |||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | ▶ | my $method = $input{method} || $old->{method} || "password"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
$input{'method'} || $old->{'method'} || 'password'
$input{"method"} or $old->{"method"}
MC/DC0%0/2
$input{'method'} || $old->{'method'} || 'password'
$input{"method"}
$old->{"method"}
Truth table0%0/3
A or B
A= $input{"method"}B= $old->{"method"}
| |||||||||||||||||||||||||||||||||||||||||||
| 405 | 0 | ▶ | $method =~ /\A(?:password|oauth2)\z/ or die "Unknown login method\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | my %kv = %$old; | |||||||||||||||||||||||||||||||||||||||||
| 408 | %kv = map +(exists $old->{$_} ? ($_ => $old->{$_}) : ()), qw(server user tls port) | ||||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | ▶ | if ($old->{method} || "password") ne $method; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/2
$old->{'method'} || 'password'
MC/DC0%0/1
$old->{'method'} || 'password'
$old->{'method'}
Truth table0%0/2
A
A= $old->{'method'}
| |||||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | %kv = (%kv, %input); | |||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | $kv{method} = $method; | |||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | ▶ | delete $kv{"password-base64"} if $method eq "oauth2"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | ▶ | delete @kv{grep /^oauth-/, keys %kv} if $method eq "password"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | for my $key (qw(server user)) { | |||||||||||||||||||||||||||||||||||||||||
| 416 | 0 | ▶ | next if defined $kv{$key} && length $kv{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $kv{$key} and length $kv{$key}
MC/DC0%0/2
defined $kv{$key} and length $kv{$key}
defined $kv{$key}
length $kv{$key}
Truth table0%0/3
A and B
A= defined $kv{$key}B= length $kv{$key}
| |||||||||||||||||||||||||||||||||||||||||||
| 417 | 0 | print STDERR "$key: "; | |||||||||||||||||||||||||||||||||||||||||
| 418 | 0 | ▶ | chomp ($kv{$key} = <STDIN> // ""); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
readline STDIN // ""
MC/DC0%0/1
readline STDIN // ""
readline STDIN
Truth table0%0/2
A
A= readline STDIN
| |||||||||||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||||||||||
| 420 | 0 | ▶ | $kv{tls} ||= "ssl"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$kv{"tls"} ||= "ssl"
MC/DC0%0/1
$kv{"tls"} ||= "ssl"
$kv{"tls"}
Truth table0%0/2
A
A= $kv{"tls"}
| |||||||||||||||||||||||||||||||||||||||||||
| 421 | 0 | ▶ | $kv{port} ||= $kv{tls} eq "ssl" ? 993 : 143; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$kv{"port"} ||= $kv{"tls"} eq "ssl" ? 993 : 143
MC/DC0%0/2
$kv{"port"} ||= $kv{"tls"} eq "ssl" ? 993 : 143
$kv{"port"}
$kv{"tls"} eq "ssl" ? 993 : 143
Truth table0%0/3
A ||= B
A= $kv{"port"}B= $kv{"tls"} eq "ssl" ? 993 : 143
| |||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | 0 | my $account; | |||||||||||||||||||||||||||||||||||||||||
| 424 | 0 | ▶ | if ($method eq "oauth2") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | require AnyEvent::MCP::IMAP::OAuth2; | |||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | AnyEvent::MCP::IMAP::OAuth2::validate (\%kv); | |||||||||||||||||||||||||||||||||||||||||
| 427 | 0 | my ($url, $state, $verifier) = AnyEvent::MCP::IMAP::OAuth2::authorize (\%kv); | |||||||||||||||||||||||||||||||||||||||||
| 428 | 0 | print STDERR "Authorize the mail account '$kv{user}', not a different account already signed into the browser.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 429 | 0 | print STDERR "Open this OAuth authorization URL in a browser:\n$url\n\n"; | |||||||||||||||||||||||||||||||||||||||||
| 430 | 0 | print STDERR "After authorization, the browser will display an error page because no server listens at the redirect URI.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 431 | 0 | print STDERR "This is expected. Copy the FULL URL from the browser address bar and paste it here.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 432 | 0 | ▶ | open my $tty, "+<", "/dev/tty" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 433 | or die "OAuth callback requires a controlling terminal: $!\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 434 | 0 | print {$tty} "Paste redirected URL: "; | |||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | Term::ReadKey::ReadMode ("noecho", $tty); | |||||||||||||||||||||||||||||||||||||||||
| 436 | 0 | my $redirect = <$tty>; | |||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | Term::ReadKey::ReadMode ("restore", $tty); | |||||||||||||||||||||||||||||||||||||||||
| 438 | 0 | print {$tty} "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 439 | 0 | close $tty; | |||||||||||||||||||||||||||||||||||||||||
| 440 | 0 | ▶ | defined $redirect or die "No OAuth redirect URL supplied\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 441 | 0 | $redirect =~ s/\r?\n\z//; | |||||||||||||||||||||||||||||||||||||||||
| 442 | 0 | my $tokens = AnyEvent::MCP::IMAP::OAuth2::finish (\%kv, $state, $verifier, $redirect); | |||||||||||||||||||||||||||||||||||||||||
| 443 | my $refresh = $tokens->{refresh_token} | ||||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | ▶ | or die "OAuth provider returned no refresh token; configure offline access and try again\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 445 | 0 | $kv{"oauth-refresh-token-base64"} = MIME::Base64::encode_base64 ($refresh, ""); | |||||||||||||||||||||||||||||||||||||||||
| 446 | 0 | $account = settings (\%kv); | |||||||||||||||||||||||||||||||||||||||||
| 447 | 0 | $account->{access_token} = $tokens->{access_token}; | |||||||||||||||||||||||||||||||||||||||||
| 448 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | ▶ | open my $tty, "+<", "/dev/tty" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 450 | or die "Password requires a controlling terminal: $!\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 451 | 0 | print {$tty} "IMAP password for $kv{user}\@$kv{server}: "; | |||||||||||||||||||||||||||||||||||||||||
| 452 | 0 | Term::ReadKey::ReadMode ("noecho", $tty); | |||||||||||||||||||||||||||||||||||||||||
| 453 | 0 | my $password = <$tty>; | |||||||||||||||||||||||||||||||||||||||||
| 454 | 0 | Term::ReadKey::ReadMode ("restore", $tty); | |||||||||||||||||||||||||||||||||||||||||
| 455 | 0 | print {$tty} "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 456 | 0 | close $tty; | |||||||||||||||||||||||||||||||||||||||||
| 457 | 0 | ▶ | defined $password or die "No password supplied\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 458 | 0 | $password =~ s/\r?\n\z//; | |||||||||||||||||||||||||||||||||||||||||
| 459 | 0 | ▶ | length $password or die "Empty password\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 460 | 0 | $kv{"password-base64"} = MIME::Base64::encode_base64 ($password, ""); | |||||||||||||||||||||||||||||||||||||||||
| 461 | 0 | $account = settings (\%kv); | |||||||||||||||||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | 0 | require AnyEvent::MCP::Worker::IMAP; | |||||||||||||||||||||||||||||||||||||||||
| 465 | 0 | AnyEvent::MCP::Worker::IMAP::imap_connect ($account); | |||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||
| 467 | 0 | my $lock = AnyEvent::MCP::File::lock $file; | |||||||||||||||||||||||||||||||||||||||||
| 468 | 0 | AnyEvent::MCP::File::replace $file, "[account]\n" | |||||||||||||||||||||||||||||||||||||||||
| 469 | . (join "", map "$_ = $kv{$_}\n", sort keys %kv); | ||||||||||||||||||||||||||||||||||||||||||
| 470 | 0 | close $lock; | |||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | 0 | print STDERR "Authenticated and saved account '$name' in $file\n"; | |||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | 0 | ▶ | if (@ARGV && $ARGV[0] eq "login") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@ARGV and $ARGV[0] eq "login"
MC/DC0%0/2
@ARGV and $ARGV[0] eq "login"
@ARGV
$ARGV[0] eq "login"
Truth table0%0/3
A and B
A= @ARGVB= $ARGV[0] eq "login"
| |||||||||||||||||||||||||||||||||||||||||||
| 476 | 0 | shift @ARGV; | |||||||||||||||||||||||||||||||||||||||||
| 477 | 0 | login (); | |||||||||||||||||||||||||||||||||||||||||
| 478 | 0 | exit; | |||||||||||||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||
| 481 | 0 | ▶ | @ARGV and die "Unexpected arguments (try 'login')\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | $ask //= $ENV{PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL} eq "stdio" | ||||||||||||||||||||||||||||||||||||||||||
| 484 | 0 | ▶ | ? "mcp" : $ENV{PERL_ANYEVENT_MCP_PERMISSION_ASK} || "helper"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/4
$ask //= $ENV{"PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL"} eq "stdio" ? "mcp" : $ENV{"PERL_ANYEVENT_MCP_PERMISSION_ASK"} || "helper"
$ENV{"PERL_ANYEVENT_MCP_PERMISSION_ASK"} or "helper"
MC/DC0%0/1
$ask //= $ENV{"PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL"} eq "stdio" ? "mcp" : $ENV{"PERL_ANYEVENT_MCP_PERMISSION_ASK"} || "helper"
$ask
MC/DC0%0/1
$ENV{"PERL_ANYEVENT_MCP_PERMISSION_ASK"} or "helper"
$ENV{"PERL_ANYEVENT_MCP_PERMISSION_ASK"}
Truth table0%0/2
A
A= $ask
Truth table0%0/2
A
A= $ENV{"PERL_ANYEVENT_MCP_PERMISSION_ASK"}
| |||||||||||||||||||||||||||||||||||||||||||
| 485 | 0 | ▶ | $ask =~ /\A(?:helper|tty|yad|mcp|auto)\z/ or die "Invalid --ask mode\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | 0 | my $permissions = AnyEvent::MCP::Server::Permissions->new ( | |||||||||||||||||||||||||||||||||||||||||
| 488 | name => "imap-$name", | ||||||||||||||||||||||||||||||||||||||||||
| 489 | actions => { | ||||||||||||||||||||||||||||||||||||||||||
| 490 | read => "Read mailboxes and messages", | ||||||||||||||||||||||||||||||||||||||||||
| 491 | modify => "Change message flags, copy, move or append mail", | ||||||||||||||||||||||||||||||||||||||||||
| 492 | manage => "Create, rename, delete or subscribe mailboxes", | ||||||||||||||||||||||||||||||||||||||||||
| 493 | expunge => "Permanently remove messages marked Deleted", | ||||||||||||||||||||||||||||||||||||||||||
| 494 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 495 | ask => $ask, | ||||||||||||||||||||||||||||||||||||||||||
| 496 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||
| 498 | 0 | mcp_config | |||||||||||||||||||||||||||||||||||||||||
| 499 | permissions => $permissions, | ||||||||||||||||||||||||||||||||||||||||||
| 500 | server_name => "mcp-imap", | ||||||||||||||||||||||||||||||||||||||||||
| 501 | server_version => $AnyEvent::MCP::VERSION, | ||||||||||||||||||||||||||||||||||||||||||
| 502 | short_instructions => "Inspect and manage IMAP mail, by mailbox/UIDVALIDITY/UID. Reads do not set Seen; deletion requires explicit expunge.", | ||||||||||||||||||||||||||||||||||||||||||
| 503 | instructions => "One account per named mcp-imap instance. Read mailboxes and pages of messages; fetch MIME parts. " | ||||||||||||||||||||||||||||||||||||||||||
| 504 | . "Message operations require current UIDVALIDITY. Reads never set Seen. " | ||||||||||||||||||||||||||||||||||||||||||
| 505 | . "Deleting flags and expunging are separate; expunge permanently removes mail. " | ||||||||||||||||||||||||||||||||||||||||||
| 506 | . "An IMAP write interrupted by a disconnect has unknown outcome: never blindly repeat."; | ||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||
| 508 | 0 | my ($rpc, $rpc_dead, $connected); | |||||||||||||||||||||||||||||||||||||||||
| 509 | 0 | my %pending; | |||||||||||||||||||||||||||||||||||||||||
| 510 | 0 | my $seq = 0; | |||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||||
| 512 | 0 | ▶ | sub start_worker { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start_worker: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 513 | 0 | ▶ | return if $rpc; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 514 | 0 | require AnyEvent::MCP::Worker::IMAP; # dependency check, no connection until authorized | |||||||||||||||||||||||||||||||||||||||||
| 515 | 0 | $rpc_dead = undef; | |||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||
| 517 | $rpc = AnyEvent::Fork | ||||||||||||||||||||||||||||||||||||||||||
| 518 | ->new | ||||||||||||||||||||||||||||||||||||||||||
| 519 | ->require ("AnyEvent::MCP::Worker::IMAP") | ||||||||||||||||||||||||||||||||||||||||||
| 520 | ->AnyEvent::Fork::RPC::run ("AnyEvent::MCP::Worker::IMAP::run", | ||||||||||||||||||||||||||||||||||||||||||
| 521 | serialiser => $AnyEvent::Fork::RPC::JSON_SERIALISER, | ||||||||||||||||||||||||||||||||||||||||||
| 522 | 0 | ▶ | on_error => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 523 | 0 | $rpc_dead = $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 524 | 0 | undef $rpc; | |||||||||||||||||||||||||||||||||||||||||
| 525 | 0 | $connected = undef; | |||||||||||||||||||||||||||||||||||||||||
| 526 | 0 | for my $id (keys %pending) { | |||||||||||||||||||||||||||||||||||||||||
| 527 | 0 | my $req = delete $pending{$id}; | |||||||||||||||||||||||||||||||||||||||||
| 528 | $req->error ("IMAP worker disconnected; outcome of in-flight writes unknown: $rpc_dead") | ||||||||||||||||||||||||||||||||||||||||||
| 529 | 0 | ▶ | unless $req->{finished}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||||||||||||||||
| 531 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 532 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||
| 534 | 0 | $connected = 0; | |||||||||||||||||||||||||||||||||||||||||
| 535 | 0 | my $account = settings (load_account ()); | |||||||||||||||||||||||||||||||||||||||||
| 536 | 0 | ▶ | $account->{allow_mbox_from} = $allow_mbox_from ? JSON::XS::true : JSON::XS::false; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 537 | 0 | ▶ | $rpc->("connect", $account, sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 538 | 0 | my ($ok, $result) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 539 | 0 | ▶ | $connected = $ok ? 1 : $result; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 540 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||
| 543 | 0 | ▶ | sub perform { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
perform: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 544 | 0 | my ($req, $cmd, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||
| 546 | 0 | start_worker (); | |||||||||||||||||||||||||||||||||||||||||
| 547 | 0 | my $id = ++$seq; | |||||||||||||||||||||||||||||||||||||||||
| 548 | 0 | $pending{$id} = $req; | |||||||||||||||||||||||||||||||||||||||||
| 549 | 0 | ▶ | $rpc->($cmd, $args, sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 550 | 0 | ▶ | delete $pending{$id} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 551 | 0 | my ($ok, $result) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 552 | 0 | ▶ | return if $req->{finished}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 553 | 0 | ▶ | $ok ? $req->result ($result) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 554 | : $req->error ($result); | ||||||||||||||||||||||||||||||||||||||||||
| 555 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||
| 558 | 0 | ▶ | sub require_mail_permission { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
require_mail_permission: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 559 | 0 | my ($req, $action, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||||||
| 561 | 0 | my $mailbox = $args->{mailbox}; | |||||||||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||||||||||
| 563 | $req->require_permission ({ | ||||||||||||||||||||||||||||||||||||||||||
| 564 | tool => $req->{tool}, | ||||||||||||||||||||||||||||||||||||||||||
| 565 | 0 | ▶ | action => $action, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 566 | (defined $mailbox ? (dimensions => { | ||||||||||||||||||||||||||||||||||||||||||
| 567 | subject => { | ||||||||||||||||||||||||||||||||||||||||||
| 568 | label => "Mailbox", | ||||||||||||||||||||||||||||||||||||||||||
| 569 | value => "mailbox:$mailbox", | ||||||||||||||||||||||||||||||||||||||||||
| 570 | choices => [ | ||||||||||||||||||||||||||||||||||||||||||
| 571 | { label => "this mailbox", match => "mailbox:$mailbox" }, | ||||||||||||||||||||||||||||||||||||||||||
| 572 | { label => "all mailboxes", match => "^mailbox:" }, | ||||||||||||||||||||||||||||||||||||||||||
| 573 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 574 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 575 | }) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 576 | sandbox_grants => [], | ||||||||||||||||||||||||||||||||||||||||||
| 577 | reason => "$req->{tool} " . (defined $mailbox ? "mailbox '$mailbox'" : "mailboxes"), | ||||||||||||||||||||||||||||||||||||||||||
| 578 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 579 | } | ||||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||
| 581 | 0 | ▶ | sub property { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
property: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 582 | 0 | my ($type, $description) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||||||||||||||||
| 584 | 0 | +{ type => $type, description => $description } | |||||||||||||||||||||||||||||||||||||||||
| 585 | } | ||||||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||||||
| 587 | 0 | my $mailbox = property ("string", "IMAP mailbox name, e.g. INBOX"); | |||||||||||||||||||||||||||||||||||||||||
| 588 | 0 | my $validity = property ("string", "Current UIDVALIDITY from messages or message"); | |||||||||||||||||||||||||||||||||||||||||
| 589 | 0 | my $uid = property ("integer", "Message UID"); | |||||||||||||||||||||||||||||||||||||||||
| 590 | 0 | my $uids = { type => "array", items => $uid, minItems => 1, maxItems => 100 }; | |||||||||||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||||||||||||
| 592 | 0 | ▶ | sub expose { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
expose: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 593 | 0 | my ($cmd, $description, $required, $properties, $action) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||||||
| 595 | mcp_tool $cmd => $description, | ||||||||||||||||||||||||||||||||||||||||||
| 596 | { | ||||||||||||||||||||||||||||||||||||||||||
| 597 | type => "object", | ||||||||||||||||||||||||||||||||||||||||||
| 598 | required => $required, | ||||||||||||||||||||||||||||||||||||||||||
| 599 | additionalProperties => JSON::XS::false, | ||||||||||||||||||||||||||||||||||||||||||
| 600 | properties => $properties, | ||||||||||||||||||||||||||||||||||||||||||
| 601 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 602 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=19 SCAR=59.4
| |||||||||||||||||||||||||||||||||||||||||||
| 603 | 0 | my ($req, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||||||
| 605 | 0 | ▶ | ref $args eq "HASH" or die "Arguments must be an object\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 606 | 0 | for my $key (@$required) { | |||||||||||||||||||||||||||||||||||||||||
| 607 | 0 | ▶ | defined $args->{$key} or die "Missing $key\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 608 | } | ||||||||||||||||||||||||||||||||||||||||||
| 609 | 0 | ▶ | if (defined $args->{mailbox}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 610 | 0 | ▶ | !ref $args->{mailbox} && length $args->{mailbox} && length $args->{mailbox} <= 1024 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not ref $args->{"mailbox"} and length $args->{"mailbox"} and length $args->{"mailbox"} <= 1024
not ref $args->{"mailbox"} and length $args->{"mailbox"}
MC/DC0%0/3
not ref $args->{"mailbox"} and length $args->{"mailbox"} and length $args->{"mailbox"} <= 1024
not ref $args->{"mailbox"}
length $args->{"mailbox"}
length $args->{"mailbox"} <= 1024
Truth table0%0/4
not(A) and B and C
A= not ref $args->{"mailbox"}B= length $args->{"mailbox"}C= length $args->{"mailbox"} <= 1024
| |||||||||||||||||||||||||||||||||||||||||||
| 611 | or die "Invalid mailbox\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 612 | } | ||||||||||||||||||||||||||||||||||||||||||
| 613 | 0 | ▶ | if (defined $args->{uidvalidity}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 614 | 0 | ▶ | !ref $args->{uidvalidity} && $args->{uidvalidity} =~ /\A[1-9][0-9]*\z/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not ref $args->{"uidvalidity"} and $args->{"uidvalidity"} =~ /\A[1-9][0-9]*\z/u
MC/DC0%0/2
not ref $args->{"uidvalidity"} and $args->{"uidvalidity"} =~ /\A[1-9][0-9]*\z/u
not ref $args->{"uidvalidity"}
$args->{"uidvalidity"} =~ /\A[1-9][0-9]*\z/u
Truth table0%0/3
not(A) and B
A= not ref $args->{"uidvalidity"}B= $args->{"uidvalidity"} =~ /\A[1-9][0-9]*\z/u
| |||||||||||||||||||||||||||||||||||||||||||
| 615 | or die "Invalid UIDVALIDITY\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 616 | } | ||||||||||||||||||||||||||||||||||||||||||
| 617 | 0 | ▶ | if (defined $args->{uid}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 618 | 0 | ▶ | !ref $args->{uid} && $args->{uid} =~ /\A[1-9][0-9]*\z/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not ref $args->{"uid"} and $args->{"uid"} =~ /\A[1-9][0-9]*\z/u
MC/DC0%0/2
not ref $args->{"uid"} and $args->{"uid"} =~ /\A[1-9][0-9]*\z/u
not ref $args->{"uid"}
$args->{"uid"} =~ /\A[1-9][0-9]*\z/u
Truth table0%0/3
not(A) and B
A= not ref $args->{"uid"}B= $args->{"uid"} =~ /\A[1-9][0-9]*\z/u
| |||||||||||||||||||||||||||||||||||||||||||
| 619 | or die "Invalid UID\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 620 | } | ||||||||||||||||||||||||||||||||||||||||||
| 621 | 0 | ▶ | for my $u (@{$args->{uids} || []}) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args->{'uids'} || []
MC/DC0%0/1
$args->{'uids'} || []
$args->{'uids'}
Truth table0%0/2
A
A= $args->{'uids'}
| |||||||||||||||||||||||||||||||||||||||||||
| 622 | 0 | ▶ | defined $u && !ref $u && $u =~ /\A[1-9][0-9]*\z/ or die "Invalid UID\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $u and not ref $u and $u =~ /\A[1-9][0-9]*\z/u
defined $u and not ref $u
MC/DC0%0/3
defined $u and not ref $u and $u =~ /\A[1-9][0-9]*\z/u
defined $u
not ref $u
$u =~ /\A[1-9][0-9]*\z/u
Truth table0%0/4
A and not(B) and C
A= defined $uB= not ref $uC= $u =~ /\A[1-9][0-9]*\z/u
| |||||||||||||||||||||||||||||||||||||||||||
| 623 | } | ||||||||||||||||||||||||||||||||||||||||||
| 624 | 0 | require_mail_permission $req, $action, $args; | |||||||||||||||||||||||||||||||||||||||||
| 625 | 0 | perform $req, $cmd, $args; | |||||||||||||||||||||||||||||||||||||||||
| 626 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 627 | } | ||||||||||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||||||||||
| 629 | 0 | expose mailboxes => "List IMAP mailboxes.", [], | |||||||||||||||||||||||||||||||||||||||||
| 630 | {}, "read"; | ||||||||||||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||||||||||||
| 632 | 0 | expose lsub => "List subscribed IMAP mailboxes (LSUB).", [], | |||||||||||||||||||||||||||||||||||||||||
| 633 | {}, "read"; | ||||||||||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||||||||||
| 635 | 0 | expose messages => | |||||||||||||||||||||||||||||||||||||||||
| 636 | "Page UID summaries, newest first. SEARCH filter; cursor anchors UIDVALIDITY. Continue on incomplete pages.", | ||||||||||||||||||||||||||||||||||||||||||
| 637 | ["mailbox"], | ||||||||||||||||||||||||||||||||||||||||||
| 638 | { | ||||||||||||||||||||||||||||||||||||||||||
| 639 | mailbox => $mailbox, | ||||||||||||||||||||||||||||||||||||||||||
| 640 | limit => { type => "integer", minimum => 1, maximum => 100 }, | ||||||||||||||||||||||||||||||||||||||||||
| 641 | cursor => { type => "object", description => "{uidvalidity, before_uid} from previous page" }, | ||||||||||||||||||||||||||||||||||||||||||
| 642 | fields => { type => "array", items => property ("string", "Summary field") }, | ||||||||||||||||||||||||||||||||||||||||||
| 643 | headers => { type => "array", items => property ("string", "RFC 5322 header name"), maxItems => 20 }, | ||||||||||||||||||||||||||||||||||||||||||
| 644 | filter => { | ||||||||||||||||||||||||||||||||||||||||||
| 645 | type => "object", | ||||||||||||||||||||||||||||||||||||||||||
| 646 | description => "AND of: from/to/cc/bcc/subject/body/text strings; header:{Name:string}; " | ||||||||||||||||||||||||||||||||||||||||||
| 647 | . "since/before/on/sent_since/sent_before/sent_on dates (D-Mon-YYYY); " | ||||||||||||||||||||||||||||||||||||||||||
| 648 | . "larger/smaller byte counts; seen/unseen/answered/unanswered/flagged/unflagged/" | ||||||||||||||||||||||||||||||||||||||||||
| 649 | . "deleted/undeleted/draft/undraft/recent/old/new booleans; " | ||||||||||||||||||||||||||||||||||||||||||
| 650 | . "all:[filter,...], any:[filter,...], not:filter (recursive Boolean grouping)", | ||||||||||||||||||||||||||||||||||||||||||
| 651 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 652 | }, "read"; | ||||||||||||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||||||||||||
| 654 | 0 | expose message => "Read one message's envelope, flags, size and MIME BODYSTRUCTURE; no Seen change.", | |||||||||||||||||||||||||||||||||||||||||
| 655 | [qw(mailbox uidvalidity uid)], | ||||||||||||||||||||||||||||||||||||||||||
| 656 | { mailbox => $mailbox, uidvalidity => $validity, uid => $uid }, "read"; | ||||||||||||||||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||||||||||||||||
| 658 | 0 | expose part => "Fetch bounded MIME part using BODY.PEEK. Raw transferred bytes in Base64; optionally decode text.", | |||||||||||||||||||||||||||||||||||||||||
| 659 | [qw(mailbox uidvalidity uid part)], | ||||||||||||||||||||||||||||||||||||||||||
| 660 | { | ||||||||||||||||||||||||||||||||||||||||||
| 661 | mailbox => $mailbox, uidvalidity => $validity, uid => $uid, | ||||||||||||||||||||||||||||||||||||||||||
| 662 | part => property ("string", "BODYSTRUCTURE part number (empty for singlepart root)"), | ||||||||||||||||||||||||||||||||||||||||||
| 663 | max_bytes => { type => "integer", minimum => 1, maximum => 262144 }, | ||||||||||||||||||||||||||||||||||||||||||
| 664 | decode_text => { type => "boolean" }, | ||||||||||||||||||||||||||||||||||||||||||
| 665 | }, "read"; | ||||||||||||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||||||||||||
| 667 | 0 | expose raw => "Fetch bounded raw RFC 5322 bytes using BODY.PEEK, Base64 encoded.", | |||||||||||||||||||||||||||||||||||||||||
| 668 | [qw(mailbox uidvalidity uid)], | ||||||||||||||||||||||||||||||||||||||||||
| 669 | { mailbox => $mailbox, uidvalidity => $validity, uid => $uid, | ||||||||||||||||||||||||||||||||||||||||||
| 670 | max_bytes => { type => "integer", minimum => 1, maximum => 262144 } }, "read"; | ||||||||||||||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||||||||||||||
| 672 | 0 | expose flags => "Add or remove Seen, Flagged, Answered, Deleted, Draft or supported keywords. Never expunges.", | |||||||||||||||||||||||||||||||||||||||||
| 673 | [qw(mailbox uidvalidity uids operation flags)], | ||||||||||||||||||||||||||||||||||||||||||
| 674 | { | ||||||||||||||||||||||||||||||||||||||||||
| 675 | mailbox => $mailbox, uidvalidity => $validity, uids => $uids, | ||||||||||||||||||||||||||||||||||||||||||
| 676 | operation => { type => "string", enum => ["add", "remove"] }, | ||||||||||||||||||||||||||||||||||||||||||
| 677 | flags => { type => "array", items => property ("string", "IMAP flag or keyword"), minItems => 1, maxItems => 20 }, | ||||||||||||||||||||||||||||||||||||||||||
| 678 | }, "modify"; | ||||||||||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||||||||||
| 680 | 0 | for my $cmd (qw(copy move)) { | |||||||||||||||||||||||||||||||||||||||||
| 681 | 0 | ▶ | expose $cmd => ($cmd eq "move" ? "MOVE UIDs to destination; requires IMAP MOVE; no emulation." | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 682 | : "COPY UIDs to destination mailbox."), | ||||||||||||||||||||||||||||||||||||||||||
| 683 | [qw(mailbox uidvalidity uids destination)], | ||||||||||||||||||||||||||||||||||||||||||
| 684 | { mailbox => $mailbox, uidvalidity => $validity, uids => $uids, | ||||||||||||||||||||||||||||||||||||||||||
| 685 | destination => property ("string", "Destination mailbox") }, "modify"; | ||||||||||||||||||||||||||||||||||||||||||
| 686 | } | ||||||||||||||||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||||||||||||||||
| 688 | 0 | expose append => "Append raw CRLF RFC 5322 message (Base64, at most 1 MiB); reject unsafe mbox From_ lines.", | |||||||||||||||||||||||||||||||||||||||||
| 689 | [qw(mailbox data_base64)], | ||||||||||||||||||||||||||||||||||||||||||
| 690 | { | ||||||||||||||||||||||||||||||||||||||||||
| 691 | mailbox => $mailbox, | ||||||||||||||||||||||||||||||||||||||||||
| 692 | data_base64 => property ("string", "Base64-encoded RFC 5322 message, max 1 MiB"), | ||||||||||||||||||||||||||||||||||||||||||
| 693 | flags => { type => "array", items => property ("string", "Initial IMAP flag") }, | ||||||||||||||||||||||||||||||||||||||||||
| 694 | }, "modify"; | ||||||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||||||
| 696 | 0 | expose expunge => | |||||||||||||||||||||||||||||||||||||||||
| 697 | "Permanently expunge Deleted mail: optional UIDs require UIDPLUS; omitted UIDs expunge ALL Deleted messages.", | ||||||||||||||||||||||||||||||||||||||||||
| 698 | [qw(mailbox uidvalidity)], | ||||||||||||||||||||||||||||||||||||||||||
| 699 | { mailbox => $mailbox, uidvalidity => $validity, uids => $uids }, "expunge"; | ||||||||||||||||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||||||||||||||||
| 701 | 0 | expose mailbox => "Create, rename, delete, subscribe or unsubscribe a mailbox.", | |||||||||||||||||||||||||||||||||||||||||
| 702 | [qw(operation mailbox)], | ||||||||||||||||||||||||||||||||||||||||||
| 703 | { | ||||||||||||||||||||||||||||||||||||||||||
| 704 | operation => { type => "string", enum => [qw(create rename delete subscribe unsubscribe)] }, | ||||||||||||||||||||||||||||||||||||||||||
| 705 | mailbox => $mailbox, | ||||||||||||||||||||||||||||||||||||||||||
| 706 | destination => property ("string", "New mailbox name for rename"), | ||||||||||||||||||||||||||||||||||||||||||
| 707 | }, "manage"; | ||||||||||||||||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||||||||||||||||
| 709 | 0 | mcp_start %$options; | |||||||||||||||||||||||||||||||||||||||||