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)
16 #include "fetchmail.h"
18 static int count, seen, recent, unseen;
20 int imap_ok (sockfp, argbuf)
21 /* parse command response */
25 char buf [POPBUFSIZE+1];
29 if (fgets(buf, sizeof(buf), sockfp) == (char *)NULL)
32 if (outlevel == O_VERBOSE)
33 fprintf(stderr,"%s",buf);
35 /* interpret untagged status responses */
36 if (strstr(buf, "EXISTS"))
38 if (strstr(buf, "RECENT"))
40 if (strstr(buf, "UNSEEN"))
42 if (strstr(buf, "FLAGS"))
43 seen = (strstr(buf, "Seen") != (char *)NULL);
45 (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
57 for (cp = buf; !isspace(*cp); cp++)
62 if (strncmp(cp, "OK", 2) == 0)
67 else if (strncmp(cp, "BAD", 2) == 0)
74 int imap_getauth(sockfp, ctl, buf)
75 /* apply for connection authorization */
80 /* try to get authorized */
81 return(gen_transact(sockfp,
83 ctl->remotename, ctl->password));
86 static int imap_getrange(sockfp, ctl, countp, newp)
87 /* get range of messages to be fetched */
94 /* find out how many messages are waiting */
96 ok = gen_transact(sockfp,
98 ctl->mailbox[0] ? ctl->mailbox : "INBOX");
104 if (unseen) /* optional response, but better if we see it */
106 else if (recent) /* mandatory */
109 *newp = -1; /* should never happen, RECENT is mandatory */
114 static int imap_getsizes(sockfp, count, sizes)
115 /* capture the sizes of all messages */
120 char buf [POPBUFSIZE+1];
122 gen_send(sockfp, "FETCH 1:%d RFC822.SIZE", count);
123 while (fgets(buf, sizeof(buf), sockfp) != (char *)NULL)
127 if (outlevel == O_VERBOSE)
128 fprintf(stderr,"%s\n",buf);
129 if (strstr(buf, "OK"))
131 else if (sscanf(buf, "* %d FETCH (RFC822.SIZE %d)", &num, &size) == 2)
132 sizes[num - 1] = size;
136 fseek(sockfp, 0L, SEEK_CUR);
141 static int imap_is_old(sockfp, ctl, num)
142 /* is the given message old? */
149 if ((ok = gen_transact(sockfp, "FETCH %d FLAGS", num)) != 0)
155 static int imap_fetch(sockfp, number, lenp)
156 /* request nth message */
161 char buf [POPBUFSIZE+1];
164 gen_send(sockfp, "FETCH %d RFC822", number);
166 /* looking for FETCH response */
168 if (fgets(buf, sizeof(buf), sockfp) == (char *)NULL)
171 (sscanf(buf+2, "%d FETCH (RFC822 {%d}", &num, lenp) != 2);
172 fseek(sockfp, 0L, SEEK_CUR);
180 static int imap_trail(sockfp, ctl, number)
181 /* discard tail of FETCH response after reading message text */
186 char buf [POPBUFSIZE+1];
188 if (fgets(buf, sizeof(buf), sockfp) == (char *)NULL)
192 fseek(sockfp, 0L, SEEK_CUR);
197 static int imap_delete(sockfp, ctl, number)
198 /* set delete flag for given message */
203 return(gen_transact(sockfp, "STORE %d +FLAGS (\\Deleted)", number));
206 const static struct method imap =
208 "IMAP", /* Internet Message Access Protocol */
209 143, /* standard IMAP2bis/IMAP4 port */
210 1, /* this is a tagged protocol */
211 0, /* no message delimiter */
212 imap_ok, /* parse command response */
213 imap_getauth, /* get authorization */
214 imap_getrange, /* query range of messages */
215 imap_getsizes, /* grab message sizes */
216 imap_is_old, /* no UID check */
217 imap_fetch, /* request given message */
218 imap_trail, /* eat message trailer */
219 imap_delete, /* set IMAP delete flag */
220 "EXPUNGE", /* the IMAP expunge command */
221 "LOGOUT", /* the IMAP exit command */
225 /* retrieve messages using IMAP Version 2bis or Version 4 */
228 return(do_protocol(ctl, &imap));
231 /* imap.c ends here */