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