/* milter-callback - a sendmail milter to perform variuos sender verifications
 Copyright (C) 2008  Eugene M. Zheganin, milter-callback@norma.perm.ru

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/	   

#include <milter-callback.h>
#include <commonprocs.h>

extern char *logString;

int
processDirectPart(char *emailToTest, uint32_t serverIPRaw, struct mlfiPriv *privData) {
    
    struct in_addr serverIP;
    int returnCode;
	
    serverIP.s_addr = serverIPRaw;
    
    if (privData->skipDirect == 0 && privData->gracefulDNS == 0) {
        returnCode = checkServerResponse(emailToTest, NULL, &serverIP, 1);
        snprintf(logString, MAXLOGLINE, "checkServerResponse() returned %d.", returnCode);
        logNow(logString, 3);
        switch (returnCode) {
    	    case 0:
	        /* 0 - email accepped */
	        snprintf(logString, MAXLOGLINE, "Got relay accepting message to %s.", emailToTest);
	        logNow(logString, 2);
	        return(CB_PASS);
	        break;
	    case 1:
	        /* 1 - email not accepted */
	        snprintf(logString, MAXLOGLINE, "Got relay rejecting message to %s.", emailToTest);
	        logNow(logString, 2);
	        return(CB_REJECT);
	        break;
	    case 2:
		/* 2 - email tempfailed */
		snprintf(logString, MAXLOGLINE, "Got relay tempfailing message to %s.", emailToTest);
		logNow(logString, 2);
		return(CB_TEMPFAIL);
		break;
	    case 3:
	        /* 3 - socket error */
	        snprintf(logString, MAXLOGLINE, "Got a socket error, returning relay tempfailing message from %s.", emailToTest );
	        logNow(logString, 2);
	        return(CB_TEMPFAIL);
	        break;
	    case 4:
	        /* 4 - DNS error */
	        snprintf(logString, MAXLOGLINE, "Got a DNS error, returning relay tempfailing message from %s.", emailToTest);
	        logNow(logString, 2);
	        return(CB_TEMPFAIL);
		break;
	    case 5:
	        /* 5 - unknown answer code */
	        snprintf(logString, MAXLOGLINE, "Got an unknown SMTP answer code, returning relay tempfailing message from %s.", emailToTest);
	        logNow(logString, 2);
	        return(CB_TEMPFAIL);
	        break;
	    case 6:
	    default:
	        /* 6 - memory error */
	        snprintf(logString, MAXLOGLINE, "Got a memory or connection error, returning relay tempfailing message from %s.", emailToTest);
	        logNow(logString, 2);
	        return(CB_TEMPFAIL);
	        break;
	}
    } else {
        if (privData->skipDirect == 1) {
	    snprintf(logString, MAXLOGLINE, "Skipping reverse relaying of e-mail %s to relay %s [%s].", emailToTest, privData->resolvedRelayIP, privData->relayPresIP);
    	    logNow(logString, 2);
    	    return(CB_SKIP);
	} else {
	    snprintf(logString, MAXLOGLINE, "Skipping reverse relaying of e-mail %s to relay %s [%s] due to graceful DNS.", emailToTest, privData->resolvedRelayIP, privData->relayPresIP);
	    logNow(logString, 2);
	    return(CB_GRACEFUL);
	}
    }
}
