summaryrefslogtreecommitdiff
path: root/mailbox/mailer.c
blob: f2dd9c0c5184a639d12c35c6970f53b9444d6bb1 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001, 2004, 2005 Free Software Foundation, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
   Public License along with this library; if not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301 USA */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>

#include <mailutils/address.h>
#include <mailutils/debug.h>
#include <mailutils/errno.h>
#include <mailutils/iterator.h>
#include <mailutils/list.h>
#include <mailutils/observer.h>
#include <mailutils/property.h>
#include <mailutils/registrar.h>
#include <mailutils/stream.h>
#include <mailutils/url.h>
#include <mailutils/header.h>
#include <mailutils/mailbox.h>
#include <mailutils/message.h>

#include <mailer0.h>

static char *mailer_url_default;

/* FIXME: I'd like to check that the URL is valid, but that requires that the
   mailers already be registered! */
int
mailer_set_url_default (const char *url)
{
  char *n = NULL;

  if (!url)
    return EINVAL;

  if ((n = strdup (url)) == NULL)
    return ENOMEM;

  if (mailer_url_default)
    free (mailer_url_default);

  mailer_url_default = n;

  return 0;
}

int
mailer_get_url_default (const char **url)
{
  if (!url)
    return EINVAL;

  if (mailer_url_default)
    *url = mailer_url_default;
  else
    *url = MAILER_URL_DEFAULT;

  return 0;
}

int
mailer_create (mailer_t * pmailer, const char *name)
{
  record_t record;

  if (pmailer == NULL)
    return MU_ERR_OUT_PTR_NULL;

  if (name == NULL)
    mailer_get_url_default (&name);

  if (registrar_lookup (name, &record, MU_FOLDER_ATTRIBUTE_FILE))
    {
      int (*m_init) (mailer_t) = NULL;
      int (*u_init) (url_t) = NULL;

      record_get_mailer (record, &m_init);
      record_get_url (record, &u_init);
      if (m_init && u_init)
        {
	  int status;
	  url_t url;
	  mailer_t mailer;
	  
	  /* Allocate memory for mailer.  */
	  mailer = calloc (1, sizeof (*mailer));
	  if (mailer == NULL)
	    return ENOMEM;

	  status = monitor_create (&mailer->monitor, 0, mailer);
	  if (status)
	    {
	      mailer_destroy (&mailer);
	      return status;
	    }

	  /* Parse the url, it may be a bad one and we should bail out if this
	     failed.  */
	  if ((status = url_create (&url, name)) != 0
	      || (status = u_init (url)) != 0)
	    {
	      mailer_destroy (&mailer);
	      return status;
	    }
	  mailer->url = url;

	  status = m_init (mailer);
	  if (status)
	    mailer_destroy (&mailer);
	  else
	    *pmailer = mailer;

	  return status;
	}
    }

    return MU_ERR_MAILER_BAD_URL;
}

void
mailer_destroy (mailer_t * pmailer)
{
  if (pmailer && *pmailer)
    {
      mailer_t mailer = *pmailer;
      monitor_t monitor = mailer->monitor;

      if (mailer->observable)
	{
	  observable_notify (mailer->observable, MU_EVT_MAILER_DESTROY);
	  observable_destroy (&(mailer->observable), mailer);
	}

      /* Call the object destructor.  */
      if (mailer->_destroy)
	mailer->_destroy (mailer);

      monitor_wrlock (monitor);

      if (mailer->stream)
	{
	  /* FIXME: Should be the client responsability to close this?  */
	  /* stream_close (mailer->stream); */
	  stream_destroy (&(mailer->stream), mailer);
	}

      if (mailer->url)
	url_destroy (&(mailer->url));

      if (mailer->debug)
	mu_debug_destroy (&(mailer->debug), mailer);

      if (mailer->property)
	property_destroy (&(mailer->property), mailer);

      free (mailer);
      *pmailer = NULL;
      monitor_unlock (monitor);
      monitor_destroy (&monitor, mailer);
    }
}


/* -------------- stub functions ------------------- */

int
mailer_open (mailer_t mailer, int flag)
{
  if (mailer == NULL || mailer->_open == NULL)
    return ENOSYS;
  return mailer->_open (mailer, flag);
}

int
mailer_close (mailer_t mailer)
{
  if (mailer == NULL || mailer->_close == NULL)
    return ENOSYS;
  return mailer->_close (mailer);
}


int
mailer_check_from (address_t from)
{
  size_t n = 0;

  if (!from)
    return EINVAL;

  if (address_get_count (from, &n) || n != 1)
    return MU_ERR_MAILER_BAD_FROM;

  if (address_get_email_count (from, &n) || n == 0)
    return MU_ERR_MAILER_BAD_FROM;

  return 0;
}

int
mailer_check_to (address_t to)
{
  size_t count = 0;
  size_t emails = 0;
  size_t groups = 0;

  if (!to)
    return EINVAL;

  if (address_get_count (to, &count))
    return MU_ERR_MAILER_BAD_TO;

  if (address_get_email_count (to, &emails))
    return MU_ERR_MAILER_BAD_TO;

  if (emails == 0)
    return MU_ERR_MAILER_NO_RCPT_TO;

  if (address_get_group_count (to, &groups))
    return MU_ERR_MAILER_BAD_TO;

  if (count - emails - groups != 0)
    /* then not everything is a group or an email address */
    return MU_ERR_MAILER_BAD_TO;

  return 0;
}

static void
save_fcc (message_t msg)
{
  header_t hdr;
  size_t count = 0, i;
  char buf[512];
  
  if (message_get_header (msg, &hdr))
    return;

  if (header_get_value (hdr, MU_HEADER_FCC, NULL, 0, NULL))
    return;
  
  header_get_field_count (hdr, &count);
  for (i = 1; i <= count; i++)
    {
      mailbox_t mbox;
      
      header_get_field_name (hdr, i, buf, sizeof buf, NULL);
      if (strcasecmp (buf, MU_HEADER_FCC) == 0)
	{
	  if (header_get_field_value (hdr, i, buf, sizeof buf, NULL))
	    continue;
	  if (mailbox_create_default (&mbox, buf))
	    continue; /*FIXME: error message?? */
	  if (mailbox_open (mbox, MU_STREAM_RDWR|MU_STREAM_CREAT|MU_STREAM_APPEND) == 0)
	    {
	      mailbox_append_message (mbox, msg);
	      mailbox_flush (mbox, 0);
	    }
	  mailbox_close (mbox);
	  mailbox_destroy (&mbox);
	}
    }
}

int
mailer_send_message (mailer_t mailer, message_t msg,
		     address_t from, address_t to)
{
  int status;

  if (mailer == NULL || mailer->_send_message == NULL)
    return ENOSYS;

  /* Common API checking. */

  /* FIXME: this should be done in the concrete APIs, sendmail doesn't
     yet, though, so do it here. */
  if (from)
    {
      if ((status = mailer_check_from (from)) != 0)
	return status;
    }

  if (to)
    {
      if ((status = mailer_check_to (to)) != 0)
	return status;
    }
  
  save_fcc (msg);
  return mailer->_send_message (mailer, msg, from, to);
}

int
mailer_set_stream (mailer_t mailer, stream_t stream)
{
  if (mailer == NULL)
    return EINVAL;
  mailer->stream = stream;
  return 0;
}

int
mailer_get_stream (mailer_t mailer, stream_t * pstream)
{
  if (mailer == NULL)
    return EINVAL;
  if (pstream == NULL)
    return MU_ERR_OUT_PTR_NULL;
  *pstream = mailer->stream;
  return 0;
}

int
mailer_get_observable (mailer_t mailer, observable_t * pobservable)
{
  /* FIXME: I should check for invalid types */
  if (mailer == NULL)
    return EINVAL;
  if (pobservable == NULL)
    return MU_ERR_OUT_PTR_NULL;
  if (mailer->observable == NULL)
    {
      int status = observable_create (&(mailer->observable), mailer);
      if (status != 0)
	return status;
    }
  *pobservable = mailer->observable;
  return 0;
}

int
mailer_get_property (mailer_t mailer, property_t * pproperty)
{
  if (mailer == NULL)
    return EINVAL;
  if (pproperty == NULL)
    return MU_ERR_OUT_PTR_NULL;
  if (mailer->property == NULL)
    {
      int status = property_create (&(mailer->property), mailer);
      if (status != 0)
	return status;
    }
  *pproperty = mailer->property;
  return 0;
}

int
mailer_set_debug (mailer_t mailer, mu_debug_t debug)
{
  if (mailer == NULL)
    return EINVAL;
  mu_debug_destroy (&(mailer->debug), mailer);
  mailer->debug = debug;
  return 0;
}

int
mailer_get_debug (mailer_t mailer, mu_debug_t * pdebug)
{
  if (mailer == NULL)
    return EINVAL;
  if (pdebug == NULL)
    return MU_ERR_OUT_PTR_NULL;
  if (mailer->debug == NULL)
    {
      int status = mu_debug_create (&(mailer->debug), mailer);
      if (status != 0)
	return status;
    }
  *pdebug = mailer->debug;
  return 0;
}

int
mailer_get_url (mailer_t mailer, url_t * purl)
{
  if (!mailer)
    return EINVAL;
  if (!purl)
    return MU_ERR_OUT_PTR_NULL;
  *purl = mailer->url;
  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.