aboutsummaryrefslogtreecommitdiff
path: root/src/builtin/mbox.bi
blob: df4f3ec25093b0af3b10d654063499b890320f1e (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
/* This file is part of Mailfromd.             -*- c -*-
   Copyright (C) 2008-2018 Sergey Poznyakoff

   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 3, 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, see <http://www.gnu.org/licenses/>. */

MF_BUILTIN_MODULE

#include "msg.h"
#include "global.h"

static size_t nmboxes = MAX_MBOXES;

static struct mu_cfg_param mbox_cfg_param[] = {
	{ "max-open-mailboxes", mu_c_size, &nmboxes, 0, NULL,
	  N_("Maximum number of mailboxes to open simultaneously.") },
	{ NULL }
};

static int
do_close(void *item, void *data)
{
	bi_close_message(item);
	return 0;
}

static void
close_mbox(struct mf_mbox *mp)
{
	if (mp->mbox) {
		mu_mailbox_close(mp->mbox);
		mu_mailbox_destroy(&mp->mbox);
		mu_list_foreach(mp->msglist, do_close, NULL);
		mu_list_destroy(&mp->msglist);
		memset(mp, 0, sizeof mp[0]);
	}
}

static void *
alloc_mboxes()
{
	return mu_calloc(nmboxes, sizeof(struct mf_mbox));
}

static void
destroy_mboxes(void *data)
{
	struct mf_mbox *mtab = data;
	struct mf_mbox *p;
	for (p = mtab; p < mtab + nmboxes; p++) {
		close_mbox(p);
	}
	free(mtab);
}

MF_DECLARE_DATA(MBOXTAB, alloc_mboxes, destroy_mboxes)

static int
find_slot(struct mf_mbox *tab)
{
	int i;
	for (i = 0; i < nmboxes; i++)
		if (tab[i].mbox == NULL)
			return i;
	return -1;
}

/* number mailbox_open(string url) */
MF_DEFUN(mailbox_open, NUMBER, STRING url, OPTIONAL, STRING mode, STRING perms)
{
	int rc;
	int md;
	struct mf_mbox *mbtab = MF_GET_DATA;
	struct mf_mbox *mp;
	int flags;
	char *p;

	md = find_slot(mbtab);
	MF_ASSERT(md >= 0,
		  mfe_failure,
		  _("no more mailboxes available"));
	MF_DEBUG(10, ("opening mailbox %s", url));
	mp = mbtab + md;

	flags = 0;
	for (p = MF_OPTVAL(mode, "r"); *p; p++) {
		switch (*p) {
		case 'a':
			flags |= MU_STREAM_APPEND;
			break;
			
		case 'r':
			flags |= MU_STREAM_READ;
			break;
			
		case 'w':
			flags |= MU_STREAM_WRITE;
			break;
			
		case '+':
			if (flags & MU_STREAM_READ)
				flags |= MU_STREAM_WRITE;
			else if (flags & MU_STREAM_WRITE)
				/* FIXME: should truncate as well */
				flags |= MU_STREAM_READ|MU_STREAM_CREAT;
			else if (flags & MU_STREAM_APPEND) {
				flags |= MU_STREAM_RDWR | MU_STREAM_CREAT;
			} else
				MF_THROW(mfe_range,
					 _("incorrect mode near `%s'"),
					 p);
			break;

		default:
			MF_THROW(mfe_range,
				 _("incorrect mode near `%s'"),
				 p);
		}
	}

	/* FIXME: MU: This is ridiculous! */
	if ((flags & (MU_STREAM_READ|MU_STREAM_WRITE))
	    == (MU_STREAM_READ|MU_STREAM_WRITE)) {
		    flags &= ~(MU_STREAM_READ|MU_STREAM_WRITE);
		    flags |= MU_STREAM_RDWR;
	}
	    
	if (MF_DEFINED(perms)) {
		/* If MU_STREAM_IRGRP is defined, the remaining three
		   permissions flags are defined as well.
		   FIXME: Remove ifdef when MU 2.0 is out. */
#ifdef MU_STREAM_IRGRP
		int f;
		const char *p;
		MF_ASSERT(mu_parse_stream_perm_string(&f, MF_OPTVAL(perms), &p)
			  == 0,
			  mfe_range, /* FIXME: introduce mfe_inval? */
			  _("invalid permissions (near %s)"), p);
		flags |= f;
#else
		MF_THROW(mfe_failure,
			 _("this version of Mailutils does not support "
			   "configurable mailbox permissions"));
#endif
	}
		
	rc = mu_mailbox_create(&mp->mbox, url);
	MF_ASSERT(rc == 0,
		  mfe_failure,
		  _("cannot create mailbox `%s': %s"), url,
		  mu_strerror(rc));
	rc = mu_mailbox_open(mp->mbox, flags);
	if (rc) {
		mu_mailbox_destroy(&mp->mbox);
		MF_THROW(mfe_failure,
			 _("cannot open mailbox `%s': %s"), url,
			 mu_strerror(rc));
	}
	mu_list_create(&mp->msglist);
	
	MF_RETURN(md);
}
END

/* void mailbox_close(number mbx) */
MF_DEFUN(mailbox_close, VOID, NUMBER md)
{
	struct mf_mbox *mbtab = MF_GET_DATA;

	MF_ASSERT(md >= 0 && md < nmboxes,
		  mfe_range,
		  _("invalid mailbox descriptor"));
	close_mbox(mbtab + md);
}
END

m4_define([<DCL_MBOX>],[<
	struct mf_mbox *mbtab = MF_GET_DATA;
	struct mf_mbox *$1;
			
	MF_ASSERT($2 >= 0 && $2 < nmboxes,
		  mfe_range,
		  _("invalid mailbox descriptor"));
	$1 = mbtab + $2;
	MF_ASSERT($1->mbox,
		  mfe_failure,
		  _("mailbox not open"))
>])
	
/* number mailbox_messages_count(number mbx) */
MF_DEFUN(mailbox_messages_count, NUMBER, NUMBER nmbx)
{
	size_t count;
	int rc;
	DCL_MBOX(mp, nmbx);
	
	rc = mu_mailbox_messages_count(mp->mbox, &count);
	MF_ASSERT(rc == 0,
		  mfe_failure,
		  "%s",
		  mu_strerror(rc));
	MF_RETURN(count);
}
END

/* number mailbox_get_message(number mbx, number msg-no) */
MF_DEFUN(mailbox_get_message, NUMBER, NUMBER nmbx, NUMBER msgno)
{
	int rc;
	mu_message_t msg;
	DCL_MBOX(mp, nmbx);

	rc = mu_mailbox_get_message(mp->mbox, msgno, &msg);
	MF_ASSERT(rc == 0,
		  mfe_failure,
		  "%s",
		  mu_strerror(rc));
	rc = bi_message_register(env, mp->msglist, msg, MF_MSG_MAILBOX);
	MF_ASSERT(rc >= 0,
		  mfe_failure,
		  _("no more message slots available"));
	MF_RETURN(rc);
}
END

/* void mailbox_append_message(number mbx, number msg-no) */
MF_DEFUN(mailbox_append_message, VOID, NUMBER nmbx, NUMBER msgno)
{
	int rc;
	mu_message_t msg;
	DCL_MBOX(mp, nmbx);

	msg = bi_message_from_descr(env, msgno);
	rc = mu_mailbox_append_message(mp->mbox, msg);
	MF_ASSERT(rc == 0,
		  mfe_failure,
		  _("cannot append message: %s"),
		  mu_strerror(rc));
}
END

MF_INIT([<
	 mf_add_runtime_params(mbox_cfg_param);
	 >])

Return to:

Send suggestions and report system problems to the System administrator.