/* RE_SID: @(%)/tmp_mnt/vol/sha/qsilver_SCCS/unix/pop3/solaris2/SCCS/s.pop_send.c 1.6 93/02/26 17:38:37 SMI */
/*
 * Copyright (c) 1989 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#ifndef lint
static char copyright[] = "Copyright (c) 1990 Regents of the University of California.\nAll rights reserved.\n";
static char SccsId[] = "@(#)@(#)pop_send.c	2.1  2.1 3/18/91";
#endif not lint

#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include "popper.h"

/* 
 *  send:   Send the header and a specified number of lines 
 *          from a mail message to a POP client.
 */

pop_send(p)
POP     *   p;
{
    MsgInfoList         *   mp;         /*  Pointer to message info list */
    register int            msg_num;
    register int            msg_lines;
    char                    buffer[MAXMSGLINELEN];
    /* We need to do basically the same things we did in openit(),
     * when reading a message -- we skip the From line and the
     * Content-Length: line.
     */
    int		i;
    int		in_header = 1;
    int		start_of_line = 1;
    int		after_skipped_line = 0;
    int		len;

    /*  Convert the first parameter into an integer */
    msg_num = atoi(p->pop_parm[1]);

    /*  Is requested message out of range? */
    if ((msg_num < 1) || (msg_num > p->msg_count))
        return (pop_msg (p,POP_FAILURE,"Message %d does not exist.",msg_num));

    /*  Get a pointer to the message in the message list */
    mp = &p->mlp[msg_num-1];

    /*  Is the message flagged for deletion? */
    if (mp->del_flag)
        return (pop_msg (p,POP_FAILURE,
            "Message %d has been deleted.",msg_num));

    /*  If this is a TOP command, get the number of lines to send */
    if (strcmp(p->pop_command,"top") == 0) {
        /*  Convert the second parameter into an integer */
        msg_lines = atoi(p->pop_parm[2]);
    }
    else {
        /*  Assume that a RETR (retrieve) command was issued */
        msg_lines = -1;
        /*  Flag the message as retreived */
        mp->retr_flag = TRUE;
    }
    
    /*  Display the number of bytes in the message */
    pop_msg(p,POP_SUCCESS,"%lu octets",mp->octets);

    /*  Position to the start of the message */
    (void)fseek(p->drop,mp->offset,0);

    /* Write the header */
    for ( i = 0; i < mp->msg_fgets; i++ ) {
	if ( !fgets( buffer, MAXMSGLINELEN, p->drop ) ) break;
	len = strlen( buffer );

	if ( i == 0 ) {
	    after_skipped_line = 1;	/* must be the "From " line */

	} else if ( (len == 1) && (buffer[0] == '\n') ) {
	    after_skipped_line = 0;	/* end of header line */

	} else if ( strncasecmp( "Content-Length:", buffer, 15 ) == NULL ) {
	    after_skipped_line = 1;	/* Skip this field */

	} else if ( !isascii(buffer[0]) || !isspace(buffer[0]) ) {
	    after_skipped_line = 0;	/* else some other header field */
	}
	/* else it was a folded header line -- handle it like the
	 * preceeding header line.
	 */

	if ( !after_skipped_line ) {
            pop_sendline( p, buffer );
	}
        /*  A single newline (blank line) signals the end of the header.
	 *  pop_sendline() converts this to a NULL, so that's what we look for.
	 */
	if ( ! *buffer ) break;
    }
    i++;		/* in case we saw the blank line */

    /* Now send the message body: if msg_lines > 0, we send the first
     * msg_lines lines, otherwise we send them all
     */
    for ( ; i < mp->msg_fgets; i++ ) {
	if ( !fgets( buffer, MAXMSGLINELEN, p->drop ) ) break;
	len = strlen( buffer );

        /*  Decrement the lines sent (for a TOP command) */
        if (   (msg_lines >= 0)
	    && (len > 1) && (buffer[len-1] == '\n')
	    && (msg_lines-- == 0)      ) break;
        pop_sendline( p, buffer );
    }

    /*  "." signals the end of a multi-line transmission */
    (void)fputs(".\r\n",p->output);
    (void)fflush(p->output);

    return(POP_SUCCESS);
}

/*
 *  sendline:   Send a line of a multi-line response to a client.
 */
pop_sendline(p,buffer)
POP         *   p;
char        *   buffer;
{
    char        *   bp;

    /*  Byte stuff lines that begin with the temirnation octet */
    if (*buffer == POP_TERMINATE) (void)fputc(POP_TERMINATE,p->output);

    /*  Look for a <NL> in the buffer */
    if (bp = strchr(buffer,NEWLINE)) *bp = 0;

    /*  Send the line to the client */
    (void)fputs(buffer,p->output);

#ifdef DEBUG
    if(p->debug)pop_log(p,POP_DEBUG,"Sending line \"%s\"",buffer);
#endif DEBUG

    /*  Put a <CR><NL> if a newline was removed from the buffer */
    if (bp) (void)fputs ("\r\n",p->output);
}
