imap4(3tcl)

πŸ“¦
version 2.0 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

✨ NAME ✨

imap4 β€” the imap client-side tcl implementation of the imap protocol, bestie πŸ’–

(Also known as imap4(3tcl), filed under the imap client section of your friendly neighbourhood manual. Hi. Welcome. Sit down, we're doing email.)


🌸 SYNOPSIS 🌸

Like, here's the whole cast list. Every single one of these functions is real and every single one is listed, no skips, no shortcuts, periodt:

package require Tcl 8.5 9

package require imap4 ?0.5.5?

::imap4::open hostname ?port?

::imap4::starttls chan

::imap4::login chan user pass

::imap4::folders chan ?-inline? ?mboxref? ?mboxname?

::imap4::select chan ?mailbox?

::imap4::examine chan ?mailbox?

::imap4::fetch chan range ?-inline? ?attr ...?

::imap4::noop chan

::imap4::check chan

::imap4::folderinfo chan ?info?

::imap4::msginfo chan msgid ?info? ?defval?

::imap4::mboxinfo chan ?info?

::imap4::isableto chan ?capability?

::imap4::create chan mailbox

::imap4::delete chan mailbox

::imap4::rename chan oldname newname

::imap4::subscribe chan mailbox

::imap4::unsubscribe chan mailbox

::imap4::search chan expr ?...?

::imap4::close chan

::imap4::cleanup chan

::imap4::debugmode chan ?errormsg?

::imap4::store chan range data flaglist

::imap4::expunge chan

::imap4::copy chan msgid mailbox

::imap4::logout chan

Look at that list. That's the whole vibe. That's the whole group chat. 🫢


πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so. The imap4 library package gives you the client side of the Internet Message Access Protocol (IMAP) πŸ’Œ β€” and it does it either over plain old standard sockets, or over a secure connection via TLS/SSL. Your choice, main character, we support both.

And the entire thing? Written in Tcl. Fully implemented. Every last bit of it. Not a C extension in sight, not one single binary blob β€” just Tcl doing its little Tcl thing and slaying while it does. ✨

This document, right here, describes the procedures and explains their usage. So like, keep reading. πŸŽ€

Note for the besties following along at home: this is part 1 of 3. More parts are coming. Don't go anywhere. πŸ’…

✨ PROCEDURES ✨

okay bestie, this package defines the following public procedures β€” and yes, we're doing all of them, no skimming πŸ’…


  • ::imap4::open hostname ?port?

opens a fresh IMAP connection and initalizes the handler β€” and the imap communication channel (that's the handler, same thing bestie) is what gets returned to you 🫢

  • hostname β€” mail server
  • port β€” connection port, defaults to 143

the namespace variable ::imap4::use_ssl can be used to establish a secure connection via TSL/SSL if set to true. when that's the vibe, the default connection port defaults to 993 instead. πŸ”₯

Note: for connecting via SSL the Tcl module tls must be already loaded, otherwise an error is raised. no tls, no secure vibes, periodt.
package require tls              ; # must be loaded for TLS/SSL
set ::imap4::use_ssl 1           ; # request a secure connection
set chan [::imap4::open $server] ; # default port is now 993
  • ::imap4::starttls chan β€” use this when you're tasked with connecting to an unsecure port that must be changed to a secure port prior to user login. this feature is known as STARTTLS, and it's giving glow-up mid-handshake ✨
  • ::imap4::login chan user pass

logs you in using the IMAP LOGIN command β€” 0 is returned on successful login πŸŽ€

  • chan β€” imap channel
  • user β€” username
  • pass β€” password
  • ::imap4::folders chan ?-inline? ?mboxref? ?mboxname?

gets you the list of matching folders, 0 is returned on success πŸ‘‘

wildcards '*' as '%' are allowed for mboxref and mboxname, and the command ::imap4::folderinfo can be used to retrieve folder information.

  • chan β€” imap channel
  • mboxref β€” mailbox reference, defaults to ""
  • mboxname β€” mailbox name, defaults to "*"

if -inline is specified a compact folderlist is returned instead of the result code. all flags are converted to lowercase and leading special characters are removed. so clean, so iconic:

{{Arc08 noselect} {Arc08/Private {noinferiors unmarked}} {INBOX noinferiors}}
  • ::imap4::select chan ?mailbox?

selects a mailbox, 0 is returned on success πŸ¦‹

  • chan β€” imap channel
  • mailbox β€” path of the mailbox, defaults to INBOX
Prior to examine/select an open mailbox must be closed β€” see: ::imap4::close. do not skip this, bestie, it's not optional 😭
  • ::imap4::examine chan ?mailbox?

"examines" a mailbox β€” it's the read-only equivalent of ::imap4::select. same energy, but you're just looking, not touching πŸ™ˆ

  • chan β€” imap channel
  • mailbox β€” mailbox name or path to mailbox, defaults to INBOX
Prior to examine/select an open mailbox must be closed β€” see: ::imap4::close.
  • ::imap4::fetch chan range ?-inline? ?attr ...?

fetches attributes from messages πŸ”₯

the attributes are fetched and stored in the internal state, which can be retrieved with the command ::imap4::msginfo, and 0 is returned on success. if -inline is specified, all records are returned as a list in the order as defined in the attr argument.

  • chan β€” imap channel
  • range β€” message index in format FROM:TO
  • attr β€” imap attributes to fetch
Note: if FROM is omitted, the 1st message is assumed; if TO is omitted the last message is assumed. all message index ranges are 1-based.
  • ::imap4::noop chan β€” sends the NOOP command to the server. may get information as untagged data πŸ‘€
    • chan β€” imap channel
  • ::imap4::check chan β€” sends the CHECK command to the server. flush to disk, bestie πŸ’Ύ
    • chan β€” imap channel
  • ::imap4::folderinfo chan ?info?

gets information on the recently selected folderlist. if the info argument is omitted or a null string, the full list of information available for the mailbox is returned πŸ“‹

if the required information name is suffixed with a ? character, the command returns true if the information is available, or false if it is not. little vibe check, love that for us.

  • chan β€” imap channel
  • info β€” folderlist options to retrieve

currently supported options: delim β€” hierarchy delimiter only, match β€” ref and mbox search patterns (see ::imap4::folders), names β€” list of folder names only, flags β€” list of folder names with flags in format { {name {flags}} ... } (see also the compact format in the function ::imap4::folders).

{{Arc08 {{\NoSelect}}} {Arc08/Private {{\NoInferiors} {\UnMarked}}} {INBOX {\NoInferiors}}}
  • ::imap4::msginfo chan msgid ?info? ?defval?

gets information (from previously collected using fetch) from a given msgid. if the info argument is omitted or a null string, the list of available information options for the given message is returned 🌸

if the required information name is suffixed with a ? character, the command returns true if the information is available, or false if it is not.

  • chan β€” imap channel
  • msgid β€” message number
  • info β€” imap keyword to retrieve
  • defval β€” default value, returned if info is empty
Note: all message index ranges are 1-based.
  • ::imap4::mboxinfo chan ?info?

gets information on the currently selected mailbox. if the info argument is omitted or a null string, the list of available information options for the mailbox is returned πŸ’Œ

if the required information name is suffixed with a ? character, the command returns true if the information is available, or false if it is not.

  • chan β€” imap channel
  • opt β€” mailbox option to retrieve

currently supported options: EXISTS (noof msgs), RECENT (noof 'recent' flagged msgs), FLAGS

in conjunction with OK: PERMFLAGS, UIDNEXT, UIDVAL, UNSEEN

div. states: CURRENT, FOUND, PERM.

::imap4::select $chan INBOX
puts "[::imap4::mboxinfo $chan exists] mails in INBOX"
  • ::imap4::isableto chan ?capability?

tests for capability. it returns 1 if the requested capability is supported, 0 otherwise. if capability is omitted all capability imap codes are retured as a list 🧚

  • chan β€” imap channel
  • info β€” imap keyword to retrieve
Note: use the capability command to ask the server if not already done by the user.
  • ::imap4::create chan mailbox β€” creates a new mailbox πŸ“¬
    • chan β€” imap channel
    • mailbox β€” mailbox name
  • ::imap4::delete chan mailbox β€” deletes a new mailbox πŸ’€
    • chan β€” imap channel
    • mailbox β€” mailbox name
  • ::imap4::rename chan oldname newname β€” renames a new mailbox ✏️
    • chan β€” imap channel
    • mailbox β€” old mailbox name
    • mailbox β€” new mailbox name
  • ::imap4::subscribe chan mailbox β€” subscribes a new mailbox 🫢
    • chan β€” imap channel
    • mailbox β€” mailbox name
  • ::imap4::unsubscribe chan mailbox β€” unsubscribes a new mailbox πŸ‘‹
    • chan β€” imap channel
    • mailbox β€” mailbox name
  • ::imap4::search chan expr ?...?

searches for mails matching search criterions, 0 is returned on success πŸ”

  • chan β€” imap channel
  • expr β€” imap search expression

Notes: currently the following search expressions are handled:

  • Mail header flags: all mail header entries (ending with a colon ":"), like "From:", "Bcc:", ...
  • Imap message search flags: ANSWERED, DELETED, DRAFT, FLAGGED, RECENT, SEEN, NEW, OLD, UNANSWERED, UNDELETED, UNDRAFT, UNFLAGGED, UNSEEN, ALL
  • Imap header search flags: BODY, CC, FROM, SUBJECT, TEXT, KEYWORD, BCC
  • Imap conditional search flags: SMALLER, LARGER, ON, SENTBEFORE, SENTON, SENTSINCE, SINCE, BEFORE (not implemented), UID (not implemented)
  • Logical search conditions: OR, NOT
::imap4::search $chan larger 4000 seen
puts "Found messages: [::imap4::mboxinfo $chan found]"
Found messages: 1 3 6 7 8 9 13 14 15 19 20
  • ::imap4::close chan β€” closes the mailbox. permanently removes \Deleted messages and returns to the AUTH state 😌
    • chan β€” imap channel
  • ::imap4::cleanup chan β€” destroys an IMAP connection and frees the used space. closes the mailbox. permanently removes \Deleted messages and returns to the AUTH state 🧹
    • chan β€” imap channel
  • ::imap4::debugmode chan ?errormsg? β€” switches the client into command line debug mode πŸ›

this is a developers mode only that passes the control to the programmer. every line entered is sent verbatim to the server (after the addition of the request identifier). the ::imap4::debug variable is automatically set to '1' on enter.

it's possible to execute Tcl commands starting the line with a slash.

  • chan β€” imap channel
  • errormsg β€” optional error message to display
  • ::imap4::store chan range data flaglist β€” alters data associated with a message in the selected mailbox πŸ’…
    • chan β€” imap channel
    • range β€” message index in format FROM:TO
    • flaglist β€” flags the data operates on.
    • data β€” the currently defined data items that can be stored are shown below. Note that all of these data types may also be suffixed with ".SILENT" to suppress the untagged FETCH response.
    • FLAGS β€” replace the flags for the message (other than \Recent) with the flaglist.
    • +FLAGS β€” add the flags in flaglist to the existing flags for the message.
    • -FLAGS β€” remove the flags in flaglist to the existing flags for the message.

for example:

::imap4::store $chan $start_msgid:$end_msgid +FLAGS "Deleted"
  • ::imap4::expunge chan β€” permanently removes all messages that have the \Deleted flag set from the currently selected mailbox, without the need to close the connection πŸ—‘οΈ
    • chan β€” imap channel
  • ::imap4::copy chan msgid mailbox β€” copies the specified message (identified by its message number) to the named mailbox, i.e. imap folder πŸ“
    • chan β€” imap channel
    • msgid β€” message number
    • mailbox β€” mailbox name
  • ::imap4::logout chan β€” informs the server that the client is done with the connection and closes the network connection. permanently removes \Deleted messages.

a new connection will need to be established to login once more πŸ‘‹

  • chan β€” imap channel

✨ EXAMPLES ✨

okay bestie, this is the part you actually came for 🫢 a whole little runnable vibe from connecting to cleaning up. watch and learn:

set user myusername
set pass xtremescrt
set server imap.test.tld
set FOLDER INBOX
# Connect to server
set imap [::imap4::open $server]
::imap4::login $imap $user $pass
::imap4::select $imap $FOLDER
# Output all the information about that mailbox
foreach info [::imap4::mboxinfo $imap] {
    puts "$info -> [::imap4::mboxinfo $imap $info]"
}
# fetch 3 records inline
set fields {from: to: subject: size}
foreach rec [::imap4::fetch $imap :3 -inline {*}$fields] {
    puts -nonewline "#[incr idx])"
    for {set j 0} {$j<[llength $fields]} {incr j} {
        puts "\t[lindex $fields $j] [lindex $rec $j]"
    }
}

# Show all the information available about the message ID 1
puts "Available info about message 1: [::imap4::msginfo $imap 1]"

# Use the capability stuff
puts "Capabilities: [::imap4::isableto $imap]"
puts "Is able to imap4rev1? [::imap4::isableto $imap imap4rev1]"

# Cleanup
::imap4::cleanup $imap

literally the whole life cycle in one screen: open β†’ login β†’ select β†’ snoop around the mailbox β†’ grab some records inline β†’ peek at message 1 β†’ flex the capability checks β†’ cleanup so you don't leave the connection hanging like an unread group chat 😭


πŸ” TLS SECURITY CONSIDERATIONS πŸ”

so here's the tea, and it's kind of important. this package uses the TLS package to handle the security for https urls and other socket connections. that's the machinery, not the policy.

policy decisions β€” like which set of protocols to support and which ciphers to use β€” are not the responsibility of TLS, and they're not this package's responsibility either. nope. those calls belong to whichever application is using the package, and they're likely influenced by the set of servers that application is going to talk to as well. so it's giving "your app, your rules, your problem" πŸ’…

for example: in light of the recent POODLE attack [http://googleonlinesecurity.blogspot.co.uk/2014/10/this-poodle-bites-exploiting-ssl-30.html] discovered by Google, many servers will disable support for the SSLv3 protocol. to handle that change, the applications using TLS must be patched β€” not this package, and not TLS itself. and honestly? such a patch may be as simple as generally activating tls1 support, like this:

package require tls
tls::init -tls1 1 ;# forcibly activate support for the TLS1 protocol

... your own application code ...
🚨 read that again: the fix lives in your application. not here. this package is not going to patch itself for you, bestie.

πŸ“š REFERENCES πŸ“š

the receipts, because we cite our sources in this house:


πŸ› BUGS, IDEAS, FEEDBACK πŸ›

okay so real talk: this document, and the package it describes, will undoubtedly contain bugs and other problems. no cap. so please report such in the category imap4 of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. and while you're there β€” please also report any ideas for enhancements you may have for either the package and/or the documentation. we want the glow-up, fr fr πŸ’–

when you're proposing code changes, please provide unified diffs, i.e. the output of diff -u.

note further that attachments are strongly preferred over inlined patches. attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. (iykyk, it's right there πŸ‘‘)

πŸ’€ heads up, and this one's a real caveat: only a small part of rfc3501 implemented. so don't come for the package expecting the whole spec, okay?

πŸ‘€ SEE ALSO πŸ‘€

ftp, http, imap, mime, pop3, tls

(the whole networking bestie squad, honestly 🌈)


🏷️ KEYWORDS 🏷️

email, imap, internet, mail, net, rfc3501, ssl, tls


πŸ’œ CATEGORY πŸ’œ

Networking


and that's the whole page, babes ✨ this came to you from tcllib, version 0.5.5, page imap4(3tcl) β€” slay, ate, no crumbs left πŸŽ€πŸ’…πŸ¦‹

πŸ“¦ where this page comes from