1 #line 982 "../../src/builtin/snarf.m4"
2 /* -*- buffer-read-only: t -*- vi: set ro:
3 THIS FILE IS GENERATED AUTOMATICALLY. PLEASE DO NOT EDIT.
4 */
5 #line 982
6 #ifdef HAVE_CONFIG_H
7 #line 982
8 # include <config.h>
9 #line 982
10 #endif
11 #line 982
12 #include <sys/types.h>
13 #line 982
14
15 #line 982
16 #include "mailfromd.h"
17 #line 982
18 #include "prog.h"
19 #line 982
20 #include "builtin.h"
21 #line 982
22
23 #line 92 "mbox.bi"
24 static mu_debug_handle_t debug_handle;
25 #line 982 "../../src/builtin/snarf.m4"
26
27 #line 1022 "../../src/builtin/snarf.m4"
28
29 /* End of snarf.m4 */
30 #line 1 "mbox.bi"
31 /* This file is part of Mailfromd. -*- c -*-
32 Copyright (C) 2008-2021 Sergey Poznyakoff
33
34 This program is free software; you can redistribute it and/or modify
35 it under the terms of the GNU General Public License as published by
36 the Free Software Foundation; either version 3, or (at your option)
37 any later version.
38
39 This program is distributed in the hope that it will be useful,
40 but WITHOUT ANY WARRANTY; without even the implied warranty of
41 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 GNU General Public License for more details.
43
44 You should have received a copy of the GNU General Public License
45 along with this program. If not, see <http://www.gnu.org/licenses/>. */
46
47
48
49 #include "msg.h"
50 #include "global.h"
51
52 static size_t nmboxes = MAX_MBOXES;
53
54 static struct mu_cfg_param mbox_cfg_param[] = {
55 { "max-open-mailboxes", mu_c_size, &nmboxes, 0, NULL,
56 N_("Maximum number of mailboxes to open simultaneously.") },
57 { NULL }
58 };
59
60 static int
do_close(void * item,void * data)61 do_close(void *item, void *data)
62 {
63 bi_close_message(item);
64 return 0;
65 }
66
67 static void
close_mbox(struct mf_mbox * mp)68 close_mbox(struct mf_mbox *mp)
69 {
70 if (mp->mbox) {
71 mu_mailbox_close(mp->mbox);
72 mu_mailbox_destroy(&mp->mbox);
73 mu_list_foreach(mp->msglist, do_close, NULL);
74 mu_list_destroy(&mp->msglist);
75 memset(mp, 0, sizeof mp[0]);
76 }
77 }
78
79 static void *
alloc_mboxes()80 alloc_mboxes()
81 {
82 return mu_calloc(nmboxes, sizeof(struct mf_mbox));
83 }
84
85 static void
destroy_mboxes(void * data)86 destroy_mboxes(void *data)
87 {
88 struct mf_mbox *mtab = data;
89 struct mf_mbox *p;
90 for (p = mtab; p < mtab + nmboxes; p++) {
91 close_mbox(p);
92 }
93 free(mtab);
94 }
95
96
97 #line 66
98
99 #line 66
100 static int MBOXTAB_id;
101 #line 66 "mbox.bi"
102
103
104 static int
find_slot(struct mf_mbox * tab)105 find_slot(struct mf_mbox *tab)
106 {
107 int i;
108 for (i = 0; i < nmboxes; i++)
109 if (tab[i].mbox == NULL)
110 return i;
111 return -1;
112 }
113
114 /* number mailbox_open(string url) */
115 void
116 #line 79
bi_mailbox_open(eval_environ_t env)117 bi_mailbox_open(eval_environ_t env)
118 #line 79
119
120 #line 79
121
122 #line 79 "mbox.bi"
123 {
124 #line 79
125
126 #line 79
127
128 #line 79
129 long __bi_argcnt;
130 #line 79
131 char * url;
132 #line 79
133 char * mode;
134 #line 79
135 char * perms;
136 #line 79
137
138 #line 79
139 get_string_arg(env, 1, &url);
140 #line 79
141 get_string_arg(env, 2, &mode);
142 #line 79
143 get_string_arg(env, 3, &perms);
144 #line 79
145
146 #line 79
147 get_numeric_arg(env, 0, &__bi_argcnt);
148 #line 79
149 adjust_stack(env, __bi_argcnt + 1);
150 #line 79
151
152 #line 79
153
154 #line 79
155 if (builtin_module_trace(BUILTIN_IDX_mbox))
156 #line 79
157 prog_trace(env, "mailbox_open %s %s %s",url, ((__bi_argcnt > 1) ? mode : ""), ((__bi_argcnt > 2) ? perms : ""));;
158 #line 79
159
160 {
161 int rc;
162 int md;
163 struct mf_mbox *mbtab = env_get_builtin_priv(env,MBOXTAB_id);
164 struct mf_mbox *mp;
165 int flags;
166 char *p;
167
168 md = find_slot(mbtab);
169 if (!(md >= 0))
170 #line 89
171 (
172 #line 89
173 env_throw_bi(env, mfe_failure, "mailbox_open", _("no more mailboxes available"))
174 #line 89
175 )
176 #line 91
177 ;
178
179 #line 92 "mbox.bi"
180
181 #line 92
182 mu_debug(debug_handle, MU_DEBUG_TRACE1,("opening mailbox %s", url));
183 mp = mbtab + md;
184
185 flags = 0;
186 for (p = ((__bi_argcnt > 1) ? mode : "r"); *p; p++) {
187 switch (*p) {
188 case 'a':
189 flags |= MU_STREAM_APPEND;
190 break;
191
192 case 'r':
193 flags |= MU_STREAM_READ;
194 break;
195
196 case 'w':
197 flags |= MU_STREAM_WRITE;
198 break;
199
200 case '+':
201 if (flags & MU_STREAM_READ)
202 flags |= MU_STREAM_WRITE;
203 else if (flags & MU_STREAM_WRITE)
204 /* FIXME: should truncate as well */
205 flags |= MU_STREAM_READ|MU_STREAM_CREAT;
206 else if (flags & MU_STREAM_APPEND) {
207 flags |= MU_STREAM_RDWR | MU_STREAM_CREAT;
208 } else
209 (
210 #line 119
211 env_throw_bi(env, mfe_range, "mailbox_open", _("incorrect mode near `%s'"),p)
212 #line 119
213 );
214 #line 122
215 break;
216
217 default:
218 (
219 #line 125
220 env_throw_bi(env, mfe_range, "mailbox_open", _("incorrect mode near `%s'"),p)
221 #line 125
222 );
223 #line 128
224 }
225 }
226
227 if ((__bi_argcnt > 2)) {
228 int f;
229 const char *p;
230 if (!(mu_parse_stream_perm_string(&f, ((__bi_argcnt > 2) ? perms : 0), &p)
231 #line 134
232 == 0))
233 #line 134
234 (
235 #line 134
236 env_throw_bi(env, mfe_range, "mailbox_open", /* FIXME: introduce mfe_inval? */
237 _("invalid permissions (near %s)"),p)
238 #line 134
239 )
240 #line 137
241 ;
242 flags |= f;
243 }
244
245 rc = mu_mailbox_create(&mp->mbox, url);
246 if (!(rc == 0))
247 #line 142
248 (
249 #line 142
250 env_throw_bi(env, mfe_failure, "mailbox_open", _("cannot create mailbox `%s': %s"),url,mu_strerror(rc))
251 #line 142
252 )
253 #line 145
254 ;
255 rc = mu_mailbox_open(mp->mbox, flags);
256 if (rc) {
257 mu_mailbox_destroy(&mp->mbox);
258 (
259 #line 149
260 env_throw_bi(env, mfe_failure, "mailbox_open", _("cannot open mailbox `%s': %s"),url,mu_strerror(rc))
261 #line 149
262 );
263 #line 152
264 }
265 mu_list_create(&mp->msglist);
266
267
268 #line 155
269 do {
270 #line 155
271 push(env, (STKVAL)(mft_number)(md));
272 #line 155
273 goto endlab;
274 #line 155
275 } while (0);
276 }
277 endlab:
278 #line 157
279 env_function_cleanup_flush(env, NULL);
280 #line 157
281 return;
282 #line 157
283 }
284
285 /* void mailbox_close(number mbx) */
286 void
287 #line 160
bi_mailbox_close(eval_environ_t env)288 bi_mailbox_close(eval_environ_t env)
289 #line 160
290
291 #line 160
292
293 #line 160 "mbox.bi"
294 {
295 #line 160
296
297 #line 160
298
299 #line 160
300
301 #line 160
302 long md;
303 #line 160
304
305 #line 160
306 get_numeric_arg(env, 0, &md);
307 #line 160
308
309 #line 160
310
311 #line 160
312 adjust_stack(env, 1);
313 #line 160
314
315 #line 160
316
317 #line 160
318 if (builtin_module_trace(BUILTIN_IDX_mbox))
319 #line 160
320 prog_trace(env, "mailbox_close %lu",md);;
321 #line 160
322
323 {
324 struct mf_mbox *mbtab = env_get_builtin_priv(env,MBOXTAB_id);
325
326 if (!(md >= 0 && md < nmboxes))
327 #line 164
328 (
329 #line 164
330 env_throw_bi(env, mfe_range, "mailbox_close", _("invalid mailbox descriptor"))
331 #line 164
332 )
333 #line 166
334 ;
335 close_mbox(mbtab + md);
336 }
337
338 #line 169
339 env_function_cleanup_flush(env, NULL);
340 #line 169
341 return;
342 #line 169
343 }
344
345 #line 182
346
347
348 /* number mailbox_messages_count(number mbx) */
349 void
350 #line 185
bi_mailbox_messages_count(eval_environ_t env)351 bi_mailbox_messages_count(eval_environ_t env)
352 #line 185
353
354 #line 185
355
356 #line 185 "mbox.bi"
357 {
358 #line 185
359
360 #line 185
361
362 #line 185
363
364 #line 185
365 long nmbx;
366 #line 185
367
368 #line 185
369 get_numeric_arg(env, 0, &nmbx);
370 #line 185
371
372 #line 185
373
374 #line 185
375 adjust_stack(env, 1);
376 #line 185
377
378 #line 185
379
380 #line 185
381 if (builtin_module_trace(BUILTIN_IDX_mbox))
382 #line 185
383 prog_trace(env, "mailbox_messages_count %lu",nmbx);;
384 #line 185
385
386 {
387 size_t count;
388 int rc;
389
390 #line 189
391 struct mf_mbox *mbtab = env_get_builtin_priv(env,MBOXTAB_id);
392 #line 189
393 struct mf_mbox *mp;
394 #line 189
395
396 #line 189
397 if (!(nmbx >= 0 && nmbx < nmboxes))
398 #line 189
399 (
400 #line 189
401 env_throw_bi(env, mfe_range, "mailbox_messages_count", _("invalid mailbox descriptor"))
402 #line 189
403 )
404 #line 189
405 ;
406 #line 189
407 mp = mbtab + nmbx;
408 #line 189
409 if (!(mp->mbox))
410 #line 189
411 (
412 #line 189
413 env_throw_bi(env, mfe_failure, "mailbox_messages_count", _("mailbox not open"))
414 #line 189
415 )
416 #line 189
417
418 #line 189
419 ;
420
421 rc = mu_mailbox_messages_count(mp->mbox, &count);
422 if (!(rc == 0))
423 #line 192
424 (
425 #line 192
426 env_throw_bi(env, mfe_failure, "mailbox_messages_count", "%s",mu_strerror(rc))
427 #line 192
428 )
429 #line 195
430 ;
431
432 #line 196
433 do {
434 #line 196
435 push(env, (STKVAL)(mft_number)(count));
436 #line 196
437 goto endlab;
438 #line 196
439 } while (0);
440 }
441 endlab:
442 #line 198
443 env_function_cleanup_flush(env, NULL);
444 #line 198
445 return;
446 #line 198
447 }
448
449 /* number mailbox_get_message(number mbx, number msg-no) */
450 void
451 #line 201
bi_mailbox_get_message(eval_environ_t env)452 bi_mailbox_get_message(eval_environ_t env)
453 #line 201
454
455 #line 201
456
457 #line 201 "mbox.bi"
458 {
459 #line 201
460
461 #line 201
462
463 #line 201
464
465 #line 201
466 long nmbx;
467 #line 201
468 long msgno;
469 #line 201
470
471 #line 201
472 get_numeric_arg(env, 0, &nmbx);
473 #line 201
474 get_numeric_arg(env, 1, &msgno);
475 #line 201
476
477 #line 201
478
479 #line 201
480 adjust_stack(env, 2);
481 #line 201
482
483 #line 201
484
485 #line 201
486 if (builtin_module_trace(BUILTIN_IDX_mbox))
487 #line 201
488 prog_trace(env, "mailbox_get_message %lu %lu",nmbx, msgno);;
489 #line 201
490
491 {
492 int rc;
493 mu_message_t msg;
494
495 #line 205
496 struct mf_mbox *mbtab = env_get_builtin_priv(env,MBOXTAB_id);
497 #line 205
498 struct mf_mbox *mp;
499 #line 205
500
501 #line 205
502 if (!(nmbx >= 0 && nmbx < nmboxes))
503 #line 205
504 (
505 #line 205
506 env_throw_bi(env, mfe_range, "mailbox_get_message", _("invalid mailbox descriptor"))
507 #line 205
508 )
509 #line 205
510 ;
511 #line 205
512 mp = mbtab + nmbx;
513 #line 205
514 if (!(mp->mbox))
515 #line 205
516 (
517 #line 205
518 env_throw_bi(env, mfe_failure, "mailbox_get_message", _("mailbox not open"))
519 #line 205
520 )
521 #line 205
522
523 #line 205
524 ;
525
526 rc = mu_mailbox_get_message(mp->mbox, msgno, &msg);
527 if (!(rc == 0))
528 #line 208
529 (
530 #line 208
531 env_throw_bi(env, mfe_failure, "mailbox_get_message", "%s",mu_strerror(rc))
532 #line 208
533 )
534 #line 211
535 ;
536 rc = bi_message_register(env, mp->msglist, msg, MF_MSG_MAILBOX);
537 if (!(rc >= 0))
538 #line 213
539 (
540 #line 213
541 env_throw_bi(env, mfe_failure, "mailbox_get_message", _("no more message slots available"))
542 #line 213
543 )
544 #line 215
545 ;
546
547 #line 216
548 do {
549 #line 216
550 push(env, (STKVAL)(mft_number)(rc));
551 #line 216
552 goto endlab;
553 #line 216
554 } while (0);
555 }
556 endlab:
557 #line 218
558 env_function_cleanup_flush(env, NULL);
559 #line 218
560 return;
561 #line 218
562 }
563
564 /* void mailbox_append_message(number mbx, number msg-no) */
565 void
566 #line 221
bi_mailbox_append_message(eval_environ_t env)567 bi_mailbox_append_message(eval_environ_t env)
568 #line 221
569
570 #line 221
571
572 #line 221 "mbox.bi"
573 {
574 #line 221
575
576 #line 221
577
578 #line 221
579
580 #line 221
581 long nmbx;
582 #line 221
583 long msgno;
584 #line 221
585
586 #line 221
587 get_numeric_arg(env, 0, &nmbx);
588 #line 221
589 get_numeric_arg(env, 1, &msgno);
590 #line 221
591
592 #line 221
593
594 #line 221
595 adjust_stack(env, 2);
596 #line 221
597
598 #line 221
599
600 #line 221
601 if (builtin_module_trace(BUILTIN_IDX_mbox))
602 #line 221
603 prog_trace(env, "mailbox_append_message %lu %lu",nmbx, msgno);;
604 #line 221
605
606 {
607 int rc;
608 mu_message_t msg;
609
610 #line 225
611 struct mf_mbox *mbtab = env_get_builtin_priv(env,MBOXTAB_id);
612 #line 225
613 struct mf_mbox *mp;
614 #line 225
615
616 #line 225
617 if (!(nmbx >= 0 && nmbx < nmboxes))
618 #line 225
619 (
620 #line 225
621 env_throw_bi(env, mfe_range, "mailbox_append_message", _("invalid mailbox descriptor"))
622 #line 225
623 )
624 #line 225
625 ;
626 #line 225
627 mp = mbtab + nmbx;
628 #line 225
629 if (!(mp->mbox))
630 #line 225
631 (
632 #line 225
633 env_throw_bi(env, mfe_failure, "mailbox_append_message", _("mailbox not open"))
634 #line 225
635 )
636 #line 225
637
638 #line 225
639 ;
640
641 msg = bi_message_from_descr(env, msgno);
642 rc = mu_mailbox_append_message(mp->mbox, msg);
643 if (!(rc == 0))
644 #line 229
645 (
646 #line 229
647 env_throw_bi(env, mfe_failure, "mailbox_append_message", _("cannot append message: %s"),mu_strerror(rc))
648 #line 229
649 )
650 #line 232
651 ;
652 }
653
654 #line 234
655 env_function_cleanup_flush(env, NULL);
656 #line 234
657 return;
658 #line 234
659 }
660
661
662 #line 982 "../../src/builtin/snarf.m4"
663
664 #line 982
665
666 #line 982
667
668 #line 982
669 void
670 #line 982
mbox_init_builtin(void)671 mbox_init_builtin(void)
672 #line 982
673 {
674 #line 982
675 debug_handle = mu_debug_register_category("bi_mbox");
676 #line 982
677
678 #line 982
679 #line 66 "mbox.bi"
680 MBOXTAB_id = builtin_priv_register(alloc_mboxes, destroy_mboxes,
681 #line 66
682 NULL);
683 #line 79 "mbox.bi"
684 va_builtin_install_ex("mailbox_open", bi_mailbox_open, 0, dtype_number, 3, 2, 0|0, dtype_string, dtype_string, dtype_string);
685 #line 160 "mbox.bi"
686 va_builtin_install_ex("mailbox_close", bi_mailbox_close, 0, dtype_unspecified, 1, 0, 0|0, dtype_number);
687 #line 185 "mbox.bi"
688 va_builtin_install_ex("mailbox_messages_count", bi_mailbox_messages_count, 0, dtype_number, 1, 0, 0|0, dtype_number);
689 #line 201 "mbox.bi"
690 va_builtin_install_ex("mailbox_get_message", bi_mailbox_get_message, 0, dtype_number, 2, 0, 0|0, dtype_number, dtype_number);
691 #line 221 "mbox.bi"
692 va_builtin_install_ex("mailbox_append_message", bi_mailbox_append_message, 0, dtype_unspecified, 2, 0, 0|0, dtype_number, dtype_number);
693
694 #line 982 "../../src/builtin/snarf.m4"
695
696 #line 982
697 mf_add_runtime_params(mbox_cfg_param);
698 #line 982
699
700 #line 982
701 }
702 #line 982 "../../src/builtin/snarf.m4"
703
704