<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <haka/alert_module.h>
#include <haka/log.h>
#include <haka/error.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

#define FROM		"<<EMAIL>>"
#define BODY_SIZE	2048

typedef enum {
	NONE,
	SSL,
	STARTTLS
} AUTH;

struct email {
	char *body;
	size_t size;
};

struct mail_alerter {
	struct alerter_module module;
	char *server;
	char *username;
	char *password;
	AUTH auth;
	curl_slist *recipients;
};

CURL *curl;
CURLcode res = CURLE_OK;
struct email email_ctx;

static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *instream)
{
	struct email *email_ctx = (struct email *)instream;
	const char *data;

	if ((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
	  return 0;
	}

	data = email_ctx->body;

	if (email_ctx->size) {
	  size_t len = strlen(data);
	  memcpy(ptr, data, len);
	  email_ctx->body += len;
	  email_ctx->size -= len;

	  return len;
	}

	return 0;
}

static int init(struct parameters *args)
{
	curl = curl_easy_init();
	return 0;
}

static void cleanup()
{
	/* Always cleanup */
	curl_easy_cleanup(curl);
}

static bool send_mail(struct mail_alerter *state, uint64 id, const struct time *time, const struct alert *alert, bool update)
{
	/*************************************************
	 * https://curl.haxx.se/libcurl/c/smtp-mail.html *
	 * https://curl.haxx.se/libcurl/c/smtp-ssl.html  *
	 * https://curl.haxx.se/libcurl/c/smtp-tls.html  *
	 *************************************************/
	if (curl) {
		/* SSL/TLS or STARTTLS based connection */
		if (state->auth) {
			/* Set username and password */
			curl_easy_setopt(curl, CURLOPT_USERNAME, state->username);
			curl_easy_setopt(curl, CURLOPT_PASSWORD, state->password);
		}

		char server[128];
		sprintf(server, "smtp%s://%s", state->auth == SSL ? "s" : "", state->server);

		curl_easy_setopt(curl, CURLOPT_URL, server);

		if (state->auth) {
#ifdef SKIP_PEER_VERIFICATION
			curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif

#ifdef SKIP_HOSTNAME_VERIFICATION
			curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
		}

		if (state->auth == STARTTLS) {
			curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
		}

		/* Mail sender */
		curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);

		/* Mail recipients */
		curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, state->recipients);

		/* Message body */
		char body[BODY_SIZE];
		snprintf(body, BODY_SIZE, "Subject: [Haka] alert: %s%s\r\n",
			update ? "update " : "", alert_tostring(id, time, alert, "", " ", false));

		email_ctx.body = body;
		email_ctx.size = strlen(body);

		curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
		curl_easy_setopt(curl, CURLOPT_READDATA, &email_ctx);
		curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

		if (getlevel(log) == HAKA_LOG_DEBUG)
			curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

		/* Send the message */
		res = curl_easy_perform(curl);

		/* Check for errors */
		if (res != CURLE_OK) {
			haka.log.error("curl_easy_perform() failed: %s\n",
					curl_easy_strerror(res));
			return false;
		}
	}
	return true;
}<commit_msg>curl_easy_init check<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <haka/alert_module.h>
#include <haka/log.h>
#include <haka/error.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

#define FROM		"<<EMAIL>>"
#define BODY_SIZE	2048

typedef enum {
	NONE,
	SSL,
	STARTTLS
} AUTH;

struct email {
	char *body;
	size_t size;
};

struct mail_alerter {
	struct alerter_module module;
	char *server;
	char *username;
	char *password;
	AUTH auth;
	curl_slist *recipients;
};

CURL *curl;
CURLcode res = CURLE_OK;
struct email email_ctx;

static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *instream)
{
	struct email *email_ctx = (struct email *)instream;
	const char *data;

	if ((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
	  return 0;
	}

	data = email_ctx->body;

	if (email_ctx->size) {
	  size_t len = strlen(data);
	  memcpy(ptr, data, len);
	  email_ctx->body += len;
	  email_ctx->size -= len;

	  return len;
	}

	return 0;
}

static int init(struct parameters *args)
{
	curl = curl_easy_init();
	return 0;
}

static void cleanup()
{
	/* Always cleanup */
	curl_easy_cleanup(curl);
}

static bool send_mail(struct mail_alerter *state, uint64 id, const struct time *time, const struct alert *alert, bool update)
{
	/*************************************************
	 * https://curl.haxx.se/libcurl/c/smtp-mail.html *
	 * https://curl.haxx.se/libcurl/c/smtp-ssl.html  *
	 * https://curl.haxx.se/libcurl/c/smtp-tls.html  *
	 *************************************************/
	if (!curl) {
		haka.log.error("curl_easy_init() failed\n");
		return false;
	}

	/* SSL/TLS or STARTTLS based connection */
	if (state->auth) {
		/* Set username and password */
		curl_easy_setopt(curl, CURLOPT_USERNAME, state->username);
		curl_easy_setopt(curl, CURLOPT_PASSWORD, state->password);
	}

	char server[128];
	sprintf(server, "smtp%s://%s", state->auth == SSL ? "s" : "", state->server);

	curl_easy_setopt(curl, CURLOPT_URL, server);

	if (state->auth) {
#ifdef SKIP_PEER_VERIFICATION
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif

#ifdef SKIP_HOSTNAME_VERIFICATION
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
	}

	if (state->auth == STARTTLS) {
		curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
	}

	/* Mail sender */
	curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);

	/* Mail recipients */
	curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, state->recipients);

	/* Message body */
	char body[BODY_SIZE];
	snprintf(body, BODY_SIZE, "Subject: [Haka] alert: %s%s\r\n",
		update ? "update " : "", alert_tostring(id, time, alert, "", " ", false));

	email_ctx.body = body;
	email_ctx.size = strlen(body);

	curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
	curl_easy_setopt(curl, CURLOPT_READDATA, &email_ctx);
	curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

	if (getlevel(log) == HAKA_LOG_DEBUG)
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

	/* Send the message */
	res = curl_easy_perform(curl);

	/* Check for errors */
	if (res != CURLE_OK) {
		haka.log.error("curl_easy_perform() failed: %s\n",
				curl_easy_strerror(res));
		return false;
	}
	return true;
}
{'fraction_non_alphanumeric': 0.17038478814193742, 'fraction_numerical': 0.006887258571642461, 'mean_word_length': 4.339728217426059, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 45, 'https://': 6, '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': '7188702', 'n_tokens_mistral': 2872, 'n_tokens_neox': 2524, 'n_words': 575}