2 * driver.c -- generic driver for mail fetch method protocols
4 * Copyright 1996 by Eric S. Raymond
6 * For license terms, see the file COPYING in this directory.
15 #if defined(STDC_HEADERS)
18 #if defined(HAVE_UNISTD_H)
21 #if defined(HAVE_STDARG_H)
26 #if defined(HAVE_ALLOCA_H)
32 #ifdef HAVE_GETHOSTBYNAME
35 #endif /* HAVE_GETHOSTBYNAME */
40 #include <netinet/in.h>
42 #endif /* KERBEROS_V4 */
44 #include "fetchmail.h"
48 /* BSD portability hack...I know, this is an ugly place to put it */
49 #if !defined(SIGCHLD) && defined(SIGCLD)
50 #define SIGCHLD SIGCLD
53 #define SMTP_PORT 25 /* standard SMTP service port */
55 int batchlimit; /* how often to tear down the delivery connection */
56 int fetchlimit; /* how often to tear down the server connection */
57 int batchcount; /* count of messages sent in current batch */
58 int peek_capable; /* can we peek for better error recovery? */
60 static const struct method *protocol;
61 static jmp_buf restart;
65 #define GENSYM (sprintf(tag, "a%04d", ++tagnum), tag)
67 static char *shroud; /* string to shroud in debug output, if non-NULL */
68 static int mytimeout; /* value of nonreponse timeout */
70 static int strcrlf(dst, src, count)
71 /* replace LFs with CR-LF; return length of string with replacements */
72 char *dst; /* new string with CR-LFs */
73 char *src; /* original string with LFs */
74 int count; /* length of src */
91 static void vtalarm(int timeleft)
92 /* reset the nonresponse-timeout */
94 struct itimerval ntimeout;
96 ntimeout.it_interval.tv_sec = ntimeout.it_interval.tv_usec = 0;
97 ntimeout.it_value.tv_sec = timeleft;
98 ntimeout.it_value.tv_usec = 0;
99 setitimer(ITIMER_VIRTUAL, &ntimeout, (struct itimerval *)NULL);
102 static void vtalarm_handler (int signal)
103 /* handle server-timeout SIGVTALARM signal */
108 #ifdef HAVE_RES_SEARCH
111 static int is_host_alias(const char *name, struct query *ctl)
112 /* determine whether name is a DNS alias of the hostname */
115 struct mxentry *mxp, *mxrecords;
118 * The first two checks are optimizations that will catch a good
119 * many cases. (1) check against the hostname the user
120 * specified. Odds are good this will either be the mailserver's
121 * FQDN or a suffix of it with the mailserver's domain's default
122 * host name omitted. Then check the rest of the `also known as'
123 * cache accumulated by previous DNS checks. This cache is primed
124 * by the aka list option.
126 * (2) check against the mailserver's FQDN, in case
127 * it's not the same as the declared hostname.
129 * Either of these on a mail address is definitive. Only if the
130 * name doesn't match either is it time to call the bind library.
131 * If this happens odds are good we're looking at an MX name.
133 if (str_in_list(&ctl->lead_server->servernames, name))
135 else if (strcmp(name, ctl->canonical_name) == 0)
139 * We know DNS service was up at the beginning of this poll cycle.
140 * If it's down, our nameserver has crashed. We don't want to try
141 * delivering the current message or anything else from this
142 * mailbox until it's back up.
144 else if ((he = gethostbyname(name)) != (struct hostent *)NULL)
146 if (strcmp(ctl->canonical_name, he->h_name) == 0)
154 case HOST_NOT_FOUND: /* specified host is unknown */
155 case NO_ADDRESS: /* valid, but does not have an IP address */
158 case NO_RECOVERY: /* non-recoverable name server error */
159 case TRY_AGAIN: /* temporary error on authoritative server */
161 if (outlevel != O_SILENT)
162 putchar('\n'); /* terminate the progress message */
164 "nameserver failure while looking for `%s' during poll of %s.",
165 name, ctl->servernames->id);
167 longjmp(restart, 2); /* try again next poll cycle */
172 * We're only here if DNS was OK but the gethostbyname() failed
173 * with a HOST_NOT_FOUND or NO_ADDRESS error.
174 * Search for a name match on MX records pointing to the server.
177 if ((mxrecords = getmxrecords(name)) == (struct mxentry *)NULL)
181 case HOST_NOT_FOUND: /* specified host is unknown */
182 case NO_ADDRESS: /* valid, but does not have an IP address */
186 case NO_RECOVERY: /* non-recoverable name server error */
187 case TRY_AGAIN: /* temporary error on authoritative server */
190 "nameserver failure while looking for `%s' during poll of %s.",
191 name, ctl->servernames->id);
193 longjmp(restart, 2); /* try again next poll cycle */
199 for (mxp = mxrecords; mxp->name; mxp++)
200 if (strcmp(ctl->canonical_name, mxp->name) == 0)
206 /* add this name to relevant server's `also known as' list */
207 save_str(&ctl->lead_server->servernames, -1, name);
211 static void map_name(name, ctl, xmit_names)
212 /* add given name to xmit_names if it matches declared localnames */
213 const char *name; /* name to map */
214 struct query *ctl; /* list of permissible aliases */
215 struct idlist **xmit_names; /* list of recipient names parsed out */
219 lname = idpair_find(&ctl->localnames, name);
220 if (!lname && ctl->wildcard)
223 if (lname != (char *)NULL)
225 if (outlevel == O_VERBOSE)
226 error(0, 0, "mapped %s to local %s", name, lname);
227 save_str(xmit_names, -1, lname);
231 void find_server_names(hdr, ctl, xmit_names)
232 /* parse names out of a RFC822 header into an ID list */
233 const char *hdr; /* RFC822 header in question */
234 struct query *ctl; /* list of permissible aliases */
235 struct idlist **xmit_names; /* list of recipient names parsed out */
237 if (hdr == (char *)NULL)
243 if ((cp = nxtaddr(hdr)) != (char *)NULL)
247 if ((atsign = strchr(cp, '@')))
250 * Address has an @. Check to see if the right-hand part
251 * is an alias or MX equivalent of the mailserver. If it's
252 * not, skip this name. If it is, we'll keep going and try
253 * to find a mapping to a client name.
255 if (!is_host_alias(atsign+1, ctl))
260 map_name(cp, ctl, xmit_names);
262 ((cp = nxtaddr((char *)NULL)) != (char *)NULL);
265 #endif /* HAVE_RES_SEARCH */
267 static FILE *smtp_open(struct query *ctl)
268 /* try to open a socket to the appropriate SMTP server for this query */
272 lead = ctl->lead_smtp; /* go to the SMTP leader for this query */
274 /* maybe it's time to close the socket in order to force delivery */
275 if (batchlimit && lead->smtp_sockfp && batchcount++ == batchlimit)
277 fclose(lead->smtp_sockfp);
278 lead->smtp_sockfp = (FILE *)NULL;
282 /* if no socket to this host is already set up, try to open one */
283 if (lead->smtp_sockfp == (FILE *)NULL)
285 if ((lead->smtp_sockfp = SockOpen(lead->smtphost, SMTP_PORT)) == (FILE *)NULL)
286 return((FILE *)NULL);
287 else if (SMTP_ok(lead->smtp_sockfp) != SM_OK
288 || SMTP_helo(lead->smtp_sockfp, ctl->servernames->id) != SM_OK)
290 fclose(lead->smtp_sockfp);
291 lead->smtp_sockfp = (FILE *)NULL;
295 return(lead->smtp_sockfp);
298 static int gen_readmsg (sockfp, len, delimited, ctl)
299 /* read message content and ship to SMTP or MDA */
300 FILE *sockfp; /* to which the server is connected */
301 long len; /* length of message */
302 int delimited; /* does the protocol use a message delimiter? */
303 struct query *ctl; /* query control record */
305 char buf [MSGBUFSIZE+1];
306 char *bufp, *headers, *fromhdr,*tohdr,*cchdr,*bcchdr,*received_for,*envto;
307 char *fromptr, *toptr;
309 int inheaders,lines,sizeticker;
311 RETSIGTYPE (*sigchld)();
312 #ifdef HAVE_GETHOSTBYNAME
313 char rbuf[HOSTLEN + USERNAMELEN + 4];
314 #endif /* HAVE_GETHOSTBYNAME */
316 /* read the message content from the server */
318 headers = fromhdr = tohdr = cchdr = bcchdr = received_for = envto = NULL;
322 while (delimited || len > 0)
324 if (!SockGets(buf,sizeof(buf),sockfp))
327 vtalarm(ctl->timeout);
329 /* squeeze out all carriage returns */
330 for (fromptr = toptr = buf; *fromptr; fromptr++)
331 if (*fromptr != '\r' && *fromptr != '\n')
335 /* write the message size dots */
339 while (sizeticker >= SIZETICKER)
341 if (outlevel > O_SILENT)
343 sizeticker -= SIZETICKER;
348 if (buf[0] == '\0' || buf[0] == '\r' || buf[0] == '\n')
350 if (delimited && *bufp == '.') {
353 break; /* end of message */
360 reply_hack(bufp, ctl->servernames->id);
364 oldlen = strlen(bufp);
365 headers = xmalloc(oldlen + 1);
366 (void) strcpy(headers, bufp);
374 * We deal with RFC822 continuation lines here.
375 * Replace previous '\n' with '\r' so nxtaddr
376 * and reply_hack will be able to see past it.
377 * (We know this is safe because SocketGets stripped
378 * out all carriage returns in the read loop above
379 * and we haven't reintroduced any since then.)
380 * We'll undo this before writing the header.
382 if (isspace(bufp[0]))
383 headers[oldlen-1] = '\r';
385 newlen = oldlen + strlen(bufp);
386 headers = realloc(headers, newlen + 1);
389 strcpy(headers + oldlen, bufp);
390 bufp = headers + oldlen;
394 if (!strncasecmp("From:", bufp, 5))
396 else if (!fromhdr && !strncasecmp("Resent-From:", bufp, 12))
398 else if (!fromhdr && !strncasecmp("Apparently-From:", bufp, 16))
400 else if (!strncasecmp("To:", bufp, 3))
402 else if (!strncasecmp("Apparently-To:", bufp, 14))
404 else if (!strncasecmp(ctl->envelope, bufp, 14))
406 else if (!strncasecmp("Cc:", bufp, 3))
408 else if (!strncasecmp("Bcc:", bufp, 4))
410 #ifdef HAVE_RES_SEARCH
411 else if (MULTIDROP(ctl) && !strncasecmp("Received:", bufp, 9))
416 * Try to extract the real envelope addressee. We look here
417 * specifically for the mailserver's Received line.
418 * Note: this will only work for sendmail, or an MTA that
419 * shares sendmail's convention for embedding the envelope
420 * address in the Received line. Sendmail itself only
421 * does this when the mail has a single recipient.
423 if ((ok = strstr(bufp, "by ")) == (char *)NULL)
429 /* extract space-delimited token after "by " */
431 for (sp = ok + 3; !isspace(*sp); sp++)
436 * If it's a DNS name of the mail server, look for the
437 * recipient name after a following "for". Otherwise
440 if (is_host_alias(rbuf, ctl))
441 ok = strstr(sp, "for ");
454 while (*sp && *sp != '>' && *sp != '@' && *sp != ';')
459 /* uh oh -- whitespace here can't be right! */
468 received_for = alloca(strlen(rbuf)+1);
469 strcpy(received_for, rbuf);
470 if (outlevel == O_VERBOSE)
472 "found Received address `%s'",
476 #endif /* HAVE_RES_SEARCH */
480 else if (headers) /* OK, we're at end of headers now */
483 struct idlist *idp, *xmit_names;
484 int good_addresses, bad_addresses;
485 #ifdef HAVE_RES_SEARCH
486 int no_local_matches = FALSE;
487 #endif /* HAVE_RES_SEARCH */
489 /* cons up a list of local recipients */
490 xmit_names = (struct idlist *)NULL;
491 bad_addresses = good_addresses = 0;
492 #ifdef HAVE_RES_SEARCH
493 /* is this a multidrop box? */
496 if (envto) /* We have the actual envelope addressee */
497 find_server_names(envto, ctl, &xmit_names);
498 else if (received_for)
500 * We have the Received for addressee.
501 * It has to be a mailserver address, or we
502 * wouldn't have got here.
504 map_name(received_for, ctl, &xmit_names);
508 * We haven't extracted the envelope address.
509 * So check all the header addresses.
511 find_server_names(tohdr, ctl, &xmit_names);
512 find_server_names(cchdr, ctl, &xmit_names);
513 find_server_names(bcchdr, ctl, &xmit_names);
517 no_local_matches = TRUE;
518 save_str(&xmit_names, -1, user);
519 if (outlevel == O_VERBOSE)
521 "no local matches, forwarding to %s",
525 else /* it's a single-drop box, use first localname */
526 #endif /* HAVE_RES_SEARCH */
527 save_str(&xmit_names, -1, ctl->localnames->id);
529 /* time to address the message */
530 if (ctl->mda[0]) /* we have a declared MDA */
536 * We go through this in order to be able to handle very
537 * long lists of users and (re)implement %s.
539 for (idp = xmit_names; idp; idp = idp->next)
540 length += (strlen(idp->id) + 1);
541 names = (char *)alloca(length);
543 for (idp = xmit_names; idp; idp = idp->next)
545 strcat(names, idp->id);
548 cmd = (char *)alloca(strlen(ctl->mda) + length);
549 sprintf(cmd, ctl->mda, names);
550 if (outlevel == O_VERBOSE)
551 error(0, 0, "about to deliver with: %s", cmd);
555 * Arrange to run with user's permissions if we're root.
556 * This will initialize the ownership of any files the
557 * MDA creates properly. (The seteuid call is available
558 * under all BSDs and Linux)
561 #endif /* HAVE_SETEUID */
563 sinkfp = popen(cmd, "w");
566 /* this will fail quietly if we didn't start as root */
568 #endif /* HAVE_SETEUID */
572 error(0, 0, "MDA open failed");
576 sigchld = signal(SIGCHLD, SIG_DFL);
582 /* build a connection to the SMTP listener */
583 if (ctl->mda[0] == '\0' && ((sinkfp = smtp_open(ctl)) == NULL))
585 free_str_list(&xmit_names);
586 error(0, 0, "SMTP connect failed");
591 * Try to get the SMTP listener to take the header
592 * From address as MAIL FROM (this makes the logging
593 * nicer). If it won't, fall back on the calling-user
594 * ID. This won't affect replies, which use the header
595 * From address anyway.
597 if (!fromhdr || !(ap = nxtaddr(fromhdr)))
599 if (SMTP_from(sinkfp, user) != SM_OK)
600 return(PS_SMTP); /* should never happen */
602 else if (SMTP_from(sinkfp, ap) != SM_OK)
603 if (smtp_response == 571)
606 * SMTP listener explicitly refuses to deliver
607 * mail coming from this address, probably due
608 * to an anti-spam domain exclusion. Respect
611 sinkfp = (FILE *)NULL;
614 else if (SMTP_from(sinkfp, user) != SM_OK)
615 return(PS_SMTP); /* should never happen */
617 /* now list the recipient addressees */
618 for (idp = xmit_names; idp; idp = idp->next)
619 if (SMTP_rcpt(sinkfp, idp->id) == SM_OK)
626 "SMTP listener doesn't like recipient address `%s'", idp->id);
628 if (!good_addresses && SMTP_rcpt(sinkfp, user) != SM_OK)
631 "can't even send to calling user!");
635 /* tell it we're ready to send data */
641 /* change continuation markers back to regular newlines */
642 for (cp = headers; cp < headers + oldlen; cp++)
646 /* replace all LFs with CR-LF before sending to the SMTP server */
649 char *newheaders = xmalloc(1 + oldlen * 2);
651 oldlen = strcrlf(newheaders, headers, oldlen);
653 headers = newheaders;
656 /* write all the headers */
659 n = fwrite(headers, 1, oldlen, sinkfp);
661 n = SockWrite(headers, 1, oldlen, sinkfp);
667 error(0, errno, "writing RFC822 headers");
671 signal(SIGCHLD, sigchld);
675 else if (outlevel == O_VERBOSE)
680 /* write error notifications */
681 #ifdef HAVE_RES_SEARCH
682 if (no_local_matches || bad_addresses)
685 #endif /* HAVE_RES_SEARCH */
688 char errhd[USERNAMELEN + POPBUFSIZE], *errmsg;
691 (void) strcpy(errhd, "X-Fetchmail-Warning: ");
692 #ifdef HAVE_RES_SEARCH
693 if (no_local_matches)
695 strcat(errhd, "no recipient addresses matched declared local names");
699 #endif /* HAVE_RES_SEARCH */
703 strcat(errhd, "SMTP listener rejected local recipient addresses: ");
704 errlen = strlen(errhd);
705 for (idp = xmit_names; idp; idp = idp->next)
707 errlen += strlen(idp->id) + 2;
709 errmsg = alloca(errlen+3);
710 (void) strcpy(errmsg, errhd);
711 for (idp = xmit_names; idp; idp = idp->next)
714 strcat(errmsg, idp->id);
716 strcat(errmsg, ", ");
720 strcat(errmsg, "\n");
723 fputs(errmsg, sinkfp);
726 free_str_list(&xmit_names);
729 /* SMTP byte-stuffing */
734 SockWrite(bufp, 1, 1, sinkfp);
736 /* replace all LFs with CR-LF in the line */
739 char *newbufp = xmalloc(1 + strlen(bufp) * 2);
741 strcrlf(newbufp, bufp, strlen(bufp));
745 /* ship out the text line */
748 n = fwrite(bufp, 1, strlen(bufp), sinkfp);
750 n = SockWrite(bufp, 1, strlen(bufp), sinkfp);
756 error(0, errno, "writing message text");
760 signal(SIGCHLD, sigchld);
764 else if (outlevel == O_VERBOSE)
771 if (outlevel == O_VERBOSE)
778 /* close the delivery pipe, we'll reopen before next message */
780 signal(SIGCHLD, sigchld);
783 error(0, 0, "MDA exited abnormally or returned nonzero status");
789 /* write message terminator */
790 if (SMTP_eom(sinkfp) != SM_OK)
792 error(0, 0, "SMTP listener refused delivery");
802 kerberos_auth (socket, canonical)
803 /* authenticate to the server host using Kerberos V4 */
804 int socket; /* socket to server host */
805 const char *canonical; /* server name */
811 Key_schedule schedule;
814 ticket = ((KTEXT) (malloc (sizeof (KTEXT_ST))));
815 rem = (krb_sendauth (0L, socket, ticket, "pop",
817 ((char *) (krb_realmofhost (canonical))),
822 ((struct sockaddr_in *) 0),
823 ((struct sockaddr_in *) 0),
828 error(0, 0, "kerberos error %s", (krb_get_err_text (rem)));
833 #endif /* KERBEROS_V4 */
835 int do_protocol(ctl, proto)
836 /* retrieve messages from server using given protocol method table */
837 struct query *ctl; /* parsed options with merged-in defaults */
838 const struct method *proto; /* protocol method table */
845 if (ctl->authenticate == A_KERBEROS)
847 error(0, 0, "Kerberos support not linked.");
850 #endif /* KERBEROS_V4 */
852 /* lacking methods, there are some options that may fail */
855 /* check for unsupported options */
858 "Option --flush is not supported with %s",
862 else if (ctl->fetchall) {
864 "Option --all is not supported with %s",
869 if (!proto->getsizes && ctl->limit)
872 "Option --limit is not supported with %s",
879 tag[0] = '\0'; /* nuke any tag hanging out from previous query */
881 error_init(poll_interval == 0 && !logfile);
883 /* set up the server-nonresponse timeout */
884 sigsave = signal(SIGVTALRM, vtalarm_handler);
885 vtalarm(mytimeout = ctl->timeout);
887 if ((js = setjmp(restart)) == 1)
890 "timeout after %d seconds waiting for %s.",
891 ctl->timeout, ctl->servernames->id);
896 /* error message printed at point of longjmp */
901 char buf [POPBUFSIZE+1];
902 int *msgsizes, len, num, count, new, deletions = 0;
904 /* execute pre-initialization command, if any */
905 if (ctl->preconnect[0] && (ok = system(ctl->preconnect)))
907 sprintf(buf, "pre-connection command failed with status %d", ok);
913 /* open a socket to the mail server */
914 if ((sockfp = SockOpen(ctl->servernames->id,
915 ctl->port ? ctl->port : protocol->port)) == NULL)
917 if (errno != EHOSTUNREACH)
918 error(0, errno, "connecting to host");
924 if (ctl->authenticate == A_KERBEROS)
926 ok = kerberos_auth(fileno(sockfp), ctl->canonical_name);
929 vtalarm(ctl->timeout);
931 #endif /* KERBEROS_V4 */
933 /* accept greeting message from mail server */
934 ok = (protocol->parse_response)(sockfp, buf);
937 vtalarm(ctl->timeout);
939 /* try to get authorized to fetch mail */
940 shroud = ctl->password;
941 ok = (protocol->getauth)(sockfp, ctl, buf);
942 shroud = (char *)NULL;
947 vtalarm(ctl->timeout);
949 /* compute number of messages and number of new messages waiting */
950 ok = (protocol->getrange)(sockfp, ctl, &count, &new);
953 vtalarm(ctl->timeout);
955 /* show user how many messages we downloaded */
956 if (outlevel > O_SILENT)
958 error(0, 0, "No mail from %s@%s",
960 ctl->servernames->id);
963 if (new != -1 && (count - new) > 0)
964 error(0, 0, "%d message%s (%d seen) from %s@%s.",
965 count, count > 1 ? "s" : "", count-new,
967 ctl->servernames->id);
969 error(0, 0, "%d message%s from %s@%s.", count, count > 1 ? "s" : "",
971 ctl->servernames->id);
974 /* we may need to get sizes in order to check message limits */
975 msgsizes = (int *)NULL;
976 if (!ctl->fetchall && proto->getsizes && ctl->limit)
978 msgsizes = (int *)alloca(sizeof(int) * count);
980 ok = (proto->getsizes)(sockfp, count, msgsizes);
983 vtalarm(ctl->timeout);
989 if (new == -1 || ctl->fetchall)
991 ok = ((new > 0) ? PS_SUCCESS : PS_NOMAIL);
997 * What forces this code is that in POP3 and IMAP2BIS you can't
998 * fetch a message without having it marked `seen'. In IMAP4,
999 * on the other hand, you can (peek_capable is set to convey
1002 * The result of being unable to peek is that if there's
1003 * any kind of transient error (DNS lookup failure, or
1004 * sendmail refusing delivery due to process-table limits)
1005 * the message will be marked "seen" on the server without
1006 * having been delivered. This is not a big problem if
1007 * fetchmail is running in foreground, because the user
1008 * will see a "skipped" message when it next runs and get
1011 * But in daemon mode this leads to the message being silently
1012 * ignored forever. This is not acceptable.
1014 * We compensate for this by checking the error count from the
1015 * previous pass and forcing all messages to be considered new
1018 int force_retrieval = !peek_capable && (ctl->errcount > 0);
1022 /* read, forward, and delete messages */
1023 for (num = 1; num <= count; num++)
1025 int toolarge = msgsizes && (msgsizes[num-1] > ctl->limit);
1026 int fetch_it = ctl->fetchall ||
1027 (!toolarge && (force_retrieval || !(protocol->is_old && (protocol->is_old)(sockfp,ctl,num))));
1029 /* we may want to reject this message if it's old */
1032 if (outlevel > O_SILENT)
1034 error_build("skipping message %d", num);
1036 error_build(" (oversized, %d bytes)", msgsizes[num-1]);
1041 /* request a message */
1042 ok = (protocol->fetch)(sockfp, ctl, num, &len);
1045 vtalarm(ctl->timeout);
1047 if (outlevel > O_SILENT)
1049 error_build("reading message %d", num);
1051 error_build(" (%d bytes)", len);
1052 if (outlevel == O_VERBOSE)
1053 error_complete(0, 0, "");
1058 /* read the message and ship it to the output sink */
1059 ok = gen_readmsg(sockfp,
1061 protocol->delimited,
1065 vtalarm(ctl->timeout);
1067 /* tell the server we got it OK and resynchronize */
1068 if (protocol->trail)
1070 ok = (protocol->trail)(sockfp, ctl, num);
1073 vtalarm(ctl->timeout);
1078 * At this point in flow of control, either we've bombed
1079 * on a protocol error or had delivery refused by the SMTP
1080 * server (unlikely -- I've never seen it) or we've seen
1081 * `accepted for delivery' and the message is shipped.
1082 * It's safe to mark the message seen and delete it on the
1086 /* maybe we delete this message now? */
1087 if (protocol->delete
1088 && (fetch_it ? !ctl->keep : ctl->flush))
1091 if (outlevel > O_SILENT)
1092 error_complete(0, 0, " flushed");
1093 ok = (protocol->delete)(sockfp, ctl, num);
1096 vtalarm(ctl->timeout);
1097 delete_str(&ctl->newsaved, num);
1099 else if (outlevel > O_SILENT)
1100 error_complete(0, 0, " not flushed");
1102 /* perhaps this as many as we're ready to handle */
1103 if (ctl->fetchlimit && ctl->fetchlimit <= num)
1107 /* remove all messages flagged for deletion */
1108 if (protocol->expunge_cmd && deletions > 0)
1110 ok = gen_transact(sockfp, protocol->expunge_cmd);
1113 vtalarm(ctl->timeout);
1116 ok = gen_transact(sockfp, protocol->exit_cmd);
1124 ok = gen_transact(sockfp, protocol->exit_cmd);
1133 vtalarm(ctl->timeout);
1134 if (ok != 0 && ok != PS_SOCKET)
1135 gen_transact(sockfp, protocol->exit_cmd);
1146 msg = "authorization";
1149 msg = "missing or bad RFC822 header";
1155 msg = "client/server synchronization";
1158 msg = "client/server protocol";
1161 msg = "SMTP transaction";
1164 error(0, 0, "undefined");
1167 if (ok==PS_SOCKET || ok==PS_AUTHFAIL || ok==PS_SYNTAX || ok==PS_IOERR
1168 || ok==PS_ERROR || ok==PS_PROTOCOL || ok==PS_SMTP)
1169 error(0, 0, "%s error while fetching from %s", msg, ctl->servernames->id);
1172 signal(SIGVTALRM, sigsave);
1176 #if defined(HAVE_STDARG_H)
1177 void gen_send(FILE *sockfp, char *fmt, ... )
1178 /* assemble command in printf(3) style and send to the server */
1180 void gen_send(sockfp, fmt, va_alist)
1181 /* assemble command in printf(3) style and send to the server */
1182 FILE *sockfp; /* socket to which server is connected */
1183 const char *fmt; /* printf-style format */
1187 char buf [POPBUFSIZE+1];
1190 if (protocol->tagged)
1191 (void) sprintf(buf, "%s ", GENSYM);
1195 #if defined(HAVE_STDARG_H)
1200 vsprintf(buf + strlen(buf), fmt, ap);
1203 strcat(buf, "\r\n");
1204 SockWrite(buf, 1, strlen(buf), sockfp);
1206 if (outlevel == O_VERBOSE)
1210 if (shroud && (cp = strstr(buf, shroud)))
1211 memset(cp, '*', strlen(shroud));
1212 buf[strlen(buf)-1] = '\0';
1213 error(0, 0, "%s> %s", protocol->name, buf);
1217 #if defined(HAVE_STDARG_H)
1218 int gen_transact(FILE *sockfp, char *fmt, ... )
1219 /* assemble command in printf(3) style, send to server, accept a response */
1221 int gen_transact(sockfp, fmt, va_alist)
1222 /* assemble command in printf(3) style, send to server, accept a response */
1223 FILE *sockfp; /* socket to which server is connected */
1224 const char *fmt; /* printf-style format */
1229 char buf [POPBUFSIZE+1];
1232 if (protocol->tagged)
1233 (void) sprintf(buf, "%s ", GENSYM);
1237 #if defined(HAVE_STDARG_H)
1242 vsprintf(buf + strlen(buf), fmt, ap);
1245 strcat(buf, "\r\n");
1246 SockWrite(buf, 1, strlen(buf), sockfp);
1248 if (outlevel == O_VERBOSE)
1252 if (shroud && (cp = strstr(buf, shroud)))
1253 memset(cp, '*', strlen(shroud));
1254 buf[strlen(buf)-1] = '\0';
1255 error(0, 0, "%s> %s", protocol->name, buf);
1258 /* we presume this does its own response echoing */
1259 ok = (protocol->parse_response)(sockfp, buf);
1265 /* driver.c ends here */