Mac GeekeryGet your geek on. |
|
blog advertising is good for you
recent popular content
User login
|
Now we delve into the obscure: IMAP over Telnet. Huzzah. IMAP really, really, really wants to be done with client software. While it does use “normal” text it is quite text-intensive, displays a lot of information as it goes, is very slow to navigate by hand, and generally un-fun. Yet, sometimes you need to do it, so here’s how. Open the ConnectionAll IMAP command start with “# “ (a number, any number, and a space). It can get annoying. Also, unlike both SMTP and POP, IMAP logins are on one line rather than a modal session. $ telnet mail.mac.com imap4 Trying 17.250.248.64... Connected to mail.mac.com. Escape character is '^]'. * OK Netscape Messaging Multiplexor ready 1 LOGIN username password 1 OK User logged in List the MailboxesIMAP, unlike POP, is based on the concept of mailboxes, so let’s get a list. 1 LIST """%" * LIST (\NoInferiors) "/" INBOX * LIST (\HasNoChildren) "/""Deleted Messages" * LIST (\HasNoChildren) "/" Drafts * LIST (\HasNoChildren) "/""Sent Messages" * LIST (\HasNoChildren) "/" Trash 1 OK Completed Open the MailboxUnless filtered or moved, mail lands in the INBOX folder by default, so we’ll check that one. 1 select INBOX * FLAGS (\Answered \Flagged \Draft \Deleted \Seen NotJunk Junk JunkRecorded Forwarded Redirected NonJunk $NotJunk $Junk) * OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen NotJunk Junk JunkRecorded Forwarded Redirected NonJunk $NotJunk $Junk \*)] * 758 EXISTS * 0 RECENT * OK [UNSEEN 2] * OK [UIDVALIDITY 1015777771] 1 OK [READ-WRITE] Completed FLAGS tells you what flags the server supports for messages in this box. EXISTS tells you the number of messages in the box. Get a MessageHere’s where it gets fun. 1 fetch 1 all
* 1 FETCH (FLAGS (\Seen) INTERNALDATE "18-Sep-2004 10:55:23 -0700" RFC822.SIZE 5033 ENVELOPE ("Sat, 18 Sep 2004 19:39:56 +0200""Re: Converting an Absolute path to a relative path?" (("someone" NIL "someone""somewhere.net")) ((NIL NIL "cocoa-dev-bounces+someone=somewhere.com""lists.apple.com")) (("someone" NIL "someone""somewhere.net")) (("=?iso-8859-1?Q?St=E9phane?= Sudre" NIL "someone""somewhere.fr")(NIL NIL "cocoa-dev""lists.apple.com")) NIL NIL "<EAE0A136-0997-11D9-A07A-003065B01042@somewhere.fr>""<a0611040dbd7223277580@[192.168.0.4]>"))
1 OK Completed
Yes, those are headers. No, they don’t get prettier. Now that we know this is the one we want (if not, knock yourself out hunting for it), we get it. 1 fetch 1 body
* 1 FETCH (BODY ("TEXT""PLAIN" ("FORMAT""flowed""CHARSET""iso-8859-1") NIL NIL "QUOTED-PRINTABLE" 946 24))
1 OK Completed
No, wait, that’s not right. Where’s the message? Oh, right, I have to specify that I want the text of the body, not just a description of it. Remember, I’m human, not a client program. 001 fetch 1 body[text]
* 1 FETCH (FLAGS (\Seen) BODY[TEXT] {946}
At 19:26 Uhr +0200 18.09.2004, St=E9phane Sudre wrote:
>Did I reinvent the wheel or is there really not=20
>a method in NSString which allows to convert an=20
>absolute path to a relative one provided a=20
>reference path?
...
001 OK Completed
Mark for DeletionIMAP is more like a transactional database than a mailbox in how it handles deletion. First, you set a flag on a message (or ten) to have it deleted and then, when you’re sure, you commit those changes with an EXPUNGE command. So, knowing the ID of the message above you can mark it deleted (or read, or flagged, or whatever) by setting the Deleted flag: 0 store 3 +FLAGS (\Deleted) * 3 FETCH (FLAGS (\Deleted)) 0 OK Completed If you make a mistake, replace When you’re sure you made no typos, flush ‘em. 0 expunge * 3 EXPUNGE * 701 EXISTS * 1 RECENT 0 OK Completed And Dismount1 logout * BYE LOGOUT received 1 OK Completed Connection closed by foreign host. Enjoy, I know I won’t.
About Adam Knight
Author Biography Adam Knight is one of the founders of Mac Geekery and is a geek at heart. Programmer by day, hacker by night, his daily life revolves around the Macintosh platform, which he has been a user and programmer for since the early days of System 7 when his LCII replaced his Apple //c. In-between tech jobs, he’s managed to learn the basics of any web hacker: PHP, MySQL, Perl, Apache, Linux, *BSD, and the intricacies of ./configure —prefix=~/bombshelter/. Today, codepoet is concentrating on blogging again, writing some software for the Mac by himself (including Notae) and for his company (such as Switchblade) and has a few other toys coming out soon. Bug him over AIM or email [link fixed]. |
The first part of an IMAP command can be any alphanumeric string. The reason for this is that the client can send multiple commands without waiting for a response, and the server can return responses in any order. The initial tag matches the response with the command. When typing IMAP commands at a terminal, it can get tedious.
SMTP and POP over telnet aren’t that unreasonable, but as you point out, IMAP is a lot of Not Fun. However, on a mac, you’ve got python installed (always – osx uses it internally) and so you can just import imaplib and poke at the server (or at the messages) that way. Still interactive, still useful for this kind of problem solving, but not all the way up to “programming”… Mark
(Ah, the identification boxes are back) Along with imaplib, the command-line tool “imtest” is often a big help, because it handles authentication for you, and once that’s set up, you can use the techniques above to talk IMAP itself to the server. Mark
Updated the article. Completely forgot to add the bit on how to actually delete a message in IMAP.
—
cp
Thanks a lot for your post. It was very useful for me.