/src/pigeonhole/src/lib-sieve/plugins/body/ext-body-common.c
Line | Count | Source |
1 | | /* Copyright (c) Pigeonhole authors, see top-level COPYING file */ |
2 | | |
3 | | #include "lib.h" |
4 | | #include "mempool.h" |
5 | | #include "buffer.h" |
6 | | #include "array.h" |
7 | | #include "str.h" |
8 | | #include "istream.h" |
9 | | #include "mail-storage.h" |
10 | | |
11 | | #include "sieve-common.h" |
12 | | #include "sieve-stringlist.h" |
13 | | #include "sieve-code.h" |
14 | | #include "sieve-message.h" |
15 | | #include "sieve-interpreter.h" |
16 | | |
17 | | #include "ext-body-common.h" |
18 | | |
19 | | |
20 | | /* |
21 | | * Body part stringlist |
22 | | */ |
23 | | |
24 | | static int |
25 | | ext_body_stringlist_next_item(struct sieve_stringlist *_strlist, |
26 | | string_t **str_r); |
27 | | static void ext_body_stringlist_reset(struct sieve_stringlist *_strlist); |
28 | | |
29 | | struct ext_body_stringlist { |
30 | | struct sieve_stringlist strlist; |
31 | | |
32 | | struct sieve_message_part_data *body_parts; |
33 | | struct sieve_message_part_data *body_parts_iter; |
34 | | }; |
35 | | |
36 | | int ext_body_get_part_list(const struct sieve_runtime_env *renv, |
37 | | enum tst_body_transform transform, |
38 | | const char *const *content_types, |
39 | | struct sieve_stringlist **strlist_r) |
40 | 0 | { |
41 | 0 | static const char *const _no_content_types[] = { "", NULL }; |
42 | 0 | struct ext_body_stringlist *strlist; |
43 | 0 | struct sieve_message_part_data *body_parts = NULL; |
44 | 0 | int ret; |
45 | |
|
46 | 0 | *strlist_r = NULL; |
47 | |
|
48 | 0 | if (content_types == NULL) |
49 | 0 | content_types = _no_content_types; |
50 | |
|
51 | 0 | switch (transform) { |
52 | 0 | case TST_BODY_TRANSFORM_RAW: |
53 | 0 | if ((ret = sieve_message_body_get_raw(renv, &body_parts)) <= 0) |
54 | 0 | return ret; |
55 | 0 | break; |
56 | 0 | case TST_BODY_TRANSFORM_CONTENT: |
57 | 0 | if ((ret = sieve_message_body_get_content(renv, content_types, |
58 | 0 | &body_parts)) <= 0) |
59 | 0 | return ret; |
60 | 0 | break; |
61 | 0 | case TST_BODY_TRANSFORM_TEXT: |
62 | 0 | if ((ret = sieve_message_body_get_text(renv, &body_parts)) <= 0) |
63 | 0 | return ret; |
64 | 0 | break; |
65 | 0 | default: |
66 | 0 | i_unreached(); |
67 | 0 | } |
68 | | |
69 | 0 | strlist = t_new(struct ext_body_stringlist, 1); |
70 | 0 | strlist->strlist.runenv = renv; |
71 | 0 | strlist->strlist.next_item = ext_body_stringlist_next_item; |
72 | 0 | strlist->strlist.reset = ext_body_stringlist_reset; |
73 | 0 | strlist->body_parts = body_parts; |
74 | 0 | strlist->body_parts_iter = body_parts; |
75 | |
|
76 | 0 | *strlist_r = &strlist->strlist; |
77 | 0 | return SIEVE_EXEC_OK; |
78 | 0 | } |
79 | | |
80 | | static int |
81 | | ext_body_stringlist_next_item(struct sieve_stringlist *_strlist, |
82 | | string_t **str_r) |
83 | 0 | { |
84 | 0 | struct ext_body_stringlist *strlist = |
85 | 0 | (struct ext_body_stringlist *)_strlist; |
86 | |
|
87 | 0 | *str_r = NULL; |
88 | |
|
89 | 0 | if (strlist->body_parts_iter->content == NULL) |
90 | 0 | return 0; |
91 | | |
92 | 0 | *str_r = t_str_new_const(strlist->body_parts_iter->content, |
93 | 0 | strlist->body_parts_iter->size); |
94 | 0 | strlist->body_parts_iter++; |
95 | 0 | return 1; |
96 | 0 | } |
97 | | |
98 | | static void |
99 | | ext_body_stringlist_reset(struct sieve_stringlist *_strlist) |
100 | 0 | { |
101 | 0 | struct ext_body_stringlist *strlist = |
102 | 0 | (struct ext_body_stringlist *)_strlist; |
103 | |
|
104 | 0 | strlist->body_parts_iter = strlist->body_parts; |
105 | 0 | } |