7 /* DSN status parsing routines
9 /* #include <dsn_util.h>
11 /* #define DSN_SIZE ...
13 /* typedef struct { ... } DSN_BUF;
17 /* DSN_STAT dsn; /* RFC 3463 status */
18 /* const char *text; /* Free text */
22 /* DSN_SPLIT *dsn_split(dp, def_dsn, text)
24 /* const char *def_dsn;
27 /* char *dsn_prepend(def_dsn, text)
28 /* const char *def_dsn;
31 /* size_t dsn_valid(text)
34 /* void DSN_UPDATE(dsn_buf, dsn, len)
39 /* const char *DSN_CODE(dsn_buf)
42 /* char *DSN_CLASS(dsn_buf)
45 /* The functions in this module manipulate pairs of RFC 3463
46 /* status codes and descriptive free text.
48 /* dsn_split() splits text into an RFC 3463 status code and
49 /* descriptive free text. When the text does not start with
50 /* a status code, the specified default status code is used
51 /* instead. Whitespace before the optional status code or
52 /* text is skipped. dsn_split() returns a copy of the RFC
53 /* 3463 status code, and returns a pointer to (not copy of)
54 /* the remainder of the text. The result value is the first
57 /* dsn_prepend() prepends the specified default RFC 3463 status
58 /* code to the specified text if no status code is present in
59 /* the text. This function produces the same result as calling
60 /* concatenate() with the results from dsn_split(). The result
61 /* should be passed to myfree(). Whitespace before the optional
62 /* status code or text is skipped.
64 /* dsn_valid() returns the length of the RFC 3463 status code
65 /* at the beginning of text, or zero. It does not skip initial
70 /* Null-terminated default RFC 3463 status code that will be
71 /* used when the free text does not start with one.
73 /* Pointer to storage for copy of DSN status code, and for
74 /* pointer to free text.
76 /* Null-terminated RFC 3463 status code.
78 /* Null-terminated free text.
80 /* msg(3) diagnostics interface
82 /* Panic: invalid default DSN code.
86 /* The Secure Mailer license must be distributed with this software.
89 /* IBM T.J. Watson Research
91 /* Yorktown Heights, NY 10598, USA
101 /* Utility library. */
104 #include <mymalloc.h>
106 #include <stringops.h>
108 /* Global library. */
110 #include <dsn_util.h>
112 /* dsn_valid - check RFC 3463 enhanced status code, return length or zero */
114 size_t dsn_valid(const char *text
)
116 const unsigned char *cp
= (unsigned char *) text
;
119 /* First portion is one digit followed by dot. */
120 if ((cp
[0] != '2' && cp
[0] != '4' && cp
[0] != '5') || cp
[1] != '.')
123 /* Second portion is 1-3 digits followed by dot. */
125 if ((len
= strspn((char *) cp
, "0123456789")) < 1 || len
> DSN_DIGS2
129 /* Last portion is 1-3 digits followed by end-of-string or whitespace. */
131 if ((len
= strspn((char *) cp
, "0123456789")) < 1 || len
> DSN_DIGS3
132 || (cp
[len
] != 0 && !ISSPACE(cp
[len
])))
135 return (((char *) cp
- text
) + len
);
138 /* dsn_split - split text into DSN and text */
140 DSN_SPLIT
*dsn_split(DSN_SPLIT
*dp
, const char *def_dsn
, const char *text
)
142 const char *myname
= "dsn_split";
143 const char *cp
= text
;
147 * Look for an optional RFC 3463 enhanced status code.
149 * XXX If we want to enforce that the first digit of the status code in the
150 * text matches the default status code, then pipe_command() needs to be
151 * changed. It currently auto-detects the reply code without knowing in
152 * advance if the result will start with '4' or '5'.
156 if ((len
= dsn_valid(cp
)) > 0) {
157 strncpy(dp
->dsn
.data
, cp
, len
);
158 dp
->dsn
.data
[len
] = 0;
160 } else if ((len
= dsn_valid(def_dsn
)) > 0) {
161 strncpy(dp
->dsn
.data
, def_dsn
, len
);
162 dp
->dsn
.data
[len
] = 0;
164 msg_panic("%s: bad default status \"%s\"", myname
, def_dsn
);
168 * The remainder is free text.
177 /* dsn_prepend - prepend optional status to text, result on heap */
179 char *dsn_prepend(const char *def_dsn
, const char *text
)
183 dsn_split(&dp
, def_dsn
, text
);
184 return (concatenate(DSN_STATUS(dp
.dsn
), " ", dp
.text
, (char *) 0));