summaryrefslogtreecommitdiffstats
path: root/pigeonhole/src/lib-sieve/sieve-smtp.c
blob: 40b40ea69a98163c666c45ca025aec99853ceb47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
 */
#include "lib.h"
#include "smtp-address.h"

#include "sieve-common.h"
#include "sieve-smtp.h"

struct sieve_smtp_context {
	const struct sieve_script_env *senv;
	void *handle;
	
	bool sent:1;
};

bool sieve_smtp_available
(const struct sieve_script_env *senv)
{
	return ( senv->smtp_start != NULL && senv->smtp_add_rcpt != NULL &&
		senv->smtp_send != NULL && senv->smtp_finish != NULL );
}

struct sieve_smtp_context *sieve_smtp_start
(const struct sieve_script_env *senv,
	const struct smtp_address *mail_from)
{
	struct sieve_smtp_context *sctx;
	void *handle;

	if ( !sieve_smtp_available(senv) )
		return NULL;

	handle = senv->smtp_start(senv, mail_from);
	i_assert( handle != NULL );
	
	sctx = i_new(struct sieve_smtp_context, 1);
	sctx->senv = senv;
	sctx->handle = handle;

	return sctx;
}

void sieve_smtp_add_rcpt
(struct sieve_smtp_context *sctx,
	const struct smtp_address *rcpt_to)
{
	i_assert(!sctx->sent);
	sctx->senv->smtp_add_rcpt(sctx->senv, sctx->handle, rcpt_to);
}

struct ostream *sieve_smtp_send
(struct sieve_smtp_context *sctx)
{
	i_assert(!sctx->sent);
	sctx->sent = TRUE;

	return sctx->senv->smtp_send(sctx->senv, sctx->handle);
}

struct sieve_smtp_context *sieve_smtp_start_single
(const struct sieve_script_env *senv,
	const struct smtp_address *rcpt_to,
		const struct smtp_address *mail_from,
	struct ostream **output_r)
{
	struct sieve_smtp_context *sctx;

	sctx = sieve_smtp_start(senv, mail_from);
	sieve_smtp_add_rcpt(sctx, rcpt_to);
	*output_r = sieve_smtp_send(sctx);

	return sctx;
}

void sieve_smtp_abort
(struct sieve_smtp_context *sctx)
{
	const struct sieve_script_env *senv = sctx->senv;
	void *handle = sctx->handle;

	i_free(sctx);
	i_assert(senv->smtp_abort != NULL);
	senv->smtp_abort(senv, handle);
}

int sieve_smtp_finish
(struct sieve_smtp_context *sctx, const char **error_r)
{
	const struct sieve_script_env *senv = sctx->senv;
	void *handle = sctx->handle;

	i_free(sctx);
	return senv->smtp_finish(senv, handle, error_r);
}