/* 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>

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