2 * odmr.c -- ODMR protocol methods (see RFC 2645)
4 * For license terms, see the file COPYING in this directory.
12 #ifdef HAVE_STRING_H /* strcat() */
15 #ifdef HAVE_NET_SOCKET_H /* BeOS needs this */
16 #include <net/socket.h>
18 #include <sys/types.h>
19 #ifdef HAVE_NET_SELECT_H /* AIX needs this */
20 #include <net/select.h>
22 #ifdef HAVE_SYS_SELECT_H /* AIX 4.1, at least, needs this */
23 #include <sys/select.h>
29 #include "fetchmail.h"
33 static int odmr_ok (int sock, char *argbuf)
34 /* parse command response */
39 ok = SMTP_ok(sock, SMTP_MODE);
40 if (ok == SM_UNRECOVERABLE)
46 static int odmr_getrange(int sock, struct query *ctl, const char *id,
47 int *countp, int *newp, int *bytes)
48 /* send ODMR and then run a reverse SMTP session */
50 int ok, opts, smtp_sock;
51 int doing_smtp_data = 0; /* Are we in SMTP DATA state? */
52 char buf [MSGBUFSIZE+1];
53 struct idlist *qnp; /* pointer to Q names */
56 if ((ok = SMTP_ehlo(sock, SMTP_MODE, fetchmailhost,
57 ctl->server.esmtp_name, ctl->server.esmtp_password,
60 report(stderr, GT_("%s's SMTP listener does not support ESMTP\n"),
61 ctl->server.pollname);
64 else if (!(opts & ESMTP_ATRN))
66 report(stderr, GT_("%s's SMTP listener does not support ATRN\n"),
67 ctl->server.pollname);
71 /* make sure we don't enter the fetch loop */
72 *bytes = *countp = *newp = -1;
74 /* authenticate via CRAM-MD5 */
75 ok = do_cram_md5(sock, "AUTH", ctl, "334 ");
80 * By default, the hostlist has a single entry, the fetchmail host's
84 for (qnp = ctl->domainlist; qnp; qnp = qnp->next)
85 if (strlen(buf) + strlen(qnp->id) + 1 >= sizeof(buf))
92 buf[strlen(buf) - 1] = '\0'; /* nuke final comma */
94 /* ship the domain list and get turnaround */
95 gen_send(sock, "ATRN %s", buf);
96 if ((ok = gen_recv(sock, buf, sizeof(buf))))
99 /* this switch includes all response codes described in RFC2645 */
102 case 250: /* OK, turnaround is about to happe */
103 if (outlevel > O_SILENT)
104 report(stdout, GT_("Turnaround now...\n"));
107 case 450: /* ATRN request refused */
108 if (outlevel > O_SILENT)
109 report(stdout, GT_("ATRN request refused.\n"));
112 case 451: /* Unable to process ATRN request now */
113 report(stderr, GT_("Unable to process ATRN request now\n"));
116 case 453: /* You have no mail */
117 if (outlevel > O_SILENT)
118 report(stderr, GT_("You have no mail.\n"));
121 case 502: /* Command not implemented */
122 report(stderr, GT_("Command not implemented\n"));
125 case 530: /* Authentication required */
126 report(stderr, GT_("Authentication required.\n"));
130 report(stderr, GT_("Unknown ODMR error %d\n"), atoi(buf));
135 * OK, if we got here it's time to become a pipe between the ODMR
136 * remote server (sending) and the SMTP listener we've designated
137 * (receiving). We're not going to try to be a protocol machine;
138 * instead, we'll use select(2) to watch the read sides of both
139 * sockets and just throw their data at each other.
141 if ((smtp_sock = smtp_open(ctl)) == -1)
145 int maxfd = (sock > smtp_sock) ? sock : smtp_sock;
150 struct timeval timeout;
151 char buf[MSGBUFSIZE];
154 FD_SET(sock, &readfds);
155 FD_SET(smtp_sock, &readfds);
157 timeout.tv_sec = ctl->server.timeout;
160 if (select(maxfd+1, &readfds, NULL, NULL, &timeout) == -1)
161 return(PS_PROTOCOL); /* timeout */
163 if (FD_ISSET(sock, &readfds))
165 int n = SockRead(sock, buf, sizeof(buf));
169 SockWrite(smtp_sock, buf, n);
170 if (outlevel >= O_MONITOR && !doing_smtp_data)
171 report(stdout, "ODMR< %s", buf);
173 if (FD_ISSET(smtp_sock, &readfds))
175 int n = SockRead(smtp_sock, buf, sizeof(buf));
179 SockWrite(sock, buf, n);
180 if (outlevel >= O_MONITOR)
181 report(stdout, "ODMR> %s", buf);
183 /* We are about to receive message data if the local MTA
184 * sends 354 (after receiving DATA) */
185 if (!doing_smtp_data && !strncmp(buf, "354", 3))
188 if (outlevel > O_SILENT)
189 report(stdout, GT_("receiving message data\n"));
191 else if (doing_smtp_data)
195 SockClose(smtp_sock);
201 static int odmr_logout(int sock, struct query *ctl)
202 /* send logout command */
204 /* if we have a smtp_socket, then we've turned around and the
205 local smtp server is in control of the connection (so we don't
207 if (ctl->smtp_socket == -1)
208 return(gen_transact(sock, "QUIT"));
213 static const struct method odmr =
215 "ODMR", /* ODMR protocol */
216 "odmr", /* standard ODMR port */
217 "odmrs", /* ssl ODMR port */
218 FALSE, /* this is not a tagged protocol */
219 FALSE, /* this does not use a message delimiter */
220 odmr_ok, /* parse command response */
221 NULL, /* no need to get authentication */
222 odmr_getrange, /* initialize message sending */
223 NULL, /* we cannot get a list of sizes */
224 NULL, /* we cannot get a list of sizes of subsets */
225 NULL, /* how do we tell a message is old? */
226 NULL, /* no way to fetch headers */
227 NULL, /* no way to fetch body */
228 NULL, /* no message trailer */
229 NULL, /* how to delete a message */
230 NULL, /* how to mark a message as seen */
231 NULL, /* no mailbox support */
232 odmr_logout, /* log out, we're done */
233 FALSE, /* no, we can't re-poll */
236 int doODMR (struct query *ctl)
237 /* retrieve messages using ODMR */
242 fprintf(stderr, GT_("Option --keep is not supported with ODMR\n"));
246 fprintf(stderr, GT_("Option --flush is not supported with ODMR\n"));
249 if (ctl->mailboxes->id) {
250 fprintf(stderr, GT_("Option --folder is not supported with ODMR\n"));
254 fprintf(stderr, GT_("Option --check is not supported with ODMR\n"));
257 peek_capable = FALSE;
259 status = do_protocol(ctl, &odmr);
260 if (status == PS_NOMAIL)
264 #endif /* ODMR_ENABLE */
266 /* odmr.c ends here */