<commit_before>/* pbnmsg_isc_email.c - email protobuf nmsg module */

/*
 * Copyright (c) 2008 by Internet Systems Consortium, Inc. ("ISC")
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/* Import. */

#include "nmsg_port.h"

#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <nmsg.h>
#include <nmsg/asprintf.h>
#include <nmsg/pbmod.h>

#include "email.pb-c.h"

/* Data structures. */

typedef enum {
	mode_keyval,
	mode_headers,
	mode_skip
} input_mode;

struct email_clos {
	Nmsg__Isc__Email	*eh;
	char			*headers, *headers_cur;
	input_mode		mode;
	size_t			max;
	ssize_t			rem;
};

struct email_types {
	Nmsg__Isc__EmailType	type;
	const char		*name;
};

/* Data. */

struct email_types module_email_types[] = {
	{ NMSG__ISC__EMAIL_TYPE__unknown,	"unknown"	},
	{ NMSG__ISC__EMAIL_TYPE__spamtrap,	"spamtrap"	},
	{ NMSG__ISC__EMAIL_TYPE__rej_network,	"rej_network"	},
	{ NMSG__ISC__EMAIL_TYPE__rej_content,	"rej_content"	},
	{ NMSG__ISC__EMAIL_TYPE__rej_user,	"rej_user"	},
	{ 0, NULL }
};

/* Macros. */

#define MSGTYPE_EMAILHDR_ID	2
#define MSGTYPE_EMAILHDR_NAME	"email"

#define PAYLOAD_MAXSZ		1204

#define linecmp(line, str) (strncmp(line, str, sizeof(str) - 1) == 0)
#define lineval(line, str) (line + sizeof(str) - 1)

/* Forward. */

static bool rem_avail(ssize_t rem, size_t len);
static nmsg_res add_field(struct email_clos *, const char *,
			  ProtobufCBinaryData *, protobuf_c_boolean *);
static nmsg_res add_field_ip(struct email_clos *, const char *,
			     ProtobufCBinaryData *, protobuf_c_boolean *);
static nmsg_res add_field_strarray(struct email_clos *, const char *,
				   ProtobufCBinaryData **, size_t *);
static nmsg_res add_field_type(const char *, Nmsg__Isc__EmailType *,
			       protobuf_c_boolean *);
static const char *email_type_to_str(Nmsg__Isc__EmailType);
static nmsg_res finalize_pbuf(struct email_clos *, uint8_t **pbuf, size_t *);
static size_t trim_newline(char *);
static void reset_eh(struct email_clos *);

/* Exported via module context. */

static void *email_init(size_t max, int debug);
static nmsg_res email_fini(void *);
static nmsg_res email_pbuf_to_pres(Nmsg__NmsgPayload *, char **pres,
				   const char *endline);
static nmsg_res email_pres_to_pbuf(void *, const char *line,
				   uint8_t **pbuf, size_t *);

/* Export. */

struct nmsg_pbmod nmsg_pbmod_ctx = {
	.pbmver = NMSG_PBMOD_VERSION,
	.init = &email_init,
	.fini = &email_fini,
	.pbuf2pres = &email_pbuf_to_pres,
	.pres2pbuf = &email_pres_to_pbuf,
	.vendor = NMSG_VENDOR_ISC,
	.msgtype = {
		{ MSGTYPE_EMAILHDR_ID, MSGTYPE_EMAILHDR_NAME },
		NMSG_IDNAME_END
	}
};

/* Exported via module context. */

static void *
email_init(size_t max, int debug) {
	struct email_clos *clos;

	if (debug > 2)
		fprintf(stderr, "email: starting module\n");

	clos = calloc(1, sizeof(*clos));
	if (clos == NULL)
		return (NULL);

	clos->max = (max > PAYLOAD_MAXSZ) ? PAYLOAD_MAXSZ : max;
	clos->rem = clos->max;

	clos->headers_cur = clos->headers = calloc(1, clos->max);
	if (clos->headers == NULL)<commit_msg>email: EMAILHDR -> EMAIL<commit_after>/* pbnmsg_isc_email.c - email protobuf nmsg module */

/*
 * Copyright (c) 2008 by Internet Systems Consortium, Inc. ("ISC")
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/* Import. */

#include "nmsg_port.h"

#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <nmsg.h>
#include <nmsg/asprintf.h>
#include <nmsg/pbmod.h>

#include "email.pb-c.h"

/* Data structures. */

typedef enum {
	mode_keyval,
	mode_headers,
	mode_skip
} input_mode;

struct email_clos {
	Nmsg__Isc__Email	*eh;
	char			*headers, *headers_cur;
	input_mode		mode;
	size_t			max;
	ssize_t			rem;
};

struct email_types {
	Nmsg__Isc__EmailType	type;
	const char		*name;
};

/* Data. */

struct email_types module_email_types[] = {
	{ NMSG__ISC__EMAIL_TYPE__unknown,	"unknown"	},
	{ NMSG__ISC__EMAIL_TYPE__spamtrap,	"spamtrap"	},
	{ NMSG__ISC__EMAIL_TYPE__rej_network,	"rej_network"	},
	{ NMSG__ISC__EMAIL_TYPE__rej_content,	"rej_content"	},
	{ NMSG__ISC__EMAIL_TYPE__rej_user,	"rej_user"	},
	{ 0, NULL }
};

/* Macros. */

#define MSGTYPE_EMAIL_ID	2
#define MSGTYPE_EMAIL_NAME	"email"

#define PAYLOAD_MAXSZ		1204

#define linecmp(line, str) (strncmp(line, str, sizeof(str) - 1) == 0)
#define lineval(line, str) (line + sizeof(str) - 1)

/* Forward. */

static bool rem_avail(ssize_t rem, size_t len);
static nmsg_res add_field(struct email_clos *, const char *,
			  ProtobufCBinaryData *, protobuf_c_boolean *);
static nmsg_res add_field_ip(struct email_clos *, const char *,
			     ProtobufCBinaryData *, protobuf_c_boolean *);
static nmsg_res add_field_strarray(struct email_clos *, const char *,
				   ProtobufCBinaryData **, size_t *);
static nmsg_res add_field_type(const char *, Nmsg__Isc__EmailType *,
			       protobuf_c_boolean *);
static const char *email_type_to_str(Nmsg__Isc__EmailType);
static nmsg_res finalize_pbuf(struct email_clos *, uint8_t **, size_t *);
static size_t trim_newline(char *);
static void reset_eh(struct email_clos *);

/* Exported via module context. */

static void *email_init(size_t max, int debug);
static nmsg_res email_fini(void *);
static nmsg_res email_pbuf_to_pres(Nmsg__NmsgPayload *, char **pres,
				   const char *endline);
static nmsg_res email_pres_to_pbuf(void *, const char *line,
				   uint8_t **pbuf, size_t *);

/* Export. */

struct nmsg_pbmod nmsg_pbmod_ctx = {
	.pbmver = NMSG_PBMOD_VERSION,
	.init = &email_init,
	.fini = &email_fini,
	.pbuf2pres = &email_pbuf_to_pres,
	.pres2pbuf = &email_pres_to_pbuf,
	.vendor = NMSG_VENDOR_ISC,
	.msgtype = {
		{ MSGTYPE_EMAIL_ID, MSGTYPE_EMAIL_NAME },
		NMSG_IDNAME_END
	}
};

/* Exported via module context. */

static void *
email_init(size_t max, int debug) {
	struct email_clos *clos;

	if (debug > 2)
		fprintf(stderr, "email: starting module\n");

	clos = calloc(1, sizeof(*clos));
	if (clos == NULL)
		return (NULL);

	clos->max = (max > PAYLOAD_MAXSZ) ? PAYLOAD_MAXSZ : max;
	clos->rem = clos->max;

	clos->headers_cur = clos->headers = calloc(1, clos->max);
	if (clos->headers == NULL)
{'fraction_non_alphanumeric': 0.11398076162867309, 'fraction_numerical': 0.005270786664909738, 'mean_word_length': 4.576781778104335, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 42, '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': '2616061', 'n_tokens_mistral': 2965, 'n_tokens_neox': 2783, 'n_words': 728}