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"
18 static int count, seen, recent, unseen, imap4;
20 int imap_ok (FILE *sockfp, char *argbuf)
21 /* parse command response */
23 char buf [POPBUFSIZE+1];
27 if (!fgets(buf, sizeof(buf), sockfp))
29 if (buf[strlen(buf)-1] == '\n')
30 buf[strlen(buf)-1] = '\0';
31 if (buf[strlen(buf)-1] == '\r')
32 buf[strlen(buf)-1] = '\r';
34 if (outlevel == O_VERBOSE)
35 error(0, 0, "IMAP< %s", buf);
37 /* interpret untagged status responses */
38 if (strstr(buf, "EXISTS"))
40 if (strstr(buf, "RECENT"))
42 if (strstr(buf, "UNSEEN"))
44 if (strstr(buf, "FLAGS"))
45 seen = (strstr(buf, "Seen") != (char *)NULL);
47 (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
59 for (cp = buf; !isspace(*cp); cp++)
64 if (strncmp(cp, "OK", 2) == 0)
69 else if (strncmp(cp, "BAD", 2) == 0)
76 int imap_getauth(FILE *sockfp, struct query *ctl, char *buf)
77 /* apply for connection authorization */
79 /* try to get authorized */
80 int ok = gen_transact(sockfp,
82 ctl->remotename, ctl->password);
87 /* probe to see if we're running IMAP4 and can use RFC822.PEEK */
88 imap4 = ((gen_transact(sockfp, "CAPABILITY")) == 0);
93 static int imap_getrange(FILE *sockfp, struct query *ctl, int*countp, int*newp)
94 /* get range of messages to be fetched */
98 /* find out how many messages are waiting */
100 ok = gen_transact(sockfp,
102 ctl->mailbox[0] ? ctl->mailbox : "INBOX");
108 if (unseen) /* optional response, but better if we see it */
110 else if (recent) /* mandatory */
113 *newp = -1; /* should never happen, RECENT is mandatory */
118 static int imap_getsizes(FILE *sockfp, int count, int *sizes)
119 /* capture the sizes of all messages */
121 char buf [POPBUFSIZE+1];
123 gen_send(sockfp, "FETCH 1:%d RFC822.SIZE", count);
124 while (fgets(buf, sizeof(buf), sockfp))
128 if (buf[strlen(buf)-1] == '\n')
129 buf[strlen(buf)-1] = '\0';
130 if (buf[strlen(buf)-1] == '\r')
131 buf[strlen(buf)-1] = '\r';
132 if (outlevel == O_VERBOSE)
133 error(0, 0, "IMAP< %s", buf);
134 if (strstr(buf, "OK"))
136 else if (sscanf(buf, "* %d FETCH (RFC822.SIZE %d)", &num, &size) == 2)
137 sizes[num - 1] = size;
145 static int imap_is_old(FILE *sockfp, struct query *ctl, int num)
146 /* is the given message old? */
150 if ((ok = gen_transact(sockfp, "FETCH %d FLAGS", num)) != 0)
156 static int imap_fetch(FILE *sockfp, int number, int *lenp)
157 /* request nth message */
159 char buf [POPBUFSIZE+1];
163 * If we're using IMAP4, we can fetch the message without setting its
164 * seen flag. This is good! It means that if the protocol exchange
165 * craps out during the message, it will still be marked `unseen' on
169 gen_send(sockfp, "FETCH %d RFC822.PEEK", number);
171 gen_send(sockfp, "FETCH %d RFC822", number);
173 /* looking for FETCH response */
175 if (!fgets(buf, sizeof(buf), sockfp))
178 (sscanf(buf+2, "%d FETCH (RFC822 {%d}", &num, lenp) != 2);
186 static int imap_trail(FILE *sockfp, struct query *ctl, int number)
187 /* discard tail of FETCH response after reading message text */
189 char buf [POPBUFSIZE+1];
191 if (!fgets(buf, sizeof(buf), sockfp))
197 static int imap_delete(FILE *sockfp, struct query *ctl, int number)
198 /* set delete flag for given message */
200 /* use SILENT if possible as a minor throughput optimization */
201 return(gen_transact(sockfp,
203 ? "STORE %d +FLAGS.SILENT (\\Deleted)"
204 : "STORE %d +FLAGS (\\Deleted)",
208 const static struct method imap =
210 "IMAP", /* Internet Message Access Protocol */
211 143, /* standard IMAP2bis/IMAP4 port */
212 1, /* this is a tagged protocol */
213 0, /* no message delimiter */
214 imap_ok, /* parse command response */
215 imap_getauth, /* get authorization */
216 imap_getrange, /* query range of messages */
217 imap_getsizes, /* grab message sizes */
218 imap_is_old, /* no UID check */
219 imap_fetch, /* request given message */
220 imap_trail, /* eat message trailer */
221 imap_delete, /* set IMAP delete flag */
222 "EXPUNGE", /* the IMAP expunge command */
223 "LOGOUT", /* the IMAP exit command */
226 int doIMAP(struct query *ctl)
227 /* retrieve messages using IMAP Version 2bis or Version 4 */
229 return(do_protocol(ctl, &imap));
232 /* imap.c ends here */