1 /* Copyright 1996 by Eric S. Raymond
3 * For license terms, see the file COPYING in this directory.
6 /***********************************************************************
9 programmer: Eric S. Raymond
10 description: IMAP client code
12 ***********************************************************************/
18 #include "fetchmail.h"
20 /*********************************************************************
22 Method declarations for IMAP
24 *********************************************************************/
26 static int count, seen;
28 int imap_ok (socket, argbuf)
29 /* parse command response */
34 char buf [POPBUFSIZE+1];
40 if (SockGets(socket, buf, sizeof(buf)) < 0)
43 if (outlevel == O_VERBOSE)
44 fprintf(stderr,"%s\n",buf);
46 /* interpret untagged status responses */
47 if (strstr(buf, "EXISTS"))
49 if (strstr(buf, "FLAGS"))
50 seen = (strstr(buf, "Seen") != (char *)NULL);
52 (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
64 for (cp = buf; !isspace(*cp); cp++)
69 if (strncmp(cp, "OK", 2) == 0)
74 else if (strncmp(cp, "BAD", 2) == 0)
81 int imap_getauth(socket, queryctl, buf)
82 /* apply for connection authorization */
84 struct hostrec *queryctl;
87 /* try to get authorized */
88 return(gen_transact(socket,
90 queryctl->remotename, queryctl->password));
93 static imap_getrange(socket, queryctl, countp)
94 /* get range of messages to be fetched */
96 struct hostrec *queryctl;
101 /* find out how many messages are waiting */
102 ok = gen_transact(socket,
104 queryctl->mailbox[0] ? queryctl->mailbox : "INBOX");
113 static imap_is_old(socket, queryctl, num)
115 struct hostrec *queryctl;
118 char buf [POPBUFSIZE+1];
121 if ((ok = gen_transact(socket, "FETCH %d FLAGS", num)) != 0)
127 static int imap_fetch(socket, number, lenp)
128 /* request nth message */
133 char buf [POPBUFSIZE+1];
136 gen_send(socket, "FETCH %d RFC822", number);
138 /* looking for FETCH response */
140 if (SockGets(socket, buf,sizeof(buf)) < 0)
143 (sscanf(buf+2, "%d FETCH (RFC822 {%d}", &num, lenp) != 2);
151 static imap_trail(socket, queryctl, number)
152 /* discard tail of FETCH response */
154 struct hostrec *queryctl;
157 char buf [POPBUFSIZE+1];
159 if (SockGets(socket, buf,sizeof(buf)) < 0)
165 static imap_delete(socket, queryctl, number)
166 /* set delete flag for given message */
168 struct hostrec *queryctl;
171 return(gen_transact(socket, "STORE %d +FLAGS (\\Deleted)", number));
174 const static struct method imap =
176 "IMAP", /* Internet Message Access Protocol */
177 143, /* standard IMAP2bis/IMAP4 port */
178 1, /* this is a tagged protocol */
179 0, /* no message delimiter */
180 imap_ok, /* parse command response */
181 imap_getauth, /* get authorization */
182 imap_getrange, /* query range of messages */
183 imap_is_old, /* no UID check */
184 imap_fetch, /* request given message */
185 imap_trail, /* eat message trailer */
186 imap_delete, /* set IMAP delete flag */
187 "EXPUNGE", /* the IMAP expunge command */
188 "LOGOUT", /* the IMAP exit command */
191 int doIMAP (queryctl)
192 /* retrieve messages using IMAP Version 2bis or Version 4 */
193 struct hostrec *queryctl;
195 return(do_protocol(queryctl, &imap));