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 #include "fetchmail.h"
15 static int count, seen, recent, unseen;
17 int imap_ok (socket, argbuf)
18 /* parse command response */
23 char buf [POPBUFSIZE+1];
29 if (SockGets(socket, buf, sizeof(buf)) < 0)
32 if (outlevel == O_VERBOSE)
33 fprintf(stderr,"%s\n",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(socket, queryctl, buf)
75 /* apply for connection authorization */
77 struct hostrec *queryctl;
80 /* try to get authorized */
81 return(gen_transact(socket,
83 queryctl->remotename, queryctl->password));
86 static imap_getrange(socket, queryctl, countp, newp)
87 /* get range of messages to be fetched */
89 struct hostrec *queryctl;
94 /* find out how many messages are waiting */
96 ok = gen_transact(socket,
98 queryctl->mailbox[0] ? queryctl->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 imap_is_old(socket, queryctl, num)
116 struct hostrec *queryctl;
119 char buf [POPBUFSIZE+1];
122 if ((ok = gen_transact(socket, "FETCH %d FLAGS", num)) != 0)
128 static int imap_fetch(socket, number, lenp)
129 /* request nth message */
134 char buf [POPBUFSIZE+1];
137 gen_send(socket, "FETCH %d RFC822", number);
139 /* looking for FETCH response */
141 if (SockGets(socket, buf,sizeof(buf)) < 0)
144 (sscanf(buf+2, "%d FETCH (RFC822 {%d}", &num, lenp) != 2);
152 static imap_trail(socket, queryctl, number)
153 /* discard tail of FETCH response */
155 struct hostrec *queryctl;
158 char buf [POPBUFSIZE+1];
160 if (SockGets(socket, buf,sizeof(buf)) < 0)
166 static imap_delete(socket, queryctl, number)
167 /* set delete flag for given message */
169 struct hostrec *queryctl;
173 * I think the SILENT modifier here is the only thing keeping this code
174 * from being IMAP2 (RFC 1176-compliant), rather than IMAP2bis. It's
175 * important for places where phone service is expensive to cut the
176 * IMAP protocol overhead as much as possible.
178 return(gen_transact(socket, "STORE %d +FLAGS.SILENT (\\Deleted)", number));
181 const static struct method imap =
183 "IMAP", /* Internet Message Access Protocol */
184 143, /* standard IMAP2bis/IMAP4 port */
185 1, /* this is a tagged protocol */
186 0, /* no message delimiter */
187 imap_ok, /* parse command response */
188 imap_getauth, /* get authorization */
189 imap_getrange, /* query range of messages */
190 imap_is_old, /* no UID check */
191 imap_fetch, /* request given message */
192 imap_trail, /* eat message trailer */
193 imap_delete, /* set IMAP delete flag */
194 "EXPUNGE", /* the IMAP expunge command */
195 "LOGOUT", /* the IMAP exit command */
198 int doIMAP (queryctl)
199 /* retrieve messages using IMAP Version 2bis or Version 4 */
200 struct hostrec *queryctl;
202 return(do_protocol(queryctl, &imap));
205 /* imap.c ends here */