view src/checkwait_smtpreply.c @ 0:21ce01de8109

Initial revision
author mmj
date Thu, 22 Apr 2004 04:02:09 +1000
parents
children 6089f38bb228
line wrap: on
line source

/* Copyright (C) 2002, 2003 Mads Martin Joergensen <mmj at mmj.dk>
 *
 * $Id$
 *
 * This file is redistributable under version 2 of the GNU General
 * Public License as described at http://www.gnu.org/licenses/gpl.txt
 */

#include <stdio.h>
#include <unistd.h>
#include "checkwait_smtpreply.h"
#include "config.h"

#define USEC_WAIT 1
#define LOOP_WAIT 10000

int checkwait_smtpreply(int sockfd, int replytype)
{
	size_t len = 0;
	char smtpreply[READ_BUFSIZE];
	int timer = 0;

	do {
		if(replytype != MLMMJ_QUIT && timer > 10) {
			usleep(USEC_WAIT);
			timer++;
		}
		len += read(sockfd, (smtpreply+len), READ_BUFSIZE-len);
	} while(smtpreply[len-1] != '\n' && timer < LOOP_WAIT);

	smtpreply[len] = 0;
#if MLMMJ_DEBUG
	printf("replytype = [%d], smtpreply = [%s]\n", replytype, smtpreply);
#endif
	if(timer > LOOP_WAIT) {
		printf("Timed out in waiting for reply--will try later\n");
		return -1;
	}

	switch(replytype) {
	case MLMMJ_CONNECT:
		if(smtpreply[0] != '2' && smtpreply[1] != '2')
			return MLMMJ_CONNECT;
		printf("Connected: ok\n");
		break;
	case MLMMJ_HELO:
		if(smtpreply[0] != '2' && smtpreply[1] != '5')
			return MLMMJ_HELO;
		printf("HELO: ok\n");
		break;
	case MLMMJ_FROM:
		if(smtpreply[0] != '2' && smtpreply[1] != '5')
			return MLMMJ_FROM;
		printf("MAIL FROM: ok\n");
		break;
	case MLMMJ_RCPTTO:
		if(smtpreply[0] != '2' && smtpreply[1] != '5')
			return MLMMJ_RCPTTO;
		printf("RCPT TO: ok\n");

		break;
	case MLMMJ_DATA:
		if(smtpreply[0] != '3' && smtpreply[1] != '5')
			return MLMMJ_DATA;
		printf("DATA: ok 35x\n");
		break;
	case MLMMJ_DOT:
		if(smtpreply[0] != '2' && smtpreply[1] != '5')
			return MLMMJ_DOT;
		printf("Mail queued: ok\n");
		break;
	case MLMMJ_QUIT:
		if(smtpreply[0] != '2' && smtpreply[1] != '2')
			return MLMMJ_QUIT;
		printf("Closed connection\n");
		break;
	case MLMMJ_RSET:
		if(smtpreply[0] != '2' && smtpreply[1] != '5')
			return MLMMJ_RSET;
		printf("RSET: ok");
		break;
	default:
		break;
	}

	return 0;
}