- int i,buflen,actsize;
- char buf [MSGBUFSIZE];
- char frombuf [MSGBUFSIZE];
- char savec;
- int msgTop;
- int needFrom;
-
- time_t now;
-
- /* This keeps the retrieved message count for display purposes */
- static int msgnum = 0;
-
- /* set up for status message if outlevel allows it */
- if (outlevel > O_SILENT && outlevel < O_VERBOSE) {
- fprintf(stderr,"reading message %d",++msgnum);
- /* won't do the '...' if retrieved messages are being sent to stdout */
- if (mboxfd == 1) /* we're writing to stdout */
- fputs(".\n",stderr);
- else
- ;
- }
- else
- ;
-
-
- /* read the specified message content length from the server */
- actsize = 0;
- msgTop = !0;
- while (msgsize > 0) {
- buflen = msgsize <= MSGBUFSIZE ? msgsize : MSGBUFSIZE;
- /* read a bufferful */
- if (SockRead(socket, buf, buflen) == 0) {
-
- /* Check for Unix 'From' header, and add bogus one if it's not
- present -- only if not using an MDA.
- XXX -- should probably parse real From: header and use its
- address field instead of bogus 'POPmail' string.
- */
- if (!topipe && msgTop) {
- msgTop = 0;
- if (strlen(buf) >= strlen("From ")) {
- savec = *(buf + 5);
- *(buf + 5) = 0;
- needFrom = strcmp(buf,"From ") != 0;
- *(buf + 5) = savec;
- }
- else
- needFrom = 1;
- if (needFrom) {
- now = time(NULL);
- sprintf(frombuf,"From POPmail %s",ctime(&now));
- if (write(mboxfd,frombuf,strlen(frombuf)) < 0) {
- perror("POP2_stateXFER: write");
- return(-1);
- }
- }
- }
-
- /* write to folder, stripping CR chars in the process */
- for (i = 0; i < buflen; i++)
- if (*(buf + i) != '\r')
- if (write(mboxfd,buf + i,1) < 0) {
- perror("POP2_stateXFER: write");
- return(-1);
- }
- else
- ; /* it was written */
- else
- ; /* ignore CR character */
- }
- else
- return(-1); /* socket problem */
-
- /* write another . for every bufferful received */
- if (outlevel > O_SILENT && outlevel < O_VERBOSE && mboxfd != 1)
- fputc('.',stderr);
- else
- ;
- msgsize -= buflen;
- actsize += buflen;
- }
-
- if (!topipe) {
- /* The server may not write the extra newline required by the Unix
- mail folder format, so we write one here just in case */
- if (write(mboxfd,"\n",1) < 1) {
- perror("POP2_stateXFER: write");
- return(-1);
- }
- }
- else {
- /* the mailer might require some sort of termination string, send
- it if it is defined */
-#ifdef BINMAIL_TERM
- if (write(mboxfd,BINMAIL_TERM,strlen(BINMAIL_TERM)) < 0) {
- perror("POP2_stateXFER: write");
- return(-1);
- }
-#endif
- }
-
- /* finish up display output */
- if (outlevel == O_VERBOSE)
- fprintf(stderr,"(%d characters of message content)\n",actsize);
- else if (outlevel > O_SILENT && mboxfd != 0)
- fputc('\n',stderr);
- else
- ;
-
- return(actsize);