/* 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>
#include <dealwithmxes.h>

int
processMXpart(char *formattedEmail, char *mailHost, struct mlfiPriv *privData) {
    int returnCode, returnSubCode;
    char tmpLog[MAXLOGLINE];
    
    
    logNow("entered processMXpart().", 3, privData);
    returnCode = dealWithMXes(formattedEmail, mailHost, privData);
    snprintf(tmpLog, MAXLOGLINE, "dealWithMXes() returned %d.", returnCode);
    switch (returnCode) {
        case 0:
    	    /* 0 - all of the MXes accepted emailAddress. */
    	    snprintf(tmpLog, MAXLOGLINE, "all of the MXes accepted mail to %s.", formattedEmail);
    	    logNow(&tmpLog[0], 2, privData);
    	    return CB_PASS;
    	    break;
	case 1:
	    /* 1 - one of the MXes accepted emailAddress. */
	    snprintf(tmpLog, MAXLOGLINE, "some of the MXes accepted mail to %s.", formattedEmail);
	    logNow(&tmpLog[0], 2, privData);
	    return CB_PASS;
	    break;
	case 2:
	    /* 2 - no MX for emailAddress found. */
	    /* need to check emailAddress on host and/or on sending host */
	    snprintf(tmpLog, MAXLOGLINE, "no MX records for the hostpart of %s found, need direct processing on the target host.", formattedEmail);
	    logNow(&tmpLog[0], 2, privData);
	    returnSubCode = checkServerResponse(formattedEmail, mailHost, NULL, 0, privData);
	    snprintf(tmpLog, MAXLOGLINE, "checkServerResponse() returned %d.", returnSubCode);
	    logNow(&tmpLog[0], 2, privData);
	    switch (returnSubCode) {
	        case 0:
	    	    /* 0 - email accepped */
	    	    snprintf(tmpLog, MAXLOGLINE, "Got relay accepting message to %s.", formattedEmail);
	    	    logNow(&tmpLog[0], 2, privData);
	    	    return(CB_PASS);
		    break;
		case 1:
		    /* 1 - email not accepted */
		    snprintf(tmpLog, MAXLOGLINE, "Got relay rejecting message to %s.", formattedEmail);
		    logNow(&tmpLog[0], 2, privData);
		    return(CB_REJECT);
	    	    break;
		case 2:
		    /* 2 - email tempfailed */
		    snprintf(tmpLog, MAXLOGLINE, "Got relay tempfailing message to %s.", formattedEmail);
		    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 to %s.", formattedEmail);
		    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.", formattedEmail);
		    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.", formattedEmail);
		    logNow(&tmpLog[0], 2, privData);
		    return(CB_TEMPFAIL);
		    break;
		case 6:
		default:
		    /* 6 - memory error */
		    snprintf(tmpLog, MAXLOGLINE, "Got a memory or some unknown error, returning relay tempfailing message from %s.", formattedEmail);
		    logNow(&tmpLog[0], 2, privData);
		    return(CB_TEMPFAIL);
		    break;
	    }
	    break;
	case 3:
	default:
	    /* 3 - some software/memory error happened. */
	    snprintf(tmpLog, MAXLOGLINE, "Got a memory or connection error while parsing message from %s.", formattedEmail);
	    logNow(&tmpLog[0], 2, privData);
	    return(CB_TEMPFAIL);
	    break;
	case 4:
	    /* 4 - none of the MXes accepted message. */
	    snprintf(tmpLog, MAXLOGLINE, "All MXes rejected message from %s.", formattedEmail);
	    logNow(&tmpLog[0], 2, privData);
	    return(CB_REJECT);
	    break;
	case 5:
	    /* 5 - MXes mostly tempfailed message. */
	    snprintf(tmpLog, MAXLOGLINE, "None of the MXes accepted message, some tempfailed message from %s.", formattedEmail);
	    logNow(&tmpLog[0], 2, privData);
	    return(CB_TEMPFAIL);
	    break;
    }
    /* some unknown code found */
    snprintf(tmpLog, MAXLOGLINE, "Unknown answer code found, while parsing message from %s.", formattedEmail);
    logNow(&tmpLog[0], 0, privData);
    return(CB_TEMPFAIL);
}
