2 * imap.c -- IMAP2bis/IMAP4 protocol methods
4 * Copyright 1996 by Eric S. Raymond
6 * For license terms, see the file COPYING in this directory.
13 #if defined(STDC_HEADERS)
16 #include "fetchmail.h"
19 extern char *strstr(); /* needed on sysV68 R3V7.1. */
21 /* imap_version values */
22 #define IMAP2 -1 /* IMAP2 or IMAP2BIS, RFC1176 */
23 #define IMAP4 0 /* IMAP4 rev 0, RFC1730 */
24 #define IMAP4rev1 1 /* IMAP4 rev 1, RFC2060 */
26 static int count, seen, recent, unseen, deletecount, imap_version;
28 int imap_ok (int sock, char *argbuf)
29 /* parse command response */
31 char buf [POPBUFSIZE+1];
37 if ((ok = gen_recv(sock, buf, sizeof(buf))))
40 /* interpret untagged status responses */
41 if (strstr(buf, "EXISTS"))
43 if (strstr(buf, "RECENT"))
45 if (strstr(buf, "UNSEEN"))
47 if (strstr(buf, "FLAGS"))
48 seen = (strstr(buf, "Seen") != (char *)NULL);
50 (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
62 for (cp = buf; !isspace(*cp); cp++)
67 if (strncmp(cp, "OK", 2) == 0)
72 else if (strncmp(cp, "BAD", 2) == 0)
79 int imap_getauth(int sock, struct query *ctl, char *buf)
80 /* apply for connection authorization */
82 char rbuf [POPBUFSIZE+1];
84 /* try to get authorized */
85 int ok = gen_transact(sock,
87 ctl->remotename, ctl->password);
92 /* probe to see if we're running IMAP4 and can use RFC822.PEEK */
93 gen_send(sock, "CAPABILITY");
94 if ((ok = gen_recv(sock, rbuf, sizeof(rbuf))))
96 if (strstr(rbuf, "BAD"))
99 if (outlevel == O_VERBOSE)
100 error(0, 0, "Protocol identified as IMAP2 or IMAP2BIS");
102 else if (strstr(rbuf, "IMAP4rev1"))
104 imap_version = IMAP4rev1;
105 if (outlevel == O_VERBOSE)
106 error(0, 0, "Protocol identified as IMAP4 rev 1");
110 imap_version = IMAP4;
111 if (outlevel == O_VERBOSE)
112 error(0, 0, "Protocol identified as IMAP4 rev 0");
115 peek_capable = (imap_version >= IMAP4);
120 static int imap_getrange(int sock,
123 int *countp, int *newp)
124 /* get range of messages to be fetched */
128 /* find out how many messages are waiting */
130 ok = gen_transact(sock, "SELECT %s", folder ? folder : "INBOX");
133 error(0, 0, "mailbox selection failed");
139 if (unseen) /* optional response, but better if we see it */
141 else if (recent) /* mandatory */
144 *newp = -1; /* should never happen, RECENT is mandatory */
151 static int imap_getsizes(int sock, int count, int *sizes)
152 /* capture the sizes of all messages */
154 char buf [POPBUFSIZE+1];
156 gen_send(sock, "FETCH 1:%d RFC822.SIZE", count);
157 while (SockRead(sock, buf, sizeof(buf)))
161 if ((ok = gen_recv(sock, buf, sizeof(buf))))
163 if (strstr(buf, "OK"))
165 else if (sscanf(buf, "* %d FETCH (RFC822.SIZE %d)", &num, &size) == 2)
166 sizes[num - 1] = size;
174 static int imap_is_old(int sock, struct query *ctl, int number)
175 /* is the given message old? */
179 /* expunges change the fetch numbers */
180 number -= deletecount;
182 if ((ok = gen_transact(sock, "FETCH %d FLAGS", number)) != 0)
188 static int imap_fetch(int sock, struct query *ctl, int number, int *lenp)
189 /* request nth message */
191 char buf [POPBUFSIZE+1];
194 /* expunges change the fetch numbers */
195 number -= deletecount;
198 * If we're using IMAP4, we can fetch the message without setting its
199 * seen flag. This is good! It means that if the protocol exchange
200 * craps out during the message, it will still be marked `unseen' on
203 * However...*don't* do this if we're using keep to suppress deletion!
204 * In that case, marking the seen flag is the only way to prevent the
205 * message from being re-fetched on subsequent runs.
207 switch (imap_version)
209 case IMAP4rev1: /* RFC 2060 */
211 gen_send(sock, "FETCH %d BODY.PEEK[]", number);
213 gen_send(sock, "FETCH %d BODY", number);
216 case IMAP4: /* RFC 1730 */
218 gen_send(sock, "FETCH %d RFC822.PEEK", number);
220 gen_send(sock, "FETCH %d RFC822", number);
223 default: /* RFC 1176 */
224 gen_send(sock, "FETCH %d RFC822", number);
228 /* looking for FETCH response */
232 if ((ok = gen_recv(sock, buf, sizeof(buf))))
235 /* third token can be "RFC822" or "BODY[]" */
236 (sscanf(buf+2, "%d FETCH (%*s {%d}", &num, lenp) != 2);
244 static int imap_trail(int sock, struct query *ctl, int number)
245 /* discard tail of FETCH response after reading message text */
247 char buf [POPBUFSIZE+1];
249 /* expunges change the fetch numbers */
250 /* number -= deletecount; */
252 return(gen_recv(sock, buf, sizeof(buf)));
255 static int imap_delete(int sock, struct query *ctl, int number)
256 /* set delete flag for given message */
260 /* expunges change the fetch numbers */
261 number -= deletecount;
264 * Use SILENT if possible as a minor throughput optimization.
265 * Note: this has been dropped from IMAP4rev1.
267 if ((ok = gen_transact(sock,
268 imap_version == IMAP4
269 ? "STORE %d +FLAGS.SILENT (\\Deleted)"
270 : "STORE %d +FLAGS (\\Deleted)",
275 * We do an expunge after each message, rather than just before quit,
276 * so that a line hit during a long session won't result in lots of
277 * messages being fetched again during the next session.
279 if ((ok = gen_transact(sock, "EXPUNGE")))
287 const static struct method imap =
289 "IMAP", /* Internet Message Access Protocol */
290 143, /* standard IMAP2bis/IMAP4 port */
291 1, /* this is a tagged protocol */
292 0, /* no message delimiter */
293 imap_ok, /* parse command response */
294 imap_getauth, /* get authorization */
295 imap_getrange, /* query range of messages */
296 imap_getsizes, /* grab message sizes */
297 imap_is_old, /* no UID check */
298 imap_fetch, /* request given message */
299 imap_trail, /* eat message trailer */
300 imap_delete, /* delete the message */
301 "LOGOUT", /* the IMAP exit command */
304 int doIMAP(struct query *ctl)
305 /* retrieve messages using IMAP Version 2bis or Version 4 */
307 return(do_protocol(ctl, &imap));
310 /* imap.c ends here */