mu_message.c
10.5 KB
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
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "mu_scm.h"
long message_tag;
struct mu_message
{
message_t msg; /* Message itself */
SCM mbox; /* Mailbox it belong to */
};
/* SMOB functions: */
static SCM
mu_scm_message_mark (SCM message_smob)
{
struct mu_message *mum = (struct mu_message *) SCM_CDR (message_smob);
return mum->mbox;
}
static scm_sizet
mu_scm_message_free (SCM message_smob)
{
struct mu_message *mum = (struct mu_message *) SCM_CDR (message_smob);
free (mum);
/* NOTE: Currently there is no way for this function to return the
amount of memory *actually freed* by mailbox_destroy */
return sizeof (struct mu_message);
}
static char *
_get_envelope_sender (envelope_t env)
{
address_t addr;
char buffer[128];
if (envelope_sender (env, buffer, sizeof (buffer), NULL)
|| address_create (&addr, buffer))
return NULL;
if (address_get_email (addr, 1, buffer, sizeof (buffer), NULL))
{
address_destroy (&addr);
return NULL;
}
address_destroy (&addr);
return strdup (buffer);
}
static int
mu_scm_message_print (SCM message_smob, SCM port, scm_print_state * pstate)
{
struct mu_message *mum = (struct mu_message *) SCM_CDR (message_smob);
envelope_t env = NULL;
char buffer[128];
const char *p;
size_t m_size = 0, m_lines = 0;
struct tm tm;
mu_timezone tz;
message_get_envelope (mum->msg, &env);
scm_puts ("#<message ", port);
p = _get_envelope_sender (env);
scm_puts ("\"", port);
if (p)
{
scm_puts (p, port);
free ((void *) p);
}
else
scm_puts ("UNKNOWN", port);
envelope_date (env, buffer, sizeof (buffer), NULL);
p = buffer;
if (mu_parse_ctime_date_time (&p, &tm, &tz) == 0)
strftime (buffer, sizeof (buffer), "%a %b %e %H:%M", &tm);
else
strcpy (buffer, "UNKNOWN");
scm_puts ("\" \"", port);
scm_puts (buffer, port);
scm_puts ("\" ", port);
message_size (mum->msg, &m_size);
message_lines (mum->msg, &m_lines);
snprintf (buffer, sizeof (buffer), "%3ld %-5ld", m_lines, m_size);
scm_puts (buffer, port);
scm_puts (">", port);
return 1;
}
/* Internal calls: */
SCM
mu_scm_message_create (SCM owner, message_t msg)
{
struct mu_message *mum;
mum = scm_must_malloc (sizeof (struct mu_message), "message");
mum->msg = msg;
mum->mbox = owner;
SCM_RETURN_NEWSMOB (message_tag, mum);
}
const message_t
mu_scm_message_get (SCM MESG)
{
struct mu_message *mum = (struct mu_message *) SCM_CDR (MESG);
return mum->msg;
}
int
mu_scm_is_message (SCM scm)
{
return SCM_NIMP (scm) && SCM_CAR (scm) == message_tag;
}
/* ************************************************************************* */
/* Guile primitives */
SCM_DEFINE (mu_message_create, "mu-message-create", 0, 0, 0,
(),
"Creates an empty message.")
#define FUNC_NAME s_mu_message_create
{
message_t msg;
message_create (&msg, NULL);
return mu_scm_message_create (SCM_BOOL_F, msg);
}
#undef FUNC_NAME
SCM_DEFINE (mu_message_set_header, "mu-message-set-header", 3, 1, 0,
(SCM MESG, SCM HEADER, SCM VALUE, SCM REPLACE),
"Sets new VALUE to the header HEADER of the message MESG.\n"
"If the HEADER is already present in the message its value\n"
"is replaced with the suplied one iff the optional REPLACE is\n"
"#t. Otherwise new header is created and appended.")
#define FUNC_NAME s_mu_message_set_header
{
message_t msg;
header_t hdr;
int replace = 0;
SCM_ASSERT (mu_scm_is_message (MESG), MESG, SCM_ARG1, FUNC_NAME);
msg = mu_scm_message_get (MESG);
SCM_ASSERT (SCM_NIMP (HEADER) && SCM_STRINGP (HEADER),
HEADER, SCM_ARG2, FUNC_NAME);
SCM_ASSERT (SCM_NIMP (VALUE) && SCM_STRINGP (VALUE),
VALUE, SCM_ARG2, FUNC_NAME);
if (!SCM_UNBNDP (REPLACE))
{
SCM_ASSERT (SCM_IMP (REPLACE) && SCM_INUMP (REPLACE),
REPLACE, SCM_ARG3, FUNC_NAME);
replace = REPLACE == SCM_BOOL_T;
}
message_get_header (msg, &hdr);
header_set_value (hdr, SCM_CHARS (HEADER), strdup (SCM_CHARS (VALUE)),
replace);
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
SCM_DEFINE (mu_message_get_sender, "mu-message-get-sender", 1, 0, 0,
(SCM MESG),
"Returns the sender email address for the message MESG")
#define FUNC_NAME s_mu_message_get_sender
{
message_t msg;
envelope_t env = NULL;
SCM ret = SCM_BOOL_F;
SCM_ASSERT (mu_scm_is_message (MESG), MESG, SCM_ARG1, FUNC_NAME);
msg = mu_scm_message_get (MESG);
if (message_get_envelope (msg, &env) == 0)
{
char *p = _get_envelope_sender (env);
ret = scm_makfrom0str (p);
free (p);
}
return ret;
}
#undef FUNC_NAME
SCM_DEFINE (mu_message_get_header, "mu-message-get-header", 2, 0, 0,
(SCM MESG, SCM HEADER),
"Returns the header value of the HEADER in the MESG.")
#define FUNC_NAME s_mu_message_get_header
{
message_t msg;
header_t hdr;
char *value = NULL;
char *header_string;
SCM ret = SCM_BOOL_F;
SCM_ASSERT (mu_scm_is_message (MESG), MESG, SCM_ARG1, FUNC_NAME);
msg = mu_scm_message_get (MESG);
SCM_ASSERT (SCM_NIMP (HEADER) && SCM_STRINGP (HEADER),
HEADER, SCM_ARG2, FUNC_NAME);
header_string = SCM_CHARS (HEADER);
message_get_header (msg, &hdr);
if (header_aget_value (hdr, header_string, &value) == 0)
{
ret = scm_makfrom0str (value);
free (value);
}
return ret;
}
#undef FUNC_NAME
SCM_DEFINE (mu_message_get_header_fields, "mu-message-get-header-fields", 1, 0, 0,
(SCM MESG),
"Returns the list of headers in the MESG.")
#define FUNC_NAME s_mu_message_get_header_fields
{
size_t i, nfields = 0;
message_t msg;
header_t hdr = NULL;
SCM scm_first = SCM_EOL, scm_last;
SCM_ASSERT (mu_scm_is_message (MESG), MESG, SCM_ARG1, FUNC_NAME);
msg = mu_scm_message_get (MESG);
message_get_header (msg, &hdr);
header_get_field_count (hdr, &nfields);
for (i = 1; i <= nfields; i++)
{
SCM new_cell, scm_name, scm_value;
char *name, *value;
SCM_NEWCELL(new_cell);
header_aget_field_name (hdr, i, &name);
header_aget_field_value (hdr, i, &value);
scm_name = scm_makfrom0str (name);
scm_value = scm_makfrom0str (value);
SCM_SETCAR(new_cell, scm_cons(scm_name, scm_value));
if (scm_first == SCM_EOL)
{
scm_first = new_cell;
scm_last = scm_first;
}
else
{
SCM_SETCDR(scm_last, new_cell);
scm_last = new_cell;
}
}
if (scm_first != SCM_EOL)
SCM_SETCDR(scm_last, SCM_EOL);
return scm_first;
}
#undef FUNC_NAME
SCM_DEFINE (mu_message_set_header_fields, "mu-message-set-header-fields", 2, 1, 0,
(SCM MESG, SCM LIST, SCM REPLACE),
"Set the headers in the message MESG from LIST\n"
"LIST is a list of (cons HEADER VALUE)\n"
"Optional parameter REPLACE spceifies whether the new header\n"
"values should replace the headers already present in the\n"
"message.")
#define FUNC_NAME s_mu_message_set_header_fields
{
message_t msg;
header_t hdr;
SCM list;
int replace = 0;
SCM_ASSERT (mu_scm_is_message (MESG), MESG, SCM_ARG1, FUNC_NAME);
msg = mu_scm_message_get (MESG);
SCM_ASSERT (((SCM_IMP (LIST) && SCM_EOL == LIST) ||
(SCM_NIMP (LIST) && SCM_CONSP (LIST))),
LIST, SCM_ARG2, FUNC_NAME);
if (!SCM_UNBNDP (REPLACE))
{
SCM_ASSERT (SCM_IMP (REPLACE) && SCM_BOOLP (REPLACE),
REPLACE, SCM_ARG3, FUNC_NAME);
replace = REPLACE == SCM_BOOL_T;
}
else
for (list = LIST; list != SCM_EOL; list = SCM_CDR (list))
{
SCM cell = SCM_CAR (list);
SCM car, cdr;
SCM_ASSERT(SCM_NIMP(cell) && SCM_CONSP(cell),
cell, SCM_ARGn, FUNC_NAME);
car = SCM_CAR (cell);
cdr = SCM_CDR (cell);
SCM_ASSERT (SCM_NIMP (car) && SCM_STRINGP (car),
car, SCM_ARGn, FUNC_NAME);
SCM_ASSERT (SCM_NIMP (cdr) && SCM_STRINGP (cdr),
cdr, SCM_ARGn, FUNC_NAME);
header_set_value (hdr, SCM_CHARS (car), SCM_CHARS (cdr), replace);
}
return SCM_UNDEFINED;
}
#undef FUNC_NAME
SCM_DEFINE (mu_message_get_body, "mu-message-get-body", 1, 0, 0,
(SCM MESG),
"Returns the message body for the message MESG")
#define FUNC_NAME s_mu_message_get_body
{
message_t msg;
body_t body = NULL;
SCM_ASSERT (mu_scm_is_message (MESG), MESG, SCM_ARG1, FUNC_NAME);
msg = mu_scm_message_get (MESG);
if (message_get_body (msg, &body))
return SCM_BOOL_F;
return mu_scm_body_create (MESG, body);
}
#undef FUNC_NAME
SCM_DEFINE (mu_message_send, "mu-message-send", 1, 1, 0,
(SCM MESG, SCM MAILER),
"Sends the message MESG. Optional MAILER overrides default\n"
"mailer settings in mu-mailer.\n")
#define FUNC_NAME s_mu_message_send
{
char *mailer_name;
mailer_t mailer;
message_t msg;
int status;
SCM_ASSERT (mu_scm_is_message (MESG), MESG, SCM_ARG1, FUNC_NAME);
msg = mu_scm_message_get (MESG);
if (!SCM_UNBNDP (MAILER))
{
SCM_ASSERT (SCM_NIMP (MAILER) && SCM_STRINGP (MAILER),
MAILER, SCM_ARG2, FUNC_NAME);
mailer_name = SCM_CHARS (MAILER);
}
else
mailer_name = SCM_CHARS(_mu_scm_mailer);
if (mailer_create (&mailer, mailer_name))
{
return SCM_BOOL_F;
}
if (SCM_INUM(_mu_scm_debug))
{
debug_t debug = NULL;
mailer_get_debug (mailer, &debug);
debug_set_level (debug, MU_DEBUG_TRACE|MU_DEBUG_PROT);
}
status = mailer_open (mailer, MU_STREAM_RDWR);
if (status == 0)
{
status = mailer_send_message (mailer, msg);
mailer_close (mailer);
}
mailer_destroy (&mailer);
return status == 0 ? SCM_BOOL_T : SCM_BOOL_F;
}
#undef FUNC_NAME
/* Initialize the module */
void
mu_scm_message_init ()
{
message_tag = scm_make_smob_type ("message", sizeof (struct mu_message));
scm_set_smob_mark (message_tag, mu_scm_message_mark);
scm_set_smob_free (message_tag, mu_scm_message_free);
scm_set_smob_print (message_tag, mu_scm_message_print);
#include "mu_message.x"
}