<commit_before>#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <mopher.h>

#define BUFLEN 1024

#define SPAMD_SPAMD "SPAMD/"
#define SPAMD_SPAMDLEN 6
#define SPAMD_VERSION "1.1"
#define SPAMD_VERSIONLEN 3
#define SPAMD_EX_OK "EX_OK"
#define SPAMD_EX_OKLEN 5
#define SPAMD_SPAM "Spam: "
#define SPAMD_SPAMLEN 6
#define SPAMD_TRUE "True ; "
#define SPAMD_TRUELEN 7
#define SPAMD_FALSE "False ; "
#define SPAMD_FALSELEN 8


static const char spamd_single[] = "Received: from %s (%s)\r\n\tby %s "
    "(envelope-sender <%s>) (%s) with SMTP id %s\r\n\tfor <%s>; %s\r\n";

static const char spamd_multi[] = "Received: from %s (%s)\r\n\tby %s "
    "(envelope-sender <%s>) (%s) with SMTP id %s;\r\n\t%s%s\r\n";

static char *spamd_symbols[] = { "spamd_spam", "spamd_score",
	"spamd_symbols", NULL};

static int
spamd_rdns_none(char *hostname, char *hostaddr)
{
	char *clean_hostname;
	int r = 0;

	clean_hostname = util_strdupenc(hostname, "[]")
	if (clean_hostname == NULL)
	{
		/*
                 * If we just ran out of memory. I don't care.
                 */
		return 0;
	}

	if (strcmp(clean_hostname, hostaddr) == 0)
	{
		r = 1;
	}

	free(clean_hostname);
	return r;
}

static int
spamd_header(var_t *attrs, char *header, int len)
{
	char *hostname;
	char *addrstr;
	char *helo;
	char *envfrom;
	VAR_INT_T recipients;
	char *envrcpt;
	char *queueid;
	time_t t;
	char timestamp[BUFLEN];
	char host[BUFLEN];
	struct tm tm;
	int r;
	const char *fmt;
	

	r = acl_symbol_dereference(attrs, "milter_received", &t,
		"milter_hostname", &hostname, "milter_helo", &helo,
		"milter_envfrom", &envfrom, "milter_envrcpt", &envrcpt,
		"milter_recipients", &recipients, "milter_queueid", &queueid,
		"milter_addrstr", &addrstr, NULL);
	if (r)
	{
		log_error("spamd_header: acl_symbol_dereference failed");
		return -1;
	}

	t = time(NULL);
	if (t == -1)
	{
		log_sys_error("spamd_header: time failed");
		return -1;
	}

	if (gmtime_r(&t, &tm) == NULL) {
		log_sys_error("spamd_header: gmtime failed");
		return -1;
	}

	if (strftime(timestamp, sizeof(timestamp),
	    "%a, %d %b %Y %H:%M:%S +0000", &tm) == 0)
	{
		log_sys_error("spamd_header: strftime failed");
		timestamp[0] = '\0';
	}

	if(recipients == 1)
	{
		fmt = spamd_single;
	}
	else
	{
		fmt = spamd_multi;
		envrcpt = "";
	}

	/*
 	 * Handle RDNS None
 	 */
	if (spamd_rdns_none(hostname, addrstr))
	{
		r = snprintf(host, sizeof(host), "[%s]", addrstr);
	}
	else
	{
		r = snprintf(host, sizeof(host), "%s [%s]", hostname, addrstr);
	}

	if (r >= sizeof(host))
	{
		log_error("spamd_header: buffer exhausted");
		return -1;
	}

	/*
         * Prevent double qouting e.g. <<<EMAIL>>>
         */
	clean_envfrom = util_strdupenc(envfrom, "<>")
	if (clean_envfrom == NULL)
	{
		log_error("spamd_header: util_strdupenc failed");
		return -1;
	}
		
	len = snprintf(header, len, fmt, helo, host, cf_hostname,
		clean_envfrom, BINNAME, queueid, envrcpt, timestamp);

	free(clean_envfrom);

	return len;
}


int
spamd_query(milter_stage_t stage, char *name, var_t *attrs)
{
	int sock = 0;
	var_t *symbols = NULL;
	int n;<commit_msg>Fixed syntax errors introduced by last commit<commit_after>#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <mopher.h>

#define BUFLEN 1024

#define SPAMD_SPAMD "SPAMD/"
#define SPAMD_SPAMDLEN 6
#define SPAMD_VERSION "1.1"
#define SPAMD_VERSIONLEN 3
#define SPAMD_EX_OK "EX_OK"
#define SPAMD_EX_OKLEN 5
#define SPAMD_SPAM "Spam: "
#define SPAMD_SPAMLEN 6
#define SPAMD_TRUE "True ; "
#define SPAMD_TRUELEN 7
#define SPAMD_FALSE "False ; "
#define SPAMD_FALSELEN 8


static const char spamd_single[] = "Received: from %s (%s)\r\n\tby %s "
    "(envelope-sender <%s>) (%s) with SMTP id %s\r\n\tfor <%s>; %s\r\n";

static const char spamd_multi[] = "Received: from %s (%s)\r\n\tby %s "
    "(envelope-sender <%s>) (%s) with SMTP id %s;\r\n\t%s%s\r\n";

static char *spamd_symbols[] = { "spamd_spam", "spamd_score",
	"spamd_symbols", NULL};

static int
spamd_rdns_none(char *hostname, char *hostaddr)
{
	char *clean_hostname;
	int r = 0;

	clean_hostname = util_strdupenc(hostname, "[]");
	if (clean_hostname == NULL)
	{
		/*
                 * If we just ran out of memory. I don't care.
                 */
		return 0;
	}

	if (strcmp(clean_hostname, hostaddr) == 0)
	{
		r = 1;
	}

	free(clean_hostname);
	return r;
}

static int
spamd_header(var_t *attrs, char *header, int len)
{
	char *hostname;
	char *addrstr;
	char *helo;
	char *envfrom;
	VAR_INT_T recipients;
	char *envrcpt;
	char *queueid;
	time_t t;
	char timestamp[BUFLEN];
	char host[BUFLEN];
	struct tm tm;
	int r;
	const char *fmt;
	

	r = acl_symbol_dereference(attrs, "milter_received", &t,
		"milter_hostname", &hostname, "milter_helo", &helo,
		"milter_envfrom", &envfrom, "milter_envrcpt", &envrcpt,
		"milter_recipients", &recipients, "milter_queueid", &queueid,
		"milter_addrstr", &addrstr, NULL);
	if (r)
	{
		log_error("spamd_header: acl_symbol_dereference failed");
		return -1;
	}

	t = time(NULL);
	if (t == -1)
	{
		log_sys_error("spamd_header: time failed");
		return -1;
	}

	if (gmtime_r(&t, &tm) == NULL) {
		log_sys_error("spamd_header: gmtime failed");
		return -1;
	}

	if (strftime(timestamp, sizeof(timestamp),
	    "%a, %d %b %Y %H:%M:%S +0000", &tm) == 0)
	{
		log_sys_error("spamd_header: strftime failed");
		timestamp[0] = '\0';
	}

	if(recipients == 1)
	{
		fmt = spamd_single;
	}
	else
	{
		fmt = spamd_multi;
		envrcpt = "";
	}

	/*
 	 * Handle RDNS None
 	 */
	if (spamd_rdns_none(hostname, addrstr))
	{
		r = snprintf(host, sizeof(host), "[%s]", addrstr);
	}
	else
	{
		r = snprintf(host, sizeof(host), "%s [%s]", hostname, addrstr);
	}

	if (r >= sizeof(host))
	{
		log_error("spamd_header: buffer exhausted");
		return -1;
	}

	/*
         * Prevent double qouting e.g. <<<EMAIL>>>
         */
	clean_envfrom = util_strdupenc(envfrom, "<>");
	if (clean_envfrom == NULL)
	{
		log_error("spamd_header: util_strdupenc failed");
		return -1;
	}
		
	len = snprintf(header, len, fmt, helo, host, cf_hostname,
		clean_envfrom, BINNAME, queueid, envrcpt, timestamp);

	free(clean_envfrom);

	return len;
}


int
spamd_query(milter_stage_t stage, char *name, var_t *attrs)
{
	int sock = 0;
	var_t *symbols = NULL;
	int n;
{'fraction_non_alphanumeric': 0.16036546943919344, 'fraction_numerical': 0.009766855702583491, 'mean_word_length': 3.9178931061192874, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6570397', 'n_tokens_mistral': 2836, 'n_tokens_neox': 2677, 'n_words': 621}