FreeCalypso > hg > falcon-mail-tools
annotate f-demime/msgstate.c @ 8:a92d0d59b669 default tip
f-demime: indicate X-backslash-escapes encoding in output
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 06 May 2023 17:00:23 +0000 |
parents | 612c4d0df768 |
children |
rev | line source |
---|---|
0
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module implements the message state machine, receiving input lines |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * and message start/end markers from the input processing main loop. |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <ctype.h> |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <stdio.h> |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <stdlib.h> |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <string.h> |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <strings.h> |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include "defs.h" |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 enum msg_state msg_state; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 enum msg_hdr_state hdr_state; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 unsigned mp_nest_level; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 char mp_boundaries[MAX_MP_NESTING][MAX_MP_BOUNDARY+1]; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 int mp_is_digest[MAX_MP_NESTING]; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 char cont_type_buf[HDR_BUF_SIZE], cont_te_buf[HDR_BUF_SIZE]; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 int got_cont_type, got_cont_te; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 void |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 start_entity_hdr() |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 msg_state = MSG_STATE_HEADER; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 hdr_state = HDR_STATE_BEGIN; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 got_cont_type = 0; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 got_cont_te = 0; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 void |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 begin_new_message() |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 mp_nest_level = 0; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 start_entity_hdr(); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 static void |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 emit_prefrom_warning() |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 static int done; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 if (done) |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 return; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 fprintf(stderr, |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 "f-demime warning: data present before the first From\n"); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 done = 1; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 static int |
4
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
50 check_mp_terminator(line, unterm) |
0
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 char *line; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 unsigned lev, bndlen; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 char *bnd, *cp; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 if (line[0] != '-' || line[1] != '-') |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 return(0); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 for (lev = 0; lev < mp_nest_level; lev++) { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 bnd = mp_boundaries[lev]; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 bndlen = strlen(bnd); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 if (strncmp(line+2, bnd, bndlen)) |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 continue; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 finish_msg_body(); |
4
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
64 fputs(line, stdout); |
0
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 cp = line + 2 + bndlen; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 if (!*cp || isspace(*cp)) { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 mp_nest_level = lev + 1; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 start_entity_hdr(); |
4
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
69 if (unterm) |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
70 msg_state = MSG_STATE_DELIM_UNTERM; |
0
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 return(1); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 } |
4
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
73 if (cp[0] != '-' || cp[1] != '-') { |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
74 if (!unterm) |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
75 puts("X-Fdemime-Error: invalid delimiter line"); |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
76 } |
0
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 mp_nest_level = lev; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 msg_state = MSG_STATE_BODY_PASS; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 return(1); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 return(0); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 void |
4
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
85 message_input_line(line, unterm, prev_unterm) |
0
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 char *line; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 { |
4
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
88 if (!prev_unterm && check_mp_terminator(line, unterm)) |
0
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 return; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 switch (msg_state) { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 case MSG_STATE_UNDEF: |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 emit_prefrom_warning(); |
4
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
93 fputs(line, stdout); |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
94 return; |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
95 case MSG_STATE_DELIM_UNTERM: |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
96 fputs(line, stdout); |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
97 if (!unterm) |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
98 msg_state = MSG_STATE_HEADER; |
0
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 return; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 case MSG_STATE_HEADER: |
4
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
101 header_input_line(line, unterm, prev_unterm); |
0
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 return; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 case MSG_STATE_BODY_PASS: |
4
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
104 fputs(line, stdout); |
0
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
105 return; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
106 case MSG_STATE_PTEXT_B64: |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 case MSG_STATE_BLOB_B64: |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
108 case MSG_STATE_B64_TO_QP: |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
109 base64_input_line(line); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
110 return; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
111 case MSG_STATE_PTEXT_QP: |
4
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
112 qpdec_input_line(line, unterm); |
0
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
113 return; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
114 default: |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
115 fprintf(stderr, |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
116 "f-demime internal error: bad state in message_input_line()\n"); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
117 abort(); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
118 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
119 } |