2 * imap.c -- IMAP2bis 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)
17 #include "fetchmail.h"
19 static int count, seen, recent, unseen;
21 int imap_ok (socket, argbuf)
22 /* parse command response */
26 char buf [POPBUFSIZE+1];
30 if (SockGets(socket, buf, sizeof(buf)) < 0)
33 if (outlevel == O_VERBOSE)
34 fprintf(stderr,"%s\n",buf);
36 /* interpret untagged status responses */
37 if (strstr(buf, "EXISTS"))
39 if (strstr(buf, "RECENT"))
41 if (strstr(buf, "UNSEEN"))
43 if (strstr(buf, "FLAGS"))
44 seen = (strstr(buf, "Seen") != (char *)NULL);
46 (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
58 for (cp = buf; !isspace(*cp); cp++)
63 if (strncmp(cp, "OK", 2) == 0)
68 else if (strncmp(cp, "BAD", 2) == 0)
75 int imap_getauth(socket, ctl, buf)
76 /* apply for connection authorization */
81 /* try to get authorized */
82 return(gen_transact(socket,
84 ctl->remotename, ctl->password));
87 static int imap_getrange(socket, ctl, countp, newp)
88 /* get range of messages to be fetched */
95 /* find out how many messages are waiting */
97 ok = gen_transact(socket,
99 ctl->mailbox[0] ? ctl->mailbox : "INBOX");
105 if (unseen) /* optional response, but better if we see it */
107 else if (recent) /* mandatory */
110 *newp = -1; /* should never happen, RECENT is mandatory */
115 static int imap_getsizes(socket, count, sizes)
116 /* capture the sizes of all messages */
121 char buf [POPBUFSIZE+1];
123 gen_send(socket, "FETCH 1:%d RFC822.SIZE", count);
124 while (SockGets(socket, buf, sizeof(buf)) >= 0)
128 if (outlevel == O_VERBOSE)
129 fprintf(stderr,"%s\n",buf);
130 if (strstr(buf, "OK"))
132 else if (sscanf(buf, "* %d FETCH (RFC822.SIZE %d)", &num, &size) == 2)
133 sizes[num - 1] = size;
141 static int imap_is_old(socket, ctl, num)
142 /* is the given message old? */
149 if ((ok = gen_transact(socket, "FETCH %d FLAGS", num)) != 0)
155 static int imap_fetch(socket, number, lenp)
156 /* request nth message */
161 char buf [POPBUFSIZE+1];
164 gen_send(socket, "FETCH %d RFC822", number);
166 /* looking for FETCH response */
168 if (SockGets(socket, buf,sizeof(buf)) < 0)
171 (sscanf(buf+2, "%d FETCH (RFC822 {%d}", &num, lenp) != 2);
179 static int imap_trail(socket, ctl, number)
180 /* discard tail of FETCH response after reading message text */
185 char buf [POPBUFSIZE+1];
187 if (SockGets(socket, buf,sizeof(buf)) < 0)
193 static int imap_delete(socket, ctl, number)
194 /* set delete flag for given message */
199 return(gen_transact(socket, "STORE %d +FLAGS (\\Deleted)", number));
202 const static struct method imap =
204 "IMAP", /* Internet Message Access Protocol */
205 143, /* standard IMAP2bis/IMAP4 port */
206 1, /* this is a tagged protocol */
207 0, /* no message delimiter */
208 imap_ok, /* parse command response */
209 imap_getauth, /* get authorization */
210 imap_getrange, /* query range of messages */
211 imap_getsizes, /* grab message sizes */
212 imap_is_old, /* no UID check */
213 imap_fetch, /* request given message */
214 imap_trail, /* eat message trailer */
215 imap_delete, /* set IMAP delete flag */
216 "EXPUNGE", /* the IMAP expunge command */
217 "LOGOUT", /* the IMAP exit command */
221 /* retrieve messages using IMAP Version 2bis or Version 4 */
224 return(do_protocol(ctl, &imap));
227 /* imap.c ends here */