summaryrefslogtreecommitdiffabout
path: root/libetpan/src/low-level/smtp/mailsmtp_helper.c
Unidiff
Diffstat (limited to 'libetpan/src/low-level/smtp/mailsmtp_helper.c') (more/less context) (ignore whitespace changes)
-rw-r--r--libetpan/src/low-level/smtp/mailsmtp_helper.c228
1 files changed, 228 insertions, 0 deletions
diff --git a/libetpan/src/low-level/smtp/mailsmtp_helper.c b/libetpan/src/low-level/smtp/mailsmtp_helper.c
new file mode 100644
index 0000000..69b73cc
--- a/dev/null
+++ b/libetpan/src/low-level/smtp/mailsmtp_helper.c
@@ -0,0 +1,228 @@
1/*
2 * libEtPan! -- a mail stuff library
3 *
4 * Copyright (C) 2001, 2005 - DINH Viet Hoa
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the libEtPan! project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32/*
33 * $Id$
34 */
35
36#include "mailsmtp.h"
37#include <string.h>
38#include <stdlib.h>
39#include "mail.h"
40
41int mailsmtp_init(mailsmtp * session)
42{
43 int r;
44
45 r = mailesmtp_ehlo(session);
46
47 if (r == MAILSMTP_NO_ERROR)
48 return MAILSMTP_NO_ERROR;
49
50 r = mailsmtp_helo(session);
51 if (r == MAILSMTP_NO_ERROR)
52 return MAILSMTP_NO_ERROR;
53
54 return r;
55}
56
57
58
59int mailesmtp_send(mailsmtp * session,
60 const char * from,
61 int return_full,
62 const char * envid,
63 clist * addresses,
64 const char * message, size_t size)
65{
66 int r;
67 clistiter * l;
68
69 if (!session->esmtp)
70 return mailsmtp_send(session, from, addresses, message, size);
71
72 r = mailesmtp_mail(session, from, return_full, envid);
73 if (r != MAILSMTP_NO_ERROR)
74 return r;
75
76 for(l = clist_begin(addresses) ; l != NULL; l = clist_next(l)) {
77 struct esmtp_address * addr;
78
79 addr = clist_content(l);
80
81 r = mailesmtp_rcpt(session, addr->address, addr->notify, addr->orcpt);
82 if (r != MAILSMTP_NO_ERROR)
83 return r;
84 }
85
86 r = mailsmtp_data(session);
87 if (r != MAILSMTP_NO_ERROR)
88 return r;
89
90 r = mailsmtp_data_message(session, message, size);
91 if (r != MAILSMTP_NO_ERROR)
92 return r;
93
94 return MAILSMTP_NO_ERROR;
95}
96
97int mailsmtp_send(mailsmtp * session,
98 const char * from,
99 clist * addresses,
100 const char * message, size_t size)
101{
102 int r;
103 clistiter * l;
104
105 r = mailsmtp_mail(session, from);
106 if (r != MAILSMTP_NO_ERROR)
107 return r;
108
109 for(l = clist_begin(addresses) ; l != NULL; l = clist_next(l)) {
110 struct esmtp_address * addr;
111
112 addr = clist_content(l);
113
114 r = mailsmtp_rcpt(session, addr->address);
115 if (r != MAILSMTP_NO_ERROR)
116 return r;
117 }
118
119 r = mailsmtp_data(session);
120 if (r != MAILSMTP_NO_ERROR)
121 return r;
122
123 r = mailsmtp_data_message(session, message, size);
124 if (r != MAILSMTP_NO_ERROR)
125 return r;
126
127 return MAILSMTP_NO_ERROR;
128}
129
130
131
132
133
134
135
136
137
138
139
140
141
142/* esmtp addresses and smtp addresses */
143
144static struct esmtp_address * esmtp_address_new(char * addr,
145 int notify, char * orcpt)
146{
147 struct esmtp_address * esmtpa;
148
149 esmtpa = malloc(sizeof(* esmtpa));
150 if (esmtpa == NULL)
151 return NULL;
152
153 esmtpa->address = strdup(addr);
154 if (esmtpa->address == NULL) {
155 free(esmtpa);
156 return NULL;
157 }
158
159 if (orcpt != NULL) {
160 esmtpa->orcpt = strdup(orcpt);
161 if (esmtpa->orcpt == NULL) {
162 free(esmtpa->address);
163 free(esmtpa);
164 return NULL;
165 }
166 }
167 else
168 esmtpa->orcpt = NULL;
169
170 esmtpa->notify = notify;
171
172 return esmtpa;
173}
174
175static void esmtp_address_free(struct esmtp_address * addr)
176{
177 if (addr->orcpt)
178 free(addr->orcpt);
179 if (addr->address)
180 free(addr->address);
181
182 free(addr);
183}
184
185clist * esmtp_address_list_new()
186{
187 return clist_new();
188}
189
190void esmtp_address_list_free(clist * l)
191{
192 clist_foreach(l, (clist_func) esmtp_address_free, NULL);
193 clist_free(l);
194}
195
196int esmtp_address_list_add(clist * list, char * address,
197 int notify, char * orcpt)
198{
199 struct esmtp_address * esmtpa;
200 int r;
201
202 esmtpa = esmtp_address_new(address, notify, orcpt);
203 if (esmtpa == NULL)
204 return -1;
205
206 r = clist_append(list, esmtpa);
207 if (r < 0) {
208 esmtp_address_free(esmtpa);
209 return -1;
210 }
211
212 return 0;
213}
214
215clist * smtp_address_list_new()
216{
217 return esmtp_address_list_new();
218}
219
220int smtp_address_list_add(clist * list, char * address)
221{
222 return esmtp_address_list_add(list, address, 0, NULL);
223}
224
225void smtp_address_list_free(clist * l)
226{
227 esmtp_address_list_free(l);
228}