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 static int count, seen, recent, unseen, imap4;
21 int imap_ok (FILE *sockfp, char *argbuf)
22 /* parse command response */
24 char buf [POPBUFSIZE+1];
28 if (!SockGets(buf, sizeof(buf), sockfp))
30 if (buf[strlen(buf)-1] == '\n')
31 buf[strlen(buf)-1] = '\0';
32 if (buf[strlen(buf)-1] == '\r')
33 buf[strlen(buf)-1] = '\r';
35 if (outlevel == O_VERBOSE)
36 error(0, 0, "IMAP< %s", buf);
38 /* interpret untagged status responses */
39 if (strstr(buf, "EXISTS"))
41 if (strstr(buf, "RECENT"))
43 if (strstr(buf, "UNSEEN"))
45 if (strstr(buf, "FLAGS"))
46 seen = (strstr(buf, "Seen") != (char *)NULL);
48 (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
60 for (cp = buf; !isspace(*cp); cp++)
65 if (strncmp(cp, "OK", 2) == 0)
70 else if (strncmp(cp, "BAD", 2) == 0)
77 int imap_getauth(FILE *sockfp, struct query *ctl, char *buf)
78 /* apply for connection authorization */
80 /* try to get authorized */
81 int ok = gen_transact(sockfp,
83 ctl->remotename, ctl->password);
88 /* probe to see if we're running IMAP4 and can use RFC822.PEEK */
89 imap4 = ((gen_transact(sockfp, "CAPABILITY")) == 0);
94 static int imap_getrange(FILE *sockfp, struct query *ctl, int*countp, int*newp)
95 /* get range of messages to be fetched */
99 /* find out how many messages are waiting */
101 ok = gen_transact(sockfp,
103 ctl->mailbox[0] ? ctl->mailbox : "INBOX");
109 if (unseen) /* optional response, but better if we see it */
111 else if (recent) /* mandatory */
114 *newp = -1; /* should never happen, RECENT is mandatory */
119 static int imap_getsizes(FILE *sockfp, int count, int *sizes)
120 /* capture the sizes of all messages */
122 char buf [POPBUFSIZE+1];
124 gen_send(sockfp, "FETCH 1:%d RFC822.SIZE", count);
125 while (SockGets(buf, sizeof(buf), sockfp))
129 if (buf[strlen(buf)-1] == '\n')
130 buf[strlen(buf)-1] = '\0';
131 if (buf[strlen(buf)-1] == '\r')
132 buf[strlen(buf)-1] = '\r';
133 if (outlevel == O_VERBOSE)
134 error(0, 0, "IMAP< %s", buf);
135 if (strstr(buf, "OK"))
137 else if (sscanf(buf, "* %d FETCH (RFC822.SIZE %d)", &num, &size) == 2)
138 sizes[num - 1] = size;
146 static int imap_is_old(FILE *sockfp, struct query *ctl, int num)
147 /* is the given message old? */
151 if ((ok = gen_transact(sockfp, "FETCH %d FLAGS", num)) != 0)
157 static int imap_fetch(FILE *sockfp, int number, int *lenp)
158 /* request nth message */
160 char buf [POPBUFSIZE+1];
164 * If we're using IMAP4, we can fetch the message without setting its
165 * seen flag. This is good! It means that if the protocol exchange
166 * craps out during the message, it will still be marked `unseen' on
170 gen_send(sockfp, "FETCH %d RFC822.PEEK", number);
172 gen_send(sockfp, "FETCH %d RFC822", number);
174 /* looking for FETCH response */
176 if (!SockGets(buf, sizeof(buf), sockfp))
179 (sscanf(buf+2, "%d FETCH (RFC822 {%d}", &num, lenp) != 2);
187 static int imap_trail(FILE *sockfp, struct query *ctl, int number)
188 /* discard tail of FETCH response after reading message text */
190 char buf [POPBUFSIZE+1];
192 if (!SockGets(buf, sizeof(buf), sockfp))
198 static int imap_delete(FILE *sockfp, struct query *ctl, int number)
199 /* set delete flag for given message */
201 /* use SILENT if possible as a minor throughput optimization */
202 return(gen_transact(sockfp,
204 ? "STORE %d +FLAGS.SILENT (\\Deleted)"
205 : "STORE %d +FLAGS (\\Deleted)",
209 const static struct method imap =
211 "IMAP", /* Internet Message Access Protocol */
212 143, /* standard IMAP2bis/IMAP4 port */
213 1, /* this is a tagged protocol */
214 0, /* no message delimiter */
215 imap_ok, /* parse command response */
216 imap_getauth, /* get authorization */
217 imap_getrange, /* query range of messages */
218 imap_getsizes, /* grab message sizes */
219 imap_is_old, /* no UID check */
220 imap_fetch, /* request given message */
221 imap_trail, /* eat message trailer */
222 imap_delete, /* set IMAP delete flag */
223 "EXPUNGE", /* the IMAP expunge command */
224 "LOGOUT", /* the IMAP exit command */
227 int doIMAP(struct query *ctl)
228 /* retrieve messages using IMAP Version 2bis or Version 4 */
230 return(do_protocol(ctl, &imap));
233 /* imap.c ends here */